Skip to content

Commit

Permalink
Cleanup pending assignment map removal in UniqueId.
Browse files Browse the repository at this point in the history
Fix a possible bug in the race condition return logic where the wrong deferred
was being returned.

Signed-off-by: unknown <clarsen@euphoriaaudio.com>
  • Loading branch information
manolama committed Dec 30, 2013
1 parent 4d008a6 commit 21e310a
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/uid/UniqueId.java
Expand Up @@ -383,6 +383,7 @@ public Object call(final Object arg) {
class ErrBack implements Callback<Object, Exception> {
public Object call(final Exception e) throws Exception {
assignment.callback(e);
LOG.warn("Failed pending assignment for: " + name);
return assignment;
}
}
Expand Down Expand Up @@ -496,22 +497,22 @@ private Deferred<byte[]> done(final Object arg) {
LOG.warn("Race condition: tried to assign ID " + id + " to "
+ kind() + ":" + name + ", but CAS failed on "
+ forwardMapping() + ", which indicates this UID must have"
+ " been allocated concurrently by another TSD. So ID "
+ id + " was leaked.");
+ " been allocated concurrently by another TSD or thread. "
+ "So ID " + id + " was leaked.");
// If two TSDs attempted to allocate a UID for the same name at the
// same time, they would both have allocated a UID, and created a
// reverse mapping, and upon getting here, only one of them would
// manage to CAS this KV into existence. The one that loses the
// race will retry and discover the UID assigned by the winner TSD,
// and a UID will have been wasted in the process. No big deal.

class GetIdCB implements Callback<Deferred<byte[]>, byte[]> {
public Deferred<byte[]> call(final byte[] row) throws Exception {
class GetIdCB implements Callback<Object, byte[]> {
public Object call(final byte[] row) throws Exception {
assignment.callback(row);
return assignment;
return null;
}
}
return getIdAsync(name).addCallbackDeferring(new GetIdCB());
getIdAsync(name).addCallback(new GetIdCB());
return assignment;
}

cacheMapping(name, row);
Expand Down Expand Up @@ -576,18 +577,25 @@ public byte[] getOrCreateId(final String name) throws HBaseException {
try {
return assignment.joinUninterruptibly();
} catch (Exception e1) {
throw new RuntimeException("Should never be here", e);
throw new RuntimeException("Should never be here", e1);
}
}

// start the assignment dance after stashing the deferred
byte[] uid = null;
try {
return new UniqueIdAllocator(name, assignment).tryAllocate().joinUninterruptibly();
uid = new UniqueIdAllocator(name, assignment).tryAllocate().joinUninterruptibly();
} catch (RuntimeException e1) {
throw e1;
} catch (Exception e1) {
throw new RuntimeException("Should never be here", e);
} finally {
LOG.info("Completed pending assignment for: " + name);
synchronized (pending_assignments) {
pending_assignments.remove(name);
}
}
return uid;
} catch (Exception e) {
throw new RuntimeException("Should never be here", e);
}
Expand Down

0 comments on commit 21e310a

Please sign in to comment.