Java Code Examples for org.apache.curator.framework.recipes.cache.ChildData#getPath()

The following examples show how to use org.apache.curator.framework.recipes.cache.ChildData#getPath() . 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: 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 2
Source File: ZKCacheListener.java    From mpush with Apache License 2.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework curator, TreeCacheEvent event) throws Exception {
    ChildData data = event.getData();
    if (data == null) return;
    String dataPath = data.getPath();
    if (Strings.isNullOrEmpty(dataPath)) return;
    if (dataPath.startsWith(watchPath)) {
        switch (event.getType()) {
            case NODE_ADDED:
                listener.onServiceAdded(dataPath, Jsons.fromJson(data.getData(), CommonServiceNode.class));
                break;
            case NODE_REMOVED:
                listener.onServiceRemoved(dataPath, Jsons.fromJson(data.getData(), CommonServiceNode.class));
                break;
            case NODE_UPDATED:
                listener.onServiceUpdated(dataPath, Jsons.fromJson(data.getData(), CommonServiceNode.class));
                break;
        }
        Logs.RSD.info("ZK node data change={}, nodePath={}, watchPath={}, ns={}");
    }
}
 
Example 3
Source File: ViewChildListener.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
/**
 * update the meta if the view updated
 */
private void createOrUpdateViewMeta(ChildData childData, boolean isReplace) throws Exception {
    String path = childData.getPath();
    String[] paths = path.split("/");
    String jsonValue = new String(childData.getData(), StandardCharsets.UTF_8);
    JSONObject obj = (JSONObject) JSONObject.parse(jsonValue);

    //if the view is create or replace by this server it self
    String serverId = obj.getString(SERVER_ID);
    if (serverId.equals(ZkConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID))) {
        return;
    }
    String createSql = obj.getString(CREATE_SQL);
    String schema = paths[paths.length - 1].split(SCHEMA_VIEW_SPLIT)[0];

    ViewMeta vm = new ViewMeta(schema, createSql, ProxyMeta.getInstance().getTmManager());
    vm.init(isReplace);
    vm.addMeta(false);

}
 
Example 4
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 5
Source File: DubboServiceDiscoveryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve the name of service.
 * @param event {@link TreeCacheEvent}
 * @return If the Zookeeper's {@link ChildData#getPath() node path} that was
 * notified comes from {@link ServiceInstance the service instance}, return it's
 * parent path as the service name, or return <code>null</code>
 */
private String resolveServiceName(TreeCacheEvent event) {
	ChildData childData = event.getData();
	String path = childData.getPath();
	if (logger.isDebugEnabled()) {
		logger.debug("ZK node[path : {}] event type : {}", path, event.getType());
	}

	String serviceName = null;

	if (pathMatcher.match(serviceInstancePathPattern, path)) {
		Map<String, String> variables = pathMatcher
				.extractUriTemplateVariables(serviceInstancePathPattern, path);
		serviceName = variables.get(SERVICE_NAME_PATH_VARIABLE_NAME);
	}

	return serviceName;
}
 
Example 6
Source File: CuratorClientService.java    From knox with Apache License 2.0 5 votes vote down vote up
@Override
public void nodeChanged() throws Exception {
    String path = null;
    byte[] data = null;

    ChildData cd = nodeCache.getCurrentData();
    if (cd != null) {
        path = cd.getPath();
        data = cd.getData();
    }

    if (path != null) {
        delegate.entryChanged(client, path, data);
    }
}
 
Example 7
Source File: AbstractJobListener.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Override
public final void childEvent(final CuratorFramework client, final TreeCacheEvent event) {
    ChildData childData = event.getData();
    if (null == childData) {
        return;
    }
    String path = childData.getPath();
    if (path.isEmpty()) {
        return;
    }
    dataChanged(path, event.getType(), null == childData.getData() ? "" : new String(childData.getData(), Charsets.UTF_8));
}
 
Example 8
Source File: ViewChildListener.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * delete the view data from view meta
 *
 * @param childData
 */
private void deleteNode(ChildData childData) throws Exception {
    String path = childData.getPath();
    String[] paths = path.split("/");
    String schema = paths[paths.length - 1].split(":")[0];
    String viewName = paths[paths.length - 1].split(":")[1];

    ProxyMeta.getInstance().getTmManager().addMetaLock(schema, viewName, "DROP VIEW " + viewName);
    try {
        ProxyMeta.getInstance().getTmManager().getCatalogs().get(schema).getViewMetas().remove(viewName);
    } finally {
        ProxyMeta.getInstance().getTmManager().removeMetaLock(schema, viewName);
    }

}
 
Example 9
Source File: AbstractChildrenDataListener.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData childData = event.getData();
    if (null == childData) {
        return;
    }
    String path = childData.getPath();
    if (path.isEmpty()) {
        return;
    }
    dataChanged(path, event.getType(), null == childData.getData() ? "" : new String(childData.getData(), Charsets.UTF_8));
}
 
