use of org.apache.zookeeper.Watcher in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithExistWatch3.
@Test
public void testSymlinkWithExistWatch3() 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 /$link doesn't exist.
_zkClient.getZooKeeper().exists("/$link", existWatch, existCallback2, null);
latch2.await(30, TimeUnit.SECONDS);
// create symlink /$link -> /foo/bar. existWatch should be notified.
_zkClient.createSymlink("/$link", "/foo/bar", new FutureCallback<None>());
latch.await(30, TimeUnit.SECONDS);
// delete symlink /$link
_zkClient.removeNodeUnsafe("/$link", new FutureCallback<None>());
}
use of org.apache.zookeeper.Watcher in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithChildrenWatcher2.
@Test
public void testSymlinkWithChildrenWatcher2() throws ExecutionException, InterruptedException {
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AsyncCallback.ChildrenCallback callback2 = new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
Assert.assertEquals(path, "/foo/$link");
Assert.assertEquals(children.size(), 5);
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.ChildrenCallback callback = new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
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 weave by continuuity.
the class ZKClientTest method testRetry.
@Test
public void testRetry() throws ExecutionException, InterruptedException, TimeoutException {
File dataDir = Files.createTempDir();
InMemoryZKServer zkServer = InMemoryZKServer.builder().setDataDir(dataDir).setTickTime(1000).build();
zkServer.startAndWait();
int port = zkServer.getLocalAddress().getPort();
final CountDownLatch disconnectLatch = new CountDownLatch(1);
ZKClientService client = ZKClientServices.delegate(ZKClients.retryOnFailure(ZKClientService.Builder.of(zkServer.getConnectionStr()).setConnectionWatcher(new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.Disconnected) {
disconnectLatch.countDown();
}
}
}).build(), RetryStrategies.fixDelay(0, TimeUnit.SECONDS)));
client.startAndWait();
zkServer.stopAndWait();
Assert.assertTrue(disconnectLatch.await(1, TimeUnit.SECONDS));
final CountDownLatch createLatch = new CountDownLatch(1);
Futures.addCallback(client.create("/testretry/test", null, CreateMode.PERSISTENT), new FutureCallback<String>() {
@Override
public void onSuccess(String result) {
createLatch.countDown();
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace(System.out);
}
});
TimeUnit.SECONDS.sleep(2);
zkServer = InMemoryZKServer.builder().setDataDir(dataDir).setAutoCleanDataDir(true).setPort(port).setTickTime(1000).build();
zkServer.startAndWait();
try {
Assert.assertTrue(createLatch.await(5, TimeUnit.SECONDS));
} finally {
zkServer.stopAndWait();
}
}
use of org.apache.zookeeper.Watcher in project distributedlog by twitter.
the class TestZKWatcherManager method testRegisterUnregisterWatcher.
@Test(timeout = 60000)
public void testRegisterUnregisterWatcher() throws Exception {
ZKWatcherManager watcherManager = ZKWatcherManager.newBuilder().name("test-register-unregister-watcher").statsLogger(NullStatsLogger.INSTANCE).build();
String path = "/test-register-unregister-watcher";
final List<WatchedEvent> events = new LinkedList<WatchedEvent>();
final CountDownLatch latch = new CountDownLatch(2);
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
events.add(event);
latch.countDown();
}
};
watcherManager.registerChildWatcher(path, watcher);
// fire the event
WatchedEvent event0 = new WatchedEvent(Watcher.Event.EventType.NodeCreated, Watcher.Event.KeeperState.SyncConnected, path);
WatchedEvent event1 = new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.SyncConnected, path);
WatchedEvent event2 = new WatchedEvent(Watcher.Event.EventType.NodeChildrenChanged, Watcher.Event.KeeperState.SyncConnected, path);
watcher.process(event1);
watcher.process(event2);
latch.await();
assertEquals(2, events.size());
assertEquals(event1, events.get(0));
assertEquals(event2, events.get(1));
// unregister watcher
watcherManager.unregisterChildWatcher(path, watcher);
assertEquals(0, watcherManager.childWatches.size());
}
use of org.apache.zookeeper.Watcher in project commons by twitter.
the class ZooKeeperClient method registerExpirationHandler.
/**
* Clients that need to re-establish state after session expiration can register an
* {@code onExpired} command to execute.
*
* @param onExpired the {@code Command} to register
* @return the new {@link Watcher} which can later be passed to {@link #unregister} for
* removal.
*/
public Watcher registerExpirationHandler(final Command onExpired) {
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
onExpired.execute();
}
}
};
register(watcher);
return watcher;
}
Aggregations