Search in sources :

Example 76 with Watcher

use of org.apache.zookeeper.Watcher in project XRTB by benmfaul.

the class ZTester method addRecursiveWatch.

public void addRecursiveWatch(String target, Zoolander z, boolean isNew) throws Exception {
    if (isNew) {
        System.out.println("New Node|: " + target);
    }
    try {
        zk.getChildren(target, new Watcher() {

            public void process(WatchedEvent event) {
                if (z != null)
                    try {
                        z.callBackR(event.getPath(), event.getType());
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
        });
    } catch (Exception error) {
        System.out.println("========> " + target);
        return;
    }
    if (nodeExists(target)) {
        List<String> list = zk.getChildren(target, null);
        for (String s : list) {
            System.out.println(target + "/" + s);
            addRecursiveWatch(target + "/" + s, z, isNew);
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher)

Example 77 with Watcher

use of org.apache.zookeeper.Watcher in project cdap by caskdata.

the class ResourceCoordinator method fetchAndProcessAllResources.

/**
   * Fetches all {@link ResourceRequirement} and perform assignment for the one that changed. Also, it will
   * remove assignments for the resource requirements that are removed.
   */
private void fetchAndProcessAllResources(final Watcher watcher) {
    Futures.addCallback(zkClient.getChildren(CoordinationConstants.REQUIREMENTS_PATH, watcher), wrapCallback(new FutureCallback<NodeChildren>() {

        @Override
        public void onSuccess(NodeChildren result) {
            Set<String> children = ImmutableSet.copyOf(result.getChildren());
            // Handle new resources
            for (String child : children) {
                String path = CoordinationConstants.REQUIREMENTS_PATH + "/" + child;
                Watcher requirementWatcher = wrapWatcher(new ResourceRequirementWatcher(path));
                fetchAndProcessRequirement(path, requirementWatcher);
            }
            // Handle removed resources
            for (String removed : ImmutableSet.copyOf(Sets.difference(requirements.keySet(), children))) {
                ResourceRequirement requirement = requirements.remove(removed);
                LOG.info("Requirement deleted {}", requirement);
                // Delete the assignment node.
                removeAssignment(removed);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            // If the resource path node doesn't exists, resort to watch for exists.
            if (t instanceof KeeperException.NoNodeException) {
                beginWatch(watcher);
            }
            // Otherwise, it's a unexpected failure.
            LOG.error("Failed to getChildren on ZK node {}{}", zkClient.getConnectString(), CoordinationConstants.REQUIREMENTS_PATH, t);
            doNotifyFailed(t);
        }
    }), executor);
}
Also used : Watcher(org.apache.zookeeper.Watcher) FutureCallback(com.google.common.util.concurrent.FutureCallback) NodeChildren(org.apache.twill.zookeeper.NodeChildren)

Example 78 with Watcher

use of org.apache.zookeeper.Watcher in project cdap by caskdata.

the class ResourceCoordinatorClient method watchAssignmentOnExists.

/**
   * Starts watch for assignment changes when the node exists.
   *
   * @param serviceName Name of the service.
   */
private void watchAssignmentOnExists(final String serviceName) {
    final String zkPath = CoordinationConstants.ASSIGNMENTS_PATH + "/" + serviceName;
    Watcher watcher = wrapWatcher(new AssignmentWatcher(serviceName, EnumSet.of(Watcher.Event.EventType.NodeCreated)));
    Futures.addCallback(zkClient.exists(zkPath, watcher), wrapCallback(new FutureCallback<Stat>() {

        @Override
        public void onSuccess(Stat result) {
            if (result != null) {
                watchAssignment(serviceName);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            LOG.error("Failed to call exists on ZK {}{}", zkClient.getConnectString(), zkPath, t);
            doNotifyFailed(t);
        }
    }), Threads.SAME_THREAD_EXECUTOR);
}
Also used : Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) FutureCallback(com.google.common.util.concurrent.FutureCallback)

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