Skip to content

Commit

Permalink
[ISSUE apache#3833] optimization ZookeeperMetaService (apache#4780)
Browse files Browse the repository at this point in the history
a. Change to private modifier.
b. Repeat code extraction as method.
  • Loading branch information
Ish1yu authored Feb 27, 2024
1 parent c93fc3e commit 713f7c6
Showing 1 changed file with 39 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -69,7 +70,7 @@ public class ZookeeperMetaService implements MetaService {
private String serverAddr;

@Getter
public CuratorFramework zkClient;
private CuratorFramework zkClient;

private ConcurrentMap<String, EventMeshRegisterInfo> eventMeshRegisterInfoMap;

Expand Down Expand Up @@ -184,49 +185,15 @@ public void shutdown() throws MetaException {
public List<EventMeshDataInfo> findEventMeshInfoByCluster(String clusterName) throws MetaException {
List<EventMeshDataInfo> 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<String> 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;
}
Expand All @@ -239,44 +206,49 @@ public List<EventMeshDataInfo> findAllEventMeshInfo() throws MetaException {

String serviceName = entry.getKey();
String clusterName = entry.getValue().getEventMeshClusterName();
try {
String servicePath = formatServicePath(clusterName, serviceName);

List<String> 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<EventMeshDataInfo> eventMeshDataInfoList) throws MetaException {
try {
String servicePath = formatServicePath(clusterName, serviceName);

for (String endpoint : instances) {
String instancePath = servicePath.concat(ZookeeperConstant.PATH_SEPARATOR).concat(endpoint);
List<String> 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
Expand All @@ -290,7 +262,7 @@ public void registerMetadata(Map<String, String> metadataMap) {

@Override
public Map<String, String> getMetaData(String key, boolean fuzzyEnabled) {
return null;
return new HashMap<>();
}

// todo: to be implemented
Expand Down

0 comments on commit 713f7c6

Please sign in to comment.