You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/07/25 01:18:30 UTC

[dubbo] branch 3.1 updated: Adding error code to multiple classes in registry module. (#10360)

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

albumenj pushed a commit to branch 3.1
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.1 by this push:
     new 88f17daf06 Adding error code to multiple classes in registry module. (#10360)
88f17daf06 is described below

commit 88f17daf064c75e72394fa46913bd5ac274f1153
Author: Andy Cheung <wi...@users.noreply.github.com>
AuthorDate: Mon Jul 25 09:18:12 2022 +0800

    Adding error code to multiple classes in registry module. (#10360)
    
    * Replace the logger implementation of class in org.apache.dubbo.registry.client
    
    * Add error code 1-17 in ServiceInstancesChangedListener.
    
    * Returns an empty string when NumberFormatException occurs. (in getErrorUrl in FailsafeErrorTypeAwareLogger)
    
    * Fix typo in MigrationInvoker.
    
    * Replace the logger implementation of ServiceDiscoveryRegistry.
    
    * Add error code 4-2 in ServiceInstanceHostPortCustomizer.
    
    * Add error code 1-18 in StandardMetadataServiceURLBuilder.
    
    * Add error code in ReflectionBasedServiceDiscovery, with adding cause to AbstractRegistry.
    
    * Change meaning of error code 4-2.
---
 .../support/FailsafeErrorTypeAwareLogger.java      |  6 +++++
 .../registry/client/AbstractServiceDiscovery.java  |  4 ++--
 .../client/FileSystemServiceDiscovery.java         |  4 ++--
 .../client/ReflectionBasedServiceDiscovery.java    | 11 ++++++---
 .../registry/client/ServiceDiscoveryRegistry.java  |  6 ++---
 .../listener/ServiceInstancesChangedListener.java  |  6 ++++-
 .../client/metadata/MetadataServiceDelegation.java |  5 ++--
 .../metadata/MetadataServiceNameMapping.java       |  4 ++--
 .../ServiceInstanceHostPortCustomizer.java         |  9 +++++++-
 .../StandardMetadataServiceURLBuilder.java         | 27 ++++++++++++++++------
 .../client/migration/MigrationInvoker.java         |  4 ++--
 .../dubbo/registry/support/AbstractRegistry.java   |  2 +-
 12 files changed, 62 insertions(+), 26 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/support/FailsafeErrorTypeAwareLogger.java b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/support/FailsafeErrorTypeAwareLogger.java
index 1c690ab24d..8438c0ebfd 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/logger/support/FailsafeErrorTypeAwareLogger.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/logger/support/FailsafeErrorTypeAwareLogger.java
@@ -66,6 +66,8 @@ public class FailsafeErrorTypeAwareLogger extends FailsafeLogger implements Erro
         } catch (NumberFormatException numberFormatException) {
             error("Invalid error code: " + code + ", the format of error code is: X-X (where X is a number).",
                 numberFormatException);
+
+            return "";
         }
 
         return String.format(INSTRUCTIONS_URL, errorCodeSegments[0], errorCodeSegments[1]);
@@ -80,6 +82,7 @@ public class FailsafeErrorTypeAwareLogger extends FailsafeLogger implements Erro
         try {
             getLogger().warn(appendContextMessageWithInstructions(code, cause, extendedInformation, msg));
         } catch (Throwable t) {
+            // ignored.
         }
     }
 
@@ -92,6 +95,7 @@ public class FailsafeErrorTypeAwareLogger extends FailsafeLogger implements Erro
         try {
             getLogger().warn(appendContextMessageWithInstructions(code, cause, extendedInformation, msg), e);
         } catch (Throwable t) {
+            // ignored.
         }
     }
 
@@ -104,6 +108,7 @@ public class FailsafeErrorTypeAwareLogger extends FailsafeLogger implements Erro
         try {
             getLogger().error(appendContextMessageWithInstructions(code, cause, extendedInformation, msg));
         } catch (Throwable t) {
+            // ignored.
         }
     }
 
