You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2021/12/03 01:27:18 UTC

[dubbo] 01/02: fix metadata report and cache

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch 3.0-metadata-refactor
in repository https://gitbox.apache.org/repos/asf/dubbo.git

commit cda07b6656b5a360dbd2e25d7553d02c99dba719
Author: ken.lj <ke...@gmail.com>
AuthorDate: Fri Nov 26 15:10:34 2021 +0800

    fix metadata report and cache
---
 .../java/org/apache/dubbo/common/cache/FileCacheStore.java  |  9 +++++----
 .../org/apache/dubbo/common/config/ConfigurationUtils.java  |  2 +-
 .../main/java/org/apache/dubbo/config/ServiceConfig.java    |  3 ++-
 .../src/main/resources/spring/dubbo-consumer.xml            |  2 +-
 .../java/org/apache/dubbo/metadata/MetadataService.java     |  4 ++++
 .../dubbo/registry/client/AbstractServiceDiscovery.java     | 13 +++++++------
 .../dubbo/registry/client/metadata/MetadataUtils.java       |  2 +-
 .../registry/client/metadata/store/MetaCacheManager.java    |  9 +++++++--
 8 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStore.java b/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStore.java
index efe764d..cbe42f0 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStore.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/cache/FileCacheStore.java
@@ -57,7 +57,7 @@ public class FileCacheStore {
     private FileLock directoryLock;
     private File lockFile;
 
-    public FileCacheStore(String basePath, String fileName) throws IOException {
+    public FileCacheStore(String basePath, String fileName) throws IOException, PathNotExclusiveException {
         if (basePath == null) {
             basePath = System.getProperty("user.home") + "/.dubbo/";
         }
@@ -65,7 +65,7 @@ public class FileCacheStore {
         this.fileName = fileName;
 
         this.cacheFile = getFile(fileName, SUFFIX);
-        if (!cacheFile.exists()) {
+        if (cacheFile != null && !cacheFile.exists()) {
             cacheFile.createNewFile();
         }
     }
@@ -95,7 +95,7 @@ public class FileCacheStore {
         return properties;
     }
 
-    public File getFile(String cacheName, String suffix) {
+    public File getFile(String cacheName, String suffix) throws PathNotExclusiveException {
         cacheName = safeName(cacheName);
         if (!cacheName.endsWith(suffix)) {
             cacheName = cacheName + suffix;
@@ -109,7 +109,7 @@ public class FileCacheStore {
      * @param name the file name
      * @return a file object
      */
-    public File getFile(String name) {
+    public File getFile(String name) throws PathNotExclusiveException {
         synchronized (this) {
             File candidate = basePath;
             // ensure cache store path exists
@@ -123,6 +123,7 @@ public class FileCacheStore {
                 logger.warn("Path '" + basePath
                     + "' is already used by an existing Dubbo process.\n"
                     + "Please specify another one explicitly.");
+                throw e;
             }
         }
 
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/config/ConfigurationUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/config/ConfigurationUtils.java
index 2f79aa2..90feeab 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/config/ConfigurationUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/config/ConfigurationUtils.java
@@ -162,7 +162,7 @@ public class ConfigurationUtils {
     public static Map<String, String> parseProperties(String content) throws IOException {
         Map<String, String> map = new HashMap<>();
         if (StringUtils.isEmpty(content)) {
-            logger.warn("You specified the config center, but there's not even one single config item in it.");
+            logger.warn("Config center was specified, but no config item found.");
         } else {
             Properties properties = new Properties();
             properties.load(new StringReader(content));
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
index d794397..f3fcacb 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
@@ -80,6 +80,7 @@ import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY;
 import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_BIND;
 import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_REGISTRY;
 import static org.apache.dubbo.config.Constants.SCOPE_NONE;
+import static org.apache.dubbo.metadata.MetadataService.isMetadataService;
 import static org.apache.dubbo.remoting.Constants.BIND_IP_KEY;
 import static org.apache.dubbo.remoting.Constants.BIND_PORT_KEY;
 import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
@@ -559,7 +560,7 @@ public class ServiceConfig<T> extends ServiceConfigBase<T> {
             // export to remote if the config is not local (export to local only when config is local)
             if (!SCOPE_LOCAL.equalsIgnoreCase(scope)) {
                 url = exportRemote(url, registryURLs);
-                if (!isGeneric(generic)) {
+                if (!isGeneric(generic) && !isMetadataService(interfaceName)) {
                     ServiceDescriptor descriptor = getScopeModel().getServiceRepository().getService(interfaceName);
                     if (descriptor != null) {
                         MetadataUtils.publishServiceDefinition(getScopeModel().getServiceRepository().getService(interfaceName), getApplicationModel());
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/spring/dubbo-consumer.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/spring/dubbo-consumer.xml
index 88f9253..e8c9521 100644
--- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/spring/dubbo-consumer.xml
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/spring/dubbo-consumer.xml
@@ -26,7 +26,7 @@
 
     <dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
 
-    <dubbo:registry address="zookeeper://127.0.0.1:2181?registry-type=service"/>
+    <dubbo:registry id="demo1" address="zookeeper://127.0.0.1:2181?registry-type=service"/>
 
     <dubbo:reference id="demoService" check="false"
                      interface="org.apache.dubbo.demo.DemoService"/>
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataService.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataService.java
index b5c5d2f..6e48658 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataService.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/MetadataService.java
@@ -186,4 +186,8 @@ public interface MetadataService {
         return unmodifiableSortedSet(stream.map(URL::toFullString).collect(TreeSet::new, Set::add, Set::addAll));
     }
 
+    static boolean isMetadataService(String interfaceName) {
+        return interfaceName != null && interfaceName.equals(MetadataService.class.getName());
+    }
+
 }
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 aa13626..4364e23 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
@@ -31,7 +31,6 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
 
 import java.util.List;
 
-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;
 import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME;
@@ -59,11 +58,13 @@ public abstract class AbstractServiceDiscovery implements ServiceDiscovery {
         this.applicationModel = applicationModel;
         MetadataReportInstance metadataReportInstance = applicationModel.getBeanFactory().getBean(MetadataReportInstance.class);
         metadataType = metadataReportInstance.getMetadataType();
-        if (REMOTE_METADATA_STORAGE_TYPE.equals(metadataReportInstance.getMetadataType())) {
-            this.metadataReport = metadataReportInstance.getMetadataReport(registryURL.getParameter(REGISTRY_CLUSTER_KEY));
-        } else {
-            this.metadataReport = metadataReportInstance.getNopMetadataReport();
-        }
+//        if (REMOTE_METADATA_STORAGE_TYPE.equals(metadataReportInstance.getMetadataType())) {
+//            this.metadataReport = metadataReportInstance.getMetadataReport(registryURL.getParameter(REGISTRY_CLUSTER_KEY));
+//        } else {
+//            this.metadataReport = metadataReportInstance.getNopMetadataReport();
+//        }
+        this.metadataReport = metadataReportInstance.getMetadataReport(registryURL.getParameter(REGISTRY_CLUSTER_KEY));
+
     }
 
     public AbstractServiceDiscovery(String serviceName, URL registryURL) {
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
index 4bfa752..6b3979a 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
@@ -146,7 +146,7 @@ public class MetadataUtils {
                 logger.debug("Instance " + instance.getAddress() + " is using metadata type " + metadataType);
             }
             if (REMOTE_METADATA_STORAGE_TYPE.equals(metadataType)) {
-                MetadataUtils.getMetadata(instance, metadataReport);
+                metadataInfo = MetadataUtils.getMetadata(instance, metadataReport);
             } else {
                 // change the instance used to communicate to avoid all requests route to the same instance
                 MetadataService metadataServiceProxy = MetadataUtils.getMetadataServiceProxy(instance);
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 ddc9213..e6ae867 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
@@ -85,7 +85,7 @@ public class MetaCacheManager implements ScopeModelAware, Disposable {
                 MetadataInfo metadataInfo = JsonUtils.getGson().fromJson(value, MetadataInfo.class);
                 cache.put(key, metadataInfo);
             }
-
+            // executorService can be empty if FileCacheStore fails
             executorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Dubbo-cache-refresh", true));
             executorService.scheduleWithFixedDelay(new CacheRefreshTask(cacheStore, cache), 10, INTERVAL, TimeUnit.MINUTES);
         } catch (Exception e) {
@@ -125,7 +125,12 @@ public class MetaCacheManager implements ScopeModelAware, Disposable {
     }
 
     public void destroy() {
-        executorService.shutdownNow();
+        if (executorService != null) {
+            executorService.shutdownNow();
+        }
+        if (cacheStore != null) {
+            cacheStore.destroy();
+        }
     }
 
     protected static class CacheRefreshTask implements Runnable {