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);
}
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();
}
Aggregations