diff --git a/eventmesh-meta/eventmesh-meta-zookeeper/src/main/java/org/apache/eventmesh/meta/zookeeper/service/ZookeeperMetaService.java b/eventmesh-meta/eventmesh-meta-zookeeper/src/main/java/org/apache/eventmesh/meta/zookeeper/service/ZookeeperMetaService.java index 5cee22d9c0..844ab18047 100644 --- a/eventmesh-meta/eventmesh-meta-zookeeper/src/main/java/org/apache/eventmesh/meta/zookeeper/service/ZookeeperMetaService.java +++ b/eventmesh-meta/eventmesh-meta-zookeeper/src/main/java/org/apache/eventmesh/meta/zookeeper/service/ZookeeperMetaService.java @@ -47,6 +47,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -69,7 +70,7 @@ public class ZookeeperMetaService implements MetaService { private String serverAddr; @Getter - public CuratorFramework zkClient; + private CuratorFramework zkClient; private ConcurrentMap eventMeshRegisterInfoMap; @@ -184,49 +185,15 @@ public void shutdown() throws MetaException { public List findEventMeshInfoByCluster(String clusterName) throws MetaException { List eventMeshDataInfoList = new ArrayList<>(); for (String key : ConfigurationContextUtil.KEYS) { + CommonConfiguration configuration = ConfigurationContextUtil.get(key); if (Objects.isNull(configuration)) { continue; } String eventMeshName = configuration.getEventMeshName(); - try { - String serviceName = eventMeshName.concat("-").concat(key); - String servicePath = formatServicePath(clusterName, serviceName); - - List instances = zkClient.getChildren() - .forPath(servicePath); - - if (CollectionUtils.isEmpty(instances)) { - continue; - } - - for (String endpoint : instances) { - String instancePath = servicePath.concat(ZookeeperConstant.PATH_SEPARATOR).concat(endpoint); - - Stat stat = new Stat(); - byte[] data; - try { - data = zkClient.getData() - .storingStatIn(stat) - .forPath(instancePath); - } catch (Exception e) { - log.warn("[ZookeeperRegistryService][findEventMeshInfoByCluster] failed for path: {}", instancePath, e); - continue; - } - - EventMeshInstance eventMeshInstance = JsonUtils.parseObject(new String(data, StandardCharsets.UTF_8), EventMeshInstance.class); - - EventMeshDataInfo eventMeshDataInfo = - new EventMeshDataInfo(clusterName, serviceName, endpoint, stat.getMtime(), - Objects.requireNonNull(eventMeshInstance, "instance must not be Null").getMetaData()); - - eventMeshDataInfoList.add(eventMeshDataInfo); - } - - } catch (Exception e) { - throw new MetaException("ZookeeperRegistry findEventMeshInfoByCluster failed", e); - } + String serviceName = eventMeshName.concat("-").concat(key); + findEventMeshInfo("findEventMeshInfoByCluster", clusterName, serviceName, eventMeshDataInfoList); } return eventMeshDataInfoList; } @@ -239,44 +206,49 @@ public List findAllEventMeshInfo() throws MetaException { String serviceName = entry.getKey(); String clusterName = entry.getValue().getEventMeshClusterName(); - try { - String servicePath = formatServicePath(clusterName, serviceName); - List instances = zkClient.getChildren() - .forPath(servicePath); + findEventMeshInfo("findAllEventMeshInfo", clusterName, serviceName, eventMeshDataInfoList); + } + return eventMeshDataInfoList; + } - if (CollectionUtils.isEmpty(instances)) { - continue; - } + private void findEventMeshInfo(String tipTitle, String clusterName, + String serviceName, List eventMeshDataInfoList) throws MetaException { + try { + String servicePath = formatServicePath(clusterName, serviceName); - for (String endpoint : instances) { - String instancePath = servicePath.concat(ZookeeperConstant.PATH_SEPARATOR).concat(endpoint); + List instances = zkClient.getChildren().forPath(servicePath); - Stat stat = new Stat(); - byte[] data; - try { - data = zkClient.getData() - .storingStatIn(stat) - .forPath(instancePath); - } catch (Exception e) { - log.warn("[ZookeeperRegistryService][findAllEventMeshInfo] failed for path: {}", instancePath, e); - continue; - } + if (CollectionUtils.isEmpty(instances)) { + return; + } - EventMeshInstance eventMeshInstance = JsonUtils.parseObject(new String(data, StandardCharsets.UTF_8), EventMeshInstance.class); + for (String endpoint : instances) { + String instancePath = servicePath.concat(ZookeeperConstant.PATH_SEPARATOR).concat(endpoint); + + Stat stat = new Stat(); + byte[] data; + try { + data = zkClient.getData() + .storingStatIn(stat) + .forPath(instancePath); + } catch (Exception e) { + log.warn("[ZookeeperRegistryService][{}] failed for path: {}", tipTitle, instancePath, e); + continue; + } - EventMeshDataInfo eventMeshDataInfo = - new EventMeshDataInfo(clusterName, serviceName, endpoint, stat.getMtime(), - Objects.requireNonNull(eventMeshInstance, "instance must not be Null").getMetaData()); + EventMeshInstance eventMeshInstance = JsonUtils.parseObject(new String(data, StandardCharsets.UTF_8), EventMeshInstance.class); - eventMeshDataInfoList.add(eventMeshDataInfo); - } + EventMeshDataInfo eventMeshDataInfo = + new EventMeshDataInfo(clusterName, serviceName, endpoint, stat.getMtime(), + Objects.requireNonNull(eventMeshInstance, "instance must not be Null").getMetaData()); - } catch (Exception e) { - throw new MetaException("ZookeeperRegistry findAllEventMeshInfo failed", e); + eventMeshDataInfoList.add(eventMeshDataInfo); } + + } catch (Exception e) { + throw new MetaException(String.format("ZookeeperRegistry {0} failed", tipTitle), e); } - return eventMeshDataInfoList; } @Override @@ -290,7 +262,7 @@ public void registerMetadata(Map metadataMap) { @Override public Map getMetaData(String key, boolean fuzzyEnabled) { - return null; + return new HashMap<>(); } // todo: to be implemented