From 6fad9de2bba445aa9409d0c50e71e9e9da67f3a0 Mon Sep 17 00:00:00 2001 From: KomachiSion Date: Fri, 14 Oct 2022 11:55:42 +0800 Subject: [PATCH] Add switch for naming async query. --- .../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 7cf8c8dc52b..8257511be86 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 ddb0d619ffb..386feeecd8f 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 ServiceInfoUpdateService(Properties properties, ServiceInfoHolder service 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 @@ private int initPollingThreadCount(Properties properties) { * @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 void run() { // 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 void run() { } } + 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) {