Search in sources :

Example 1 with Create2Callback

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

the class TestZooKeeperClient method testRetryAsyncOperations.

@Test
public void testRetryAsyncOperations() 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 = "/a";
    byte[] data = "test".getBytes();
    expireZooKeeperSession(client, timeout);
    logger.info("Create znode " + path);
    final CountDownLatch createLatch = new CountDownLatch(1);
    client.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new StringCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, String name) {
            if (KeeperException.Code.OK.intValue() == rc) {
                createLatch.countDown();
            }
        }
    }, null);
    createLatch.await();
    logger.info("Created znode " + path);
    expireZooKeeperSession(client, timeout);
    logger.info("Create znode " + path);
    final CountDownLatch create2Latch = new CountDownLatch(1);
    client.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new Create2Callback() {

        @Override
        public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
            if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                create2Latch.countDown();
            }
        }
    }, null);
    create2Latch.await();
    logger.info("Created znode " + path);
    expireZooKeeperSession(client, timeout);
    logger.info("Exists znode " + path);
    final CountDownLatch existsLatch = new CountDownLatch(1);
    client.exists(path, false, new StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                existsLatch.countDown();
            }
        }
    }, null);
    existsLatch.await();
    expireZooKeeperSession(client, timeout);
    final CountDownLatch getLatch = new CountDownLatch(1);
    logger.info("Get data from znode " + path);
    client.getData(path, false, new DataCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                getLatch.countDown();
            }
        }
    }, null);
    getLatch.await();
    expireZooKeeperSession(client, timeout);
    logger.info("Create children under znode " + path);
    final CountDownLatch createChildLatch = new CountDownLatch(1);
    client.create(path + "/children", data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new StringCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, String name) {
            if (KeeperException.Code.OK.intValue() == rc) {
                createChildLatch.countDown();
            }
        }
    }, null);
    createChildLatch.await();
    expireZooKeeperSession(client, timeout);
    final CountDownLatch getChildLatch = new CountDownLatch(1);
    final AtomicReference<List<String>> children = new AtomicReference<List<String>>();
    client.getChildren(path, false, new Children2Callback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<String> childList, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                children.set(childList);
                getChildLatch.countDown();
            }
        }
    }, null);
    getChildLatch.await();
    Assert.assertNotNull(children.get());
    Assert.assertEquals(1, children.get().size());
    Assert.assertEquals("children", children.get().get(0));
    logger.info("Get children under znode " + path);
    expireZooKeeperSession(client, timeout);
    final CountDownLatch deleteChildLatch = new CountDownLatch(1);
    client.delete(path + "/children", -1, new VoidCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx) {
            if (KeeperException.Code.OK.intValue() == rc) {
                deleteChildLatch.countDown();
            }
        }
    }, null);
    deleteChildLatch.await();
    logger.info("Delete children from znode " + path);
}
Also used : StringCallback(org.apache.zookeeper.AsyncCallback.StringCallback) Watcher(org.apache.zookeeper.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) Stat(org.apache.zookeeper.data.Stat) Children2Callback(org.apache.zookeeper.AsyncCallback.Children2Callback) ArrayList(java.util.ArrayList) List(java.util.List) Create2Callback(org.apache.zookeeper.AsyncCallback.Create2Callback) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Test(org.junit.Test)

Example 2 with Create2Callback

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

the class ZooKeeperClient method create.

@Override
public void create(final String path, final byte[] data, final List<ACL> acl, final CreateMode createMode, final Create2Callback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, createStats) {

        final Create2Callback createCb = new Create2Callback() {

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

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                ZooKeeperClient.super.create(path, data, acl, createMode, createCb, worker);
            } else {
                zkHandle.create(path, data, acl, createMode, createCb, worker);
            }
        }

        @Override
        public String toString() {
            return String.format("create (%s, acl = %s, mode = %s)", path, acl, createMode);
        }
    };
    // execute it immediately
    proc.run();
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZooKeeper(org.apache.zookeeper.ZooKeeper) Create2Callback(org.apache.zookeeper.AsyncCallback.Create2Callback)

Aggregations

Create2Callback (org.apache.zookeeper.AsyncCallback.Create2Callback)2 Stat (org.apache.zookeeper.data.Stat)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Children2Callback (org.apache.zookeeper.AsyncCallback.Children2Callback)1 DataCallback (org.apache.zookeeper.AsyncCallback.DataCallback)1 StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)1 StringCallback (org.apache.zookeeper.AsyncCallback.StringCallback)1 VoidCallback (org.apache.zookeeper.AsyncCallback.VoidCallback)1 Watcher (org.apache.zookeeper.Watcher)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 Test (org.junit.Test)1