@@ -116,6 +121,7 @@ public class FailsafeErrorTypeAwareLogger extends FailsafeLogger implements Erro
         try {
             getLogger().error(appendContextMessageWithInstructions(code, cause, extendedInformation, msg), e);
         } catch (Throwable t) {
+            // ignored.
         }
     }
 }
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..72fcedc984 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
@@ -17,7 +17,7 @@
 package org.apache.dubbo.registry.client;
 
 import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
 import org.apache.dubbo.common.utils.ConcurrentHashSet;
@@ -48,7 +48,7 @@ import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataU
  * Each service discovery is bond to one application.
  */
 public abstract class AbstractServiceDiscovery implements ServiceDiscovery {
-    private final Logger logger = LoggerFactory.getLogger(AbstractServiceDiscovery.class);
+    private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(AbstractServiceDiscovery.class);
     private volatile boolean isDestroy;
 
     protected final String serviceName;
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/FileSystemServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/FileSystemServiceDiscovery.java
index 0dcb0e67dc..d1d46e1e55 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/FileSystemServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/FileSystemServiceDiscovery.java
@@ -22,7 +22,7 @@
 //import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
 //import org.apache.dubbo.common.config.configcenter.file.FileSystemDynamicConfiguration;
 //import org.apache.dubbo.common.lang.ShutdownHookCallbacks;
-//import org.apache.dubbo.common.logger.Logger;
+//import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 //import org.apache.dubbo.common.logger.LoggerFactory;
 //import org.apache.dubbo.common.utils.StringUtils;
 //
@@ -53,7 +53,7 @@
 // */
 //public class FileSystemServiceDiscovery extends AbstractServiceDiscovery {
 //
-//    private final Logger logger = LoggerFactory.getLogger(getClass());
+//    private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(getClass());
 //
 //    private final Map<File, FileLock> fileLocksCache = new ConcurrentHashMap<>();
 //
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ReflectionBasedServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ReflectionBasedServiceDiscovery.java
index fe5b193d51..f262b5a634 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ReflectionBasedServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ReflectionBasedServiceDiscovery.java
@@ -18,7 +18,7 @@ package org.apache.dubbo.registry.client;
 
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.constants.CommonConstants;
-import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.utils.NamedThreadFactory;
 import org.apache.dubbo.common.utils.NetUtils;
@@ -54,7 +54,7 @@ import java.util.concurrent.TimeUnit;
 
 public class ReflectionBasedServiceDiscovery extends AbstractServiceDiscovery {
 
-    private final Logger logger = LoggerFactory.getLogger(getClass());
+    private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(getClass());
 
     /**
      * Echo check if consumer is still work
@@ -160,7 +160,12 @@ public class ReflectionBasedServiceDiscovery extends AbstractServiceDiscovery {
                 try {
                     entry.getValue().onEvent(metadataString);
                 } catch (RpcException e) {
-                    logger.warn("Notify to consumer error. Possible cause: consumer is offline.");
+                    // 1-7 - Failed to notify registry event.
+                    // The updating of metadata to consumer is a type of registry event.
+
+                    logger.warn("1-7", "consumer is offline", "",
+                        "Notify to consumer error, removing listener.");
+
                     // remove listener if consumer is offline
                     iterator.remove();
                 }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
index a1aaf01cfe..098f6453a5 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
@@ -17,7 +17,7 @@
 package org.apache.dubbo.registry.client;
 
 import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.metadata.AbstractServiceNameMapping;
@@ -66,7 +66,7 @@ import static org.apache.dubbo.registry.client.ServiceDiscoveryFactory.getExtens
  */
 public class ServiceDiscoveryRegistry extends FailbackRegistry {
 
-    protected final Logger logger = LoggerFactory.getLogger(getClass());
+    protected final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(getClass());
 
     private final ServiceDiscovery serviceDiscovery;
 
@@ -348,7 +348,7 @@ public class ServiceDiscoveryRegistry extends FailbackRegistry {
     }
 
     private class DefaultMappingListener implements MappingListener {
-        private final Logger logger = LoggerFactory.getLogger(DefaultMappingListener.class);
+        private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DefaultMappingListener.class);
         private final URL url;
         private Set<String> oldApps;
         private NotifyListener listener;
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListener.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListener.java
index 6ecfaef142..91709c949d 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListener.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/event/listener/ServiceInstancesChangedListener.java
@@ -177,9 +177,13 @@ public class ServiceInstancesChangedListener {
                 retryFuture = scheduler.schedule(new AddressRefreshRetryTask(retryPermission, event.getServiceName()), 10_000L, TimeUnit.MILLISECONDS);
                 logger.warn("Address refresh try task submitted");
             }
+
             // return if all metadata is empty, this notification will not take effect.
             if (emptyNum == revisionToInstances.size()) {
-                logger.error("Address refresh failed because of Metadata Server failure, wait for retry or new address refresh event.");
+                // 1-17 - Address refresh failed.
+                logger.error("1-17", "metadata Server failure", "",
+                    "Address refresh failed because of Metadata Server failure, wait for retry or new address refresh event.");
+
                 return;
             }
         }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceDelegation.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceDelegation.java
index eefc097d36..5cd7556450 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceDelegation.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceDelegation.java
@@ -17,7 +17,7 @@
 package org.apache.dubbo.registry.client.metadata;
 
 import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.resource.Disposable;
 import org.apache.dubbo.common.utils.StringUtils;
@@ -50,7 +50,8 @@ import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty;
  * Implementation providing remote RPC service to facilitate the query of metadata information.
  */
 public class MetadataServiceDelegation implements MetadataService, Disposable {
-    Logger logger = LoggerFactory.getLogger(getClass());
+    ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(getClass());
+
     private final ApplicationModel applicationModel;
     private final RegistryManager registryManager;
     private ConcurrentMap<String, InstanceMetadataChangedListener> instanceMetadataChangedListenerMap = new ConcurrentHashMap<>();
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceNameMapping.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceNameMapping.java
index 2850faa7d3..d8cf5284e2 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceNameMapping.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceNameMapping.java
@@ -18,7 +18,7 @@ package org.apache.dubbo.registry.client.metadata;
 
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.config.configcenter.ConfigItem;
-import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.common.utils.StringUtils;
@@ -40,7 +40,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
 
 public class MetadataServiceNameMapping extends AbstractServiceNameMapping {
 
-    private final Logger logger = LoggerFactory.getLogger(getClass());
+    private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(getClass());
 
     private static final List<String> IGNORED_SERVICE_INTERFACES = Collections.singletonList(MetadataService.class.getName());
 
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceHostPortCustomizer.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceHostPortCustomizer.java
index b5e24bec8c..3b8c193c3f 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceHostPortCustomizer.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceHostPortCustomizer.java
@@ -71,7 +71,14 @@ public class ServiceInstanceHostPortCustomizer implements ServiceInstanceCustomi
                 }
                 
                 if (host == null || port == -1) {
-                    logger.warn("The default preferredProtocol \"" + preferredProtocol + "\" is not found, fall back to the strategy that pick the first found protocol. Please try to modify the config of dubbo.application.protocol");
+
+                    // 4-2 - Can't find an instance URL using the default preferredProtocol.
+
+                    logger.warn("4-2", "typo in preferred protocol", "",
+                        "Can't find an instance URL  using the default preferredProtocol \"" + preferredProtocol + "\", " +
+                        "falling back to the strategy that pick the first found protocol. " +
+                        "Please try modifying the config of dubbo.application.protocol");
+
                     URL url = urls.iterator().next();
                     host = url.getHost();
                     port = url.getPort();
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilder.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilder.java
index f2cc886331..509a85c2f3 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilder.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilder.java
@@ -19,7 +19,7 @@ package org.apache.dubbo.registry.client.metadata;
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.URLBuilder;
 import org.apache.dubbo.common.config.ConfigurationUtils;
-import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.utils.JsonUtils;
 import org.apache.dubbo.metadata.MetadataService;
@@ -58,7 +58,7 @@ import static org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY;
  */
 public class StandardMetadataServiceURLBuilder implements MetadataServiceURLBuilder {
 
-    private final Logger logger = LoggerFactory.getLogger(getClass());
+    private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(getClass());
 
     public static final String NAME = "standard";
 
@@ -111,8 +111,7 @@ public class StandardMetadataServiceURLBuilder implements MetadataServiceURLBuil
             .addParameter(THREADPOOL_KEY, "cached")
             .addParameter(THREADS_KEY, "100")
             .addParameter(CORE_THREADS_KEY, "2")
-                .addParameter(RETRIES_KEY, 0);
-
+            .addParameter(RETRIES_KEY, 0);
 
         // add parameters
         params.forEach(urlBuilder::addParameter);
@@ -125,17 +124,31 @@ public class StandardMetadataServiceURLBuilder implements MetadataServiceURLBuil
     private URL generateUrlWithoutMetadata(String serviceName, String host, Integer instancePort) {
         Integer port = metadataServicePort;
         if (port == null || port < 1) {
-            logger.warn("Metadata Service Port is not provided, since DNS is not able to negotiate the metadata port " +
-                    "between Provider and Consumer, will try to use instance port as the default metadata port.");
+
+            // 1-18 - Metadata Service Port should be specified for consumer.
+
+            logger.warn("1-18", "missing configuration of metadata service port", "",
+                "Metadata Service Port is not provided. Since DNS is not able to negotiate the metadata port " +
+                    "between Provider and Consumer, Dubbo will try using instance port as the default metadata port.");
+
             port = instancePort;
         }
 
         if (port == null || port < 1) {
+
+            // 1-18 - Metadata Service Port should be specified for consumer.
+
             String message = "Metadata Service Port should be specified for consumer. " +
                     "Please set dubbo.application.metadataServicePort and " +
                     "make sure it has been set on provider side. " +
                     "ServiceName: " + serviceName + " Host: " + host;
-            throw new IllegalStateException(message);
+
+            IllegalStateException illegalStateException = new IllegalStateException(message);
+
+            logger.error("1-18", "missing configuration of metadata service port", "",
+                message, illegalStateException);
+
+            throw illegalStateException;
         }
 
         URLBuilder urlBuilder = new URLBuilder()
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/migration/MigrationInvoker.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/migration/MigrationInvoker.java
index 8f41a305d5..fc525e9f87 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/migration/MigrationInvoker.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/migration/MigrationInvoker.java
@@ -255,7 +255,7 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
             try {
                 Thread.sleep(delay * 1000L);
             } catch (InterruptedException e) {
-                logger.error("Interrupter when waiting for address notify!" + e);
+                logger.error("Interrupted when waiting for address notify!" + e);
             }
         } else {
             // do not wait address notify by default
@@ -264,7 +264,7 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
         try {
             latch.await(delay, TimeUnit.SECONDS);
         } catch (InterruptedException e) {
-            logger.error("Interrupter when waiting for address notify!" + e);
+            logger.error("Interrupted when waiting for address notify!" + e);
         }
     }
 
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java
index e7877048ab..170a684f78 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java
@@ -433,7 +433,7 @@ public abstract class AbstractRegistry implements Registry {
                         notify(url, listener, filterEmpty(url, urls));
                     } catch (Throwable t) {
                         // 1-7: Failed to notify registry event.
-                        logger.error("1-7", "", "",
+                        logger.error("1-7", "consumer is offline", "",
                             "Failed to notify registry event, urls: " + urls + ", cause: " + t.getMessage(), t);
                     }
                 }