Search in sources :

Example 51 with Watcher

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>());
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) None(com.linkedin.common.util.None) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 52 with Watcher

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();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) AsyncCallback(org.apache.zookeeper.AsyncCallback) Watcher(org.apache.zookeeper.Watcher) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 53 with Watcher

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();
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 54 with Watcher

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());
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 55 with Watcher

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;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher)

Aggregations

Watcher (org.apache.zookeeper.Watcher)78 WatchedEvent (org.apache.zookeeper.WatchedEvent)62 KeeperException (org.apache.zookeeper.KeeperException)34 CountDownLatch (java.util.concurrent.CountDownLatch)26 ZooKeeper (org.apache.zookeeper.ZooKeeper)24 Stat (org.apache.zookeeper.data.Stat)21 Test (org.junit.Test)18 IOException (java.io.IOException)11 AsyncCallback (org.apache.zookeeper.AsyncCallback)10 List (java.util.List)8 Test (org.testng.annotations.Test)8 None (com.linkedin.common.util.None)7 HashSet (java.util.HashSet)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Map (java.util.Map)5 Set (java.util.Set)5 HashMap (java.util.HashMap)4 TimeoutException (java.util.concurrent.TimeoutException)4 FutureCallback (com.google.common.util.concurrent.FutureCallback)3 FutureCallback (com.linkedin.common.callback.FutureCallback)3