in twill-zookeeper/src/main/java/org/apache/twill/internal/zookeeper/RewatchOnExpireWatcher.java [94:126]
private void exists() {
Futures.addCallback(client.exists(path, this), new FutureCallback<Stat>() {
@Override
public void onSuccess(Stat stat) {
// Since we know all callbacks and watcher are triggered from single event thread, there is no race condition.
Object oldResult = lastResult.getReference();
lastResult.compareAndSet(oldResult, null, true, false);
if (stat != oldResult && (stat == null || !stat.equals(oldResult))) {
if (stat == null) {
// previous stat is not null, means node deleted
process(new WatchedEvent(Event.EventType.NodeDeleted, Event.KeeperState.SyncConnected, path));
} else if (oldResult == null) {
// previous stat is null, means node created
process(new WatchedEvent(Event.EventType.NodeCreated, Event.KeeperState.SyncConnected, path));
} else {
// Otherwise, something changed on the node
process(new WatchedEvent(Event.EventType.NodeDataChanged, Event.KeeperState.SyncConnected, path));
}
}
}
@Override
public void onFailure(Throwable t) {
if (RetryUtils.canRetry(t)) {
exists();
} else {
lastResult.set(null, false);
LOG.error("Fail to re-set watch on exists for path " + path, t);
}
}
});
}