org.apache.curator.framework.recipes.cache.ChildData Java Examples
The following examples show how to use
org.apache.curator.framework.recipes.cache.ChildData.
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: CuratorNodeCache.java From yuzhouwan with Apache License 2.0 | 7 votes |
public void addNodeCacheListener(String path) throws Exception { Stat existStat = curatorFramework .checkExists() .forPath(path); if (existStat == null) curatorFramework .create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(path); NodeCache nodeCache = new NodeCache(curatorFramework, path, false); nodeCache.start(); nodeCache.getListenable().addListener(() -> { ChildData currentData = nodeCache.getCurrentData(); LOG.info("New Cache Data: {}", currentData == null ? "null" : new String(currentData.getData())); } ); }
Example #2
Source File: CuratorUtil.java From fluo with Apache License 2.0 | 6 votes |
/** * Start watching the fluo app uuid. If it changes or goes away then halt the process. */ public static NodeCache startAppIdWatcher(Environment env) { try { CuratorFramework curator = env.getSharedResources().getCurator(); byte[] uuidBytes = curator.getData().forPath(ZookeeperPath.CONFIG_FLUO_APPLICATION_ID); if (uuidBytes == null) { Halt.halt("Fluo Application UUID not found"); throw new RuntimeException(); // make findbugs happy } final String uuid = new String(uuidBytes, StandardCharsets.UTF_8); final NodeCache nodeCache = new NodeCache(curator, ZookeeperPath.CONFIG_FLUO_APPLICATION_ID); nodeCache.getListenable().addListener(() -> { ChildData node = nodeCache.getCurrentData(); if (node == null || !uuid.equals(new String(node.getData(), StandardCharsets.UTF_8))) { Halt.halt("Fluo Application UUID has changed or disappeared"); } }); nodeCache.start(); return nodeCache; } catch (Exception e) { throw new RuntimeException(e); } }
Example #3
Source File: ZKCacheListener.java From mpush with Apache License 2.0 | 6 votes |
@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 #4
Source File: ZKDelegationTokenSecretManager.java From big-c with Apache License 2.0 | 6 votes |
private void processTokenAddOrUpdate(ChildData data) throws IOException { ByteArrayInputStream bin = new ByteArrayInputStream(data.getData()); DataInputStream din = new DataInputStream(bin); TokenIdent ident = createIdentifier(); ident.readFields(din); long renewDate = din.readLong(); int pwdLen = din.readInt(); byte[] password = new byte[pwdLen]; int numRead = din.read(password, 0, pwdLen); if (numRead > -1) { DelegationTokenInformation tokenInfo = new DelegationTokenInformation(renewDate, password); synchronized (this) { currentTokens.put(ident, tokenInfo); // The cancel task might be waiting notifyAll(); } } }
Example #5
Source File: CuratorMasterSelector.java From zookeeper-book-example with Apache License 2.0 | 6 votes |
void assignTask (String task, byte[] data) throws Exception { /* * Choose worker at random. */ //String designatedWorker = workerList.get(rand.nextInt(workerList.size())); List<ChildData> workersList = workersCache.getCurrentData(); LOG.info("Assigning task {}, data {}", task, new String(data)); String designatedWorker = workersList.get(rand.nextInt(workersList.size())).getPath().replaceFirst("/workers/", ""); /* * Assign task to randomly chosen worker. */ String path = "/assign/" + designatedWorker + "/" + task; createAssignment(path, data); }
Example #6
Source File: GroupMember.java From xian with Apache License 2.0 | 6 votes |
/** * Return the current view of membership. The keys are the IDs * of the members. The values are each member's payload * * @return membership */ public Map<String, byte[]> getCurrentMembers() { ImmutableMap.Builder<String, byte[]> builder = ImmutableMap.builder(); boolean thisIdAdded = false; for ( ChildData data : cache.getCurrentData() ) { String id = idFromPath(data.getPath()); thisIdAdded = thisIdAdded || id.equals(thisId); builder.put(id, data.getData()); } if ( !thisIdAdded ) { builder.put(thisId, pen.getData()); // this instance is always a member } return builder.build(); }
Example #7
Source File: DDLChildListener.java From dble with GNU General Public License v2.0 | 6 votes |
private void lockTableByNewNode(ChildData childData) throws Exception { String data = new String(childData.getData(), StandardCharsets.UTF_8); LOGGER.info("DDL node " + childData.getPath() + " created , and data is " + data); DDLInfo ddlInfo = new DDLInfo(data); final String fromNode = ddlInfo.getFrom(); if (fromNode.equals(ZkConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID))) { return; //self node } if (DDLStatus.INIT != ddlInfo.getStatus()) { return; } String nodeName = childData.getPath().substring(childData.getPath().lastIndexOf("/") + 1); String[] tableInfo = nodeName.split("\\."); final String schema = StringUtil.removeBackQuote(tableInfo[0]); final String table = StringUtil.removeBackQuote(tableInfo[1]); ClusterDelayProvider.delayBeforeUpdateMeta(); try { ProxyMeta.getInstance().getTmManager().addMetaLock(schema, table, ddlInfo.getSql()); } catch (Exception t) { ProxyMeta.getInstance().getTmManager().removeMetaLock(schema, table); throw t; } }
Example #8
Source File: ZookeeperMasterMonitor.java From titus-control-plane with Apache License 2.0 | 6 votes |
private void retrieveAllMasters(CuratorFramework curator, TreeCacheEvent cacheEvent) { logger.debug("Received TreeCacheEvent: {}", cacheEvent); Map<String, ChildData> currentChildren = Evaluators.getOrDefault( masterMonitor.getCurrentChildren(allMastersPath), Collections.emptyMap() ); List<MasterInstance> updatedMasterList = new ArrayList<>(); for (Map.Entry<String, ChildData> entry : currentChildren.entrySet()) { parseMasterInstanceData(entry.getValue()).ifPresent(updatedMasterList::add); } if (!knownMasterInstances.equals(updatedMasterList)) { logger.info("Detected change in TitusMaster state and/or topology: {}", updatedMasterList); knownMasterInstances = updatedMasterList; masterUpdates.onNext(Collections.unmodifiableList(updatedMasterList)); } }
Example #9
Source File: ZookeeperSyncToNacosServiceImplTest.java From nacos-sync with Apache License 2.0 | 6 votes |
public boolean mockSync(TaskDO taskDO) throws Exception { List<ChildData> childDataList = spy(ArrayList.class); ListenerContainer<PathChildrenCacheListener> listeners = mock(ListenerContainer.class); childDataList.add(new ChildData(TEST_PATH, null, null)); when(taskDO.getTaskId()).thenReturn(TEST_TASK_ID); when(taskDO.getSourceClusterId()).thenReturn(TEST_SOURCE_CLUSTER_ID); when(taskDO.getDestClusterId()).thenReturn(TEST_DEST_CLUSTER_ID); CuratorFramework curatorFramework = mock(CuratorFramework.class); doReturn(curatorFramework).when(zookeeperServerHolder).get(any(), any()); doReturn(destNamingService).when(nacosServerHolder).get(any(), any()); doReturn(pathChildrenCache).when(zookeeperSyncToNacosService).getPathCache(any()); when(pathChildrenCache.getCurrentData()).thenReturn(childDataList); doReturn(ClusterTypeEnum.ZK).when(skyWalkerCacheServices).getClusterType(any()); when(pathChildrenCache.getListenable()).thenReturn(listeners); return zookeeperSyncToNacosService.sync(taskDO); }
Example #10
Source File: ZookeeperConfigObserver.java From sofa-rpc with Apache License 2.0 | 6 votes |
/** * 接口配置修改子节点Data列表 * * @param config 接口配置 * @param configPath 配置Path * @param currentData 子节点Data列表 */ public void updateConfigAll(AbstractInterfaceConfig config, String configPath, List<ChildData> currentData) { if (CommonUtils.isEmpty(currentData)) { if (LOGGER.isInfoEnabled(config.getAppName())) { LOGGER.infoWithApp(config.getAppName(), "Receive updateAll data is null"); } } else { if (LOGGER.isInfoEnabled(config.getAppName())) { for (ChildData data : currentData) { LOGGER.infoWithApp(config.getAppName(), "Receive updateAll data: path=[" + data.getPath() + "], data=[" + StringSerializer.decode(data.getData()) + "]" + ", stat=[" + data.getStat() + "]"); } } List<ConfigListener> configListeners = configListenerMap.get(config); if (CommonUtils.isNotEmpty(configListeners)) { List<Map<String, String>> attributes = ZookeeperRegistryHelper.convertConfigToAttributes(configPath, currentData); for (ConfigListener listener : configListeners) { for (Map<String, String> attribute : attributes) { listener.configChanged(attribute); } } } } }
Example #11
Source File: DDLChildListener.java From dble with GNU General Public License v2.0 | 6 votes |
@Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { ClusterDelayProvider.delayAfterGetDdlNotice(); ChildData childData = event.getData(); switch (event.getType()) { case CHILD_ADDED: try { lockTableByNewNode(childData); } catch (Exception e) { LOGGER.warn("CHILD_ADDED error", e); } break; case CHILD_UPDATED: updateMeta(childData); break; case CHILD_REMOVED: deleteNode(childData); break; default: break; } }
Example #12
Source File: ZkClient.java From binlake with Apache License 2.0 | 6 votes |
/** * 关闭dump线程 退出leader 但是不close 临时节点 * * @param data * @throws Exception */ private void closeDump(ChildData data) { LogUtils.debug.debug("closeWork"); /** * 需要放弃leader的线程 future.cancel() */ String leaderKey = getChildDataKey(data); ILeaderSelector ls = lsm.get(leaderKey); if (ls != null) { IBinlogWorker work = null; if ((work = ls.getWork()) != null) { work.close(); } if (ls.getLeaderSelector().hasLeadership()) { ls.abandonLeaderShip(); } } }
Example #13
Source File: ClusterStateHolder.java From hermes with Apache License 2.0 | 6 votes |
private void updateLeaderInfo() { List<ChildData> children = m_leaderLatchPathChildrenCache.getCurrentData(); if (children != null && !children.isEmpty()) { List<String> childrenNames = new ArrayList<>(children.size()); Map<String, byte[]> nameDataMapping = new HashMap<>(children.size()); for (ChildData child : children) { if (child.getData() != null && child.getData().length > 0) { String name = ZKPathUtils.lastSegment(child.getPath()); childrenNames.add(name); nameDataMapping.put(name, child.getData()); } } List<String> sortedChildren = LockInternals.getSortedChildren("latch-", new LockInternalsSorter() { @Override public String fixForSorting(String str, String lockName) { return StandardLockInternalsDriver.standardFixForSorting(str, lockName); } }, childrenNames); m_leader.set(ZKSerializeUtils.deserialize(nameDataMapping.get(sortedChildren.get(0)), HostPort.class)); } }
Example #14
Source File: ViewChildListener.java From dble with GNU General Public License v2.0 | 6 votes |
/** * 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 #15
Source File: ZookeeperClient.java From Bats with Apache License 2.0 | 6 votes |
/** * Returns the value corresponding to the given key, null otherwise. * * If the flag consistent is set, the check is consistent as it is made against Zookeeper directly. Otherwise, * the check is eventually consistent. * * If consistency flag is set to true and version holder is not null, passes version holder to get data change version. * Data change version is retrieved from {@link Stat} object, it increases each time znode data change is performed. * Link to Zookeeper documentation - https://zookeeper.apache.org/doc/r3.2.2/zookeeperProgrammers.html#sc_zkDataModel_znodes * * @param path target path * @param consistent consistency check * @param version version holder */ public byte[] get(final String path, final boolean consistent, final DataChangeVersion version) { Preconditions.checkNotNull(path, "path is required"); final String target = PathUtils.join(root, path); if (consistent) { try { if (version != null) { Stat stat = new Stat(); final byte[] bytes = curator.getData().storingStatIn(stat).forPath(target); version.setVersion(stat.getVersion()); return bytes; } return curator.getData().forPath(target); } catch (final Exception ex) { throw new DrillRuntimeException(String.format("error retrieving value for [%s]", path), ex); } } else { final ChildData data = getCache().getCurrentData(target); if (data != null) { return data.getData(); } } return null; }
Example #16
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 #17
Source File: DubboServiceDiscoveryAutoConfiguration.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
/** * 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 #18
Source File: PollingZooKeeperConfigurationProvider.java From pulsar with Apache License 2.0 | 5 votes |
private void refreshConfiguration() throws IOException { LOGGER.info("Refreshing configuration from ZooKeeper"); byte[] data = null; ChildData childData = agentNodeCache.getCurrentData(); if (childData != null) { data = childData.getData(); } flumeConfiguration = configFromBytes(data); eventBus.post(getConfiguration()); }
Example #19
Source File: CuratorClientService.java From knox with Apache License 2.0 | 5 votes |
@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 #20
Source File: MetaServerAssignmentHolder.java From hermes with Apache License 2.0 | 5 votes |
public Assignment<String> getAssignments() { Assignment<String> assignment = new Assignment<>(); List<ChildData> topicNodes = m_pathChildrenCache.getCurrentData(); if (topicNodes != null) { for (ChildData topicNode : topicNodes) { String topic = ZKPathUtils.lastSegment(topicNode.getPath()); assignment.addAssignment(topic, getAssignmentWithoutCache(topic)); } } return assignment; }
Example #21
Source File: OfflineStatusListener.java From dble with GNU General Public License v2.0 | 5 votes |
@Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { ChildData childData = event.getData(); switch (event.getType()) { case CHILD_ADDED: break; case CHILD_UPDATED: break; case CHILD_REMOVED: deleteNode(childData); break; default: break; } }
Example #22
Source File: ZookeeperRegistryCenter.java From shardingsphere-elasticjob-lite with Apache License 2.0 | 5 votes |
@Override public String get(final String key) { TreeCache cache = findTreeCache(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 #23
Source File: MetaServerAssignmentHolder.java From hermes with Apache License 2.0 | 5 votes |
private Map<String, ClientContext> getAssignmentWithoutCache(String topic) { Map<String, ClientContext> assignment = null; ChildData node = m_pathChildrenCache.getCurrentData(ZKPathUtils.getMetaServerAssignmentZkPath(topic)); if (node != null) { assignment = ZKSerializeUtils.deserialize(node.getData(), new TypeReference<Map<String, ClientContext>>() { }.getType()); } return assignment == null ? new HashMap<String, ClientContext>() : assignment; }
Example #24
Source File: ZookeeperProviderObserver.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * Update Provider * * @param config ConsumerConfig * @param providerPath Provider path of zookeeper * @param data Event data * @param currentData provider data list * @throws UnsupportedEncodingException decode error */ public void updateProvider(ConsumerConfig config, String providerPath, ChildData data, List<ChildData> currentData) throws UnsupportedEncodingException { if (LOGGER.isInfoEnabled(config.getAppName())) { LOGGER.infoWithApp(config.getAppName(), "Receive update provider: path=[" + data.getPath() + "]" + ", data=[" + StringSerializer.decode(data.getData()) + "]" + ", stat=[" + data.getStat() + "]" + ", list=[" + currentData.size() + "]"); } notifyListeners(config, providerPath, currentData, false); }
Example #25
Source File: ZookeeperConfigObserver.java From sofa-rpc with Apache License 2.0 | 5 votes |
private void notifyListeners(AbstractInterfaceConfig config, String configPath, ChildData data, boolean removeType) { List<ConfigListener> configListeners = configListenerMap.get(config); if (CommonUtils.isNotEmpty(configListeners)) { //转换子节点Data为接口级配置<配置属性名,配置属性值>,例如<timeout,200> Map<String, String> attribute = ZookeeperRegistryHelper.convertConfigToAttribute(configPath, data, removeType); for (ConfigListener listener : configListeners) { listener.configChanged(attribute); } } }
Example #26
Source File: ZookeeperRegistryCenter.java From eagle with Apache License 2.0 | 5 votes |
@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 #27
Source File: ZookeeperConfigObserver.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 接口配置删除子节点Data * * @param config 接口配置 * @param configPath 配置Path * @param data 子节点Data */ public void removeConfig(AbstractInterfaceConfig config, String configPath, ChildData data) { if (data == null) { if (LOGGER.isInfoEnabled(config.getAppName())) { LOGGER.infoWithApp(config.getAppName(), "Receive remove data is null"); } } else { if (LOGGER.isInfoEnabled(config.getAppName())) { LOGGER.infoWithApp(config.getAppName(), "Receive remove data: path=[" + data.getPath() + "]" + ", data=[" + StringSerializer.decode(data.getData()) + "]" + ", stat=[" + data.getStat() + "]"); } notifyListeners(config, configPath, data, true); } }
Example #28
Source File: ZookeeperConfigObserver.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 接口配置修改子节点Data * * @param config 接口配置 * @param configPath 配置Path * @param data 子节点Data */ public void updateConfig(AbstractInterfaceConfig config, String configPath, ChildData data) { if (data == null) { if (LOGGER.isInfoEnabled(config.getAppName())) { LOGGER.infoWithApp(config.getAppName(), "Receive update data is null"); } } else { if (LOGGER.isInfoEnabled(config.getAppName())) { LOGGER.infoWithApp(config.getAppName(), "Receive update data: path=[" + data.getPath() + "]" + ", data=[" + StringSerializer.decode(data.getData()) + "]" + ", stat=[" + data.getStat() + "]"); } notifyListeners(config, configPath, data, false); } }
Example #29
Source File: SimpleResourceConfigFactoryBean.java From cloud-config with MIT License | 5 votes |
@Override protected void handleChildUpdated(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { ChildData childData = event.getData(); String nodeName = safeGetNodeNameFromEvent(event); if(Arrays.asList(configProfiles).contains(nodeName) || canApplyForLocalMachine(nodeName)) { T newConfig = createConfig(childData.getData(), getObject()); BeanUtils.copyProperties(newConfig, getObject()); getObject().reload(); } }
Example #30
Source File: PathCacheExample.java From curator 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())); } } }