Search in sources :

Example 6 with StatCallback

use of org.apache.zookeeper.AsyncCallback.StatCallback 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 7 with StatCallback

use of org.apache.zookeeper.AsyncCallback.StatCallback in project bookkeeper by apache.

the class TestZooKeeperClient method testACLSetAndGet.

@Test
public void testACLSetAndGet() throws Exception {
    final int timeout = 2000;
    ZooKeeperClient client = ZooKeeperClient.createConnectedZooKeeperClient(zkUtil.getZooKeeperConnectString(), timeout, new HashSet<Watcher>(), new BoundExponentialBackoffRetryPolicy(timeout, timeout, Integer.MAX_VALUE));
    Assert.assertTrue("Client failed to connect an alive ZooKeeper.", client.getState().isConnected());
    String path = "/testACLSetAndGet";
    byte[] data = "test".getBytes();
    // create a node and call getACL to verify the received ACL
    logger.info("Create znode " + path);
    List<ACL> setACLList = new ArrayList<ACL>();
    setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
    client.create(path, data, setACLList, CreateMode.PERSISTENT);
    Stat status = new Stat();
    List<ACL> receivedACLList = client.getACL(path, status);
    Assert.assertEquals("Test1 - ACLs are expected to match", setACLList, receivedACLList);
    // update node's ACL and call getACL to verify the received ACL
    setACLList.clear();
    setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
    setACLList.addAll(Ids.READ_ACL_UNSAFE);
    status = client.setACL(path, setACLList, status.getAversion());
    receivedACLList = client.getACL(path, status);
    Assert.assertEquals("Test2 - ACLs are expected to match", setACLList, receivedACLList);
    // update node's ACL by calling async setACL and call async getACL to verify the received ACL
    setACLList.clear();
    setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
    CountDownLatch latch = new CountDownLatch(1);
    final Stat[] statArray = { null };
    final int[] rcArray = { -1 };
    client.setACL(path, setACLList, status.getAversion(), new StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            CountDownLatch cdLatch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            statArray[0] = stat;
            cdLatch.countDown();
        }
    }, latch);
    latch.await(3000, TimeUnit.MILLISECONDS);
    if (rcArray[0] != KeeperException.Code.OK.intValue()) {
        Assert.fail("Test3 - SetACL call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
    }
    status = statArray[0];
    latch = new CountDownLatch(1);
    rcArray[0] = 0;
    statArray[0] = null;
    final List<List<ACL>> aclListArray = new ArrayList<List<ACL>>(1);
    client.getACL(path, status, new ACLCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<ACL> acl, Stat stat) {
            CountDownLatch cdLatch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            statArray[0] = stat;
            aclListArray.add(acl);
            cdLatch.countDown();
        }
    }, latch);
    latch.await(3000, TimeUnit.MILLISECONDS);
    if (rcArray[0] != KeeperException.Code.OK.intValue()) {
        Assert.fail("Test4 - GetACL call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
    }
    status = statArray[0];
    receivedACLList = aclListArray.get(0);
    Assert.assertEquals("Test5 - ACLs are expected to match", setACLList, receivedACLList);
    // delete the node
    client.delete(path, status.getVersion());
}
Also used : ArrayList(java.util.ArrayList) Watcher(org.apache.zookeeper.Watcher) ACL(org.apache.zookeeper.data.ACL) CountDownLatch(java.util.concurrent.CountDownLatch) Stat(org.apache.zookeeper.data.Stat) ArrayList(java.util.ArrayList) List(java.util.List) ACLCallback(org.apache.zookeeper.AsyncCallback.ACLCallback) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Test(org.junit.Test)

Example 8 with StatCallback

use of org.apache.zookeeper.AsyncCallback.StatCallback in project bookkeeper by apache.

the class TestZooKeeperClient method testZnodeExists.

@Test
public void testZnodeExists() throws Exception {
    final int timeout = 2000;
    ZooKeeperClient client = ZooKeeperClient.createConnectedZooKeeperClient(zkUtil.getZooKeeperConnectString(), timeout, new HashSet<Watcher>(), new BoundExponentialBackoffRetryPolicy(timeout, timeout, Integer.MAX_VALUE));
    Assert.assertTrue("Client failed to connect an alive ZooKeeper.", client.getState().isConnected());
    String path = "/testZnodeExists";
    byte[] data = "test".getBytes();
    // create a node
    logger.info("Create znode " + path);
    client.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // expire the ZKClient session and then call exists method for the path
    expireZooKeeperSession(client, timeout);
    final AtomicBoolean isDeleted = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    Stat stat = client.exists(path, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == Watcher.Event.EventType.NodeDeleted) {
                isDeleted.set(true);
                latch.countDown();
            }
        }
    });
    Assert.assertNotNull("node with path " + path + " should exists", stat);
    // now delete the znode and verify if the Watcher is called
    client.delete(path, stat.getVersion());
    latch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertTrue("The watcher on the node should have been called", isDeleted.get());
    // calling async exists method and verifying the return values
    CountDownLatch latch2 = new CountDownLatch(1);
    final int[] rcArray = { -1 };
    final boolean[] statIsnull = { false };
    client.exists(path, null, new StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            CountDownLatch cdlatch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            statIsnull[0] = (stat == null);
            cdlatch.countDown();
        }
    }, latch2);
    latch2.await(3000, TimeUnit.MILLISECONDS);
    if (rcArray[0] != KeeperException.Code.NONODE.intValue()) {
        Assert.fail("exists call is supposed to return NONODE rcvalue, but it returned - " + KeeperException.Code.get(rcArray[0]));
    }
    Assert.assertTrue("exists is supposed to return null for Stat," + " since the node is already deleted", statIsnull[0]);
}
Also used : Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Stat(org.apache.zookeeper.data.Stat) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Test(org.junit.Test)

