You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/08/09 00:59:33 UTC

[incubator-servicecomb-java-chassis] 03/05: consider multipul verticle is deployed, pull and push config at the same time

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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 6c942b2aa3815bb45c0134d2f7e9c3242d377053
Author: heyile <he...@huawei.com>
AuthorDate: Thu Aug 2 10:51:13 2018 +0800

    consider multipul verticle is deployed, pull and push config at the same time
---
 .../config/client/ParseConfigUtils.java            | 59 ++++++++++++++--------
 1 file changed, 39 insertions(+), 20 deletions(-)

diff --git a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
index 0ced4fd..b9e517e 100644
--- a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
+++ b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
@@ -21,6 +21,8 @@ import java.io.IOException;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler;
 import org.apache.servicecomb.foundation.common.utils.JsonUtils;
@@ -44,35 +46,52 @@ public class ParseConfigUtils {
 
   private UpdateHandler updateHandler;
 
+  private Lock configLock = new ReentrantLock();
+
   public ParseConfigUtils(UpdateHandler updateHandler) {
     this.updateHandler = updateHandler;
   }
 
+  /*
+    as the data is returned, we can block the thread at a short time. consider that if the multiple verticle is deployed
+    and if we use pull mode and push mode at the same time , we must share a common lock with all methods which would
+    change the config setting
+   */
   public void refreshConfigItems(Map<String, Map<String, Object>> remoteItems) {
-    CURRENT_VERSION_INFO =
-        remoteItems.getOrDefault("revision", new HashMap<>()).getOrDefault("version", "default").toString();
-    //make sure the CURRENT_VERSION_INFO != ""
-    CURRENT_VERSION_INFO = CURRENT_VERSION_INFO.equals("") ? "default" : CURRENT_VERSION_INFO;
-    remoteItems.remove("revision");//the key revision is not the config setting
-    multiDimensionItems.clear();
-    multiDimensionItems.putAll(remoteItems);
-    doRefreshItems();
-    LOGGER.debug("refresh config success");
+    try {
+      configLock.lock();
+      CURRENT_VERSION_INFO =
+          remoteItems.getOrDefault("revision", new HashMap<>()).getOrDefault("version", "default").toString();
+      //make sure the CURRENT_VERSION_INFO != ""
+      CURRENT_VERSION_INFO = CURRENT_VERSION_INFO.equals("") ? "default" : CURRENT_VERSION_INFO;
+      remoteItems.remove("revision");//the key revision is not the config setting
+      multiDimensionItems.clear();
+      multiDimensionItems.putAll(remoteItems);
+      doRefreshItems();
+      LOGGER.debug("refresh config success");
+    } finally {
+      configLock.unlock();
+    }
   }
 
   public void refreshConfigItemsIncremental(Map<String, Object> action) {
-    if ("UPDATE".equals(action.get("action"))) {
-      try {
-        multiDimensionItems.put((String) action.get("key"), JsonUtils.OBJ_MAPPER
-            .readValue(action.get("value").toString(), new TypeReference<Map<String, Object>>() {
-            }));
-      } catch (IOException e) {
-        LOGGER.error("parse config change action fail");
+    try {
+      configLock.lock();
+      if ("UPDATE".equals(action.get("action"))) {
+        try {
+          multiDimensionItems.put((String) action.get("key"), JsonUtils.OBJ_MAPPER
+              .readValue(action.get("value").toString(), new TypeReference<Map<String, Object>>() {
+              }));
+        } catch (IOException e) {
+          LOGGER.error("parse config change action fail");
+        }
+        doRefreshItems();
+      } else if ("DELETE".equals(action.get("action"))) {
+        multiDimensionItems.remove(action.get("key"));
+        doRefreshItems();
       }
-      doRefreshItems();
-    } else if ("DELETE".equals(action.get("action"))) {
-      multiDimensionItems.remove(action.get("key"));
-      doRefreshItems();
+    } finally {
+      configLock.unlock();
     }
   }