Skip to content

Commit

Permalink
[ISSUE #11618] Add the config of max thread count for client worker &…
Browse files Browse the repository at this point in the history
… naming polling (#11693)

* [ISSUE #11618] Add the config of max thread count for client worker & naming polling

* Fix
  • Loading branch information
zhongqishang authored Jan 31, 2024
1 parent 8f21508 commit 9d473ef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
6 changes: 6 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class PropertyKeyConst {

public static final String CONFIG_RETRY_TIME = "configRetryTime";

public static final String CLIENT_WORKER_MAX_THREAD_COUNT = "clientWorkerMaxThreadCount";

public static final String CLIENT_WORKER_THREAD_COUNT = "clientWorkerThreadCount";

public static final String MAX_RETRY = "maxRetry";

public static final String ENABLE_REMOTE_SYNC_CONFIG = "enableRemoteSyncConfig";
Expand All @@ -69,6 +73,8 @@ public class PropertyKeyConst {

public static final String NAMING_CLIENT_BEAT_THREAD_COUNT = "namingClientBeatThreadCount";

public static final String NAMING_POLLING_MAX_THREAD_COUNT = "namingPollingMaxThreadCount";

public static final String NAMING_POLLING_THREAD_COUNT = "namingPollingThreadCount";

public static final String NAMING_REQUEST_DOMAIN_RETRY_COUNT = "namingRequestDomainMaxRetryCount";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,24 @@ public ClientWorker(final ConfigFilterChainManager configFilterChainManager, Ser
init(properties);

agent = new ConfigRpcTransportClient(properties, serverListManager);
int count = ThreadUtils.getSuitableThreadCount(THREAD_MULTIPLE);
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(Math.max(count, MIN_THREAD_NUM),
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(
initWorkerThreadCount(properties),
new NameThreadFactory("com.alibaba.nacos.client.Worker"));
agent.setExecutor(executorService);
agent.start();

}

private int initWorkerThreadCount(NacosClientProperties properties) {
int count = ThreadUtils.getSuitableThreadCount(THREAD_MULTIPLE);
if (properties == null) {
return count;
}
count = Math.min(count, properties.getInteger(PropertyKeyConst.CLIENT_WORKER_MAX_THREAD_COUNT, count));
count = Math.max(count, MIN_THREAD_NUM);
return properties.getInteger(PropertyKeyConst.CLIENT_WORKER_THREAD_COUNT, count);
}

private void refreshContentAndCheck(String groupKey, boolean notify) {
if (cacheMap.get() != null && cacheMap.get().containsKey(groupKey)) {
CacheData cache = cacheMap.get().get(groupKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.alibaba.nacos.client.naming.event.InstancesChangeNotifier;
import com.alibaba.nacos.client.naming.remote.NamingClientProxy;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.utils.ConvertUtils;
Expand All @@ -51,6 +50,8 @@ public class ServiceInfoUpdateService implements Closeable {

private static final int DEFAULT_UPDATE_CACHE_TIME_MULTIPLE = 6;

private static final int MIN_THREAD_NUM = 1;

private final Map<String, ScheduledFuture<?>> futureMap = new HashMap<>();

private final ServiceInfoHolder serviceInfoHolder;
Expand Down Expand Up @@ -82,11 +83,13 @@ private boolean isAsyncQueryForSubscribeService(NacosClientProperties properties
}

private int initPollingThreadCount(NacosClientProperties properties) {
int count = ThreadUtils.getSuitableThreadCount(1) > 1 ? ThreadUtils.getSuitableThreadCount(1) / 2 : 1;
if (properties == null) {
return UtilAndComs.DEFAULT_POLLING_THREAD_COUNT;
return count;
}
return ConvertUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT),
UtilAndComs.DEFAULT_POLLING_THREAD_COUNT);
count = Math.min(properties.getInteger(PropertyKeyConst.NAMING_POLLING_MAX_THREAD_COUNT, count), count);
count = Math.max(count, MIN_THREAD_NUM);
return properties.getInteger(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT, count);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.alibaba.nacos.client.naming.utils;

import com.alibaba.nacos.common.utils.ThreadUtils;

/**
* Util and constants.
*
Expand Down Expand Up @@ -49,9 +47,6 @@ public class UtilAndComs {
@Deprecated
public static final String NACOS_NAMING_LOG_LEVEL = "com.alibaba.nacos.naming.log.level";

public static final int DEFAULT_POLLING_THREAD_COUNT =
ThreadUtils.getSuitableThreadCount(1) > 1 ? ThreadUtils.getSuitableThreadCount(1) / 2 : 1;

public static final String ENV_CONFIGS = "00-00---000-ENV_CONFIGS-000---00-00";

public static final String VIP_CLIENT_FILE = "vipclient.properties";
Expand Down

0 comments on commit 9d473ef

Please sign in to comment.