Search in sources :

Example 66 with ZooKeeper

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)));
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Test(org.junit.Test)

Example 67 with ZooKeeper

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();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 68 with ZooKeeper

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();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper)

Example 69 with ZooKeeper

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();
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZooKeeper(org.apache.zookeeper.ZooKeeper) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback)

Example 70 with ZooKeeper

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();
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZooKeeper(org.apache.zookeeper.ZooKeeper) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)605 Test (org.junit.jupiter.api.Test)190 KeeperException (org.apache.zookeeper.KeeperException)153 Test (org.junit.Test)100 Stat (org.apache.zookeeper.data.Stat)86 IOException (java.io.IOException)83 CountDownLatch (java.util.concurrent.CountDownLatch)80 WatchedEvent (org.apache.zookeeper.WatchedEvent)70 Watcher (org.apache.zookeeper.Watcher)59 ArrayList (java.util.ArrayList)47 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)41 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)38 Timeout (org.junit.jupiter.api.Timeout)32 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)31 File (java.io.File)28 HashMap (java.util.HashMap)26 CompletableFuture (java.util.concurrent.CompletableFuture)22 Test (org.testng.annotations.Test)21 AsyncCallback (org.apache.zookeeper.AsyncCallback)19 ACL (org.apache.zookeeper.data.ACL)19