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:15:32 UTC
[dubbo] branch master updated: [master] do not show the warning at
first replaceWithLazyClient() (#8815)
This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new e39e494 [master] do not show the warning at first replaceWithLazyClient() (#8815)
e39e494 is described below
commit e39e4945c2f2b1ed3d6a457020e783a9a09ed288
Author: zrlw <zr...@sina.com>
AuthorDate: Mon Sep 20 23:15:23 2021 +0800
[master] do not show the warning at first replaceWithLazyClient() (#8815)
* not warning at first replaceWithLazyClient()
* fix UT test_counter_error
* fix LazyConnectExchangeClient
* fix ReferenceCountExchangeClientTest
* 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 | 10 +++++++---
4 files changed, 27 insertions(+), 18 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 681915a..07b091e 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 static final 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 80c4c99..31ffaa1 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 b5e8066..b95315c 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
@@ -43,6 +43,7 @@ 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;
@@ -166,7 +167,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.
@@ -213,8 +214,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);