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

liujun pushed a commit to branch dev-metadata
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git

commit 6140bc2a653cc3066d15ceeff5a3f06962fb32ad
Author: ken.lj <ken.lj...@gmail.com>
AuthorDate: Wed Nov 21 14:40:03 2018 +0800

    Fix concurrent problem of zookeeper configcenter, wait to start until cache 
being fully populated.
---
 .../java/org/apache/dubbo/config/AbstractConfig.java |  2 --
 .../support/apollo/ApolloDynamicConfiguration.java   |  2 --
 .../sources/ZooKeeperConfigurationSource.java        | 20 +++++++++-----------
 3 files changed, 9 insertions(+), 15 deletions(-)

diff --git 
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java
 
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java
index f60b378..a894d76 100644
--- 
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java
+++ 
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java
@@ -505,8 +505,6 @@ public abstract class AbstractConfig implements 
Serializable {
                     }
                 }
             } catch (Exception e) {
-                System.out.println(this.getClass().getName());
-                System.out.println(method.getName());
                 throw new IllegalStateException(e.getMessage(), e);
             }
         }
diff --git 
a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java
 
b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java
index f5f2cc0..f3f392d 100644
--- 
a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java
+++ 
b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java
@@ -56,8 +56,6 @@ public class ApolloDynamicConfiguration extends 
AbstractDynamicConfiguration<Con
         /**
          * Instead of using Dubbo's configuration, I would suggest use the 
original configuration method Apollo provides.
          */
-//        String configEnv = env.getCompositeConf().getString(ENV_KEY);
-//        String configCluster = env.getCompositeConf().getString(CLUSTER_KEY);
         String configEnv = url.getParameter(Constants.CONFIG_ENV_KEY);
         String configAddr = url.getBackupAddress();
         String configCluster = url.getParameter(Constants.CONFIG_CLUSTER_KEY);
diff --git 
a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/archaius/sources/ZooKeeperConfigurationSource.java
 
b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/archaius/sources/ZooKeeperConfigurationSource.java
index 647dcf6..4366ac9 100644
--- 
a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/archaius/sources/ZooKeeperConfigurationSource.java
+++ 
b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/archaius/sources/ZooKeeperConfigurationSource.java
@@ -39,6 +39,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
@@ -59,7 +60,7 @@ public class ZooKeeperConfigurationSource implements 
WatchedConfigurationSource,
     // The final root path would be: /configRootPath/"config"
     private final String configRootPath;
     private final TreeCache treeCache;
-    private boolean connected = false;
+    private CountDownLatch initializedLatch = new CountDownLatch(1);
 
     private final Charset charset = Charset.forName("UTF-8");
 
@@ -96,7 +97,7 @@ public class ZooKeeperConfigurationSource implements 
WatchedConfigurationSource,
                 new ExponentialBackoffRetry(1000, 3));
         client.start();
         try {
-            connected = client.blockUntilConnected(connectTimeout, 
TimeUnit.MILLISECONDS);
+            boolean connected = client.blockUntilConnected(connectTimeout, 
TimeUnit.MILLISECONDS);
             if (!connected) {
                 boolean check = 
Boolean.parseBoolean(System.getProperty(ARCHAIUS_CONFIG_CHECK_KEY, "false"));
                 if (check) {
@@ -138,8 +139,8 @@ public class ZooKeeperConfigurationSource implements 
WatchedConfigurationSource,
 
                 TreeCacheEvent.Type type = event.getType();
                 ChildData data = event.getData();
-                if (type == TreeCacheEvent.Type.INITIALIZED || type == 
TreeCacheEvent.Type.CONNECTION_RECONNECTED) {
-                    connected = true;
+                if (type == TreeCacheEvent.Type.INITIALIZED) {
+                    initializedLatch.countDown();
                 }
 
                 // TODO, ignore other event types
@@ -202,9 +203,10 @@ public class ZooKeeperConfigurationSource implements 
WatchedConfigurationSource,
 
         Map<String, Object> all = new HashMap<>();
 
-        if (!connected) {
-            logger.warn("ConfigCenter is not connected yet, zookeeper does't 
support local snapshot, so there's no backup data to use!");
-            return all;
+        try {
+            initializedLatch.await();
+        } catch (InterruptedException e) {
+            logger.error("Being interrupted unexpectedly when waiting 
zookeeper to initialize, the config data may not ready yet, be careful!");
         }
 
         Map<String, ChildData> dataMap = 
treeCache.getCurrentChildren(configRootPath);
@@ -257,8 +259,4 @@ public class ZooKeeperConfigurationSource implements 
WatchedConfigurationSource,
             logger.error("IOException should not have been thrown.", exc);
         }
     }
-
-    public boolean isConnected() {
-        return connected;
-    }
 }

Reply via email to