Search in sources :

Example 41 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent 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 42 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent 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 43 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 44 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent 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)

Example 45 with WatchedEvent

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

the class ZkStateReader method createClusterStateWatchersAndUpdate.

public synchronized void createClusterStateWatchersAndUpdate() throws KeeperException, InterruptedException {
    // We need to fetch the current cluster state and the set of live nodes
    LOG.debug("Updating cluster state from ZooKeeper... ");
    // Sanity check ZK structure.
    if (!zkClient.exists(CLUSTER_STATE, true)) {
        throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "Cannot connect to cluster at " + zkClient.getZkServerAddress() + ": cluster not found/not ready");
    }
    // on reconnect of SolrZkClient force refresh and re-add watches.
    loadClusterProperties();
    refreshLiveNodes(new LiveNodeWatcher());
    refreshLegacyClusterState(new LegacyClusterStateWatcher());
    refreshStateFormat2Collections();
    refreshCollectionList(new CollectionsChildWatcher());
    synchronized (ZkStateReader.this.getUpdateLock()) {
        constructState(Collections.emptySet());
        zkClient.exists(ALIASES, new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                // session events are not change events, and do not remove the watcher
                if (EventType.None.equals(event.getType())) {
                    return;
                }
                try {
                    synchronized (ZkStateReader.this.getUpdateLock()) {
                        LOG.debug("Updating aliases... ");
                        // remake watch
                        final Watcher thisWatch = this;
                        final Stat stat = new Stat();
                        final byte[] data = zkClient.getData(ALIASES, thisWatch, stat, true);
                        ZkStateReader.this.aliases = ClusterState.load(data);
                        LOG.debug("New alias definition is: " + ZkStateReader.this.aliases.toString());
                    }
                } catch (KeeperException.ConnectionLossException | KeeperException.SessionExpiredException e) {
                    LOG.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK: [{}]", e.getMessage());
                } catch (KeeperException e) {
                    LOG.error("A ZK error has occurred", e);
                    throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "A ZK error has occurred", e);
                } catch (InterruptedException e) {
                    // Restore the interrupted status
                    Thread.currentThread().interrupt();
                    LOG.warn("Interrupted", e);
                }
            }
        }, true);
    }
    updateAliases();
    if (securityNodeListener != null) {
        addSecuritynodeWatcher(pair -> {
            ConfigData cd = new ConfigData();
            cd.data = pair.first() == null || pair.first().length == 0 ? EMPTY_MAP : Utils.getDeepCopy((Map) fromJSON(pair.first()), 4, false);
            cd.version = pair.second() == null ? -1 : pair.second().getVersion();
            securityData = cd;
            securityNodeListener.run();
        });
        securityData = getSecurityProps(true);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) SolrException(org.apache.solr.common.SolrException) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)97 Watcher (org.apache.zookeeper.Watcher)61 ZooKeeper (org.apache.zookeeper.ZooKeeper)36 KeeperException (org.apache.zookeeper.KeeperException)35 Test (org.junit.Test)32 Stat (org.apache.zookeeper.data.Stat)28 CountDownLatch (java.util.concurrent.CountDownLatch)25 Test (org.testng.annotations.Test)14 IOException (java.io.IOException)12 AsyncCallback (org.apache.zookeeper.AsyncCallback)11 None (com.linkedin.common.util.None)7 List (java.util.List)6 TimeoutException (java.util.concurrent.TimeoutException)4 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)4 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)4 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)4 WatcherEvent (org.apache.zookeeper.proto.WatcherEvent)4 ZooKeeperx (com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx)3 FutureCallback (com.linkedin.common.callback.FutureCallback)3 Set (java.util.Set)3