Search in sources :

Example 71 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 72 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 73 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)

Example 74 with Watcher

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

the class MultiTransactionTest method testMultiRollback.

/**
 * ZOOKEEPER-2052:
 * Multi abort shouldn't have any side effect.
 * We fix a bug in rollback and the following scenario should work:
 * 1. multi delete abort because of not empty directory
 * 2. ephemeral nodes under that directory are deleted
 * 3. multi delete should succeed.
 */
@Test
public void testMultiRollback() throws Exception {
    zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    ZooKeeper epheZk = createClient();
    epheZk.create("/foo/bar", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    List<Op> opList = Arrays.asList(Op.delete("/foo", -1));
    try {
        zk.multi(opList);
        Assert.fail("multi delete should failed for not empty directory");
    } catch (KeeperException.NotEmptyException e) {
    }
    final CountDownLatch latch = new CountDownLatch(1);
    zk.exists("/foo/bar", new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == Event.EventType.NodeDeleted) {
                latch.countDown();
            }
        }
    });
    epheZk.close();
    latch.await();
    try {
        zk.getData("/foo/bar", false, null);
        Assert.fail("ephemeral node should have been deleted");
    } catch (KeeperException.NoNodeException e) {
    }
    zk.multi(opList);
    try {
        zk.getData("/foo", false, null);
        Assert.fail("persistent node should have been deleted after multi");
    } catch (KeeperException.NoNodeException e) {
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Op(org.apache.zookeeper.Op) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 75 with Watcher

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

the class WatchManager method triggerWatch.

Set<Watcher> triggerWatch(String path, EventType type, Set<Watcher> supress) {
    WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path);
    Set<Watcher> watchers;
    synchronized (this) {
        watchers = watchTable.remove(path);
        if (watchers == null || watchers.isEmpty()) {
            if (LOG.isTraceEnabled()) {
                ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "No watchers for " + path);
            }
            return null;
        }
        for (Watcher w : watchers) {
            Set<String> paths = watch2Paths.get(w);
            if (paths != null) {
                paths.remove(path);
            }
        }
    }
    for (Watcher w : watchers) {
        if (supress != null && supress.contains(w)) {
            continue;
        }
        w.process(e);
    }
    return watchers;
}
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