org.apache.curator.framework.recipes.cache.TreeCache Java Examples

The following examples show how to use org.apache.curator.framework.recipes.cache.TreeCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CuratorZookeeperClient.java    From FATE-Serving with Apache License 2.0 6 votes vote down vote up
@Override
protected void addTargetDataListener(String path, CuratorZookeeperClient.CuratorWatcherImpl treeCacheListener, Executor executor) {
    try {
        TreeCache treeCache = TreeCache.newBuilder(client, path).setCacheData(false).build();
        treeCacheMap.putIfAbsent(path, treeCache);

        if (executor == null) {
            treeCache.getListenable().addListener(treeCacheListener);
        } else {
            treeCache.getListenable().addListener(treeCacheListener, executor);
        }

        treeCache.start();
    } catch (Exception e) {
        throw new IllegalStateException("Add treeCache listener for path:" + path, e);
    }
}
 
Example #2
Source File: MonitorService.java    From shardingsphere-elasticjob-lite with Apache License 2.0 6 votes vote down vote up
private void dumpDirectly(final String path, final List<String> result) {
    for (String each : regCenter.getChildrenKeys(path)) {
        String zkPath = path + "/" + each;
        String zkValue = regCenter.get(zkPath);
        if (null == zkValue) {
            zkValue = "";
        }
        TreeCache treeCache = (TreeCache) regCenter.getRawCache("/" + jobName);
        ChildData treeCacheData = treeCache.getCurrentData(zkPath);
        String treeCachePath = null == treeCacheData ? "" : treeCacheData.getPath();
        String treeCacheValue = null == treeCacheData ? "" : new String(treeCacheData.getData());
        if (zkValue.equals(treeCacheValue) && zkPath.equals(treeCachePath)) {
            result.add(Joiner.on(" | ").join(zkPath, zkValue));
        } else {
            result.add(Joiner.on(" | ").join(zkPath, zkValue, treeCachePath, treeCacheValue));
        }
        dumpDirectly(zkPath, result);
    }
}
 
Example #3
Source File: ZookeeperMasterMonitor.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
public ZookeeperMasterMonitor(ZookeeperPaths zkPaths,
                              CuratorFramework curator,
                              MasterDescription initValue,
                              TitusRuntime titusRuntime,
                              Scheduler scheduler) {
    this.curator = curator;
    this.titusRuntime = titusRuntime;

    this.leaderPath = zkPaths.getLeaderAnnouncementPath();
    this.leaderSubject = BehaviorSubject.create(initValue).toSerialized();
    this.leaderMonitor = new NodeCache(curator, leaderPath);
    this.latestLeader.set(initValue);

    this.allMastersPath = zkPaths.getAllMastersPath();
    this.masterMonitor = new TreeCache(curator, allMastersPath);

    this.refreshOwnMasterInstanceSubscriber = ObservableExt.schedule(
            ZookeeperConstants.METRICS_ROOT + "masterMonitor.ownInstanceRefreshScheduler",
            titusRuntime.getRegistry(),
            "reRegisterOwnMasterInstanceInZookeeper",
            registerOwnMasterInstance(() -> ownMasterInstance),
            OWN_MASTER_REFRESH_INTERVAL_MS, OWN_MASTER_REFRESH_INTERVAL_MS, TimeUnit.MILLISECONDS,
            scheduler
    ).subscribe();
}
 
Example #4
Source File: ZKUtils.java    From kafka-graphs with Apache License 2.0 6 votes vote down vote up
public static PregelState maybeCreateReadyToReceiveNode(
    CuratorFramework curator, String id, PregelState pregelState, TreeCache treeCache) throws Exception {
    if (pregelState.superstep() < 0) {
        return pregelState.next();
    }
    String barrierPath = barrierPath(id, pregelState);
    Map<String, ChildData> children = treeCache.getCurrentChildren(barrierPath);
    if (children == null) {
        return pregelState;
    }
    int childrenSize = children.size();
    if (children.containsKey(READY)) childrenSize--;
    if (childrenSize == 0) {
        addReady(curator, id, pregelState);
        return pregelState.next();
    }
    return pregelState;
}
 
Example #5
Source File: CuratorZookeeperCenterRepository.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@Override
public void watch(final String key, final DataChangedEventListener dataChangedEventListener) {
    final String path = key + "/";
    if (!caches.containsKey(path)) {
        addCacheData(key);
    }
    TreeCache cache = caches.get(path);
    cache.getListenable().addListener((client, event) -> {
        ChildData data = event.getData();
        if (null == data || null == data.getPath()) {
            return;
        }
        DataChangedEvent.ChangedType changedType = getChangedType(event);
        if (DataChangedEvent.ChangedType.IGNORED != changedType) {
            dataChangedEventListener.onChange(new DataChangedEvent(data.getPath(), null == data.getData() ? null : new String(data.getData(), Charsets.UTF_8), changedType));
        }
    });
}
 
