[ISSUE #11612] Unified use of NameThreadFactory to create thread pools. (#11613)

This commit is contained in:
Qishang Zhong 2024-01-17 15:47:19 +08:00 committed by GitHub
parent bdbc356ce4
commit 07168099db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 25 additions and 49 deletions

View File

@ -28,6 +28,7 @@ import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListene
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.TenantUtil;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.common.utils.NumberUtils;
@ -73,12 +74,9 @@ public class CacheData {
if (scheduledExecutor == null) {
synchronized (CacheData.class) {
if (scheduledExecutor == null) {
scheduledExecutor = new ScheduledThreadPoolExecutor(1, r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.notify.block.monitor");
t.setDaemon(true);
return t;
}, new ThreadPoolExecutor.DiscardPolicy());
scheduledExecutor = new ScheduledThreadPoolExecutor(1,
new NameThreadFactory("com.alibaba.nacos.client.notify.block.monitor"),
new ThreadPoolExecutor.DiscardPolicy());
scheduledExecutor.setRemoveOnCancelPolicy(true);
}
}

View File

@ -48,6 +48,7 @@ import com.alibaba.nacos.client.utils.EnvUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.TenantUtil;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
@ -454,12 +455,7 @@ public class ClientWorker implements Closeable {
agent = new ConfigRpcTransportClient(properties, serverListManager);
int count = ThreadUtils.getSuitableThreadCount(THREAD_MULTIPLE);
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(Math.max(count, MIN_THREAD_NUM),
r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.Worker");
t.setDaemon(true);
return t;
});
new NameThreadFactory("com.alibaba.nacos.client.Worker"));
agent.setExecutor(executorService);
agent.start();

View File

@ -24,6 +24,7 @@ import com.alibaba.nacos.client.utils.ContextPathUtil;
import com.alibaba.nacos.client.utils.EnvUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
@ -63,12 +64,8 @@ public class ServerListManager implements Closeable {
private final NacosRestTemplate nacosRestTemplate = ConfigHttpClientManager.getInstance().getNacosRestTemplate();
private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.ServerListManager");
t.setDaemon(true);
return t;
});
private final ScheduledExecutorService executorService =
new ScheduledThreadPoolExecutor(1, new NameThreadFactory("com.alibaba.nacos.client.ServerListManager"));
/**
* The name of the different environment.

View File

@ -32,6 +32,7 @@ import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy;
import com.alibaba.nacos.client.naming.utils.InitUtils;
import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.utils.ThreadUtils;
import java.util.Map;
@ -86,12 +87,8 @@ public class NacosNamingMaintainService implements NamingMaintainService {
}
private void initSecurityProxy(Properties properties) {
this.executorService = new ScheduledThreadPoolExecutor(1, r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.naming.maintainService.security");
t.setDaemon(true);
return t;
});
this.executorService = new ScheduledThreadPoolExecutor(1,
new NameThreadFactory("com.alibaba.nacos.client.naming.maintainService.security"));
this.securityProxy.login(properties);
this.executorService
.scheduleWithFixedDelay(() -> securityProxy.login(properties), 0, SECURITY_INFO_REFRESH_INTERVAL_MILLS,

View File

@ -20,6 +20,7 @@ import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder;
import com.alibaba.nacos.client.naming.event.InstancesChangeEvent;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
@ -70,12 +71,8 @@ public class FailoverReactor implements Closeable {
break;
}
// init executorService
this.executorService = new ScheduledThreadPoolExecutor(1, r -> {
Thread thread = new Thread(r);
thread.setDaemon(true);
thread.setName("com.alibaba.nacos.naming.failover");
return thread;
});
this.executorService = new ScheduledThreadPoolExecutor(1,
new NameThreadFactory("com.alibaba.nacos.naming.failover"));
this.init();
}

View File

@ -33,6 +33,7 @@ import com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy;
import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientManager;
import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy;
import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
@ -81,12 +82,8 @@ public class NamingClientProxyDelegate implements NamingClientProxy {
}
private void initSecurityProxy(NacosClientProperties properties) {
this.executorService = new ScheduledThreadPoolExecutor(1, r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.naming.security");
t.setDaemon(true);
return t;
});
this.executorService = new ScheduledThreadPoolExecutor(1,
new NameThreadFactory("com.alibaba.nacos.client.naming.security"));
final Properties nacosClientPropertiesView = properties.asProperties();
this.securityProxy.login(nacosClientPropertiesView);
this.executorService.scheduleWithFixedDelay(() -> securityProxy.login(nacosClientPropertiesView), 0,

View File

@ -30,6 +30,7 @@ import com.alibaba.nacos.api.remote.response.ClientDetectionResponse;
import com.alibaba.nacos.api.remote.response.ConnectResetResponse;
import com.alibaba.nacos.api.remote.response.ErrorResponse;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.packagescan.resource.DefaultResourceLoader;
import com.alibaba.nacos.common.packagescan.resource.ResourceLoader;
@ -242,12 +243,8 @@ public abstract class RpcClient implements Closeable {
return;
}
clientEventExecutor = new ScheduledThreadPoolExecutor(2, r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.remote.worker");
t.setDaemon(true);
return t;
});
clientEventExecutor = new ScheduledThreadPoolExecutor(2,
new NameThreadFactory("com.alibaba.nacos.client.remote.worker"));
// connection event consumer.
clientEventExecutor.submit(() -> {

View File

@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.service;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.constant.PropertiesConstant;
import com.alibaba.nacos.config.server.model.ConfigInfo;
@ -88,13 +89,9 @@ public class ConfigDetailService {
*/
private void initWorker() {
this.eventLinkedBlockingQueue = new LinkedBlockingQueue<>(maxCapacity);
clientEventExecutor = new ScheduledThreadPoolExecutor(maxThread, r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.config.search.worker");
t.setDaemon(true);
return t;
});
clientEventExecutor = new ScheduledThreadPoolExecutor(maxThread,
new NameThreadFactory("com.alibaba.nacos.config.search.worker"));
for (int i = 0; i < maxThread; i++) {
clientEventExecutor.submit(() -> {