Search in sources :

Example 1 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project sharding-jdbc by shardingjdbc.

the class ZookeeperRegistryCenter method watch.

@Override
public void watch(final String key, final EventListener eventListener) {
    final String path = key + "/";
    if (!caches.containsKey(path)) {
        addCacheData(key);
    }
    TreeCache cache = caches.get(path);
    cache.getListenable().addListener(new TreeCacheListener() {

        @Override
        public void childEvent(final CuratorFramework client, final TreeCacheEvent event) throws Exception {
            ChildData data = event.getData();
            if (null == data || null == data.getPath()) {
                return;
            }
            eventListener.onChange(new DataChangedEvent(getEventType(event), data.getPath(), null == data.getData() ? null : new String(data.getData(), "UTF-8")));
        }

        private DataChangedEvent.Type getEventType(final TreeCacheEvent event) {
            switch(event.getType()) {
                case NODE_UPDATED:
                    return DataChangedEvent.Type.UPDATED;
                case NODE_REMOVED:
                    return DataChangedEvent.Type.DELETED;
                default:
                    return DataChangedEvent.Type.IGNORED;
            }
        }
    });
}
Also used : DataChangedEvent(io.shardingjdbc.orchestration.reg.listener.DataChangedEvent) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) CuratorFramework(org.apache.curator.framework.CuratorFramework) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) OperationTimeoutException(org.apache.zookeeper.KeeperException.OperationTimeoutException)

Example 2 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project dubbo by alibaba.

the class ZKTools method testTreeCache.

public static void testTreeCache() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3));
    client.start();
    CountDownLatch latch = new CountDownLatch(1);
    TreeCache treeCache = TreeCache.newBuilder(client, "/dubbo/config").setCacheData(true).build();
    treeCache.start();
    treeCache.getListenable().addListener(new TreeCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
            TreeCacheEvent.Type type = event.getType();
            ChildData data = event.getData();
            if (type == TreeCacheEvent.Type.INITIALIZED) {
                latch.countDown();
            }
            System.out.println(data.getPath() + "\n");
            if (data.getPath().split("/").length == 5) {
                byte[] value = data.getData();
                String stringValue = new String(value, StandardCharsets.UTF_8);
                // fire event to all listeners
                Map<String, Object> added = null;
                Map<String, Object> changed = null;
                Map<String, Object> deleted = null;
                switch(type) {
                    case NODE_ADDED:
                        added = new HashMap<>(1);
                        added.put(pathToKey(data.getPath()), stringValue);
                        added.forEach((k, v) -> System.out.println(k + "  " + v));
                        break;
                    case NODE_REMOVED:
                        deleted = new HashMap<>(1);
                        deleted.put(pathToKey(data.getPath()), stringValue);
                        deleted.forEach((k, v) -> System.out.println(k + "  " + v));
                        break;
                    case NODE_UPDATED:
                        changed = new HashMap<>(1);
                        changed.put(pathToKey(data.getPath()), stringValue);
                        changed.forEach((k, v) -> System.out.println(k + "  " + v));
                }
            }
        }
    });
    latch.await();
/* Map<String, ChildData> dataMap = treeCache.getCurrentChildren("/dubbo/config");
        dataMap.forEach((k, v) -> {
            System.out.println(k);
            treeCache.getCurrentChildren("/dubbo/config/" + k).forEach((ck, cv) -> {
                System.out.println(ck);
            });
        });*/
}
Also used : TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) CuratorListener(org.apache.curator.framework.api.CuratorListener) HashMap(java.util.HashMap) NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) StringUtils(org.apache.dubbo.common.utils.StringUtils) ChildData(org.apache.curator.framework.recipes.cache.ChildData) CountDownLatch(java.util.concurrent.CountDownLatch) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Map(java.util.Map) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) ExecutorService(java.util.concurrent.ExecutorService) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) HashMap(java.util.HashMap) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener in project elastic-job by dangdangdotcom.

the class JobNodeStorageTest method assertAddDataListener.

@Test
public void assertAddDataListener() {
    TreeCache treeCache = mock(TreeCache.class);
    @SuppressWarnings("unchecked") Listenable<TreeCacheListener> listeners = mock(Listenable.class);
    TreeCacheListener listener = mock(TreeCacheListener.class);
    when(treeCache.getListenable()).thenReturn(listeners);
    when(regCenter.getRawCache("/test_job")).thenReturn(treeCache);
    jobNodeStorage.addDataListener(listener);
    verify(listeners).addListener(listener);
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) Test(org.junit.Test)

Example 4 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener 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 5 with TreeCacheListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCacheListener 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)

Aggregations

TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)9 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)5 ChildData (org.apache.curator.framework.recipes.cache.ChildData)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TimeDiffIntolerableException (com.vip.saturn.job.exception.TimeDiffIntolerableException)1 ShardingException (com.vip.saturn.job.sharding.exception.ShardingException)1 SaturnThreadFactory (com.vip.saturn.job.threads.SaturnThreadFactory)1 DataChangedEvent (io.shardingjdbc.orchestration.reg.listener.DataChangedEvent)1 StandardCharsets (java.nio.charset.StandardCharsets)1 EnumMap (java.util.EnumMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)1