Search in sources :

Example 76 with WatchedEvent

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

the class WatchedEventTest method testCreatingWatchedEvent.

@Test
public void testCreatingWatchedEvent() {
    // EventWatch is a simple, immutable type, so all we need to do
    // is make sure we can create all possible combinations of values.
    EnumSet<EventType> allTypes = EnumSet.allOf(EventType.class);
    EnumSet<KeeperState> allStates = EnumSet.allOf(KeeperState.class);
    WatchedEvent we;
    for (EventType et : allTypes) {
        for (KeeperState ks : allStates) {
            we = new WatchedEvent(et, ks, "blah");
            assertEquals(et, we.getType());
            assertEquals(ks, we.getState());
            assertEquals("blah", we.getPath());
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) EventType(org.apache.zookeeper.Watcher.Event.EventType) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Test(org.junit.jupiter.api.Test)

Example 77 with WatchedEvent

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

the class WatcherTest method testWatcherDisconnectOnClose.

@Test
public void testWatcherDisconnectOnClose() throws IOException, InterruptedException, KeeperException {
    ZooKeeper zk = null;
    try {
        final BlockingQueue<WatchedEvent> queue = new LinkedBlockingQueue<>();
        MyWatcher connWatcher = new MyWatcher();
        Watcher watcher = event -> {
            try {
                queue.put(event);
            } catch (InterruptedException e) {
            // Oh well, never mind
            }
        };
        zk = createClient(connWatcher, hostPort);
        StatCallback scb = new StatCallback() {

            public void processResult(int rc, String path, Object ctx, Stat stat) {
            // don't do anything
            }
        };
        // Register a watch on the node
        zk.exists("/missing", watcher, scb, null);
        // Close the client without changing the node
        zk.close();
        WatchedEvent event = queue.poll(10, TimeUnit.SECONDS);
        assertNotNull(event, "No watch event was received after closing the Zookeeper client. A 'Closed' event should have occurred");
        assertEquals(Event.EventType.None, event.getType(), "Closed events are not generated by the server, and so should have a type of 'None'");
        assertEquals(Event.KeeperState.Closed, event.getState(), "A 'Closed' event was expected as the Zookeeper client was closed without altering the node it was watching");
    } finally {
        if (zk != null) {
            zk.close();
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CreateMode(org.apache.zookeeper.CreateMode) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Ids(org.apache.zookeeper.ZooDefs.Ids) Event(org.apache.zookeeper.Watcher.Event) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) Stat(org.apache.zookeeper.data.Stat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZKClientConfig(org.apache.zookeeper.client.ZKClientConfig) ZooKeeper(org.apache.zookeeper.ZooKeeper) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) WatchedEvent(org.apache.zookeeper.WatchedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) EventType(org.apache.zookeeper.Watcher.Event.EventType) ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Test(org.junit.jupiter.api.Test)

Example 78 with WatchedEvent

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

the class WatcherTest method testWatcherCorrectness.

/**
 * Verify that we get all of the events we expect to get. This particular
 * case verifies that we see all of the data events on a particular node.
 * There was a bug (ZOOKEEPER-137) that resulted in events being dropped
 * in some cases (timing).
 *
 * @throws IOException
 * @throws InterruptedException
 * @throws KeeperException
 */
@Test
public void testWatcherCorrectness() throws IOException, InterruptedException, KeeperException {
    ZooKeeper zk = null;
    try {
        MyWatcher watcher = new MyWatcher();
        zk = createClient(watcher, hostPort);
        StatCallback scb = new StatCallback() {

            public void processResult(int rc, String path, Object ctx, Stat stat) {
            // don't do anything
            }
        };
        VoidCallback vcb = new VoidCallback() {

            public void processResult(int rc, String path, Object ctx) {
            // don't do anything
            }
        };
        String[] names = new String[10];
        for (int i = 0; i < names.length; i++) {
            String name = zk.create("/tc-", "initialvalue".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
            names[i] = name;
            Stat stat = new Stat();
            zk.getData(name, watcher, stat);
            zk.setData(name, "new".getBytes(), stat.getVersion(), scb, null);
            stat = zk.exists(name, watcher);
            zk.delete(name, stat.getVersion(), vcb, null);
        }
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            WatchedEvent event = watcher.events.poll(10, TimeUnit.SECONDS);
            assertEquals(name, event.getPath());
            assertEquals(Event.EventType.NodeDataChanged, event.getType());
            assertEquals(Event.KeeperState.SyncConnected, event.getState());
            event = watcher.events.poll(10, TimeUnit.SECONDS);
            assertEquals(name, event.getPath());
            assertEquals(Event.EventType.NodeDeleted, event.getType());
            assertEquals(Event.KeeperState.SyncConnected, event.getState());
        }
    } finally {
        if (zk != null) {
            zk.close();
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Stat(org.apache.zookeeper.data.Stat) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Test(org.junit.jupiter.api.Test)

Example 79 with WatchedEvent

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

the class ZooKeeperTestClient method getEvent.

private WatchedEvent getEvent(int numTries) throws InterruptedException {
    WatchedEvent event = null;
    for (int i = 0; i < numTries; i++) {
        System.out.println("i = " + i);
        event = events.poll(10, TimeUnit.SECONDS);
        if (event != null) {
            break;
        }
        Thread.sleep(5000);
    }
    return event;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent)

Example 80 with WatchedEvent

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

the class ZooKeeperTestClient method enode_test_2.

private void enode_test_2() throws IOException, InterruptedException, KeeperException {
    checkRoot();
    String parentName = testDirOnZK;
    String nodeName = parentName + "/enode_abc";
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);
    ZooKeeper zk_1 = new ZooKeeper(hostPort, 10000, this);
    Stat stat_parent = zk_1.exists(parentName, false);
    if (stat_parent == null) {
        try {
            zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException ke) {
            fail("Creating node " + parentName + ke.getMessage());
        }
    }
    Stat stat_node = zk_1.exists(nodeName, false);
    if (stat_node != null) {
        try {
            zk.delete(nodeName, -1);
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY;
            if (!valid) {
                fail("Unexpected exception code for delete: " + ke.getMessage());
            }
        }
    }
    List<String> firstGen1 = zk_1.getChildren(parentName, true);
    Stat stat = new Stat();
    List<String> firstGen2 = zk_1.getChildren(parentName, true, stat);
    if (!firstGen1.equals(firstGen2)) {
        fail("children lists from getChildren()/getChildren2() do not match");
    }
    if (!stat_parent.equals(stat)) {
        fail("stat from exists()/getChildren() do not match");
    }
    try {
        zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    } catch (KeeperException ke) {
        Code code = ke.code();
        boolean valid = code == KeeperException.Code.NODEEXISTS;
        if (!valid) {
            fail("Unexpected exception code for createin: " + ke.getMessage());
        }
    }
    Thread.sleep(5000);
    WatchedEvent event = events.poll(10, TimeUnit.SECONDS);
    if (event == null) {
        throw new IOException("No event was delivered promptly");
    }
    if (event.getType() != EventType.NodeChildrenChanged || !event.getPath().equalsIgnoreCase(parentName)) {
        fail("Unexpected event was delivered: " + event.toString());
    }
    stat_node = zk_1.exists(nodeName, false);
    if (stat_node == null) {
        fail("node " + nodeName + " should exist");
    }
    try {
        zk.delete(parentName, -1);
        fail("Should be impossible to delete a non-empty node " + parentName);
    } catch (KeeperException ke) {
        Code code = ke.code();
        boolean valid = code == KeeperException.Code.NOTEMPTY;
        if (!valid) {
            fail("Unexpected exception code for delete: " + code);
        }
    }
    try {
        zk.create(nodeName + "/def", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        fail("Should be impossible to create child off Ephemeral node " + nodeName);
    } catch (KeeperException ke) {
        Code code = ke.code();
        boolean valid = code == KeeperException.Code.NOCHILDRENFOREPHEMERALS;
        if (!valid) {
            fail("Unexpected exception code for createin: " + code);
        }
    }
    try {
        List<String> children1 = zk.getChildren(nodeName, false);
        List<String> children2 = zk.getChildren(nodeName, false, null);
        if (!children1.equals(children2)) {
            fail("children lists from getChildren()/getChildren2() does not match");
        }
        if (children1.size() > 0) {
            fail("ephemeral node " + nodeName + " should not have children");
        }
    } catch (KeeperException ke) {
        Code code = ke.code();
        boolean valid = code == KeeperException.Code.NONODE;
        if (!valid) {
            fail("Unexpected exception code for createin: " + code);
        }
    }
    firstGen1 = zk_1.getChildren(parentName, true);
    firstGen2 = zk_1.getChildren(parentName, true, null);
    if (!firstGen1.equals(firstGen2)) {
        fail("children list from getChildren()/getChildren2() does not match");
    }
    stat_node = zk_1.exists(nodeName, true);
    if (stat_node == null) {
        fail("node " + nodeName + " should exist");
    }
    System.out.println("session id of zk: " + zk.getSessionId());
    System.out.println("session id of zk_1: " + zk_1.getSessionId());
    zk.close();
    zk_1.exists("nosuchnode", false);
    event = this.getEvent(10);
    if (event == null) {
        throw new Error("First event was not delivered promptly");
    }
    if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || (event.getType() == EventType.NodeDeleted && event.getPath().equalsIgnoreCase(nodeName)))) {
        System.out.print(parentName + " " + EventType.NodeChildrenChanged + " " + nodeName + " " + EventType.NodeDeleted);
        fail("Unexpected first event was delivered: " + event.toString());
    }
    event = this.getEvent(10);
    if (event == null) {
        throw new Error("Second event was not delivered promptly");
    }
    if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || (event.getType() == EventType.NodeDeleted && event.getPath().equalsIgnoreCase(nodeName)))) {
        System.out.print(parentName + " " + EventType.NodeChildrenChanged + " " + nodeName + " " + EventType.NodeDeleted);
        fail("Unexpected second event was delivered: " + event.toString());
    }
    firstGen1 = zk_1.getChildren(parentName, false);
    stat_node = zk_1.exists(nodeName, false);
    if (stat_node != null) {
        fail("node " + nodeName + " should have been deleted");
    }
    if (firstGen1.contains(nodeName)) {
        fail("node " + nodeName + " should not be a children");
    }
    deleteZKDir(zk_1, nodeName);
    zk_1.close();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) IOException(java.io.IOException) Code(org.apache.zookeeper.KeeperException.Code) KeeperException(org.apache.zookeeper.KeeperException)

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