Skip to content

Commit

Permalink
improve NacosNamingServiceWrapper performance (#11665)
Browse files Browse the repository at this point in the history
Signed-off-by: mantuliu <240951888@qq.com>
  • Loading branch information
sourcelliu authored Mar 8, 2023
1 parent d3d8c54 commit d0b98b3
Showing 1 changed file with 7 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public class NacosNamingServiceWrapper {

private final ConcurrentMap<InstanceId, InstancesInfo> registerStatus = new ConcurrentHashMap<>();
private final ConcurrentMap<SubscribeInfo, NamingService> subscribeStatus = new ConcurrentHashMap<>();
private final Lock mapLock = new ReentrantLock();

public NacosNamingServiceWrapper(NacosConnectionManager nacosConnectionManager, int retryTimes, int sleepMsBetweenRetries) {
this.nacosConnectionManager = nacosConnectionManager;
Expand Down Expand Up @@ -108,13 +107,7 @@ public List<Instance> 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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit d0b98b3

Please sign in to comment.