Skip to content

Commit 403e127

Browse files
authored
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.
1 parent 90f49e5 commit 403e127

File tree

5 files changed

+59
-14
lines changed

5 files changed

+59
-14
lines changed

dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractCacheManager.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ protected void init(
6565
for (Map.Entry<String, String> entry : properties.entrySet()) {
6666
String key = entry.getKey();
6767
String value = entry.getValue();
68-
this.cache.put(key, toValueType(value));
68+
V v = toValueType(value);
69+
put(key, v);
6970
}
7071
// executorService can be empty if FileCacheStore fails
7172
if (executorService == null) {
@@ -89,12 +90,18 @@ protected void init(
8990

9091
protected abstract String getName();
9192

93+
protected boolean validate(String key, V value) {
94+
return value != null;
95+
}
96+
9297
public V get(String key) {
9398
return cache.get(key);
9499
}
95100

96101
public void put(String key, V apps) {
97-
cache.put(key, apps);
102+
if (validate(key, apps)) {
103+
cache.put(key, apps);
104+
}
98105
}
99106

100107
public V remove(String key) {
@@ -120,7 +127,7 @@ public Map<String, V> getAll() {
120127

121128
public void update(Map<String, V> newCache) {
122129
for (Map.Entry<String, V> entry : newCache.entrySet()) {
123-
cache.put(entry.getKey(), entry.getValue());
130+
put(entry.getKey(), entry.getValue());
124131
}
125132
}
126133

dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataInfo.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ public synchronized String calAndGetRevision() {
193193
if (CollectionUtils.isEmptyMap(services)) {
194194
this.revision = EMPTY_REVISION;
195195
} else {
196-
StringBuilder sb = new StringBuilder();
197-
sb.append(app);
198-
for (Map.Entry<String, ServiceInfo> entry : new TreeMap<>(services).entrySet()) {
199-
sb.append(entry.getValue().toDescString());
200-
}
201-
String tempRevision = RevisionResolver.calRevision(sb.toString());
196+
String tempRevision = calRevision();
202197
if (!StringUtils.isEquals(this.revision, tempRevision)) {
203198
if (logger.isInfoEnabled()) {
204199
logger.info(String.format(
@@ -212,6 +207,15 @@ public synchronized String calAndGetRevision() {
212207
return revision;
213208
}
214209

210+
public synchronized String calRevision() {
211+
StringBuilder sb = new StringBuilder();
212+
sb.append(app);
213+
for (Map.Entry<String, ServiceInfo> entry : new TreeMap<>(services).entrySet()) {
214+
sb.append(entry.getValue().toDescString());
215+
}
216+
return RevisionResolver.calRevision(sb.toString());
217+
}
218+
215219
public void setRevision(String revision) {
216220
this.revision = revision;
217221
}

dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManager.java

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.dubbo.metadata.MetadataInfo;
2424
import org.apache.dubbo.rpc.model.ScopeModel;
2525

26+
import java.util.Objects;
2627
import java.util.concurrent.ScheduledExecutorService;
2728

2829
import static org.apache.dubbo.common.constants.CommonConstants.DubboProperty.DUBBO_META_CACHE_ENTRYSIZE;
@@ -76,4 +77,13 @@ protected MetadataInfo toValueType(String value) {
7677
protected String getName() {
7778
return "meta";
7879
}
80+
81+
@Override
82+
protected boolean validate(String key, MetadataInfo value) {
83+
if (!super.validate(key, value)) {
84+
return false;
85+
}
86+
String revision = value.calRevision();
87+
return Objects.equals(key, revision);
88+
}
7989
}

dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/store/MetaCacheManagerTest.java

+28-5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import org.junit.jupiter.api.Test;
3333

3434
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3536
import static org.junit.jupiter.api.Assertions.assertNotNull;
3637
import static org.junit.jupiter.api.Assertions.assertNull;
38+
import static org.junit.jupiter.api.Assertions.assertTrue;
3739
import static org.junit.jupiter.api.Assertions.fail;
3840

3941
class MetaCacheManagerTest {
@@ -66,24 +68,44 @@ void testCache() {
6668
// cacheManager.setExtensionAccessor(extensionAccessor);
6769

6870
MetadataInfo metadataInfo = cacheManager.get("1");
69-
assertNotNull(metadataInfo);
70-
assertEquals("demo", metadataInfo.getApp());
71+
assertNull(metadataInfo);
7172
metadataInfo = cacheManager.get("2");
7273
assertNull(metadataInfo);
7374

75+
metadataInfo = cacheManager.get("065787862412c2cc0a1b9577bc194c9a");
76+
assertNotNull(metadataInfo);
77+
assertEquals("demo", metadataInfo.getApp());
78+
7479
Map<String, MetadataInfo> newMetadatas = new HashMap<>();
7580
MetadataInfo metadataInfo2 = JsonUtils.toJavaObject(
7681
"{\"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",
7782
MetadataInfo.class);
83+
assertNotEquals("2", metadataInfo2.calRevision());
7884
newMetadatas.put("2", metadataInfo2);
7985

86+
MetadataInfo metadataInfo3 = JsonUtils.toJavaObject(
87+
"{\"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",
88+
MetadataInfo.class);
89+
assertEquals("84f10ebf1226b496c9ff102f311918e4", metadataInfo3.calRevision());
90+
newMetadatas.put("84f10ebf1226b496c9ff102f311918e4", metadataInfo3);
91+
8092
cacheManager.update(newMetadatas);
8193
metadataInfo = cacheManager.get("1");
94+
assertNull(metadataInfo);
95+
96+
metadataInfo = cacheManager.get("065787862412c2cc0a1b9577bc194c9a");
8297
assertNotNull(metadataInfo);
8398
assertEquals("demo", metadataInfo.getApp());
99+
84100
metadataInfo = cacheManager.get("2");
101+
assertNull(metadataInfo);
102+
103+
metadataInfo = cacheManager.get("84f10ebf1226b496c9ff102f311918e4");
85104
assertNotNull(metadataInfo);
86-
assertEquals("demo2", metadataInfo.getApp());
105+
assertEquals("demo3", metadataInfo.getApp());
106+
assertTrue(metadataInfo
107+
.getServices()
108+
.containsKey("greeting/org.apache.dubbo.registry.service.DemoService3:1.0.0:dubbo"));
87109
} finally {
88110
cacheManager.destroy();
89111
}
@@ -97,7 +119,8 @@ void testCacheDump() {
97119
MetadataInfo.class);
98120
MetaCacheManager cacheManager = new MetaCacheManager();
99121
try {
100-
cacheManager.put("3", metadataInfo3);
122+
assertEquals("97370ff779b6b6ebb7012bae61710de2", metadataInfo3.calRevision());
123+
cacheManager.put("97370ff779b6b6ebb7012bae61710de2", metadataInfo3);
101124

102125
try {
103126
MetaCacheManager.CacheRefreshTask<MetadataInfo> task = new MetaCacheManager.CacheRefreshTask<>(
@@ -112,7 +135,7 @@ void testCacheDump() {
112135
MetaCacheManager newCacheManager = null;
113136
try {
114137
newCacheManager = new MetaCacheManager();
115-
MetadataInfo metadataInfo = newCacheManager.get("3");
138+
MetadataInfo metadataInfo = newCacheManager.get("97370ff779b6b6ebb7012bae61710de2");
116139
assertNotNull(metadataInfo);
117140
assertEquals("demo3", metadataInfo.getApp());
118141
} finally {
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
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"}}}}
2+
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"}}}}

0 commit comments

Comments
 (0)