in curator-recipes/src/main/java/org/apache/curator/framework/recipes/barriers/DistributedDoubleBarrier.java [173:247]
private boolean internalLeave(long startMs, boolean hasMaxWait, long maxWaitMs) throws Exception {
String ourPathName = ZKPaths.getNodeFromPath(ourPath);
boolean ourNodeShouldExist = true;
boolean result = true;
for (; ; ) {
if (connectionLost.get()) {
throw new KeeperException.ConnectionLossException();
}
List<String> children;
try {
children = client.getChildren().forPath(barrierPath);
} catch (KeeperException.NoNodeException dummy) {
children = Lists.newArrayList();
}
children = filterAndSortChildren(children);
if (children.isEmpty()) {
break;
}
int ourIndex = children.indexOf(ourPathName);
if ((ourIndex < 0) && ourNodeShouldExist) {
if (connectionLost.get()) {
break; // connection was lost but we've reconnected. However, our ephemeral node is gone
} else {
throw new IllegalStateException(String.format("Our path (%s) is missing", ourPathName));
}
}
if (children.size() == 1) {
if (ourNodeShouldExist && !children.get(0).equals(ourPathName)) {
throw new IllegalStateException(
String.format("Last path (%s) is not ours (%s)", children.get(0), ourPathName));
}
checkDeleteOurPath(ourNodeShouldExist);
break;
}
Stat stat;
boolean IsLowestNode = (ourIndex == 0);
if (IsLowestNode) {
String highestNodePath = ZKPaths.makePath(barrierPath, children.get(children.size() - 1));
stat = client.checkExists().usingWatcher(watcher).forPath(highestNodePath);
} else {
String lowestNodePath = ZKPaths.makePath(barrierPath, children.get(0));
stat = client.checkExists().usingWatcher(watcher).forPath(lowestNodePath);
checkDeleteOurPath(ourNodeShouldExist);
ourNodeShouldExist = false;
}
if (stat != null) {
if (hasMaxWait) {
long elapsed = System.currentTimeMillis() - startMs;
long thisWaitMs = maxWaitMs - elapsed;
if (thisWaitMs <= 0) {
result = false;
break;
} else {
wait(thisWaitMs);
}
} else {
wait();
}
}
}
try {
client.delete().forPath(readyPath);
} catch (KeeperException.NoNodeException ignore) {
// ignore
}
return result;
}