@Override public Closeable watchLocks(String lockPathRoot, Executor executor, final Watcher watcher) { lockPathRoot = norm(lockPathRoot); PathChildrenCache cache = new PathChildrenCache(curator, lockPathRoot, true); try { cache.start(); cache.getListenable().addListener(new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { switch (event.getType()) { case CHILD_ADDED: watcher.onLock(event.getData().getPath(), new String(event.getData().getData(), StandardCharsets.UTF_8)); break; case CHILD_REMOVED: watcher.onUnlock(event.getData().getPath(), new String(event.getData().getData(), StandardCharsets.UTF_8)); break; default: break; } } }, executor); } catch (Exception ex) { logger.error("Error to watch lock path " + lockPathRoot, ex); } return cache; }
final List<ChildData> children; if (isTokenCache) { children = tokenCache.getCurrentData(); } else { children = keyCache.getCurrentData(); processTokenAddOrUpdate(child); } else { processKeyAddOrUpdate(child.getData()); child.getPath()); LOG.debug("Failure exception:", e); ++count;
public void start() { try { cache.getListenable().addListener((client, event) -> handleChildEvent(event)); cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT); } catch (Exception ex) { throw Throwables.propagate(ex); } }
public void stopListener() { try { if (start.compareAndSet(true, false)) { childrenCache.getListenable().removeListener(childrenCacheListener); childrenCache.clear(); childrenCache.close(); } } catch (IOException e) { throw new ZkException(e); } } }
curatorFramework = CuratorFrameworkFactory.newClient(getZkConn(), new ExponentialBackoffRetry(1000, 3)); curatorFramework.start(); final GetChildrenBuilder children = curatorFramework.getChildren(); try { final String serviceZKPath = RegistryUtil.getServicePath(serviceName); PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, serviceZKPath, true); pathChildrenCache.start(); pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { LOGGER.info("Listen Event {}", event);
private void init(final CuratorFramework zkClient, final String path, MessageListener<T> messageListener) { this.zkClient = zkClient; this.path = path; addMessageListener(messageListener); try { open(); if (zkClient.checkExists().forPath(path) == null) { zkClient.create().creatingParentsIfNeeded().forPath(path); } notifyListeners(zkClient.getChildren().forPath(path)); cache = new PathChildrenCache(zkClient, path, true); cache.start(); cache.getListenable().addListener(new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception { switch (pathChildrenCacheEvent.getType()) { case CHILD_UPDATED: notifyListeners(StringSerializer.deserialize(pathChildrenCacheEvent.getData().getData())); break; default: log.debug("Ignored path event for node: " + pathChildrenCacheEvent); } } }); } catch (Exception e) { log.warn("error opening client: ", e); } }
@Before public void setUp() throws Exception { setupServerAndCurator(); curator.start(); curator.blockUntilConnected(); curator.create().creatingParentsIfNeeded().forPath(LOAD_QUEUE_PATH); loadQueueCache = new PathChildrenCache( curator, LOAD_QUEUE_PATH, true, true, Execs.singleThreaded("load_queue_cache-%d") ); }
loadQueueCache.getListenable().addListener( new PathChildrenCacheListener() loadQueueCache.start(); Assert.assertNotNull(curator.checkExists().forPath(loadRequestPath)); Assert.assertEquals( segment, ((SegmentChangeRequestLoad) jsonMapper .readValue(curator.getData().decompressed().forPath(loadRequestPath), DataSegmentChangeRequest.class)) .getSegment() );
this.nodeValueCodec = requireNonNull(nodeValueCodec, "nodeValueCodec"); internalClient = true; client = CuratorFrameworkFactory.builder() .connectString(zkConnectionStr) .retryPolicy(ZooKeeperDefaults.DEFAULT_RETRY_POLICY) .sessionTimeoutMs(sessionTimeout) .build(); client.start(); boolean success = false; try { pathChildrenCache = pathChildrenCache(zNodePath); pathChildrenCache.start(); success = true; } catch (Exception e) { } finally { if (!success) { client.close();
public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null; PathChildrenCache cache = null; try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); // in this example we will cache data. Notice that this is optional. cache = new PathChildrenCache(client, PATH, true); cache.start(); processCommands(client, cache); } finally { CloseableUtils.closeQuietly(cache); CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); } }
if (!isExternalClient) { try { zkClient.start(); } catch (Exception e) { throw new IOException("Could not start Curator Framework", e); CuratorFramework nullNsFw = zkClient.usingNamespace(null); EnsurePath ensureNs = nullNsFw.newNamespaceAwareEnsurePath("/" + zkClient.getNamespace()); try { ensureNs.ensure(nullNsFw.getZookeeperClient()); keyCache = new PathChildrenCache(zkClient, ZK_DTSM_MASTER_KEY_ROOT, true); if (keyCache != null) { keyCache.start(StartMode.BUILD_INITIAL_CACHE); keyCache.getListenable().addListener(new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, tokenCache = new PathChildrenCache(zkClient, ZK_DTSM_TOKENS_ROOT, true); if (tokenCache != null) { tokenCache.start(StartMode.BUILD_INITIAL_CACHE); tokenCache.getListenable().addListener(new PathChildrenCacheListener() {
@PostConstruct public void start() { curatorClient.start(); curatorClient.getConnectionStateListenable().addListener(new ConnectionStateListener() { private ConnectionState lastState; if (curatorClient.checkExists().creatingParentContainersIfNeeded().forPath(rootPath) == null) { curatorClient.create().creatingParentContainersIfNeeded().forPath(rootPath); evictionPathCache = new PathChildrenCache(curatorClient, "/evictions", true); evictionPathCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); } catch (Exception e) { throw new RuntimeException("Unable to start eviction path cache: " + e.getMessage(), e); try { if (curatorClient.checkExists().creatingParentContainersIfNeeded().forPath(serversPath) == null) { curatorClient.create().creatingParentContainersIfNeeded().forPath(serversPath);
protected final void followGroup() { membershipLock.lock(); try { this.cache.start(); this.cache.getListenable().addListener(this); if (zkClient.checkExists().forPath(path) == null) { try { zkClient.create().creatingParentsIfNeeded().forPath(path, null); } catch (KeeperException.NodeExistsException e) { // do nothing if it already exists } } memberSet = getCurrentMembers(); log.info("Initial members for path: {} are: {}", path, memberSet); } catch (Exception ex) { log.warn("Failed to pre-load members for path: {}", path, ex); } finally { membershipLock.unlock(); } }
boolean imFirst = childrenCache.getCurrentData().stream().map(ChildData::getPath) .map(ZKPaths::getNodeFromPath).sorted().findFirst().map(s -> s.equals(me2)) .orElse(false); ChildData childData = childrenCache.getCurrentData(ZookeeperPath.FINDERS + "/splits"); if (childData == null) { byte[] currSplitData = SerializedSplits.serializeTableSplits(env); curator.create().forPath(ZookeeperPath.FINDERS + "/splits", currSplitData); } else { HashSet<Bytes> zkSplits = new HashSet<>(); SerializedSplits.deserialize(zkSplits::add, childData.getData()); curator.setData().forPath(ZookeeperPath.FINDERS + "/splits", currSplitData);
public synchronized void start() throws Exception { if (started) { throw new IllegalStateException(); } final InetSocketAddress addr = startServer(); curatorFramework = CuratorUtil.newAppCurator(env.getConfiguration()); curatorFramework.getConnectionStateListenable().addListener(cnxnListener); curatorFramework.start(); while (!cnxnListener.isConnected()) { Thread.sleep(200); } leaderSelector = new LeaderSelector(curatorFramework, ZookeeperPath.ORACLE_SERVER, this); String leaderId = HostUtil.getHostName() + ":" + addr.getPort(); leaderSelector.setId(leaderId); log.info("Leader ID = " + leaderId); leaderSelector.start(); pathChildrenCache = new PathChildrenCache(curatorFramework, oraclePath, true); pathChildrenCache.getListenable().addListener(this); pathChildrenCache.start(); while (!cnxnListener.isConnected()) { Thread.sleep(200); } log.info("Listening " + addr); started = true; }
private void start() throws Exception{ curaFramework = CuratorFrameworkFactory.newClient(getZkConnectString(), new ExponentialBackoffRetry(1000, 3)); curaFramework.start(); curaFramework.setACL().withACL(CONFIG.getAcls()); connectionStateListener = getConnectionStateListener(); curaFramework.getConnectionStateListenable() .addListener(connectionStateListener); unhandledErrorListener = getUnhandledErrorListener(); curaFramework.getUnhandledErrorListenable() .addListener(unhandledErrorListener); cache = new PathChildrenCache(curaFramework, CONFIG.getParentPath(), true); cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); closeAbles.add(cache); closeAbles.add(curaFramework); } }
ArgumentChecker.notNull(callback); evictionPathCache.getListenable().addListener((client, event) -> { if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)) { String path = event.getData().getPath(); String dn = ZKPaths.getNodeFromPath(path); callback.evict(dn); if (curatorClient.checkExists().creatingParentContainersIfNeeded().forPath(responsePath) == null) { curatorClient.create().creatingParentContainersIfNeeded().forPath(responsePath);
public synchronized void stop() throws Exception { if (started) { server.stop(); serverThread.join(); if (gcTsTracker != null) { gcTsTracker.stop(); } started = false; currentLeader = null; if (curatorFramework.getState().equals(CuratorFrameworkState.STARTED)) { pathChildrenCache.getListenable().removeListener(this); pathChildrenCache.close(); leaderSelector.close(); curatorFramework.getConnectionStateListenable().removeListener(this); curatorFramework.close(); } log.info("Oracle server has been stopped."); } }
druidCoordinatorConfig ); final PathChildrenCache pathChildrenCacheCold = new PathChildrenCache( curator, loadPathCold, pathChildrenCache.start(); pathChildrenCacheCold.start(); pathChildrenCache.getListenable().addListener( (client, event) -> { if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)) { .entrySet() .stream() .filter(x -> event.getData().getPath().contains(x.getKey())) .map(Map.Entry::getValue) .findFirst() curator.delete().guaranteed().forPath(event.getData().getPath()); pathChildrenCacheCold.getListenable().addListener( (client, event) -> { if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)) { curator.delete().guaranteed().forPath(event.getData().getPath());