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