Search in sources :

Example 1 with Watcher

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

the class ZooKeeperServiceTest method test.

@Test
public void test() throws Exception {
    class ConnectionWatcher implements Watcher {

        private ZooKeeper zk;

        private CountDownLatch latch = new CountDownLatch(1);

        public void connect(String hosts) throws IOException, InterruptedException {
            zk = new ZooKeeper(hosts, 5000, this);
            latch.await();
        }

        public ZooKeeper getZooKeeper() {
            return zk;
        }

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }

        public void close() throws InterruptedException {
            if (zk != null) {
                zk.close();
            }
        }
    }
    String path = "/data";
    String data = "Hello";
    ConnectionWatcher watcher = new ConnectionWatcher();
    watcher.connect(cluster.getHosts());
    watcher.getZooKeeper().create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    watcher.close();
    watcher = new ConnectionWatcher();
    watcher.connect(cluster.getHosts());
    byte[] actualData = watcher.getZooKeeper().getData(path, false, null);
    assertEquals(data, new String(actualData));
    watcher.close();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Watcher

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

the class WatchManager method getWatchesByPath.

/**
     * Returns a watch report by path.
     *
     * @return watch report
     * @see WatchesPathReport
     */
synchronized WatchesPathReport getWatchesByPath() {
    Map<String, Set<Long>> path2ids = new HashMap<String, Set<Long>>();
    for (Entry<String, HashSet<Watcher>> e : watchTable.entrySet()) {
        Set<Long> ids = new HashSet<Long>(e.getValue().size());
        path2ids.put(e.getKey(), ids);
        for (Watcher watcher : e.getValue()) {
            ids.add(((ServerCnxn) watcher).getSessionId());
        }
    }
    return new WatchesPathReport(path2ids);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Watcher(org.apache.zookeeper.Watcher) HashSet(java.util.HashSet)

Example 3 with Watcher

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

the class BaseSysTest method setUp.

@Before
public void setUp() throws Exception {
    if (!fakeMachines) {
        zk = new ZooKeeper(zkHostPort, 15000, new Watcher() {

            public void process(WatchedEvent e) {
            }
        });
        im = new InstanceManager(zk, prefix);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) Before(org.junit.Before)

Example 4 with Watcher

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

the class InstanceContainer method processResult.

@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
    if (rc != KeeperException.Code.OK.intValue()) {
        // try it again
        zk.getChildren(assignmentsNode, true, this, null);
        return;
    }
    HashMap<String, Instance> newList = new HashMap<String, Instance>();
    // check for differences
    Stat stat = new Stat();
    for (String child : children) {
        Instance i = instances.remove(child);
        if (i == null) {
            // Start up a new instance
            byte[] data = null;
            String myNode = assignmentsNode + '/' + child;
            while (true) {
                try {
                    data = zk.getData(myNode, true, stat);
                    break;
                } catch (NoNodeException e) {
                    // The node doesn't exist anymore, so skip it
                    break;
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    return;
                }
            }
            if (data != null) {
                String instanceSpec = new String(data);
                int spaceIndex = instanceSpec.indexOf(' ');
                String clazz;
                String conf;
                if (spaceIndex == -1) {
                    clazz = instanceSpec;
                    conf = null;
                } else {
                    clazz = instanceSpec.substring(0, spaceIndex);
                    conf = instanceSpec.substring(spaceIndex + 1);
                }
                try {
                    Class<?> c = Class.forName(clazz);
                    i = (Instance) c.newInstance();
                    Reporter reporter = new MyReporter(child);
                    i.setReporter(reporter);
                    i.configure(conf);
                    i.start();
                    newList.put(child, i);
                    int ver = stat.getVersion();
                    Instance myInstance = i;
                    DataCallback dc = new MyDataCallback(myNode, myInstance, ver);
                    Watcher watcher = new MyWatcher(myNode, dc);
                    zk.getData(myNode, watcher, dc, watcher);
                } catch (Exception e) {
                    LOG.warn("Skipping " + child, e);
                    if (e.getCause() != null) {
                        LOG.warn("Caused by", e.getCause());
                    }
                }
            }
        } else {
            // just move it to the new list
            newList.put(child, i);
        }
    }
    // kill anything that was removed for the children
    for (Map.Entry<String, Instance> i : instances.entrySet()) {
        i.getValue().stop();
        try {
            rmnod(reportsNode + '/' + i.getKey());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }
    instances = newList;
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) HashMap(java.util.HashMap) Reporter(org.apache.zookeeper.test.system.Instance.Reporter) Watcher(org.apache.zookeeper.Watcher) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) Stat(org.apache.zookeeper.data.Stat) HashMap(java.util.HashMap) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException)

Example 5 with Watcher

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

the class DataTreeTest method testRootWatchTriggered.

@Test(timeout = 60000)
public void testRootWatchTriggered() throws Exception {
    class MyWatcher implements Watcher {

        boolean fired = false;

        public void process(WatchedEvent event) {
            if (event.getPath().equals("/"))
                fired = true;
        }
    }
    MyWatcher watcher = new MyWatcher();
    // set a watch on the root node
    dt.getChildren("/", new Stat(), watcher);
    // add a new node, should trigger a watch
    dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion() + 1, 1, 1);
    Assert.assertFalse("Root node watch not triggered", !watcher.fired);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) Test(org.junit.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