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;
}
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;
}
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);
}
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;
}
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();
}
}
Aggregations