Java Code Examples for org.apache.curator.framework.recipes.cache.PathChildrenCache#getCurrentData()

The following examples show how to use org.apache.curator.framework.recipes.cache.PathChildrenCache#getCurrentData() . 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: ZKBarrierHandler.java    From twister2 with Apache License 2.0 6 votes vote down vote up
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 2
Source File: Scheduler.java    From workflow with Apache License 2.0 6 votes vote down vote up
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 3
Source File: ZookeeperSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 5 votes vote down vote up
private void registerAllInstances(TaskDO taskDO, PathChildrenCache pathChildrenCache,
    NamingService destNamingService) throws NacosException {
    List<ChildData> currentData = pathChildrenCache.getCurrentData();
    for (ChildData childData : currentData) {
        String path = childData.getPath();
        Map<String, String> queryParam = parseQueryString(childData.getPath());
        if (isMatch(taskDO, queryParam) && needSync(queryParam)) {
            Map<String, String> ipAndPortParam = parseIpAndPortString(path);
            Instance instance = buildSyncInstance(queryParam, ipAndPortParam, taskDO);
            destNamingService.registerInstance(getServiceNameFromCache(taskDO.getTaskId(), queryParam),
                instance);
        }
    }
}
 
Example 4
Source File: PathCacheExample.java    From xian with Apache License 2.0 5 votes vote down vote up
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 5
Source File: ZookeeperRegistryCenter.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public String get(final String key) {
    PathChildrenCache cache = findChildrenCach(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 6
Source File: TestPathChildren.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
private void printAllData(PathChildrenCache pathChildrenCache) {

        logger.info("[begin]-------------");
        for (ChildData childData : pathChildrenCache.getCurrentData()) {
            logger.info("{}", childData.getPath());
        }
        logger.info("[end]-------------");
    }
 
Example 7
Source File: ZKClientImpl.java    From codes-scratch-zookeeper-netty with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> listenChildrenPath(final String parent, final NodeListener listener, final boolean sync)
        throws Exception {
    PathChildrenCache cache = new PathChildrenCache(client, parent, false, false, EVENT_THREAD_POOL);
    cache.getListenable().addListener(new PathChildrenCacheListener() {
        @Override
        public void childEvent(CuratorFramework c, PathChildrenCacheEvent e)
                throws Exception {
            if (e.getData() == null) { return; }
            switch (e.getType()) {
                case CHILD_ADDED:
                    listener.nodeChanged(ZKClientImpl.this,
                                         new ChangedEvent(e.getData().getPath(), ChangedEvent.Type.CHILD_ADDED));
                    break;
                case CHILD_REMOVED:
                    listener.nodeChanged(ZKClientImpl.this,
                                         new ChangedEvent(e.getData().getPath(), ChangedEvent.Type.CHILD_REMOVED));
                    break;
                case CHILD_UPDATED:
                    listener.nodeChanged(ZKClientImpl.this,
                                         new ChangedEvent(e.getData().getPath(), ChangedEvent.Type.CHILD_UPDATED));
                    break;
            }
        }
    }, SAME_EXECUTOR);
    PathChildrenCache.StartMode mode = sync ? PathChildrenCache.StartMode.BUILD_INITIAL_CACHE : PathChildrenCache.StartMode.NORMAL;
    cache.start(mode);
    List<ChildData> children = cache.getCurrentData();
    List<String> result = new ArrayList<String>();
    for (ChildData child : children) {
        result.add(child.getPath());
    }
    return result;
}
 
Example 8
Source File: PathCacheExample.java    From curator with Apache License 2.0 5 votes vote down vote up
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 9
Source File: PathCacheExample.java    From ZKRecipesByExample with Apache License 2.0 5 votes vote down vote up
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 10
Source File: CuratorPCWatcher.java    From BigData-In-Practice with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    final String nodePath = "/testZK";
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(10000, 5);
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkServerIps)
            .sessionTimeoutMs(10000).retryPolicy(retryPolicy).build();
    client.start();
    try {
        // 为子节点添加watcher,PathChildrenCache: 监听数据节点的增删改,可以设置触发的事件
        final PathChildrenCache childrenCache = new PathChildrenCache(client, nodePath, true);

        /**
         * StartMode: 初始化方式
         *  - POST_INITIALIZED_EVENT:异步初始化,初始化之后会触发事件
         *  - NORMAL:异步初始化
         *  - BUILD_INITIAL_CACHE:同步初始化
         */
        childrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);

        // 列出子节点数据列表,需要使用BUILD_INITIAL_CACHE同步初始化模式才能获得,异步是获取不到的
        List<ChildData> childDataList = childrenCache.getCurrentData();
        System.out.println("当前节点的子节点详细数据列表:");
        for (ChildData childData : childDataList) {
            System.out.println("\t* 子节点路径:" + new String(childData.getPath()) + ",该节点的数据为:" + new String(childData.getData()));
        }

        // 添加事件监听器
        childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception {
                // 通过判断event type的方式来实现不同事件的触发
                if (event.getType().equals(PathChildrenCacheEvent.Type.INITIALIZED)) {  // 子节点初始化时触发
                    System.out.println("子节点初始化成功");
                } else if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)) {  // 添加子节点时触发
                    System.out.print("子节点:" + event.getData().getPath() + " 添加成功,");
                    System.out.println("该子节点的数据为:" + new String(event.getData().getData()));
                } else if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)) {  // 删除子节点时触发
                    System.out.println("子节点:" + event.getData().getPath() + " 删除成功");
                } else if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) {  // 修改子节点数据时触发
                    System.out.print("子节点:" + event.getData().getPath() + " 数据更新成功,");
                    System.out.println("子节点:" + event.getData().getPath() + " 新的数据为:" + new String(event.getData().getData()));
                }
            }
        });
        Thread.sleep(100000); // sleep 100秒,在 zkCli.sh 操作子节点,注意查看控制台的输出
    } finally {
        client.close();
    }
}
 
