use of org.apache.zookeeper.Watcher in project weave by continuuity.
the class AbstractZKServiceController method actOnExists.
private void actOnExists(final String path, final Runnable action) {
// Watch for node existence.
final AtomicBoolean nodeExists = new AtomicBoolean(false);
Futures.addCallback(zkClient.exists(path, new Watcher() {
@Override
public void process(WatchedEvent event) {
// Other event type would be handled by the action.
if (event.getType() == Event.EventType.NodeCreated && nodeExists.compareAndSet(false, true)) {
action.run();
}
}
}), new FutureCallback<Stat>() {
@Override
public void onSuccess(Stat result) {
if (result != null && nodeExists.compareAndSet(false, true)) {
action.run();
}
}
@Override
public void onFailure(Throwable t) {
LOG.error("Failed in exists call to {}. Shutting down service.", path, t);
forceShutDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
}
use of org.apache.zookeeper.Watcher in project rest.li by linkedin.
the class ZKConnection method start.
public void start() throws IOException {
if (_zkRef.get() != null) {
throw new IllegalStateException("Already started");
}
// We take advantage of the fact that the default watcher is always
// notified of connection state changes (without having to explicitly register)
// and never notified of anything else.
Watcher defaultWatcher = new DefaultWatcher();
ZooKeeper zk = new VanillaZooKeeperAdapter(_connectString, _timeout, defaultWatcher);
if (_retryLimit <= 0) {
if (_isSymlinkAware) {
zk = new SymlinkAwareZooKeeper(zk, defaultWatcher, _symlinkSerializer);
LOG.info("Using symlink aware ZooKeeper without retry");
} else {
// do nothing
LOG.info("Using vanilla ZooKeeper without retry.");
}
} else {
zk = new RetryZooKeeper(zk, _retryLimit, _exponentialBackoff, _scheduler, _initInterval);
if (_isSymlinkAware) {
zk = new SymlinkAwareRetryZooKeeper((RetryZooKeeper) zk, defaultWatcher, _symlinkSerializer);
LOG.info("Using symlink aware RetryZooKeeper with retry limit set to " + _retryLimit);
} else {
LOG.info("Using RetryZooKeeper with retry limit set to " + _retryLimit);
}
if (_exponentialBackoff) {
LOG.info("Exponential backoff enabled. Initial retry interval set to " + _initInterval + " ms.");
} else {
LOG.info("Exponential backoff disabled.");
}
}
LOG.debug("Going to set zkRef");
if (!_zkRef.compareAndSet(null, zk)) {
try {
doShutdown(zk);
} catch (InterruptedException e) {
LOG.warn("Failed to shutdown extra ZooKeeperConnection", e);
}
throw new IllegalStateException("Already started");
}
LOG.debug("counting down");
_zkRefLatch.countDown();
}
use of org.apache.zookeeper.Watcher in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithChildrenWatcher3.
// test children2Callback watcher
@Test
public void testSymlinkWithChildrenWatcher3() throws ExecutionException, InterruptedException, KeeperException {
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
Stat expectedStat1 = _zkClient.getZooKeeper().exists("/foo/bar", false);
Stat expectedStat2 = _zkClient.getZooKeeper().exists("/bar/foo", false);
final AsyncCallback.Children2Callback callback2 = new AsyncCallback.Children2Callback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
Assert.assertEquals(path, "/foo/$link");
Assert.assertEquals(children.size(), 5);
Assert.assertEquals(stat, expectedStat2);
latch2.countDown();
}
};
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged);
_zkClient.getZooKeeper().getChildren(event.getPath(), null, callback2, null);
}
};
AsyncCallback.Children2Callback callback = new AsyncCallback.Children2Callback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
Assert.assertEquals(stat, expectedStat1);
latch1.countDown();
}
};
// set watcher to /foo/$link
_zkClient.getZooKeeper().getChildren("/foo/$link", watcher, callback, null);
latch1.await(30, TimeUnit.SECONDS);
// update symlink
_zkClient.setSymlinkData("/foo/$link", "/bar/foo", new FutureCallback<None>());
latch2.await(30, TimeUnit.SECONDS);
FutureCallback<None> fcb = new FutureCallback<None>();
// restore symlink
_zkClient.setSymlinkData("/foo/$link", "/foo/bar", fcb);
fcb.get();
}
use of org.apache.zookeeper.Watcher in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithExistWatch.
@Test
public void testSymlinkWithExistWatch() throws InterruptedException, ExecutionException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AsyncCallback.StatCallback existCallback = new AsyncCallback.StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.OK);
latch.countDown();
}
};
Watcher existWatch = new Watcher() {
@Override
public void process(WatchedEvent event) {
Assert.assertEquals(event.getType(), Event.EventType.NodeCreated);
_zkClient.getZooKeeper().exists(event.getPath(), null, existCallback, null);
}
};
AsyncCallback.StatCallback existCallback2 = new AsyncCallback.StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.NONODE);
latch2.countDown();
}
};
// symlink: /foo/$link/newNode -> /foo/bar/newNode
_zkClient.getZooKeeper().exists("/foo/$link/newNode", existWatch, existCallback2, null);
latch2.await(30, TimeUnit.SECONDS);
_zkClient.ensurePersistentNodeExists("/foo/bar/newNode", new FutureCallback<None>());
latch.await(30, TimeUnit.SECONDS);
_zkClient.removeNodeUnsafe("/foo/bar/newNode", new FutureCallback<None>());
}
use of org.apache.zookeeper.Watcher in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithChildrenWatch.
@Test
public void testSymlinkWithChildrenWatch() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AsyncCallback.ChildrenCallback childrenCallback = new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.OK);
Assert.assertEquals(children.size(), 11);
latch.countDown();
}
};
Watcher childrenWatch = new Watcher() {
@Override
public void process(WatchedEvent event) {
Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged);
_zkClient.getZooKeeper().getChildren(event.getPath(), null, childrenCallback, null);
}
};
AsyncCallback.ChildrenCallback childrenCallback2 = new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.OK);
latch2.countDown();
}
};
// symlink: /foo/$link -> /foo/bar
_zkClient.getZooKeeper().getChildren("/foo/$link", childrenWatch, childrenCallback2, null);
latch2.await(30, TimeUnit.SECONDS);
_zkClient.ensurePersistentNodeExists("/foo/bar/newNode", new FutureCallback<None>());
latch.await(30, TimeUnit.SECONDS);
_zkClient.removeNodeUnsafe("/foo/bar/newNode", new FutureCallback<None>());
}
Aggregations