From 9f2f965654c2690fd569bcfdbb879e4c994efa72 Mon Sep 17 00:00:00 2001 From: mantuliu <240951888@qq.com> Date: Mon, 27 Feb 2023 00:09:42 +0800 Subject: [PATCH] improve NacosNamingServiceWrapper performance Signed-off-by: mantuliu <240951888@qq.com> --- .../nacos/NacosNamingServiceWrapper.java | 44 +++---------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosNamingServiceWrapper.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosNamingServiceWrapper.java index 3bd6116d159..2b09d8f6755 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosNamingServiceWrapper.java +++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosNamingServiceWrapper.java @@ -60,7 +60,6 @@ public class NacosNamingServiceWrapper { private final ConcurrentMap registerStatus = new ConcurrentHashMap<>(); private final ConcurrentMap subscribeStatus = new ConcurrentHashMap<>(); - private final Lock mapLock = new ReentrantLock(); public NacosNamingServiceWrapper(NacosConnectionManager nacosConnectionManager, int retryTimes, int sleepMsBetweenRetries) { this.nacosConnectionManager = nacosConnectionManager; @@ -108,13 +107,7 @@ public List getAllInstances(String serviceName, String group) throws N public void registerInstance(String serviceName, String group, Instance instance) throws NacosException { String nacosServiceName = handleInnerSymbol(serviceName); - InstancesInfo instancesInfo; - try { - mapLock.lock(); - instancesInfo = ConcurrentHashMapUtils.computeIfAbsent(registerStatus, new InstanceId(nacosServiceName, group), id -> new InstancesInfo()); - } finally { - mapLock.unlock(); - } + InstancesInfo instancesInfo = ConcurrentHashMapUtils.computeIfAbsent(registerStatus, new InstanceId(nacosServiceName, group), id -> new InstancesInfo()); try { instancesInfo.lock(); @@ -176,13 +169,7 @@ public void registerInstance(String serviceName, String group, Instance instance public void updateInstance(String serviceName, String group, Instance oldInstance, Instance newInstance) throws NacosException { String nacosServiceName = handleInnerSymbol(serviceName); - InstancesInfo instancesInfo; - try { - mapLock.lock(); - instancesInfo = ConcurrentHashMapUtils.computeIfAbsent(registerStatus, new InstanceId(nacosServiceName, group), id -> new InstancesInfo()); - } finally { - mapLock.unlock(); - } + InstancesInfo instancesInfo = ConcurrentHashMapUtils.computeIfAbsent(registerStatus, new InstanceId(nacosServiceName, group), id -> new InstancesInfo()); try { instancesInfo.lock(); @@ -222,13 +209,7 @@ public void updateInstance(String serviceName, String group, Instance oldInstanc public void deregisterInstance(String serviceName, String group, String ip, int port) throws NacosException { String nacosServiceName = handleInnerSymbol(serviceName); - InstancesInfo instancesInfo; - try { - mapLock.lock(); - instancesInfo = ConcurrentHashMapUtils.computeIfAbsent(registerStatus, new InstanceId(nacosServiceName, group), id -> new InstancesInfo()); - } finally { - mapLock.unlock(); - } + InstancesInfo instancesInfo = ConcurrentHashMapUtils.computeIfAbsent(registerStatus, new InstanceId(nacosServiceName, group), id -> new InstancesInfo()); try { instancesInfo.lock(); @@ -249,13 +230,7 @@ public void deregisterInstance(String serviceName, String group, String ip, int public void deregisterInstance(String serviceName, String group, Instance instance) throws NacosException { String nacosServiceName = handleInnerSymbol(serviceName); - InstancesInfo instancesInfo; - try { - mapLock.lock(); - instancesInfo = ConcurrentHashMapUtils.computeIfAbsent(registerStatus, new InstanceId(nacosServiceName, group), id -> new InstancesInfo()); - } finally { - mapLock.unlock(); - } + InstancesInfo instancesInfo = ConcurrentHashMapUtils.computeIfAbsent(registerStatus, new InstanceId(nacosServiceName, group), id -> new InstancesInfo()); try { instancesInfo.lock(); @@ -269,14 +244,9 @@ public void deregisterInstance(String serviceName, String group, Instance instan InstanceInfo instanceInfo = optional.get(); instancesInfo.getInstances().remove(instanceInfo); - try { - mapLock.lock(); - if (instancesInfo.getInstances().isEmpty()) { - registerStatus.remove(new InstanceId(nacosServiceName, group)); - instancesInfo.setValid(false); - } - } finally { - mapLock.unlock(); + if (instancesInfo.getInstances().isEmpty()) { + registerStatus.remove(new InstanceId(nacosServiceName, group)); + instancesInfo.setValid(false); } // only one registered