diff --git a/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java b/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java
index f71fb78b6..59e4b0d36 100644
--- a/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java
+++ b/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java
@@ -93,7 +93,7 @@ public interface CmdbService {
*
* @param entityName name of entity
* @param entityType type of entity
- * @return
+ * @return entity.
*/
Entity getEntity(String entityName, String entityType);
}
diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigBatchListenRequest.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigBatchListenRequest.java
index dd7c615a6..5e4cc9bcb 100644
--- a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigBatchListenRequest.java
+++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigBatchListenRequest.java
@@ -100,13 +100,13 @@ public class ConfigBatchListenRequest extends AbstractConfigRequest {
public ConfigListenContext() {
}
-
+
@Override
public String toString() {
return "ConfigListenContext{" + "group='" + group + '\'' + ", md5='" + md5 + '\'' + ", dataId='" + dataId
+ '\'' + ", tenant='" + tenant + '\'' + '}';
}
-
+
/**
* Getter method for property group.
*
diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequest.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequest.java
index 171d0615a..e3b51a95a 100644
--- a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequest.java
+++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequest.java
@@ -41,7 +41,7 @@ public class ConfigChangeClusterSyncRequest extends AbstractConfigRequest {
/**
* is beta.
*
- * @return
+ * @return is beta or not.
*/
public boolean isBeta() {
return "Y".equalsIgnoreCase(isBeta);
diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigChangeBatchListenResponse.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigChangeBatchListenResponse.java
index 105a6d08c..72fc7ba5c 100644
--- a/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigChangeBatchListenResponse.java
+++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigChangeBatchListenResponse.java
@@ -36,9 +36,10 @@ public class ConfigChangeBatchListenResponse extends Response {
}
/**
- * add changed config.
+ * add changed config.
+ *
* @param dataId dataId.
- * @param group group.
+ * @param group group.
* @param tenant tenant.
*/
public void addChangeConfig(String dataId, String group, String tenant) {
@@ -71,7 +72,7 @@ public class ConfigChangeBatchListenResponse extends Response {
* build fail response.
*
* @param errorMessage errorMessage.
- * @return
+ * @return response.
*/
public static ConfigChangeBatchListenResponse buildFailResponse(String errorMessage) {
ConfigChangeBatchListenResponse response = new ConfigChangeBatchListenResponse();
@@ -145,7 +146,7 @@ public class ConfigChangeBatchListenResponse extends Response {
public void setTenant(String tenant) {
this.tenant = tenant;
}
-
+
@Override
public String toString() {
return "ConfigContext{" + "group='" + group + '\'' + ", dataId='" + dataId + '\'' + ", tenant='" + tenant
diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigPubishResponse.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigPublishResponse.java
similarity index 60%
rename from api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigPubishResponse.java
rename to api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigPublishResponse.java
index 9376f463d..b43c1940a 100644
--- a/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigPubishResponse.java
+++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigPublishResponse.java
@@ -20,35 +20,35 @@ import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
/**
- * ConfigPubishResponse.
+ * ConfigPublishResponse.
*
* @author liuzunfei
* @version $Id: ConfigPubishResponse.java, v 0.1 2020年07月16日 4:59 PM liuzunfei Exp $
*/
-public class ConfigPubishResponse extends Response {
+public class ConfigPublishResponse extends Response {
- public ConfigPubishResponse() {
+ public ConfigPublishResponse() {
super();
}
/**
- * Buidl success resposne.
+ * Build success response.
*
- * @return
+ * @return response.
*/
- public static ConfigPubishResponse buildSuccessResponse() {
- return new ConfigPubishResponse();
+ public static ConfigPublishResponse buildSuccessResponse() {
+ return new ConfigPublishResponse();
}
/**
- * Buidl fail resposne.
+ * Build fail response.
*
- * @return
+ * @return response.
*/
- public static ConfigPubishResponse buildFailResponse(String errorMsg) {
- ConfigPubishResponse configPubishResponse = new ConfigPubishResponse();
- configPubishResponse.setResultCode(ResponseCode.FAIL.getCode());
- configPubishResponse.setMessage(errorMsg);
- return configPubishResponse;
+ public static ConfigPublishResponse buildFailResponse(String errorMsg) {
+ ConfigPublishResponse configPublishResponse = new ConfigPublishResponse();
+ configPublishResponse.setResultCode(ResponseCode.FAIL.getCode());
+ configPublishResponse.setMessage(errorMsg);
+ return configPublishResponse;
}
}
diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigQueryResponse.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigQueryResponse.java
index ec0094f3f..3b67269c5 100644
--- a/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigQueryResponse.java
+++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigQueryResponse.java
@@ -57,7 +57,7 @@ public class ConfigQueryResponse extends Response {
*
* @param errorCode errorCode.
* @param message message.
- * @return
+ * @return response.
*/
public static ConfigQueryResponse buildFailResponse(int errorCode, String message) {
ConfigQueryResponse response = new ConfigQueryResponse();
@@ -66,10 +66,10 @@ public class ConfigQueryResponse extends Response {
}
/**
- * Buidl success resposne.
+ * Build success response.
*
* @param content content.
- * @return
+ * @return response.
*/
public static ConfigQueryResponse buildSuccessResponse(String content) {
ConfigQueryResponse response = new ConfigQueryResponse();
diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigRemoveResponse.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigRemoveResponse.java
index 9d10199dd..7edf4108d 100644
--- a/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigRemoveResponse.java
+++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/response/ConfigRemoveResponse.java
@@ -32,18 +32,18 @@ public class ConfigRemoveResponse extends Response {
}
/**
- * Buidl success resposne.
+ * Build success response.
*
- * @return
+ * @return response.
*/
public static ConfigRemoveResponse buildSuccessResponse() {
return new ConfigRemoveResponse();
}
/**
- * Buidl fail resposne.
+ * Build fail response.
*
- * @return
+ * @return response.
*/
public static ConfigRemoveResponse buildFailResponse(String errorMsg) {
ConfigRemoveResponse removeResponse = new ConfigRemoveResponse();
diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java
index 28f0276ef..d4059f873 100644
--- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java
+++ b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java
@@ -107,11 +107,16 @@ public class NacosException extends Exception {
*/
public static final int CLIENT_INVALID_PARAM = -400;
+ /**
+ * invalid param(参数错误).
+ */
+ public static final int CLIENT_DISCONNECT = -401;
+
/**
* over client threshold(超过server端的限流阈值).
*/
public static final int CLIENT_OVER_THRESHOLD = -503;
-
+
/*
* server error code.
* 400 403 throw exception to user
@@ -160,4 +165,6 @@ public class NacosException extends Exception {
* ome exceptions that occurred when the use the Nacos RestTemplate and Nacos AsyncRestTemplate.
*/
public static final int HTTP_CLIENT_ERROR_CODE = -500;
+
+
}
diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/DefaultRequestFuture.java b/api/src/main/java/com/alibaba/nacos/api/remote/DefaultRequestFuture.java
index cf757c53d..807a3c945 100644
--- a/api/src/main/java/com/alibaba/nacos/api/remote/DefaultRequestFuture.java
+++ b/api/src/main/java/com/alibaba/nacos/api/remote/DefaultRequestFuture.java
@@ -82,7 +82,7 @@ public class DefaultRequestFuture implements RequestFuture {
this.requestId = requestId;
this.connectionId = connectionId;
if (requestCallBack != null) {
- this.timeoutFuture = RpcScheduledExecutor.TIMEOUT_SHEDULER
+ this.timeoutFuture = RpcScheduledExecutor.TIMEOUT_SCHEDULER
.schedule(new TimeoutHandler(), requestCallBack.getTimeout(), TimeUnit.MILLISECONDS);
}
this.timeoutInnerTrigger = timeoutInnerTrigger;
diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/RequestCallBack.java b/api/src/main/java/com/alibaba/nacos/api/remote/RequestCallBack.java
index 880a53d21..192c5df36 100644
--- a/api/src/main/java/com/alibaba/nacos/api/remote/RequestCallBack.java
+++ b/api/src/main/java/com/alibaba/nacos/api/remote/RequestCallBack.java
@@ -31,14 +31,14 @@ public interface RequestCallBack {
/**
* get executor on callback.
*
- * @return
+ * @return executor.
*/
public Executor getExecutor();
/**
* get timeout mills.
*
- * @return
+ * @return timeouts.
*/
public long getTimeout();
diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/RequestFuture.java b/api/src/main/java/com/alibaba/nacos/api/remote/RequestFuture.java
index 31e5c5685..0f48bc348 100644
--- a/api/src/main/java/com/alibaba/nacos/api/remote/RequestFuture.java
+++ b/api/src/main/java/com/alibaba/nacos/api/remote/RequestFuture.java
@@ -31,7 +31,7 @@ public interface RequestFuture {
/**
* check that it is done or not..
- * @return
+ * @return is done .
*/
boolean isDone();
diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/Requester.java b/api/src/main/java/com/alibaba/nacos/api/remote/Requester.java
index b35abcfba..01dbe3421 100644
--- a/api/src/main/java/com/alibaba/nacos/api/remote/Requester.java
+++ b/api/src/main/java/com/alibaba/nacos/api/remote/Requester.java
@@ -87,7 +87,7 @@ public interface Requester {
/**
* check this requester is busy.
*
- * @return
+ * @return busy or not.
*/
public boolean isBusy();
}
diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/RpcScheduledExecutor.java b/api/src/main/java/com/alibaba/nacos/api/remote/RpcScheduledExecutor.java
index 2c66c30da..a1baa1c0d 100644
--- a/api/src/main/java/com/alibaba/nacos/api/remote/RpcScheduledExecutor.java
+++ b/api/src/main/java/com/alibaba/nacos/api/remote/RpcScheduledExecutor.java
@@ -27,17 +27,11 @@ import java.util.concurrent.ThreadFactory;
*/
public class RpcScheduledExecutor extends ScheduledThreadPoolExecutor {
- public static final RpcScheduledExecutor TIMEOUT_SHEDULER = new RpcScheduledExecutor(1,
+ public static final RpcScheduledExecutor TIMEOUT_SCHEDULER = new RpcScheduledExecutor(0,
"com.alibaba.nacos.remote.TimerScheduler");
-
- /**
- * executor to execute future request.
- */
- public static final RpcScheduledExecutor AYNS_REQUEST_EXECUTOR = new RpcScheduledExecutor(
- Runtime.getRuntime().availableProcessors(), "com.alibaba.nacos.remote.RpcRequestExecutor");
-
+
public static final RpcScheduledExecutor COMMON_SERVER_EXECUTOR = new RpcScheduledExecutor(
- Runtime.getRuntime().availableProcessors(), "com.alibaba.nacos.remote.ServerCommonScheduler");
+ 0, "com.alibaba.nacos.remote.ServerCommonScheduler");
public RpcScheduledExecutor(int corePoolSize, final String threadName) {
super(corePoolSize, new ThreadFactory() {
diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/request/Request.java b/api/src/main/java/com/alibaba/nacos/api/remote/request/Request.java
index df4861da7..0d16817fd 100644
--- a/api/src/main/java/com/alibaba/nacos/api/remote/request/Request.java
+++ b/api/src/main/java/com/alibaba/nacos/api/remote/request/Request.java
@@ -115,6 +115,6 @@ public abstract class Request {
@Override
public String toString() {
- return "Request{" + "headers=" + headers + ", requestId='" + requestId + '\'' + '}';
+ return this.getClass().getSimpleName() + "{" + "headers=" + headers + ", requestId='" + requestId + '\'' + '}';
}
}
diff --git a/api/src/main/java/com/alibaba/nacos/api/remote/response/Response.java b/api/src/main/java/com/alibaba/nacos/api/remote/response/Response.java
index e337e366e..969b2abee 100644
--- a/api/src/main/java/com/alibaba/nacos/api/remote/response/Response.java
+++ b/api/src/main/java/com/alibaba/nacos/api/remote/response/Response.java
@@ -52,9 +52,9 @@ public abstract class Response {
}
/**
- * Check Response is Successd.
+ * Check Response is Successed.
*
- * @return
+ * @return success or not.
*/
public boolean isSuccess() {
return this.resultCode == ResponseCode.SUCCESS.getCode();
diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java
index 13cba5ca9..4e2c5871b 100644
--- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java
+++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java
@@ -51,14 +51,6 @@ public class CacheData {
this.isInitializing = isInitializing;
}
- public boolean isListenSuccess() {
- return isListenSuccess;
- }
-
- public void setListenSuccess(boolean listenSuccess) {
- isListenSuccess = listenSuccess;
- }
-
public String getMd5() {
return md5;
}
@@ -285,6 +277,21 @@ public class CacheData {
return content;
}
+ /**
+ * 1.first add listener.default is false;need to check.
+ * 2.receive config change notify,set false;need to check.
+ * 3.last listener is remove,set to false;need to check
+ *
+ * @return
+ */
+ public boolean isSync() {
+ return isSync;
+ }
+
+ public void setSync(boolean sync) {
+ isSync = sync;
+ }
+
public CacheData(ConfigFilterChainManager configFilterChainManager, String name, String dataId, String group) {
if (null == dataId || null == group) {
throw new IllegalArgumentException("dataId=" + dataId + ", group=" + group);
@@ -350,7 +357,10 @@ public class CacheData {
private volatile boolean isInitializing = true;
- private volatile boolean isListenSuccess = false;
+ /**
+ * if is sync with the server.
+ */
+ private volatile boolean isSync = false;
private String type;
diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java
index 7e1fd9379..8c96b42b2 100644
--- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java
+++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java
@@ -27,13 +27,12 @@ import com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest;
import com.alibaba.nacos.api.config.remote.request.ConfigRemoveRequest;
import com.alibaba.nacos.api.config.remote.response.ConfigChangeBatchListenResponse;
import com.alibaba.nacos.api.config.remote.response.ConfigChangeNotifyResponse;
-import com.alibaba.nacos.api.config.remote.response.ConfigPubishResponse;
+import com.alibaba.nacos.api.config.remote.response.ConfigPublishResponse;
import com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse;
import com.alibaba.nacos.api.config.remote.response.ConfigRemoveResponse;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.remote.RemoteConstants;
import com.alibaba.nacos.api.remote.request.Request;
-import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.client.config.common.GroupKey;
import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager;
@@ -58,7 +57,6 @@ import com.alibaba.nacos.common.remote.client.ConnectionEventListener;
import com.alibaba.nacos.common.remote.client.RpcClient;
import com.alibaba.nacos.common.remote.client.RpcClientFactory;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
-import com.alibaba.nacos.common.remote.client.ServerRequestHandler;
import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.common.utils.StringUtils;
@@ -116,29 +114,11 @@ public class ClientWorker implements Closeable {
for (Listener listener : listeners) {
cache.addListener(listener);
}
- if (!cache.isListenSuccess()) {
+ if (!cache.isSync()) {
agent.notifyListenConfig();
}
}
- /**
- * Remove listener.
- *
- * @param dataId dataId of data
- * @param group group of data
- * @param listener listener
- */
- public void removeListener(String dataId, String group, Listener listener) {
- group = null2defaultGroup(group);
- CacheData cache = getCache(dataId, group);
- if (null != cache) {
- cache.removeListener(listener);
- if (cache.getListeners().isEmpty()) {
- agent.removeCache(dataId, group);
- }
- }
- }
-
/**
* Add listeners for tenant.
*
@@ -152,12 +132,15 @@ public class ClientWorker implements Closeable {
group = null2defaultGroup(group);
String tenant = agent.getTenant();
CacheData cache = addCacheDataIfAbsent(dataId, group, tenant);
- for (Listener listener : listeners) {
- cache.addListener(listener);
- }
- if (!cache.isListenSuccess()) {
- agent.notifyListenConfig();
+ synchronized (cache) {
+ for (Listener listener : listeners) {
+ cache.addListener(listener);
+ }
+ if (!cache.isSync()) {
+ agent.notifyListenConfig();
+ }
}
+
}
/**
@@ -174,13 +157,38 @@ public class ClientWorker implements Closeable {
group = null2defaultGroup(group);
String tenant = agent.getTenant();
CacheData cache = addCacheDataIfAbsent(dataId, group, tenant);
- cache.setContent(content);
- for (Listener listener : listeners) {
- cache.addListener(listener);
+ synchronized (cache) {
+ cache.setContent(content);
+ for (Listener listener : listeners) {
+ cache.addListener(listener);
+ }
+ // if current cache is already at listening status,do not notify.
+ if (!cache.isSync()) {
+ agent.notifyListenConfig();
+ }
}
- // if current cache is already at listening status,do not notify.
- if (!cache.isListenSuccess()) {
- agent.notifyListenConfig();
+
+ }
+
+ /**
+ * Remove listener.
+ *
+ * @param dataId dataId of data
+ * @param group group of data
+ * @param listener listener
+ */
+ public void removeListener(String dataId, String group, Listener listener) {
+ group = null2defaultGroup(group);
+ CacheData cache = getCache(dataId, group);
+ if (null != cache) {
+ synchronized (cache) {
+ cache.removeListener(listener);
+ if (cache.getListeners().isEmpty()) {
+ cache.setSync(false);
+ agent.removeCache(dataId, group);
+ }
+ }
+
}
}
@@ -198,6 +206,7 @@ public class ClientWorker implements Closeable {
if (null != cache) {
cache.removeListener(listener);
if (cache.getListeners().isEmpty()) {
+ cache.setSync(false);
agent.removeCache(dataId, group);
}
}
@@ -415,39 +424,29 @@ public class ClientWorker implements Closeable {
this.configFilterChainManager = configFilterChainManager;
init(properties);
-
+
ServerListManager serverListManager = new ServerListManager(properties);
serverListManager.start();
-
+
if (ParamUtils.useHttpSwitch()) {
agent = new ConfigHttpTransportClient(properties, serverListManager);
} else {
agent = new ConfigRpcTransportClient(properties, serverListManager);
}
-
- this.executor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
- t.setName("com.alibaba.nacos.client.Worker." + agent.getName());
- t.setDaemon(true);
- return t;
- }
- });
- this.executorService = Executors
+ ScheduledExecutorService executorService = Executors
.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
- t.setName("com.alibaba.nacos.client.Worker.longPolling." + agent.getName());
+ t.setName("com.alibaba.nacos.client.Worker_" + agent.getName());
t.setDaemon(true);
return t;
}
});
agent.setExecutor(executorService);
agent.start();
-
+
}
private void refreshContentAndCheck(String groupKey, boolean notify) {
@@ -464,6 +463,11 @@ public class ClientWorker implements Closeable {
if (null != ct[1]) {
cacheData.setType(ct[1]);
}
+ if (notify) {
+ LOGGER.info("[{}] [data-received] dataId={}, group={}, tenant={}, md5={}, content={}, type={}",
+ agent.getName(), cacheData.dataId, cacheData.group, cacheData.tenant, cacheData.getMd5(),
+ ContentUtils.truncateContent(ct[0]), ct[1]);
+ }
cacheData.checkListenerMd5();
} catch (Exception e) {
LOGGER.error("refresh content and check md5 fail ,dataid={},group={},tenant={} ", cacheData.dataId,
@@ -487,8 +491,7 @@ public class ClientWorker implements Closeable {
public void shutdown() throws NacosException {
String className = this.getClass().getName();
LOGGER.info("{} do shutdown begin", className);
- ThreadUtils.shutdownThreadPool(executorService, LOGGER);
- ThreadUtils.shutdownThreadPool(executor, LOGGER);
+ ThreadUtils.shutdownThreadPool(agent.executor, LOGGER);
LOGGER.info("{} do shutdown stop", className);
}
@@ -500,10 +503,6 @@ public class ClientWorker implements Closeable {
this.isHealthServer = isHealthServer;
}
- final ScheduledExecutorService executor;
-
- final ScheduledExecutorService executorService;
-
/**
* groupKey -> cacheData.
*/
@@ -529,127 +528,124 @@ public class ClientWorker implements Closeable {
private BlockingQueue