ruanwenjun commented on code in PR #974:
URL:
https://github.com/apache/incubator-eventmesh/pull/974#discussion_r910091389
##########
eventmesh-registry-plugin/eventmesh-registry-zookeeper/src/main/java/org/apache/eventmesh/registry/zookeeper/pojo/Instance.java:
##########
@@ -0,0 +1,50 @@
+package org.apache.eventmesh.registry.zookeeper.pojo;
Review Comment:
Please add license header.
##########
eventmesh-registry-plugin/eventmesh-registry-zookeeper/src/main/java/org/apache/eventmesh/registry/zookeeper/pojo/Instance.java:
##########
@@ -0,0 +1,50 @@
+package org.apache.eventmesh.registry.zookeeper.pojo;
+
+
+import java.io.Serializable;
+import java.util.Map;
+
+public class Instance implements Serializable {
Review Comment:
It's better to rename to EventMeshInstance?
And please add lombok to this module, then you can move the get/set method
of the pojo.
```
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
```
##########
eventmesh-registry-plugin/eventmesh-registry-zookeeper/build.gradle:
##########
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+dependencies {
+ implementation 'log4j:log4j:1.2.17'
+ implementation 'org.apache.zookeeper:zookeeper:3.4.6'
+ implementation 'org.apache.curator:curator-client:4.0.1'
+ implementation 'org.apache.curator:curator-framework:4.0.1'
+ implementation 'org.apache.curator:curator-recipes:4.0.1'
+ implementation project(":eventmesh-registry-plugin:eventmesh-registry-api")
+ implementation project(":eventmesh-common")
+ testImplementation 'org.mockito:mockito-core'
+ testImplementation 'org.apache.curator:curator-test:2.12.0'
Review Comment:
Please manage the dependency version in root/build.gradle, and I remember we
already have log4j in our dependency.
##########
eventmesh-registry-plugin/eventmesh-registry-zookeeper/src/main/java/org/apache/eventmesh/registry/zookeeper/util/JsonUtils.java:
##########
@@ -0,0 +1,26 @@
+package org.apache.eventmesh.registry.zookeeper.util;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class JsonUtils {
+
+ public static String toJSON(Object o) {
+ ObjectMapper objectMapper = new ObjectMapper();
+ try {
+ return objectMapper.writeValueAsString(o);
+ } catch (JsonProcessingException e) {
+ return null;
+ }
+ }
+
+ public static <T> T parseJson(String json, Class<T> c) {
+ ObjectMapper objectMapper = new ObjectMapper();
+ try {
+
+ return objectMapper.readValue(json, c);
+ } catch (JsonProcessingException e) {
+ return null;
+ }
Review Comment:
I remember there is already a JsonUtils in EventMesh
##########
eventmesh-registry-plugin/eventmesh-registry-zookeeper/src/main/java/org/apache/eventmesh/registry/zookeeper/service/ZookeeperRegistryService.java:
##########
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eventmesh.registry.zookeeper.service;
+
+
+import org.apache.eventmesh.api.exception.RegistryException;
+import org.apache.eventmesh.api.registry.RegistryService;
+import org.apache.eventmesh.api.registry.dto.EventMeshDataInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshRegisterInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshUnRegisterInfo;
+import org.apache.eventmesh.common.config.CommonConfiguration;
+import org.apache.eventmesh.common.utils.ConfigurationContextUtil;
+import org.apache.eventmesh.registry.zookeeper.constant.ZookeeperConstant;
+import org.apache.eventmesh.registry.zookeeper.pojo.Instance;
+import org.apache.eventmesh.registry.zookeeper.util.JsonUtils;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.curator.RetryPolicy;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.data.Stat;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class ZookeeperRegistryService implements RegistryService {
+
+ private static final Logger logger =
LoggerFactory.getLogger(ZookeeperRegistryService.class);
+
+ private static final AtomicBoolean INIT_STATUS = new AtomicBoolean(false);
+
+ private static final AtomicBoolean START_STATUS = new AtomicBoolean(false);
+
+ private String serverAddr;
+
+ public CuratorFramework zkClient = null;
+
+ private Map<String, EventMeshRegisterInfo> eventMeshRegisterInfoMap;
+
+ @Override
+ public void init() throws RegistryException {
+ boolean update = INIT_STATUS.compareAndSet(false, true);
+ if (!update) {
+ return;
+ }
+ eventMeshRegisterInfoMap = new
HashMap<>(ConfigurationContextUtil.KEYS.size());
+ for (String key : ConfigurationContextUtil.KEYS) {
+ CommonConfiguration commonConfiguration =
ConfigurationContextUtil.get(key);
+ if (null == commonConfiguration) {
+ continue;
+ }
+ if (StringUtils.isBlank(commonConfiguration.namesrvAddr)) {
+ throw new RegistryException("namesrvAddr cannot be null");
+ }
+ this.serverAddr = commonConfiguration.namesrvAddr;
+ break;
+ }
+ }
+
+ @Override
+ public void start() throws RegistryException {
+ boolean update = START_STATUS.compareAndSet(false, true);
+ if (!update) {
+ return;
+ }
+ try {
+ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
+ zkClient = CuratorFrameworkFactory.builder()
+ .connectString(serverAddr)
+ .retryPolicy(retryPolicy)
+ .namespace(ZookeeperConstant.NAMESPACE)
+ .build();
+ zkClient.start();
+
+ } catch (Exception e) {
+ logger.error("[ZookeeperRegistryService][start] error", e);
+ throw new RegistryException(e.getMessage());
+ }
+ }
+
+ @Override
+ public void shutdown() throws RegistryException {
+ INIT_STATUS.compareAndSet(true, false);
+ START_STATUS.compareAndSet(true, false);
+ try {
+ zkClient.close();
+ } catch (Exception e) {
+ logger.error("[ZookeeperRegistryService][shutdown] error", e);
+ throw new RegistryException(e.getMessage());
+ }
+ logger.info("ZookeeperRegistryService close");
Review Comment:
```suggestion
logger.info("ZookeeperRegistryService closed");
```
##########
eventmesh-registry-plugin/eventmesh-registry-zookeeper/src/main/java/org/apache/eventmesh/registry/zookeeper/service/ZookeeperRegistryService.java:
##########
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eventmesh.registry.zookeeper.service;
+
+
+import org.apache.eventmesh.api.exception.RegistryException;
+import org.apache.eventmesh.api.registry.RegistryService;
+import org.apache.eventmesh.api.registry.dto.EventMeshDataInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshRegisterInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshUnRegisterInfo;
+import org.apache.eventmesh.common.config.CommonConfiguration;
+import org.apache.eventmesh.common.utils.ConfigurationContextUtil;
+import org.apache.eventmesh.registry.zookeeper.constant.ZookeeperConstant;
+import org.apache.eventmesh.registry.zookeeper.pojo.Instance;
+import org.apache.eventmesh.registry.zookeeper.util.JsonUtils;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.curator.RetryPolicy;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.data.Stat;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class ZookeeperRegistryService implements RegistryService {
+
+ private static final Logger logger =
LoggerFactory.getLogger(ZookeeperRegistryService.class);
+
+ private static final AtomicBoolean INIT_STATUS = new AtomicBoolean(false);
+
+ private static final AtomicBoolean START_STATUS = new AtomicBoolean(false);
+
+ private String serverAddr;
+
+ public CuratorFramework zkClient = null;
+
+ private Map<String, EventMeshRegisterInfo> eventMeshRegisterInfoMap;
+
+ @Override
+ public void init() throws RegistryException {
+ boolean update = INIT_STATUS.compareAndSet(false, true);
+ if (!update) {
+ return;
+ }
+ eventMeshRegisterInfoMap = new
HashMap<>(ConfigurationContextUtil.KEYS.size());
+ for (String key : ConfigurationContextUtil.KEYS) {
+ CommonConfiguration commonConfiguration =
ConfigurationContextUtil.get(key);
+ if (null == commonConfiguration) {
+ continue;
+ }
+ if (StringUtils.isBlank(commonConfiguration.namesrvAddr)) {
+ throw new RegistryException("namesrvAddr cannot be null");
+ }
+ this.serverAddr = commonConfiguration.namesrvAddr;
+ break;
+ }
Review Comment:
If the `serverAddr` is null throw exception here?
##########
eventmesh-registry-plugin/eventmesh-registry-zookeeper/src/main/java/org/apache/eventmesh/registry/zookeeper/service/ZookeeperRegistryService.java:
##########
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eventmesh.registry.zookeeper.service;
+
+
+import org.apache.eventmesh.api.exception.RegistryException;
+import org.apache.eventmesh.api.registry.RegistryService;
+import org.apache.eventmesh.api.registry.dto.EventMeshDataInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshRegisterInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshUnRegisterInfo;
+import org.apache.eventmesh.common.config.CommonConfiguration;
+import org.apache.eventmesh.common.utils.ConfigurationContextUtil;
+import org.apache.eventmesh.registry.zookeeper.constant.ZookeeperConstant;
+import org.apache.eventmesh.registry.zookeeper.pojo.Instance;
+import org.apache.eventmesh.registry.zookeeper.util.JsonUtils;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.curator.RetryPolicy;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.data.Stat;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class ZookeeperRegistryService implements RegistryService {
+
+ private static final Logger logger =
LoggerFactory.getLogger(ZookeeperRegistryService.class);
+
+ private static final AtomicBoolean INIT_STATUS = new AtomicBoolean(false);
+
+ private static final AtomicBoolean START_STATUS = new AtomicBoolean(false);
+
+ private String serverAddr;
+
+ public CuratorFramework zkClient = null;
+
+ private Map<String, EventMeshRegisterInfo> eventMeshRegisterInfoMap;
+
+ @Override
+ public void init() throws RegistryException {
+ boolean update = INIT_STATUS.compareAndSet(false, true);
+ if (!update) {
+ return;
+ }
+ eventMeshRegisterInfoMap = new
HashMap<>(ConfigurationContextUtil.KEYS.size());
+ for (String key : ConfigurationContextUtil.KEYS) {
+ CommonConfiguration commonConfiguration =
ConfigurationContextUtil.get(key);
+ if (null == commonConfiguration) {
+ continue;
+ }
+ if (StringUtils.isBlank(commonConfiguration.namesrvAddr)) {
+ throw new RegistryException("namesrvAddr cannot be null");
+ }
+ this.serverAddr = commonConfiguration.namesrvAddr;
+ break;
+ }
+ }
+
+ @Override
+ public void start() throws RegistryException {
+ boolean update = START_STATUS.compareAndSet(false, true);
+ if (!update) {
+ return;
+ }
+ try {
+ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
+ zkClient = CuratorFrameworkFactory.builder()
+ .connectString(serverAddr)
+ .retryPolicy(retryPolicy)
+ .namespace(ZookeeperConstant.NAMESPACE)
+ .build();
+ zkClient.start();
+
+ } catch (Exception e) {
+ logger.error("[ZookeeperRegistryService][start] error", e);
+ throw new RegistryException(e.getMessage());
+ }
+ }
+
+ @Override
+ public void shutdown() throws RegistryException {
+ INIT_STATUS.compareAndSet(true, false);
+ START_STATUS.compareAndSet(true, false);
+ try {
+ zkClient.close();
+ } catch (Exception e) {
+ logger.error("[ZookeeperRegistryService][shutdown] error", e);
+ throw new RegistryException(e.getMessage());
+ }
+ logger.info("ZookeeperRegistryService close");
+ }
+
+ @Override
+ public List<EventMeshDataInfo> findEventMeshInfoByCluster(String
clusterName) throws RegistryException {
+ List<EventMeshDataInfo> eventMeshDataInfoList = new ArrayList<>();
+ for (String key : ConfigurationContextUtil.KEYS) {
+ CommonConfiguration configuration =
ConfigurationContextUtil.get(key);
+ if (Objects.isNull(configuration)) {
+ continue;
+ }
+ String eventMeshName = configuration.eventMeshName;
+ 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 = new byte[0];
+ try {
+ data = zkClient.getData()
+ .storingStatIn(stat)
+ .forPath(instancePath);
+ } catch (Exception e) {
+
logger.error("[ZookeeperRegistryService][findEventMeshInfoByCluster][findInstanceData]
error", e);
+ }
+
+ Instance instance = JsonUtils.parseJson(new String(data,
ZookeeperConstant.CHARSET_UTF8), Instance.class);
+
+ EventMeshDataInfo eventMeshDataInfo =
+ new EventMeshDataInfo(clusterName, serviceName,
endpoint, stat.getMtime(), instance.getMetaData());
+
+ eventMeshDataInfoList.add(eventMeshDataInfo);
+ }
+
+ } catch (Exception e) {
+
logger.error("[ZookeeperRegistryService][findEventMeshInfoByCluster] error", e);
+ throw new RegistryException(e.getMessage());
+ }
+
+ }
+ return eventMeshDataInfoList;
+ }
+
+ @Override
+ public List<EventMeshDataInfo> findAllEventMeshInfo() throws
RegistryException {
+ List<EventMeshDataInfo> eventMeshDataInfoList = new ArrayList<>();
+
+ for (Map.Entry<String, EventMeshRegisterInfo> entry :
eventMeshRegisterInfoMap.entrySet()) {
+
+ String serviceName = entry.getKey();
+ String clusterName = entry.getValue().getEventMeshClusterName();
+ try {
+ 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 = new byte[0];
+ try {
+ data = zkClient.getData()
+ .storingStatIn(stat)
+ .forPath(instancePath);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ Instance instance = JsonUtils.parseJson(new String(data,
ZookeeperConstant.CHARSET_UTF8), Instance.class);
+
+ EventMeshDataInfo eventMeshDataInfo =
+ new EventMeshDataInfo(clusterName, serviceName,
endpoint, stat.getMtime(), instance.getMetaData());
+
+ eventMeshDataInfoList.add(eventMeshDataInfo);
+ }
+
+ } catch (Exception e) {
+ logger.error("[ZookeeperRegistryService][findAllEventMeshInfo]
error", e);
+ throw new RegistryException(e.getMessage());
+ }
+ }
+ return eventMeshDataInfoList;
+ }
+
+ @Override
+ public Map<String, Map<String, Integer>>
findEventMeshClientDistributionData(String clusterName, String group, String
purpose)
+ throws RegistryException {
+ // todo find metadata
+ return null;
+ }
+
+ @Override
+ public void registerMetadata(Map<String, String> metadataMap) {
+ for (Map.Entry<String, EventMeshRegisterInfo> eventMeshRegisterInfo :
eventMeshRegisterInfoMap.entrySet()) {
+ EventMeshRegisterInfo registerInfo =
eventMeshRegisterInfo.getValue();
+ registerInfo.setMetadata(metadataMap);
+ this.register(registerInfo);
+ }
+ }
+
+ @Override
+ public boolean register(EventMeshRegisterInfo eventMeshRegisterInfo)
throws RegistryException {
+ try {
+
+ String[] ipPort =
eventMeshRegisterInfo.getEndPoint().split(ZookeeperConstant.IP_PORT_SEPARATOR);
+ String ip = ipPort[0];
+ Integer port = Integer.valueOf(ipPort[1]);
+ String eventMeshName = eventMeshRegisterInfo.getEventMeshName();
+ String eventMeshClusterName =
eventMeshRegisterInfo.getEventMeshClusterName();
+ Map<String, Map<String, Integer>> instanceNumMap =
eventMeshRegisterInfo.getEventMeshInstanceNumMap();
+ Map<String, String> metadata = eventMeshRegisterInfo.getMetadata();
+
+ // clusterName/eventMeshName/ip:port
+ String path = formatInstancePath(eventMeshClusterName,
eventMeshName, eventMeshRegisterInfo.getEndPoint());
+
+ Instance instance = new Instance();
+ instance.setIp(ip);
+ instance.setPort(port);
+ instance.setInstanceNumMap(instanceNumMap);
+ instance.setMetaData(metadata);
+
+ Stat stat = zkClient.checkExists().forPath(path);
+ if (stat == null) {
+ //not exits,to create
+ zkClient.create()
+ .creatingParentsIfNeeded()
+ .withMode(CreateMode.EPHEMERAL)
+ .forPath(path,
JsonUtils.toJSON(instance).getBytes(ZookeeperConstant.CHARSET_UTF8));
+ } else {
Review Comment:
This is not atomic operation, you can use `.create().orSetData()`
##########
eventmesh-registry-plugin/eventmesh-registry-zookeeper/src/main/java/org/apache/eventmesh/registry/zookeeper/service/ZookeeperRegistryService.java:
##########
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eventmesh.registry.zookeeper.service;
+
+
+import org.apache.eventmesh.api.exception.RegistryException;
+import org.apache.eventmesh.api.registry.RegistryService;
+import org.apache.eventmesh.api.registry.dto.EventMeshDataInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshRegisterInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshUnRegisterInfo;
+import org.apache.eventmesh.common.config.CommonConfiguration;
+import org.apache.eventmesh.common.utils.ConfigurationContextUtil;
+import org.apache.eventmesh.registry.zookeeper.constant.ZookeeperConstant;
+import org.apache.eventmesh.registry.zookeeper.pojo.Instance;
+import org.apache.eventmesh.registry.zookeeper.util.JsonUtils;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.curator.RetryPolicy;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.data.Stat;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class ZookeeperRegistryService implements RegistryService {
+
+ private static final Logger logger =
LoggerFactory.getLogger(ZookeeperRegistryService.class);
+
+ private static final AtomicBoolean INIT_STATUS = new AtomicBoolean(false);
+
+ private static final AtomicBoolean START_STATUS = new AtomicBoolean(false);
+
+ private String serverAddr;
+
+ public CuratorFramework zkClient = null;
+
+ private Map<String, EventMeshRegisterInfo> eventMeshRegisterInfoMap;
+
+ @Override
+ public void init() throws RegistryException {
+ boolean update = INIT_STATUS.compareAndSet(false, true);
+ if (!update) {
+ return;
Review Comment:
Add a warning log here?
##########
eventmesh-registry-plugin/eventmesh-registry-zookeeper/src/main/java/org/apache/eventmesh/registry/zookeeper/service/ZookeeperRegistryService.java:
##########
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.eventmesh.registry.zookeeper.service;
+
+
+import org.apache.eventmesh.api.exception.RegistryException;
+import org.apache.eventmesh.api.registry.RegistryService;
+import org.apache.eventmesh.api.registry.dto.EventMeshDataInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshRegisterInfo;
+import org.apache.eventmesh.api.registry.dto.EventMeshUnRegisterInfo;
+import org.apache.eventmesh.common.config.CommonConfiguration;
+import org.apache.eventmesh.common.utils.ConfigurationContextUtil;
+import org.apache.eventmesh.registry.zookeeper.constant.ZookeeperConstant;
+import org.apache.eventmesh.registry.zookeeper.pojo.Instance;
+import org.apache.eventmesh.registry.zookeeper.util.JsonUtils;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.curator.RetryPolicy;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.data.Stat;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class ZookeeperRegistryService implements RegistryService {
+
+ private static final Logger logger =
LoggerFactory.getLogger(ZookeeperRegistryService.class);
+
+ private static final AtomicBoolean INIT_STATUS = new AtomicBoolean(false);
+
+ private static final AtomicBoolean START_STATUS = new AtomicBoolean(false);
+
+ private String serverAddr;
+
+ public CuratorFramework zkClient = null;
+
+ private Map<String, EventMeshRegisterInfo> eventMeshRegisterInfoMap;
+
+ @Override
+ public void init() throws RegistryException {
+ boolean update = INIT_STATUS.compareAndSet(false, true);
+ if (!update) {
+ return;
+ }
+ eventMeshRegisterInfoMap = new
HashMap<>(ConfigurationContextUtil.KEYS.size());
+ for (String key : ConfigurationContextUtil.KEYS) {
+ CommonConfiguration commonConfiguration =
ConfigurationContextUtil.get(key);
+ if (null == commonConfiguration) {
+ continue;
+ }
+ if (StringUtils.isBlank(commonConfiguration.namesrvAddr)) {
+ throw new RegistryException("namesrvAddr cannot be null");
+ }
+ this.serverAddr = commonConfiguration.namesrvAddr;
+ break;
+ }
+ }
+
+ @Override
+ public void start() throws RegistryException {
+ boolean update = START_STATUS.compareAndSet(false, true);
+ if (!update) {
+ return;
+ }
+ try {
+ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
+ zkClient = CuratorFrameworkFactory.builder()
+ .connectString(serverAddr)
+ .retryPolicy(retryPolicy)
+ .namespace(ZookeeperConstant.NAMESPACE)
+ .build();
+ zkClient.start();
+
+ } catch (Exception e) {
+ logger.error("[ZookeeperRegistryService][start] error", e);
+ throw new RegistryException(e.getMessage());
+ }
+ }
+
+ @Override
+ public void shutdown() throws RegistryException {
+ INIT_STATUS.compareAndSet(true, false);
+ START_STATUS.compareAndSet(true, false);
+ try {
+ zkClient.close();
+ } catch (Exception e) {
+ logger.error("[ZookeeperRegistryService][shutdown] error", e);
+ throw new RegistryException(e.getMessage());
+ }
Review Comment:
```suggestion
try (CuratorFramework closedClient = zkClient){
} catch (Exception e) {
throw new RegistryException("ZookeeperRegistry shutdown failed"
,e);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]