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 2023/02/17 13:33:25 UTC

[servicecomb-java-chassis] branch master updated: [SCB-2771]fix MicroserviceVersions concurrent access problem (#3637)

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/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new fe9ca2218 [SCB-2771]fix MicroserviceVersions concurrent access problem (#3637)
fe9ca2218 is described below

commit fe9ca2218bfdfb1e1464d43911f00d289e229a86
Author: liubao68 <bi...@qq.com>
AuthorDate: Fri Feb 17 21:33:17 2023 +0800

    [SCB-2771]fix MicroserviceVersions concurrent access problem (#3637)
---
 .../registry/consumer/MicroserviceVersions.java        | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/consumer/MicroserviceVersions.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/consumer/MicroserviceVersions.java
index da5f99cb5..563f872b6 100644
--- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/consumer/MicroserviceVersions.java
+++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/consumer/MicroserviceVersions.java
@@ -42,6 +42,8 @@ import org.apache.servicecomb.registry.definition.MicroserviceNameParser;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.annotations.VisibleForTesting;
+
 public class MicroserviceVersions {
   private static final Logger LOGGER = LoggerFactory.getLogger(MicroserviceVersions.class);
 
@@ -120,13 +122,18 @@ public class MicroserviceVersions {
     return shortName;
   }
 
+  @VisibleForTesting
   public Map<String, MicroserviceVersion> getVersions() {
-    return versions;
+    synchronized (lock) {
+      return versions;
+    }
   }
 
   @SuppressWarnings("unchecked")
   public <T extends MicroserviceVersion> T getVersion(String serviceId) {
-    return (T) versions.get(serviceId);
+    synchronized (lock) {
+      return (T) versions.get(serviceId);
+    }
   }
 
   public String getRevision() {
@@ -318,10 +325,11 @@ public class MicroserviceVersions {
   }
 
   public void destroy() {
-    for (MicroserviceVersion microserviceVersion : versions.values()) {
-      microserviceVersion.destroy();
+    synchronized (lock) {
+      for (MicroserviceVersion microserviceVersion : versions.values()) {
+        microserviceVersion.destroy();
+      }
     }
-
     appManager.getEventBus().post(new DestroyMicroserviceEvent(this));
   }
 }