Search in sources :

Example 61 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project distributedlog by twitter.

the class ZooKeeperClient method buildZooKeeper.

private ZooKeeper buildZooKeeper() throws ZooKeeperConnectionException, InterruptedException {
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            switch(event.getType()) {
                case None:
                    switch(event.getState()) {
                        case Expired:
                            if (null == retryPolicy) {
                                LOG.info("ZooKeeper {}' session expired. Event: {}", name, event);
                                closeInternal();
                            }
                            authenticated = false;
                            break;
                        case Disconnected:
                            if (null == retryPolicy) {
                                LOG.info("ZooKeeper {} is disconnected from zookeeper now," + " but it is OK unless we received EXPIRED event.", name);
                            }
                            // Mark as not authenticated if expired or disconnected. In both cases
                            // we lose any attached auth info. Relying on Expired/Disconnected is
                            // sufficient since all Expired/Disconnected events are processed before
                            // all SyncConnected events, and the underlying member is not updated until
                            // SyncConnected is received.
                            authenticated = false;
                            break;
                        default:
                            break;
                    }
            }
            try {
                for (Watcher watcher : watchers) {
                    try {
                        watcher.process(event);
                    } catch (Throwable t) {
                        LOG.warn("Encountered unexpected exception from watcher {} : ", watcher, t);
                    }
                }
            } catch (Throwable t) {
                LOG.warn("Encountered unexpected exception when firing watched event {} : ", event, t);
            }
        }
    };
    Set<Watcher> watchers = new HashSet<Watcher>();
    watchers.add(watcher);
    ZooKeeper zk;
    try {
        RetryPolicy opRetryPolicy = null == retryPolicy ? new BoundExponentialBackoffRetryPolicy(sessionTimeoutMs, sessionTimeoutMs, 0) : retryPolicy;
        RetryPolicy connectRetryPolicy = null == retryPolicy ? new BoundExponentialBackoffRetryPolicy(sessionTimeoutMs, sessionTimeoutMs, 0) : new BoundExponentialBackoffRetryPolicy(sessionTimeoutMs, sessionTimeoutMs, Integer.MAX_VALUE);
        zk = org.apache.bookkeeper.zookeeper.ZooKeeperClient.newBuilder().connectString(zooKeeperServers).sessionTimeoutMs(sessionTimeoutMs).watchers(watchers).operationRetryPolicy(opRetryPolicy).connectRetryPolicy(connectRetryPolicy).statsLogger(statsLogger).retryThreadCount(retryThreadCount).requestRateLimit(requestRateLimit).build();
    } catch (KeeperException e) {
        throw new ZooKeeperConnectionException("Problem connecting to servers: " + zooKeeperServers, e);
    } catch (IOException e) {
        throw new ZooKeeperConnectionException("Problem connecting to servers: " + zooKeeperServers, e);
    }
    return zk;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) BoundExponentialBackoffRetryPolicy(org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy) RetryPolicy(org.apache.bookkeeper.zookeeper.RetryPolicy) BoundExponentialBackoffRetryPolicy(org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy) KeeperException(org.apache.zookeeper.KeeperException) HashSet(java.util.HashSet)

Example 62 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project distributedlog by twitter.

the class TestZooKeeperClient method testExceptionOnWatchers.

