Search in sources :

Example 6 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project jstorm by alibaba.

the class Zookeeper method mkClient.

/**
     * connect ZK, register Watch/unhandle Watch
     * 
     * @return
     */
public CuratorFramework mkClient(Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher) {
    CuratorFramework fk = Utils.newCurator(conf, servers, port, root);
    fk.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework _fk, CuratorEvent e) throws Exception {
            if (e.getType().equals(CuratorEventType.WATCHED)) {
                WatchedEvent event = e.getWatchedEvent();
                watcher.execute(event.getState(), event.getType(), event.getPath());
            }
        }
    });
    fk.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {

        @Override
        public void unhandledError(String msg, Throwable error) {
            String errmsg = "Unrecoverable Zookeeper error, halting process: " + msg;
            LOG.error(errmsg, error);
            JStormUtils.halt_process(1, "Unrecoverable Zookeeper error");
        }
    });
    fk.start();
    return fk;
}
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) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 7 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent 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 8 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project flink by apache.

the class ZooKeeperCompletedCheckpointStore method remove.

/**
	 * Removes the state handle from ZooKeeper, discards the checkpoints, and the state handle.
	 */
private void remove(final Tuple2<RetrievableStateHandle<CompletedCheckpoint>, String> stateHandleAndPath, final Callable<Void> action) throws Exception {
    BackgroundCallback callback = new BackgroundCallback() {

        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
            final long checkpointId = pathToCheckpointId(stateHandleAndPath.f1);
            try {
                if (event.getType() == CuratorEventType.DELETE) {
                    if (event.getResultCode() == 0) {
                        Exception exception = null;
                        if (null != action) {
                            try {
                                action.call();
                            } catch (Exception e) {
                                exception = new Exception("Could not execute callable action " + "for checkpoint " + checkpointId + '.', e);
                            }
                        }
                        try {
                            // Discard the state handle
                            stateHandleAndPath.f0.discardState();
                        } catch (Exception e) {
                            Exception newException = new Exception("Could not discard meta " + "data for completed checkpoint " + checkpointId + '.', e);
                            if (exception == null) {
                                exception = newException;
                            } else {
                                exception.addSuppressed(newException);
                            }
                        }
                        if (exception != null) {
                            throw exception;
                        }
                    } else {
                        throw new IllegalStateException("Unexpected result code " + event.getResultCode() + " in '" + event + "' callback.");
                    }
                } else {
                    throw new IllegalStateException("Unexpected event type " + event.getType() + " in '" + event + "' callback.");
                }
            } catch (Exception e) {
                LOG.warn("Failed to discard checkpoint {}.", checkpointId, e);
            }
        }
    };
    // Remove state handle from ZooKeeper first. If this fails, we can still recover, but if
    // we remove a state handle and fail to remove it from ZooKeeper, we end up in an
    // inconsistent state.
    checkpointsInZooKeeper.remove(stateHandleAndPath.f1, callback);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) FlinkException(org.apache.flink.util.FlinkException) ConcurrentModificationException(java.util.ConcurrentModificationException)

Example 9 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project storm by apache.

the class Zookeeper method mkClientImpl.

public CuratorFramework mkClientImpl(Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher, Map authConf) {
    CuratorFramework fk;
    if (authConf != null) {
        fk = Utils.newCurator(conf, servers, port, root, new ZookeeperAuthInfo(authConf));
    } else {
        fk = Utils.newCurator(conf, servers, port, root);
    }
    fk.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework _fk, CuratorEvent e) throws Exception {
            if (e.getType().equals(CuratorEventType.WATCHED)) {
                WatchedEvent event = e.getWatchedEvent();
                watcher.execute(event.getState(), event.getType(), event.getPath());
            }
        }
    });
    LOG.info("Staring ZK Curator");
    fk.start();
    return fk;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) ZookeeperAuthInfo(org.apache.storm.utils.ZookeeperAuthInfo) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) BindException(java.net.BindException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AuthorizationException(org.apache.storm.generated.AuthorizationException) KeyNotFoundException(org.apache.storm.generated.KeyNotFoundException)

Example 10 with CuratorEvent

use of org.apache.curator.framework.api.CuratorEvent in project druid by druid-io.

the class AnnouncerTest method testSessionKilled.

@Test(timeout = 60_000L)
public void testSessionKilled() throws Exception {
    curator.start();
    curator.blockUntilConnected();
    Announcer announcer = new Announcer(curator, exec);
    try {
        curator.inTransaction().create().forPath("/somewhere").and().commit();
        announcer.start();
        final byte[] billy = "billy".getBytes();
        final String testPath1 = "/test1";
        final String testPath2 = "/somewhere/test2";
        final Set<String> paths = Sets.newHashSet(testPath1, testPath2);
        announcer.announce(testPath1, billy);
        announcer.announce(testPath2, billy);
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2));
        final CountDownLatch latch = new CountDownLatch(1);
        curator.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.CREATE) {
                    paths.remove(event.getPath());
                    if (paths.isEmpty()) {
                        latch.countDown();
                    }
                }
            }
        });
        KillSession.kill(curator.getZookeeperClient().getZooKeeper(), server.getConnectString());
        Assert.assertTrue(timing.forWaiting().awaitLatch(latch));
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2));
        announcer.stop();
        while ((curator.checkExists().forPath(testPath1) != null) || (curator.checkExists().forPath(testPath2) != null)) {
            Thread.sleep(100);
        }
        Assert.assertNull(curator.checkExists().forPath(testPath1));
        Assert.assertNull(curator.checkExists().forPath(testPath2));
    } finally {
        announcer.stop();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

CuratorEvent (org.apache.curator.framework.api.CuratorEvent)12 CuratorFramework (org.apache.curator.framework.CuratorFramework)11 CuratorListener (org.apache.curator.framework.api.CuratorListener)7 Test (org.junit.Test)6 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 KeeperException (org.apache.zookeeper.KeeperException)4 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 IOException (java.io.IOException)2 Watcher (org.apache.zookeeper.Watcher)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Message (com.google.protobuf.Message)1 BindException (java.net.BindException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Executor (java.util.concurrent.Executor)1