From 3d8b2c0b173f53b1d9996447b75bc8d00bc07909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Mon, 24 May 2021 20:38:34 +0800 Subject: [PATCH] For #5831, Remove the sets in connection event lister (#5832) --- .../NamingGrpcConnectionEventListener.java | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcConnectionEventListener.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcConnectionEventListener.java index 08fd6ee65..b112b1f64 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcConnectionEventListener.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcConnectionEventListener.java @@ -42,7 +42,7 @@ public class NamingGrpcConnectionEventListener implements ConnectionEventListene private final NamingGrpcClientProxy clientProxy; - private final ConcurrentMap> registeredInstanceCached = new ConcurrentHashMap>(); + private final ConcurrentMap registeredInstanceCached = new ConcurrentHashMap<>(); private final Set subscribes = new ConcurrentHashSet(); @@ -70,21 +70,19 @@ public class NamingGrpcConnectionEventListener implements ConnectionEventListene private void redoRegisterEachService() { LogUtils.NAMING_LOGGER.info("Grpc re-connect, redo register services"); - for (Map.Entry> each : registeredInstanceCached.entrySet()) { + for (Map.Entry each : registeredInstanceCached.entrySet()) { String serviceName = NamingUtils.getServiceName(each.getKey()); String groupName = NamingUtils.getGroupName(each.getKey()); redoRegisterEachInstance(serviceName, groupName, each.getValue()); } } - private void redoRegisterEachInstance(String serviceName, String groupName, Set instances) { - for (Instance each : instances) { - try { - clientProxy.registerService(serviceName, groupName, each); - } catch (NacosException e) { - LogUtils.NAMING_LOGGER - .warn(String.format("redo register for service %s@@%s failed", groupName, serviceName), e); - } + private void redoRegisterEachInstance(String serviceName, String groupName, Instance instance) { + try { + clientProxy.registerService(serviceName, groupName, instance); + } catch (NacosException e) { + LogUtils.NAMING_LOGGER.warn(String + .format("redo register for service %s@@%s, %s failed", groupName, serviceName, instance.toString()), e); } } @@ -102,8 +100,7 @@ public class NamingGrpcConnectionEventListener implements ConnectionEventListene */ public void cacheInstanceForRedo(String serviceName, String groupName, Instance instance) { String key = NamingUtils.getGroupedName(serviceName, groupName); - registeredInstanceCached.putIfAbsent(key, new ConcurrentHashSet()); - registeredInstanceCached.get(key).add(instance); + registeredInstanceCached.put(key, instance); } /** @@ -115,10 +112,7 @@ public class NamingGrpcConnectionEventListener implements ConnectionEventListene */ public void removeInstanceForRedo(String serviceName, String groupName, Instance instance) { String key = NamingUtils.getGroupedName(serviceName, groupName); - Set instances = registeredInstanceCached.get(key); - if (null != instances) { - instances.remove(instance); - } + registeredInstanceCached.remove(key); } public void cacheSubscriberForRedo(String fullServiceName, String cluster) {