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 2021/09/20 15:16:08 UTC

[dubbo] branch 3.0 updated: [3.0] do not show the warning at first replaceWithLazyClient() (#8814)

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 0f9f1ba  [3.0] do not show the warning at first replaceWithLazyClient() (#8814)
0f9f1ba is described below

commit 0f9f1ba325d45d242e3f7fed1db70a3a92c79cb4
Author: zrlw <zr...@sina.com>
AuthorDate: Mon Sep 20 23:15:55 2021 +0800

    [3.0] do not show the warning at first replaceWithLazyClient() (#8814)
    
    * not warning at first replaceWithLazyClient()
    
    * fix UT test_counter_error
    
    * fix LazyConnectExchangeClient
    
    * fix constant name
    
    * change warning period variable name
    
    * change variable name warningPeriod
---
 .../org/apache/dubbo/rpc/protocol/dubbo/Constants.java   |  7 +++++++
 .../rpc/protocol/dubbo/LazyConnectExchangeClient.java    | 12 +++++-------
 .../rpc/protocol/dubbo/ReferenceCountExchangeClient.java | 16 ++++++++--------
 .../protocol/dubbo/ReferenceCountExchangeClientTest.java | 11 +++++++----
 4 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/Constants.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/Constants.java
index 6ab8e40..23a9829 100644
--- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/Constants.java
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/Constants.java
@@ -60,6 +60,13 @@ public interface Constants {
      */
     boolean DEFAULT_LAZY_CONNECT_INITIAL_STATE = true;
 
+    /**
+     * when this warning rises from invocation, program probably have bug.
+     */
+    String LAZY_REQUEST_WITH_WARNING_KEY = "lazyclient_request_with_warning";
+
+    boolean DEFAULT_LAZY_REQUEST_WITH_WARNING = false;
+
     String OPTIMIZER_KEY = "optimizer";
 
     String ON_CONNECT_KEY = "onconnect";
diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java
index 2947e7e..94e1fd1 100644
--- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java
@@ -36,7 +36,9 @@ import java.util.concurrent.locks.ReentrantLock;
 
 import static org.apache.dubbo.remoting.Constants.SEND_RECONNECT_KEY;
 import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DEFAULT_LAZY_CONNECT_INITIAL_STATE;
+import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DEFAULT_LAZY_REQUEST_WITH_WARNING;
 import static org.apache.dubbo.rpc.protocol.dubbo.Constants.LAZY_CONNECT_INITIAL_STATE_KEY;
+import static org.apache.dubbo.rpc.protocol.dubbo.Constants.LAZY_REQUEST_WITH_WARNING_KEY;
 
 /**
  * dubbo protocol support class.
@@ -44,16 +46,12 @@ import static org.apache.dubbo.rpc.protocol.dubbo.Constants.LAZY_CONNECT_INITIAL
 @SuppressWarnings("deprecation")
 final class LazyConnectExchangeClient implements ExchangeClient {
 
-    /**
-     * when this warning rises from invocation, program probably have bug.
-     */
-    protected static final String REQUEST_WITH_WARNING_KEY = "lazyclient_request_with_warning";
     private final static Logger logger = LoggerFactory.getLogger(LazyConnectExchangeClient.class);
     protected final boolean requestWithWarning;
     private final URL url;
     private final ExchangeHandler requestHandler;
     private final Lock connectLock = new ReentrantLock();
-    private final int warning_period = 5000;
+    private final int warningPeriod = 5000;
     /**
      * lazy connect, initial state for connection
      */
@@ -66,7 +64,7 @@ final class LazyConnectExchangeClient implements ExchangeClient {
         this.url = url.addParameter(SEND_RECONNECT_KEY, Boolean.TRUE.toString());
         this.requestHandler = requestHandler;
         this.initialState = url.getParameter(LAZY_CONNECT_INITIAL_STATE_KEY, DEFAULT_LAZY_CONNECT_INITIAL_STATE);
-        this.requestWithWarning = url.getParameter(REQUEST_WITH_WARNING_KEY, false);
+        this.requestWithWarning = url.getParameter(LAZY_REQUEST_WITH_WARNING_KEY, DEFAULT_LAZY_REQUEST_WITH_WARNING);
     }
 
     private void initClient() throws RemotingException {
@@ -134,7 +132,7 @@ final class LazyConnectExchangeClient implements ExchangeClient {
      */
     private void warning() {
         if (requestWithWarning) {
-            if (warningcount.get() % warning_period == 0) {
+            if (warningcount.get() % warningPeriod == 0) {
                 logger.warn(url.getAddress() + " " + url.getServiceKey() + " safe guard client , should not be called ,must have a bug.");
             }
             warningcount.incrementAndGet();
diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java
index 53ea913..ac222de 100644
--- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClient.java
@@ -44,7 +44,7 @@ final class ReferenceCountExchangeClient implements ExchangeClient {
     private final URL url;
     private final AtomicInteger referenceCount = new AtomicInteger(0);
     private final AtomicInteger disconnectCount = new AtomicInteger(0);
-    private final Integer maxDisconnectCount = 50;
+    private final Integer warningPeriod = 50;
     private ExchangeClient client;
 
     public ReferenceCountExchangeClient(ExchangeClient client) {
@@ -197,13 +197,8 @@ final class ReferenceCountExchangeClient implements ExchangeClient {
      * @return
      */
     private void replaceWithLazyClient() {
-        // this is a defensive operation to avoid client is closed by accident, the initial state of the client is false
-        URL lazyUrl = url.addParameter(LAZY_CONNECT_INITIAL_STATE_KEY, Boolean.TRUE)
-                //.addParameter(RECONNECT_KEY, Boolean.FALSE)
-                .addParameter(SEND_RECONNECT_KEY, Boolean.TRUE.toString());
-        //.addParameter(LazyConnectExchangeClient.REQUEST_WITH_WARNING_KEY, true);
-
-        if (disconnectCount.getAndIncrement() % maxDisconnectCount == 0) {
+        // start warning at second replaceWithLazyClient()
+        if (disconnectCount.getAndIncrement() % warningPeriod == 1) {
             logger.warn(url.getAddress() + " " + url.getServiceKey() + " safe guard client , should not be called ,must have a bug.");
         }
 
@@ -211,6 +206,11 @@ final class ReferenceCountExchangeClient implements ExchangeClient {
          * the order of judgment in the if statement cannot be changed.
          */
         if (!(client instanceof LazyConnectExchangeClient) || client.isClosed()) {
+            // this is a defensive operation to avoid client is closed by accident, the initial state of the client is false
+            URL lazyUrl = url.addParameter(LAZY_CONNECT_INITIAL_STATE_KEY, Boolean.TRUE)
+                    //.addParameter(RECONNECT_KEY, Boolean.FALSE)
+                    .addParameter(SEND_RECONNECT_KEY, Boolean.TRUE.toString());
+            //.addParameter(LazyConnectExchangeClient.REQUEST_WITH_WARNING_KEY, true);
             client = new LazyConnectExchangeClient(lazyUrl, client.getExchangeHandler());
         }
     }
diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java
index 7d2c543..d15eb48 100644
--- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java
@@ -42,9 +42,9 @@ import java.util.List;
 import java.util.Objects;
 
 import static org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY;
+import static org.apache.dubbo.rpc.protocol.dubbo.Constants.LAZY_REQUEST_WITH_WARNING_KEY;
 import static org.apache.dubbo.rpc.protocol.dubbo.Constants.SHARE_CONNECTIONS_KEY;
 
-
 public class ReferenceCountExchangeClientTest {
 
     public static ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
@@ -164,7 +164,7 @@ public class ReferenceCountExchangeClientTest {
         Assertions.assertEquals("hello", helloService.hello());
         Assertions.assertEquals(0, LogUtil.findMessage(errorMsg), "should not warning message");
 
-        // generally a client can only be closed once, here it is closed twice, counter is incorrect
+        // close twice, counter counts down from 1 to 0, no warning occurs
         client.close();
 
         // wait close done.
@@ -211,8 +211,11 @@ public class ReferenceCountExchangeClientTest {
         Assertions.assertTrue(shareConnections >= 1);
 
         int port = NetUtils.getAvailablePort();
-        URL demoUrl = URL.valueOf("dubbo://127.0.0.1:" + port + "/demo?" + CONNECTIONS_KEY + "=" + connections + "&" + SHARE_CONNECTIONS_KEY + "=" + shareConnections);
-        URL helloUrl = URL.valueOf("dubbo://127.0.0.1:" + port + "/hello?" + CONNECTIONS_KEY + "=" + connections + "&" + SHARE_CONNECTIONS_KEY + "=" + shareConnections);
+        String params = CONNECTIONS_KEY + "=" + connections
+                + "&" + SHARE_CONNECTIONS_KEY + "=" + shareConnections
+                + "&" + LAZY_REQUEST_WITH_WARNING_KEY + "=" + "true";
+        URL demoUrl = URL.valueOf("dubbo://127.0.0.1:" + port + "/demo?" + params);
+        URL helloUrl = URL.valueOf("dubbo://127.0.0.1:" + port + "/hello?" + params);
 
         demoExporter = export(new DemoServiceImpl(), IDemoService.class, demoUrl);
         helloExporter = export(new HelloServiceImpl(), IHelloService.class, helloUrl);