You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2019/12/11 12:24:14 UTC
[dubbo] branch master updated: use Map.computeIfAbsent to simplify
the code and fix potential concurrent bug (#5380)
This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 7987100 use Map.computeIfAbsent to simplify the code and fix potential concurrent bug (#5380)
7987100 is described below
commit 7987100cad797ae82394ae88c87bac43a93ccf40
Author: Hansanshi <37...@users.noreply.github.com>
AuthorDate: Wed Dec 11 20:24:00 2019 +0800
use Map.computeIfAbsent to simplify the code and fix potential concurrent bug (#5380)
---
.../cluster/loadbalance/RoundRobinLoadBalance.java | 6 +---
.../common/store/support/SimpleDataStore.java | 6 +---
.../org/apache/dubbo/common/utils/PojoUtils.java | 7 +---
.../dubbo/config/utils/ReferenceConfigCache.java | 7 +---
.../config/spring/AbstractRegistryService.java | 14 ++------
.../dubbo/config/spring/SimpleRegistryService.java | 12 ++-----
.../dubbo/monitor/support/MonitorFilter.java | 7 +---
.../apache/dubbo/monitor/dubbo/DubboMonitor.java | 6 +---
.../registry/dubbo/AbstractRegistryService.java | 14 ++------
.../registry/dubbo/SimpleRegistryService.java | 12 ++-----
.../registry/multicast/MulticastRegistry.java | 6 +---
.../registry/zookeeper/ZookeeperRegistry.java | 40 +++++++---------------
.../remoting/etcd/support/AbstractEtcdClient.java | 19 ++--------
.../zookeeper/support/AbstractZookeeperClient.java | 24 +++----------
.../main/java/org/apache/dubbo/rpc/RpcStatus.java | 20 ++---------
.../rpc/protocol/dubbo/filter/TraceFilter.java | 6 +---
16 files changed, 40 insertions(+), 166 deletions(-)
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java
index 001c06d..d580b46 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java
@@ -85,11 +85,7 @@ public class RoundRobinLoadBalance extends AbstractLoadBalance {
@Override
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
- ConcurrentMap<String, WeightedRoundRobin> map = methodWeightMap.get(key);
- if (map == null) {
- methodWeightMap.putIfAbsent(key, new ConcurrentHashMap<String, WeightedRoundRobin>());
- map = methodWeightMap.get(key);
- }
+ ConcurrentMap<String, WeightedRoundRobin> map = methodWeightMap.computeIfAbsent(key, k -> new ConcurrentHashMap<>());
int totalWeight = 0;
long maxCurrent = Long.MIN_VALUE;
long now = System.currentTimeMillis();
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/store/support/SimpleDataStore.java b/dubbo-common/src/main/java/org/apache/dubbo/common/store/support/SimpleDataStore.java
index d60cc5d..e9709b0 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/store/support/SimpleDataStore.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/store/support/SimpleDataStore.java
@@ -50,11 +50,7 @@ public class SimpleDataStore implements DataStore {
@Override
public void put(String componentName, String key, Object value) {
- Map<String, Object> componentData = data.get(componentName);
- if (null == componentData) {
- data.putIfAbsent(componentName, new ConcurrentHashMap<String, Object>());
- componentData = data.get(componentName);
- }
+ Map<String, Object> componentData = data.computeIfAbsent(componentName, k -> new ConcurrentHashMap<>());
componentData.put(key, value);
}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java
index d167afe..0b1a3a8 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java
@@ -608,12 +608,7 @@ public class PojoUtils {
}
}
if (result != null) {
- ConcurrentMap<String, Field> fields = CLASS_FIELD_CACHE.get(cls);
- if (fields == null) {
- fields = new ConcurrentHashMap<String, Field>();
- CLASS_FIELD_CACHE.putIfAbsent(cls, fields);
- }
- fields = CLASS_FIELD_CACHE.get(cls);
+ ConcurrentMap<String, Field> fields = CLASS_FIELD_CACHE.computeIfAbsent(cls, k -> new ConcurrentHashMap<>());
fields.putIfAbsent(fieldName, result);
}
return result;
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java
index 173e929..e29b587 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java
@@ -99,12 +99,7 @@ public class ReferenceConfigCache {
* Create cache if not existed yet.
*/
public static ReferenceConfigCache getCache(String name, KeyGenerator keyGenerator) {
- ReferenceConfigCache cache = CACHE_HOLDER.get(name);
- if (cache != null) {
- return cache;
- }
- CACHE_HOLDER.putIfAbsent(name, new ReferenceConfigCache(name, keyGenerator));
- return CACHE_HOLDER.get(name);
+ return CACHE_HOLDER.computeIfAbsent(name, k -> new ReferenceConfigCache(k, keyGenerator));
}
@SuppressWarnings("unchecked")
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/AbstractRegistryService.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/AbstractRegistryService.java
index 41f8e29..c664601 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/AbstractRegistryService.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/AbstractRegistryService.java
@@ -100,11 +100,7 @@ public abstract class AbstractRegistryService implements RegistryService {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
- List<URL> urls = registered.get(service);
- if (urls == null) {
- registered.putIfAbsent(service, new CopyOnWriteArrayList<URL>());
- urls = registered.get(service);
- }
+ List<URL> urls = registered.computeIfAbsent(service, k -> new CopyOnWriteArrayList<>());
if (!urls.contains(url)) {
urls.add(url);
}
@@ -164,12 +160,8 @@ public abstract class AbstractRegistryService implements RegistryService {
if (listener == null) {
return;
}
- List<NotifyListener> listeners = notifyListeners.get(service);
- if (listeners == null) {
- notifyListeners.putIfAbsent(service, new CopyOnWriteArrayList<NotifyListener>());
- listeners = notifyListeners.get(service);
- }
- if (listeners != null && !listeners.contains(listener)) {
+ List<NotifyListener> listeners = notifyListeners.computeIfAbsent(service, k -> new CopyOnWriteArrayList<>());
+ if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryService.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryService.java
index e073e41..c9b7ac7 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryService.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/SimpleRegistryService.java
@@ -45,11 +45,7 @@ public class SimpleRegistryService extends AbstractRegistryService {
public void register(String service, URL url) {
super.register(service, url);
String client = RpcContext.getContext().getRemoteAddressString();
- Map<String, URL> urls = remoteRegistered.get(client);
- if (urls == null) {
- remoteRegistered.putIfAbsent(client, new ConcurrentHashMap<String, URL>());
- urls = remoteRegistered.get(client);
- }
+ Map<String, URL> urls = remoteRegistered.computeIfAbsent(client, k -> new ConcurrentHashMap<>());
urls.put(service, url);
notify(service, getRegistered().get(service));
}
@@ -88,11 +84,7 @@ public class SimpleRegistryService extends AbstractRegistryService {
}
super.subscribe(service, url, listener);
- Map<String, NotifyListener> listeners = remoteListeners.get(client);
- if (listeners == null) {
- remoteListeners.putIfAbsent(client, new ConcurrentHashMap<String, NotifyListener>());
- listeners = remoteListeners.get(client);
- }
+ Map<String, NotifyListener> listeners = remoteListeners.computeIfAbsent(client, k -> new ConcurrentHashMap<>());
listeners.put(service, listener);
urls = getRegistered().get(service);
if (urls != null && urls.size() > 0) {
diff --git a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MonitorFilter.java b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MonitorFilter.java
index 7b1fc14..b358e3d 100644
--- a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MonitorFilter.java
+++ b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MonitorFilter.java
@@ -92,12 +92,7 @@ public class MonitorFilter implements Filter, Filter.Listener {
// concurrent counter
private AtomicInteger getConcurrent(Invoker<?> invoker, Invocation invocation) {
String key = invoker.getInterface().getName() + "." + invocation.getMethodName();
- AtomicInteger concurrent = concurrents.get(key);
- if (concurrent == null) {
- concurrents.putIfAbsent(key, new AtomicInteger());
- concurrent = concurrents.get(key);
- }
- return concurrent;
+ return concurrents.computeIfAbsent(key, k -> new AtomicInteger());
}
@Override
diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitor.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitor.java
index b104364..9eeb677 100644
--- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitor.java
+++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitor.java
@@ -160,11 +160,7 @@ public class DubboMonitor implements Monitor {
int concurrent = url.getParameter(MonitorService.CONCURRENT, 0);
// init atomic reference
Statistics statistics = new Statistics(url);
- AtomicReference<long[]> reference = statisticsMap.get(statistics);
- if (reference == null) {
- statisticsMap.putIfAbsent(statistics, new AtomicReference<long[]>());
- reference = statisticsMap.get(statistics);
- }
+ AtomicReference<long[]> reference = statisticsMap.computeIfAbsent(statistics, k -> new AtomicReference<>());
// use CompareAndSet to sum
long[] current;
long[] update = new long[LENGTH];
diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/AbstractRegistryService.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/AbstractRegistryService.java
index b4c8926..8b7a32a 100644
--- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/AbstractRegistryService.java
+++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/AbstractRegistryService.java
@@ -99,11 +99,7 @@ public abstract class AbstractRegistryService implements RegistryService {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
- List<URL> urls = registered.get(service);
- if (urls == null) {
- registered.putIfAbsent(service, new CopyOnWriteArrayList<URL>());
- urls = registered.get(service);
- }
+ List<URL> urls = registered.computeIfAbsent(service, k -> new CopyOnWriteArrayList<>());
if (!urls.contains(url)) {
urls.add(url);
}
@@ -164,12 +160,8 @@ public abstract class AbstractRegistryService implements RegistryService {
if (listener == null) {
return;
}
- List<NotifyListener> listeners = notifyListeners.get(service);
- if (listeners == null) {
- notifyListeners.putIfAbsent(service, new CopyOnWriteArrayList<NotifyListener>());
- listeners = notifyListeners.get(service);
- }
- if (listeners != null && !listeners.contains(listener)) {
+ List<NotifyListener> listeners = notifyListeners.computeIfAbsent(service, k -> new CopyOnWriteArrayList<>());
+ if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryService.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryService.java
index fc11595..40c37b0 100644
--- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryService.java
+++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/SimpleRegistryService.java
@@ -49,11 +49,7 @@ public class SimpleRegistryService extends AbstractRegistryService {
public void register(String service, URL url) {
super.register(service, url);
String client = RpcContext.getContext().getRemoteAddressString();
- Map<String, URL> urls = remoteRegistered.get(client);
- if (urls == null) {
- remoteRegistered.putIfAbsent(client, new ConcurrentHashMap<String, URL>());
- urls = remoteRegistered.get(client);
- }
+ Map<String, URL> urls = remoteRegistered.computeIfAbsent(client, k -> new ConcurrentHashMap<>());
urls.put(service, url);
notify(service, getRegistered().get(service));
}
@@ -92,11 +88,7 @@ public class SimpleRegistryService extends AbstractRegistryService {
}
super.subscribe(service, url, listener);
- Map<String, NotifyListener> listeners = remoteListeners.get(client);
- if (listeners == null) {
- remoteListeners.putIfAbsent(client, new ConcurrentHashMap<String, NotifyListener>());
- listeners = remoteListeners.get(client);
- }
+ Map<String, NotifyListener> listeners = remoteListeners.computeIfAbsent(client, k -> new ConcurrentHashMap<>());
listeners.put(service, listener);
urls = getRegistered().get(service);
if (urls != null && urls.size() > 0) {
diff --git a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java
index e72e53f..3a6462d 100644
--- a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java
+++ b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java
@@ -317,11 +317,7 @@ public class MulticastRegistry extends FailbackRegistry {
for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) {
URL key = entry.getKey();
if (UrlUtils.isMatch(key, url)) {
- Set<URL> urls = received.get(key);
- if (urls == null) {
- received.putIfAbsent(key, new ConcurrentHashSet<URL>());
- urls = received.get(key);
- }
+ Set<URL> urls = received.computeIfAbsent(key, k -> new ConcurrentHashSet<>());
urls.add(url);
List<URL> list = toList(urls);
for (final NotifyListener listener : entry.getValue()) {
diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java
index 9fda052..38d4caf 100644
--- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java
+++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperRegistry.java
@@ -145,25 +145,17 @@ public class ZookeeperRegistry extends FailbackRegistry {
try {
if (ANY_VALUE.equals(url.getServiceInterface())) {
String root = toRootPath();
- ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
- if (listeners == null) {
- zkListeners.putIfAbsent(url, new ConcurrentHashMap<>());
- listeners = zkListeners.get(url);
- }
- ChildListener zkListener = listeners.get(listener);
- if (zkListener == null) {
- listeners.putIfAbsent(listener, (parentPath, currentChilds) -> {
- for (String child : currentChilds) {
- child = URL.decode(child);
- if (!anyServices.contains(child)) {
- anyServices.add(child);
- subscribe(url.setPath(child).addParameters(INTERFACE_KEY, child,
- Constants.CHECK_KEY, String.valueOf(false)), listener);
- }
+ ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
+ ChildListener zkListener = listeners.computeIfAbsent(listener, k -> (parentPath, currentChilds) -> {
+ for (String child : currentChilds) {
+ child = URL.decode(child);
+ if (!anyServices.contains(child)) {
+ anyServices.add(child);
+ subscribe(url.setPath(child).addParameters(INTERFACE_KEY, child,
+ Constants.CHECK_KEY, String.valueOf(false)), k);
}
- });
- zkListener = listeners.get(listener);
- }
+ }
+ });
zkClient.create(root, false);
List<String> services = zkClient.addChildListener(root, zkListener);
if (CollectionUtils.isNotEmpty(services)) {
@@ -177,16 +169,8 @@ public class ZookeeperRegistry extends FailbackRegistry {
} else {
List<URL> urls = new ArrayList<>();
for (String path : toCategoriesPath(url)) {
- ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
- if (listeners == null) {
- zkListeners.putIfAbsent(url, new ConcurrentHashMap<>());
- listeners = zkListeners.get(url);
- }
- ChildListener zkListener = listeners.get(listener);
- if (zkListener == null) {
- listeners.putIfAbsent(listener, (parentPath, currentChilds) -> ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds)));
- zkListener = listeners.get(listener);
- }
+ ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
+ ChildListener zkListener = listeners.computeIfAbsent(listener, k -> (parentPath, currentChilds) -> ZookeeperRegistry.this.notify(url, k, toUrlsWithEmpty(url, parentPath, currentChilds)));
zkClient.create(path, false);
List<String> children = zkClient.addChildListener(path, zkListener);
if (children != null) {
diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/support/AbstractEtcdClient.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/support/AbstractEtcdClient.java
index 362b268..430bc54 100644
--- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/support/AbstractEtcdClient.java
+++ b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/support/AbstractEtcdClient.java
@@ -106,16 +106,8 @@ public abstract class AbstractEtcdClient<WatcherListener> implements EtcdClient
@Override
public List<String> addChildListener(String path, final ChildListener listener) {
- ConcurrentMap<ChildListener, WatcherListener> listeners = childListeners.get(path);
- if (listeners == null) {
- childListeners.putIfAbsent(path, new ConcurrentHashMap<>());
- listeners = childListeners.get(path);
- }
- WatcherListener targetListener = listeners.get(listener);
- if (targetListener == null) {
- listeners.putIfAbsent(listener, createChildWatcherListener(path, listener));
- targetListener = listeners.get(listener);
- }
+ ConcurrentMap<ChildListener, WatcherListener> listeners = childListeners.computeIfAbsent(path, k -> new ConcurrentHashMap<>());
+ WatcherListener targetListener = listeners.computeIfAbsent(listener, k -> createChildWatcherListener(path, k));
return addChildWatcherListener(path, targetListener);
}
@@ -125,12 +117,7 @@ public abstract class AbstractEtcdClient<WatcherListener> implements EtcdClient
if (listeners == null) {
return null;
}
- WatcherListener targetListener = listeners.get(listener);
- if (targetListener == null) {
- listeners.putIfAbsent(listener, createChildWatcherListener(path, listener));
- targetListener = listeners.get(listener);
- }
- return targetListener;
+ return listeners.computeIfAbsent(listener, k -> createChildWatcherListener(path, k));
}
@Override
diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java
index c0258eb..6b7f8e2 100644
--- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java
+++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/org/apache/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java
@@ -107,16 +107,8 @@ public abstract class AbstractZookeeperClient<TargetDataListener, TargetChildLis
@Override
public List<String> addChildListener(String path, final ChildListener listener) {
- ConcurrentMap<ChildListener, TargetChildListener> listeners = childListeners.get(path);
- if (listeners == null) {
- childListeners.putIfAbsent(path, new ConcurrentHashMap<ChildListener, TargetChildListener>());
- listeners = childListeners.get(path);
- }
- TargetChildListener targetListener = listeners.get(listener);
- if (targetListener == null) {
- listeners.putIfAbsent(listener, createTargetChildListener(path, listener));
- targetListener = listeners.get(listener);
- }
+ ConcurrentMap<ChildListener, TargetChildListener> listeners = childListeners.computeIfAbsent(path, k -> new ConcurrentHashMap<>());
+ TargetChildListener targetListener = listeners.computeIfAbsent(listener, k -> createTargetChildListener(path, k));
return addTargetChildListener(path, targetListener);
}
@@ -127,16 +119,8 @@ public abstract class AbstractZookeeperClient<TargetDataListener, TargetChildLis
@Override
public void addDataListener(String path, DataListener listener, Executor executor) {
- ConcurrentMap<DataListener, TargetDataListener> dataListenerMap = listeners.get(path);
- if (dataListenerMap == null) {
- listeners.putIfAbsent(path, new ConcurrentHashMap<DataListener, TargetDataListener>());
- dataListenerMap = listeners.get(path);
- }
- TargetDataListener targetListener = dataListenerMap.get(listener);
- if (targetListener == null) {
- dataListenerMap.putIfAbsent(listener, createTargetDataListener(path, listener));
- targetListener = dataListenerMap.get(listener);
- }
+ ConcurrentMap<DataListener, TargetDataListener> dataListenerMap = listeners.computeIfAbsent(path, k -> new ConcurrentHashMap<>());
+ TargetDataListener targetListener = dataListenerMap.computeIfAbsent(listener, k -> createTargetDataListener(path, k));
addTargetDataListener(path, targetListener, executor);
}
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcStatus.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcStatus.java
index 70b0d7b..9aa8c17 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcStatus.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcStatus.java
@@ -54,12 +54,7 @@ public class RpcStatus {
*/
public static RpcStatus getStatus(URL url) {
String uri = url.toIdentityString();
- RpcStatus status = SERVICE_STATISTICS.get(uri);
- if (status == null) {
- SERVICE_STATISTICS.putIfAbsent(uri, new RpcStatus());
- status = SERVICE_STATISTICS.get(uri);
- }
- return status;
+ return SERVICE_STATISTICS.computeIfAbsent(uri, key -> new RpcStatus());
}
/**
@@ -77,17 +72,8 @@ public class RpcStatus {
*/
public static RpcStatus getStatus(URL url, String methodName) {
String uri = url.toIdentityString();
- ConcurrentMap<String, RpcStatus> map = METHOD_STATISTICS.get(uri);
- if (map == null) {
- METHOD_STATISTICS.putIfAbsent(uri, new ConcurrentHashMap<String, RpcStatus>());
- map = METHOD_STATISTICS.get(uri);
- }
- RpcStatus status = map.get(methodName);
- if (status == null) {
- map.putIfAbsent(methodName, new RpcStatus());
- status = map.get(methodName);
- }
- return status;
+ ConcurrentMap<String, RpcStatus> map = METHOD_STATISTICS.computeIfAbsent(uri, k -> new ConcurrentHashMap<>());
+ return map.computeIfAbsent(methodName, k -> new RpcStatus());
}
/**
diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/TraceFilter.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/TraceFilter.java
index ac412f4..9d922fc 100644
--- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/TraceFilter.java
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/TraceFilter.java
@@ -57,11 +57,7 @@ public class TraceFilter implements Filter {
channel.setAttribute(TRACE_MAX, max);
channel.setAttribute(TRACE_COUNT, new AtomicInteger());
String key = method != null && method.length() > 0 ? type.getName() + "." + method : type.getName();
- Set<Channel> channels = tracers.get(key);
- if (channels == null) {
- tracers.putIfAbsent(key, new ConcurrentHashSet<>());
- channels = tracers.get(key);
- }
+ Set<Channel> channels = tracers.computeIfAbsent(key, k -> new ConcurrentHashSet<>());
channels.add(channel);
}