Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check the md5 of the metadata cache file #15006

Open
wants to merge 11 commits into
base: 3.3
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ protected void init(
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) {
Expand All @@ -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) {
Expand All @@ -120,7 +127,7 @@ public Map<String, V> getAll() {

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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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(
Expand All @@ -212,6 +207,15 @@ public synchronized String calAndGetRevision() {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<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.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();
}
Expand All @@ -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<MetadataInfo> task = new MetaCacheManager.CacheRefreshTask<>(
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}}}}
Loading