Search in sources :

Example 1 with ACLCallback

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

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

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

the class ZooKeeperClient method getACL.

@Override
public void getACL(final String path, final Stat stat, final ACLCallback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, getACLStats) {

        final ACLCallback aclCb = new ACLCallback() {

            @Override
            public void processResult(int rc, String path, Object ctx, List<ACL> acl, Stat stat) {
                ZooWorker worker = (ZooWorker) ctx;
                if (allowRetry(worker, rc)) {
                    backOffAndRetry(that, worker.nextRetryWaitTime());
                } else {
                    cb.processResult(rc, path, context, acl, stat);
                }
            }
        };

        @Override
        public String toString() {
            return String.format("getACL (%s, stat = %s)", path, stat);
        }

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                ZooKeeperClient.super.getACL(path, stat, aclCb, worker);
            } else {
                zkHandle.getACL(path, stat, aclCb, worker);
            }
        }
    };
    // execute it immediately
    proc.run();
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZooKeeper(org.apache.zookeeper.ZooKeeper) ACL(org.apache.zookeeper.data.ACL) ACLCallback(org.apache.zookeeper.AsyncCallback.ACLCallback)

Aggregations

ACLCallback (org.apache.zookeeper.AsyncCallback.ACLCallback)3 ACL (org.apache.zookeeper.data.ACL)3 Stat (org.apache.zookeeper.data.Stat)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)2 Watcher (org.apache.zookeeper.Watcher)2 Test (org.junit.Test)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)1