Search in sources :

Example 1 with StatCallback

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

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

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

the class AbstractZkLedgerManager method writeLedgerMetadata.

@Override
public void writeLedgerMetadata(final long ledgerId, final LedgerMetadata metadata, final GenericCallback<Void> cb) {
    Version v = metadata.getVersion();
    if (!(v instanceof LongVersion)) {
        cb.operationComplete(BKException.Code.MetadataVersionException, null);
        return;
    }
    final LongVersion zv = (LongVersion) v;
    zk.setData(getLedgerPath(ledgerId), metadata.serialize(), (int) zv.getLongVersion(), new StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            if (KeeperException.Code.BADVERSION.intValue() == rc) {
                cb.operationComplete(BKException.Code.MetadataVersionException, null);
            } else if (KeeperException.Code.OK.intValue() == rc) {
                // update metadata version
                metadata.setVersion(zv.setLongVersion(stat.getVersion()));
                cb.operationComplete(BKException.Code.OK, null);
            } else {
                LOG.warn("Conditional update ledger metadata failed: {}", KeeperException.Code.get(rc));
                cb.operationComplete(BKException.Code.ZKException, null);
            }
        }
    }, null);
}
Also used : Stat(org.apache.zookeeper.data.Stat) LongVersion(org.apache.bookkeeper.versioning.LongVersion) Version(org.apache.bookkeeper.versioning.Version) LongVersion(org.apache.bookkeeper.versioning.LongVersion) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback)

Example 4 with StatCallback

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

the class ZooKeeperClient method setData.

@Override
public void setData(final String path, final byte[] data, final int version, final StatCallback cb, final Object context) {
    final Runnable proc = new ZkRetryRunnable(operationRetryPolicy, rateLimiter, setStats) {

        final StatCallback stCb = new StatCallback() {

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

        @Override
        void zkRun() {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                ZooKeeperClient.super.setData(path, data, version, stCb, worker);
            } else {
                zkHandle.setData(path, data, version, stCb, worker);
            }
        }

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

Example 5 with StatCallback

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

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