Search in sources :

Example 21 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project weave by continuuity.

the class KillZKSession method kill.

/**
   * Kills a Zookeeper client to simulate failure scenarious during testing.
   * Callee will provide the amount of time to wait before it's considered failure
   * to kill a client.
   *
   * @param client that needs to be killed.
   * @param connectionString of Quorum
   * @param maxMs time in millisecond specifying the max time to kill a client.
   * @throws IOException When there is IO error
   * @throws InterruptedException When call has been interrupted.
   */
public static void kill(ZooKeeper client, String connectionString, int maxMs) throws IOException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    ZooKeeper zk = new ZooKeeper(connectionString, maxMs, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.SyncConnected) {
                latch.countDown();
            }
        }
    }, client.getSessionId(), client.getSessionPasswd());
    try {
        Preconditions.checkState(latch.await(maxMs, TimeUnit.MILLISECONDS), "Fail to kill ZK connection.");
    } finally {
        zk.close();
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 22 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project weave by continuuity.

the class ZKClientTest method testExpireRewatch.

@Test
public void testExpireRewatch() throws InterruptedException, IOException, ExecutionException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().setTickTime(1000).build();
    zkServer.startAndWait();
    try {
        final CountDownLatch expireReconnectLatch = new CountDownLatch(1);
        final AtomicBoolean expired = new AtomicBoolean(false);
        final ZKClientService client = ZKClientServices.delegate(ZKClients.reWatchOnExpire(ZKClientService.Builder.of(zkServer.getConnectionStr()).setSessionTimeout(2000).setConnectionWatcher(new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.Expired) {
                    expired.set(true);
                } else if (event.getState() == Event.KeeperState.SyncConnected && expired.compareAndSet(true, true)) {
                    expireReconnectLatch.countDown();
                }
            }
        }).build()));
        client.startAndWait();
        try {
            final BlockingQueue<Watcher.Event.EventType> events = new LinkedBlockingQueue<Watcher.Event.EventType>();
            client.exists("/expireRewatch", new Watcher() {

                @Override
                public void process(WatchedEvent event) {
                    client.exists("/expireRewatch", this);
                    events.add(event.getType());
                }
            });
            client.create("/expireRewatch", null, CreateMode.PERSISTENT);
            Assert.assertEquals(Watcher.Event.EventType.NodeCreated, events.poll(2, TimeUnit.SECONDS));
            KillZKSession.kill(client.getZooKeeperSupplier().get(), zkServer.getConnectionStr(), 1000);
            Assert.assertTrue(expireReconnectLatch.await(5, TimeUnit.SECONDS));
            client.delete("/expireRewatch");
            Assert.assertEquals(Watcher.Event.EventType.NodeDeleted, events.poll(4, TimeUnit.SECONDS));
        } finally {
            client.stopAndWait();
        }
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Watcher(org.apache.zookeeper.Watcher) WatchedEvent(org.apache.zookeeper.WatchedEvent) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 23 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project weave by continuuity.

the class AbstractZKServiceController method actOnExists.

