use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project Saturn by vipshop.
the class SaturnExecutorService method registerCallbackAndStartExistingJob.
/**
* Register NewJobCallback, and async start existing jobs. The TreeCache will publish the create events for already
* existing jobs.
*/
public void registerCallbackAndStartExistingJob(final ScheduleNewJobCallback callback) throws Exception {
jobsTreeCache = TreeCache.newBuilder((CuratorFramework) coordinatorRegistryCenter.getRawClient(), JobNodePath.ROOT).setExecutor(new CloseableExecutorService(Executors.newSingleThreadExecutor(new SaturnThreadFactory(executorName + "-$Jobs-watcher", false)), true)).setMaxDepth(1).build();
jobsTreeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
if (event == null) {
return;
}
ChildData data = event.getData();
if (data == null) {
return;
}
String path = data.getPath();
if (path == null || path.equals(JobNodePath.ROOT)) {
return;
}
Type type = event.getType();
if (type == null || !type.equals(Type.NODE_ADDED)) {
return;
}
String jobName = StringUtils.substringAfterLast(path, "/");
String jobClassPath = JobNodePath.getNodeFullPath(jobName, ConfigurationNode.JOB_CLASS);
// wait 5 seconds at most until jobClass created.
for (int i = 0; i < WAIT_JOBCLASS_ADDED_COUNT; i++) {
if (client.checkExists().forPath(jobClassPath) == null) {
Thread.sleep(200);
continue;
}
log.info("new job: {} 's jobClass created event received", jobName);
if (!jobNames.contains(jobName)) {
if (callback.call(jobName)) {
jobNames.add(jobName);
log.info("the job {} initialize successfully", jobName);
} else {
log.warn("the job {} initialize fail", jobName);
}
} else {
log.warn("the job {} is unnecessary to initialize, because it's already existing", jobName);
}
break;
}
}
});
jobsTreeCache.start();
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project metron by apache.
the class ConfigurationFunctions method setupTreeCache.
private static synchronized void setupTreeCache(Context context) throws Exception {
try {
Optional<Object> treeCacheOpt = context.getCapability("treeCache");
if (treeCacheOpt.isPresent()) {
return;
}
} catch (IllegalStateException ex) {
}
Optional<Object> clientOpt = context.getCapability(Context.Capabilities.ZOOKEEPER_CLIENT);
if (!clientOpt.isPresent()) {
throw new IllegalStateException("I expected a zookeeper client to exist and it did not. Please connect to zookeeper.");
}
CuratorFramework client = (CuratorFramework) clientOpt.get();
TreeCache cache = new TreeCache(client, Constants.ZOOKEEPER_TOPOLOGY_ROOT);
TreeCacheListener listener = new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
if (event.getType().equals(TreeCacheEvent.Type.NODE_ADDED) || event.getType().equals(TreeCacheEvent.Type.NODE_UPDATED)) {
String path = event.getData().getPath();
byte[] data = event.getData().getData();
String sensor = Iterables.getLast(Splitter.on("/").split(path), null);
if (path.startsWith(ConfigurationType.PARSER.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.PARSER);
sensorMap.put(sensor, new String(data));
} else if (ConfigurationType.GLOBAL.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.GLOBAL, new String(data));
} else if (ConfigurationType.PROFILER.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.PROFILER, new String(data));
} else if (path.startsWith(ConfigurationType.ENRICHMENT.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.ENRICHMENT);
sensorMap.put(sensor, new String(data));
} else if (path.startsWith(ConfigurationType.INDEXING.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.INDEXING);
sensorMap.put(sensor, new String(data));
}
} else if (event.getType().equals(TreeCacheEvent.Type.NODE_REMOVED)) {
String path = event.getData().getPath();
String sensor = Iterables.getLast(Splitter.on("/").split(path), null);
if (path.startsWith(ConfigurationType.PARSER.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.PARSER);
sensorMap.remove(sensor);
} else if (path.startsWith(ConfigurationType.ENRICHMENT.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.ENRICHMENT);
sensorMap.remove(sensor);
} else if (path.startsWith(ConfigurationType.INDEXING.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.INDEXING);
sensorMap.remove(sensor);
} else if (ConfigurationType.PROFILER.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.PROFILER, null);
} else if (ConfigurationType.GLOBAL.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.GLOBAL, null);
}
}
}
};
cache.getListenable().addListener(listener);
cache.start();
for (ConfigurationType ct : ConfigurationType.values()) {
switch(ct) {
case GLOBAL:
case PROFILER:
{
String data = "";
try {
byte[] bytes = ConfigurationsUtils.readFromZookeeper(ct.getZookeeperRoot(), client);
data = new String(bytes);
} catch (Exception ex) {
}
configMap.put(ct, data);
}
break;
case INDEXING:
case ENRICHMENT:
case PARSER:
{
List<String> sensorTypes = client.getChildren().forPath(ct.getZookeeperRoot());
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ct);
for (String sensorType : sensorTypes) {
sensorMap.put(sensorType, new String(ConfigurationsUtils.readFromZookeeper(ct.getZookeeperRoot() + "/" + sensorType, client)));
}
}
break;
}
}
context.addCapability("treeCache", () -> cache);
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project Saturn by vipshop.
the class AbstractTreeCacheListener method childEvent.
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
ChildData data = event.getData();
if (data != null) {
TreeCacheEvent.Type type = event.getType();
String path = data.getPath();
String nodeData = null;
byte[] dataData = data.getData();
if (dataData != null) {
nodeData = new String(dataData, "UTF-8");
}
if (path != null) {
childEvent(type, path, nodeData);
}
}
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project coprhd-controller by CoprHD.
the class DistributedLockQueueManagerImpl method addLoggingCacheEventListener.
/**
* For logging purposes only, this will cause all nodes to log received TreeCache events.
*/
private void addLoggingCacheEventListener() {
TreeCacheListener listener = new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
switch(event.getType()) {
case NODE_ADDED:
log.debug("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
case NODE_UPDATED:
log.debug("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
case NODE_REMOVED:
log.debug("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
break;
}
}
};
treeCache.getListenable().addListener(listener);
}
use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent in project flink by apache.
the class ZooKeeperUtilsTreeCacheTest method testCallbackNotCalledOnConnectionOrInitializationEvents.
@Test
public void testCallbackNotCalledOnConnectionOrInitializationEvents() throws Exception {
final TreeCacheListener treeCacheListener = ZooKeeperUtils.createTreeCacheListener(() -> {
throw new AssertionError("Should not be called.");
});
treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.INITIALIZED, null));
treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.CONNECTION_RECONNECTED, null));
treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.CONNECTION_LOST, null));
treeCacheListener.childEvent(client, new TreeCacheEvent(TreeCacheEvent.Type.CONNECTION_SUSPENDED, null));
}
Aggregations