use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project druid by druid-io.
the class ZkCoordinator method start.
@LifecycleStart
public void start() throws IOException {
synchronized (lock) {
if (started) {
return;
}
log.info("Starting zkCoordinator for server[%s]", me.getName());
final String loadQueueLocation = ZKPaths.makePath(zkPaths.getLoadQueuePath(), me.getName());
final String servedSegmentsLocation = ZKPaths.makePath(zkPaths.getServedSegmentsPath(), me.getName());
final String liveSegmentsLocation = ZKPaths.makePath(zkPaths.getLiveSegmentsPath(), me.getName());
loadQueueCache = new PathChildrenCache(curator, loadQueueLocation, true, true, Execs.singleThreaded("ZkCoordinator"));
try {
curator.newNamespaceAwareEnsurePath(loadQueueLocation).ensure(curator.getZookeeperClient());
curator.newNamespaceAwareEnsurePath(servedSegmentsLocation).ensure(curator.getZookeeperClient());
curator.newNamespaceAwareEnsurePath(liveSegmentsLocation).ensure(curator.getZookeeperClient());
loadQueueCache.getListenable().addListener((client, event) -> {
final ChildData child = event.getData();
switch(event.getType()) {
case CHILD_ADDED:
childAdded(child);
break;
case CHILD_REMOVED:
log.info("zNode[%s] was removed", event.getData().getPath());
break;
default:
log.info("Ignoring event[%s]", event);
}
});
loadQueueCache.start();
} catch (Exception e) {
Throwables.propagateIfPossible(e, IOException.class);
throw new RuntimeException(e);
}
started = true;
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project druid by druid-io.
the class RemoteTaskRunnerTest method testStatusListenerEventDataNullShouldNotThrowException.
@Test
public void testStatusListenerEventDataNullShouldNotThrowException() throws Exception {
// Set up mock emitter to verify log alert when exception is thrown inside the status listener
Worker worker = EasyMock.createMock(Worker.class);
EasyMock.expect(worker.getHost()).andReturn("host").atLeastOnce();
EasyMock.replay(worker);
ServiceEmitter emitter = EasyMock.createMock(ServiceEmitter.class);
Capture<EmittingLogger.EmittingAlertBuilder> capturedArgument = Capture.newInstance();
emitter.emit(EasyMock.capture(capturedArgument));
EasyMock.expectLastCall().atLeastOnce();
EmittingLogger.registerEmitter(emitter);
EasyMock.replay(emitter);
PathChildrenCache cache = new PathChildrenCache(cf, "/test", true);
testStartWithNoWorker();
cache.getListenable().addListener(remoteTaskRunner.getStatusListener(worker, new ZkWorker(worker, cache, jsonMapper), null));
cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
// Status listener will recieve event with null data
Assert.assertTrue(TestUtils.conditionValid(() -> cache.getCurrentData().size() == 1));
// Verify that the log emitter was called
EasyMock.verify(worker);
EasyMock.verify(emitter);
Map<String, Object> alertDataMap = capturedArgument.getValue().build(null).getDataMap();
Assert.assertTrue(alertDataMap.containsKey("znode"));
Assert.assertNull(alertDataMap.get("znode"));
// Status listener should successfully completes without throwing exception
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project Mycat-Server by MyCATApache.
the class ZktoXmlMain method runCommandWatch.
/**
* 进行命令的监听操作
* 方法描述
* @param zkConn zk的连接信息
* @param path 路径信息
* @param ZKLISTENER 监控路径信息
* @throws Exception
* @创建日期 2016年9月20日
*/
@SuppressWarnings("resource")
private static void runCommandWatch(final CuratorFramework zkConn, final String path) throws Exception {
PathChildrenCache children = new PathChildrenCache(zkConn, path, true);
CommandPathListener commandListener = new CommandPathListener();
// 移除原来的监听再进行添加
children.getListenable().addListener(commandListener);
children.start();
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project yyl_example by Relucent.
the class CuratorWatcherTest method main.
public static void main(String[] args) throws Exception {
// 1.Connect to zk
CuratorFramework client = CuratorFrameworkFactory.newClient(ZK_ADDRESS, new RetryNTimes(10, 5000));
client.start();
System.out.println("zk client start successfully!");
// 2.Register watcher
PathChildrenCache watcher = new PathChildrenCache(client, ZK_PATH, true);
watcher.getListenable().addListener((client1, event) -> {
ChildData data = event.getData();
if (data == null) {
System.out.println("No data in event[" + event + "]");
} else {
System.out.println(//
"Receive event: " + "type=[" + event.getType() + "]" + //
", " + "path=[" + data.getPath() + "]" + //
", " + "data=[" + new String(data.getData()) + "]" + //
", " + "stat=[" + data.getStat() + "]");
}
});
watcher.start(StartMode.BUILD_INITIAL_CACHE);
System.out.println("Register zk watcher successfully!");
client.create().creatingParentsIfNeeded().forPath(ZK_CHILDREN_PATH, "hello".getBytes());
client.setData().forPath(ZK_CHILDREN_PATH, "world".getBytes());
client.delete().deletingChildrenIfNeeded().forPath(ZK_CHILDREN_PATH);
watcher.close();
Thread.sleep(100);
client.close();
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project hive by apache.
the class ZkRegistryBase method ensureInstancesCache.
// Bogus warnings despite closeQuietly.
@SuppressWarnings("resource")
protected final synchronized PathChildrenCache ensureInstancesCache(long clusterReadyTimeoutMs) throws IOException {
Preconditions.checkArgument(zooKeeperClient != null && zooKeeperClient.getState() == CuratorFrameworkState.STARTED, "client is not started");
// lazily create PathChildrenCache
PathChildrenCache instancesCache = this.instancesCache;
if (instancesCache != null)
return instancesCache;
ExecutorService tp = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("StateChangeNotificationHandler").build());
long startTimeNs = System.nanoTime(), deltaNs = clusterReadyTimeoutMs * 1000000L;
long sleepTimeMs = Math.min(16, clusterReadyTimeoutMs);
while (true) {
instancesCache = new PathChildrenCache(zooKeeperClient, workersPath, true);
instancesCache.getListenable().addListener(new InstanceStateChangeListener(), tp);
try {
instancesCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
this.instancesCache = instancesCache;
return instancesCache;
} catch (InvalidACLException e) {
// PathChildrenCache tried to mkdir when the znode wasn't there, and failed.
CloseableUtils.closeQuietly(instancesCache);
long elapsedNs = System.nanoTime() - startTimeNs;
if (deltaNs == 0 || deltaNs <= elapsedNs) {
LOG.error("Unable to start curator PathChildrenCache", e);
throw new IOException(e);
}
LOG.warn("The cluster is not started yet (InvalidACL); will retry");
try {
Thread.sleep(Math.min(sleepTimeMs, (deltaNs - elapsedNs) / 1000000L));
} catch (InterruptedException e1) {
LOG.error("Interrupted while retrying the PathChildrenCache startup");
throw new IOException(e1);
}
sleepTimeMs = sleepTimeMs << 1;
} catch (Exception e) {
CloseableUtils.closeQuietly(instancesCache);
LOG.error("Unable to start curator PathChildrenCache", e);
throw new IOException(e);
}
}
}
Aggregations