Example 11
Source File: ZKMasterController.java    From twister2 with Apache License 2.0 4 votes vote down vote up
/**
 * initialize JM when it is coming from failure
 * @throws Exception
 */
private void initRestarting() throws Exception {
  LOG.info("Job Master restarting .... ");

  // build the cache
  // we will not get events for the past worker joins/fails
  ephemChildrenCache = new PathChildrenCache(client, ephemDir, true);
  addEphemChildrenCacheListener(ephemChildrenCache);
  ephemChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);

  List<ChildData> joinedWorkerZnodes = ephemChildrenCache.getCurrentData();
  LOG.info("Initially existing workers: " + joinedWorkerZnodes.size());

  // We listen for status updates for persistent path
  persChildrenCache = new PathChildrenCache(client, persDir, true);
  addPersChildrenCacheListener(persChildrenCache);
  persChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);

  // get all joined workers and provide them to workerMonitor
  List<WorkerWithState> joinedWorkers = new LinkedList<>();
  for (ChildData child : joinedWorkerZnodes) {
    String fullPath = child.getPath();
    int workerID = ZKUtils.getWorkerIDFromEphemPath(fullPath);

    WorkerWithState workerWithState = getWorkerWithState(workerID);
    if (workerWithState != null) {
      joinedWorkers.add(workerWithState);
    } else {
      LOG.severe("worker[" + fullPath + "] added, but its data can not be retrieved.");
    }
  }

  // publish jm restarted event
  jmRestarted();

  // if all workers joined and allJoined event has not been published, publish it
  boolean allJoined = workerMonitor.addJoinedWorkers(joinedWorkers);
  if (allJoined && !allJoinedPublished()) {
    LOG.info("Publishing AllJoined event when restarting, since it is not previously published.");
    allJoined();
  }
}
 
Example 12
Source File: Scheduler.java    From workflow with Apache License 2.0 4 votes vote down vote up
private boolean taskIsStarted(PathChildrenCache startedTasksCache, RunId runId, TaskId taskId)
{
    String startedTaskPath = ZooKeeperConstants.getStartedTaskPath(runId, taskId);
    return (startedTasksCache.getCurrentData(startedTaskPath) != null);
}