You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by te...@apache.org on 2020/08/30 09:03:32 UTC

[shardingsphere-elasticjob] branch master updated: Replace the `get-check-put` operations with atomic `computeIfAbsent` (#1426)

This is an automated email from the ASF dual-hosted git repository.

technoboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git


The following commit(s) were added to refs/heads/master by this push:
     new eb851f4  Replace the `get-check-put` operations with atomic `computeIfAbsent` (#1426)
eb851f4 is described below

commit eb851f4e5eb109ae2a354906ab59fc0238f2c354
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Sun Aug 30 17:03:18 2020 +0800

    Replace the `get-check-put` operations with atomic `computeIfAbsent` (#1426)
---
 .../internal/reg/RegistryCenterFactory.java        | 27 +++++++++++++---------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/elasticjob-lite/elasticjob-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/internal/reg/RegistryCenterFactory.java b/elasticjob-lite/elasticjob-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/internal/reg/RegistryCenterFactory.java
index b6f16b8..d94ef23 100644
--- a/elasticjob-lite/elasticjob-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/internal/reg/RegistryCenterFactory.java
+++ b/elasticjob-lite/elasticjob-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/internal/reg/RegistryCenterFactory.java
@@ -28,18 +28,20 @@ import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration
 import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
 
 import java.nio.charset.StandardCharsets;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Registry center factory.
  */
+@SuppressWarnings("UnstableApiUsage")
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class RegistryCenterFactory {
     
-    private static final ConcurrentHashMap<HashCode, CoordinatorRegistryCenter> REG_CENTER_REGISTRY = new ConcurrentHashMap<>(); 
+    private static final Map<HashCode, CoordinatorRegistryCenter> REG_CENTER_REGISTRY = new ConcurrentHashMap<>();
     
     /**
-     * Create registry center.
+     * Create a {@link CoordinatorRegistryCenter} or return the existing one if there is one set up with the same {@code connectionString}, {@code namespace} and {@code digest} already.
      *
      * @param connectString registry center connect string
      * @param namespace registry center namespace
@@ -52,17 +54,20 @@ public final class RegistryCenterFactory {
             hasher.putString(digest, StandardCharsets.UTF_8);
         }
         HashCode hashCode = hasher.hash();
-        CoordinatorRegistryCenter result = REG_CENTER_REGISTRY.get(hashCode);
-        if (null != result) {
-            return result;
-        }
-        ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
+        return REG_CENTER_REGISTRY.computeIfAbsent(hashCode, unused -> {
+            final CoordinatorRegistryCenter coordinatorRegistryCenter = newCoordinatorRegistryCenter(connectString, namespace, digest);
+            coordinatorRegistryCenter.init();
+            return coordinatorRegistryCenter;
+        });
+    }
+    
+    private static CoordinatorRegistryCenter newCoordinatorRegistryCenter(final String connectString,
+                                                                          final String namespace,
+                                                                          final String digest) {
+        final ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
         if (!Strings.isNullOrEmpty(digest)) {
             zkConfig.setDigest(digest);
         }
-        result = new ZookeeperRegistryCenter(zkConfig);
-        result.init();
-        REG_CENTER_REGISTRY.put(hashCode, result);
-        return result;
+        return new ZookeeperRegistryCenter(zkConfig);
     }
 }