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..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 @@ -65,7 +65,8 @@ 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); + put(key, v); } // executorService can be empty if FileCacheStore fails if (executorService == null) { @@ -89,12 +90,18 @@ protected void init( 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 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-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..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 @@ -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 synchronized 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/metadata/store/MetaCacheManager.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManager.java index 73ff54c0e66..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 @@ -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(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 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"}}}}