use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class TestZKSessionLock method testParseClientID.
@Test(timeout = 60000)
public void testParseClientID() throws Exception {
ZooKeeper zk = zkc.get();
String lockPath = "/test-parse-clientid";
String clientId = "test-parse-clientid-" + System.currentTimeMillis();
Pair<String, Long> lockId = Pair.of(clientId, zk.getSessionId());
createLockPath(zk, lockPath);
// Correct data
String node1 = getLockIdFromPath(createLockNodeV1(zk, lockPath, clientId));
String node2 = getLockIdFromPath(createLockNodeV2(zk, lockPath, clientId));
String node3 = getLockIdFromPath(createLockNodeV3(zk, lockPath, clientId));
assertEquals(lockId, Utils.ioResult(asyncParseClientID(zk, lockPath, node1)));
assertEquals(lockId, Utils.ioResult(asyncParseClientID(zk, lockPath, node2)));
assertEquals(lockId, Utils.ioResult(asyncParseClientID(zk, lockPath, node3)));
// Bad Lock Node Name
String node4 = getLockIdFromPath(createLockNodeWithBadNodeName(zk, lockPath, clientId, "member"));
String node5 = getLockIdFromPath(createLockNodeWithBadNodeName(zk, lockPath, clientId, "member_badnode"));
String node6 = getLockIdFromPath(createLockNodeWithBadNodeName(zk, lockPath, clientId, "member_badnode_badnode"));
String node7 = getLockIdFromPath(createLockNodeWithBadNodeName(zk, lockPath, clientId, "member_badnode_badnode_badnode"));
String node8 = getLockIdFromPath(createLockNodeWithBadNodeName(zk, lockPath, clientId, "member_badnode_badnode_badnode_badnode"));
assertEquals(lockId, Utils.ioResult(asyncParseClientID(zk, lockPath, node4)));
assertEquals(lockId, Utils.ioResult(asyncParseClientID(zk, lockPath, node5)));
assertEquals(lockId, Utils.ioResult(asyncParseClientID(zk, lockPath, node6)));
assertEquals(lockId, Utils.ioResult(asyncParseClientID(zk, lockPath, node7)));
assertEquals(lockId, Utils.ioResult(asyncParseClientID(zk, lockPath, node8)));
// Malformed Node Name
String node9 = getLockIdFromPath(createLockNodeWithBadNodeName(zk, lockPath, clientId, "member_malformed_s12345678_999999"));
assertEquals(Pair.of("malformed", 12345678L), Utils.ioResult(asyncParseClientID(zk, lockPath, node9)));
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class TestBookieWatcher method expireZooKeeperSession.
private void expireZooKeeperSession(ZooKeeper zk, int timeout) throws IOException, InterruptedException, KeeperException {
final CountDownLatch latch = new CountDownLatch(1);
ZooKeeper newZk = new ZooKeeper(zkUtil.getZooKeeperConnectString(), timeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
latch.countDown();
}
}
}, zk.getSessionId(), zk.getSessionPasswd());
if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
}
newZk.close();
}
use of org.apache.zookeeper.ZooKeeper in project bookkeeper by apache.
the class EnableZkSecurityBasicTest method checkAllAcls.
private void checkAllAcls() throws IOException, InterruptedException, KeeperException {
ZooKeeper zk = ZooKeeperClient.newBuilder().connectString(zkUtil.getZooKeeperConnectString()).sessionTimeoutMs(20000).build();
checkACls(zk, "/");
zk.close();
}
use of org.apache.zookeeper.ZooKeeper 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.ZooKeeper 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();
}
Aggregations