Example 10
Source File: TopicListener.java    From kafka-monitor with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework curator, TreeCacheEvent event) throws Exception {
    ChildData data = event.getData();
    if (data != null) {
        if (event.getType() == NODE_ADDED) {

        }
        String path = data.getPath();
        //判断是否为topics节点
        if (path.contains(String.format("%s/",ZkUtils.BrokerTopicsPath())) && (!path.contains("partitions"))) {
            Topic topic = JSONObject.parseObject(data.getData(), Topic.class);
            String name = path.substring(path.lastIndexOf("/") + 1, path.length());
            topic.setName(name);

            int[] tPartiyions = topic.getPartitions().keySet().stream().mapToInt((t) -> Integer.valueOf(t)).sorted().toArray();
            for (Object key : tPartiyions
                    ) {
                String partitionPath = String.format("%s/partitions/%s/state", path, key);
                String state = new String(curator.getData().forPath(partitionPath));
                Partition partition = JSONObject.parseObject(state, Partition.class);
                JSONArray replicas = topic.getPartitions().getJSONArray(String.valueOf(key));
                int[] replicasArray = new int[replicas.size()];
                for (int i = 0; i <
                        replicas.size(); i++) {
                    replicasArray[i] = replicas.getInteger(i);
                }
                partition.setReplicasArray(replicasArray);

                topic.getPartitionMap().put((Integer) key, partition);
            }
            topicList.add(topic);
        }
    }
}
 
Example 11
Source File: ZkPathManager.java    From xian with Apache License 2.0 5 votes vote down vote up
public static boolean isServiceNode(ChildData zNode) {
    String path = zNode.getPath();
    LOG.debug("正在检查节点路径:   " + path);
    LOG.debug("服务注册路径:   " + getNodeBasePath());
    if (!path.startsWith(getNodeBasePath()) || path.equals(getNodeBasePath())) {
        return false;
    }
    String name = path.substring((getNodeBasePath() + "/").length());
    String[] names = name.split("/");
    if (names.length != 2) {
        return false;
    }
    /*if (!SystemEnum.isServiceNodeName(names[0])) {
        todo 由于去掉了systemEnum类,我们在watch到zk外部服务注册事件之前,根本不知道到底有哪些application
        return false;
    }*/
    String zNodeDataStr = new String(zNode.getData());
    JSONObject zNodeData;
    try {
        zNodeData = JSON.parseObject(zNodeDataStr);
    } catch (JSONException notJsonString) {
        LOG.debug(String.format("节点%s的data是%s,它不是我们要找的那个服务节点,排除掉!  这是我期待结果之一,不打印堆栈", path, zNodeDataStr));
        return false;
    }
    try {
        if (zNodeData.getJSONObject("payload") == null) {
            LOG.debug(String.format("节点=%s,内容=%s,其内缺少payload属性,它不是我们预期的服务节点!", path, zNodeDataStr));
            return false;
        }
    } catch (JSONException payloadIsNotJsonString) {
        LOG.info(String.format("节点%s的data=%s,其内的payload属性是不是json格式,肯定是有非预期的节点混入,这里排除之", path, zNodeDataStr));
        return false;
    }
    return true;
}
 
Example 12
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 13
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 14
Source File: AbstractGenericData.java    From niubi-job with Apache License 2.0 4 votes vote down vote up
public AbstractGenericData(ChildData childData) {
    this(childData.getPath(), childData.getData());
}
 
Example 15
Source File: ConsumerNodeChangeListener.java    From sofa-dashboard with Apache License 2.0 4 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) {

    // 解决自动重连情况下出现的空指针问题
    ChildData data = event.getData();
    if (data == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("event type : {}", event.getType());
        }
        return;
    }

    final String path = data.getPath();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("consumer : {}", path);
    }

    switch (event.getType()) {

        case CHILD_ADDED:

            String addConsumerData = StringUtil.substringAfterLast(path, "/");
            String addServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/consumers/");

            List<RpcConsumer> addConsumers = new ArrayList<>();
            addConsumers.add(convert2Consumer(addServiceName, addConsumerData));
            registryDataCache.addConsumers(addServiceName, addConsumers);
            break;
        case CHILD_REMOVED:

            String removeConsumerData = StringUtil.substringAfterLast(path, "/");
            String removeServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/consumers/");

            List<RpcConsumer> removeConsumers = new ArrayList<>();
            removeConsumers.add(convert2Consumer(removeServiceName, removeConsumerData));
            registryDataCache.removeConsumers(removeServiceName, removeConsumers);

            break;
        case CHILD_UPDATED:

            String updateConsumerData = StringUtil.substringAfterLast(path, "/");
            String updateServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/consumers/");

            List<RpcConsumer> updateConsumers = new ArrayList<>();
            updateConsumers.add(convert2Consumer(updateServiceName, updateConsumerData));
            registryDataCache.updateConsumers(updateServiceName, updateConsumers);
            break;

        default:
            break;
    }

}
 
