Search in sources :

Example 71 with WatchedEvent

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

the class DataTree method setWatches.

public void setWatches(long relativeZxid, List<String> dataWatches, List<String> existWatches, List<String> childWatches, List<String> persistentWatches, List<String> persistentRecursiveWatches, Watcher watcher) {
    for (String path : dataWatches) {
        DataNode node = getNode(path);
        WatchedEvent e = null;
        if (node == null) {
            watcher.process(new WatchedEvent(EventType.NodeDeleted, KeeperState.SyncConnected, path));
        } else if (node.stat.getMzxid() > relativeZxid) {
            watcher.process(new WatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, path));
        } else {
            this.dataWatches.addWatch(path, watcher);
        }
    }
    for (String path : existWatches) {
        DataNode node = getNode(path);
        if (node != null) {
            watcher.process(new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected, path));
        } else {
            this.dataWatches.addWatch(path, watcher);
        }
    }
    for (String path : childWatches) {
        DataNode node = getNode(path);
        if (node == null) {
            watcher.process(new WatchedEvent(EventType.NodeDeleted, KeeperState.SyncConnected, path));
        } else if (node.stat.getPzxid() > relativeZxid) {
            watcher.process(new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected, path));
        } else {
            this.childWatches.addWatch(path, watcher);
        }
    }
    for (String path : persistentWatches) {
        this.childWatches.addWatch(path, watcher, WatcherMode.PERSISTENT);
        this.dataWatches.addWatch(path, watcher, WatcherMode.PERSISTENT);
    }
    for (String path : persistentRecursiveWatches) {
        this.childWatches.addWatch(path, watcher, WatcherMode.PERSISTENT_RECURSIVE);
        this.dataWatches.addWatch(path, watcher, WatcherMode.PERSISTENT_RECURSIVE);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent)

Example 72 with WatchedEvent

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

the class PersistentWatcherTest method assertEvent.

private void assertEvent(BlockingQueue<WatchedEvent> events, Watcher.Event.EventType eventType, String path) throws InterruptedException {
    WatchedEvent event = events.poll(5, TimeUnit.SECONDS);
    assertNotNull(event);
    assertEquals(eventType, event.getType());
    assertEquals(path, event.getPath());
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent)

Example 73 with WatchedEvent

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

the class PersistentWatcherTest method testBasicAsync.

@Test
public void testBasicAsync() throws IOException, InterruptedException, KeeperException {
    CountdownWatcher watcher = new CountdownWatcher() {

        @Override
        public synchronized void process(WatchedEvent event) {
            super.process(event);
            events.add(event);
        }
    };
    try (ZooKeeper zk = createClient(watcher, hostPort)) {
        final CountDownLatch latch = new CountDownLatch(1);
        AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
            if (rc == KeeperException.Code.OK.intValue()) {
                latch.countDown();
            }
        };
        zk.addWatch("/a/b", persistentWatcher, PERSISTENT, cb, null);
        assertTrue(latch.await(5, TimeUnit.SECONDS));
        // clear any events added during client connection
        events.clear();
        internalTestBasic(zk);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) CreateMode(org.apache.zookeeper.CreateMode) ZooKeeper(org.apache.zookeeper.ZooKeeper) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) WatchedEvent(org.apache.zookeeper.WatchedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ZooDefs(org.apache.zookeeper.ZooDefs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) PERSISTENT(org.apache.zookeeper.AddWatchMode.PERSISTENT) AsyncCallback(org.apache.zookeeper.AsyncCallback) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZooKeeper(org.apache.zookeeper.ZooKeeper) AsyncCallback(org.apache.zookeeper.AsyncCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 74 with WatchedEvent

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

the class PersistentWatcherTest method testDefaultWatcher.

@Test
public void testDefaultWatcher() throws IOException, InterruptedException, KeeperException {
    CountdownWatcher watcher = new CountdownWatcher() {

        @Override
        public synchronized void process(WatchedEvent event) {
            super.process(event);
            events.add(event);
        }
    };
    try (ZooKeeper zk = createClient(watcher, hostPort)) {
        zk.addWatch("/a/b", PERSISTENT);
        // clear any events added during client connection
        events.clear();
        internalTestBasic(zk);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Test(org.junit.jupiter.api.Test)

Example 75 with WatchedEvent

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

the class WatchedEventTest method testCreatingWatchedEventFromInvalidWrapper.

@Test
public void testCreatingWatchedEventFromInvalidWrapper() {
    try {
        WatcherEvent wep = new WatcherEvent(-2342, -252352, "foo");
        new WatchedEvent(wep);
        fail("Was able to create WatchedEvent from bad wrapper");
    } catch (RuntimeException re) {
    // we're good
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) WatcherEvent(org.apache.zookeeper.proto.WatcherEvent) Test(org.junit.jupiter.api.Test)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)194 Watcher (org.apache.zookeeper.Watcher)123 ZooKeeper (org.apache.zookeeper.ZooKeeper)67 CountDownLatch (java.util.concurrent.CountDownLatch)64 KeeperException (org.apache.zookeeper.KeeperException)56 Test (org.junit.Test)38 Stat (org.apache.zookeeper.data.Stat)35 IOException (java.io.IOException)31 Test (org.testng.annotations.Test)27 Test (org.junit.jupiter.api.Test)18 CuratorFramework (org.apache.curator.framework.CuratorFramework)14 AsyncCallback (org.apache.zookeeper.AsyncCallback)14 List (java.util.List)10 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)10 Set (java.util.Set)7 TimeoutException (java.util.concurrent.TimeoutException)7 HashSet (java.util.HashSet)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RetryOneTime (org.apache.curator.retry.RetryOneTime)6