Search in sources :

Example 16 with TreeCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project BRFS by zhangnianli.

the class CuratorTreeCache method cancelListener.

public void cancelListener(String path) {
    LOG.info("remove listeners for tree:" + path);
    TreeCache cache = cacheMap.get(path);
    if (cache != null) {
        cache.close();
        cacheMap.remove(path);
    }
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache)

Example 17 with TreeCache

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

the class ZookeeperRegistryCenter method addCacheData.

@Override
public void addCacheData(final String cachePath) {
    TreeCache cache = new TreeCache(client, cachePath);
    try {
        cache.start();
    //CHECKSTYLE:OFF
    } catch (final Exception ex) {
        //CHECKSTYLE:ON
        RegExceptionHandler.handleException(ex);
    }
    caches.put(cachePath + "/", cache);
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) KeeperException(org.apache.zookeeper.KeeperException)

Example 18 with TreeCache

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

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache in project BRFS by zhangnianli.

the class CuratorTreeCache method removeListener.

public void removeListener(String path, AbstractTreeCacheListener listener) {
    LOG.info("remove listener for tree:" + path);
    TreeCache cache = cacheMap.get(path);
    if (cache != null) {
        cache.getListenable().removeListener(listener);
    }
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache)

Example 20 with TreeCache

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

TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)26 ChildData (org.apache.curator.framework.recipes.cache.ChildData)6 TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 CloseableExecutorService (org.apache.curator.utils.CloseableExecutorService)2 TreeCache (org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.TreeCache)2 KeeperException (org.apache.zookeeper.KeeperException)2 OperationTimeoutException (org.apache.zookeeper.KeeperException.OperationTimeoutException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ShardingTreeCache (com.vip.saturn.job.sharding.entity.ShardingTreeCache)1 ShardingException (com.vip.saturn.job.sharding.exception.ShardingException)1 DataChangedEvent (io.shardingjdbc.orchestration.reg.listener.DataChangedEvent)1 StandardCharsets (java.nio.charset.StandardCharsets)1 EnumMap (java.util.EnumMap)1 Entry (java.util.Map.Entry)1 CountDownLatch (java.util.concurrent.CountDownLatch)1