Search in sources :

Example 26 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent 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 27 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent 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 28 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent 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)

Example 29 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project pinpoint by naver.

the class ZookeeperClusterService method setUp.

@PostConstruct
@Override
public void setUp() throws KeeperException, IOException, InterruptedException {
    if (!config.isClusterEnable()) {
        logger.info("pinpoint-collector cluster disable.");
        return;
    }
    switch(this.serviceState.getCurrentState()) {
        case NEW:
            if (this.serviceState.changeStateInitializing()) {
                logger.info("{} initialization started.", this.getClass().getSimpleName());
                ClusterManagerWatcher watcher = new ClusterManagerWatcher();
                this.client = new DefaultZookeeperClient(config.getClusterAddress(), config.getClusterSessionTimeout(), watcher);
                this.client.connect();
                this.profilerClusterManager = new ZookeeperProfilerClusterManager(client, serverIdentifier, clusterPointRouter.getTargetClusterPointRepository());
                this.profilerClusterManager.start();
                this.webClusterManager = new ZookeeperWebClusterManager(client, PINPOINT_WEB_CLUSTER_PATH, serverIdentifier, clusterConnectionManager);
                this.webClusterManager.start();
                this.serviceState.changeStateStarted();
                logger.info("{} initialization completed.", this.getClass().getSimpleName());
                if (client.isConnected()) {
                    WatcherEvent watcherEvent = new WatcherEvent(EventType.None.getIntValue(), KeeperState.SyncConnected.getIntValue(), "");
                    WatchedEvent event = new WatchedEvent(watcherEvent);
                    watcher.process(event);
                }
            }
            break;
        case INITIALIZING:
            logger.info("{} already initializing.", this.getClass().getSimpleName());
            break;
        case STARTED:
            logger.info("{} already started.", this.getClass().getSimpleName());
            break;
        case DESTROYING:
            throw new IllegalStateException("Already destroying.");
        case STOPPED:
            throw new IllegalStateException("Already stopped.");
        case ILLEGAL_STATE:
            throw new IllegalStateException("Invalid State.");
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) WatcherEvent(org.apache.zookeeper.proto.WatcherEvent) PostConstruct(javax.annotation.PostConstruct)

Example 30 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project jstorm by alibaba.

the class Zookeeper method mkClient.

/**
     * connect ZK, register Watch/unhandle Watch
     * 
     * @return
     */
public CuratorFramework mkClient(Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher) {
    CuratorFramework fk = Utils.newCurator(conf, servers, port, root);
    fk.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework _fk, CuratorEvent e) throws Exception {
            if (e.getType().equals(CuratorEventType.WATCHED)) {
                WatchedEvent event = e.getWatchedEvent();
                watcher.execute(event.getState(), event.getType(), event.getPath());
            }
        }
    });
    fk.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {

        @Override
        public void unhandledError(String msg, Throwable error) {
            String errmsg = "Unrecoverable Zookeeper error, halting process: " + msg;
            LOG.error(errmsg, error);
            JStormUtils.halt_process(1, "Unrecoverable Zookeeper error");
        }
    });
    fk.start();
    return fk;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)97 Watcher (org.apache.zookeeper.Watcher)61 ZooKeeper (org.apache.zookeeper.ZooKeeper)37 KeeperException (org.apache.zookeeper.KeeperException)36 Test (org.junit.Test)32 Stat (org.apache.zookeeper.data.Stat)28 CountDownLatch (java.util.concurrent.CountDownLatch)24 Test (org.testng.annotations.Test)14 IOException (java.io.IOException)12 AsyncCallback (org.apache.zookeeper.AsyncCallback)11 None (com.linkedin.common.util.None)7 List (java.util.List)6 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)5 TimeoutException (java.util.concurrent.TimeoutException)4 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)4 WatcherEvent (org.apache.zookeeper.proto.WatcherEvent)4 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)4 ZooKeeperx (com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx)3 FutureCallback (com.linkedin.common.callback.FutureCallback)3 ArrayList (java.util.ArrayList)3