Search in sources :

Example 1 with DataCallback

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

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

the class TestZooKeeperClient method testGetSetData.

@Test
public void testGetSetData() 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 = "/testGetSetData";
    byte[] data = "test".getBytes();
    // create a node and call async getData method and verify its return value
    logger.info("Create znode " + path);
    client.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    CountDownLatch latch = new CountDownLatch(1);
    final Stat[] statArray = { null };
    final int[] rcArray = { -1 };
    final byte[][] dataArray = { {} };
    client.getData(path, true, new DataCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            CountDownLatch cdLatch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            statArray[0] = stat;
            dataArray[0] = data;
            cdLatch.countDown();
        }
    }, latch);
    latch.await(3000, TimeUnit.MILLISECONDS);
    if (rcArray[0] != KeeperException.Code.OK.intValue()) {
        Assert.fail("Test1 - getData call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
    }
    Assert.assertArrayEquals("Test1 - getData output - ", data, dataArray[0]);
    Stat stat = statArray[0];
    // expire the ZKClient session and then call async setData with new data
    expireZooKeeperSession(client, timeout);
    latch = new CountDownLatch(1);
    data = "newtest".getBytes();
    client.setData(path, data, stat.getVersion(), 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);
    Assert.assertTrue("setData operation should have completed successfully", latch.await(6000, TimeUnit.MILLISECONDS));
    if (rcArray[0] != KeeperException.Code.OK.intValue()) {
        Assert.fail("Test2 - setData call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
    }
    stat = statArray[0];
    // call getData
    byte[] getDataRet = client.getData(path, null, stat);
    Assert.assertArrayEquals("Test3 - getData output - ", data, getDataRet);
    // call setdata and then async getData call
    data = "newesttest".getBytes();
    stat = client.setData(path, data, stat.getVersion());
    latch = new CountDownLatch(1);
    client.getData(path, null, new DataCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            CountDownLatch cdLatch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            statArray[0] = stat;
            dataArray[0] = data;
            cdLatch.countDown();
        }
    }, latch);
    latch.await(3000, TimeUnit.MILLISECONDS);
    if (rcArray[0] != KeeperException.Code.OK.intValue()) {
        Assert.fail("Test4 - getData call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
    }
    Assert.assertArrayEquals("Test4 - getData output - ", data, dataArray[0]);
    stat = statArray[0];
    client.delete(path, stat.getVersion());
}
Also used : Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) Stat(org.apache.zookeeper.data.Stat) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Test(org.junit.Test)

Example 3 with DataCallback

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

the class AbstractZkLedgerManager method readLedgerMetadata.

protected void readLedgerMetadata(final long ledgerId, final GenericCallback<LedgerMetadata> readCb, Watcher watcher) {
    zk.getData(getLedgerPath(ledgerId), watcher, new DataCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            if (rc == KeeperException.Code.NONODE.intValue()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("No such ledger: " + ledgerId, KeeperException.create(KeeperException.Code.get(rc), path));
                }
                readCb.operationComplete(BKException.Code.NoSuchLedgerExistsException, null);
                return;
            }
            if (rc != KeeperException.Code.OK.intValue()) {
                LOG.error("Could not read metadata for ledger: " + ledgerId, KeeperException.create(KeeperException.Code.get(rc), path));
                readCb.operationComplete(BKException.Code.ZKException, null);
                return;
            }
            if (stat == null) {
                LOG.error("Could not parse ledger metadata for ledger: {}. Stat object is null", ledgerId);
                readCb.operationComplete(BKException.Code.ZKException, null);
                return;
            }
            LedgerMetadata metadata;
            try {
                metadata = LedgerMetadata.parseConfig(data, new LongVersion(stat.getVersion()), Optional.of(stat.getCtime()));
            } catch (IOException e) {
                LOG.error("Could not parse ledger metadata for ledger: " + ledgerId, e);
                readCb.operationComplete(BKException.Code.ZKException, null);
                return;
            }
            readCb.operationComplete(BKException.Code.OK, metadata);
        }
    }, null);
}
Also used : Stat(org.apache.zookeeper.data.Stat) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) LongVersion(org.apache.bookkeeper.versioning.LongVersion) IOException(java.io.IOException) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback)

Example 4 with DataCallback

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

the class ZooKeeperClient method getData.

@Override
public void getData(final String path, final Watcher watcher, final DataCallback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, getStats) {

        final DataCallback dataCb = new DataCallback() {

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

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                ZooKeeperClient.super.getData(path, watcher, dataCb, worker);
            } else {
                zkHandle.getData(path, watcher, dataCb, worker);
            }
        }

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

Example 5 with DataCallback

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

the class ZooKeeperClient method getData.

@Override
public void getData(final String path, final boolean watch, final DataCallback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, getStats) {

        final DataCallback dataCb = new DataCallback() {

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

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                ZooKeeperClient.super.getData(path, watch, dataCb, worker);
            } else {
                zkHandle.getData(path, watch, dataCb, worker);
            }
        }

        @Override
        public String toString() {
            return String.format("getData (%s, watcher = %s)", path, watch);
        }
    };
    // execute it immediately
    proc.run();
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZooKeeper(org.apache.zookeeper.ZooKeeper) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback)

Aggregations

DataCallback (org.apache.zookeeper.AsyncCallback.DataCallback)7 Stat (org.apache.zookeeper.data.Stat)6 CountDownLatch (java.util.concurrent.CountDownLatch)3 Watcher (org.apache.zookeeper.Watcher)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Random (java.util.Random)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)1 LongVersion (org.apache.bookkeeper.versioning.LongVersion)1 Children2Callback (org.apache.zookeeper.AsyncCallback.Children2Callback)1 Create2Callback (org.apache.zookeeper.AsyncCallback.Create2Callback)1 StringCallback (org.apache.zookeeper.AsyncCallback.StringCallback)1 VoidCallback (org.apache.zookeeper.AsyncCallback.VoidCallback)1