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

albumenj pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3 by this push:
     new 403e127385 Check the md5 of the metadata cache file (#15006)
403e127385 is described below

commit 403e127385782e2cf4db48df7c25a4cbfc5fcfd5
Author: TomlongTK <[email protected]>
AuthorDate: Fri Feb 28 10:12:08 2025 +0800

    Check the md5 of the metadata cache file (#15006)
    
    * Read remote metadata once during application execution
    
    * Use local cache record is remotely loaded.
    
    * Check the md5 of the cache file.
    
    * Fix unit tests.
    
    * Synchronous calculation.
---
 .../dubbo/metadata/AbstractCacheManager.java       | 13 +++++++--
 .../org/apache/dubbo/metadata/MetadataInfo.java    | 16 +++++++----
 .../client/metadata/store/MetaCacheManager.java    | 10 +++++++
 .../metadata/store/MetaCacheManagerTest.java       | 33 ++++++++++++++++++----
 .../src/test/resources/test-metadata.dubbo.cache   |  1 +
 5 files changed, 59 insertions(+), 14 deletions(-)

diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractCacheManager.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractCacheManager.java
index 3344115751..1a1b4ad605 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractCacheManager.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractCacheManager.java
@@ -65,7 +65,8 @@ public abstract class AbstractCacheManager<V> implements 
Disposable {
             for (Map.Entry<String, String> entry : properties.entrySet()) {
                 String key = entry.getKey();
                 String value = entry.getValue();
-                this.cache.put(key, toValueType(value));
+                V v = toValueType(value);
+                put(key, v);
             }
             // executorService can be empty if FileCacheStore fails
             if (executorService == null) {
@@ -89,12 +90,18 @@ public abstract class AbstractCacheManager<V> implements 
Disposable {
 
     protected abstract String getName();
 
+    protected boolean validate(String key, V value) {
+        return value != null;
+    }
+
     public V get(String key) {
         return cache.get(key);
     }
 
     public void put(String key, V apps) {
-        cache.put(key, apps);
+        if (validate(key, apps)) {
+            cache.put(key, apps);
+        }
     }
 
     public V remove(String key) {
@@ -120,7 +127,7 @@ public abstract class AbstractCacheManager<V> implements 
Disposable {
 
     public void update(Map<String, V> newCache) {
         for (Map.Entry<String, V> entry : newCache.entrySet()) {
-            cache.put(entry.getKey(), entry.getValue());
+            put(entry.getKey(), entry.getValue());
         }
     }
 
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
index d7dccf8df1..fe477a846e 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java
@@ -193,12 +193,7 @@ public class MetadataInfo implements Serializable {
         if (CollectionUtils.isEmptyMap(services)) {
             this.revision = EMPTY_REVISION;
         } else {
-            StringBuilder sb = new StringBuilder();
-            sb.append(app);
-            for (Map.Entry<String, ServiceInfo> entry : new 
TreeMap<>(services).entrySet()) {
-                sb.append(entry.getValue().toDescString());
-            }
-            String tempRevision = RevisionResolver.calRevision(sb.toString());
+            String tempRevision = calRevision();
             if (!StringUtils.isEquals(this.revision, tempRevision)) {
                 if (logger.isInfoEnabled()) {
                     logger.info(String.format(
@@ -212,6 +207,15 @@ public class MetadataInfo implements Serializable {
         return revision;
     }
 
+    public synchronized String calRevision() {
+        StringBuilder sb = new StringBuilder();
+        sb.append(app);
+        for (Map.Entry<String, ServiceInfo> entry : new 
TreeMap<>(services).entrySet()) {
+            sb.append(entry.getValue().toDescString());
+        }
+        return RevisionResolver.calRevision(sb.toString());
+    }
+
     public void setRevision(String revision) {
         this.revision = revision;
     }
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManager.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManager.java
index 73ff54c0e6..82ac7596a8 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManager.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManager.java
@@ -23,6 +23,7 @@ import org.apache.dubbo.metadata.AbstractCacheManager;
 import org.apache.dubbo.metadata.MetadataInfo;
 import org.apache.dubbo.rpc.model.ScopeModel;
 
+import java.util.Objects;
 import java.util.concurrent.ScheduledExecutorService;
 
 import static 
org.apache.dubbo.common.constants.CommonConstants.DubboProperty.DUBBO_META_CACHE_ENTRYSIZE;
@@ -76,4 +77,13 @@ public class MetaCacheManager extends 
AbstractCacheManager<MetadataInfo> {
     protected String getName() {
         return "meta";
     }
+
+    @Override
+    protected boolean validate(String key, MetadataInfo value) {
+        if (!super.validate(key, value)) {
+            return false;
+        }
+        String revision = value.calRevision();
+        return Objects.equals(key, revision);
+    }
 }
diff --git 
a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManagerTest.java
 
b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManagerTest.java
index 519d0ebd9b..a0882b8d3b 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManagerTest.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManagerTest.java
@@ -32,8 +32,10 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
 class MetaCacheManagerTest {
@@ -66,24 +68,44 @@ class MetaCacheManagerTest {
             //        cacheManager.setExtensionAccessor(extensionAccessor);
 
             MetadataInfo metadataInfo = cacheManager.get("1");
-            assertNotNull(metadataInfo);
-            assertEquals("demo", metadataInfo.getApp());
+            assertNull(metadataInfo);
             metadataInfo = cacheManager.get("2");
             assertNull(metadataInfo);
 
+            metadataInfo = 
cacheManager.get("065787862412c2cc0a1b9577bc194c9a");
+            assertNotNull(metadataInfo);
+            assertEquals("demo", metadataInfo.getApp());
+
             Map<String, MetadataInfo> newMetadatas = new HashMap<>();
             MetadataInfo metadataInfo2 = JsonUtils.toJavaObject(
                     
"{\"app\":\"demo2\",\"services\":{\"greeting/org.apache.dubbo.registry.service.DemoService2:1.0.0:dubbo\":{\"name\":\"org.apache.dubbo.registry.service.DemoService2\",\"group\":\"greeting\",\"version\":\"1.0.0\",\"protocol\":\"dubbo\",\"path\":\"org.apache.dubbo.registry.service.DemoService2\",\"params\":{\"application\":\"demo-provider2\",\"sayHello.timeout\":\"7000\",\"version\":\"1.0.0\",\"timeout\":\"5000\",\"group\":\"greeting\"}},\"greeting/org.apache.dubbo.regi
 [...]
                     MetadataInfo.class);
+            assertNotEquals("2", metadataInfo2.calRevision());
             newMetadatas.put("2", metadataInfo2);
 
+            MetadataInfo metadataInfo3 = JsonUtils.toJavaObject(
+                    
"{\"app\":\"demo3\",\"services\":{\"greeting/org.apache.dubbo.registry.service.DemoService3:1.0.0:dubbo\":{\"name\":\"org.apache.dubbo.registry.service.DemoService3\",\"group\":\"greeting\",\"version\":\"1.0.0\",\"protocol\":\"dubbo\",\"path\":\"org.apache.dubbo.registry.service.DemoService3\",\"params\":{\"application\":\"demo-provider3\",\"sayHello.timeout\":\"7000\",\"version\":\"1.0.0\",\"timeout\":\"5000\",\"group\":\"greeting\"}},\"greeting/org.apache.dubbo.regi
 [...]
+                    MetadataInfo.class);
+            assertEquals("84f10ebf1226b496c9ff102f311918e4", 
metadataInfo3.calRevision());
+            newMetadatas.put("84f10ebf1226b496c9ff102f311918e4", 
metadataInfo3);
+
             cacheManager.update(newMetadatas);
             metadataInfo = cacheManager.get("1");
+            assertNull(metadataInfo);
+
+            metadataInfo = 
cacheManager.get("065787862412c2cc0a1b9577bc194c9a");
             assertNotNull(metadataInfo);
             assertEquals("demo", metadataInfo.getApp());
+
             metadataInfo = cacheManager.get("2");
+            assertNull(metadataInfo);
+
+            metadataInfo = 
cacheManager.get("84f10ebf1226b496c9ff102f311918e4");
             assertNotNull(metadataInfo);
-            assertEquals("demo2", metadataInfo.getApp());
+            assertEquals("demo3", metadataInfo.getApp());
+            assertTrue(metadataInfo
+                    .getServices()
+                    
.containsKey("greeting/org.apache.dubbo.registry.service.DemoService3:1.0.0:dubbo"));
         } finally {
             cacheManager.destroy();
         }
@@ -97,7 +119,8 @@ class MetaCacheManagerTest {
                 MetadataInfo.class);
         MetaCacheManager cacheManager = new MetaCacheManager();
         try {
-            cacheManager.put("3", metadataInfo3);
+            assertEquals("97370ff779b6b6ebb7012bae61710de2", 
metadataInfo3.calRevision());
+            cacheManager.put("97370ff779b6b6ebb7012bae61710de2", 
metadataInfo3);
 
             try {
                 MetaCacheManager.CacheRefreshTask<MetadataInfo> task = new 
MetaCacheManager.CacheRefreshTask<>(
@@ -112,7 +135,7 @@ class MetaCacheManagerTest {
             MetaCacheManager newCacheManager = null;
             try {
                 newCacheManager = new MetaCacheManager();
-                MetadataInfo metadataInfo = newCacheManager.get("3");
+                MetadataInfo metadataInfo = 
newCacheManager.get("97370ff779b6b6ebb7012bae61710de2");
                 assertNotNull(metadataInfo);
                 assertEquals("demo3", metadataInfo.getApp());
             } finally {
diff --git 
a/dubbo-registry/dubbo-registry-api/src/test/resources/test-metadata.dubbo.cache
 
b/dubbo-registry/dubbo-registry-api/src/test/resources/test-metadata.dubbo.cache
index e3fa4ae0b7..67afe8b592 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/test/resources/test-metadata.dubbo.cache
+++ 
b/dubbo-registry/dubbo-registry-api/src/test/resources/test-metadata.dubbo.cache
@@ -1 +1,2 @@
 
1={"app":"demo","services":{"greeting/org.apache.dubbo.registry.service.DemoService2:1.0.0:dubbo":{"name":"org.apache.dubbo.registry.service.DemoService2","group":"greeting","version":"1.0.0","protocol":"dubbo","path":"org.apache.dubbo.registry.service.DemoService2","params":{"application":"demo-provider2","sayHello.timeout":"7000","version":"1.0.0","timeout":"5000","group":"greeting"}}}}
+065787862412c2cc0a1b9577bc194c9a={"app":"demo","services":{"greeting/org.apache.dubbo.registry.service.DemoService2:1.0.0:dubbo":{"name":"org.apache.dubbo.registry.service.DemoService2","group":"greeting","version":"1.0.0","protocol":"dubbo","path":"org.apache.dubbo.registry.service.DemoService2","params":{"application":"demo-provider2","sayHello.timeout":"7000","version":"1.0.0","timeout":"5000","group":"greeting"}}}}

Reply via email to