@Test(timeout = 60000)
public void testExceptionOnWatchers() throws Exception {
    TestWatcher w1 = new TestWatcher();
    TestWatcher w2 = new TestWatcher();
    final CountDownLatch latch = new CountDownLatch(2);
    w1.setLatch(latch);
    w2.setLatch(latch);
    zkc.register(w1);
    zkc.register(w2);
    // register bad watcher
    zkc.register(new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            throw new NullPointerException("bad watcher returning null");
        }
    });
    assertEquals(3, zkc.watchers.size());
    final String zkPath = "/test-exception-on-watchers";
    zkc.get().create(zkPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zkc.get().getData(zkPath, true, new Stat());
    zkc.get().setData(zkPath, "first-set".getBytes(), -1);
    latch.await();
    assertEquals(1, w1.receivedEvents.size());
    assertEquals(zkPath, w1.receivedEvents.get(0).getPath());
    assertEquals(Watcher.Event.EventType.NodeDataChanged, w1.receivedEvents.get(0).getType());
    assertEquals(1, w2.receivedEvents.size());
    assertEquals(zkPath, w2.receivedEvents.get(0).getPath());
    assertEquals(Watcher.Event.EventType.NodeDataChanged, w2.receivedEvents.get(0).getType());
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 63 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project whirr by apache.

the class Cdh3ZooKeeperServiceTest method test.

@Test
public void test() throws Exception {
    class ConnectionWatcher implements Watcher {

        private ZooKeeper zk;

        private CountDownLatch latch = new CountDownLatch(1);

        public void connect(String hosts) throws IOException, InterruptedException {
            zk = new ZooKeeper(hosts, 5000, this);
            latch.await();
        }

        public ZooKeeper getZooKeeper() {
            return zk;
        }

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }

        public void close() throws InterruptedException {
            if (zk != null) {
                zk.close();
            }
        }
    }
    String path = "/data";
    String data = "Hello";
    ConnectionWatcher watcher = new ConnectionWatcher();
    watcher.connect(hosts);
    watcher.getZooKeeper().create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    watcher.close();
    watcher = new ConnectionWatcher();
    watcher.connect(hosts);
    byte[] actualData = watcher.getZooKeeper().getData(path, false, null);
    assertEquals(data, new String(actualData));
    watcher.close();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 64 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project whirr by apache.

the class ZooKeeperServiceTest method test.

@Test(timeout = TestConstants.ITEST_TIMEOUT)
public void test() throws Exception {
    class ConnectionWatcher implements Watcher {

        private ZooKeeper zk;

        private CountDownLatch latch = new CountDownLatch(1);

        public void connect(String hosts) throws IOException, InterruptedException {
            zk = new ZooKeeper(hosts, 5000, this);
            latch.await();
        }

        public ZooKeeper getZooKeeper() {
            return zk;
        }

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }

        public void close() throws InterruptedException {
            if (zk != null) {
                zk.close();
            }
        }
    }
    String path = "/data";
    String data = "Hello";
    ConnectionWatcher watcher = new ConnectionWatcher();
    watcher.connect(hosts);
    watcher.getZooKeeper().create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    watcher.close();
    watcher = new ConnectionWatcher();
    watcher.connect(hosts);
    byte[] actualData = watcher.getZooKeeper().getData(path, false, null);
    assertEquals(data, new String(actualData));
    watcher.close();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 65 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project otter by alibaba.

the class DistributedLock method acquireLock.

// ===================== helper method =============================
/**
 * 执行lock操作,允许传递watch变量控制是否需要阻塞lock操作
 */
private Boolean acquireLock(final BooleanMutex mutex) {
    try {
        do {
            if (id == null) {
                // 构建当前lock的唯一标识
                long sessionId = getSessionId();
                String prefix = "x-" + sessionId + "-";
                // 如果第一次,则创建一个节点
                String path = zookeeper.create(root + "/" + prefix, data, CreateMode.EPHEMERAL_SEQUENTIAL);
                int index = path.lastIndexOf("/");
                id = StringUtils.substring(path, index + 1);
                idName = new LockNode(id);
            }
            if (id != null) {
                List<String> names = zookeeper.getChildren(root);
                if (names.isEmpty()) {
                    logger.warn("lock lost with scene:empty list, id[] and node[]", id, idName);
                    // 异常情况,退出后重新创建一个
                    unlock();
                } else {
                    // 对节点进行排序
                    SortedSet<LockNode> sortedNames = new TreeSet<LockNode>();
                    for (String name : names) {
                        sortedNames.add(new LockNode(name));
                    }
                    if (sortedNames.contains(idName) == false) {
                        logger.warn("lock lost with scene:not contains ,id[] and node[]", id, idName);
                        // 异常情况,退出后重新创建一个
                        unlock();
                        continue;
                    }
                    // 将第一个节点做为ownerId
                    ownerId = sortedNames.first().getName();
                    if (mutex != null && isOwner()) {
                        // 直接更新状态,返回
                        mutex.set(true);
                        return true;
                    } else if (mutex == null) {
                        return isOwner();
                    }
                    SortedSet<LockNode> lessThanMe = sortedNames.headSet(idName);
                    if (!lessThanMe.isEmpty()) {
                        // 关注一下排队在自己之前的最近的一个节点
                        LockNode lastChildName = lessThanMe.last();
                        lastChildId = lastChildName.getName();
                        // 异步watcher处理
                        IZkConnection connection = zookeeper.getConnection();
                        // zkclient包装的是一个持久化的zk,分布式lock只需要一次性的watcher,需要调用原始的zk链接进行操作
                        ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper();
                        Stat stat = orginZk.exists(root + "/" + lastChildId, new AsyncWatcher() {

                            public void asyncProcess(WatchedEvent event) {
                                if (!mutex.state()) {
                                    // 避免重复获取lock
                                    acquireLock(mutex);
                                } else {
                                    logger.warn("locked successful.");
                                }
                            }
                        });
                        if (stat == null) {
                            // 如果节点不存在,需要自己重新触发一下,watcher不会被挂上去
                            acquireLock(mutex);
                        }
                    } else {
                        if (isOwner()) {
                            mutex.set(true);
                        } else {
                            logger.warn("lock lost with scene:no less ,id[] and node[]", id, idName);
                            // 可能自己的节点已超时挂了,所以id和ownerId不相同
                            unlock();
                        }
                    }
                }
            }
        } while (id == null);
    } catch (KeeperException e) {
        exception = e;
        if (mutex != null) {
            mutex.set(true);
        }
    } catch (InterruptedException e) {
        interrupt = e;
        if (mutex != null) {
            mutex.set(true);
        }
    } catch (Throwable e) {
        other = e;
        if (mutex != null) {
            mutex.set(true);
        }
    }
    if (isOwner() && mutex != null) {
        mutex.set(true);
    }
    return Boolean.FALSE;
}
Also used : IZkConnection(org.I0Itec.zkclient.IZkConnection) ZkInterruptedException(org.I0Itec.zkclient.exception.ZkInterruptedException) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) TreeSet(java.util.TreeSet) ZooKeeperx(com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx) KeeperException(org.apache.zookeeper.KeeperException) AsyncWatcher(com.alibaba.otter.shared.arbitrate.impl.zookeeper.AsyncWatcher)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)194 Watcher (org.apache.zookeeper.Watcher)123 ZooKeeper (org.apache.zookeeper.ZooKeeper)67 CountDownLatch (java.util.concurrent.CountDownLatch)64 KeeperException (org.apache.zookeeper.KeeperException)56 Test (org.junit.Test)38 Stat (org.apache.zookeeper.data.Stat)35 IOException (java.io.IOException)31 Test (org.testng.annotations.Test)27 Test (org.junit.jupiter.api.Test)18 CuratorFramework (org.apache.curator.framework.CuratorFramework)14 AsyncCallback (org.apache.zookeeper.AsyncCallback)14 List (java.util.List)10 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)10 Set (java.util.Set)7 TimeoutException (java.util.concurrent.TimeoutException)7 HashSet (java.util.HashSet)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RetryOneTime (org.apache.curator.retry.RetryOneTime)6