From dff48ec0d838c323c320b7ce14b31e02e2b620da Mon Sep 17 00:00:00 2001 From: TomlongTK Date: Fri, 20 Dec 2024 10:50:45 +0800 Subject: [PATCH 1/5] Read remote metadata once during application execution --- .../java/org/apache/dubbo/metadata/MetadataInfo.java | 9 +++++++++ .../dubbo/registry/client/AbstractServiceDiscovery.java | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) 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 d7dccf8df1c..b426b012881 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 @@ -79,6 +79,7 @@ public class MetadataInfo implements Serializable { private transient ConcurrentNavigableMap> subscribedServiceURLs; private transient ConcurrentNavigableMap> exportedServiceURLs; private transient ExtensionLoader loader; + private transient volatile boolean remoteLoaded = false; public MetadataInfo() { this(null); @@ -488,6 +489,14 @@ private Object readResolve() { return new MetadataInfo(this.app, this.revision, this.services); } + public boolean isRemoteLoaded() { + return remoteLoaded; + } + + public void setRemoteLoaded(boolean remoteLoaded) { + this.remoteLoaded = remoteLoaded; + } + public static class ServiceInfo implements Serializable { private String name; private String group; diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java index b05408caacf..bc226e2fe0b 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java @@ -235,7 +235,7 @@ public MetadataInfo getLocalMetadata(String revision) { public MetadataInfo getRemoteMetadata(String revision, List instances) { MetadataInfo metadata = metaCacheManager.get(revision); - if (metadata != null && metadata != MetadataInfo.EMPTY) { + if (metadata != null && metadata != MetadataInfo.EMPTY && metadata.isRemoteLoaded()) { metadata.init(); // metadata loaded from cache if (logger.isDebugEnabled()) { @@ -279,6 +279,7 @@ public MetadataInfo getRemoteMetadata(String revision, List ins "Failed to get metadata for revision after 3 retries, revision=" + revision); } else { metaCacheManager.put(revision, metadata); + metadata.setRemoteLoaded(true); } } return metadata; From f06d49eb5ae67cee395b7252263ec80f8f55a553 Mon Sep 17 00:00:00 2001 From: TomlongTK Date: Mon, 6 Jan 2025 16:34:49 +0800 Subject: [PATCH 2/5] Use local cache record is remotely loaded. --- .../apache/dubbo/metadata/MetadataInfo.java | 9 -- .../client/AbstractServiceDiscovery.java | 90 ++++++++++--------- 2 files changed, 49 insertions(+), 50 deletions(-) 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 b426b012881..d7dccf8df1c 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 @@ -79,7 +79,6 @@ public class MetadataInfo implements Serializable { private transient ConcurrentNavigableMap> subscribedServiceURLs; private transient ConcurrentNavigableMap> exportedServiceURLs; private transient ExtensionLoader loader; - private transient volatile boolean remoteLoaded = false; public MetadataInfo() { this(null); @@ -489,14 +488,6 @@ private Object readResolve() { return new MetadataInfo(this.app, this.revision, this.services); } - public boolean isRemoteLoaded() { - return remoteLoaded; - } - - public void setRemoteLoaded(boolean remoteLoaded) { - this.remoteLoaded = remoteLoaded; - } - public static class ServiceInfo implements Serializable { private String name; private String group; diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java index bc226e2fe0b..0b6e11680c2 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java @@ -80,6 +80,8 @@ public abstract class AbstractServiceDiscovery implements ServiceDiscovery { protected ApplicationModel applicationModel; + private final Set remoteLoadedMetadataVersions = new ConcurrentHashSet<>(); + public AbstractServiceDiscovery(ApplicationModel applicationModel, URL registryURL) { this(applicationModel, applicationModel.getApplicationName(), registryURL); MetadataReportInstance metadataReportInstance = @@ -234,53 +236,16 @@ public MetadataInfo getLocalMetadata(String revision) { @Override public MetadataInfo getRemoteMetadata(String revision, List instances) { MetadataInfo metadata = metaCacheManager.get(revision); + if (!remoteLoadedMetadataVersions.contains(revision) || metadata == null || metadata == MetadataInfo.EMPTY) { + metadata = loadRemoteMetadata(revision, instances); + } - if (metadata != null && metadata != MetadataInfo.EMPTY && metadata.isRemoteLoaded()) { + if (metadata != null && metadata != MetadataInfo.EMPTY) { metadata.init(); // metadata loaded from cache if (logger.isDebugEnabled()) { logger.debug("MetadataInfo for revision=" + revision + ", " + metadata); } - return metadata; - } - - synchronized (metaCacheManager) { - // try to load metadata from remote. - int triedTimes = 0; - while (triedTimes < 3) { - - metadata = MetricsEventBus.post( - MetadataEvent.toSubscribeEvent(applicationModel), - () -> MetadataUtils.getRemoteMetadata(revision, instances, metadataReport), - result -> result != MetadataInfo.EMPTY); - - if (metadata != MetadataInfo.EMPTY) { // succeeded - metadata.init(); - break; - } else { // failed - if (triedTimes > 0) { - if (logger.isDebugEnabled()) { - logger.debug("Retry the " + triedTimes + " times to get metadata for revision=" + revision); - } - } - triedTimes++; - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - } - } - - if (metadata == MetadataInfo.EMPTY) { - logger.error( - REGISTRY_FAILED_LOAD_METADATA, - "", - "", - "Failed to get metadata for revision after 3 retries, revision=" + revision); - } else { - metaCacheManager.put(revision, metadata); - metadata.setRemoteLoaded(true); - } } return metadata; } @@ -432,6 +397,49 @@ private String getCacheNameSuffix() { return stringBuilder.toString(); } + private MetadataInfo loadRemoteMetadata(String revision, List instances) { + MetadataInfo metadata = null; + synchronized (metaCacheManager) { + // try to load metadata from remote. + int triedTimes = 0; + while (triedTimes < 3) { + + metadata = MetricsEventBus.post( + MetadataEvent.toSubscribeEvent(applicationModel), + () -> MetadataUtils.getRemoteMetadata(revision, instances, metadataReport), + result -> result != MetadataInfo.EMPTY); + + if (metadata != MetadataInfo.EMPTY) { // succeeded + metadata.init(); + break; + } else { // failed + if (triedTimes > 0) { + if (logger.isDebugEnabled()) { + logger.debug("Retry the " + triedTimes + " times to get metadata for revision=" + revision); + } + } + triedTimes++; + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + } + } + + if (metadata == MetadataInfo.EMPTY) { + logger.error( + REGISTRY_FAILED_LOAD_METADATA, + "", + "", + "Failed to get metadata for revision after 3 retries, revision=" + revision); + } else { + metaCacheManager.put(revision, metadata); + remoteLoadedMetadataVersions.add(revision); + } + } + return metadata; + } + private static class MetadataInfoStat { private final MetadataInfo metadataInfo; private final long updateTime = System.currentTimeMillis(); From 50d535944b9804c8a204e138cd733223822ef28a Mon Sep 17 00:00:00 2001 From: TomlongTK Date: Tue, 7 Jan 2025 19:44:48 +0800 Subject: [PATCH 3/5] Check the md5 of the cache file. --- .../dubbo/metadata/AbstractCacheManager.java | 9 +- .../apache/dubbo/metadata/MetadataInfo.java | 16 ++-- .../client/AbstractServiceDiscovery.java | 87 +++++++++---------- .../metadata/store/MetaCacheManager.java | 10 +++ 4 files changed, 67 insertions(+), 55 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 33441157516..0d2652bc86c 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,10 @@ protected void init( for (Map.Entry entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); - this.cache.put(key, toValueType(value)); + V v = toValueType(value); + if (validate(v)) { + cache.put(key, v); + } } // executorService can be empty if FileCacheStore fails if (executorService == null) { @@ -89,6 +92,10 @@ protected void init( protected abstract String getName(); + protected boolean validate(V value) { + return value != null; + } + public V get(String key) { return cache.get(key); } 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 d7dccf8df1c..ecf185d46b3 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 synchronized String calAndGetRevision() { if (CollectionUtils.isEmptyMap(services)) { this.revision = EMPTY_REVISION; } else { - StringBuilder sb = new StringBuilder(); - sb.append(app); - for (Map.Entry 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 synchronized String calAndGetRevision() { return revision; } + public String calRevision() { + StringBuilder sb = new StringBuilder(); + sb.append(app); + for (Map.Entry 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/AbstractServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java index 0b6e11680c2..b05408caacf 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java @@ -80,8 +80,6 @@ public abstract class AbstractServiceDiscovery implements ServiceDiscovery { protected ApplicationModel applicationModel; - private final Set remoteLoadedMetadataVersions = new ConcurrentHashSet<>(); - public AbstractServiceDiscovery(ApplicationModel applicationModel, URL registryURL) { this(applicationModel, applicationModel.getApplicationName(), registryURL); MetadataReportInstance metadataReportInstance = @@ -236,9 +234,6 @@ public MetadataInfo getLocalMetadata(String revision) { @Override public MetadataInfo getRemoteMetadata(String revision, List instances) { MetadataInfo metadata = metaCacheManager.get(revision); - if (!remoteLoadedMetadataVersions.contains(revision) || metadata == null || metadata == MetadataInfo.EMPTY) { - metadata = loadRemoteMetadata(revision, instances); - } if (metadata != null && metadata != MetadataInfo.EMPTY) { metadata.init(); @@ -246,6 +241,45 @@ public MetadataInfo getRemoteMetadata(String revision, List ins if (logger.isDebugEnabled()) { logger.debug("MetadataInfo for revision=" + revision + ", " + metadata); } + return metadata; + } + + synchronized (metaCacheManager) { + // try to load metadata from remote. + int triedTimes = 0; + while (triedTimes < 3) { + + metadata = MetricsEventBus.post( + MetadataEvent.toSubscribeEvent(applicationModel), + () -> MetadataUtils.getRemoteMetadata(revision, instances, metadataReport), + result -> result != MetadataInfo.EMPTY); + + if (metadata != MetadataInfo.EMPTY) { // succeeded + metadata.init(); + break; + } else { // failed + if (triedTimes > 0) { + if (logger.isDebugEnabled()) { + logger.debug("Retry the " + triedTimes + " times to get metadata for revision=" + revision); + } + } + triedTimes++; + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + } + } + + if (metadata == MetadataInfo.EMPTY) { + logger.error( + REGISTRY_FAILED_LOAD_METADATA, + "", + "", + "Failed to get metadata for revision after 3 retries, revision=" + revision); + } else { + metaCacheManager.put(revision, metadata); + } } return metadata; } @@ -397,49 +431,6 @@ private String getCacheNameSuffix() { return stringBuilder.toString(); } - private MetadataInfo loadRemoteMetadata(String revision, List instances) { - MetadataInfo metadata = null; - synchronized (metaCacheManager) { - // try to load metadata from remote. - int triedTimes = 0; - while (triedTimes < 3) { - - metadata = MetricsEventBus.post( - MetadataEvent.toSubscribeEvent(applicationModel), - () -> MetadataUtils.getRemoteMetadata(revision, instances, metadataReport), - result -> result != MetadataInfo.EMPTY); - - if (metadata != MetadataInfo.EMPTY) { // succeeded - metadata.init(); - break; - } else { // failed - if (triedTimes > 0) { - if (logger.isDebugEnabled()) { - logger.debug("Retry the " + triedTimes + " times to get metadata for revision=" + revision); - } - } - triedTimes++; - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - } - } - - if (metadata == MetadataInfo.EMPTY) { - logger.error( - REGISTRY_FAILED_LOAD_METADATA, - "", - "", - "Failed to get metadata for revision after 3 retries, revision=" + revision); - } else { - metaCacheManager.put(revision, metadata); - remoteLoadedMetadataVersions.add(revision); - } - } - return metadata; - } - private static class MetadataInfoStat { private final MetadataInfo metadataInfo; private final long updateTime = System.currentTimeMillis(); 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 73ff54c0e66..c71b5be1547 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.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 @@ protected MetadataInfo toValueType(String value) { protected String getName() { return "meta"; } + + @Override + protected boolean validate(MetadataInfo value) { + if (!super.validate(value)) { + return false; + } + String revision = value.calRevision(); + return Objects.equals(value.getRevision(), revision); + } } From daef60fe5c11e5d94715a155fd63a97fb8e7a83e Mon Sep 17 00:00:00 2001 From: TomlongTK Date: Wed, 8 Jan 2025 12:01:11 +0800 Subject: [PATCH 4/5] Fix unit tests. --- .../dubbo/metadata/AbstractCacheManager.java | 12 +++---- .../metadata/store/MetaCacheManager.java | 6 ++-- .../metadata/store/MetaCacheManagerTest.java | 33 ++++++++++++++++--- .../test/resources/test-metadata.dubbo.cache | 1 + 4 files changed, 38 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 0d2652bc86c..1a1b4ad6058 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 @@ -66,9 +66,7 @@ protected void init( String key = entry.getKey(); String value = entry.getValue(); V v = toValueType(value); - if (validate(v)) { - cache.put(key, v); - } + put(key, v); } // executorService can be empty if FileCacheStore fails if (executorService == null) { @@ -92,7 +90,7 @@ protected void init( protected abstract String getName(); - protected boolean validate(V value) { + protected boolean validate(String key, V value) { return value != null; } @@ -101,7 +99,9 @@ public V get(String 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) { @@ -127,7 +127,7 @@ public Map getAll() { public void update(Map newCache) { for (Map.Entry entry : newCache.entrySet()) { - cache.put(entry.getKey(), entry.getValue()); + put(entry.getKey(), entry.getValue()); } } 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 c71b5be1547..82ac7596a8d 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 @@ -79,11 +79,11 @@ protected String getName() { } @Override - protected boolean validate(MetadataInfo value) { - if (!super.validate(value)) { + protected boolean validate(String key, MetadataInfo value) { + if (!super.validate(key, value)) { return false; } String revision = value.calRevision(); - return Objects.equals(value.getRevision(), revision); + 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 519d0ebd9b8..a0882b8d3b7 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.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 @@ void testCache() { // 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 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.registry.service.DemoService:1.0.0:dubbo\":{\"name\":\"org.apache.dubbo.registry.service.DemoService\",\"group\":\"greeting\",\"version\":\"1.0.0\",\"protocol\":\"dubbo\",\"path\":\"org.apache.dubbo.registry.service.DemoService\",\"params\":{\"application\":\"demo-provider2\",\"version\":\"1.0.0\",\"timeout\":\"5000\",\"group\":\"greeting\"}}}}\n", 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.registry.service.DemoService:1.0.0:dubbo\":{\"name\":\"org.apache.dubbo.registry.service.DemoService\",\"group\":\"greeting\",\"version\":\"1.0.0\",\"protocol\":\"dubbo\",\"path\":\"org.apache.dubbo.registry.service.DemoService\",\"params\":{\"application\":\"demo-provider3\",\"version\":\"1.0.0\",\"timeout\":\"5000\",\"group\":\"greeting\"}}}}\n", + 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 @@ void testCacheDump() { MetadataInfo.class); MetaCacheManager cacheManager = new MetaCacheManager(); try { - cacheManager.put("3", metadataInfo3); + assertEquals("97370ff779b6b6ebb7012bae61710de2", metadataInfo3.calRevision()); + cacheManager.put("97370ff779b6b6ebb7012bae61710de2", metadataInfo3); try { MetaCacheManager.CacheRefreshTask task = new MetaCacheManager.CacheRefreshTask<>( @@ -112,7 +135,7 @@ void testCacheDump() { 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 e3fa4ae0b75..67afe8b592a 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"}}}} From 15398ac14d045457f5e25a6d737f5971a9c28e76 Mon Sep 17 00:00:00 2001 From: TomlongTK Date: Tue, 14 Jan 2025 14:23:03 +0800 Subject: [PATCH 5/5] Synchronous calculation. --- .../src/main/java/org/apache/dubbo/metadata/MetadataInfo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ecf185d46b3..fe477a846ea 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 @@ -207,7 +207,7 @@ public synchronized String calAndGetRevision() { return revision; } - public String calRevision() { + public synchronized String calRevision() { StringBuilder sb = new StringBuilder(); sb.append(app); for (Map.Entry entry : new TreeMap<>(services).entrySet()) {