This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new 4b31978481 Cache file add module name (#10338)
4b31978481 is described below
commit 4b3197848167f469eb0e539c696f1cabe28f6a0c
Author: Owen.Cai <[email protected]>
AuthorDate: Mon Aug 1 11:01:57 2022 +0800
Cache file add module name (#10338)
* cache_file_add_module_name & support config file cache
* add application name for file name
* fix compile bug & meta report support file cache
* meta report support config file cache
* fix compile bug
* change param name to enableFileCached
* rename from enableFileCached to enableFileCache
---
.../dubbo/common/cache/FileCacheStoreFactory.java | 11 ++-
.../apache/dubbo/rpc/service/GenericException.java | 81 ++++++++++++++++++++++
.../dubbo/metadata/AbstractCacheManager.java | 4 +-
.../dubbo/metadata/AbstractServiceNameMapping.java | 10 ++-
.../apache/dubbo/metadata/MappingCacheManager.java | 4 +-
.../metadata/report/MetadataReportInstance.java | 2 +
.../report/support/AbstractMetadataReport.java | 5 +-
.../registry/client/AbstractServiceDiscovery.java | 18 ++++-
.../client/metadata/store/MetaCacheManager.java | 6 +-
9 files changed, 127 insertions(+), 14 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStoreFactory.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStoreFactory.java
index 4dbacaf6ec..f4375c7210 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStoreFactory.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStoreFactory.java
@@ -59,6 +59,10 @@ public class FileCacheStoreFactory {
}});
public static FileCacheStore getInstance(String basePath, String
cacheName) {
+ return getInstance(basePath, cacheName, true);
+ }
+
+ public static FileCacheStore getInstance(String basePath, String
cacheName, boolean enableFileCache) {
if (basePath == null) {
basePath = System.getProperty("user.home") + File.separator +
".dubbo";
}
@@ -79,7 +83,7 @@ public class FileCacheStoreFactory {
String cacheFilePath = basePath + File.separator + cacheName;
- return cacheMap.computeIfAbsent(cacheFilePath, (k) -> getFile(k));
+ return cacheMap.computeIfAbsent(cacheFilePath, (k) -> getFile(k,
enableFileCache));
}
/**
@@ -109,7 +113,10 @@ public class FileCacheStoreFactory {
* @param name the file name
* @return a file object
*/
- private static FileCacheStore getFile(String name) {
+ private static FileCacheStore getFile(String name, boolean
enableFileCache) {
+ if(!enableFileCache) {
+ return FileCacheStore.Empty.getInstance(name);
+ }
try {
FileCacheStore.Builder builder = FileCacheStore.newBuilder();
tryFileLock(builder, name);
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/service/GenericException.java
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/service/GenericException.java
index 0d3d784e82..9f99ecac54 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/service/GenericException.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/service/GenericException.java
@@ -16,8 +16,12 @@
*/
package org.apache.dubbo.rpc.service;
+import org.apache.dubbo.common.utils.JsonUtils;
import org.apache.dubbo.common.utils.StringUtils;
+import java.beans.Transient;
+
+
/**
* GenericException
*
@@ -27,33 +31,48 @@ public class GenericException extends RuntimeException {
private static final long serialVersionUID = -1182299763306599962L;
+ private boolean useCause;
+
private String exceptionClass;
private String exceptionMessage;
public GenericException() {
+ this.useCause = false;
}
public GenericException(String exceptionClass, String exceptionMessage) {
super(exceptionMessage);
+ this.useCause = false;
+ this.exceptionClass = exceptionClass;
+ this.exceptionMessage = exceptionMessage;
+ }
+
+ public GenericException(String exceptionClass, String exceptionMessage,
String message) {
+ super(message);
+ this.useCause = false;
this.exceptionClass = exceptionClass;
this.exceptionMessage = exceptionMessage;
}
public GenericException(Throwable cause) {
super(StringUtils.toString(cause));
+ this.useCause = false;
this.exceptionClass = cause.getClass().getName();
this.exceptionMessage = cause.getMessage();
}
+ @Transient
public String getExceptionClass() {
return exceptionClass;
}
+
public void setExceptionClass(String exceptionClass) {
this.exceptionClass = exceptionClass;
}
+ @Transient
public String getExceptionMessage() {
return exceptionMessage;
}
@@ -62,4 +81,66 @@ public class GenericException extends RuntimeException {
this.exceptionMessage = exceptionMessage;
}
+ @Override
+ public String getMessage() {
+ if(this.useCause) {
+ Throwable cause = getCause();
+ if(cause != null) {
+ return cause.getMessage();
+ }
+ }
+ GenericExceptionInfo genericExceptionInfo = new
GenericExceptionInfo(exceptionClass, exceptionMessage, getMessage());
+ return JsonUtils.getJson().toJson(genericExceptionInfo);
+ }
+
+ public void setMessage(String json) {
+ GenericExceptionInfo info = JsonUtils.getJson().toJavaObject(json,
GenericExceptionInfo.class);
+ this.useCause = true;
+ initCause(new GenericException(info.getExClass(), info.getExMsg(),
info.getMsg()));
+ }
+
+ @Override
+ @Transient
+ public String getLocalizedMessage() {
+ return getMessage();
+ }
+
+ /**
+ * create generic exception info
+ */
+ public static class GenericExceptionInfo {
+ private String exClass;
+ private String exMsg;
+ private String msg;
+
+ public GenericExceptionInfo(String exceptionClass, String
exceptionMessage, String message) {
+ this.exClass = exceptionClass;
+ this.exMsg = exceptionMessage;
+ this.msg = message;
+ }
+
+ public String getMsg() {
+ return msg;
+ }
+
+ public String getExClass() {
+ return exClass;
+ }
+
+ public String getExMsg() {
+ return exMsg;
+ }
+
+ public void setExClass(String exClass) {
+ this.exClass = exClass;
+ }
+
+ public void setExMsg(String exMsg) {
+ this.exMsg = exMsg;
+ }
+
+ public void setMsg(String msg) {
+ this.msg = msg;
+ }
+ }
}
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 6f0277731f..4863f3dd6e 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
@@ -40,11 +40,11 @@ public abstract class AbstractCacheManager<V> implements
Disposable {
protected FileCacheStore cacheStore;
protected LRUCache<String, V> cache;
- protected void init(String filePath, String fileName, int entrySize, long
fileSize, int interval, ScheduledExecutorService executorService) {
+ protected void init(boolean enableFileCache, String filePath, String
fileName, int entrySize, long fileSize, int interval, ScheduledExecutorService
executorService) {
this.cache = new LRUCache<>(entrySize);
try {
- cacheStore = FileCacheStoreFactory.getInstance(filePath, fileName);
+ cacheStore = FileCacheStoreFactory.getInstance(filePath, fileName,
enableFileCache);
Map<String, String> properties = cacheStore.loadCache(entrySize);
logger.info("Successfully loaded mapping cache from file " +
fileName + ", entries " + properties.size());
for (Map.Entry<String, String> entry : properties.entrySet()) {
diff --git
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractServiceNameMapping.java
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractServiceNameMapping.java
index 3e47b8e417..534f00063a 100644
---
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractServiceNameMapping.java
+++
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractServiceNameMapping.java
@@ -22,6 +22,7 @@ import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ScopeModelAware;
@@ -29,6 +30,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
@@ -60,7 +62,13 @@ public abstract class AbstractServiceNameMapping implements
ServiceNameMapping,
public AbstractServiceNameMapping(ApplicationModel applicationModel) {
this.applicationModel = applicationModel;
- this.mappingCacheManager = new MappingCacheManager("",
+ boolean enableFileCache = true;
+ Optional<ApplicationConfig> application =
applicationModel.getApplicationConfigManager().getApplication();
+ if(application.isPresent()) {
+ enableFileCache =
Boolean.TRUE.equals(application.get().getEnableFileCache()) ? true : false;
+ }
+ this.mappingCacheManager = new MappingCacheManager(enableFileCache,
+ applicationModel.tryGetApplicationName(),
applicationModel.getFrameworkModel().getBeanFactory()
.getBean(FrameworkExecutorRepository.class).getCacheRefreshingScheduledExecutor());
}
diff --git
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingCacheManager.java
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingCacheManager.java
index e5c04035d2..90d44b3362 100644
---
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingCacheManager.java
+++
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MappingCacheManager.java
@@ -36,7 +36,7 @@ public class MappingCacheManager extends
AbstractCacheManager<Set<String>> {
return
scopeModel.getBeanFactory().getOrRegisterBean(MappingCacheManager.class);
}
- public MappingCacheManager(String name, ScheduledExecutorService
executorService) {
+ public MappingCacheManager(boolean enableFileCache, String name,
ScheduledExecutorService executorService) {
String filePath = System.getProperty("dubbo.mapping.cache.filePath");
String fileName = System.getProperty("dubbo.mapping.cache.fileName");
if (StringUtils.isEmpty(fileName)) {
@@ -54,7 +54,7 @@ public class MappingCacheManager extends
AbstractCacheManager<Set<String>> {
String rawMaxFileSize =
System.getProperty("dubbo.mapping.cache.maxFileSize");
long maxFileSize = StringUtils.parseLong(rawMaxFileSize);
- init(filePath, fileName, entrySize, maxFileSize, 50, executorService);
+ init(enableFileCache, filePath, fileName, entrySize, maxFileSize, 50,
executorService);
}
@Override
diff --git
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/MetadataReportInstance.java
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/MetadataReportInstance.java
index be4664d333..98ed48bb76 100644
---
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/MetadataReportInstance.java
+++
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/MetadataReportInstance.java
@@ -29,6 +29,7 @@ import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import static
org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
+import static
org.apache.dubbo.common.constants.CommonConstants.REGISTRY_LOCAL_FILE_CACHE_ENABLED;
import static
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_DIRECTORY;
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
import static
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE;
@@ -90,6 +91,7 @@ public class MetadataReportInstance implements Disposable {
.build();
}
url = url.addParameterIfAbsent(APPLICATION_KEY,
applicationModel.getCurrentConfig().getName());
+ url = url.addParameterIfAbsent(REGISTRY_LOCAL_FILE_CACHE_ENABLED,
String.valueOf(applicationModel.getCurrentConfig().getEnableFileCache()));
String relatedRegistryId = isEmpty(config.getRegistry()) ?
(isEmpty(config.getId()) ? DEFAULT_KEY : config.getId()) : config.getRegistry();
// RegistryConfig registryConfig =
applicationModel.getConfigManager().getRegistry(relatedRegistryId)
// .orElseThrow(() -> new IllegalStateException("Registry id "
+ relatedRegistryId + " does not exist."));
diff --git
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
index e24e273739..9453f4b287 100644
---
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
+++
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
@@ -61,6 +61,7 @@ import static
org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static
org.apache.dubbo.common.constants.CommonConstants.CYCLE_REPORT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.FILE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
+import static
org.apache.dubbo.common.constants.CommonConstants.REGISTRY_LOCAL_FILE_CACHE_ENABLED;
import static
org.apache.dubbo.common.constants.CommonConstants.REPORT_DEFINITION_KEY;
import static
org.apache.dubbo.common.constants.CommonConstants.REPORT_METADATA_KEY;
import static
org.apache.dubbo.common.constants.CommonConstants.RETRY_PERIOD_KEY;
@@ -103,13 +104,15 @@ public abstract class AbstractMetadataReport implements
MetadataReport {
public AbstractMetadataReport(URL reportServerURL) {
setUrl(reportServerURL);
+
+ boolean localCacheEnabled =
reportServerURL.getParameter(REGISTRY_LOCAL_FILE_CACHE_ENABLED, true);
// Start file save timer
String defaultFilename = System.getProperty(USER_HOME) +
DUBBO_METADATA +
reportServerURL.getApplication() + "-" +
replace(reportServerURL.getAddress(), ":", "-") + CACHE;
String filename = reportServerURL.getParameter(FILE_KEY,
defaultFilename);
File file = null;
- if (ConfigUtils.isNotEmpty(filename)) {
+ if (localCacheEnabled && ConfigUtils.isNotEmpty(filename)) {
file = new File(filename);
if (!file.exists() && file.getParentFile() != null &&
!file.getParentFile().exists()) {
if (!file.getParentFile().mkdirs()) {
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 cbcb8c6dd1..0b3fea6ccd 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
@@ -21,6 +21,7 @@ import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
import org.apache.dubbo.common.utils.ConcurrentHashSet;
+import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.metadata.MetadataInfo;
import org.apache.dubbo.metadata.report.MetadataReport;
import org.apache.dubbo.metadata.report.MetadataReportInstance;
@@ -33,9 +34,11 @@ import
org.apache.dubbo.registry.client.metadata.store.MetaCacheManager;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.List;
+import java.util.Optional;
import java.util.Set;
import static
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE;
+import static
org.apache.dubbo.common.constants.CommonConstants.REGISTRY_LOCAL_FILE_CACHE_ENABLED;
import static
org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE;
import static
org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
import static org.apache.dubbo.metadata.RevisionResolver.EMPTY_REVISION;
@@ -81,7 +84,8 @@ public abstract class AbstractServiceDiscovery implements
ServiceDiscovery {
this.registryURL = registryURL;
this.serviceName = serviceName;
this.metadataInfo = new MetadataInfo(serviceName);
- this.metaCacheManager = new MetaCacheManager(getCacheNameSuffix(),
+ boolean localCacheEnabled =
registryURL.getParameter(REGISTRY_LOCAL_FILE_CACHE_ENABLED, true);
+ this.metaCacheManager = new MetaCacheManager(localCacheEnabled,
getCacheNameSuffix(),
applicationModel.getFrameworkModel().getBeanFactory()
.getBean(FrameworkExecutorRepository.class).getCacheRefreshingScheduledExecutor());
}
@@ -299,10 +303,18 @@ public abstract class AbstractServiceDiscovery implements
ServiceDiscovery {
if (i != -1) {
name = name.substring(0, i);
}
+ StringBuilder stringBuilder = new StringBuilder(128);
+ Optional<ApplicationConfig> application =
applicationModel.getApplicationConfigManager().getApplication();
+ if(application.isPresent()) {
+ stringBuilder.append(application.get().getName());
+ stringBuilder.append(".");
+ }
+ stringBuilder.append(name.toLowerCase());
URL url = this.getUrl();
if (url != null) {
- return name.toLowerCase() + url.getBackupAddress();
+ stringBuilder.append(".");
+ stringBuilder.append(url.getBackupAddress());
}
- return name.toLowerCase();
+ return stringBuilder.toString();
}
}
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 ce41e7059c..3a4991f7fe 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
@@ -35,7 +35,7 @@ public class MetaCacheManager extends
AbstractCacheManager<MetadataInfo> {
return
scopeModel.getBeanFactory().getOrRegisterBean(MetaCacheManager.class);
}
- public MetaCacheManager(String registryName, ScheduledExecutorService
executorService) {
+ public MetaCacheManager(boolean enableFileCache, String registryName,
ScheduledExecutorService executorService) {
String filePath = System.getProperty("dubbo.meta.cache.filePath");
String fileName = System.getProperty("dubbo.meta.cache.fileName");
if (StringUtils.isEmpty(fileName)) {
@@ -53,12 +53,12 @@ public class MetaCacheManager extends
AbstractCacheManager<MetadataInfo> {
String rawMaxFileSize =
System.getProperty("dubbo.meta.cache.maxFileSize");
long maxFileSize = StringUtils.parseLong(rawMaxFileSize);
- init(filePath, fileName, entrySize, maxFileSize, 60, executorService);
+ init(enableFileCache, filePath, fileName, entrySize, maxFileSize, 60,
executorService);
}
// for unit test only
public MetaCacheManager() {
- this("", null);
+ this(true, "", null);
}
@Override