Example #6
Source File: AccumuloGraph.java    From vertexium with Apache License 2.0 6 votes vote down vote up
public AccumuloGraphMetadataStore(CuratorFramework curatorFramework, String zkPath) {
    this.zkPath = zkPath;
    this.curatorFramework = curatorFramework;
    this.treeCache = new TreeCache(curatorFramework, zkPath);
    this.treeCache.getListenable().addListener((client, event) -> {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("treeCache event, clearing cache %s", event);
        }
        writeValues(entries::clear);
        getSearchIndex().clearCache();
        invalidatePropertyDefinitions(event);
    });
    try {
        this.treeCache.start();
    } catch (Exception e) {
        throw new VertexiumException("Could not start metadata sync", e);
    }
}
 
Example #7
Source File: LogLevelFilterHandler.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() throws Exception {
  if (logFeederProps.isZkFilterStorage() && logFeederProps.isUseLocalConfigs()) {
    LogLevelFilterManagerZK filterManager = (LogLevelFilterManagerZK) config.getLogLevelFilterManager();
    CuratorFramework client = filterManager.getClient();
    client.start();
    Gson gson = filterManager.getGson();
    LogSearchConfigZKHelper.waitUntilRootAvailable(client);
    TreeCache clusterCache = LogSearchConfigZKHelper.createClusterCache(client, logFeederProps.getClusterName());
    TreeCacheListener listener = LogSearchConfigZKHelper.createTreeCacheListener(
      logFeederProps.getClusterName(), gson, this);
    LogSearchConfigZKHelper.addAndStartListenersOnCluster(clusterCache, listener);
  }
  if (config.getLogLevelFilterManager() != null) {
    TreeMap<String, LogLevelFilter> sortedFilters = config.getLogLevelFilterManager()
      .getLogLevelFilters(logFeederProps.getClusterName())
      .getFilter();
    filters = new ConcurrentHashMap<>(sortedFilters);
  }
}
 
Example #8
Source File: TreeCacheExample.java    From ZKRecipesByExample with Apache License 2.0 5 votes vote down vote up
private static void list(TreeCache cache) {
	if (cache.getCurrentChildren(PATH).size() == 0) {
		System.out.println("* empty *");
	} else {			
		for (Map.Entry<String, ChildData> entry : cache.getCurrentChildren(PATH).entrySet()) {
			System.out.println(entry.getKey() + " = " + new String(entry.getValue().getData()));
		}
	}
}
 
Example #9
Source File: JobNodeStorageTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #10
Source File: TreeCacheExample.java    From ZKRecipesByExample with Apache License 2.0 5 votes vote down vote up
private static void processCommands(CuratorFramework client, TreeCache cache) throws Exception {
	// More scaffolding that does a simple command line processor
	printHelp();
	try {
		addListener(cache);
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		boolean done = false;
		while (!done) {
			System.out.print("> ");
			String line = in.readLine();
			if (line == null) {
				break;
			}
			String command = line.trim();
			String[] parts = command.split("\\s");
			if (parts.length == 0) {
				continue;
			}
			String operation = parts[0];
			String args[] = Arrays.copyOfRange(parts, 1, parts.length);
			if (operation.equalsIgnoreCase("help") || operation.equalsIgnoreCase("?")) {
				printHelp();
			} else if (operation.equalsIgnoreCase("q") || operation.equalsIgnoreCase("quit")) {
				done = true;
			} else if (operation.equals("set")) {
				setValue(client, command, args);
			} else if (operation.equals("remove")) {
				remove(client, command, args);
			} else if (operation.equals("list")) {
				list(cache);
			}
			Thread.sleep(1000); // just to allow the console output to catch
								// up
		}
	} finally {

	}
}
 
Example #11
Source File: TreeCacheExample.java    From ZKRecipesByExample with Apache License 2.0 5 votes vote down vote up
private static void addListener(final TreeCache cache) {
	TreeCacheListener listener = new TreeCacheListener() {

		@Override
		public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
			switch (event.getType()) {
			case NODE_ADDED: {
				System.out.println("TreeNode added: " + ZKPaths.getNodeFromPath(event.getData().getPath()) + ", value: "
						+ new String(event.getData().getData()));
				break;
			}
			case NODE_UPDATED: {
				System.out.println("TreeNode changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()) + ", value: "
						+ new String(event.getData().getData()));
				break;
			}
			case NODE_REMOVED: {
				System.out.println("TreeNode removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
				break;
			}
			default:
				System.out.println("Other event: " + event.getType().name());
			}
		}

	};

	cache.getListenable().addListener(listener);
}
 
