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

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

* Fix
This commit is contained in:
Qishang Zhong 2024-01-31 09:43:46 +08:00 committed by GitHub
parent 8f2150892a
commit 9d473ef60c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 11 deletions

View File

@ -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";
@ -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";

View File

@ -453,14 +453,24 @@ public class ClientWorker implements Closeable {
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);

View File

@ -25,7 +25,6 @@ import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder;
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;
@ -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;
@ -82,11 +83,13 @@ public class ServiceInfoUpdateService implements Closeable {
}
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);
}
/**

View File

@ -16,8 +16,6 @@
package com.alibaba.nacos.client.naming.utils;
import com.alibaba.nacos.common.utils.ThreadUtils;
/**
* Util and constants.
*
@ -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";