Example 9 with StatCallback

use of org.apache.zookeeper.AsyncCallback.StatCallback in project bookkeeper by apache.

the class TestZooKeeperClient method testACLSetAndGetAfterSessionExpiry.

@Test
public void testACLSetAndGetAfterSessionExpiry() throws Exception {
    final int timeout = 2000;
    ZooKeeperClient client = ZooKeeperClient.createConnectedZooKeeperClient(zkUtil.getZooKeeperConnectString(), timeout, new HashSet<Watcher>(), new BoundExponentialBackoffRetryPolicy(timeout, timeout, Integer.MAX_VALUE));
    Assert.assertTrue("Client failed to connect an alive ZooKeeper.", client.getState().isConnected());
    String path = "/testACLSetAndGetAfterSessionExpiry";
    byte[] data = "test".getBytes();
    // create a node
    logger.info("Create znode " + path);
    List<ACL> setACLList = new ArrayList<ACL>();
    setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
    client.create(path, data, setACLList, CreateMode.PERSISTENT);
    // expire the ZKClient session
    expireZooKeeperSession(client, timeout);
    // call getACL and verify if it returns the previously set ACL
    Stat status = new Stat();
    List<ACL> receivedACLList = client.getACL(path, status);
    Assert.assertEquals("Test1 - ACLs are expected to match", setACLList, receivedACLList);
    // update ACL of that node
    setACLList.clear();
    setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
    setACLList.addAll(Ids.READ_ACL_UNSAFE);
    status = client.setACL(path, setACLList, status.getAversion());
    // expire the ZKClient session
    expireZooKeeperSession(client, timeout);
    // call getACL and verify if it returns the previously set ACL
    receivedACLList = client.getACL(path, status);
    Assert.assertEquals("Test2 - ACLs are expected to match", setACLList, receivedACLList);
    // update the ACL of node by calling async setACL
    setACLList.clear();
    setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
    CountDownLatch latch = new CountDownLatch(1);
    final Stat[] statArray = { null };
    final int[] rcArray = { -1 };
    client.setACL(path, setACLList, status.getAversion(), new StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            CountDownLatch cdLatch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            statArray[0] = stat;
            cdLatch.countDown();
        }
    }, latch);
    latch.await(3000, TimeUnit.MILLISECONDS);
    if (rcArray[0] != KeeperException.Code.OK.intValue()) {
        Assert.fail("Test3 - SetACL call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
    }
    status = statArray[0];
    // expire the ZKClient session
    expireZooKeeperSession(client, timeout);
    // call async getACL and verify if it returns the previously set ACL
    latch = new CountDownLatch(1);
    rcArray[0] = 0;
    statArray[0] = null;
    final List<List<ACL>> aclListArray = new ArrayList<List<ACL>>(1);
    client.getACL(path, status, new ACLCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<ACL> acl, Stat stat) {
            CountDownLatch cdLatch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            statArray[0] = stat;
            aclListArray.add(acl);
            cdLatch.countDown();
        }
    }, latch);
    Assert.assertTrue("getACL operation should have completed successfully", latch.await(6000, TimeUnit.MILLISECONDS));
    if (rcArray[0] != KeeperException.Code.OK.intValue()) {
        Assert.fail("Test4 - GetACL call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
    }
    status = statArray[0];
    receivedACLList = aclListArray.get(0);
    Assert.assertEquals("Test5 - ACLs are expected to match", setACLList, receivedACLList);
    client.delete(path, status.getVersion());
}
Also used : ArrayList(java.util.ArrayList) Watcher(org.apache.zookeeper.Watcher) ACL(org.apache.zookeeper.data.ACL) CountDownLatch(java.util.concurrent.CountDownLatch) Stat(org.apache.zookeeper.data.Stat) ArrayList(java.util.ArrayList) List(java.util.List) ACLCallback(org.apache.zookeeper.AsyncCallback.ACLCallback) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Test(org.junit.Test)

Example 10 with StatCallback

use of org.apache.zookeeper.AsyncCallback.StatCallback in project bookkeeper by apache.

the class MockZooKeeperTestCase method mockZkSetData.

protected void mockZkSetData(String expectedLedgerPath, byte[] expectedBytes, int expectedVersion, int retCode, Stat retStat) throws Exception {
    doAnswer(invocationOnMock -> {
        String path = invocationOnMock.getArgument(0);
        StatCallback callback = invocationOnMock.getArgument(3);
        Object ctx = invocationOnMock.getArgument(4);
        callback.processResult(retCode, path, ctx, retStat);
        return null;
    }).when(mockZk).setData(eq(expectedLedgerPath), eq(expectedBytes), eq(expectedVersion), any(StatCallback.class), any());
}
Also used : StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback)

Aggregations

StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)14 Stat (org.apache.zookeeper.data.Stat)12 ZooKeeper (org.apache.zookeeper.ZooKeeper)7 Watcher (org.apache.zookeeper.Watcher)6 Test (org.junit.Test)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 VoidCallback (org.apache.zookeeper.AsyncCallback.VoidCallback)3 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 ACLCallback (org.apache.zookeeper.AsyncCallback.ACLCallback)2 DataCallback (org.apache.zookeeper.AsyncCallback.DataCallback)2 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)2 ACL (org.apache.zookeeper.data.ACL)2 Test (org.junit.jupiter.api.Test)2 IOException (java.io.IOException)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1