use of org.apache.zookeeper.WatchedEvent in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithChildrenWatcher3.
// test children2Callback watcher
@Test
public void testSymlinkWithChildrenWatcher3() throws ExecutionException, InterruptedException, KeeperException {
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
Stat expectedStat1 = _zkClient.getZooKeeper().exists("/foo/bar", false);
Stat expectedStat2 = _zkClient.getZooKeeper().exists("/bar/foo", false);
final AsyncCallback.Children2Callback callback2 = new AsyncCallback.Children2Callback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
Assert.assertEquals(path, "/foo/$link");
Assert.assertEquals(children.size(), 5);
Assert.assertEquals(stat, expectedStat2);
latch2.countDown();
}
};
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged);
_zkClient.getZooKeeper().getChildren(event.getPath(), null, callback2, null);
}
};
AsyncCallback.Children2Callback callback = new AsyncCallback.Children2Callback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
Assert.assertEquals(stat, expectedStat1);
latch1.countDown();
}
};
// set watcher to /foo/$link
_zkClient.getZooKeeper().getChildren("/foo/$link", watcher, callback, null);
latch1.await(30, TimeUnit.SECONDS);
// update symlink
_zkClient.setSymlinkData("/foo/$link", "/bar/foo", new FutureCallback<None>());
latch2.await(30, TimeUnit.SECONDS);
FutureCallback<None> fcb = new FutureCallback<None>();
// restore symlink
_zkClient.setSymlinkData("/foo/$link", "/foo/bar", fcb);
fcb.get();
}
use of org.apache.zookeeper.WatchedEvent in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithExistWatch.
@Test
public void testSymlinkWithExistWatch() throws InterruptedException, ExecutionException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AsyncCallback.StatCallback existCallback = new AsyncCallback.StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.OK);
latch.countDown();
}
};
Watcher existWatch = new Watcher() {
@Override
public void process(WatchedEvent event) {
Assert.assertEquals(event.getType(), Event.EventType.NodeCreated);
_zkClient.getZooKeeper().exists(event.getPath(), null, existCallback, null);
}
};
AsyncCallback.StatCallback existCallback2 = new AsyncCallback.StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.NONODE);
latch2.countDown();
}
};
// symlink: /foo/$link/newNode -> /foo/bar/newNode
_zkClient.getZooKeeper().exists("/foo/$link/newNode", existWatch, existCallback2, null);
latch2.await(30, TimeUnit.SECONDS);
_zkClient.ensurePersistentNodeExists("/foo/bar/newNode", new FutureCallback<None>());
latch.await(30, TimeUnit.SECONDS);
_zkClient.removeNodeUnsafe("/foo/bar/newNode", new FutureCallback<None>());
}
use of org.apache.zookeeper.WatchedEvent in project rest.li by linkedin.
the class SymlinkAwareZooKeeperTest method testSymlinkWithChildrenWatch.
@Test
public void testSymlinkWithChildrenWatch() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AsyncCallback.ChildrenCallback childrenCallback = new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.OK);
Assert.assertEquals(children.size(), 11);
latch.countDown();
}
};
Watcher childrenWatch = new Watcher() {
@Override
public void process(WatchedEvent event) {
Assert.assertEquals(event.getType(), Event.EventType.NodeChildrenChanged);
_zkClient.getZooKeeper().getChildren(event.getPath(), null, childrenCallback, null);
}
};
AsyncCallback.ChildrenCallback childrenCallback2 = new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
KeeperException.Code result = KeeperException.Code.get(rc);
Assert.assertEquals(result, KeeperException.Code.OK);
latch2.countDown();
}
};
// symlink: /foo/$link -> /foo/bar
_zkClient.getZooKeeper().getChildren("/foo/$link", childrenWatch, childrenCallback2, null);
latch2.await(30, TimeUnit.SECONDS);
_zkClient.ensurePersistentNodeExists("/foo/bar/newNode", new FutureCallback<None>());
latch.await(30, TimeUnit.SECONDS);
_zkClient.removeNodeUnsafe("/foo/bar/newNode", new FutureCallback<None>());
}
use of org.apache.zookeeper.WatchedEvent in project pinpoint by naver.
the class ZookeeperClusterService method setUp.
@PostConstruct
@Override
public void setUp() throws KeeperException, IOException, InterruptedException {
if (!config.isClusterEnable()) {
logger.info("pinpoint-collector cluster disable.");
return;
}
switch(this.serviceState.getCurrentState()) {
case NEW:
if (this.serviceState.changeStateInitializing()) {
logger.info("{} initialization started.", this.getClass().getSimpleName());
ClusterManagerWatcher watcher = new ClusterManagerWatcher();
this.client = new DefaultZookeeperClient(config.getClusterAddress(), config.getClusterSessionTimeout(), watcher);
this.client.connect();
this.profilerClusterManager = new ZookeeperProfilerClusterManager(client, serverIdentifier, clusterPointRouter.getTargetClusterPointRepository());
this.profilerClusterManager.start();
this.webClusterManager = new ZookeeperWebClusterManager(client, PINPOINT_WEB_CLUSTER_PATH, serverIdentifier, clusterConnectionManager);
this.webClusterManager.start();
this.serviceState.changeStateStarted();
logger.info("{} initialization completed.", this.getClass().getSimpleName());
if (client.isConnected()) {
WatcherEvent watcherEvent = new WatcherEvent(EventType.None.getIntValue(), KeeperState.SyncConnected.getIntValue(), "");
WatchedEvent event = new WatchedEvent(watcherEvent);
watcher.process(event);
}
}
break;
case INITIALIZING:
logger.info("{} already initializing.", this.getClass().getSimpleName());
break;
case STARTED:
logger.info("{} already started.", this.getClass().getSimpleName());
break;
case DESTROYING:
throw new IllegalStateException("Already destroying.");
case STOPPED:
throw new IllegalStateException("Already stopped.");
case ILLEGAL_STATE:
throw new IllegalStateException("Invalid State.");
}
}
use of org.apache.zookeeper.WatchedEvent 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;
}
Aggregations