From 0bfd75252b6948cbf531b6d9f8c07c4e46e9f51c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= Date: Fri, 14 Oct 2022 14:01:01 +0800 Subject: [PATCH] Add switch for naming async query. (#9325) --- .../alibaba/nacos/api/PropertyKeyConst.java | 2 ++ .../naming/core/ServiceInfoUpdateService.java | 32 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java b/api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java index 7cf8c8dc5..8257511be 100644 --- a/api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java +++ b/api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java @@ -75,6 +75,8 @@ public class PropertyKeyConst { public static final String NAMING_PUSH_EMPTY_PROTECTION = "namingPushEmptyProtection"; + public static final String NAMING_ASYNC_QUERY_SUBSCRIBE_SERVICE = "namingAsyncQuerySubscribeService"; + public static final String PUSH_RECEIVER_UDP_PORT = "push.receiver.udp.port"; /** diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateService.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateService.java index ddb0d619f..386feeecd 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateService.java @@ -61,8 +61,11 @@ public class ServiceInfoUpdateService implements Closeable { private final InstancesChangeNotifier changeNotifier; + private final boolean asyncQuerySubscribeService; + public ServiceInfoUpdateService(Properties properties, ServiceInfoHolder serviceInfoHolder, NamingClientProxy namingClientProxy, InstancesChangeNotifier changeNotifier) { + this.asyncQuerySubscribeService = isAsyncQueryForSubscribeService(properties); this.executor = new ScheduledThreadPoolExecutor(initPollingThreadCount(properties), new NameThreadFactory("com.alibaba.nacos.client.naming.updater")); this.serviceInfoHolder = serviceInfoHolder; @@ -70,6 +73,13 @@ public class ServiceInfoUpdateService implements Closeable { this.changeNotifier = changeNotifier; } + private boolean isAsyncQueryForSubscribeService(Properties properties) { + if (properties == null || !properties.containsKey(PropertyKeyConst.NAMING_ASYNC_QUERY_SUBSCRIBE_SERVICE)) { + return true; + } + return ConvertUtils.toBoolean(properties.getProperty(PropertyKeyConst.NAMING_ASYNC_QUERY_SUBSCRIBE_SERVICE), true); + } + private int initPollingThreadCount(Properties properties) { if (properties == null) { return UtilAndComs.DEFAULT_POLLING_THREAD_COUNT; @@ -86,6 +96,9 @@ public class ServiceInfoUpdateService implements Closeable { * @param clusters clusters */ public void scheduleUpdateIfAbsent(String serviceName, String groupName, String clusters) { + if (!asyncQuerySubscribeService) { + return; + } String serviceKey = ServiceInfo.getKey(NamingUtils.getGroupedName(serviceName, groupName), clusters); if (futureMap.get(serviceKey) != null) { return; @@ -193,9 +206,10 @@ public class ServiceInfoUpdateService implements Closeable { // TODO multiple time can be configured. delayTime = serviceObj.getCacheMillis() * DEFAULT_UPDATE_CACHE_TIME_MULTIPLE; resetFailCount(); + } catch (NacosException e) { + handleNacosException(e); } catch (Throwable e) { - incFailCount(); - NAMING_LOGGER.warn("[NA] failed to update serviceName: {}", groupedServiceName, e); + handleUnknownException(e); } finally { if (!isCancel) { executor.schedule(this, Math.min(delayTime << failCount, DEFAULT_DELAY * 60), @@ -204,6 +218,20 @@ public class ServiceInfoUpdateService implements Closeable { } } + private void handleNacosException(NacosException e) { + incFailCount(); + int errorCode = e.getErrCode(); + if (NacosException.SERVER_ERROR == errorCode) { + handleUnknownException(e); + } + NAMING_LOGGER.warn("Can't update serviceName: {}, reason: {}", groupedServiceName, e.getErrMsg()); + } + + private void handleUnknownException(Throwable throwable) { + incFailCount(); + NAMING_LOGGER.warn("[NA] failed to update serviceName: {}", groupedServiceName, throwable); + } + private void incFailCount() { int limit = 6; if (failCount == limit) {