if (curator.checkExists().forPath(parentPath) == null) { buildParentPath = true; if (!listeners.containsKey(parentPath)) { final PathChildrenCache cache = factory.make(curator, parentPath); cache.getListenable().addListener( new PathChildrenCacheListener() private final AtomicReference<Set<String>> pathsLost = new AtomicReference<Set<String>>(null);
@Override public void close() throws Exception { if (this.nodeCache != null) { this.nodeCache.getListenable().removeListener(listener); this.nodeCache.close(); } if (this.zkClient != null) { this.zkClient.close(); } pool.shutdown(); }
/** {@inheritDoc} */ @Override public void start() throws Exception { Preconditions.checkState(mState.compareAndSet(State.INITIALIZED, State.STARTED), "Can not start ManagedFijiClient in state %s.", mState.get()); /** The listener updates the set of served instances based on ZK changes. */ mZKInstances.getListenable().addListener(new InstanceListener()); mZKInstances.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); refreshInstances(); LOG.info("Successfully started ManagedFijiClient!"); }
@Override public void close() throws IOException { if ( state.compareAndSet(State.STARTED, State.CLOSED) ) { listeners.clear(); } client.getConnectionStateListenable().removeListener(connectionStateListener); }
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);
@VisibleForTesting @SneakyThrows(Exception.class) public void initialize(Duration monitorInterval) { Exceptions.checkNotClosed(closed.get(), this); // Start loading the segment container to node assigment map from zookeeper. this.hostContainerMapNode.start(); // There are two triggers for the segment container monitor. // 1. On any segment container ownership changes notified via zookeeper. We will ensure the local containers // are stopped/started according to the new ownership mapping. // 2. At scheduled intervals to perform retries on local segment container start failures. this.assigmentTask.set(this.executor.scheduleWithFixedDelay( this::checkAssignment, 0L, monitorInterval.getSeconds(), TimeUnit.SECONDS)); this.hostContainerMapNode.getListenable().addListener(this::checkAssignment, this.executor); }
@Override public void close() throws IOException { client.getConnectionStateListenable().removeListener(connectionStateListener); state.set(State.CLOSED); listeners.clear(); }
if ( state.compareAndSet(CuratorFrameworkState.STARTED, CuratorFrameworkState.STOPPED) ) listeners.forEach(new Function<CuratorListener, Void>() listeners.clear(); unhandledErrorListeners.clear(); connectionStateManager.close(); client.close();
@Override public void close() { if ( state.compareAndSet(State.STARTED, State.CLOSED) ) { service.shutdownNow(); listeners.clear(); } }
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); } }
this.zkClient = CuratorFrameworkFactory.newClient(serverAddr, new ExponentialBackoffRetry(SLEEP_TIME, RETRY_TIMES)); this.zkClient.start(); Stat stat = this.zkClient.checkExists().forPath(this.path); if (stat == null) { this.zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(this.path, null); this.nodeCache = new NodeCache(this.zkClient, this.path); this.nodeCache.getListenable().addListener(this.listener, this.pool); this.nodeCache.start(); } catch (Exception e) { RecordLog.warn("[ZookeeperDataSource] Error occurred when initializing Zookeeper data source", e);
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() );
@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; }
@Override public void start() { LOGGER.debug("Starting..."); try { client.start(); try { agentNodeCache = new NodeCache(client, basePath + "/" + getAgentName()); agentNodeCache.start(); agentNodeCache.getListenable().addListener(new NodeCacheListener() { @Override public void nodeChanged() throws Exception { refreshConfiguration(); } }); } catch (Exception e) { client.close(); throw e; } } catch (Exception e) { lifecycleState = LifecycleState.ERROR; if (e instanceof RuntimeException) { throw (RuntimeException) e; } else { throw new FlumeException(e); } } lifecycleState = LifecycleState.START; }
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, if (tokenCache != null) { tokenCache.start(StartMode.BUILD_INITIAL_CACHE); tokenCache.getListenable().addListener(new PathChildrenCacheListener() {
client = CuratorFrameworkFactory.newClient(controller.getBrokerConfig().getZkPath(), new ExponentialBackoffRetry(1000, 3)); client.start(); if (client.checkExists().forPath(path) == null) { log.error("config path in not exist, path:{}", path); return false; cache = new NodeCache(client, path); NodeCacheListener listener = new NodeCacheListener() { @Override public void nodeChanged() throws Exception { cache.getListenable().addListener(listener); cache.start(); } catch (Exception ex) { log.error("cache start failed", ex);
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(); } }
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; }
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."); } }