Example usage for org.apache.zookeeper WatchedEvent getPath

List of usage examples for org.apache.zookeeper WatchedEvent getPath

Introduction

In this page you can find the example usage for org.apache.zookeeper WatchedEvent getPath.

Prototype

public String getPath() 

Source Link

Usage

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java

License:Apache License

@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//from   w ww  . j  av a  2  s.com
        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>());
}

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java

License:Apache License

@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/* ww w  .ja va 2  s .  c o  m*/
        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();
}

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java

License:Apache License

@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//from  ww  w. java 2s.  c  om
        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();
}

From source file:com.metamx.druid.master.LoadQueuePeon.java

License:Open Source License

private void doNext() {
    synchronized (lock) {
        if (currentlyLoading == null) {
            if (!segmentsToDrop.isEmpty()) {
                currentlyLoading = segmentsToDrop.first();
                log.info("Server[%s] dropping [%s]", basePath, currentlyLoading);
            } else if (!segmentsToLoad.isEmpty()) {
                currentlyLoading = segmentsToLoad.first();
                log.info("Server[%s] loading [%s]", basePath, currentlyLoading);
            } else {
                return;
            }/*  w  ww. ja  v  a  2  s . c  o m*/

            zkWritingExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock) {
                        try {
                            if (currentlyLoading == null) {
                                log.makeAlert("Crazy race condition! server[%s]", basePath).emit();
                                actionCompleted();
                                doNext();
                                return;
                            }
                            log.info("Server[%s] adding segment[%s]", basePath,
                                    currentlyLoading.getSegmentIdentifier());
                            final String path = ZKPaths.makePath(basePath,
                                    currentlyLoading.getSegmentIdentifier());
                            final byte[] payload = jsonMapper
                                    .writeValueAsBytes(currentlyLoading.getChangeRequest());
                            curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, payload);

                            zkWritingExecutor.schedule(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        if (curator.checkExists().forPath(path) != null) {
                                            failAssign(new ISE("%s was never removed! Failing this assign!",
                                                    path));
                                        }
                                    } catch (Exception e) {
                                        failAssign(e);
                                    }
                                }
                            }, config.getLoadTimeoutDelay().getMillis(), TimeUnit.MILLISECONDS);

                            final Stat stat = curator.checkExists().usingWatcher(new CuratorWatcher() {
                                @Override
                                public void process(WatchedEvent watchedEvent) throws Exception {
                                    switch (watchedEvent.getType()) {
                                    case NodeDeleted:
                                        entryRemoved(watchedEvent.getPath());
                                    }
                                }
                            }).forPath(path);

                            if (stat == null) {
                                final byte[] noopPayload = jsonMapper
                                        .writeValueAsBytes(new SegmentChangeRequestNoop());

                                // Create a node and then delete it to remove the registered watcher.  This is a work-around for
                                // a zookeeper race condition.  Specifically, when you set a watcher, it fires on the next event
                                // that happens for that node.  If no events happen, the watcher stays registered foreverz.
                                // Couple that with the fact that you cannot set a watcher when you create a node, but what we
                                // want is to create a node and then watch for it to get deleted.  The solution is that you *can*
                                // set a watcher when you check to see if it exists so, we first create the node and then set a
                                // watcher on its existence.  However, if already does not exist by the time the existence check
                                // returns, then the watcher that was set will never fire (nobody will ever create the node
                                // again) and thus lead to a slow, but real, memory leak.  So, we create another node to cause
                                // that watcher to fire and delete it right away.
                                //
                                // We do not create the existence watcher first, because then it will fire when we create the
                                // node and we'll have the same race when trying to refresh that watcher.
                                curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, noopPayload);

                                entryRemoved(path);
                            }
                        } catch (Exception e) {
                            failAssign(e);
                        }
                    }
                }
            });
        } else {
            log.info("Server[%s] skipping doNext() because something is currently loading[%s].", basePath,
                    currentlyLoading);
        }
    }
}

From source file:com.navercorp.nbasearc.confmaster.server.leaderelection.LeaderElectionSupport.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (event.getType().equals(Watcher.Event.EventType.NodeDeleted)
            && !event.getPath().equals(leaderOffer.getNodePath()) && state != State.STOP) {
        logger.debug("Node {} deleted. Need to run through the election process.", event.getPath());
        try {/* ww w.j a  v a2  s  . c  om*/
            determineElectionStatus();
        } catch (KeeperException e) {
            becomeFailed(e);
        } catch (InterruptedException e) {
            becomeFailed(e);
        }
    }
}

From source file:com.navercorp.nbasearc.confmaster.server.watcher.WatchEventHandler.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    Level logLevel = INFO;//from w w  w.j a v a2  s.  c om

    try {
        if (event.getType() == Event.EventType.NodeDeleted) {
            return;
        }

        lock(event.getPath());
        work(event);
    } catch (Exception e) {
        Logger.error("Handle watch event fail. {} {}", event.getPath(), event, e);

        logLevel = DEBUG;
    } finally {
        unlock();
    }

    Logger.flush(logLevel);
}

From source file:com.navercorp.nbasearc.confmaster.server.watcher.WatchEventHandlerCluster.java

License:Apache License

@Override
public void onChangedEvent(WatchedEvent event) throws MgmtZooKeeperException {
    if (LeaderState.isLeader()) {
        return;//from ww  w. j  a v  a 2 s  . co  m
    } else {
        registerBoth(event.getPath());

        Cluster cluster = clusterImo.getByPath(event.getPath());
        if (null == cluster) {
            // this znode already removed.
            return;
        }

        zookeeper.reflectZkIntoMemory(cluster);
    }
}

From source file:com.navercorp.nbasearc.confmaster.server.watcher.WatchEventHandlerClusterRoot.java

License:Apache License

@Override
public void onChildEvent(WatchedEvent event) throws MgmtZooKeeperException, NoNodeException {
    if (LeaderState.isLeader()) {
        return;/* www. j  a  va2s.  c o  m*/
    } else {
        registerBoth(event.getPath());

        // Delete
        List<String> deleted = getDeletedChild(event.getPath(), clusterImo.getAll());
        for (String clusterName : deleted) {
            clusterImo.delete(PathUtil.clusterPath(clusterName));
        }

        // Created
        List<String> created = getCreatedChild(event.getPath(), clusterImo.getAll());
        for (String clusterName : created) {
            // TDOO : eliminate null
            clusterImo.load(clusterName);
        }
    }
}

From source file:com.navercorp.nbasearc.confmaster.server.watcher.WatchEventHandlerFd.java

License:Apache License

@Override
public void onChildEvent(WatchedEvent event) throws MgmtZooKeeperException {
    registerBoth(event.getPath());

    workflowExecutor.perform(UPDATE_HEARTBEAT_CHECKER);
}

From source file:com.navercorp.nbasearc.confmaster.server.watcher.WatchEventHandlerGw.java

License:Apache License

@Override
public void onChildEvent(WatchedEvent event) throws MgmtZooKeeperException {
    registerBoth(event.getPath());

    Gateway gw = gwImo.getByPath(event.getPath());

    if (gw.getData().getHB().equals(Constant.HB_MONITOR_YES)) {
        gw.getHbc().urgent();//from ww  w  .  j a va 2 s . c om
    }

    workflowExecutor.perform(FAILOVER_COMMON, gw);
}