Example #12
Source File: TreeCacheExample.java    From curator with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception
{
    CuratorFramework client = CreateClientExamples.createSimple("127.0.0.1:2181");
    client.getUnhandledErrorListenable().addListener((message, e) -> {
        System.err.println("error=" + message);
        e.printStackTrace();
    });
    client.getConnectionStateListenable().addListener((c, newState) -> {
        System.out.println("state=" + newState);
    });
    client.start();

    TreeCache cache = TreeCache.newBuilder(client, "/").setCacheData(false).build();
    cache.getListenable().addListener((c, event) -> {
        if ( event.getData() != null )
        {
            System.out.println("type=" + event.getType() + " path=" + event.getData().getPath());
        }
        else
        {
            System.out.println("type=" + event.getType());
        }
    });
    cache.start();

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    in.readLine();
}
 
Example #13
Source File: ZookeeperDomainOps.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    if (watcher != null) {
        if (client.checkExists().forPath(buildDomainPath()) == null) {
            client.create().creatingParentContainersIfNeeded().forPath(buildDomainPath());
        }

        new TreeCache(client, buildDomainPath()).start().getListenable().addListener((cf, event) -> {
            switch (event.getType()) {
                case NODE_ADDED:
                    LOG.debug("Domain {} added", event.getData().getPath());
                    try {
                        Domain domain = MAPPER.readValue(event.getData().getData(), Domain.class);

                        LOG.info("Domain {} created", domain.getKey());
                        watcher.added(domain);
                    } catch (IOException e) {
                        LOG.debug("Could not parse {}", new String(event.getData().getData()), e);
                    }
                    break;

                case NODE_UPDATED:
                    LOG.debug("Domain {} update", event.getData().getPath());
                    break;

                case NODE_REMOVED:
                    LOG.debug("Domain {} removed", event.getData().getPath());
                    watcher.removed(StringUtils.substringAfter(event.getData().getPath(), DOMAIN_PATH + '/'));
                    break;

                default:
                    LOG.debug("Event {} received", event);
            }
        });
    }
}
 
Example #14
Source File: ZkClient.java    From xio with Apache License 2.0 5 votes vote down vote up
public void stop() throws Exception {
  leaderListener.relinquish();
  if (leaderSelector != null) {
    leaderSelector.close();
  }
  nodeCaches.values().forEach(this::stopNodeCache);
  treeCaches.values().forEach(TreeCache::close);
  client.close();
}
 
Example #15
Source File: ServiceDiscoverer.java    From metron with Apache License 2.0 5 votes vote down vote up
/**
 * This class listens to zookeeper and updates its internal state when new model endpoints are
 * added via the MaaS service.
 *
 * @param client The zookeeper client
 * @param root The discovery root
 * @param blacklistExpirationMin The amount of time (in minutes) that a blacklisted URL can be held in the blacklist before retrying.
 */
public ServiceDiscoverer(CuratorFramework client, String root, int blacklistExpirationMin) {
  blacklist = CacheBuilder.newBuilder()
                          .concurrencyLevel(4)
                          .weakKeys()
                          .expireAfterWrite(blacklistExpirationMin, TimeUnit.MINUTES)
                          .build();
  try {
    Stat exists = client.checkExists().forPath(root);
    if(exists == null) {
      client.create().creatingParentsIfNeeded().forPath(root);
    }
  } catch (Exception e) {
    LOG.error("Unable to create path: " + e.getMessage(), e);
  }
  JsonInstanceSerializer<ModelEndpoint> serializer = new JsonInstanceSerializer<>(ModelEndpoint.class);
  serviceDiscovery = ServiceDiscoveryBuilder.builder(ModelEndpoint.class)
              .client(client)
              .basePath(root)
              .serializer(serializer)
              .build();
  cache = new TreeCache(client, root);
  cache.getListenable().addListener((client1, event) -> {
    updateState();
  });
  updateState();
}
 
Example #16
Source File: ZookeeperRegistryCenter.java    From idworker with Apache License 2.0 5 votes vote down vote up
@Override
public void evictCacheData(final String cachePath) {
    TreeCache cache = caches.remove(cachePath + "/");
    if (null != cache) {
        cache.close();
    }
}
 
Example #17
Source File: ZookeeperTreeCacheListener.java    From Thunder with Apache License 2.0 5 votes vote down vote up
public ZookeeperTreeCacheListener(CuratorFramework client, String path) throws Exception {
    super(client, path);

    treeCache = new TreeCache(client, path);
    treeCache.start();
    treeCache.getListenable().addListener(this);

    addListener();
}
 
