Search in sources :

Example 16 with Watcher

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);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher)

Example 17 with Watcher

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

Example 18 with Watcher

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();
}
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) List(java.util.List) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 19 with Watcher

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>());
}
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 20 with Watcher

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

Aggregations

Watcher (org.apache.zookeeper.Watcher)78 WatchedEvent (org.apache.zookeeper.WatchedEvent)62 KeeperException (org.apache.zookeeper.KeeperException)35 CountDownLatch (java.util.concurrent.CountDownLatch)25 ZooKeeper (org.apache.zookeeper.ZooKeeper)25 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 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 TimeoutException (java.util.concurrent.TimeoutException)4 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)4