Example 16
Source File: ProviderNodeChangeListener.java    From sofa-dashboard with Apache License 2.0 4 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {

    // 解决自动重连情况下出现的空指针问题
    ChildData data = event.getData();
    if (data == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("event type : {}", event.getType());
        }
        return;
    }

    final String path = data.getPath();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("provider : {}", path);
    }
    switch (event.getType()) {

        case CHILD_ADDED:
            String providerData = StringUtil.substringAfterLast(path, "/");
            String serviceName = StringUtil.substringBetween(path, "/sofa-rpc/", "/providers/");
            List<RpcProvider> providerDataList = new ArrayList<>();
            providerDataList.add(convert2Provider(serviceName, providerData));
            registryDataCache.addProviders(serviceName, providerDataList);
            break;
        case CHILD_REMOVED:

            List<RpcProvider> removeProviders = new ArrayList<>();
            String removeProviderData = StringUtil.substringAfterLast(path, "/");
            String removeServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/providers/");
            removeProviders.add(convert2Provider(removeServiceName, removeProviderData));
            registryDataCache.removeProviders(removeServiceName, removeProviders);
            break;
        case CHILD_UPDATED:

            List<RpcProvider> updateProviders = new ArrayList<>();
            String updateProviderData = StringUtil.substringAfterLast(path, "/");
            String updateServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/providers/");
            updateProviders.add(convert2Provider(updateServiceName, updateProviderData));
            registryDataCache.updateProviders(updateServiceName, updateProviders);
            break;

        default:
            break;
    }

}
 
Example 17
Source File: ZkClient.java    From binlake with Apache License 2.0 4 votes vote down vote up
/**
 * add ephemeral node to MySQL znode
 *
 * @param client
 * @param data
 * @param zkConfig
 * @throws Exception
 */
private void addEphemeralNode(CuratorFramework client, ChildData data, ZKConfig zkConfig) throws Exception {
    String key = getChildDataKey(data);

    if (lsm.containsKey(key)) {
        LogUtils.warn.warn("key: " + key + " already exist in leaderMap");
        return;
    }

    String path = data.getPath() + ConstUtils.ZK_DYNAMIC_PATH;
    String counterPath = data.getPath() + ConstUtils.ZK_COUNTER_PATH;

    if (LogUtils.debug.isDebugEnabled()) {
        LogUtils.debug.debug("addEphemeralNode path :" + path);
    }

    byte[] binLogByte = client.getData().forPath(path);
    byte[] counterBytes = client.getData().forPath(counterPath);
    Meta.DbInfo dbInfo = MetaUtils.dbInfo(client, data.getPath());
    Meta.BinlogInfo binlogInfo = Meta.BinlogInfo.unmarshalJson(binLogByte);
    Meta.Counter counter = Meta.Counter.unmarshalJson(counterBytes);
    MetaInfo metaInfo = new MetaInfo(dbInfo, binlogInfo, counter);

    String candidatePath = data.getPath() + ConstUtils.ZK_CANDIDATE_PATH;
    if (client.checkExists().forPath(candidatePath) != null) {
        byte[] candidateBytes = client.getData().forPath(candidatePath);
        Meta.Candidate candidate = Meta.Candidate.unmarshalJson(candidateBytes);
        if (candidate.getHost() != null && !candidate.getHost().contains(host)) {
            LogUtils.warn.warn("local host " + host + " is not contained in candidate hosts, not add ephemeral node");
            return;
        }
    }

    if (metaInfo.getRetryTimes() > sc.getDumpLatch()) {
        // session retry times is over than latch, just abandon to create listener on this node
        LogUtils.info.info("MySQL ZNode " + metaInfo.getHost() + ":" + metaInfo.getPort() + " retryTimes : " + metaInfo.getRetryTimes() + " > " +
                sc.getDumpLatch());
        return;
    }

    switch (dbInfo.getState()) {
        case OFFLINE:
            LogUtils.warn.warn("host: " + metaInfo.getHost() + " is offline");
            return;
    }

    LogUtils.debug.debug(binlogInfo);

    // start current leader
    ILeaderSelector lsi = new ZkLeaderSelector(data.getPath(),
            key, zkConfig, sc, hc, workInit);
    lsi.startSelector();

    // previous leader
    ILeaderSelector pl = lsm.putIfAbsent(getChildDataKey(data), lsi);

    if (pl != null) {
        pl.close();
    }
}
 
Example 18
Source File: ZkClient.java    From binlake with Apache License 2.0 2 votes vote down vote up
/**
 * 带上时间戳是为防止 删除立马又新建情况下
 * <p>
 * 本应该删除老的却删除了新的节点 出现数据不对
 *
 * @param data
 * @return
 */
private String getChildDataKey(ChildData data) {
    return data.getPath() + ":" + data.getStat().getCtime();
}