Example #18
Source File: ZKCache.java    From metron with Apache License 2.0 5 votes vote down vote up
/**
 * Start the cache.
 * @throws Exception If unable to be started.
 */
public void start() throws Exception {
  if(cache == null) {
    if(ownClient) {
      client.start();
    }
    TreeCache.Builder builder = TreeCache.newBuilder(client, zkRoot);
    builder.setCacheData(true);
    cache = builder.build();
    for(TreeCacheListener l : listeners) {
      cache.getListenable().addListener(l);
    }
    cache.start();
  }
}
 
Example #19
Source File: BlacklistService.java    From nakadi with MIT License 5 votes vote down vote up
@PostConstruct
public void initIt() {
    try {
        this.blacklistCache =
                TreeCache.newBuilder(zooKeeperHolder.get(), PATH_BLACKLIST).setCacheData(false).build();
        this.blacklistCache.start();
    } catch (final Exception e) {
        LOG.error(e.getMessage(), e);
    }
}
 
Example #20
Source File: ZkClient.java    From xio with Apache License 2.0 5 votes vote down vote up
public void startTreeCache(TreeCache cache) {
  try {
    cache.start();
  } catch (Exception e) {
    log.error("Error starting treeCache {}", cache, e);
    throw new RuntimeException(e);
  }
}
 
Example #21
Source File: ZookeeperRegistryCenter.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Override
public void evictCacheData(final String cachePath) {
    TreeCache cache = caches.remove(cachePath + "/");
    if (null != cache) {
        cache.close();
    }
}
 
Example #22
Source File: ZookeeperRegistryCenter.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #23
Source File: ZookeeperRegistryCenter.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
private TreeCache findTreeCache(final String key) {
    for (Entry<String, TreeCache> entry : caches.entrySet()) {
        if (key.startsWith(entry.getKey())) {
            return entry.getValue();
        }
    }
    return null;
}
 
Example #24
Source File: ZookeeperRegistryCenter.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Override
public String get(final String key) {
    TreeCache cache = findTreeCache(key);
    if (null == cache) {
        return getDirectly(key);
    }
    ChildData resultInCache = cache.getCurrentData(key);
    if (null != resultInCache) {
        return null == resultInCache.getData() ? null : new String(resultInCache.getData(), Charsets.UTF_8);
    }
    return getDirectly(key);
}
 
Example #25
Source File: ZookeeperRegistryCenter.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    for (Entry<String, TreeCache> each : caches.entrySet()) {
        each.getValue().close();
    }
    waitForCacheClose();
    CloseableUtils.closeQuietly(client);
}
 
Example #26
Source File: CuratorZookeeperCenterRepository.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private void addCacheData(final String cachePath) {
    TreeCache cache = new TreeCache(client, cachePath);
    try {
        cache.start();
        // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        CuratorZookeeperExceptionHandler.handleException(ex);
    }
    caches.put(cachePath + "/", cache);
}
 
Example #27
Source File: CuratorZookeeperCenterRepository.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Override
public String get(final String key) {
    TreeCache cache = findTreeCache(key);
    if (null == cache) {
        return getDirectly(key);
    }
    ChildData resultInCache = cache.getCurrentData(key);
    if (null != resultInCache) {
        return null == resultInCache.getData() ? null : new String(resultInCache.getData(), Charsets.UTF_8);
    }
    return getDirectly(key);
}
 
Example #28
Source File: ZookeeperRegistryCenter.java    From idworker with Apache License 2.0 5 votes vote down vote up
@Override
public void addCacheData(final String cachePath) {
    TreeCache cache = new TreeCache(client, cachePath);
    try {
        cache.start();
    } catch (final Exception ex) {
        RegExceptionHandler.handleException(ex);
    }
    caches.put(cachePath + "/", cache);
}
 
Example #29
Source File: ZookeeperRegistryCenter.java    From idworker with Apache License 2.0 5 votes vote down vote up
private TreeCache findTreeCache(final String key) {
    for (Entry<String, TreeCache> entry : caches.entrySet()) {
        if (key.startsWith(entry.getKey())) {
            return entry.getValue();
        }
    }
    return null;
}
 
Example #30
Source File: ZookeeperRegistryCenter.java    From idworker with Apache License 2.0 5 votes vote down vote up
@Override
public String get(final String key) {
    TreeCache cache = findTreeCache(key);
    if (null == cache) {
        return getDirectly(key);
    }
    ChildData resultInCache = cache.getCurrentData(key);
    if (null != resultInCache) {
        return null == resultInCache.getData() ? null : new String(resultInCache.getData(), StandardCharsets.UTF_8);
    }
    return getDirectly(key);
}