private void actOnExists(final String path, final Runnable action) {
    // Watch for node existence.
    final AtomicBoolean nodeExists = new AtomicBoolean(false);
    Futures.addCallback(zkClient.exists(path, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            // Other event type would be handled by the action.
            if (event.getType() == Event.EventType.NodeCreated && nodeExists.compareAndSet(false, true)) {
                action.run();
            }
        }
    }), new FutureCallback<Stat>() {

        @Override
        public void onSuccess(Stat result) {
            if (result != null && nodeExists.compareAndSet(false, true)) {
                action.run();
            }
        }

        @Override
        public void onFailure(Throwable t) {
            LOG.error("Failed in exists call to {}. Shutting down service.", path, t);
            forceShutDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher)

Example 24 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project druid by druid-io.

the class LoadQueuePeon method processSegmentChangeRequest.

private void processSegmentChangeRequest() {
    if (currentlyProcessing == null) {
        if (!segmentsToDrop.isEmpty()) {
            currentlyProcessing = segmentsToDrop.firstEntry().getValue();
            log.info("Server[%s] dropping [%s]", basePath, currentlyProcessing.getSegmentIdentifier());
        } else if (!segmentsToLoad.isEmpty()) {
            currentlyProcessing = segmentsToLoad.firstEntry().getValue();
            log.info("Server[%s] loading [%s]", basePath, currentlyProcessing.getSegmentIdentifier());
        } else {
            return;
        }
        try {
            if (currentlyProcessing == null) {
                if (!stopped) {
                    log.makeAlert("Crazy race condition! server[%s]", basePath).emit();
                }
                actionCompleted();
                return;
            }
            log.info("Server[%s] processing segment[%s]", basePath, currentlyProcessing.getSegmentIdentifier());
            final String path = ZKPaths.makePath(basePath, currentlyProcessing.getSegmentIdentifier());
            final byte[] payload = jsonMapper.writeValueAsBytes(currentlyProcessing.getChangeRequest());
            curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, payload);
            processingExecutor.schedule(new Runnable() {

                @Override
                public void run() {
                    try {
                        if (curator.checkExists().forPath(path) != null) {
                            failAssign(new ISE("%s was never removed! Failing this operation!", path));
                        }
                    } catch (Exception e) {
                        failAssign(e);
                    }
                }
            }, config.getLoadTimeoutDelay().getMillis(), TimeUnit.MILLISECONDS);
            final Stat stat = curator.checkExists().usingWatcher(new CuratorWatcher() {

                @Override
                public void process(WatchedEvent watchedEvent) throws Exception {
                    switch(watchedEvent.getType()) {
                        case NodeDeleted:
                            entryRemoved(watchedEvent.getPath());
                    }
                }
            }).forPath(path);
            if (stat == null) {
                final byte[] noopPayload = jsonMapper.writeValueAsBytes(new SegmentChangeRequestNoop());
                // Create a node and then delete it to remove the registered watcher.  This is a work-around for
                // a zookeeper race condition.  Specifically, when you set a watcher, it fires on the next event
                // that happens for that node.  If no events happen, the watcher stays registered foreverz.
                // Couple that with the fact that you cannot set a watcher when you create a node, but what we
                // want is to create a node and then watch for it to get deleted.  The solution is that you *can*
                // set a watcher when you check to see if it exists so, we first create the node and then set a
                // watcher on its existence.  However, if already does not exist by the time the existence check
                // returns, then the watcher that was set will never fire (nobody will ever create the node
                // again) and thus lead to a slow, but real, memory leak.  So, we create another node to cause
                // that watcher to fire and delete it right away.
                //
                // We do not create the existence watcher first, because then it will fire when we create the
                // node and we'll have the same race when trying to refresh that watcher.
                curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, noopPayload);
                entryRemoved(path);
            }
        } catch (Exception e) {
            failAssign(e);
        }
    } else {
        log.info("Server[%s] skipping doNext() because something is currently loading[%s].", basePath, currentlyProcessing.getSegmentIdentifier());
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) CuratorWatcher(org.apache.curator.framework.api.CuratorWatcher) ISE(io.druid.java.util.common.ISE) SegmentChangeRequestNoop(io.druid.server.coordination.SegmentChangeRequestNoop)

Example 25 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project curator by Netflix.

the class CuratorZKClientBridge method connect.

@Override
public void connect(final Watcher watcher) {
    if (watcher != null) {
        CuratorListener localListener = new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getWatchedEvent() != null) {
                    watcher.process(event.getWatchedEvent());
                }
            }
        };
        curator.getCuratorListenable().addListener(localListener);
        listener.set(localListener);
        try {
            BackgroundCallback callback = new BackgroundCallback() {

                @Override
                public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                    WatchedEvent fakeEvent = new WatchedEvent(Watcher.Event.EventType.None, curator.getZookeeperClient().isConnected() ? Watcher.Event.KeeperState.SyncConnected : Watcher.Event.KeeperState.Disconnected, null);
                    watcher.process(fakeEvent);
                }
            };
            curator.checkExists().inBackground(callback).forPath("/foo");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)97 Watcher (org.apache.zookeeper.Watcher)61 ZooKeeper (org.apache.zookeeper.ZooKeeper)37 KeeperException (org.apache.zookeeper.KeeperException)36 Test (org.junit.Test)32 Stat (org.apache.zookeeper.data.Stat)28 CountDownLatch (java.util.concurrent.CountDownLatch)24 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 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)5 TimeoutException (java.util.concurrent.TimeoutException)4 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)4 WatcherEvent (org.apache.zookeeper.proto.WatcherEvent)4 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)4 ZooKeeperx (com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx)3 FutureCallback (com.linkedin.common.callback.FutureCallback)3 ArrayList (java.util.ArrayList)3