Skip to content

Commit e19402a

Browse files
authored
[ISSUE apache#3833] optimization ZookeeperMetaService (apache#4780)
a. Change to private modifier. b. Repeat code extraction as method.
1 parent a55ca33 commit e19402a

File tree

1 file changed

+39
-67
lines changed

1 file changed

+39
-67
lines changed

eventmesh-meta/eventmesh-meta-zookeeper/src/main/java/org/apache/eventmesh/meta/zookeeper/service/ZookeeperMetaService.java

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
import java.nio.charset.StandardCharsets;
4949
import java.util.ArrayList;
50+
import java.util.HashMap;
5051
import java.util.List;
5152
import java.util.Map;
5253
import java.util.Objects;
@@ -69,7 +70,7 @@ public class ZookeeperMetaService implements MetaService {
6970
private String serverAddr;
7071

7172
@Getter
72-
public CuratorFramework zkClient;
73+
private CuratorFramework zkClient;
7374

7475
private ConcurrentMap<String, EventMeshRegisterInfo> eventMeshRegisterInfoMap;
7576

@@ -184,49 +185,15 @@ public void shutdown() throws MetaException {
184185
public List<EventMeshDataInfo> findEventMeshInfoByCluster(String clusterName) throws MetaException {
185186
List<EventMeshDataInfo> eventMeshDataInfoList = new ArrayList<>();
186187
for (String key : ConfigurationContextUtil.KEYS) {
188+
187189
CommonConfiguration configuration = ConfigurationContextUtil.get(key);
188190
if (Objects.isNull(configuration)) {
189191
continue;
190192
}
191193
String eventMeshName = configuration.getEventMeshName();
192-
try {
193-
String serviceName = eventMeshName.concat("-").concat(key);
194-
String servicePath = formatServicePath(clusterName, serviceName);
195-
196-
List<String> instances = zkClient.getChildren()
197-
.forPath(servicePath);
198-
199-
if (CollectionUtils.isEmpty(instances)) {
200-
continue;
201-
}
202-
203-
for (String endpoint : instances) {
204-
String instancePath = servicePath.concat(ZookeeperConstant.PATH_SEPARATOR).concat(endpoint);
205-
206-
Stat stat = new Stat();
207-
byte[] data;
208-
try {
209-
data = zkClient.getData()
210-
.storingStatIn(stat)
211-
.forPath(instancePath);
212-
} catch (Exception e) {
213-
log.warn("[ZookeeperRegistryService][findEventMeshInfoByCluster] failed for path: {}", instancePath, e);
214-
continue;
215-
}
216-
217-
EventMeshInstance eventMeshInstance = JsonUtils.parseObject(new String(data, StandardCharsets.UTF_8), EventMeshInstance.class);
218-
219-
EventMeshDataInfo eventMeshDataInfo =
220-
new EventMeshDataInfo(clusterName, serviceName, endpoint, stat.getMtime(),
221-
Objects.requireNonNull(eventMeshInstance, "instance must not be Null").getMetaData());
222-
223-
eventMeshDataInfoList.add(eventMeshDataInfo);
224-
}
225-
226-
} catch (Exception e) {
227-
throw new MetaException("ZookeeperRegistry findEventMeshInfoByCluster failed", e);
228-
}
194+
String serviceName = eventMeshName.concat("-").concat(key);
229195

196+
findEventMeshInfo("findEventMeshInfoByCluster", clusterName, serviceName, eventMeshDataInfoList);
230197
}
231198
return eventMeshDataInfoList;
232199
}
@@ -239,44 +206,49 @@ public List<EventMeshDataInfo> findAllEventMeshInfo() throws MetaException {
239206

240207
String serviceName = entry.getKey();
241208
String clusterName = entry.getValue().getEventMeshClusterName();
242-
try {
243-
String servicePath = formatServicePath(clusterName, serviceName);
244209

245-
List<String> instances = zkClient.getChildren()
246-
.forPath(servicePath);
210+
findEventMeshInfo("findAllEventMeshInfo", clusterName, serviceName, eventMeshDataInfoList);
211+
}
212+
return eventMeshDataInfoList;
213+
}
247214

248-
if (CollectionUtils.isEmpty(instances)) {
249-
continue;
250-
}
215+
private void findEventMeshInfo(String tipTitle, String clusterName,
216+
String serviceName, List<EventMeshDataInfo> eventMeshDataInfoList) throws MetaException {
217+
try {
218+
String servicePath = formatServicePath(clusterName, serviceName);
251219

252-
for (String endpoint : instances) {
253-
String instancePath = servicePath.concat(ZookeeperConstant.PATH_SEPARATOR).concat(endpoint);
220+
List<String> instances = zkClient.getChildren().forPath(servicePath);
254221

255-
Stat stat = new Stat();
256-
byte[] data;
257-
try {
258-
data = zkClient.getData()
259-
.storingStatIn(stat)
260-
.forPath(instancePath);
261-
} catch (Exception e) {
262-
log.warn("[ZookeeperRegistryService][findAllEventMeshInfo] failed for path: {}", instancePath, e);
263-
continue;
264-
}
222+
if (CollectionUtils.isEmpty(instances)) {
223+
return;
224+
}
265225

266-
EventMeshInstance eventMeshInstance = JsonUtils.parseObject(new String(data, StandardCharsets.UTF_8), EventMeshInstance.class);
226+
for (String endpoint : instances) {
227+
String instancePath = servicePath.concat(ZookeeperConstant.PATH_SEPARATOR).concat(endpoint);
228+
229+
Stat stat = new Stat();
230+
byte[] data;
231+
try {
232+
data = zkClient.getData()
233+
.storingStatIn(stat)
234+
.forPath(instancePath);
235+
} catch (Exception e) {
236+
log.warn("[ZookeeperRegistryService][{}] failed for path: {}", tipTitle, instancePath, e);
237+
continue;
238+
}
267239

268-
EventMeshDataInfo eventMeshDataInfo =
269-
new EventMeshDataInfo(clusterName, serviceName, endpoint, stat.getMtime(),
270-
Objects.requireNonNull(eventMeshInstance, "instance must not be Null").getMetaData());
240+
EventMeshInstance eventMeshInstance = JsonUtils.parseObject(new String(data, StandardCharsets.UTF_8), EventMeshInstance.class);
271241

272-
eventMeshDataInfoList.add(eventMeshDataInfo);
273-
}
242+
EventMeshDataInfo eventMeshDataInfo =
243+
new EventMeshDataInfo(clusterName, serviceName, endpoint, stat.getMtime(),
244+
Objects.requireNonNull(eventMeshInstance, "instance must not be Null").getMetaData());
274245

275-
} catch (Exception e) {
276-
throw new MetaException("ZookeeperRegistry findAllEventMeshInfo failed", e);
246+
eventMeshDataInfoList.add(eventMeshDataInfo);
277247
}
248+
249+
} catch (Exception e) {
250+
throw new MetaException(String.format("ZookeeperRegistry {0} failed", tipTitle), e);
278251
}
279-
return eventMeshDataInfoList;
280252
}
281253

282254
@Override
@@ -290,7 +262,7 @@ public void registerMetadata(Map<String, String> metadataMap) {
290262

291263
@Override
292264
public Map<String, String> getMetaData(String key, boolean fuzzyEnabled) {
293-
return null;
265+
return new HashMap<>();
294266
}
295267

296268
// todo: to be implemented

0 commit comments

Comments
 (0)