Search in sources :

Example 31 with Watcher

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

the class LocalDLMEmulator method connectZooKeeper.

public static ZooKeeper connectZooKeeper(String zkHost, int zkPort, int zkTimeoutSec) throws IOException, KeeperException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final String zkHostPort = zkHost + ":" + zkPort;
    ZooKeeper zkc = new ZooKeeper(zkHostPort, zkTimeoutSec * 1000, new Watcher() {

        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.SyncConnected) {
                latch.countDown();
            }
        }
    });
    if (!latch.await(zkTimeoutSec, TimeUnit.SECONDS)) {
        throw new IOException("Zookeeper took too long to connect");
    }
    return zkc;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 32 with Watcher

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

the class ZooKeeperClient method registerExpirationHandler.

/**
     * Clients that need to re-establish state after session expiration can register an
     * {@code onExpired} command to execute.
     *
     * @param onExpired the {@code Command} to register
     * @return the new {@link Watcher} which can later be passed to {@link #unregister} for
     *         removal.
     */
public Watcher registerExpirationHandler(final ZooKeeperSessionExpireNotifier onExpired) {
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
                try {
                    onExpired.notifySessionExpired();
                } catch (Exception exc) {
                // do nothing
                }
            }
        }
    };
    register(watcher);
    return watcher;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 33 with Watcher

use of org.apache.zookeeper.Watcher 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 34 with Watcher

use of org.apache.zookeeper.Watcher in project heron by twitter.

the class CuratorStateManager method getNodeData.

@Override
protected <M extends Message> ListenableFuture<M> getNodeData(WatchCallback watcher, String path, final Message.Builder builder) {
    final SettableFuture<M> future = SettableFuture.create();
    Watcher wc = ZkWatcherCallback.makeZkWatcher(watcher);
    BackgroundCallback cb = new BackgroundCallback() {

        @Override
        // we don't know what M is until runtime
        @SuppressWarnings("unchecked")
        public void processResult(CuratorFramework aClient, CuratorEvent event) throws Exception {
            byte[] data;
            if (event != null & (data = event.getData()) != null) {
                builder.mergeFrom(data);
                future.set((M) builder.build());
            } else {
                future.setException(new RuntimeException("Failed to fetch data from path: " + event.getPath()));
            }
        }
    };
    try {
        client.getData().usingWatcher(wc).inBackground(cb).forPath(path);
    // Suppress it since forPath() throws Exception
    // SUPPRESS CHECKSTYLE IllegalCatch
    } catch (Exception e) {
        future.setException(new RuntimeException("Could not getNodeData", e));
    }
    return future;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Watcher(org.apache.zookeeper.Watcher) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) KeeperException(org.apache.zookeeper.KeeperException) ExecutionException(java.util.concurrent.ExecutionException)

Example 35 with Watcher

use of org.apache.zookeeper.Watcher in project lucene-solr by apache.

the class ZkSolrClientTest method testWatchChildren.

public void testWatchChildren() throws Exception {
    try (ZkConnection conn = new ZkConnection()) {
        final SolrZkClient zkClient = conn.getClient();
        final AtomicInteger cnt = new AtomicInteger();
        final CountDownLatch latch = new CountDownLatch(1);
        zkClient.makePath("/collections", true);
        zkClient.getChildren("/collections", new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                cnt.incrementAndGet();
                // remake watch
                try {
                    zkClient.getChildren("/collections", this, true);
                    latch.countDown();
                } catch (KeeperException | InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, true);
        zkClient.makePath("/collections/collection99/shards", true);
        //wait until watch has been re-created
        latch.await();
        zkClient.makePath("collections/collection99/config=collection1", true);
        zkClient.makePath("collections/collection99/config=collection3", true);
        zkClient.makePath("/collections/collection97/shards", true);
        // pause for the watches to fire
        Thread.sleep(700);
        if (cnt.intValue() < 2) {
            // wait a bit more
            Thread.sleep(4000);
        }
        if (cnt.intValue() < 2) {
            // wait a bit more
            Thread.sleep(4000);
        }
        assertEquals(2, cnt.intValue());
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient)

Aggregations

Watcher (org.apache.zookeeper.Watcher)78 WatchedEvent (org.apache.zookeeper.WatchedEvent)62 KeeperException (org.apache.zookeeper.KeeperException)34 CountDownLatch (java.util.concurrent.CountDownLatch)26 ZooKeeper (org.apache.zookeeper.ZooKeeper)24 Stat (org.apache.zookeeper.data.Stat)21 Test (org.junit.Test)18 IOException (java.io.IOException)11 AsyncCallback (org.apache.zookeeper.AsyncCallback)10 List (java.util.List)8 Test (org.testng.annotations.Test)8 None (com.linkedin.common.util.None)7 HashSet (java.util.HashSet)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Map (java.util.Map)5 Set (java.util.Set)5 HashMap (java.util.HashMap)4 TimeoutException (java.util.concurrent.TimeoutException)4 FutureCallback (com.google.common.util.concurrent.FutureCallback)3 FutureCallback (com.linkedin.common.callback.FutureCallback)3