org.apache.curator.framework.recipes.cache.PathChildrenCache Java Examples
The following examples show how to use
org.apache.curator.framework.recipes.cache.PathChildrenCache.
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: TransactorCache.java From fluo with Apache License 2.0 | 6 votes |
public TransactorCache(Environment env) { final FluoConfiguration conf = env.getConfiguration(); timeoutCache = CacheBuilder.newBuilder().maximumSize(FluoConfigurationImpl.getTransactorMaxCacheSize(conf)) .expireAfterAccess( FluoConfigurationImpl.getTransactorCacheTimeout(conf, TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS) .concurrencyLevel(10).build(); this.env = env; cache = new PathChildrenCache(env.getSharedResources().getCurator(), ZookeeperPath.TRANSACTOR_NODES, true); try { cache.start(StartMode.BUILD_INITIAL_CACHE); status = TcStatus.OPEN; } catch (Exception e) { throw new RuntimeException(e); } }
Example #2
Source File: ZKUtils.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
public static void addChildPathCache(String path, PathChildrenCacheListener listener) { NameableExecutor businessExecutor = MycatServer.getInstance().getBusinessExecutor(); ExecutorService executor = businessExecutor == null ? Executors.newFixedThreadPool(5) : businessExecutor; try { /** * 监听子节点的变化情况 */ final PathChildrenCache childrenCache = new PathChildrenCache(getConnection(), path, true); childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT); childrenCache.getListenable().addListener(listener, executor); watchMap.put(path, childrenCache); } catch (Exception e) { throw new RuntimeException(e); } }
Example #3
Source File: NodeDiscovery.java From curator-extensions with Apache License 2.0 | 6 votes |
/** * Creates an instance of {@code ZooKeeperNodeDiscovery}. * * @param curator Curator framework reference. * @param nodePath The path in ZooKeeper to watch. * @param parser The strategy to convert from ZooKeeper {@code byte[]} to {@code T}. */ public NodeDiscovery(CuratorFramework curator, String nodePath, NodeDataParser<T> parser) { Objects.requireNonNull(curator); Objects.requireNonNull(nodePath); Objects.requireNonNull(parser); checkArgument(curator.getState() == CuratorFrameworkState.STARTED); checkArgument(!"".equals(nodePath)); ThreadFactory threadFactory = new ThreadFactoryBuilder() .setNameFormat(getClass().getSimpleName() + "(" + nodePath + ")-%d") .setDaemon(true) .build(); _nodes = new ConcurrentHashMap<>(); _listeners = Collections.newSetFromMap(new ConcurrentHashMap<>()); _curator = curator; _executor = Executors.newSingleThreadScheduledExecutor(threadFactory); _pathCache = new PathChildrenCache(curator, nodePath, true, false, _executor); _nodeDataParser = parser; _closed = false; }
Example #4
Source File: ZkUtils.java From GoPush with GNU General Public License v2.0 | 6 votes |
/** * 设置子节点更改监听 * * @param path * @throws Exception */ public boolean listenerPathChildrenCache(String path, BiConsumer<CuratorFramework, PathChildrenCacheEvent> biConsumer) { if (!ObjectUtils.allNotNull(zkClient, path, biConsumer)) { return Boolean.FALSE; } try { Stat stat = exists(path); if (stat != null) { PathChildrenCache watcher = new PathChildrenCache(zkClient, path, true); watcher.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT); //该模式下 watcher在重连的时候会自动 rebuild 否则需要重新rebuild watcher.getListenable().addListener(biConsumer::accept, pool); if (!pathChildrenCaches.contains(watcher)) { pathChildrenCaches.add(watcher); } // else{ // watcher.rebuild(); // } return Boolean.TRUE; } } catch (Exception e) { log.error("listen path children cache fail! path:{} , error:{}", path, e); } return Boolean.FALSE; }
Example #5
Source File: MasterSlaveJobSummaryListener.java From niubi-job with Apache License 2.0 | 6 votes |
@PostConstruct public void listen() throws Exception { MasterSlaveApiFactory masterSlaveApiFactory = new MasterSlaveApiFactoryImpl(client); PathChildrenCache pathChildrenCache = new PathChildrenCache(client, masterSlaveApiFactory.pathApi().getJobPath(), true); pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() { @Override public synchronized void childEvent(CuratorFramework clientInner, PathChildrenCacheEvent event) throws Exception { if (!EventHelper.isChildUpdateEvent(event) && !EventHelper.isChildAddEvent(event)) { return; } MasterSlaveJobData masterSlaveJobData = new MasterSlaveJobData(event.getData()); if (!masterSlaveJobData.getData().isOperated()) { return; } LoggerHelper.info("begin update master-slave job summary " + masterSlaveJobData.getData()); masterSlaveJobSummaryService.updateJobSummary(masterSlaveJobData.getData()); masterSlaveJobLogService.updateJobLog(masterSlaveJobData.getData()); LoggerHelper.info("update master-slave job summary successfully " + masterSlaveJobData.getData()); } }); pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT); }
Example #6
Source File: PartitionedLeaderService.java From emodb with Apache License 2.0 | 6 votes |
@Override public void start() throws Exception { if (_serviceFactory == null) { throw new IllegalStateException("Cannot start service without first providing a service factory"); } String participantsPath = ZKPaths.makePath(_basePath, String.format(LEADER_PATH_PATTERN, 0)); // Watch the participants for partition 0 to determine how many potential leaders there are total _participantsCache = new PathChildrenCache(_curator, participantsPath, true); _participantsCache.getListenable().addListener((ignore1, ignore2) -> participantsUpdated(false)); _participantsCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); participantsUpdated(true); List<Future<Void>> futures = Lists.newArrayListWithCapacity(_numPartitions); for (PartitionLeader partitionLeader : _partitionLeaders) { futures.add(partitionLeader.startAsync()); } for (Future<Void> future : futures) { future.get(); } }
Example #7
Source File: ZookeeperAdminRegistry.java From sofa-dashboard with Apache License 2.0 | 6 votes |
@Override public void subscribe(String group, RegistryDataChangeListener listener) { // 注册Consumer节点 try { PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, SofaDashboardConstants.SEPARATOR + group, true); pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL); pathChildrenCache.getListenable().addListener(rootNodeChangeListener); } catch (Exception e) { throw new SofaRpcRuntimeException("Failed to register consumer to zookeeperRegistry!", e); } }
Example #8
Source File: ZKWorkerController.java From twister2 with Apache License 2.0 | 6 votes |
/** * listen for additions to the job events directory in zk server * @param cache */ private void addEventsChildrenCacheListener(PathChildrenCache cache) { PathChildrenCacheListener listener = new PathChildrenCacheListener() { public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) { switch (event.getType()) { case CHILD_ADDED: eventPublished(event); break; default: // nothing to do } } }; cache.getListenable().addListener(listener); }
Example #9
Source File: ZKMasterController.java From twister2 with Apache License 2.0 | 6 votes |
/** * create the listener for persistent worker znodes to determine worker status changes */ private void addPersChildrenCacheListener(PathChildrenCache cache) { PathChildrenCacheListener listener = new PathChildrenCacheListener() { public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) { switch (event.getType()) { case CHILD_UPDATED: childZnodeUpdated(event); break; default: // nothing to do } } }; cache.getListenable().addListener(listener); }
Example #10
Source File: ZKBarrierHandler.java From twister2 with Apache License 2.0 | 6 votes |
private long getInitialWorkersAtBarrier(PathChildrenCache childrenCache, Set<Integer> workersAtBarrier) { long timeout = 0; List<ChildData> existingWorkerZnodes = childrenCache.getCurrentData(); for (ChildData child: existingWorkerZnodes) { String fullPath = child.getPath(); int workerID = ZKUtils.getWorkerIDFromPersPath(fullPath); workersAtBarrier.add(workerID); if (timeout == 0) { try { ZKBarrierManager.readWorkerTimeout(client, fullPath); } catch (Twister2Exception e) { throw new Twister2RuntimeException(e); } } } return timeout; }
Example #11
Source File: RefreshableTransportPool.java From jigsaw-payment with Apache License 2.0 | 6 votes |
public void start() throws Exception { if (this.cache == null) { if (this.executorService == null) { this.cache = new PathChildrenCache(client, path, cacheData); } else { this.cache = new PathChildrenCache(client, path, cacheData, dataIsCompressed, this.executorService); } } this.cache.getListenable().addListener(this); this.cache.start(StartMode.POST_INITIALIZED_EVENT); //this.prepareInstances(); // call super to initialize the pool; super.start(); LOG.info("transport pooling factory started. "); }
Example #12
Source File: ZooKeeperDiscover.java From turbo-rpc with Apache License 2.0 | 6 votes |
@Override public void close() throws IOException { for (int i = 0; i < watchers.size(); i++) { PathChildrenCache watcher = watchers.get(i); try { watcher.close(); } catch (Exception e) { if (logger.isErrorEnabled()) { logger.error("watcher关闭失败 ", e); } } } watchers = null; client.close(); client = null; }
Example #13
Source File: CuratorServiceDiscoveryModule.java From attic-aurora with Apache License 2.0 | 6 votes |
@Provides @Singleton @Exposed ServiceGroupMonitor provideServiceGroupMonitor( ShutdownRegistry shutdownRegistry, CuratorFramework client) { PathChildrenCache groupCache = new PathChildrenCache(client, discoveryPath, true /* cacheData */); // NB: Even though we do not start the serviceGroupMonitor here, the registered close shutdown // action is safe since the underlying PathChildrenCache close is tolerant of an un-started // state. Its also crucial so that its underlying groupCache is closed prior to its // curatorFramework dependency in the case when the PathChildrenCache is in fact started (via // CuratorServiceGroupMonitor::start) since a CuratorFramework should have no active clients // when it is closed to avoid errors in those clients when attempting to use it. ServiceGroupMonitor serviceGroupMonitor = new CuratorServiceGroupMonitor(groupCache, MEMBER_SELECTOR); shutdownRegistry.addAction(groupCache::close); return serviceGroupMonitor; }
Example #14
Source File: MetaServerAssignmentHolder.java From hermes with Apache License 2.0 | 6 votes |
private void initPathChildrenCache() throws InitializationException { m_pathChildrenCache = new PathChildrenCache(m_zkClient.get(), ZKPathUtils.getMetaServerAssignmentRootZkPath(), true); m_pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED || event.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED || event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) { String topic = ZKPathUtils.lastSegment(event.getData().getPath()); m_topiAssignmentCache.invalidate(topic); } } }); try { m_pathChildrenCache.start(StartMode.BUILD_INITIAL_CACHE); } catch (Exception e) { throw new InitializationException("Init metaServerAssignmentHolder failed.", e); } }
Example #15
Source File: ZookeeperSyncToNacosServiceImpl.java From nacos-sync with Apache License 2.0 | 6 votes |
/** * fetch the Path cache when the task sync */ protected PathChildrenCache getPathCache(TaskDO taskDO) { return pathChildrenCacheMap.computeIfAbsent(taskDO.getTaskId(), (key) -> { try { PathChildrenCache pathChildrenCache = new PathChildrenCache(zookeeperServerHolder.get(taskDO.getSourceClusterId(), ""), convertDubboProvidersPath(taskDO.getServiceName()), false); pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); return pathChildrenCache; } catch (Exception e) { log.error("zookeeper path children cache start failed, taskId:{}", taskDO.getTaskId(), e); return null; } }); }
Example #16
Source File: ZKMasterController.java From twister2 with Apache License 2.0 | 6 votes |
/** * create the listener for ephemeral worker znodes to determine worker joins and failures */ private void addEphemChildrenCacheListener(PathChildrenCache cache) { PathChildrenCacheListener listener = new PathChildrenCacheListener() { public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) { switch (event.getType()) { case CHILD_ADDED: workerZnodeAdded(event); break; case CHILD_REMOVED: workerZnodeRemoved(event); break; default: // nothing to do } } }; cache.getListenable().addListener(listener); }
Example #17
Source File: Scheduler.java From workflow with Apache License 2.0 | 6 votes |
private boolean taskIsComplete(PathChildrenCache completedTasksCache, RunId runId, ExecutableTask task) { if ( (task == null) || !task.isExecutable() ) { return true; } String completedTaskPath = ZooKeeperConstants.getCompletedTaskPath(runId, task.getTaskId()); ChildData currentData = completedTasksCache.getCurrentData(completedTaskPath); if ( currentData != null ) { TaskExecutionResult result = workflowManager.getSerializer().deserialize(currentData.getData(), TaskExecutionResult.class); if ( result.getSubTaskRunId().isPresent() ) { RunnableTask runnableTask = getRunnableTask(result.getSubTaskRunId().get()); return (runnableTask != null) && runnableTask.getCompletionTimeUtc().isPresent(); } return true; } return false; }
Example #18
Source File: ZookeeperConfigWatcherRegister.java From skywalking with Apache License 2.0 | 5 votes |
public ZookeeperConfigWatcherRegister(ZookeeperServerSettings settings) throws Exception { super(settings.getPeriod()); prefix = settings.getNameSpace() + "/"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(settings.getBaseSleepTimeMs(), settings.getMaxRetries()); CuratorFramework client = CuratorFrameworkFactory.newClient(settings.getHostPort(), retryPolicy); client.start(); this.childrenCache = new PathChildrenCache(client, settings.getNameSpace(), true); this.childrenCache.start(); }
Example #19
Source File: ScrollChildrenChangeListener.java From SkyEye with GNU General Public License v3.0 | 5 votes |
@Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { switch (event.getType()) { case CHILD_ADDED: PathChildrenCache pathChildrenCache = new PathChildrenCache(client, event.getData().getPath(), true); pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT); pathChildrenCache.getListenable().addListener(new AppChildrenChangeListener(this.rabbitmqService, this.zkClient, this.appInfoService)); LOGGER.info("app added: " + event.getData().getPath()); break; case CHILD_REMOVED: LOGGER.info("app removed: " + event.getData().getPath()); break; } }
Example #20
Source File: PartitionManager.java From fluo with Apache License 2.0 | 5 votes |
PartitionManager(Environment env, long minSleepTime, long maxSleepTime) { try { this.curator = env.getSharedResources().getCurator(); this.env = env; this.minSleepTime = minSleepTime; this.maxSleepTime = maxSleepTime; this.retrySleepTime = minSleepTime; groupSize = env.getConfiguration().getInt(FluoConfigurationImpl.WORKER_PARTITION_GROUP_SIZE, FluoConfigurationImpl.WORKER_PARTITION_GROUP_SIZE_DEFAULT); myESNode = new PersistentNode(curator, CreateMode.EPHEMERAL_SEQUENTIAL, false, ZookeeperPath.FINDERS + "/" + ZK_FINDER_PREFIX, ("" + groupSize).getBytes(UTF_8)); myESNode.start(); myESNode.waitForInitialCreate(1, TimeUnit.MINUTES); childrenCache = new PathChildrenCache(curator, ZookeeperPath.FINDERS, true); childrenCache.getListenable().addListener(new FindersListener()); childrenCache.start(StartMode.BUILD_INITIAL_CACHE); schedExecutor = Executors.newScheduledThreadPool(1, new FluoThreadFactory("Fluo worker partition manager")); schedExecutor.scheduleWithFixedDelay(new CheckTabletsTask(), 0, maxSleepTime, TimeUnit.MILLISECONDS); scheduleUpdate(); } catch (Exception e) { throw new RuntimeException(e); } }
Example #21
Source File: ZKUtils.java From dble with GNU General Public License v2.0 | 5 votes |
public static void addViewPathCache(String path, PathChildrenCacheListener listener) { try { //watch the child status final PathChildrenCache childrenCache = new PathChildrenCache(getConnection(), path, true); childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT); childrenCache.getListenable().addListener(listener); } catch (Exception e) { throw new RuntimeException(e); } }
Example #22
Source File: ZookeeperRegistryCenter.java From eagle with Apache License 2.0 | 5 votes |
private PathChildrenCache findChildrenCach(final String key) { for (Map.Entry<String, PathChildrenCache> entry : childrenCaches.entrySet()) { if (key.startsWith(entry.getKey())) { return entry.getValue(); } } return null; }
Example #23
Source File: ClusterStateHolder.java From hermes with Apache License 2.0 | 5 votes |
private void startLeaderLatchPathChildrenCache() throws InitializationException { try { m_leaderLatchPathChildrenCache = new PathChildrenCache(m_client.get(), ZKPathUtils.getMetaServersZkPath(), true); m_leaderLatchPathChildrenCache.getListenable().addListener( new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED || event.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED || event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) { updateLeaderInfo(); } } }, Executors.newSingleThreadExecutor(HermesThreadFactory.create( "LeaderLatchPathChildrenCacheListenerExecutor", true))); m_leaderLatchPathChildrenCache.start(StartMode.BUILD_INITIAL_CACHE); } catch (Exception e) { throw new InitializationException("Init metaServer leaderLatch parent pathChildrenCache failed.", e); } updateLeaderInfo(); }
Example #24
Source File: PathCacheExample.java From curator with Apache License 2.0 | 5 votes |
private static void addListener(PathChildrenCache cache) { // a PathChildrenCacheListener is optional. Here, it's used just to log changes PathChildrenCacheListener listener = new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { switch ( event.getType() ) { case CHILD_ADDED: { System.out.println("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath())); break; } case CHILD_UPDATED: { System.out.println("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath())); break; } case CHILD_REMOVED: { System.out.println("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath())); break; } } } }; cache.getListenable().addListener(listener); }
Example #25
Source File: ServiceCacheImpl.java From xian with Apache License 2.0 | 5 votes |
@Override public void start() throws Exception { Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTED), "Cannot be started more than once"); cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); for (ChildData childData : cache.getCurrentData()) { addInstance(childData, true); } discovery.cacheOpened(this); }
Example #26
Source File: ZookeeperRegistryService.java From Jupiter with Apache License 2.0 | 5 votes |
@Override public void destroy() { for (PathChildrenCache childrenCache : pathChildrenCaches.values()) { try { childrenCache.close(); } catch (IOException ignored) {} } configClient.close(); }
Example #27
Source File: FeatureToggleServiceZk.java From nakadi with MIT License | 5 votes |
public FeatureToggleServiceZk(final ZooKeeperHolder zkHolder) { this.zkHolder = zkHolder; try { this.featuresCache = new PathChildrenCache(zkHolder.get(), PREFIX, false); this.featuresCache.start(); } catch (final Exception e) { LOG.error(e.getMessage(), e); } featurePaths = new HashMap<>(Feature.values().length); for (final Feature feature : Feature.values()) { featurePaths.put(feature, PREFIX + "/" + feature.getId()); } }
Example #28
Source File: PathCacheExample.java From xian with Apache License 2.0 | 5 votes |
private static void list(PathChildrenCache cache) { if ( cache.getCurrentData().size() == 0 ) { System.out.println("* empty *"); } else { for ( ChildData data : cache.getCurrentData() ) { System.out.println(data.getPath() + " = " + new String(data.getData())); } } }
Example #29
Source File: PathCacheExample.java From xian with Apache License 2.0 | 5 votes |
private static void addListener(PathChildrenCache cache) { // a PathChildrenCacheListener is optional. Here, it's used just to log changes PathChildrenCacheListener listener = new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { switch ( event.getType() ) { case CHILD_ADDED: { System.out.println("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath())); break; } case CHILD_UPDATED: { System.out.println("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath())); break; } case CHILD_REMOVED: { System.out.println("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath())); break; } } } }; cache.getListenable().addListener(listener); }
Example #30
Source File: ZKBarrierHandler.java From twister2 with Apache License 2.0 | 5 votes |
/** * create the listener for barrier event znodes in the barrier directory * to determine whether all workers arrived on the barrier */ private void addBarrierChildrenCacheListener( PathChildrenCache cache, JobMasterAPI.BarrierType barrierType) { PathChildrenCacheListener listener = new PathChildrenCacheListener() { public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) { String childPath = event.getData().getPath(); int workerID = ZKUtils.getWorkerIDFromPersPath(childPath); long timeout = Longs.fromByteArray(event.getData().getData()); switch (event.getType()) { case CHILD_ADDED: if (barrierType == JobMasterAPI.BarrierType.DEFAULT) { barrierMonitor.arrivedAtDefault(workerID, timeout); } else if (barrierType == JobMasterAPI.BarrierType.INIT) { barrierMonitor.arrivedAtInit(workerID, timeout); } break; case CHILD_REMOVED: if (barrierType == JobMasterAPI.BarrierType.DEFAULT) { barrierMonitor.removedFromDefault(workerID); } else if (barrierType == JobMasterAPI.BarrierType.INIT) { barrierMonitor.removedFromInit(workerID); } break; default: // nothing to do } } }; cache.getListenable().addListener(listener); }