Search in sources :

Example 71 with TreeCacheEvent

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();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Type(org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) CloseableExecutorService(org.apache.curator.utils.CloseableExecutorService) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) SaturnThreadFactory(com.vip.saturn.job.threads.SaturnThreadFactory) TimeDiffIntolerableException(com.vip.saturn.job.exception.TimeDiffIntolerableException)

Example 72 with TreeCacheEvent

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);
}
Also used : ConfigurationType(org.apache.metron.common.configuration.ConfigurationType) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ParseException(org.apache.metron.stellar.dsl.ParseException) CuratorFramework(org.apache.curator.framework.CuratorFramework) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap)

Example 73 with TreeCacheEvent

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);
        }
    }
}
Also used : ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent)

Example 74 with TreeCacheEvent

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);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent)

Example 75 with TreeCacheEvent

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));
}
Also used : TreeCacheListener(org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener) TreeCacheEvent(org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheEvent) Test(org.junit.Test)

Aggregations

TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)74 ChildData (org.apache.curator.framework.recipes.cache.ChildData)71 Test (org.junit.Test)69 LeaderElectionJobListener (com.dangdang.ddframe.job.lite.internal.election.ElectionListenerManager.LeaderElectionJobListener)7 SimpleJobConfiguration (com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration)6 JobPausedStatusJobListener (com.dangdang.ddframe.job.lite.internal.server.JobOperationListenerManager.JobPausedStatusJobListener)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)5 TestSimpleJob (com.dangdang.ddframe.job.lite.fixture.TestSimpleJob)4 CronSettingAndJobEventChangedJobListener (com.dangdang.ddframe.job.lite.internal.config.ConfigurationListenerManager.CronSettingAndJobEventChangedJobListener)4 MonitorExecutionChangedJobListener (com.dangdang.ddframe.job.lite.internal.execution.ExecutionListenerManager.MonitorExecutionChangedJobListener)4 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TimeDiffIntolerableException (com.vip.saturn.job.exception.TimeDiffIntolerableException)1 SaturnThreadFactory (com.vip.saturn.job.threads.SaturnThreadFactory)1 DataChangedEvent (io.shardingjdbc.orchestration.reg.listener.DataChangedEvent)1 StandardCharsets (java.nio.charset.StandardCharsets)1