From 11be0c56cfbb030f297a25af0341f12b1b88c778 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Fri, 24 May 2019 20:57:34 +0800 Subject: [PATCH 01/28] commit follow #4148 --- .../main/java/org/apache/dubbo/rpc/AsyncRpcResult.java | 10 ++++++++++ .../apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java | 9 +-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java index fc262475e8..658797d0b6 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java @@ -143,6 +143,16 @@ public class AsyncRpcResult extends AbstractResult { return this; } + public void subscribeTo(CompletableFuture future) { + future.whenComplete((obj, t) -> { + if (t != null) { + this.completeExceptionally(t); + } else { + this.complete((Result) obj); + } + }); + } + @Override public Map getAttachments() { return getAppResponse().getAttachments(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java index f40b664e16..ece71497c5 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java @@ -23,7 +23,6 @@ import org.apache.dubbo.remoting.Constants; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.TimeoutException; import org.apache.dubbo.remoting.exchange.ExchangeClient; -import org.apache.dubbo.rpc.AppResponse; import org.apache.dubbo.rpc.AsyncRpcResult; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -97,13 +96,7 @@ public class DubboInvoker extends AbstractInvoker { } else { AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); CompletableFuture responseFuture = currentClient.request(inv, timeout); - responseFuture.whenComplete((obj, t) -> { - if (t != null) { - asyncRpcResult.completeExceptionally(t); - } else { - asyncRpcResult.complete((AppResponse) obj); - } - }); + asyncRpcResult.subscribeTo(responseFuture); RpcContext.getContext().setFuture(new FutureAdapter(asyncRpcResult)); return asyncRpcResult; } From f5bd795fbd598a0ffd0b8d389747742c96d53d6a Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Mon, 3 Jun 2019 13:00:29 +0800 Subject: [PATCH 02/28] completely remove 'default.' prefix of URL key (#4222) --- .../router/condition/ConditionRouter.java | 11 +++++------ .../main/java/org/apache/dubbo/common/URL.java | 17 ++++++----------- .../org/apache/dubbo/common/URLBuilder.java | 8 +------- .../org/apache/dubbo/config/AbstractConfig.java | 6 +----- 4 files changed, 13 insertions(+), 29 deletions(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java index 87e9b25a00..dbf32fde8d 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java @@ -38,16 +38,15 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.HOST_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.METHOD_KEY; import static org.apache.dubbo.rpc.cluster.Constants.ADDRESS_KEY; import static org.apache.dubbo.rpc.cluster.Constants.FORCE_KEY; import static org.apache.dubbo.rpc.cluster.Constants.PRIORITY_KEY; import static org.apache.dubbo.rpc.cluster.Constants.RULE_KEY; import static org.apache.dubbo.rpc.cluster.Constants.RUNTIME_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX; -import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.METHOD_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.HOST_KEY; /** * ConditionRouter @@ -238,7 +237,7 @@ public class ConditionRouter extends AbstractRouter { } else { sampleValue = sample.get(key); if (sampleValue == null) { - sampleValue = sample.get(DEFAULT_KEY_PREFIX + key); + sampleValue = sample.get(key); } } if (sampleValue != null) { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java index 106bebc70c..11d89cb243 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java @@ -40,20 +40,19 @@ import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; -import static org.apache.dubbo.common.constants.CommonConstants.HOST_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE; import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.HOST_KEY; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PASSWORD_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; import static org.apache.dubbo.common.constants.CommonConstants.USERNAME_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; /** * URL - Uniform Resource Locator (Immutable, ThreadSafe) @@ -492,11 +491,7 @@ class URL implements Serializable { } public String getParameter(String key) { - String value = parameters.get(key); - if (StringUtils.isEmpty(value)) { - value = parameters.get(DEFAULT_KEY_PREFIX + key); - } - return value; + return parameters.get(key); } public String getParameter(String key, String defaultValue) { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java index 5c926f9438..677cbe083f 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java @@ -24,8 +24,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; -import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX; - public final class URLBuilder { private String protocol; @@ -343,10 +341,6 @@ public final class URLBuilder { } public String getParameter(String key) { - String value = parameters.get(key); - if (StringUtils.isEmpty(value)) { - value = parameters.get(DEFAULT_KEY_PREFIX + key); - } - return value; + return parameters.get(key); } } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java index 246694822a..e1018108a5 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java @@ -178,11 +178,7 @@ public abstract class AbstractConfig implements Serializable { str = URL.encode(str); } if (parameter != null && parameter.append()) { - String pre = parameters.get(DEFAULT_KEY + "." + key); - if (pre != null && pre.length() > 0) { - str = pre + "," + str; - } - pre = parameters.get(key); + String pre = parameters.get(key); if (pre != null && pre.length() > 0) { str = pre + "," + str; } From d76748880003613ad8f1c7705c1eab8ef8ac2f1b Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Mon, 3 Jun 2019 13:01:07 +0800 Subject: [PATCH 03/28] performance tuning: avoid address resolve (#4220) --- .../org/apache/dubbo/common/utils/NetUtils.java | 13 ++++++++++++- .../dubbo/rpc/filter/ConsumerContextFilter.java | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index 6fd1a4e27e..6700241dd3 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -37,9 +37,9 @@ import java.util.concurrent.ThreadLocalRandom; import java.util.regex.Pattern; import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE; +import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_IP_TO_BIND; import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_KEY; import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE; -import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_IP_TO_BIND; /** * IP and Port Helper for RPC @@ -180,6 +180,17 @@ public class NetUtils { return address; } + private static volatile String HOST_ADDRESS; + + public static String getHostAddress () { + if (HOST_ADDRESS != null) { + return HOST_ADDRESS; + } + + HOST_ADDRESS = getLocalHost(); + return HOST_ADDRESS; + } + public static String getLocalHost() { InetAddress address = getLocalAddress(); return address == null ? LOCALHOST_VALUE : address.getHostAddress(); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.java index 43353f7e7d..b9b3691dd7 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.java @@ -47,7 +47,7 @@ public class ConsumerContextFilter extends ListenableFilter { RpcContext.getContext() .setInvoker(invoker) .setInvocation(invocation) - .setLocalAddress(NetUtils.getLocalHost(), 0) + .setLocalAddress(NetUtils.getHostAddress(), 0) .setRemoteAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort()); if (invocation instanceof RpcInvocation) { From b5a078f186fd2e7f27a3d8c2af1bce3ff2e88e80 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Mon, 3 Jun 2019 13:03:29 +0800 Subject: [PATCH 04/28] performance tuning: save rpcinvocation allocation (#4215) --- .../cluster/directory/MockDirInvocation.java | 5 + .../router/condition/ConditionRouterTest.java | 32 ++-- .../support/AbstractClusterInvokerTest.java | 8 +- .../com/alibaba/dubbo/rpc/Invocation.java | 5 + .../alibaba/dubbo/rpc/support/RpcUtils.java | 5 - .../apache/dubbo/config/MethodConfigTest.java | 4 +- .../apache/dubbo/service/MockInvocation.java | 5 + .../apache/dubbo/config/AbstractConfig.java | 6 +- .../apache/dubbo/config/ReferenceConfig.java | 29 +--- .../apache/dubbo/config/ServiceConfig.java | 34 ++-- .../apache/dubbo/config/cache/CacheTest.java | 2 +- .../org/apache/dubbo/demo/DemoService.java | 6 + .../dubbo/demo/consumer/Application.java | 8 +- .../dubbo/demo/provider/Application.java | 3 + .../dubbo/demo/provider/DemoServiceImpl.java | 14 ++ .../monitor/support/MonitorFilterTest.java | 10 +- .../monitor/dubbo/DubboMonitorFactory.java | 4 +- .../dubbo/monitor/dubbo/MetricsFilter.java | 2 + .../monitor/dubbo/MetricsFilterTest.java | 16 +- .../org/apache/dubbo/qos/command/impl/Ls.java | 4 +- .../dubbo/qos/command/impl/Offline.java | 4 +- .../apache/dubbo/qos/command/impl/Online.java | 4 +- .../apache/dubbo/qos/command/impl/LsTest.java | 7 +- .../dubbo/qos/command/impl/OfflineTest.java | 5 +- .../dubbo/qos/command/impl/OnlineTest.java | 5 +- .../registry/dubbo/RegistryDirectoryTest.java | 19 ++- .../java/org/apache/dubbo/rpc/Invocation.java | 7 + .../org/apache/dubbo/rpc/RpcInvocation.java | 62 +++++-- .../dubbo/rpc/filter/GenericFilter.java | 2 +- .../dubbo/rpc/filter/GenericImplFilter.java | 3 + .../dubbo/rpc/model/ApplicationModel.java | 53 +++++- .../dubbo/rpc/model/ConsumerMethodModel.java | 161 ------------------ .../apache/dubbo/rpc/model/ConsumerModel.java | 144 +++++++++++----- .../apache/dubbo/rpc/model/MethodModel.java | 83 +++++++++ .../dubbo/rpc/model/ProviderMethodModel.java | 63 ------- .../apache/dubbo/rpc/model/ProviderModel.java | 66 ++----- .../apache/dubbo/rpc/model/ServiceModel.java | 100 +++++++++++ .../dubbo/rpc/proxy/AbstractProxyInvoker.java | 6 +- .../rpc/proxy/InvokerInvocationHandler.java | 2 +- .../apache/dubbo/rpc/support/RpcUtils.java | 18 +- .../dubbo/rpc/filter/ExceptionFilterTest.java | 9 +- .../dubbo/rpc/filter/GenericFilterTest.java | 14 +- .../rpc/filter/GenericImplFilterTest.java | 11 +- .../dubbo/rpc/proxy/AbstractProxyTest.java | 4 +- .../dubbo/rpc/support/MockInvocation.java | 5 + .../dubbo/rpc/support/RpcUtilsTest.java | 27 +-- .../dubbo/DecodeableRpcInvocation.java | 50 ++++-- .../protocol/dubbo/DecodeableRpcResult.java | 9 +- .../dubbo/rpc/protocol/dubbo/DubboCodec.java | 8 +- .../rpc/protocol/dubbo/DubboProtocol.java | 31 ++-- .../protocol/dubbo/filter/FutureFilter.java | 16 +- .../dubbo/telnet/InvokeTelnetHandler.java | 9 +- .../dubbo/telnet/ListTelnetHandler.java | 23 ++- .../protocol/dubbo/ImplicitCallBackTest.java | 13 +- .../telnet/InvokerTelnetHandlerTest.java | 15 +- .../dubbo/telnet/ListTelnetHandlerTest.java | 12 +- .../dubbo/telnet/SelectTelnetHandlerTest.java | 7 +- .../rpc/protocol/rest/RestProtocolTest.java | 5 +- 58 files changed, 698 insertions(+), 586 deletions(-) delete mode 100644 dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerMethodModel.java create mode 100644 dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java delete mode 100644 dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderMethodModel.java create mode 100644 dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java index cc99a87598..3cd3edeb9c 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/directory/MockDirInvocation.java @@ -38,6 +38,11 @@ public class MockDirInvocation implements Invocation { return "echo"; } + @Override + public String getServiceName() { + return "DemoService"; + } + public Class[] getParameterTypes() { return new Class[]{String.class}; } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouterTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouterTest.java index a88a759161..c7ffe66b47 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouterTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouterTest.java @@ -89,7 +89,7 @@ public class ConditionRouterTest { public void testRoute_matchFilter() { List> invokers = new ArrayList>(); Invoker invoker1 = new MockInvoker(URL.valueOf( - "dubbo://10.20.3.3:20880/com.foo.BarService?default.serialization=fastjson")); + "dubbo://10.10.3.3:20880/com.foo.BarService?default.serialization=fastjson")); Invoker invoker2 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); Invoker invoker3 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() @@ -102,19 +102,19 @@ public class ConditionRouterTest { System.err.println(invoker3.getUrl().getAddress()); Router router1 = new ConditionRouterFactory().getRouter(getRouteUrl( - "host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.3").addParameter(FORCE_KEY, + "host = " + NetUtils.getLocalHost() + " => " + " host = 10.10.3.3").addParameter(FORCE_KEY, String.valueOf(true))); Router router2 = new ConditionRouterFactory().getRouter(getRouteUrl( - "host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.* & host != 10.20.3.3").addParameter( + "host = " + NetUtils.getLocalHost() + " => " + " host = 10.10.3.* & host != 10.10.3.3").addParameter( FORCE_KEY, String.valueOf(true))); Router router3 = new ConditionRouterFactory().getRouter(getRouteUrl( - "host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.3 & host != 10.20.3.3").addParameter( + "host = " + NetUtils.getLocalHost() + " => " + " host = 10.10.3.3 & host != 10.10.3.3").addParameter( FORCE_KEY, String.valueOf(true))); Router router4 = new ConditionRouterFactory().getRouter(getRouteUrl( - "host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.2,10.20.3.3,10.20.3.4").addParameter( + "host = " + NetUtils.getLocalHost() + " => " + " host = 10.10.3.2,10.10.3.3,10.10.3.4").addParameter( FORCE_KEY, String.valueOf(true))); Router router5 = new ConditionRouterFactory().getRouter(getRouteUrl( - "host = " + NetUtils.getLocalHost() + " => " + " host != 10.20.3.3").addParameter(FORCE_KEY, + "host = " + NetUtils.getLocalHost() + " => " + " host != 10.10.3.3").addParameter(FORCE_KEY, String.valueOf(true))); Router router6 = new ConditionRouterFactory().getRouter(getRouteUrl( "host = " + NetUtils.getLocalHost() + " => " + " serialization = fastjson").addParameter( @@ -138,7 +138,7 @@ public class ConditionRouterTest { @Test public void testRoute_methodRoute() { - Invocation invocation = new RpcInvocation("getFoo", new Class[0], new Object[0]); + Invocation invocation = new RpcInvocation("getFoo", "com.foo.BarService", new Class[0], new Object[0]); // More than one methods, mismatch Router router = new ConditionRouterFactory().getRouter(getRouteUrl("methods=getFoo => host = 1.2.3.4")); boolean matchWhen = ((ConditionRouter) router).matchWhen( @@ -162,7 +162,7 @@ public class ConditionRouterTest { Assertions.assertTrue(matchWhen); // Test filter condition List> invokers = new ArrayList>(); - Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")); + Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService")); Invoker invoker2 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); Invoker invoker3 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() @@ -172,14 +172,14 @@ public class ConditionRouterTest { invokers.add(invoker3); Router router4 = new ConditionRouterFactory().getRouter(getRouteUrl( - "host = " + NetUtils.getLocalHost() + " & methods = getFoo => " + " host = 10.20.3.3").addParameter( + "host = " + NetUtils.getLocalHost() + " & methods = getFoo => " + " host = 10.10.3.3").addParameter( FORCE_KEY, String.valueOf(true))); List> filteredInvokers1 = router4.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), invocation); Assertions.assertEquals(1, filteredInvokers1.size()); Router router5 = new ConditionRouterFactory().getRouter(getRouteUrl( - "host = " + NetUtils.getLocalHost() + " & methods = unvalidmethod => " + " host = 10.20.3.3") + "host = " + NetUtils.getLocalHost() + " & methods = unvalidmethod => " + " host = 10.10.3.3") .addParameter(FORCE_KEY, String.valueOf(true))); List> filteredInvokers2 = router5.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), invocation); @@ -224,7 +224,7 @@ public class ConditionRouterTest { public void testRoute_HostFilter() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = " + NetUtils.getLocalHost())); List> invokers = new ArrayList>(); - Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")); + Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService")); Invoker invoker2 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); Invoker invoker3 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); invokers.add(invoker1); @@ -240,7 +240,7 @@ public class ConditionRouterTest { public void testRoute_Empty_HostFilter() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl(" => " + " host = " + NetUtils.getLocalHost())); List> invokers = new ArrayList>(); - Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")); + Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService")); Invoker invoker2 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); Invoker invoker3 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); invokers.add(invoker1); @@ -256,7 +256,7 @@ public class ConditionRouterTest { public void testRoute_False_HostFilter() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl("true => " + " host = " + NetUtils.getLocalHost())); List> invokers = new ArrayList>(); - Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")); + Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService")); Invoker invoker2 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); Invoker invoker3 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); invokers.add(invoker1); @@ -272,7 +272,7 @@ public class ConditionRouterTest { public void testRoute_Placeholder() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = $host")); List> invokers = new ArrayList>(); - Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")); + Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService")); Invoker invoker2 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); Invoker invoker3 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); invokers.add(invoker1); @@ -288,7 +288,7 @@ public class ConditionRouterTest { public void testRoute_NoForce() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = 1.2.3.4")); List> invokers = new ArrayList>(); - Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")); + Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService")); Invoker invoker2 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); Invoker invoker3 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); invokers.add(invoker1); @@ -302,7 +302,7 @@ public class ConditionRouterTest { public void testRoute_Force() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = 1.2.3.4").addParameter(FORCE_KEY, String.valueOf(true))); List> invokers = new ArrayList>(); - Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")); + Invoker invoker1 = new MockInvoker(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService")); Invoker invoker2 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); Invoker invoker3 = new MockInvoker(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")); invokers.add(invoker1); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java index 37402d8d9a..9a8f77faea 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java @@ -47,10 +47,10 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; +import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.apache.dubbo.rpc.cluster.Constants.CLUSTER_AVAILABLE_CHECK_KEY; import static org.apache.dubbo.rpc.cluster.Constants.INVOCATION_NEED_MOCK; import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -493,21 +493,21 @@ public class AbstractClusterInvokerTest { Directory directory = new StaticDirectory(invokers); FailoverClusterInvoker failoverClusterInvoker = new FailoverClusterInvoker(directory); try { - failoverClusterInvoker.invoke(new RpcInvocation("sayHello", new Class[0], new Object[0])); + failoverClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), new Class[0], new Object[0])); Assertions.fail(); } catch (RpcException e) { Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode()); } ForkingClusterInvoker forkingClusterInvoker = new ForkingClusterInvoker(directory); try { - forkingClusterInvoker.invoke(new RpcInvocation("sayHello", new Class[0], new Object[0])); + forkingClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), new Class[0], new Object[0])); Assertions.fail(); } catch (RpcException e) { Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode()); } FailfastClusterInvoker failfastClusterInvoker = new FailfastClusterInvoker(directory); try { - failfastClusterInvoker.invoke(new RpcInvocation("sayHello", new Class[0], new Object[0])); + failfastClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), new Class[0], new Object[0])); Assertions.fail(); } catch (RpcException e) { Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode()); diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Invocation.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Invocation.java index d283c63b26..fde22937b4 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Invocation.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Invocation.java @@ -38,6 +38,11 @@ public interface Invocation extends org.apache.dubbo.rpc.Invocation { } + @Override + default String getServiceName() { + return null; + } + class CompatibleInvocation implements Invocation, org.apache.dubbo.rpc.Invocation { private org.apache.dubbo.rpc.Invocation delegate; diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/support/RpcUtils.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/support/RpcUtils.java index d7db04fe10..378f14adc2 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/support/RpcUtils.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/support/RpcUtils.java @@ -21,7 +21,6 @@ import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.rpc.Invocation; import java.lang.reflect.Type; -import java.util.Map; /** * 2019-04-18 @@ -76,8 +75,4 @@ public class RpcUtils extends org.apache.dubbo.rpc.support.RpcUtils { public static boolean isOneway(URL url, Invocation inv) { return org.apache.dubbo.rpc.support.RpcUtils.isOneway(url.getOriginalURL(), inv); } - - public static Map getNecessaryAttachments(Invocation inv) { - return org.apache.dubbo.rpc.support.RpcUtils.getNecessaryAttachments(inv); - } } diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java index 9c811f5c9e..fa97ff5cf2 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java @@ -17,7 +17,7 @@ package org.apache.dubbo.config; -import org.apache.dubbo.rpc.model.ConsumerMethodModel; +import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.service.Person; import com.alibaba.dubbo.config.ArgumentConfig; @@ -113,7 +113,7 @@ public class MethodConfigTest { methodConfig.setOninvokeMethod("setName"); methodConfig.setOninvoke(new Person()); - ConsumerMethodModel.AsyncMethodInfo methodInfo = org.apache.dubbo.config.MethodConfig.convertMethodConfig2AyncInfo(methodConfig); + ConsumerModel.AsyncMethodInfo methodInfo = org.apache.dubbo.config.MethodConfig.convertMethodConfig2AyncInfo(methodConfig); assertEquals(methodInfo.getOninvokeMethod(), Person.class.getMethod("setName", String.class)); } diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java b/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java index 0834746676..b48b4295e3 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/service/MockInvocation.java @@ -44,6 +44,11 @@ public class MockInvocation implements Invocation { return "echo"; } + @Override + public String getServiceName() { + return "DemoService"; + } + public Class[] getParameterTypes() { return new Class[]{String.class}; } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java index e1018108a5..5786b5d923 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java @@ -30,7 +30,7 @@ import org.apache.dubbo.common.utils.MethodUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.support.Parameter; -import org.apache.dubbo.rpc.model.ConsumerMethodModel; +import org.apache.dubbo.rpc.model.ConsumerModel; import java.io.Serializable; import java.lang.reflect.Method; @@ -245,7 +245,7 @@ public abstract class AbstractConfig implements Serializable { } } - protected static ConsumerMethodModel.AsyncMethodInfo convertMethodConfig2AyncInfo(MethodConfig methodConfig) { + protected static ConsumerModel.AsyncMethodInfo convertMethodConfig2AyncInfo(MethodConfig methodConfig) { if (methodConfig == null || (methodConfig.getOninvoke() == null && methodConfig.getOnreturn() == null && methodConfig.getOnthrow() == null)) { return null; } @@ -255,7 +255,7 @@ public abstract class AbstractConfig implements Serializable { throw new IllegalStateException("method config error : return attribute must be set true when onreturn or onthrow has been set."); } - ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = new ConsumerMethodModel.AsyncMethodInfo(); + ConsumerModel.AsyncMethodInfo asyncMethodInfo = new ConsumerModel.AsyncMethodInfo(); asyncMethodInfo.setOninvokeInstance(methodConfig.getOninvoke()); asyncMethodInfo.setOnreturnInstance(methodConfig.getOnreturn()); diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java index a3c020ceec..c4a66a4b35 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java @@ -46,7 +46,6 @@ import org.apache.dubbo.rpc.support.ProtocolUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -56,24 +55,24 @@ import java.util.Map; import java.util.Properties; import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE; +import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SEPARATOR; import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE; import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.SEMICOLON_SPLIT_PATTERN; import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; -import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY; -import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; -import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; -import static org.apache.dubbo.registry.Constants.CONSUMER_PROTOCOL; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL; -import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL; import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost; +import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY; +import static org.apache.dubbo.registry.Constants.CONSUMER_PROTOCOL; +import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY; +import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL; +import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; /** * ReferenceConfig @@ -304,7 +303,7 @@ public class ReferenceConfig extends AbstractReferenceConfig { appendParameters(map, this); Map attributes = null; if (CollectionUtils.isNotEmpty(methods)) { - attributes = new HashMap(); + attributes = new HashMap<>(); for (MethodConfig methodConfig : methods) { appendParameters(map, methodConfig, methodConfig.getName()); String retryKey = methodConfig.getName() + ".retry"; @@ -334,17 +333,7 @@ public class ReferenceConfig extends AbstractReferenceConfig { } private ConsumerModel buildConsumerModel(String serviceKey, Map attributes) { - Method[] methods = interfaceClass.getMethods(); - Class serviceInterface = interfaceClass; - if (interfaceClass == GenericService.class) { - try { - serviceInterface = Class.forName(interfaceName); - methods = serviceInterface.getMethods(); - } catch (ClassNotFoundException e) { - methods = interfaceClass.getMethods(); - } - } - return new ConsumerModel(serviceKey, serviceInterface, ref, methods, attributes); + return new ConsumerModel(serviceKey, interfaceClass, ref, ApplicationModel.registerServiceModel(interfaceClass), attributes); } @SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) 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 257d2a15ee..2b16496d5a 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 @@ -65,33 +65,33 @@ import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE; import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO; +import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_IP_TO_BIND; import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE; import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE; import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_IP_TO_BIND; -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.rpc.cluster.Constants.EXPORT_KEY; -import static org.apache.dubbo.config.Constants.MULTICAST; -import static org.apache.dubbo.config.Constants.PROTOCOLS_SUFFIX; -import static org.apache.dubbo.rpc.Constants.SCOPE_KEY; -import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL; -import static org.apache.dubbo.config.Constants.SCOPE_NONE; -import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE; -import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_KEY; -import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; -import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL; -import static org.apache.dubbo.rpc.Constants.PROXY_KEY; -import static org.apache.dubbo.rpc.Constants.TOKEN_KEY; import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort; import static org.apache.dubbo.common.utils.NetUtils.getLocalHost; import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost; import static org.apache.dubbo.common.utils.NetUtils.isInvalidPort; +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.MULTICAST; +import static org.apache.dubbo.config.Constants.PROTOCOLS_SUFFIX; +import static org.apache.dubbo.config.Constants.SCOPE_NONE; +import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; +import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL; +import static org.apache.dubbo.rpc.Constants.PROXY_KEY; +import static org.apache.dubbo.rpc.Constants.SCOPE_KEY; +import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL; +import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE; +import static org.apache.dubbo.rpc.Constants.TOKEN_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.EXPORT_KEY; /** * ServiceConfig @@ -452,7 +452,7 @@ public class ServiceConfig extends AbstractServiceConfig { List registryURLs = loadRegistries(true); for (ProtocolConfig protocolConfig : protocols) { String pathKey = URL.buildKey(getContextPath(protocolConfig).map(p -> p + "/" + path).orElse(path), group, version); - ProviderModel providerModel = new ProviderModel(pathKey, ref, interfaceClass); + ProviderModel providerModel = new ProviderModel(pathKey, ref, ApplicationModel.registerServiceModel(interfaceClass)); ApplicationModel.initProviderModel(pathKey, providerModel); doExportUrlsFor1Protocol(protocolConfig, registryURLs); } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/cache/CacheTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/cache/CacheTest.java index b9b5c45d43..0444e4fa2a 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/cache/CacheTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/cache/CacheTest.java @@ -132,7 +132,7 @@ public class CacheTest { parameters.put("findCache.cache", "threadlocal"); URL url = new URL("dubbo", "127.0.0.1", 29582, "org.apache.dubbo.config.cache.CacheService", parameters); - Invocation invocation = new RpcInvocation("findCache", new Class[]{String.class}, new String[]{"0"}, null, null); + Invocation invocation = new RpcInvocation("findCache", "org.apache.dubbo.config.cache.CacheService", new Class[]{String.class}, new String[]{"0"}, null, null); Cache cache = cacheFactory.getCache(url, invocation); assertTrue(cache instanceof ThreadLocalCache); diff --git a/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/DemoService.java b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/DemoService.java index 1172c9be0f..32337acdfd 100644 --- a/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/DemoService.java +++ b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/DemoService.java @@ -16,8 +16,14 @@ */ package org.apache.dubbo.demo; +import java.util.concurrent.CompletableFuture; + public interface DemoService { String sayHello(String name); + default CompletableFuture sayHelloAsync(String name) { + return CompletableFuture.completedFuture(sayHello(name)); + } + } \ No newline at end of file diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java index 90ed7c0b6a..955d3eb5a3 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java @@ -20,16 +20,18 @@ import org.apache.dubbo.demo.DemoService; import org.springframework.context.support.ClassPathXmlApplicationContext; +import java.util.concurrent.CompletableFuture; + public class Application { /** * In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before * launch the application */ - public static void main(String[] args) { + public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml"); context.start(); DemoService demoService = context.getBean("demoService", DemoService.class); - String hello = demoService.sayHello("world"); - System.out.println("result: " + hello); + CompletableFuture hello = demoService.sayHelloAsync("world"); + System.out.println("result: " + hello.get()); } } diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/Application.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/Application.java index 25a9dd4be0..f35f82ad92 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/Application.java +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/Application.java @@ -16,6 +16,8 @@ */ package org.apache.dubbo.demo.provider; +import org.apache.dubbo.common.utils.ReflectUtils; + import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { @@ -25,6 +27,7 @@ public class Application { */ public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml"); + System.err.println(ReflectUtils.getDesc(new Class[]{String.class, String[].class, Object[].class})); context.start(); System.in.read(); } diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java index d0d315c9b4..31b9040072 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java @@ -22,6 +22,8 @@ import org.apache.dubbo.rpc.RpcContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.concurrent.CompletableFuture; + public class DemoServiceImpl implements DemoService { private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class); @@ -31,4 +33,16 @@ public class DemoServiceImpl implements DemoService { return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); } + @Override + public CompletableFuture sayHelloAsync(String name) { + CompletableFuture cf = CompletableFuture.supplyAsync(() -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "async result"; + }); + return cf; + } } diff --git a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MonitorFilterTest.java b/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MonitorFilterTest.java index a24711fdda..0a75b9a212 100644 --- a/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MonitorFilterTest.java +++ b/dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MonitorFilterTest.java @@ -40,8 +40,8 @@ import java.util.List; import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE; -import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -118,7 +118,7 @@ public class MonitorFilterTest { public void testFilter() throws Exception { MonitorFilter monitorFilter = new MonitorFilter(); monitorFilter.setMonitorFactory(monitorFactory); - Invocation invocation = new RpcInvocation("aaa", new Class[0], new Object[0]); + Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), new Class[0], new Object[0]); RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); Result result = monitorFilter.invoke(serviceInvoker, invocation); result.thenApplyWithContext((r) -> { @@ -145,7 +145,7 @@ public class MonitorFilterTest { MonitorFilter monitorFilter = new MonitorFilter(); MonitorFactory mockMonitorFactory = mock(MonitorFactory.class); monitorFilter.setMonitorFactory(mockMonitorFactory); - Invocation invocation = new RpcInvocation("aaa", new Class[0], new Object[0]); + Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), new Class[0], new Object[0]); Invoker invoker = mock(Invoker.class); given(invoker.getUrl()).willReturn(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880?" + APPLICATION_KEY + "=abc&" + SIDE_KEY + "=" + CONSUMER_SIDE)); @@ -158,7 +158,7 @@ public class MonitorFilterTest { public void testGenericFilter() throws Exception { MonitorFilter monitorFilter = new MonitorFilter(); monitorFilter.setMonitorFactory(monitorFactory); - Invocation invocation = new RpcInvocation("$invoke", new Class[]{String.class, String[].class, Object[].class}, new Object[]{"xxx", new String[]{}, new Object[]{}}); + Invocation invocation = new RpcInvocation("$invoke", MonitorService.class.getName(), new Class[]{String.class, String[].class, Object[].class}, new Object[]{"xxx", new String[]{}, new Object[]{}}); RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); Result result = monitorFilter.invoke(serviceInvoker, invocation); result.thenApplyWithContext((r) -> { @@ -189,7 +189,7 @@ public class MonitorFilterTest { monitorFilter.setMonitorFactory(mockMonitorFactory); given(mockMonitorFactory.getMonitor(any(URL.class))).willReturn(mockMonitor); - Invocation invocation = new RpcInvocation("aaa", new Class[0], new Object[0]); + Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), new Class[0], new Object[0]); monitorFilter.invoke(serviceInvoker, invocation); } diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java index 591296fa44..20460d53b1 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java @@ -25,9 +25,10 @@ import org.apache.dubbo.monitor.support.AbstractMonitorFactory; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; +import org.apache.dubbo.rpc.model.ApplicationModel; -import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL; +import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; import static org.apache.dubbo.remoting.Constants.CHECK_KEY; import static org.apache.dubbo.rpc.Constants.REFERENCE_FILTER_KEY; @@ -63,6 +64,7 @@ public class DubboMonitorFactory extends AbstractMonitorFactory { } urlBuilder.addParameters(CHECK_KEY, String.valueOf(false), REFERENCE_FILTER_KEY, filter + "-monitor"); + ApplicationModel.registerServiceModel(MonitorService.class); Invoker monitorInvoker = protocol.refer(MonitorService.class, urlBuilder.build()); MonitorService monitorService = proxyFactory.getProxy(monitorInvoker); return new DubboMonitor(monitorInvoker, monitorService); diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java index 761de5945d..97b75d5ff4 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java @@ -31,6 +31,7 @@ import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.support.RpcUtils; import com.alibaba.fastjson.JSON; @@ -88,6 +89,7 @@ public class MetricsFilter implements Filter { Invoker metricsInvoker = initMetricsInvoker(); try { + ApplicationModel.registerServiceModel(MetricsService.class); protocol.export(metricsInvoker); } catch (RuntimeException e) { logger.error("Metrics Service need to be configured" + diff --git a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/MetricsFilterTest.java b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/MetricsFilterTest.java index 47b607db7f..4bf8a4c393 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/MetricsFilterTest.java +++ b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/MetricsFilterTest.java @@ -112,7 +112,7 @@ public class MetricsFilterTest { IMetricManager metricManager = MetricManager.getIMetricManager(); metricManager.clear(); MetricsFilter metricsFilter = new MetricsFilter(); - Invocation invocation = new RpcInvocation("sayName", new Class[]{Integer.class}, new Object[0]); + Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), new Class[]{Integer.class}, new Object[0]); RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, CONSUMER_SIDE)); for (int i = 0; i < 100; i++) { @@ -137,7 +137,7 @@ public class MetricsFilterTest { IMetricManager metricManager = MetricManager.getIMetricManager(); metricManager.clear(); MetricsFilter metricsFilter = new MetricsFilter(); - Invocation invocation = new RpcInvocation("timeoutException", null, null); + Invocation invocation = new RpcInvocation("timeoutException", DemoService.class.getName(), null, null); RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); RpcContext.getContext().setUrl(timeoutInvoker.getUrl().addParameter(SIDE_KEY, CONSUMER_SIDE) .addParameter(TIMEOUT_KEY, 300)); @@ -167,7 +167,7 @@ public class MetricsFilterTest { IMetricManager metricManager = MetricManager.getIMetricManager(); metricManager.clear(); MetricsFilter metricsFilter = new MetricsFilter(); - Invocation invocation = new RpcInvocation("sayName", new Class[0], new Object[0]); + Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), new Class[0], new Object[0]); RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER)); for (int i = 0; i < 100; i++) { @@ -191,7 +191,7 @@ public class MetricsFilterTest { IMetricManager metricManager = MetricManager.getIMetricManager(); metricManager.clear(); MetricsFilter metricsFilter = new MetricsFilter(); - Invocation invocation = new RpcInvocation("sayName", new Class[0], new Object[0]); + Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), new Class[0], new Object[0]); RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE) .addParameter(TIMEOUT_KEY, 300)); @@ -206,7 +206,7 @@ public class MetricsFilterTest { Protocol protocol = new DubboProtocol(); URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":20880/" + MetricsService.class.getName()); Invoker invoker = protocol.refer(MetricsService.class, url); - invocation = new RpcInvocation("getMetricsByGroup", new Class[]{String.class}, new Object[]{DUBBO_GROUP}); + invocation = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), new Class[]{String.class}, new Object[]{DUBBO_GROUP}); try { Thread.sleep(5000); } catch (Exception e) { @@ -235,8 +235,8 @@ public class MetricsFilterTest { IMetricManager metricManager = MetricManager.getIMetricManager(); metricManager.clear(); MetricsFilter metricsFilter = new MetricsFilter(); - Invocation sayNameInvocation = new RpcInvocation("sayName", new Class[0], new Object[0]); - Invocation echoInvocation = new RpcInvocation("echo", new Class[]{Integer.class}, new Integer[]{1}); + Invocation sayNameInvocation = new RpcInvocation("sayName", DemoService.class.getName(), new Class[0], new Object[0]); + Invocation echoInvocation = new RpcInvocation("echo", DemoService.class.getName(), new Class[]{Integer.class}, new Integer[]{1}); RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345); RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE) .addParameter(TIMEOUT_KEY, 300)); @@ -258,7 +258,7 @@ public class MetricsFilterTest { Protocol protocol = new DubboProtocol(); URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":20880/" + MetricsService.class.getName()); Invoker invoker = protocol.refer(MetricsService.class, url); - Invocation invocation = new RpcInvocation("getMetricsByGroup", new Class[]{String.class}, new Object[]{DUBBO_GROUP}); + Invocation invocation = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), new Class[]{String.class}, new Object[]{DUBBO_GROUP}); try { Thread.sleep(15000); } catch (Exception e) { diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java index 469a40a339..ca23068abb 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java @@ -57,7 +57,7 @@ public class Ls implements BaseCommand { //Content for (ProviderModel providerModel : providerModelList) { - tTable.addRow(providerModel.getServiceName(), isRegistered(providerModel.getServiceName()) ? "Y" : "N"); + tTable.addRow(providerModel.getServiceKey(), isRegistered(providerModel.getServiceKey()) ? "Y" : "N"); } stringBuilder.append(tTable.rendering()); @@ -80,7 +80,7 @@ public class Ls implements BaseCommand { //Content //TODO to calculate consumerAddressNum for (ConsumerModel consumerModel : consumerModelList) { - tTable.addRow(consumerModel.getServiceName(), getConsumerAddressNum(consumerModel.getServiceName())); + tTable.addRow(consumerModel.getServiceKey(), getConsumerAddressNum(consumerModel.getServiceKey())); } stringBuilder.append(tTable.rendering()); diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java index 79c41f3b66..cf2862e36e 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java @@ -51,9 +51,9 @@ public class Offline implements BaseCommand { Collection providerModelList = ApplicationModel.allProviderModels(); for (ProviderModel providerModel : providerModelList) { - if (providerModel.getServiceName().matches(servicePattern)) { + if (providerModel.getServiceKey().matches(servicePattern)) { hasService = true; - Set providerInvokerWrapperSet = ProviderConsumerRegTable.getProviderInvoker(providerModel.getServiceName()); + Set providerInvokerWrapperSet = ProviderConsumerRegTable.getProviderInvoker(providerModel.getServiceKey()); for (ProviderInvokerWrapper providerInvokerWrapper : providerInvokerWrapperSet) { if (!providerInvokerWrapper.isReg()) { continue; diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java index 30ba14b6fe..6bcf089464 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java @@ -53,9 +53,9 @@ public class Online implements BaseCommand { Collection providerModelList = ApplicationModel.allProviderModels(); for (ProviderModel providerModel : providerModelList) { - if (providerModel.getServiceName().matches(servicePattern)) { + if (providerModel.getServiceKey().matches(servicePattern)) { hasService = true; - Set providerInvokerWrapperSet = ProviderConsumerRegTable.getProviderInvoker(providerModel.getServiceName()); + Set providerInvokerWrapperSet = ProviderConsumerRegTable.getProviderInvoker(providerModel.getServiceKey()); for (ProviderInvokerWrapper providerInvokerWrapper : providerInvokerWrapperSet) { if (providerInvokerWrapper.isReg()) { continue; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java index c82e2d5d68..4b4fda2a71 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java @@ -25,14 +25,15 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.rpc.model.ProviderModel; + import org.junit.jupiter.api.Test; import org.mockito.Mockito; import java.util.Map; import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker; -import static org.hamcrest.Matchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -40,9 +41,9 @@ public class LsTest { @Test public void testExecute() throws Exception { ConsumerModel consumerModel = mock(ConsumerModel.class); - when(consumerModel.getServiceName()).thenReturn("org.apache.dubbo.FooService"); + when(consumerModel.getServiceKey()).thenReturn("org.apache.dubbo.FooService"); ProviderModel providerModel = mock(ProviderModel.class); - when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService"); + when(providerModel.getServiceKey()).thenReturn("org.apache.dubbo.BarService"); ApplicationModel.initConsumerModel("org.apache.dubbo.FooService", consumerModel); ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel); diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java index f498d5aeb3..6f6be8e0bb 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java @@ -24,13 +24,14 @@ import org.apache.dubbo.registry.support.ProviderInvokerWrapper; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.ProviderModel; + import org.junit.jupiter.api.Test; import org.mockito.Mockito; import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.core.Is.is; -import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -38,7 +39,7 @@ public class OfflineTest { @Test public void testExecute() throws Exception { ProviderModel providerModel = mock(ProviderModel.class); - when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService"); + when(providerModel.getServiceKey()).thenReturn("org.apache.dubbo.BarService"); ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel); Invoker providerInvoker = mock(Invoker.class); diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java index 71132c0955..b419f0ca9f 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java @@ -24,11 +24,12 @@ import org.apache.dubbo.registry.support.ProviderInvokerWrapper; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.ProviderModel; + import org.junit.jupiter.api.Test; import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -37,7 +38,7 @@ public class OnlineTest { @Test public void testExecute() throws Exception { ProviderModel providerModel = mock(ProviderModel.class); - when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService"); + when(providerModel.getServiceKey()).thenReturn("org.apache.dubbo.BarService"); ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel); Invoker providerInvoker = mock(Invoker.class); diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java index 7bf8197bff..e64a1bf8a4 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java @@ -34,6 +34,7 @@ import org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance; import org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance; import org.apache.dubbo.rpc.cluster.router.script.ScriptRouterFactory; import org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker; +import org.apache.dubbo.rpc.service.GenericService; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -49,19 +50,12 @@ import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; -import static org.apache.dubbo.rpc.cluster.Constants.INVOCATION_NEED_MOCK; -import static org.apache.dubbo.rpc.cluster.Constants.LOADBALANCE_KEY; -import static org.apache.dubbo.rpc.cluster.Constants.MOCK_PROTOCOL; -import static org.apache.dubbo.rpc.cluster.Constants.ROUTER_KEY; -import static org.apache.dubbo.rpc.cluster.Constants.RULE_KEY; -import static org.apache.dubbo.rpc.cluster.Constants.TYPE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE; import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE; import static org.apache.dubbo.common.constants.CommonConstants.DISABLED_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY; import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; -import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL; @@ -70,6 +64,13 @@ import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGO import static org.apache.dubbo.common.constants.RegistryConstants.ROUTE_PROTOCOL; import static org.apache.dubbo.rpc.Constants.$INVOKE; import static org.apache.dubbo.rpc.Constants.MOCK_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.INVOCATION_NEED_MOCK; +import static org.apache.dubbo.rpc.cluster.Constants.LOADBALANCE_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.MOCK_PROTOCOL; +import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.ROUTER_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.RULE_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.TYPE_KEY; import static org.junit.jupiter.api.Assertions.fail; @SuppressWarnings({"rawtypes", "unchecked"}) @@ -482,7 +483,7 @@ public class RegistryDirectoryTest { registryDirectory.notify(serviceUrls); // Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException; - invocation = new RpcInvocation($INVOKE, new Class[]{String.class, String[].class, Object[].class}, + invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), new Class[]{String.class, String[].class, Object[].class}, new Object[]{"getXXX1", "", new Object[]{}}); List invokers = registryDirectory.list(invocation); @@ -511,7 +512,7 @@ public class RegistryDirectoryTest { registryDirectory.notify(serviceUrls); - invocation = new RpcInvocation($INVOKE, + invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), new Class[]{String.class, String[].class, Object[].class}, new Object[]{"getXXX1", new String[]{"Enum"}, new Object[]{Param.MORGAN}}); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Invocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Invocation.java index 058e0ad09c..23f6f63b17 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Invocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Invocation.java @@ -35,6 +35,13 @@ public interface Invocation { */ String getMethodName(); + + /** + * get the interface name + * @return + */ + String getServiceName(); + /** * get parameter types. * diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java index 06b7cb6a0d..d7eb9234d5 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java @@ -18,9 +18,11 @@ package org.apache.dubbo.rpc; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.StringUtils; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.io.Serializable; import java.lang.reflect.Method; +import java.lang.reflect.Type; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -43,8 +45,10 @@ public class RpcInvocation implements Invocation, Serializable { private static final long serialVersionUID = -4355285085441097045L; private String methodName; + private String serviceName; - private Class[] parameterTypes; + private transient Class[] parameterTypes; + private String parameterTypesDesc; private Object[] arguments; @@ -54,13 +58,15 @@ public class RpcInvocation implements Invocation, Serializable { private transient Class returnType; + private transient Type[] returnTypes; + private transient InvokeMode invokeMode; public RpcInvocation() { } public RpcInvocation(Invocation invocation, Invoker invoker) { - this(invocation.getMethodName(), invocation.getParameterTypes(), + this(invocation.getMethodName(), invocation.getServiceName(), invocation.getParameterTypes(), invocation.getArguments(), new HashMap(invocation.getAttachments()), invocation.getInvoker()); if (invoker != null) { @@ -88,33 +94,40 @@ public class RpcInvocation implements Invocation, Serializable { } public RpcInvocation(Invocation invocation) { - this(invocation.getMethodName(), invocation.getParameterTypes(), + this(invocation.getMethodName(), invocation.getServiceName(), invocation.getParameterTypes(), invocation.getArguments(), invocation.getAttachments(), invocation.getInvoker()); } - public RpcInvocation(Method method, Object[] arguments) { - this(method.getName(), method.getParameterTypes(), arguments, null, null); + public RpcInvocation(Method method, String serviceName, Object[] arguments) { + this(method, serviceName, arguments, null); } - public RpcInvocation(Method method, Object[] arguments, Map attachment) { - this(method.getName(), method.getParameterTypes(), arguments, attachment, null); + public RpcInvocation(Method method, String serviceName, Object[] arguments, Map attachment) { + this(method.getName(), serviceName, method.getParameterTypes(), arguments, attachment, null); this.returnType = method.getReturnType(); } - public RpcInvocation(String methodName, Class[] parameterTypes, Object[] arguments) { - this(methodName, parameterTypes, arguments, null, null); + public RpcInvocation(String methodName, String serviceName, Class[] parameterTypes, Object[] arguments) { + this(methodName, serviceName, parameterTypes, arguments, null, null); } - public RpcInvocation(String methodName, Class[] parameterTypes, Object[] arguments, Map attachments) { - this(methodName, parameterTypes, arguments, attachments, null); + public RpcInvocation(String methodName, String serviceName, Class[] parameterTypes, Object[] arguments, Map attachments) { + this(methodName, serviceName, parameterTypes, arguments, attachments, null); } - public RpcInvocation(String methodName, Class[] parameterTypes, Object[] arguments, Map attachments, Invoker invoker) { + public RpcInvocation(String methodName, String serviceName, Class[] parameterTypes, Object[] arguments, Map attachments, Invoker invoker) { this.methodName = methodName; this.parameterTypes = parameterTypes == null ? new Class[0] : parameterTypes; this.arguments = arguments == null ? new Object[0] : arguments; this.attachments = attachments == null ? new HashMap() : attachments; this.invoker = invoker; + ApplicationModel.getServiceModel(serviceName).ifPresent(serviceModel -> + serviceModel.getMethod(methodName, parameterTypes) + .ifPresent(methodModel -> { + this.parameterTypesDesc = methodModel.getParamDesc(); + this.returnTypes = methodModel.getReturnTypes(); + }) + ); } @Override @@ -131,6 +144,15 @@ public class RpcInvocation implements Invocation, Serializable { return methodName; } + @Override + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + public void setMethodName(String methodName) { this.methodName = methodName; } @@ -144,6 +166,14 @@ public class RpcInvocation implements Invocation, Serializable { this.parameterTypes = parameterTypes == null ? new Class[0] : parameterTypes; } + public String getParameterTypesDesc() { + return parameterTypesDesc; + } + + public void setParameterTypesDesc(String parameterTypesDesc) { + this.parameterTypesDesc = parameterTypesDesc; + } + @Override public Object[] getArguments() { return arguments; @@ -227,6 +257,14 @@ public class RpcInvocation implements Invocation, Serializable { this.returnType = returnType; } + public Type[] getReturnTypes() { + return returnTypes; + } + + public void setReturnTypes(Type[] returnTypes) { + this.returnTypes = returnTypes; + } + public InvokeMode getInvokeMode() { return invokeMode; } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java index ed6e123971..037734e6cf 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java @@ -138,7 +138,7 @@ public class GenericFilter extends ListenableFilter { args[0].getClass().getName()); } } - return invoker.invoke(new RpcInvocation(method, args, inv.getAttachments())); + return invoker.invoke(new RpcInvocation(method, invoker.getInterface().getName(), args, inv.getAttachments())); } catch (NoSuchMethodException e) { throw new RpcException(e.getMessage(), e); } catch (ClassNotFoundException e) { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java index 0fd0d826df..06710bc1d5 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java @@ -54,6 +54,8 @@ public class GenericImplFilter extends ListenableFilter { private static final Class[] GENERIC_PARAMETER_TYPES = new Class[]{String.class, String[].class, Object[].class}; + private static final String GENERIC_PARAMETER_DESC = "Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;"; + public GenericImplFilter() { super.listener = new GenericImplListener(); } @@ -90,6 +92,7 @@ public class GenericImplFilter extends ListenableFilter { invocation2.setMethodName($INVOKE); } invocation2.setParameterTypes(GENERIC_PARAMETER_TYPES); + invocation2.setParameterTypesDesc(GENERIC_PARAMETER_DESC); invocation2.setArguments(new Object[]{methodName, types, args}); return invoker.invoke(invocation2); } else if ((invocation.getMethodName().equals($INVOKE) || invocation.getMethodName().equals($INVOKE_ASYNC)) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java index 33dcc908e1..22663513e1 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import java.util.Collection; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -37,14 +38,21 @@ public class ApplicationModel { protected static final Logger LOGGER = LoggerFactory.getLogger(ApplicationModel.class); /** - * full qualified class name -> provided service + * serviceKey -> exported service + * each service may has different group, version, path, instance and so on, but they point to the same {@link ServiceModel} */ private static final ConcurrentMap PROVIDED_SERVICES = new ConcurrentHashMap<>(); /** - * full qualified class name -> subscribe service + * serviceKey -> referred service + * each service may has different group, version, path, instance and so on, but they point to the same {@link ServiceModel} */ private static final ConcurrentMap CONSUMED_SERVICES = new ConcurrentHashMap<>(); + /** + * The description of a unique service (interface definition in Dubbo) + */ + private static final ConcurrentHashMap SERVICES = new ConcurrentHashMap<>(); + private static String application; public static Collection allConsumerModels() { @@ -55,12 +63,12 @@ public class ApplicationModel { return PROVIDED_SERVICES.values(); } - public static ProviderModel getProviderModel(String serviceName) { - return PROVIDED_SERVICES.get(serviceName); + public static ProviderModel getProviderModel(String serviceKey) { + return PROVIDED_SERVICES.get(serviceKey); } - public static ConsumerModel getConsumerModel(String serviceName) { - return CONSUMED_SERVICES.get(serviceName); + public static ConsumerModel getConsumerModel(String serviceKey) { + return CONSUMED_SERVICES.get(serviceKey); } public static void initConsumerModel(String serviceName, ConsumerModel consumerModel) { @@ -75,6 +83,38 @@ public class ApplicationModel { } } + public static ServiceModel registerServiceModel(Class interfaceClass) { + return SERVICES.computeIfAbsent(interfaceClass.getName(), (k) -> new ServiceModel(interfaceClass)); + } + + /** + * See {@link #registerServiceModel(Class)} + * + * we assume: + * 1. services with different interface are not allowed to have the same path. + * 2. services with the same interface but different group/version can share the same path. + * 3. path's default value is the name of the interface. + * @param path + * @param interfaceClass + * @return + */ + public static ServiceModel registerServiceModel(String path, Class interfaceClass) { + ServiceModel serviceModel = registerServiceModel(interfaceClass); + // register path + if (!interfaceClass.getName().equals(path)) { + SERVICES.putIfAbsent(path, serviceModel); + } + return serviceModel; + } + + public static Optional getServiceModel (String interfaceName) { + return Optional.ofNullable(SERVICES.get(interfaceName)); + } + + public static Optional getServiceModel (Class interfaceClass) { + return Optional.ofNullable(SERVICES.get(interfaceClass.getName())); + } + public static String getApplication() { return application; } @@ -90,4 +130,5 @@ public class ApplicationModel { PROVIDED_SERVICES.clear(); CONSUMED_SERVICES.clear(); } + } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerMethodModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerMethodModel.java deleted file mode 100644 index 0798a50029..0000000000 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerMethodModel.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.dubbo.rpc.model; - - -import java.lang.reflect.Method; -import java.util.Map; - -import static org.apache.dubbo.rpc.Constants.$INVOKE; - -public class ConsumerMethodModel { - private final Method method; - // private final boolean isCallBack; -// private final boolean isFuture; - private final String[] parameterTypes; - private final Class[] parameterClasses; - private final Class returnClass; - private final String methodName; - private final boolean generic; - - private final AsyncMethodInfo asyncInfo; - - - public ConsumerMethodModel(Method method, Map attributes) { - this.method = method; - this.parameterClasses = method.getParameterTypes(); - this.returnClass = method.getReturnType(); - this.parameterTypes = this.createParamSignature(parameterClasses); - this.methodName = method.getName(); - this.generic = methodName.equals($INVOKE) && parameterTypes != null && parameterTypes.length == 3; - - if (attributes != null) { - asyncInfo = (AsyncMethodInfo) attributes.get(methodName); - } else { - asyncInfo = null; - } - } - - public Method getMethod() { - return method; - } - - public Class getReturnClass() { - return returnClass; - } - - public AsyncMethodInfo getAsyncInfo() { - return asyncInfo; - } - - public String getMethodName() { - return methodName; - } - - public String[] getParameterTypes() { - return parameterTypes; - } - - private String[] createParamSignature(Class[] args) { - if (args == null || args.length == 0) { - return new String[]{}; - } - String[] paramSig = new String[args.length]; - for (int x = 0; x < args.length; x++) { - paramSig[x] = args[x].getName(); - } - return paramSig; - } - - - public boolean isGeneric() { - return generic; - } - - public Class[] getParameterClasses() { - return parameterClasses; - } - - - public static class AsyncMethodInfo { - // callback instance when async-call is invoked - private Object oninvokeInstance; - - // callback method when async-call is invoked - private Method oninvokeMethod; - - // callback instance when async-call is returned - private Object onreturnInstance; - - // callback method when async-call is returned - private Method onreturnMethod; - - // callback instance when async-call has exception thrown - private Object onthrowInstance; - - // callback method when async-call has exception thrown - private Method onthrowMethod; - - public Object getOninvokeInstance() { - return oninvokeInstance; - } - - public void setOninvokeInstance(Object oninvokeInstance) { - this.oninvokeInstance = oninvokeInstance; - } - - public Method getOninvokeMethod() { - return oninvokeMethod; - } - - public void setOninvokeMethod(Method oninvokeMethod) { - this.oninvokeMethod = oninvokeMethod; - } - - public Object getOnreturnInstance() { - return onreturnInstance; - } - - public void setOnreturnInstance(Object onreturnInstance) { - this.onreturnInstance = onreturnInstance; - } - - public Method getOnreturnMethod() { - return onreturnMethod; - } - - public void setOnreturnMethod(Method onreturnMethod) { - this.onreturnMethod = onreturnMethod; - } - - public Object getOnthrowInstance() { - return onthrowInstance; - } - - public void setOnthrowInstance(Object onthrowInstance) { - this.onthrowInstance = onthrowInstance; - } - - public Method getOnthrowMethod() { - return onthrowMethod; - } - - public void setOnthrowMethod(Method onthrowMethod) { - this.onthrowMethod = onthrowMethod; - } - } -} diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java index 4c990a5088..135cadbd7a 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java @@ -17,50 +17,49 @@ package org.apache.dubbo.rpc.model; import org.apache.dubbo.common.utils.Assert; +import org.apache.dubbo.common.utils.CollectionUtils; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.IdentityHashMap; -import java.util.List; +import java.util.HashMap; import java.util.Map; -import java.util.Optional; +import java.util.Set; /** - * Consumer Model which is about subscribed services. + * This model is bind to your reference's configuration, for example, group, version or method level configuration. */ public class ConsumerModel { + private final String serviceKey; private final Object proxyObject; - private final String serviceName; - private final Class serviceInterfaceClass; - - private final Map methodModels = new IdentityHashMap(); + private final ServiceModel serviceModel; + private final Map methodConfigs = new HashMap<>(); /** * This constructor create an instance of ConsumerModel and passed objects should not be null. * If service name, service instance, proxy object,methods should not be null. If these are null * then this constructor will throw {@link IllegalArgumentException} - * @param serviceName Name of the service. + * @param serviceKey Name of the service. * @param serviceInterfaceClass Service interface class. * @param proxyObject Proxy object. - * @param methods Methods of service class * @param attributes Attributes of methods. */ - public ConsumerModel(String serviceName + public ConsumerModel(String serviceKey , Class serviceInterfaceClass , Object proxyObject - , Method[] methods + , ServiceModel serviceModel , Map attributes) { - Assert.notEmptyString(serviceName, "Service name can't be null or blank"); + Assert.notEmptyString(serviceKey, "Service name can't be null or blank"); Assert.notNull(serviceInterfaceClass, "Service interface class can't null"); Assert.notNull(proxyObject, "Proxy object can't be null"); - Assert.notNull(methods, "Methods can't be null"); - this.serviceName = serviceName; - this.serviceInterfaceClass = serviceInterfaceClass; + this.serviceKey = serviceKey; this.proxyObject = proxyObject; - for (Method method : methods) { - methodModels.put(method, new ConsumerMethodModel(method, attributes)); + this.serviceModel = serviceModel; + + if (CollectionUtils.isNotEmptyMap(attributes)) { + attributes.forEach((method, object) -> { + methodConfigs.put(method, (AsyncMethodInfo) object); + }); } } @@ -72,41 +71,96 @@ public class ConsumerModel { return proxyObject; } - /** - * Return method model for the given method on consumer side - * - * @param method method object - * @return method model - */ - public ConsumerMethodModel getMethodModel(Method method) { - return methodModels.get(method); - } - - /** - * Return method model for the given method on consumer side - * - * @param method method object - * @return method model - */ - public ConsumerMethodModel getMethodModel(String method) { - Optional> consumerMethodModelEntry = methodModels.entrySet().stream().filter(entry -> entry.getKey().getName().equals(method)).findFirst(); - return consumerMethodModelEntry.map(Map.Entry::getValue).orElse(null); - } - /** * Return all method models for the current service * * @return method model list */ - public List getAllMethods() { - return new ArrayList(methodModels.values()); + public Set getAllMethods() { + return serviceModel.getAllMethods(); } public Class getServiceInterfaceClass() { - return serviceInterfaceClass; + return serviceModel.getServiceInterfaceClass(); } - public String getServiceName() { - return serviceName; + public String getServiceKey() { + return serviceKey; + } + + public AsyncMethodInfo getMethodConfig(String methodName) { + return methodConfigs.get(methodName); + } + + public ServiceModel getServiceModel() { + return serviceModel; + } + + public static class AsyncMethodInfo { + // callback instance when async-call is invoked + private Object oninvokeInstance; + + // callback method when async-call is invoked + private Method oninvokeMethod; + + // callback instance when async-call is returned + private Object onreturnInstance; + + // callback method when async-call is returned + private Method onreturnMethod; + + // callback instance when async-call has exception thrown + private Object onthrowInstance; + + // callback method when async-call has exception thrown + private Method onthrowMethod; + + public Object getOninvokeInstance() { + return oninvokeInstance; + } + + public void setOninvokeInstance(Object oninvokeInstance) { + this.oninvokeInstance = oninvokeInstance; + } + + public Method getOninvokeMethod() { + return oninvokeMethod; + } + + public void setOninvokeMethod(Method oninvokeMethod) { + this.oninvokeMethod = oninvokeMethod; + } + + public Object getOnreturnInstance() { + return onreturnInstance; + } + + public void setOnreturnInstance(Object onreturnInstance) { + this.onreturnInstance = onreturnInstance; + } + + public Method getOnreturnMethod() { + return onreturnMethod; + } + + public void setOnreturnMethod(Method onreturnMethod) { + this.onreturnMethod = onreturnMethod; + } + + public Object getOnthrowInstance() { + return onthrowInstance; + } + + public void setOnthrowInstance(Object onthrowInstance) { + this.onthrowInstance = onthrowInstance; + } + + public Method getOnthrowMethod() { + return onthrowMethod; + } + + public void setOnthrowMethod(Method onthrowMethod) { + this.onthrowMethod = onthrowMethod; + } } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java new file mode 100644 index 0000000000..58fd2a0939 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.rpc.model; + +import org.apache.dubbo.common.utils.ReflectUtils; + +import java.lang.reflect.Method; +import java.lang.reflect.Type; + +import static org.apache.dubbo.rpc.Constants.$INVOKE; +import static org.apache.dubbo.rpc.Constants.$INVOKE_ASYNC; + +/** + * + */ +public class MethodModel { + private final Method method; + // private final boolean isCallBack; +// private final boolean isFuture; + private final String paramDesc; + private final Class[] parameterClasses; + private final Class returnClass; + private final Type[] returnTypes; + private final String methodName; + private final boolean generic; + + public MethodModel (Method method) { + this.method = method; + this.parameterClasses = method.getParameterTypes(); + this.returnClass = method.getReturnType(); + this.returnTypes = ReflectUtils.getReturnTypes(method); + this.paramDesc = ReflectUtils.getDesc(method); + this.methodName = method.getName(); + this.generic = (methodName.equals($INVOKE) || methodName.equals($INVOKE_ASYNC)) && parameterClasses.length == 3; + } + + public boolean matchParams (String params) { + return paramDesc.equalsIgnoreCase(params); + } + + public Method getMethod() { + return method; + } + + public String getParamDesc() { + return paramDesc; + } + + public Class[] getParameterClasses() { + return parameterClasses; + } + + public Class getReturnClass() { + return returnClass; + } + + public Type[] getReturnTypes() { + return returnTypes; + } + + public String getMethodName() { + return methodName; + } + + public boolean isGeneric() { + return generic; + } + +} diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderMethodModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderMethodModel.java deleted file mode 100644 index d7b8a9bb87..0000000000 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderMethodModel.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.dubbo.rpc.model; - -import java.lang.reflect.Method; - -public class ProviderMethodModel { - private transient final Method method; - private final String methodName; - private final String[] methodArgTypes; - private final String serviceName; - - - public ProviderMethodModel(Method method, String serviceName) { - this.method = method; - this.serviceName = serviceName; - this.methodName = method.getName(); - this.methodArgTypes = getArgTypes(method); - } - - public Method getMethod() { - return method; - } - - public String getMethodName() { - return methodName; - } - - public String[] getMethodArgTypes() { - return methodArgTypes; - } - - public String getServiceName() { - return serviceName; - } - - private static String[] getArgTypes(Method method) { - String[] methodArgTypes = new String[0]; - Class[] parameterTypes = method.getParameterTypes(); - if (parameterTypes.length > 0) { - methodArgTypes = new String[parameterTypes.length]; - int index = 0; - for (Class paramType : parameterTypes) { - methodArgTypes[index++] = paramType.getName(); - } - } - return methodArgTypes; - } -} diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java index 6fb9bebd7e..41f6f7cc63 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java @@ -16,81 +16,43 @@ */ package org.apache.dubbo.rpc.model; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.Set; /** * ProviderModel which is about published services */ public class ProviderModel { - private final String serviceName; + private final String serviceKey; private final Object serviceInstance; - private final Class serviceInterfaceClass; - private final Map> methods = new HashMap>(); + private final ServiceModel serviceModel; - public ProviderModel(String serviceName, Object serviceInstance, Class serviceInterfaceClass) { + public ProviderModel(String serviceKey, Object serviceInstance, ServiceModel serviceModel) { if (null == serviceInstance) { - throw new IllegalArgumentException("Service[" + serviceName + "]Target is NULL."); + throw new IllegalArgumentException("Service[" + serviceKey + "]Target is NULL."); } - this.serviceName = serviceName; + this.serviceKey = serviceKey; this.serviceInstance = serviceInstance; - this.serviceInterfaceClass = serviceInterfaceClass; - - initMethod(); + this.serviceModel = serviceModel; } - - public String getServiceName() { - return serviceName; + public String getServiceKey() { + return serviceKey; } public Class getServiceInterfaceClass() { - return serviceInterfaceClass; + return serviceModel.getServiceInterfaceClass(); } public Object getServiceInstance() { return serviceInstance; } - public List getAllMethods() { - List result = new ArrayList(); - for (List models : methods.values()) { - result.addAll(models); - } - return result; + public Set getAllMethods() { + return serviceModel.getAllMethods(); } - public ProviderMethodModel getMethodModel(String methodName, String[] argTypes) { - List methodModels = methods.get(methodName); - if (methodModels != null) { - for (ProviderMethodModel methodModel : methodModels) { - if (Arrays.equals(argTypes, methodModel.getMethodArgTypes())) { - return methodModel; - } - } - } - return null; + public ServiceModel getServiceModel() { + return serviceModel; } - - private void initMethod() { - Method[] methodsToExport = null; - methodsToExport = this.serviceInterfaceClass.getMethods(); - - for (Method method : methodsToExport) { - method.setAccessible(true); - - List methodModels = methods.get(method.getName()); - if (methodModels == null) { - methodModels = new ArrayList(1); - methods.put(method.getName(), methodModels); - } - methodModels.add(new ProviderMethodModel(method, serviceName)); - } - } - } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java new file mode 100644 index 0000000000..94e0ad223d --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.rpc.model; + +import org.apache.dubbo.common.utils.CollectionUtils; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +public class ServiceModel { + private final String serviceName; + private final Class serviceInterfaceClass; + // to accelarate search + private final Map> methods = new HashMap<>(); + private final Map> descToMethods = new HashMap<>(); + + public ServiceModel (Class interfaceClass) { + this.serviceInterfaceClass = interfaceClass; + this.serviceName = interfaceClass.getName(); + initMethods(); + } + + private void initMethods() { + Method[] methodsToExport = null; + methodsToExport = this.serviceInterfaceClass.getMethods(); + + for (Method method : methodsToExport) { + method.setAccessible(true); + + Set methodModels = methods.computeIfAbsent(method.getName(), (k) ->new HashSet<>(1)); + methodModels.add(new MethodModel(method)); + } + + methods.forEach((methodName, methodList) -> { + Map descMap = descToMethods.computeIfAbsent(methodName, k -> new HashMap<>()); + methodList.forEach(methodModel -> descMap.put(methodModel.getParamDesc(), methodModel)); + +// Map[], MethodModel> typesMap = typeToMethods.computeIfAbsent(methodName, k -> new HashMap<>()); +// methodList.forEach(methodModel -> typesMap.put(methodModel.getParameterClasses(), methodModel)); + }); + } + + public String getServiceName() { + return serviceName; + } + + public Class getServiceInterfaceClass() { + return serviceInterfaceClass; + } + + public Set getAllMethods () { + Set methodModels = new HashSet<>(); + methods.forEach((k, v) -> methodModels.addAll(v)); + return methodModels; + } + + public Optional getMethod (String methodName, String params) { + Map methods = descToMethods.get(methodName); + if (CollectionUtils.isNotEmptyMap(methods)) { + return Optional.ofNullable(methods.get(params)); + } + return Optional.empty(); + } + + public Optional getMethod (String methodName, Class[] paramTypes) { + Set methodModels = methods.get(methodName); + if (CollectionUtils.isNotEmpty(methodModels)) { + for (MethodModel methodModel : methodModels) { + if (Arrays.equals(paramTypes, methodModel.getParameterClasses())) { + return Optional.of(methodModel); + } + } + } + return Optional.empty(); + } + + public Set getMethods (String methodName) { + return methods.get(methodName); + } + +} diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java index fe42fb2c6b..4008c447ba 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java @@ -27,7 +27,6 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.support.RpcUtils; import java.lang.reflect.InvocationTargetException; import java.util.concurrent.CompletableFuture; @@ -112,10 +111,7 @@ public abstract class AbstractProxyInvoker implements Invoker { private CompletableFuture wrapWithFuture (Object value, Invocation invocation) { if (RpcContext.getContext().isAsyncStarted()) { return ((AsyncContextImpl)(RpcContext.getContext().getAsyncContext())).getInternalFuture(); - } else if (RpcUtils.isReturnTypeFuture(invocation)) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } + } else if (value instanceof CompletableFuture) { return (CompletableFuture) value; } return CompletableFuture.completedFuture(value); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java index 1d047cdccf..9cd1f27ca2 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java @@ -52,6 +52,6 @@ public class InvokerInvocationHandler implements InvocationHandler { return invoker.equals(args[0]); } - return invoker.invoke(new RpcInvocation(method, args)).recreate(); + return invoker.invoke(new RpcInvocation(method, invoker.getInterface().getName(), args)).recreate(); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java index 955cb31f98..2d70a9ed8e 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java @@ -27,8 +27,6 @@ import org.apache.dubbo.rpc.RpcInvocation; import java.lang.reflect.Method; import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicLong; @@ -36,7 +34,6 @@ import static org.apache.dubbo.rpc.Constants.$INVOKE; import static org.apache.dubbo.rpc.Constants.$INVOKE_ASYNC; import static org.apache.dubbo.rpc.Constants.ASYNC_KEY; import static org.apache.dubbo.rpc.Constants.AUTO_ATTACH_INVOCATIONID_KEY; -import static org.apache.dubbo.rpc.Constants.FUTURE_GENERATED_KEY; import static org.apache.dubbo.rpc.Constants.ID_KEY; import static org.apache.dubbo.rpc.Constants.RETURN_KEY; /** @@ -171,7 +168,12 @@ public class RpcUtils { } public static boolean isReturnTypeFuture(Invocation inv) { - Class clazz = getReturnType(inv); + Class clazz; + if (inv instanceof RpcInvocation) { + clazz = ((RpcInvocation) inv).getReturnType(); + } else { + clazz = getReturnType(inv); + } return (clazz != null && CompletableFuture.class.isAssignableFrom(clazz)) || isGenericAsync(inv); } @@ -198,12 +200,4 @@ public class RpcUtils { } return isOneway; } - - public static Map getNecessaryAttachments(Invocation inv) { - Map attachments = new HashMap<>(inv.getAttachments()); - attachments.remove(ASYNC_KEY); - attachments.remove(FUTURE_GENERATED_KEY); - return attachments; - } - } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExceptionFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExceptionFilterTest.java index bc9c132037..9b8ed5d7a2 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExceptionFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExceptionFilterTest.java @@ -28,7 +28,6 @@ import org.apache.dubbo.rpc.support.DemoService; import org.apache.dubbo.rpc.support.LocalException; import com.alibaba.com.caucho.hessian.HessianException; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -52,7 +51,7 @@ public class ExceptionFilterTest { RpcException exception = new RpcException("TestRpcException"); ExceptionFilter exceptionFilter = new ExceptionFilter(); - RpcInvocation invocation = new RpcInvocation("sayHello", new Class[]{String.class}, new Object[]{"world"}); + RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), new Class[]{String.class}, new Object[]{"world"}); Invoker invoker = mock(Invoker.class); given(invoker.getInterface()).willReturn(DemoService.class); given(invoker.invoke(eq(invocation))).willThrow(exception); @@ -76,7 +75,7 @@ public class ExceptionFilterTest { public void testJavaException() { ExceptionFilter exceptionFilter = new ExceptionFilter(); - RpcInvocation invocation = new RpcInvocation("sayHello", new Class[]{String.class}, new Object[]{"world"}); + RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), new Class[]{String.class}, new Object[]{"world"}); AppResponse appResponse = new AppResponse(); appResponse.setException(new IllegalArgumentException("java")); @@ -96,7 +95,7 @@ public class ExceptionFilterTest { public void testRuntimeException() { ExceptionFilter exceptionFilter = new ExceptionFilter(); - RpcInvocation invocation = new RpcInvocation("sayHello", new Class[]{String.class}, new Object[]{"world"}); + RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), new Class[]{String.class}, new Object[]{"world"}); AppResponse appResponse = new AppResponse(); appResponse.setException(new LocalException("localException")); @@ -116,7 +115,7 @@ public class ExceptionFilterTest { public void testConvertToRunTimeException() throws Exception { ExceptionFilter exceptionFilter = new ExceptionFilter(); - RpcInvocation invocation = new RpcInvocation("sayHello", new Class[]{String.class}, new Object[]{"world"}); + RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), new Class[]{String.class}, new Object[]{"world"}); AppResponse mockRpcResult = new AppResponse(); mockRpcResult.setException(new HessianException("hessian")); diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java index b571bd1de3..f3702673e2 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java @@ -36,11 +36,11 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; +import static org.apache.dubbo.rpc.Constants.$INVOKE; +import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; +import static org.apache.dubbo.rpc.Constants.GENERIC_SERIALIZATION_NATIVE_JAVA; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; -import static org.apache.dubbo.rpc.Constants.$INVOKE; -import static org.apache.dubbo.rpc.Constants.GENERIC_SERIALIZATION_NATIVE_JAVA; -import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; public class GenericFilterTest { GenericFilter genericFilter = new GenericFilter(); @@ -54,7 +54,7 @@ public class GenericFilterTest { person.put("name", "dubbo"); person.put("age", 10); - RpcInvocation invocation = new RpcInvocation($INVOKE, genericInvoke.getParameterTypes(), + RpcInvocation invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), genericInvoke.getParameterTypes(), new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}}); URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + @@ -82,7 +82,7 @@ public class GenericFilterTest { person.put("name", "dubbo"); person.put("age", 10); - RpcInvocation invocation = new RpcInvocation($INVOKE, genericInvoke.getParameterTypes(), + RpcInvocation invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), genericInvoke.getParameterTypes(), new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}}); invocation.setAttachment(GENERIC_KEY, GENERIC_SERIALIZATION_NATIVE_JAVA); @@ -106,7 +106,7 @@ public class GenericFilterTest { person.put("name", "dubbo"); person.put("age", 10); - RpcInvocation invocation = new RpcInvocation("sayHi", genericInvoke.getParameterTypes() + RpcInvocation invocation = new RpcInvocation("sayHi", GenericService.class.getName(), genericInvoke.getParameterTypes() , new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}}); URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + @@ -130,7 +130,7 @@ public class GenericFilterTest { person.put("name", "dubbo"); person.put("age", 10); - RpcInvocation invocation = new RpcInvocation($INVOKE, genericInvoke.getParameterTypes() + RpcInvocation invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), genericInvoke.getParameterTypes() , new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}}); URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java index 7145aa08ab..0d6e9fa406 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java @@ -36,11 +36,10 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.when; - import static org.apache.dubbo.rpc.Constants.$INVOKE; import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.when; public class GenericImplFilterTest { @@ -49,7 +48,7 @@ public class GenericImplFilterTest { @Test public void testInvoke() throws Exception { - RpcInvocation invocation = new RpcInvocation("getPerson", + RpcInvocation invocation = new RpcInvocation("getPerson", "org.apache.dubbo.rpc.support.DemoService", new Class[]{Person.class}, new Object[]{new Person("dubbo", 10)}); @@ -77,7 +76,7 @@ public class GenericImplFilterTest { @Test public void testInvokeWithException() throws Exception { - RpcInvocation invocation = new RpcInvocation("getPerson", + RpcInvocation invocation = new RpcInvocation("getPerson", "org.apache.dubbo.rpc.support.DemoService", new Class[]{Person.class}, new Object[]{new Person("dubbo", 10)}); URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + @@ -105,7 +104,7 @@ public class GenericImplFilterTest { person.put("name", "dubbo"); person.put("age", 10); - RpcInvocation invocation = new RpcInvocation($INVOKE, genericInvoke.getParameterTypes(), + RpcInvocation invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), genericInvoke.getParameterTypes(), new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}}); URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/AbstractProxyTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/AbstractProxyTest.java index 1bd288f12f..1abaf0e821 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/AbstractProxyTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/proxy/AbstractProxyTest.java @@ -51,7 +51,7 @@ public abstract class AbstractProxyTest { //Assertions.assertEquals(proxy.toString(), invoker.toString()); //Assertions.assertEquals(proxy.hashCode(), invoker.hashCode()); - Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", new Class[]{String.class}, new Object[]{"aa"})).getValue() + Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", DemoService.class.getName(), new Class[]{String.class}, new Object[]{"aa"})).getValue() , proxy.echo("aa")); } @@ -65,7 +65,7 @@ public abstract class AbstractProxyTest { Assertions.assertEquals(invoker.getInterface(), DemoService.class); - Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", new Class[]{String.class}, new Object[]{"aa"})).getValue(), + Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", DemoService.class.getName(), new Class[]{String.class}, new Object[]{"aa"})).getValue(), origin.echo("aa")); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java index e479445ba6..ec5b2abdf2 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvocation.java @@ -38,6 +38,11 @@ public class MockInvocation implements Invocation { return "echo"; } + @Override + public String getServiceName() { + return "DemoService"; + } + public Class[] getParameterTypes() { return new Class[]{String.class}; } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java index 6a326c295a..6e75a541d4 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcInvocation; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -28,6 +29,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static org.apache.dubbo.rpc.Constants.AUTO_ATTACH_INVOCATIONID_KEY; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -35,8 +37,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; -import static org.apache.dubbo.rpc.Constants.AUTO_ATTACH_INVOCATIONID_KEY; - public class RpcUtilsTest { /** @@ -48,7 +48,7 @@ public class RpcUtilsTest { URL url = URL.valueOf("dubbo://localhost/?test.async=true"); Map attachments = new HashMap(); attachments.put("aa", "bb"); - Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{}, attachments); + Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{}, attachments); RpcUtils.attachInvocationIdIfAsync(url, inv); long id1 = RpcUtils.getInvocationId(inv); RpcUtils.attachInvocationIdIfAsync(url, inv); @@ -65,7 +65,7 @@ public class RpcUtilsTest { @Test public void testAttachInvocationIdIfAsync_sync() { URL url = URL.valueOf("dubbo://localhost/"); - Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{}); + Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{}); RpcUtils.attachInvocationIdIfAsync(url, inv); assertNull(RpcUtils.getInvocationId(inv)); } @@ -77,7 +77,7 @@ public class RpcUtilsTest { @Test public void testAttachInvocationIdIfAsync_nullAttachments() { URL url = URL.valueOf("dubbo://localhost/?test.async=true"); - Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{}); + Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{}); RpcUtils.attachInvocationIdIfAsync(url, inv); assertTrue(RpcUtils.getInvocationId(inv) >= 0l); } @@ -89,7 +89,7 @@ public class RpcUtilsTest { @Test public void testAttachInvocationIdIfAsync_forceNotAttache() { URL url = URL.valueOf("dubbo://localhost/?test.async=true&" + AUTO_ATTACH_INVOCATIONID_KEY + "=false"); - Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{}); + Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{}); RpcUtils.attachInvocationIdIfAsync(url, inv); assertNull(RpcUtils.getInvocationId(inv)); } @@ -101,7 +101,7 @@ public class RpcUtilsTest { @Test public void testAttachInvocationIdIfAsync_forceAttache() { URL url = URL.valueOf("dubbo://localhost/?" + AUTO_ATTACH_INVOCATIONID_KEY + "=true"); - Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{}); + Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{}); RpcUtils.attachInvocationIdIfAsync(url, inv); assertNotNull(RpcUtils.getInvocationId(inv)); } @@ -109,40 +109,41 @@ public class RpcUtilsTest { @Test public void testGetReturnTypes() throws Exception { Invoker invoker = mock(Invoker.class); + String service = "org.apache.dubbo.rpc.support.DemoService"; given(invoker.getUrl()).willReturn(URL.valueOf("test://127.0.0.1:1/org.apache.dubbo.rpc.support.DemoService?interface=org.apache.dubbo.rpc.support.DemoService")); - Invocation inv = new RpcInvocation("testReturnType", new Class[]{String.class}, null, null, invoker); + Invocation inv = new RpcInvocation("testReturnType", service, new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types = RpcUtils.getReturnTypes(inv); Assertions.assertEquals(2, types.length); Assertions.assertEquals(String.class, types[0]); Assertions.assertEquals(String.class, types[1]); - Invocation inv1 = new RpcInvocation("testReturnType1", new Class[]{String.class}, null, null, invoker); + Invocation inv1 = new RpcInvocation("testReturnType1", service, new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types1 = RpcUtils.getReturnTypes(inv1); Assertions.assertEquals(2, types1.length); Assertions.assertEquals(List.class, types1[0]); Assertions.assertEquals(DemoService.class.getMethod("testReturnType1", new Class[]{String.class}).getGenericReturnType(), types1[1]); - Invocation inv2 = new RpcInvocation("testReturnType2", new Class[]{String.class}, null, null, invoker); + Invocation inv2 = new RpcInvocation("testReturnType2", service, new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types2 = RpcUtils.getReturnTypes(inv2); Assertions.assertEquals(2, types2.length); Assertions.assertEquals(String.class, types2[0]); Assertions.assertEquals(String.class, types2[1]); - Invocation inv3 = new RpcInvocation("testReturnType3", new Class[]{String.class}, null, null, invoker); + Invocation inv3 = new RpcInvocation("testReturnType3", service, new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types3 = RpcUtils.getReturnTypes(inv3); Assertions.assertEquals(2, types3.length); Assertions.assertEquals(List.class, types3[0]); java.lang.reflect.Type genericReturnType3 = DemoService.class.getMethod("testReturnType3", new Class[]{String.class}).getGenericReturnType(); Assertions.assertEquals(((ParameterizedType) genericReturnType3).getActualTypeArguments()[0], types3[1]); - Invocation inv4 = new RpcInvocation("testReturnType4", new Class[]{String.class}, null, null, invoker); + Invocation inv4 = new RpcInvocation("testReturnType4", service, new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types4 = RpcUtils.getReturnTypes(inv4); Assertions.assertEquals(2, types4.length); Assertions.assertNull(types4[0]); Assertions.assertNull(types4[1]); - Invocation inv5 = new RpcInvocation("testReturnType5", new Class[]{String.class}, null, null, invoker); + Invocation inv5 = new RpcInvocation("testReturnType5", service, new Class[]{String.class}, null, null, invoker); java.lang.reflect.Type[] types5 = RpcUtils.getReturnTypes(inv5); Assertions.assertEquals(2, types5.length); Assertions.assertEquals(Map.class, types5[0]); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java index 69a0629c4e..6d0d995222 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java @@ -22,7 +22,6 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.serialize.Cleanable; import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.utils.Assert; -import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.Codec; @@ -30,17 +29,21 @@ import org.apache.dubbo.remoting.Decodeable; import org.apache.dubbo.remoting.exchange.Request; import org.apache.dubbo.remoting.transport.CodecSupport; import org.apache.dubbo.rpc.RpcInvocation; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.MethodModel; +import org.apache.dubbo.rpc.model.ServiceModel; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import static org.apache.dubbo.rpc.protocol.dubbo.CallbackServiceCodec.decodeInvocationArgument; import static org.apache.dubbo.remoting.Constants.DUBBO_VERSION_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.CallbackServiceCodec.decodeInvocationArgument; public class DecodeableRpcInvocation extends RpcInvocation implements Codec, Decodeable { @@ -97,27 +100,37 @@ public class DecodeableRpcInvocation extends RpcInvocation implements Codec, Dec request.setVersion(dubboVersion); setAttachment(DUBBO_VERSION_KEY, dubboVersion); - setAttachment(PATH_KEY, in.readUTF()); + String path = in.readUTF(); + setAttachment(PATH_KEY, path); setAttachment(VERSION_KEY, in.readUTF()); setMethodName(in.readUTF()); + + String desc = in.readUTF(); + setParameterTypesDesc(desc); + try { - Object[] args; - Class[] pts; - String desc = in.readUTF(); - if (desc.length() == 0) { - pts = DubboCodec.EMPTY_CLASS_ARRAY; - args = DubboCodec.EMPTY_OBJECT_ARRAY; - } else { - pts = ReflectUtils.desc2classArray(desc); - args = new Object[pts.length]; - for (int i = 0; i < args.length; i++) { - try { - args[i] = in.readObject(pts[i]); - } catch (Exception e) { - if (log.isWarnEnabled()) { - log.warn("Decode argument failed: " + e.getMessage(), e); + Object[] args = DubboCodec.EMPTY_OBJECT_ARRAY; + Class[] pts = DubboCodec.EMPTY_CLASS_ARRAY; + if (desc.length() > 0) { + // TODO, lambda function requires variables to be final. + Optional serviceModel = ApplicationModel.getServiceModel(path); + if (serviceModel.isPresent()) { + Optional methodOptional = serviceModel.get().getMethod(getMethodName(), desc); + if (methodOptional.isPresent()) { + pts = methodOptional.get().getParameterClasses(); + args = new Object[pts.length]; + for (int i = 0; i < args.length; i++) { + try { + args[i] = in.readObject(pts[i]); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("Decode argument failed: " + e.getMessage(), e); + } + } } + + this.setReturnTypes(methodOptional.get().getReturnTypes()); } } } @@ -132,6 +145,7 @@ public class DecodeableRpcInvocation extends RpcInvocation implements Codec, Dec attachment.putAll(map); setAttachments(attachment); } + //decode argument ,may be callback for (int i = 0; i < args.length; i++) { args[i] = decodeInvocationArgument(channel, this, pts, i, args[i]); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java index 9515a1ba51..1a2347c07a 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java @@ -30,6 +30,7 @@ import org.apache.dubbo.remoting.exchange.Response; import org.apache.dubbo.remoting.transport.CodecSupport; import org.apache.dubbo.rpc.AppResponse; import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.support.RpcUtils; import java.io.IOException; @@ -38,6 +39,7 @@ import java.io.OutputStream; import java.lang.reflect.Type; import java.util.Map; + public class DecodeableRpcResult extends AppResponse implements Codec, Decodeable { private static final Logger log = LoggerFactory.getLogger(DecodeableRpcResult.class); @@ -124,7 +126,12 @@ public class DecodeableRpcResult extends AppResponse implements Codec, Decodeabl private void handleValue(ObjectInput in) throws IOException { try { - Type[] returnTypes = RpcUtils.getReturnTypes(invocation); + Type[] returnTypes; + if (invocation instanceof RpcInvocation) { + returnTypes = ((RpcInvocation)invocation).getReturnTypes(); + } else { + returnTypes = RpcUtils.getReturnTypes(invocation); + } Object value = null; if (ArrayUtils.isEmpty(returnTypes)) { value = in.readObject(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCodec.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCodec.java index dd3e847748..c4a165809a 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCodec.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCodec.java @@ -23,7 +23,6 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.serialize.ObjectOutput; -import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.exchange.Request; @@ -33,17 +32,16 @@ import org.apache.dubbo.remoting.transport.CodecSupport; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcInvocation; -import org.apache.dubbo.rpc.support.RpcUtils; import java.io.IOException; import java.io.InputStream; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; +import static org.apache.dubbo.remoting.Constants.DUBBO_VERSION_KEY; import static org.apache.dubbo.rpc.protocol.dubbo.CallbackServiceCodec.encodeInvocationArgument; import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DECODE_IN_IO_THREAD_KEY; import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DEFAULT_DECODE_IN_IO_THREAD; -import static org.apache.dubbo.remoting.Constants.DUBBO_VERSION_KEY; /** * Dubbo codec. @@ -179,14 +177,14 @@ public class DubboCodec extends ExchangeCodec { out.writeUTF(inv.getAttachment(VERSION_KEY)); out.writeUTF(inv.getMethodName()); - out.writeUTF(ReflectUtils.getDesc(inv.getParameterTypes())); + out.writeUTF(inv.getParameterTypesDesc()); Object[] args = inv.getArguments(); if (args != null) { for (int i = 0; i < args.length; i++) { out.writeObject(encodeInvocationArgument(channel, inv, i)); } } - out.writeObject(RpcUtils.getNecessaryAttachments(inv)); + out.writeObject(inv.getAttachments()); } @Override diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java index 3cc20237f3..ae52a671fc 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java @@ -62,27 +62,27 @@ import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import static org.apache.dubbo.rpc.Constants.LAZY_CONNECT_KEY; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.ON_CONNECT_KEY; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.ON_DISCONNECT_KEY; -import static org.apache.dubbo.remoting.Constants.DEFAULT_HEARTBEAT; -import static org.apache.dubbo.remoting.Constants.HEARTBEAT_KEY; import static org.apache.dubbo.remoting.Constants.CHANNEL_READONLYEVENT_SENT_KEY; import static org.apache.dubbo.remoting.Constants.CLIENT_KEY; import static org.apache.dubbo.remoting.Constants.CODEC_KEY; +import static org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY; +import static org.apache.dubbo.remoting.Constants.DEFAULT_HEARTBEAT; import static org.apache.dubbo.remoting.Constants.DEFAULT_REMOTING_CLIENT; +import static org.apache.dubbo.remoting.Constants.HEARTBEAT_KEY; import static org.apache.dubbo.remoting.Constants.SERVER_KEY; import static org.apache.dubbo.rpc.Constants.DEFAULT_REMOTING_SERVER; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CALLBACK_SERVICE_KEY; -import static org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY; import static org.apache.dubbo.rpc.Constants.DEFAULT_STUB_EVENT; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.IS_CALLBACK_SERVICE; import static org.apache.dubbo.rpc.Constants.IS_SERVER_KEY; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.OPTIMIZER_KEY; +import static org.apache.dubbo.rpc.Constants.LAZY_CONNECT_KEY; import static org.apache.dubbo.rpc.Constants.STUB_EVENT_KEY; import static org.apache.dubbo.rpc.Constants.STUB_EVENT_METHODS_KEY; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.SHARE_CONNECTIONS_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CALLBACK_SERVICE_KEY; import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DEFAULT_SHARE_CONNECTIONS; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.IS_CALLBACK_SERVICE; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.ON_CONNECT_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.ON_DISCONNECT_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.OPTIMIZER_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.SHARE_CONNECTIONS_KEY; /** @@ -187,13 +187,22 @@ public class DubboProtocol extends AbstractProtocol { } } + /** + * FIXME channel.getUrl() always binds to a fixed service, and this service is random. + * we can choose to use a common service to carry onConnect event if there's no easy way to get the specific + * service this connection is binding to. + * @param channel + * @param url + * @param methodKey + * @return + */ private Invocation createInvocation(Channel channel, URL url, String methodKey) { String method = url.getParameter(methodKey); if (method == null || method.length() == 0) { return null; } - RpcInvocation invocation = new RpcInvocation(method, new Class[0], new Object[0]); + RpcInvocation invocation = new RpcInvocation(method, url.getParameter(INTERFACE_KEY), new Class[0], new Object[0]); invocation.setAttachment(PATH_KEY, url.getPath()); invocation.setAttachment(GROUP_KEY, url.getParameter(GROUP_KEY)); invocation.setAttachment(INTERFACE_KEY, url.getParameter(INTERFACE_KEY)); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java index dcf732e2e5..5d34270a86 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java @@ -26,7 +26,6 @@ import org.apache.dubbo.rpc.ListenableFilter; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ConsumerMethodModel; import org.apache.dubbo.rpc.model.ConsumerModel; import java.lang.reflect.InvocationTargetException; @@ -55,7 +54,7 @@ public class FutureFilter extends ListenableFilter { } private void fireInvokeCallback(final Invoker invoker, final Invocation invocation) { - final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation); + final ConsumerModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation); if (asyncMethodInfo == null) { return; } @@ -83,7 +82,7 @@ public class FutureFilter extends ListenableFilter { } private void fireReturnCallback(final Invoker invoker, final Invocation invocation, final Object result) { - final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation); + final ConsumerModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation); if (asyncMethodInfo == null) { return; } @@ -129,7 +128,7 @@ public class FutureFilter extends ListenableFilter { } private void fireThrowCallback(final Invoker invoker, final Invocation invocation, final Throwable exception) { - final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation); + final ConsumerModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation); if (asyncMethodInfo == null) { return; } @@ -175,7 +174,7 @@ public class FutureFilter extends ListenableFilter { } } - private ConsumerMethodModel.AsyncMethodInfo getAsyncMethodInfo(Invoker invoker, Invocation invocation) { + private ConsumerModel.AsyncMethodInfo getAsyncMethodInfo(Invoker invoker, Invocation invocation) { final ConsumerModel consumerModel = ApplicationModel.getConsumerModel(invoker.getUrl().getServiceKey()); if (consumerModel == null) { return null; @@ -186,12 +185,7 @@ public class FutureFilter extends ListenableFilter { methodName = (String) invocation.getArguments()[0]; } - ConsumerMethodModel methodModel = consumerModel.getMethodModel(methodName); - if (methodModel == null) { - return null; - } - - final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = methodModel.getAsyncInfo(); + final ConsumerModel.AsyncMethodInfo asyncMethodInfo = consumerModel.getMethodConfig(methodName); if (asyncMethodInfo == null) { return null; } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java index 3998026b70..1534bb2ee5 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java @@ -25,7 +25,7 @@ import org.apache.dubbo.remoting.telnet.TelnetHandler; import org.apache.dubbo.remoting.telnet.support.Help; import org.apache.dubbo.rpc.AppResponse; import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderMethodModel; +import org.apache.dubbo.rpc.model.MethodModel; import org.apache.dubbo.rpc.model.ProviderModel; import com.alibaba.fastjson.JSON; @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; import static org.apache.dubbo.common.utils.PojoUtils.realize; @@ -152,15 +153,15 @@ public class InvokeTelnetHandler implements TelnetHandler { private boolean isServiceMatch(String service, ProviderModel provider) { - return provider.getServiceName().equalsIgnoreCase(service) + return provider.getServiceKey().equalsIgnoreCase(service) || provider.getServiceInterfaceClass().getSimpleName().equalsIgnoreCase(service) || provider.getServiceInterfaceClass().getName().equalsIgnoreCase(service) || StringUtils.isEmpty(service); } - private List findSameSignatureMethod(List methods, String lookupMethodName, List args) { + private List findSameSignatureMethod(Set methods, String lookupMethodName, List args) { List sameSignatureMethods = new ArrayList<>(); - for (ProviderMethodModel model : methods) { + for (MethodModel model : methods) { Method method = model.getMethod(); if (method.getName().equals(lookupMethodName) && method.getParameterTypes().length == args.size()) { sameSignatureMethods.add(method); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java index 0eaf1a22d0..f0953ea627 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java @@ -23,9 +23,8 @@ import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.telnet.TelnetHandler; import org.apache.dubbo.remoting.telnet.support.Help; import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ConsumerMethodModel; import org.apache.dubbo.rpc.model.ConsumerModel; -import org.apache.dubbo.rpc.model.ProviderMethodModel; +import org.apache.dubbo.rpc.model.MethodModel; import org.apache.dubbo.rpc.model.ProviderModel; import java.lang.reflect.Method; @@ -87,11 +86,11 @@ public class ListTelnetHandler implements TelnetHandler { } for (ProviderModel provider : ApplicationModel.allProviderModels()) { - buf.append(provider.getServiceName()); + buf.append(provider.getServiceKey()); if (detail) { buf.append(" -> "); buf.append(" published: "); - buf.append(isRegistered(provider.getServiceName()) ? "Y" : "N"); + buf.append(isRegistered(provider.getServiceKey()) ? "Y" : "N"); } buf.append("\r\n"); } @@ -103,11 +102,11 @@ public class ListTelnetHandler implements TelnetHandler { } for (ConsumerModel consumer : ApplicationModel.allConsumerModels()) { - buf.append(consumer.getServiceName()); + buf.append(consumer.getServiceKey()); if (detail) { buf.append(" -> "); buf.append(" addresses: "); - buf.append(getConsumerAddressNum(consumer.getServiceName())); + buf.append(getConsumerAddressNum(consumer.getServiceKey())); } } } @@ -120,8 +119,8 @@ public class ListTelnetHandler implements TelnetHandler { private void printSpecifiedProvidedService(String service, StringBuilder buf, boolean detail) { for (ProviderModel provider : ApplicationModel.allProviderModels()) { if (isProviderMatched(service,provider)) { - buf.append(provider.getServiceName()).append(" (as provider):\r\n"); - for (ProviderMethodModel method : provider.getAllMethods()) { + buf.append(provider.getServiceKey()).append(" (as provider):\r\n"); + for (MethodModel method : provider.getAllMethods()) { printMethod(method.getMethod(), buf, detail); } } @@ -131,8 +130,8 @@ public class ListTelnetHandler implements TelnetHandler { private void printSpecifiedReferredService(String service, StringBuilder buf, boolean detail) { for (ConsumerModel consumer : ApplicationModel.allConsumerModels()) { if (isConsumerMatcher(service,consumer)) { - buf.append(consumer.getServiceName()).append(" (as consumer):\r\n"); - for (ConsumerMethodModel method : consumer.getAllMethods()) { + buf.append(consumer.getServiceKey()).append(" (as consumer):\r\n"); + for (MethodModel method : consumer.getAllMethods()) { printMethod(method.getMethod(), buf, detail); } } @@ -149,13 +148,13 @@ public class ListTelnetHandler implements TelnetHandler { } private boolean isProviderMatched(String service, ProviderModel provider) { - return service.equalsIgnoreCase(provider.getServiceName()) + return service.equalsIgnoreCase(provider.getServiceKey()) || service.equalsIgnoreCase(provider.getServiceInterfaceClass().getName()) || service.equalsIgnoreCase(provider.getServiceInterfaceClass().getSimpleName()); } private boolean isConsumerMatcher(String service,ConsumerModel consumer) { - return service.equalsIgnoreCase(consumer.getServiceName()) + return service.equalsIgnoreCase(consumer.getServiceKey()) || service.equalsIgnoreCase(consumer.getServiceInterfaceClass().getName()) || service.equalsIgnoreCase(consumer.getServiceInterfaceClass().getSimpleName()); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java index 5085a447cf..ffc175fd21 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java @@ -23,7 +23,6 @@ import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ConsumerMethodModel; import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; @@ -108,31 +107,31 @@ public class ImplicitCallBackTest { public void initImplicitCallBackURL_onlyOnthrow() throws Exception { Map attitudes = new HashMap<>(); - ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = new ConsumerMethodModel.AsyncMethodInfo(); + ConsumerModel.AsyncMethodInfo asyncMethodInfo = new ConsumerModel.AsyncMethodInfo(); asyncMethodInfo.setOnthrowInstance(notify); asyncMethodInfo.setOnthrowMethod(onThrowMethod); attitudes.put("get", asyncMethodInfo); - ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, IDemoService.class.getMethods(), attitudes)); + ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, ApplicationModel.registerServiceModel(IDemoService.class), attitudes)); } //================================================================================================ public void initImplicitCallBackURL_onlyOnreturn() throws Exception { Map attitudes = new HashMap<>(); - ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = new ConsumerMethodModel.AsyncMethodInfo(); + ConsumerModel.AsyncMethodInfo asyncMethodInfo = new ConsumerModel.AsyncMethodInfo(); asyncMethodInfo.setOnreturnInstance(notify); asyncMethodInfo.setOnreturnMethod(onReturnMethod); attitudes.put("get", asyncMethodInfo); - ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, IDemoService.class.getMethods(), attitudes)); + ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, ApplicationModel.registerServiceModel(IDemoService.class), attitudes)); } public void initImplicitCallBackURL_onlyOninvoke() throws Exception { Map attitudes = new HashMap<>(); - ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = new ConsumerMethodModel.AsyncMethodInfo(); + ConsumerModel.AsyncMethodInfo asyncMethodInfo = new ConsumerModel.AsyncMethodInfo(); asyncMethodInfo.setOninvokeInstance(notify); asyncMethodInfo.setOninvokeMethod(onInvokeMethod); attitudes.put("get", asyncMethodInfo); - ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, IDemoService.class.getMethods(), attitudes)); + ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, ApplicationModel.registerServiceModel(IDemoService.class), attitudes)); } @Test diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java index 4512d29a6a..e14b5dc023 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java @@ -27,6 +27,7 @@ import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -68,7 +69,7 @@ public class InvokerTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = invoke.telnet(mockChannel, "echo(\"ok\")"); @@ -83,7 +84,7 @@ public class InvokerTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = invoke.telnet(mockChannel, "DemoService.echo(\"ok\")"); @@ -98,7 +99,7 @@ public class InvokerTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); try { invoke.telnet(mockChannel, "sayHello(null)"); @@ -114,7 +115,7 @@ public class InvokerTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = invoke.telnet(mockChannel, "getType(\"High\")"); @@ -130,7 +131,7 @@ public class InvokerTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = invoke.telnet(mockChannel, "getPerson({\"name\":\"zhangsan\",\"age\":12,\"class\":\"org.apache.dubbo.rpc.protocol.dubbo.support.Person\"})"); assertTrue(result.contains("result: 12")); @@ -144,7 +145,7 @@ public class InvokerTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String param = "{\"name\":\"Dubbo\",\"age\":8}"; String result = invoke.telnet(mockChannel, "getPerson(" + param + ")"); @@ -161,7 +162,7 @@ public class InvokerTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String param = "{\"name\":\"Dubbo\",\"age\":8},{\"name\":\"Apache\",\"age\":20}"; String result = invoke.telnet(mockChannel, "getPerson(" + param + ")"); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java index 23306a5c64..420cbd4676 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java @@ -27,8 +27,8 @@ import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.lang.reflect.Method; @@ -66,7 +66,7 @@ public class ListTelnetHandlerTest { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService"); - ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel); String result = list.telnet(mockChannel, "-l DemoService"); @@ -80,7 +80,7 @@ public class ListTelnetHandlerTest { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService"); - ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel); String result = list.telnet(mockChannel, "DemoService"); @@ -94,7 +94,7 @@ public class ListTelnetHandlerTest { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(null); - ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel); String result = list.telnet(mockChannel, ""); @@ -106,7 +106,7 @@ public class ListTelnetHandlerTest { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(null); - ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel); String result = list.telnet(mockChannel, "-l"); @@ -118,7 +118,7 @@ public class ListTelnetHandlerTest { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService"); - ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel); String result = list.telnet(mockChannel, ""); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java index df8bf470ad..fbff17bc53 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java @@ -25,6 +25,7 @@ import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -71,7 +72,7 @@ public class SelectTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = select.telnet(mockChannel, "1"); @@ -86,7 +87,7 @@ public class SelectTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = select.telnet(mockChannel, "index"); @@ -107,7 +108,7 @@ public class SelectTelnetHandlerTest { given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555")); given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886")); - ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); + ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel); String result = select.telnet(mockChannel, null); diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index a1682af280..f3d77af865 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -36,11 +36,10 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import static org.apache.dubbo.remoting.Constants.SERVER_KEY; +import static org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY; - public class RestProtocolTest { private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest"); private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); @@ -163,7 +162,7 @@ public class RestProtocolTest { Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, exportUrl)); - RpcInvocation rpcInvocation = new RpcInvocation("hello", new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3}); + RpcInvocation rpcInvocation = new RpcInvocation("hello", DemoService.class.getName(), new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3}); Result result = exporter.getInvoker().invoke(rpcInvocation); assertThat(result.getValue(), CoreMatchers.is(5)); From 5cc3821438e4f9bcf3a8503ba08e9a0008ef0f4c Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Mon, 3 Jun 2019 16:07:27 +0800 Subject: [PATCH 05/28] Reduce context switching cost by optimizing thread model on consumer side. (#4131) --- .../common/constants/CommonConstants.java | 3 + .../common/threadpool/ThreadlessExecutor.java | 150 ++++++++++++++++++ .../manager/DefaultExecutorRepository.java | 123 ++++++++++++++ .../manager/ExecutorRepository.java | 65 ++++++++ .../dubbo/common/threadpool/manager/Ring.java | 66 ++++++++ ...mmon.threadpool.manager.ExecutorRepository | 1 + .../dubbo/demo/provider/Application.java | 3 - .../dubbo/demo/provider/DemoServiceImpl.java | 5 + .../dubbo/monitor/dubbo/MetricsFilter.java | 2 +- .../dubbo/registry/dubbo/MockChannel.java | 11 ++ .../dubbo/registry/dubbo/MockedClient.java | 15 +- .../org/apache/dubbo/remoting/Constants.java | 4 - .../remoting/exchange/ExchangeChannel.java | 22 +++ .../exchange/support/DefaultFuture.java | 44 +++-- .../support/header/HeaderExchangeChannel.java | 15 +- .../support/header/HeaderExchangeClient.java | 11 ++ .../remoting/transport/AbstractClient.java | 12 +- .../remoting/transport/AbstractServer.java | 39 +---- .../dispatcher/WrappedChannelHandler.java | 88 ++++++---- .../dispatcher/all/AllChannelHandler.java | 16 +- .../ConnectionOrderedChannelHandler.java | 15 +- .../direct/DirectChannelHandler.java | 51 ++++++ .../dispatcher/direct/DirectDispatcher.java | 2 +- .../execution/ExecutionChannelHandler.java | 18 +-- .../message/MessageOnlyChannelHandler.java | 2 +- .../exchange/support/DefaultFutureTest.java | 4 +- .../header/HeaderExchangeChannelTest.java | 12 +- .../org/apache/dubbo/rpc/AsyncRpcResult.java | 63 +++++--- .../org/apache/dubbo/rpc}/FutureAdapter.java | 15 +- .../dubbo/rpc/protocol/AbstractInvoker.java | 28 +++- .../protocol/dubbo/DecodeableRpcResult.java | 1 + .../rpc/protocol/dubbo/DubboInvoker.java | 6 +- .../dubbo/LazyConnectExchangeClient.java | 15 ++ .../dubbo/ReferenceCountExchangeClient.java | 11 ++ .../dubbo/status/ThreadPoolStatusChecker.java | 4 +- .../rpc/protocol/thrift/ThriftInvoker.java | 3 - 36 files changed, 761 insertions(+), 184 deletions(-) create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/ThreadlessExecutor.java create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/DefaultExecutorRepository.java create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/ExecutorRepository.java create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/Ring.java create mode 100644 dubbo-common/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.manager.ExecutorRepository create mode 100644 dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/direct/DirectChannelHandler.java rename dubbo-rpc/{dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo => dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc}/FutureAdapter.java (86%) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java index d005ae931a..90811e85d1 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java @@ -17,6 +17,7 @@ package org.apache.dubbo.common.constants; +import java.util.concurrent.ExecutorService; import java.util.regex.Pattern; public interface CommonConstants { @@ -70,6 +71,8 @@ public interface CommonConstants { int DEFAULT_THREADS = 200; + String EXECUTOR_SERVICE_COMPONENT_KEY = ExecutorService.class.getName(); + String THREADPOOL_KEY = "threadpool"; String THREAD_NAME_KEY = "threadname"; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/ThreadlessExecutor.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/ThreadlessExecutor.java new file mode 100644 index 0000000000..322d8d9773 --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/ThreadlessExecutor.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.threadpool; + +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; + +import java.util.List; +import java.util.concurrent.AbstractExecutorService; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +/** + * The most important difference between this Executor and other normal Executor is that this one doesn't manage + * any thread. + * + * Tasks submitted to this executor through {@link #execute(Runnable)} will not get scheduled to a specific thread, though normal executors always do the schedule. + * Those tasks are stored in a blocking queue and will only be executed when a thead calls {@link #waitAndDrain()}, the thead executing the task + * is exactly the same as the one calling waitAndDrain. + */ +public class ThreadlessExecutor extends AbstractExecutorService { + private static final Logger logger = LoggerFactory.getLogger(ThreadlessExecutor.class.getName()); + + private final BlockingQueue queue = new LinkedBlockingQueue<>(); + + private ExecutorService sharedExecutor; + + private volatile boolean waiting = true; + + private final Object lock = new Object(); + + public ThreadlessExecutor(ExecutorService sharedExecutor) { + this.sharedExecutor = sharedExecutor; + } + + public boolean isWaiting() { + return waiting; + } + + /** + * Waits until there is a Runnable, then executes it and all queued Runnables after it. + */ + public void waitAndDrain() throws InterruptedException { + Runnable runnable = queue.take(); + + synchronized (lock) { + waiting = false; + runnable.run(); + } + + runnable = queue.poll(); + while (runnable != null) { + try { + runnable.run(); + } catch (Throwable t) { + logger.info(t); + + } + runnable = queue.poll(); + } + } + + public long waitAndDrain(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException { + /*long startInMs = System.currentTimeMillis(); + Runnable runnable = queue.poll(timeout, unit); + if (runnable == null) { + throw new TimeoutException(); + } + runnable.run(); + long elapsedInMs = System.currentTimeMillis() - startInMs; + long timeLeft = timeout - elapsedInMs; + if (timeLeft < 0) { + throw new TimeoutException(); + } + return timeLeft;*/ + throw new UnsupportedOperationException(); + } + + /** + * If the calling thread is still waiting for a callback task, add the task into the blocking queue to wait for schedule. + * Otherwise, submit to shared callback executor directly. + * + * @param runnable + */ + @Override + public void execute(Runnable runnable) { + synchronized (lock) { + if (!waiting) { + sharedExecutor.execute(runnable); + } else { + queue.add(runnable); + } + } + } + + /** + * tells the thread blocking on {@link #waitAndDrain()} to return, despite of the current status, to avoid endless waiting. + */ + public void notifyReturn() { + // an empty runnable task. + execute(() -> { + }); + } + + /** + * The following methods are still not supported + */ + + @Override + public void shutdown() { + + } + + @Override + public List shutdownNow() { + return null; + } + + @Override + public boolean isShutdown() { + return false; + } + + @Override + public boolean isTerminated() { + return false; + } + + @Override + public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { + return false; + } +} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/DefaultExecutorRepository.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/DefaultExecutorRepository.java new file mode 100644 index 0000000000..149905668b --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/DefaultExecutorRepository.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.threadpool.manager; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.threadpool.ThreadPool; +import org.apache.dubbo.common.utils.NamedThreadFactory; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; + +import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE; +import static org.apache.dubbo.common.constants.CommonConstants.EXECUTOR_SERVICE_COMPONENT_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.THREADS_KEY; + +/** + * Consider implementing {@code Licycle} to enable executors shutdown when the process stops. + */ +public class DefaultExecutorRepository implements ExecutorRepository { + private static final Logger logger = LoggerFactory.getLogger(DefaultExecutorRepository.class); + + private int DEFAULT_SCHEDULER_SIZE = Runtime.getRuntime().availableProcessors(); + + private final ExecutorService SHARED_EXECUTOR = Executors.newCachedThreadPool(new NamedThreadFactory("DubboSharedHandler", true)); + + private Ring scheduledExecutors = new Ring<>(); + + private ScheduledExecutorService reconnectScheduledExecutor; + + private ConcurrentMap> data = new ConcurrentHashMap<>(); + + public DefaultExecutorRepository() { +// for (int i = 0; i < DEFAULT_SCHEDULER_SIZE; i++) { +// ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Dubbo-framework-scheduler")); +// scheduledExecutors.addItem(scheduler); +// } +// +// reconnectScheduledExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Dubbo-reconnect-scheduler")); + } + + public ExecutorService createExecutorIfAbsent(URL url) { + String componentKey = EXECUTOR_SERVICE_COMPONENT_KEY; + if (CONSUMER_SIDE.equalsIgnoreCase(url.getParameter(SIDE_KEY))) { + componentKey = CONSUMER_SIDE; + } + Map executors = data.computeIfAbsent(componentKey, k -> new ConcurrentHashMap<>()); + return executors.computeIfAbsent(url.getPort(), k -> (ExecutorService) ExtensionLoader.getExtensionLoader(ThreadPool.class).getAdaptiveExtension().getExecutor(url)); + } + + public ExecutorService getExecutor(URL url) { + String componentKey = EXECUTOR_SERVICE_COMPONENT_KEY; + if (CONSUMER_SIDE.equalsIgnoreCase(url.getParameter(SIDE_KEY))) { + componentKey = CONSUMER_SIDE; + } + Map executors = data.get(componentKey); + if (executors == null) { + return null; + } + return executors.get(url.getPort()); + } + + @Override + public void updateThreadpool(URL url, ExecutorService executor) { + try { + if (url.hasParameter(THREADS_KEY) + && executor instanceof ThreadPoolExecutor && !executor.isShutdown()) { + ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; + int threads = url.getParameter(THREADS_KEY, 0); + int max = threadPoolExecutor.getMaximumPoolSize(); + int core = threadPoolExecutor.getCorePoolSize(); + if (threads > 0 && (threads != max || threads != core)) { + if (threads < core) { + threadPoolExecutor.setCorePoolSize(threads); + if (core == max) { + threadPoolExecutor.setMaximumPoolSize(threads); + } + } else { + threadPoolExecutor.setMaximumPoolSize(threads); + if (core == max) { + threadPoolExecutor.setCorePoolSize(threads); + } + } + } + } + } catch (Throwable t) { + logger.error(t.getMessage(), t); + } + } + + @Override + public ScheduledExecutorService nextScheduledExecutor() { + return scheduledExecutors.pollItem(); + } + + @Override + public ExecutorService getSharedExecutor() { + return SHARED_EXECUTOR; + } + +} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/ExecutorRepository.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/ExecutorRepository.java new file mode 100644 index 0000000000..ce42081b5b --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/ExecutorRepository.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.threadpool.manager; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.SPI; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; + +/** + * + */ +@SPI("default") +public interface ExecutorRepository { + + /** + * Called by both Client and Server. TODO, consider separate these two parts. + * When the Client or Server starts for the first time, generate a new threadpool according to the parameters passed in usr. + * + * @param url + * @return + */ + ExecutorService createExecutorIfAbsent(URL url); + + ExecutorService getExecutor(URL url); + + /** + * Modify some of the threadpool's properties according to the url, for example, coreSize, maxSize, ... + * + * @param url + * @param executor + */ + void updateThreadpool(URL url, ExecutorService executor); + + /** + * Returns a scheduler from the scheduler list, call this method whenever you need a scheduler for a cron job. + * If your cron cannot burden the possible schedule delay caused by sharing the same scheduler, please consider define a dedicate one. + * + * @return + */ + ScheduledExecutorService nextScheduledExecutor(); + + /** + * Get the default shared threadpool. + * + * @return + */ + ExecutorService getSharedExecutor(); + +} diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/Ring.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/Ring.java new file mode 100644 index 0000000000..eb9e50a9ee --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/Ring.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.threadpool.manager; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicInteger; + +public class Ring { + + AtomicInteger count = new AtomicInteger(); + + private List itemList = new CopyOnWriteArrayList(); + + public void addItem(T t) { + if (t != null) { + itemList.add(t); + } + } + + public T pollItem() { + if (itemList.isEmpty()) { + return null; + } + if (itemList.size() == 1) { + return itemList.get(0); + } + + if (count.intValue() > Integer.MAX_VALUE - 10000) { + count.set(count.get() % itemList.size()); + } + + int index = Math.abs(count.getAndIncrement()) % itemList.size(); + return itemList.get(index); + } + + public T peekItem() { + if (itemList.isEmpty()) { + return null; + } + if (itemList.size() == 1) { + return itemList.get(0); + } + int index = Math.abs(count.get()) % itemList.size(); + return itemList.get(index); + } + + public List listItems() { + return Collections.unmodifiableList(itemList); + } +} diff --git a/dubbo-common/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.manager.ExecutorRepository b/dubbo-common/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.manager.ExecutorRepository new file mode 100644 index 0000000000..44199b0219 --- /dev/null +++ b/dubbo-common/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.manager.ExecutorRepository @@ -0,0 +1 @@ +default=org.apache.dubbo.common.threadpool.manager.DefaultExecutorRepository \ No newline at end of file diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/Application.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/Application.java index f35f82ad92..25a9dd4be0 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/Application.java +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/Application.java @@ -16,8 +16,6 @@ */ package org.apache.dubbo.demo.provider; -import org.apache.dubbo.common.utils.ReflectUtils; - import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { @@ -27,7 +25,6 @@ public class Application { */ public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml"); - System.err.println(ReflectUtils.getDesc(new Class[]{String.class, String[].class, Object[].class})); context.start(); System.in.read(); } diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java index 31b9040072..5e57f77e51 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java @@ -30,6 +30,11 @@ public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); } diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java index 97b75d5ff4..2f26743706 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java @@ -57,6 +57,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_PROTOCOL; +import static org.apache.dubbo.common.constants.CommonConstants.EXECUTOR_SERVICE_COMPONENT_KEY; import static org.apache.dubbo.monitor.Constants.DUBBO_CONSUMER; import static org.apache.dubbo.monitor.Constants.DUBBO_CONSUMER_METHOD; import static org.apache.dubbo.monitor.Constants.DUBBO_GROUP; @@ -66,7 +67,6 @@ import static org.apache.dubbo.monitor.Constants.METHOD; import static org.apache.dubbo.monitor.Constants.METRICS_PORT; import static org.apache.dubbo.monitor.Constants.METRICS_PROTOCOL; import static org.apache.dubbo.monitor.Constants.SERVICE; -import static org.apache.dubbo.remoting.Constants.EXECUTOR_SERVICE_COMPONENT_KEY; public class MetricsFilter implements Filter { diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java index 7a4961bf95..e563da616c 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java @@ -24,6 +24,7 @@ import org.apache.dubbo.remoting.exchange.ExchangeHandler; import java.net.InetSocketAddress; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; public class MockChannel implements ExchangeChannel { @@ -93,6 +94,16 @@ public class MockChannel implements ExchangeChannel { return null; } + @Override + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + return null; + } + + @Override + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { + return null; + } + public ExchangeHandler getExchangeHandler() { return null; } diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java index 1afe0d550c..ad06cd2096 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java @@ -29,6 +29,7 @@ import java.net.InetSocketAddress; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeoutException; /** @@ -81,13 +82,23 @@ public class MockedClient implements ExchangeClient { } public CompletableFuture request(Object msg) throws RemotingException { - return request(msg, 0); + return request(msg, null); } public CompletableFuture request(Object msg, int timeout) throws RemotingException { + return this.request(msg, timeout, null); + } + + @Override + public CompletableFuture request(Object msg, ExecutorService executor) throws RemotingException { + return this.request(msg, 0, executor); + } + + @Override + public CompletableFuture request(Object msg, int timeout, ExecutorService executor) throws RemotingException { this.invoked = msg; return new CompletableFuture() { - public Object get() throws InterruptedException, ExecutionException { + public Object get() throws InterruptedException, ExecutionException { return received; } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Constants.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Constants.java index 8fd636183e..45e5a67405 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Constants.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Constants.java @@ -18,8 +18,6 @@ package org.apache.dubbo.remoting; -import java.util.concurrent.ExecutorService; - public interface Constants { String BUFFER_KEY = "buffer"; @@ -117,8 +115,6 @@ public interface Constants { String CHANNEL_SEND_READONLYEVENT_KEY = "channel.readonly.send"; - String EXECUTOR_SERVICE_COMPONENT_KEY = ExecutorService.class.getName(); - String RECONNECT_KEY = "reconnect"; int DEFAULT_RECONNECT_PERIOD = 2000; diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java index 0e4917d200..c0cf131d8e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java @@ -20,6 +20,7 @@ import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.RemotingException; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; /** * ExchangeChannel. (API/SPI, Prototype, ThreadSafe) @@ -33,6 +34,7 @@ public interface ExchangeChannel extends Channel { * @return response future * @throws RemotingException */ + @Deprecated CompletableFuture request(Object request) throws RemotingException; /** @@ -43,8 +45,28 @@ public interface ExchangeChannel extends Channel { * @return response future * @throws RemotingException */ + @Deprecated CompletableFuture request(Object request, int timeout) throws RemotingException; + /** + * send request. + * + * @param request + * @return response future + * @throws RemotingException + */ + CompletableFuture request(Object request, ExecutorService executor) throws RemotingException; + + /** + * send request. + * + * @param request + * @param timeout + * @return response future + * @throws RemotingException + */ + CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException; + /** * get message handler. * diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index 7f0e3a7227..0abecf2030 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -18,6 +18,7 @@ package org.apache.dubbo.remoting.exchange.support; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.threadpool.ThreadlessExecutor; import org.apache.dubbo.common.timer.HashedWheelTimer; import org.apache.dubbo.common.timer.Timeout; import org.apache.dubbo.common.timer.Timer; @@ -34,6 +35,7 @@ import java.util.Date; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; @@ -64,6 +66,16 @@ public class DefaultFuture extends CompletableFuture { private volatile long sent; private Timeout timeoutCheckTask; + private ExecutorService executor; + + public ExecutorService getExecutor() { + return executor; + } + + public void setExecutor(ExecutorService executor) { + this.executor = executor; + } + private DefaultFuture(Channel channel, Request request, int timeout) { this.channel = channel; this.request = request; @@ -92,8 +104,9 @@ public class DefaultFuture extends CompletableFuture { * @param timeout timeout * @return a new DefaultFuture */ - public static DefaultFuture newFuture(Channel channel, Request request, int timeout) { + public static DefaultFuture newFuture(Channel channel, Request request, int timeout, ExecutorService executor) { final DefaultFuture future = new DefaultFuture(channel, request, timeout); + future.setExecutor(executor); // timeout check timeoutCheck(future); return future; @@ -178,7 +191,6 @@ public class DefaultFuture extends CompletableFuture { this.cancel(true); } - private void doReceived(Response res) { if (res == null) { throw new IllegalStateException("response cannot be null"); @@ -190,6 +202,15 @@ public class DefaultFuture extends CompletableFuture { } else { this.completeExceptionally(new RemotingException(channel, res.getErrorMessage())); } + + // the result is returning, but the caller thread may still waiting + // to avoid endless waiting for whatever reason, notify caller thread to return. + if (executor != null && executor instanceof ThreadlessExecutor) { + ThreadlessExecutor threadlessExecutor = (ThreadlessExecutor) executor; + if (threadlessExecutor.isWaiting()) { + threadlessExecutor.notifyReturn(); + } + } } private long getId() { @@ -243,14 +264,17 @@ public class DefaultFuture extends CompletableFuture { if (future == null || future.isDone()) { return; } - // create exception response. - Response timeoutResponse = new Response(future.getId()); - // set timeout status. - timeoutResponse.setStatus(future.isSent() ? Response.SERVER_TIMEOUT : Response.CLIENT_TIMEOUT); - timeoutResponse.setErrorMessage(future.getTimeoutMessage(true)); - // handle response. - DefaultFuture.received(future.getChannel(), timeoutResponse, true); - + if (future.getExecutor() != null) { + future.getExecutor().execute(() -> { + // create exception response. + Response timeoutResponse = new Response(future.getId()); + // set timeout status. + timeoutResponse.setStatus(future.isSent() ? Response.SERVER_TIMEOUT : Response.CLIENT_TIMEOUT); + timeoutResponse.setErrorMessage(future.getTimeoutMessage(true)); + // handle response. + DefaultFuture.received(future.getChannel(), timeoutResponse, true); + }); + } } } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java index 2529ac7e48..f96207b604 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java @@ -31,6 +31,7 @@ import org.apache.dubbo.remoting.exchange.support.DefaultFuture; import java.net.InetSocketAddress; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; @@ -100,11 +101,21 @@ final class HeaderExchangeChannel implements ExchangeChannel { @Override public CompletableFuture request(Object request) throws RemotingException { - return request(request, channel.getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT)); + return request(request, null); } @Override public CompletableFuture request(Object request, int timeout) throws RemotingException { + return request(request, timeout, null); + } + + @Override + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + return request(request, channel.getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT), executor); + } + + @Override + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { if (closed) { throw new RemotingException(this.getLocalAddress(), null, "Failed to send request " + request + ", cause: The channel " + this + " is closed!"); } @@ -113,7 +124,7 @@ final class HeaderExchangeChannel implements ExchangeChannel { req.setVersion(Version.getProtocolVersion()); req.setTwoWay(true); req.setData(request); - DefaultFuture future = DefaultFuture.newFuture(channel, req, timeout); + DefaultFuture future = DefaultFuture.newFuture(channel, req, timeout, executor); try { channel.send(req); } catch (RemotingException e) { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java index 5a010faf52..6671d1a30b 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java @@ -31,6 +31,7 @@ import org.apache.dubbo.remoting.exchange.ExchangeHandler; import java.net.InetSocketAddress; import java.util.Collections; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import static org.apache.dubbo.remoting.utils.UrlUtils.getHeartbeat; @@ -84,6 +85,16 @@ public class HeaderExchangeClient implements ExchangeClient { return channel.request(request, timeout); } + @Override + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + return channel.request(request, executor); + } + + @Override + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { + return channel.request(request, timeout, executor); + } + @Override public ChannelHandler getChannelHandler() { return channel.getChannelHandler(); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java index e005ad533b..0241c292de 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java @@ -21,7 +21,7 @@ import org.apache.dubbo.common.Version; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.store.DataStore; +import org.apache.dubbo.common.threadpool.manager.ExecutorRepository; import org.apache.dubbo.common.utils.ExecutorUtil; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; @@ -36,7 +36,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; -import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_CLIENT_THREADPOOL; import static org.apache.dubbo.common.constants.CommonConstants.THREADPOOL_KEY; @@ -50,6 +49,7 @@ public abstract class AbstractClient extends AbstractEndpoint implements Client private final Lock connectLock = new ReentrantLock(); private final boolean needReconnect; protected volatile ExecutorService executor; + private ExecutorRepository executorRepository = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); public AbstractClient(URL url, ChannelHandler handler) throws RemotingException { super(url, handler); @@ -84,11 +84,7 @@ public abstract class AbstractClient extends AbstractEndpoint implements Client "Failed to start " + getClass().getSimpleName() + " " + NetUtils.getLocalAddress() + " connect to the server " + getRemoteAddress() + ", cause: " + t.getMessage(), t); } - - executor = (ExecutorService) ExtensionLoader.getExtensionLoader(DataStore.class) - .getDefaultExtension().get(CONSUMER_SIDE, Integer.toString(url.getPort())); - ExtensionLoader.getExtensionLoader(DataStore.class) - .getDefaultExtension().remove(CONSUMER_SIDE, Integer.toString(url.getPort())); + executor = executorRepository.createExecutorIfAbsent(url); } protected static ChannelHandler wrapChannelHandler(URL url, ChannelHandler handler) { @@ -196,7 +192,7 @@ public abstract class AbstractClient extends AbstractEndpoint implements Client } else { if (logger.isInfoEnabled()) { - logger.info("Succeed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + logger.info("Successed connect to server " + getRemoteAddress() + " from " + getClass().getSimpleName() + " " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", channel is " + this.getChannel()); } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java index d1cbdbbb0e..da5f7e326a 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java @@ -20,7 +20,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.store.DataStore; +import org.apache.dubbo.common.threadpool.manager.ExecutorRepository; import org.apache.dubbo.common.utils.ExecutorUtil; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; @@ -32,15 +32,13 @@ import org.apache.dubbo.remoting.Server; import java.net.InetSocketAddress; import java.util.Collection; import java.util.concurrent.ExecutorService; -import java.util.concurrent.ThreadPoolExecutor; import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE; -import static org.apache.dubbo.common.constants.CommonConstants.THREADS_KEY; -import static org.apache.dubbo.remoting.Constants.IDLE_TIMEOUT_KEY; -import static org.apache.dubbo.remoting.Constants.DEFAULT_IDLE_TIMEOUT; import static org.apache.dubbo.remoting.Constants.ACCEPTS_KEY; import static org.apache.dubbo.remoting.Constants.DEFAULT_ACCEPTS; +import static org.apache.dubbo.remoting.Constants.DEFAULT_IDLE_TIMEOUT; +import static org.apache.dubbo.remoting.Constants.IDLE_TIMEOUT_KEY; /** * AbstractServer @@ -55,6 +53,8 @@ public abstract class AbstractServer extends AbstractEndpoint implements Server private int accepts; private int idleTimeout; + private ExecutorRepository executorRepository = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension(); + public AbstractServer(URL url, ChannelHandler handler) throws RemotingException { super(url, handler); localAddress = getUrl().toInetSocketAddress(); @@ -76,9 +76,7 @@ public abstract class AbstractServer extends AbstractEndpoint implements Server throw new RemotingException(url.toInetSocketAddress(), null, "Failed to bind " + getClass().getSimpleName() + " on " + getLocalAddress() + ", cause: " + t.getMessage(), t); } - //fixme replace this with better method - DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension(); - executor = (ExecutorService) dataStore.get(Constants.EXECUTOR_SERVICE_COMPONENT_KEY, Integer.toString(url.getPort())); + executor = executorRepository.createExecutorIfAbsent(url); } protected abstract void doOpen() throws Throwable; @@ -110,30 +108,7 @@ public abstract class AbstractServer extends AbstractEndpoint implements Server } catch (Throwable t) { logger.error(t.getMessage(), t); } - try { - if (url.hasParameter(THREADS_KEY) - && executor instanceof ThreadPoolExecutor && !executor.isShutdown()) { - ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; - int threads = url.getParameter(THREADS_KEY, 0); - int max = threadPoolExecutor.getMaximumPoolSize(); - int core = threadPoolExecutor.getCorePoolSize(); - if (threads > 0 && (threads != max || threads != core)) { - if (threads < core) { - threadPoolExecutor.setCorePoolSize(threads); - if (core == max) { - threadPoolExecutor.setMaximumPoolSize(threads); - } - } else { - threadPoolExecutor.setMaximumPoolSize(threads); - if (core == max) { - threadPoolExecutor.setCorePoolSize(threads); - } - } - } - } - } catch (Throwable t) { - logger.error(t.getMessage(), t); - } + executorRepository.updateThreadpool(url, executor); super.setUrl(getUrl().addParameters(url.getParameters())); } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/WrappedChannelHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/WrappedChannelHandler.java index 4fe83ecd3e..a709a45318 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/WrappedChannelHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/WrappedChannelHandler.java @@ -20,29 +20,21 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.store.DataStore; -import org.apache.dubbo.common.threadpool.ThreadPool; -import org.apache.dubbo.common.utils.NamedThreadFactory; +import org.apache.dubbo.common.threadpool.manager.ExecutorRepository; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; -import org.apache.dubbo.remoting.Constants; import org.apache.dubbo.remoting.RemotingException; +import org.apache.dubbo.remoting.exchange.Request; +import org.apache.dubbo.remoting.exchange.Response; +import org.apache.dubbo.remoting.exchange.support.DefaultFuture; import org.apache.dubbo.remoting.transport.ChannelHandlerDelegate; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE; -import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; public class WrappedChannelHandler implements ChannelHandlerDelegate { protected static final Logger logger = LoggerFactory.getLogger(WrappedChannelHandler.class); - protected static final ExecutorService SHARED_EXECUTOR = Executors.newCachedThreadPool(new NamedThreadFactory("DubboSharedHandler", true)); - - protected final ExecutorService executor; - protected final ChannelHandler handler; protected final URL url; @@ -50,24 +42,10 @@ public class WrappedChannelHandler implements ChannelHandlerDelegate { public WrappedChannelHandler(ChannelHandler handler, URL url) { this.handler = handler; this.url = url; - executor = (ExecutorService) ExtensionLoader.getExtensionLoader(ThreadPool.class).getAdaptiveExtension().getExecutor(url); - - String componentKey = Constants.EXECUTOR_SERVICE_COMPONENT_KEY; - if (CONSUMER_SIDE.equalsIgnoreCase(url.getParameter(SIDE_KEY))) { - componentKey = CONSUMER_SIDE; - } - DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension(); - dataStore.put(componentKey, Integer.toString(url.getPort()), executor); } public void close() { - try { - if (executor != null) { - executor.shutdown(); - } - } catch (Throwable t) { - logger.warn("fail to destroy thread pool of server: " + t.getMessage(), t); - } + } @Override @@ -95,8 +73,16 @@ public class WrappedChannelHandler implements ChannelHandlerDelegate { handler.caught(channel, exception); } - public ExecutorService getExecutor() { - return executor; + protected void sendFeedback(Channel channel, Request request, Throwable t) throws RemotingException { + if (request.isTwoWay()) { + String msg = "Server side(" + url.getIp() + "," + url.getPort() + + ") thread pool is exhausted, detail msg:" + t.getMessage(); + Response response = new Response(request.getId(), request.getVersion()); + response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR); + response.setErrorMessage(msg); + channel.send(response); + return; + } } @Override @@ -112,12 +98,46 @@ public class WrappedChannelHandler implements ChannelHandlerDelegate { return url; } - public ExecutorService getExecutorService() { - ExecutorService cexecutor = executor; - if (cexecutor == null || cexecutor.isShutdown()) { - cexecutor = SHARED_EXECUTOR; + /** + * Currently, this method is mainly customized to facilitate the thread model on consumer side. + * 1. Use ThreadlessExecutor, aka., delegate callback directly to the thread initiating the call. + * 2. Use shared executor to execute the callback. + * + * @param msg + * @return + */ + public ExecutorService getPreferredExecutorService(Object msg) { + if (msg instanceof Response) { + Response response = (Response) msg; + DefaultFuture responseFuture = DefaultFuture.getFuture(response.getId()); + // a typical scenario is the response returned after timeout, the timeout response may has completed the future + if (responseFuture == null) { + return getSharedExecutorService(); + } else { + ExecutorService executor = responseFuture.getExecutor(); + if (executor == null || executor.isShutdown()) { + executor = getSharedExecutorService(); + } + return executor; + } + } else { + return getSharedExecutorService(); } - return cexecutor; } + /** + * get the shared executor for current Server or Client + * + * @return + */ + public ExecutorService getSharedExecutorService() { + return ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension().createExecutorIfAbsent(url); + } + + @Deprecated + public ExecutorService getExecutorService() { + return getSharedExecutorService(); + } + + } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/all/AllChannelHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/all/AllChannelHandler.java index 3ea9d40a67..25158d0d21 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/all/AllChannelHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/all/AllChannelHandler.java @@ -22,7 +22,6 @@ import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.ExecutionException; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.exchange.Request; -import org.apache.dubbo.remoting.exchange.Response; import org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable; import org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.ChannelState; import org.apache.dubbo.remoting.transport.dispatcher.WrappedChannelHandler; @@ -58,22 +57,13 @@ public class AllChannelHandler extends WrappedChannelHandler { @Override public void received(Channel channel, Object message) throws RemotingException { - ExecutorService executor = getExecutorService(); + ExecutorService executor = getPreferredExecutorService(message); try { executor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message)); } catch (Throwable t) { - //TODO A temporary solution to the problem that the exception information can not be sent to the opposite end after the thread pool is full. Need a refactoring - //fix The thread pool is full, refuses to call, does not return, and causes the consumer to wait for time out if(message instanceof Request && t instanceof RejectedExecutionException){ - Request request = (Request)message; - if(request.isTwoWay()){ - String msg = "Server side(" + url.getIp() + "," + url.getPort() + ") threadpool is exhausted ,detail msg:" + t.getMessage(); - Response response = new Response(request.getId(), request.getVersion()); - response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR); - response.setErrorMessage(msg); - channel.send(response); - return; - } + sendFeedback(channel, (Request) message, t); + return; } throw new ExecutionException(message, channel, getClass() + " error when process received event .", t); } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/connection/ConnectionOrderedChannelHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/connection/ConnectionOrderedChannelHandler.java index 33bc2791bb..2132cac9d1 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/connection/ConnectionOrderedChannelHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/connection/ConnectionOrderedChannelHandler.java @@ -24,7 +24,6 @@ import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.ExecutionException; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.exchange.Request; -import org.apache.dubbo.remoting.exchange.Response; import org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable; import org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.ChannelState; import org.apache.dubbo.remoting.transport.dispatcher.WrappedChannelHandler; @@ -80,21 +79,13 @@ public class ConnectionOrderedChannelHandler extends WrappedChannelHandler { @Override public void received(Channel channel, Object message) throws RemotingException { - ExecutorService executor = getExecutorService(); + ExecutorService executor = getPreferredExecutorService(message); try { executor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message)); } catch (Throwable t) { - //fix, reject exception can not be sent to consumer because thread pool is full, resulting in consumers waiting till timeout. if (message instanceof Request && t instanceof RejectedExecutionException) { - Request request = (Request) message; - if (request.isTwoWay()) { - String msg = "Server side(" + url.getIp() + "," + url.getPort() + ") threadpool is exhausted ,detail msg:" + t.getMessage(); - Response response = new Response(request.getId(), request.getVersion()); - response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR); - response.setErrorMessage(msg); - channel.send(response); - return; - } + sendFeedback(channel, (Request) message, t); + return; } throw new ExecutionException(message, channel, getClass() + " error when process received event .", t); } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/direct/DirectChannelHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/direct/DirectChannelHandler.java new file mode 100644 index 0000000000..9bb8d1f40f --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/direct/DirectChannelHandler.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.remoting.transport.dispatcher.direct; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.threadpool.ThreadlessExecutor; +import org.apache.dubbo.remoting.Channel; +import org.apache.dubbo.remoting.ChannelHandler; +import org.apache.dubbo.remoting.ExecutionException; +import org.apache.dubbo.remoting.RemotingException; +import org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable; +import org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.ChannelState; +import org.apache.dubbo.remoting.transport.dispatcher.WrappedChannelHandler; + +import java.util.concurrent.ExecutorService; + +public class DirectChannelHandler extends WrappedChannelHandler { + + public DirectChannelHandler(ChannelHandler handler, URL url) { + super(handler, url); + } + + @Override + public void received(Channel channel, Object message) throws RemotingException { + ExecutorService executor = getPreferredExecutorService(message); + if (executor instanceof ThreadlessExecutor) { + try { + executor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message)); + } catch (Throwable t) { + throw new ExecutionException(message, channel, getClass() + " error when process received event .", t); + } + } else { + handler.received(channel, message); + } + } + +} diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/direct/DirectDispatcher.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/direct/DirectDispatcher.java index f18065d9e7..aaed4e7922 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/direct/DirectDispatcher.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/direct/DirectDispatcher.java @@ -29,7 +29,7 @@ public class DirectDispatcher implements Dispatcher { @Override public ChannelHandler dispatch(ChannelHandler handler, URL url) { - return handler; + return new DirectChannelHandler(handler, url); } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/execution/ExecutionChannelHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/execution/ExecutionChannelHandler.java index e39d138af1..761e26ca6d 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/execution/ExecutionChannelHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/execution/ExecutionChannelHandler.java @@ -17,12 +17,12 @@ package org.apache.dubbo.remoting.transport.dispatcher.execution; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.threadpool.ThreadlessExecutor; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.ExecutionException; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.exchange.Request; -import org.apache.dubbo.remoting.exchange.Response; import org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable; import org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.ChannelState; import org.apache.dubbo.remoting.transport.dispatcher.WrappedChannelHandler; @@ -42,7 +42,8 @@ public class ExecutionChannelHandler extends WrappedChannelHandler { @Override public void received(Channel channel, Object message) throws RemotingException { - ExecutorService executor = getExecutorService(); + ExecutorService executor = getPreferredExecutorService(message); + if (message instanceof Request) { try { executor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message)); @@ -51,19 +52,12 @@ public class ExecutionChannelHandler extends WrappedChannelHandler { // therefore the consumer side has to wait until gets timeout. This is a temporary solution to prevent // this scenario from happening, but a better solution should be considered later. if (t instanceof RejectedExecutionException) { - Request request = (Request) message; - if (request.isTwoWay()) { - String msg = "Server side(" + url.getIp() + "," + url.getPort() - + ") thread pool is exhausted, detail msg:" + t.getMessage(); - Response response = new Response(request.getId(), request.getVersion()); - response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR); - response.setErrorMessage(msg); - channel.send(response); - return; - } + sendFeedback(channel, (Request) message, t); } throw new ExecutionException(message, channel, getClass() + " error when process received event.", t); } + } else if (executor instanceof ThreadlessExecutor) { + executor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message)); } else { handler.received(channel, message); } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/message/MessageOnlyChannelHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/message/MessageOnlyChannelHandler.java index 2f51860578..2cd20cc43a 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/message/MessageOnlyChannelHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/message/MessageOnlyChannelHandler.java @@ -35,7 +35,7 @@ public class MessageOnlyChannelHandler extends WrappedChannelHandler { @Override public void received(Channel channel, Object message) throws RemotingException { - ExecutorService executor = getExecutorService(); + ExecutorService executor = getPreferredExecutorService(message); try { executor.execute(new ChannelEventRunnable(channel, handler, ChannelState.RECEIVED, message)); } catch (Throwable t) { diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java index 2dd400545a..0f19d157a7 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java @@ -95,7 +95,7 @@ public class DefaultFutureTest { // timeout after 5 seconds. Channel channel = new MockedChannel(); Request request = new Request(10); - DefaultFuture f = DefaultFuture.newFuture(channel, request, 5000); + DefaultFuture f = DefaultFuture.newFuture(channel, request, 5000, null); //mark the future is sent DefaultFuture.sent(channel, request); while (!f.isDone()) { @@ -119,7 +119,7 @@ public class DefaultFutureTest { private DefaultFuture defaultFuture(int timeout) { Channel channel = new MockedChannel(); Request request = new Request(index.getAndIncrement()); - return DefaultFuture.newFuture(channel, request, timeout); + return DefaultFuture.newFuture(channel, request, timeout, null); } } \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java index 92739bfef9..2affe7d513 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java @@ -17,23 +17,21 @@ package org.apache.dubbo.remoting.exchange.support.header; import org.apache.dubbo.common.URL; +import org.apache.dubbo.remoting.Channel; +import org.apache.dubbo.remoting.RemotingException; +import org.apache.dubbo.remoting.exchange.Request; import org.apache.dubbo.remoting.exchange.support.DefaultFuture; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.RemotingException; -import org.apache.dubbo.remoting.exchange.Request; - import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import java.util.List; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class HeaderExchangeChannelTest { @@ -193,7 +191,7 @@ public class HeaderExchangeChannelTest { public void closeWithTimeoutTest02() { Assertions.assertFalse(channel.isClosed()); Request request = new Request(); - DefaultFuture.newFuture(channel, request, 100); + DefaultFuture.newFuture(channel, request, 100, null); header.close(100); //return directly header.close(1000); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java index 815bec562e..978bf7b888 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java @@ -18,10 +18,14 @@ package org.apache.dubbo.rpc; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.threadpool.ThreadlessExecutor; import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.function.Function; /** @@ -47,6 +51,7 @@ public class AsyncRpcResult extends AbstractResult { */ private RpcContext storedContext; private RpcContext storedServerContext; + private Executor executor; private Invocation invocation; @@ -99,7 +104,7 @@ public class AsyncRpcResult extends AbstractResult { public Result getAppResponse() { try { if (this.isDone()) { - return this.get(); + return super.get(); } } catch (Exception e) { // This should never happen; @@ -108,32 +113,36 @@ public class AsyncRpcResult extends AbstractResult { return new AppResponse(); } + /** + * This method will always return after a maximum 'timeout' waiting: + * 1. if value returns before timeout, return normally. + * 2. if no value returns after timeout, throw TimeoutException. + * + * @return + * @throws InterruptedException + * @throws ExecutionException + */ + @Override + public Result get() throws InterruptedException, ExecutionException { + if (executor != null) { + ThreadlessExecutor threadlessExecutor = (ThreadlessExecutor) executor; + threadlessExecutor.waitAndDrain(); + } + return super.get(); + } + + @Override + public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { + return this.get(); + } + @Override public Object recreate() throws Throwable { RpcInvocation rpcInvocation = (RpcInvocation) invocation; if (InvokeMode.FUTURE == rpcInvocation.getInvokeMode()) { - AppResponse appResponse = new AppResponse(); - CompletableFuture future = new CompletableFuture<>(); - appResponse.setValue(future); - this.whenComplete((result, t) -> { - if (t != null) { - if (t instanceof CompletionException) { - t = t.getCause(); - } - future.completeExceptionally(t); - } else { - if (result.hasException()) { - future.completeExceptionally(result.getException()); - } else { - future.complete(result.getValue()); - } - } - }); - return appResponse.recreate(); - } else if (this.isDone()) { - return this.get().recreate(); + return RpcContext.getContext().getFuture(); } - return (new AppResponse()).recreate(); + return getAppResponse().recreate(); } @Override @@ -196,6 +205,14 @@ public class AsyncRpcResult extends AbstractResult { return invocation; } + public Executor getExecutor() { + return executor; + } + + public void setExecutor(Executor executor) { + this.executor = executor; + } + /** * tmp context to use when the thread switch to Dubbo thread. */ diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/FutureAdapter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/FutureAdapter.java similarity index 86% rename from dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/FutureAdapter.java rename to dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/FutureAdapter.java index 03954d1b34..d50f91e019 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/FutureAdapter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/FutureAdapter.java @@ -14,11 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.protocol.dubbo; - -import org.apache.dubbo.rpc.AppResponse; -import org.apache.dubbo.rpc.RpcException; +package org.apache.dubbo.rpc; +import java.lang.ref.SoftReference; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; @@ -32,7 +30,10 @@ public class FutureAdapter extends CompletableFuture { private CompletableFuture appResponseFuture; - public FutureAdapter(CompletableFuture future) { + private SoftReference invocationSoftReference; + + public FutureAdapter(CompletableFuture future, Invocation invocation) { + this.invocationSoftReference = new SoftReference<>(invocation); this.appResponseFuture = future; future.whenComplete((appResponse, t) -> { if (t != null) { @@ -53,6 +54,10 @@ public class FutureAdapter extends CompletableFuture { // TODO figure out the meaning of cancel in DefaultFuture. @Override public boolean cancel(boolean mayInterruptIfRunning) { +// Invocation invocation = invocationSoftReference.get(); +// if (invocation != null) { +// invocation.getInvoker().invoke(cancel); +// } return appResponseFuture.cancel(mayInterruptIfRunning); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java index 58cb4105f8..be32bb5498 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java @@ -18,13 +18,18 @@ package org.apache.dubbo.rpc.protocol; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.Version; +import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.threadpool.ThreadlessExecutor; +import org.apache.dubbo.common.threadpool.manager.ExecutorRepository; import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.AsyncRpcResult; +import org.apache.dubbo.rpc.FutureAdapter; import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.InvokeMode; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; @@ -36,6 +41,7 @@ import java.lang.reflect.InvocationTargetException; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -151,26 +157,38 @@ public abstract class AbstractInvoker implements Invoker { invocation.setInvokeMode(RpcUtils.getInvokeMode(url, invocation)); RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation); + AsyncRpcResult asyncResult; try { - return doInvoke(invocation); + asyncResult = (AsyncRpcResult) doInvoke(invocation); } catch (InvocationTargetException e) { // biz exception Throwable te = e.getTargetException(); if (te == null) { - return AsyncRpcResult.newDefaultAsyncResult(null, e, invocation); + asyncResult = AsyncRpcResult.newDefaultAsyncResult(null, e, invocation); } else { if (te instanceof RpcException) { ((RpcException) te).setCode(RpcException.BIZ_EXCEPTION); } - return AsyncRpcResult.newDefaultAsyncResult(null, te, invocation); + asyncResult = AsyncRpcResult.newDefaultAsyncResult(null, te, invocation); } } catch (RpcException e) { if (e.isBiz()) { - return AsyncRpcResult.newDefaultAsyncResult(null, e, invocation); + asyncResult = AsyncRpcResult.newDefaultAsyncResult(null, e, invocation); } else { throw e; } } catch (Throwable e) { - return AsyncRpcResult.newDefaultAsyncResult(null, e, invocation); + asyncResult = AsyncRpcResult.newDefaultAsyncResult(null, e, invocation); + } + RpcContext.getContext().setFuture(new FutureAdapter(asyncResult, inv)); + return asyncResult; + } + + protected ExecutorService getCallbackExecutor(URL url, Invocation inv) { + ExecutorService sharedExecutor = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension().getExecutor(url); + if (InvokeMode.SYNC == RpcUtils.getInvokeMode(getUrl(), inv)) { + return new ThreadlessExecutor(sharedExecutor); + } else { + return sharedExecutor; } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java index 1a2347c07a..2a190ad896 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java @@ -74,6 +74,7 @@ public class DecodeableRpcResult extends AppResponse implements Codec, Decodeabl @Override public Object decode(Channel channel, InputStream input) throws IOException { + log.debug("Decoding in thread -- " + Thread.currentThread().getName()); ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType) .deserialize(channel.getUrl(), input); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java index ece71497c5..4cf6259be5 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java @@ -35,6 +35,7 @@ import org.apache.dubbo.rpc.support.RpcUtils; import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; import java.util.concurrent.locks.ReentrantLock; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; @@ -95,9 +96,10 @@ public class DubboInvoker extends AbstractInvoker { return AsyncRpcResult.newDefaultAsyncResult(invocation); } else { AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); - CompletableFuture responseFuture = currentClient.request(inv, timeout); + ExecutorService executor = getCallbackExecutor(getUrl(), inv); + asyncRpcResult.setExecutor(executor); + CompletableFuture responseFuture = currentClient.request(inv, timeout, executor); asyncRpcResult.subscribeTo(responseFuture); - RpcContext.getContext().setFuture(new FutureAdapter(asyncRpcResult)); return asyncRpcResult; } } catch (TimeoutException e) { 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 a495775d11..77eae2cefc 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 @@ -29,6 +29,7 @@ import org.apache.dubbo.remoting.exchange.Exchangers; import java.net.InetSocketAddress; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -114,6 +115,20 @@ final class LazyConnectExchangeClient implements ExchangeClient { return client.request(request, timeout); } + @Override + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + warning(); + initClient(); + return client.request(request, executor); + } + + @Override + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { + warning(); + initClient(); + return client.request(request, timeout, executor); + } + /** * If {@link #REQUEST_WITH_WARNING_KEY} is configured, then warn once every 5000 invocations. */ 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 c7074aa9db..ed3c61b2af 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 @@ -27,6 +27,7 @@ import org.apache.dubbo.remoting.exchange.ExchangeHandler; import java.net.InetSocketAddress; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicInteger; import static org.apache.dubbo.remoting.Constants.RECONNECT_KEY; @@ -80,6 +81,16 @@ final class ReferenceCountExchangeClient implements ExchangeClient { return client.request(request, timeout); } + @Override + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + return client.request(request, executor); + } + + @Override + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { + return client.request(request, timeout, executor); + } + @Override public boolean isConnected() { return client.isConnected(); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/status/ThreadPoolStatusChecker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/status/ThreadPoolStatusChecker.java index 8506f1be42..ea623c807a 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/status/ThreadPoolStatusChecker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/status/ThreadPoolStatusChecker.java @@ -16,12 +16,12 @@ */ package org.apache.dubbo.rpc.protocol.dubbo.status; +import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.status.Status; import org.apache.dubbo.common.status.StatusChecker; import org.apache.dubbo.common.store.DataStore; -import org.apache.dubbo.remoting.Constants; import java.util.Map; import java.util.concurrent.ExecutorService; @@ -36,7 +36,7 @@ public class ThreadPoolStatusChecker implements StatusChecker { @Override public Status check() { DataStore dataStore = ExtensionLoader.getExtensionLoader(DataStore.class).getDefaultExtension(); - Map executors = dataStore.get(Constants.EXECUTOR_SERVICE_COMPONENT_KEY); + Map executors = dataStore.get(CommonConstants.EXECUTOR_SERVICE_COMPONENT_KEY); StringBuilder msg = new StringBuilder(); Status.Level level = Status.Level.OK; diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java index 3b0e3d9352..3d0aee9d03 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java @@ -26,11 +26,9 @@ import org.apache.dubbo.rpc.AsyncRpcResult; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; -import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.protocol.AbstractInvoker; -import org.apache.dubbo.rpc.protocol.dubbo.FutureAdapter; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -97,7 +95,6 @@ public class ThriftInvoker extends AbstractInvoker { AsyncRpcResult asyncRpcResult = new AsyncRpcResult(invocation); CompletableFuture responseFuture = currentClient.request(inv, timeout); asyncRpcResult.subscribeTo(responseFuture); - RpcContext.getContext().setFuture(new FutureAdapter(asyncRpcResult)); return asyncRpcResult; } catch (TimeoutException e) { throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e); From b20f72e1051ce4d837232729380aec3283ca4fb2 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Mon, 3 Jun 2019 17:03:54 +0800 Subject: [PATCH 06/28] Perf tuning: reduce string allocation in URL.getParameter (#4240) --- .../java/org/apache/dubbo/common/URL.java | 113 ++++++++++++++---- .../org/apache/dubbo/common/URLBuilder.java | 42 ++++++- .../common/constants/CommonConstants.java | 6 + .../dubbo/common}/model/ApplicationModel.java | 3 +- .../dubbo/common}/model/ConsumerModel.java | 2 +- .../dubbo/common}/model/MethodModel.java | 6 +- .../dubbo/common}/model/ProviderModel.java | 2 +- .../dubbo/common}/model/ServiceModel.java | 2 +- .../apache/dubbo/config/MethodConfigTest.java | 3 +- .../apache/dubbo/config/AbstractConfig.java | 2 +- .../dubbo/config/AbstractInterfaceConfig.java | 28 ++--- .../apache/dubbo/config/ReferenceConfig.java | 6 +- .../apache/dubbo/config/ServiceConfig.java | 4 +- .../monitor/dubbo/DubboMonitorFactory.java | 2 +- .../dubbo/monitor/dubbo/MetricsFilter.java | 2 +- .../org/apache/dubbo/qos/command/impl/Ls.java | 6 +- .../dubbo/qos/command/impl/Offline.java | 4 +- .../apache/dubbo/qos/command/impl/Online.java | 4 +- .../apache/dubbo/qos/command/impl/LsTest.java | 6 +- .../dubbo/qos/command/impl/OfflineTest.java | 4 +- .../dubbo/qos/command/impl/OnlineTest.java | 4 +- .../integration/RegistryDirectory.java | 12 +- .../integration/RegistryProtocol.java | 38 +++--- .../registry/dubbo/RegistryDirectoryTest.java | 2 +- .../java/org/apache/dubbo/rpc/Constants.java | 2 - .../org/apache/dubbo/rpc/RpcInvocation.java | 2 +- .../dubbo/rpc/filter/GenericFilter.java | 4 +- .../dubbo/rpc/filter/GenericImplFilter.java | 4 +- .../apache/dubbo/rpc/support/RpcUtils.java | 4 +- .../dubbo/rpc/filter/GenericFilterTest.java | 2 +- .../rpc/filter/GenericImplFilterTest.java | 2 +- .../dubbo/DecodeableRpcInvocation.java | 6 +- .../protocol/dubbo/filter/FutureFilter.java | 6 +- .../dubbo/telnet/InvokeTelnetHandler.java | 6 +- .../dubbo/telnet/ListTelnetHandler.java | 8 +- .../protocol/dubbo/ImplicitCallBackTest.java | 4 +- .../telnet/InvokerTelnetHandlerTest.java | 4 +- .../dubbo/telnet/ListTelnetHandlerTest.java | 4 +- .../dubbo/telnet/SelectTelnetHandlerTest.java | 4 +- .../dubbo/rpc/protocol/rest/RestProtocol.java | 2 +- .../rpc/protocol/rest/RestProtocolTest.java | 4 +- 41 files changed, 242 insertions(+), 129 deletions(-) rename {dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc => dubbo-common/src/main/java/org/apache/dubbo/common}/model/ApplicationModel.java (99%) rename {dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc => dubbo-common/src/main/java/org/apache/dubbo/common}/model/ConsumerModel.java (99%) rename {dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc => dubbo-common/src/main/java/org/apache/dubbo/common}/model/MethodModel.java (92%) rename {dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc => dubbo-common/src/main/java/org/apache/dubbo/common}/model/ProviderModel.java (97%) rename {dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc => dubbo-common/src/main/java/org/apache/dubbo/common}/model/ServiceModel.java (98%) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java index 11d89cb243..5dac550f9e 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java @@ -19,6 +19,8 @@ package org.apache.dubbo.common; import org.apache.dubbo.common.config.Configuration; import org.apache.dubbo.common.config.InmemoryConfiguration; import org.apache.dubbo.common.constants.RemotingConstants; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.MethodModel; import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; @@ -37,6 +39,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; @@ -107,10 +110,14 @@ class URL implements Serializable { private final Map parameters; + private final Map> methodParameters; + // ==== cache ==== private volatile transient Map numbers; + private volatile transient Map> methodNumbers; + private volatile transient Map urls; private volatile transient String ip; @@ -131,6 +138,7 @@ class URL implements Serializable { this.port = 0; this.path = null; this.parameters = null; + this.methodParameters = null; } public URL(String protocol, String host, int port) { @@ -165,7 +173,24 @@ class URL implements Serializable { this(protocol, username, password, host, port, path, CollectionUtils.toStringMap(pairs)); } - public URL(String protocol, String username, String password, String host, int port, String path, Map parameters) { + public URL(String protocol, + String username, + String password, + String host, + int port, + String path, + Map parameters) { + this (protocol, username, password, host, port, path, parameters, toMethodParameters(parameters, path)); + } + + public URL(String protocol, + String username, + String password, + String host, + int port, + String path, + Map parameters, + Map> methodParameters) { if (StringUtils.isEmpty(username) && StringUtils.isNotEmpty(password)) { throw new IllegalArgumentException("Invalid url, password without username!"); @@ -186,6 +211,7 @@ class URL implements Serializable { parameters = new HashMap<>(parameters); } this.parameters = Collections.unmodifiableMap(parameters); + this.methodParameters = Collections.unmodifiableMap(methodParameters); } /** @@ -272,9 +298,32 @@ class URL implements Serializable { if (url.length() > 0) { host = url; } + return new URL(protocol, username, password, host, port, path, parameters); } + public static Map> toMethodParameters(Map parameters, String path) { + Map> methodParameters = new HashMap<>(); + if (parameters != null) { + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + int methodSeparator = key.indexOf("."); + if (methodSeparator > 0) { + String method = key.substring(0, methodSeparator); + String realKey = key.substring(methodSeparator + 1); + ApplicationModel.getServiceModel(path).ifPresent(serviceModel -> { + Set methodModels = serviceModel.getMethods(method); + if (CollectionUtils.isNotEmpty(methodModels)) { + Map subParameter = methodParameters.computeIfAbsent(method, k -> new HashMap<>()); + subParameter.put(realKey, entry.getValue()); + } + }); + } + } + } + return methodParameters; + } + public static URL valueOf(String url, String... reserveParams) { URL result = valueOf(url); if (reserveParams == null || reserveParams.length == 0) { @@ -482,6 +531,10 @@ class URL implements Serializable { return parameters; } + public Map> getMethodParameters() { + return methodParameters; + } + public String getParameterAndDecoded(String key) { return getParameterAndDecoded(key, null); } @@ -526,6 +579,13 @@ class URL implements Serializable { return numbers; } + private Map> getMethodNumbers() { + if (methodNumbers == null) { // concurrent initialization is tolerant + methodNumbers = new ConcurrentHashMap<>(); + } + return methodNumbers; + } + private Map getUrls() { if (urls == null) { // concurrent initialization is tolerant urls = new ConcurrentHashMap<>(); @@ -727,9 +787,13 @@ class URL implements Serializable { } public String getMethodParameter(String method, String key) { - String value = parameters.get(method + "." + key); + Map keyMap = methodParameters.get(method); + String value = null; + if (keyMap != null) { + value = keyMap.get(key); + } if (StringUtils.isEmpty(value)) { - return getParameter(key); + value = parameters.get(key); } return value; } @@ -743,8 +807,7 @@ class URL implements Serializable { } public double getMethodParameter(String method, String key, double defaultValue) { - String methodKey = method + "." + key; - Number n = getNumbers().get(methodKey); + Number n = getCachedNumber(method, key); if (n != null) { return n.doubleValue(); } @@ -753,13 +816,12 @@ class URL implements Serializable { return defaultValue; } double d = Double.parseDouble(value); - getNumbers().put(methodKey, d); + updateCachedNumber(method, key, d); return d; } public float getMethodParameter(String method, String key, float defaultValue) { - String methodKey = method + "." + key; - Number n = getNumbers().get(methodKey); + Number n = getCachedNumber(method, key); if (n != null) { return n.floatValue(); } @@ -768,13 +830,12 @@ class URL implements Serializable { return defaultValue; } float f = Float.parseFloat(value); - getNumbers().put(methodKey, f); + updateCachedNumber(method, key, f); return f; } public long getMethodParameter(String method, String key, long defaultValue) { - String methodKey = method + "." + key; - Number n = getNumbers().get(methodKey); + Number n = getCachedNumber(method, key); if (n != null) { return n.longValue(); } @@ -783,13 +844,12 @@ class URL implements Serializable { return defaultValue; } long l = Long.parseLong(value); - getNumbers().put(methodKey, l); + updateCachedNumber(method, key, l); return l; } public int getMethodParameter(String method, String key, int defaultValue) { - String methodKey = method + "." + key; - Number n = getNumbers().get(methodKey); + Number n = getCachedNumber(method, key); if (n != null) { return n.intValue(); } @@ -798,13 +858,12 @@ class URL implements Serializable { return defaultValue; } int i = Integer.parseInt(value); - getNumbers().put(methodKey, i); + updateCachedNumber(method, key, i); return i; } public short getMethodParameter(String method, String key, short defaultValue) { - String methodKey = method + "." + key; - Number n = getNumbers().get(methodKey); + Number n = getCachedNumber(method, key); if (n != null) { return n.shortValue(); } @@ -813,13 +872,12 @@ class URL implements Serializable { return defaultValue; } short s = Short.parseShort(value); - getNumbers().put(methodKey, s); + updateCachedNumber(method, key, s); return s; } public byte getMethodParameter(String method, String key, byte defaultValue) { - String methodKey = method + "." + key; - Number n = getNumbers().get(methodKey); + Number n = getCachedNumber(method, key); if (n != null) { return n.byteValue(); } @@ -828,10 +886,23 @@ class URL implements Serializable { return defaultValue; } byte b = Byte.parseByte(value); - getNumbers().put(methodKey, b); + updateCachedNumber(method, key, b); return b; } + private Number getCachedNumber(String method, String key) { + Map keyNumber = getMethodNumbers().get(method); + if (keyNumber != null) { + return keyNumber.get(key); + } + return null; + } + + private void updateCachedNumber(String method, String key, Number n) { + Map keyNumber = getMethodNumbers().computeIfAbsent(method, m -> new HashMap<>()); + keyNumber.put(key, n); + } + public double getMethodPositiveParameter(String method, String key, double defaultValue) { if (defaultValue <= 0) { throw new IllegalArgumentException("defaultValue <= 0"); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java index 677cbe083f..b1c1a3d694 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java @@ -41,6 +41,8 @@ public final class URLBuilder { private Map parameters; + private Map> methodParameters; + public URLBuilder() { protocol = null; username = null; @@ -49,6 +51,7 @@ public final class URLBuilder { port = 0; path = null; parameters = new HashMap<>(); + methodParameters = new HashMap<>(); } public URLBuilder(String protocol, String host, int port) { @@ -75,7 +78,22 @@ public final class URLBuilder { this(protocol, null, null, host, port, path, parameters); } - public URLBuilder(String protocol, String username, String password, String host, int port, String path, Map parameters) { + public URLBuilder(String protocol, + String username, + String password, + String host, + int port, + String path, Map parameters) { + this(protocol, username, password, host, port, path, parameters, URL.toMethodParameters(parameters, path)); + } + + public URLBuilder(String protocol, + String username, + String password, + String host, + int port, + String path, Map parameters, + Map> methodParameters) { this.protocol = protocol; this.username = username; this.password = password; @@ -83,6 +101,7 @@ public final class URLBuilder { this.port = port; this.path = path; this.parameters = parameters != null ? parameters : new HashMap<>(); + this.methodParameters = (methodParameters != null ? methodParameters : new HashMap<>()); } public static URLBuilder from(URL url) { @@ -120,7 +139,7 @@ public final class URLBuilder { path = path.substring(firstNonSlash); } } - return new URL(protocol, username, password, host, port, path, parameters); + return new URL(protocol, username, password, host, port, path, parameters, methodParameters); } @@ -242,6 +261,16 @@ public final class URLBuilder { return this; } + public URLBuilder addMethodParameter(String method, String key, String value) { + if (StringUtils.isEmpty(method) || StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { + return this; + } + Map keyParameter = methodParameters.computeIfAbsent(method, m -> new HashMap<>()); + + keyParameter.put(key, value); + return this; + } + public URLBuilder addParameterIfAbsent(String key, String value) { if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { return this; @@ -276,6 +305,15 @@ public final class URLBuilder { return this; } + public URLBuilder addMethodParameters(Map> methodParameters) { + if (CollectionUtils.isEmptyMap(methodParameters)) { + return this; + } + + this.methodParameters.putAll(methodParameters); + return this; + } + public URLBuilder addParametersIfAbsent(Map parameters) { if (CollectionUtils.isEmptyMap(parameters)) { return this; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java index 90811e85d1..33634b981f 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java @@ -154,6 +154,12 @@ public interface CommonConstants { String REVISION_KEY = "revision"; + /** + * generic call + */ + String $INVOKE = "$invoke"; + String $INVOKE_ASYNC = "$invokeAsync"; + /** * package version in the manifest */ diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java b/dubbo-common/src/main/java/org/apache/dubbo/common/model/ApplicationModel.java similarity index 99% rename from dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java rename to dubbo-common/src/main/java/org/apache/dubbo/common/model/ApplicationModel.java index 22663513e1..0f5c688a94 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/model/ApplicationModel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.model; +package org.apache.dubbo.common.model; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; @@ -34,7 +34,6 @@ import java.util.concurrent.ConcurrentMap; * adjust project structure in order to fully utilize the methods introduced here. */ public class ApplicationModel { - protected static final Logger LOGGER = LoggerFactory.getLogger(ApplicationModel.class); /** diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java b/dubbo-common/src/main/java/org/apache/dubbo/common/model/ConsumerModel.java similarity index 99% rename from dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java rename to dubbo-common/src/main/java/org/apache/dubbo/common/model/ConsumerModel.java index 135cadbd7a..1641b8ba10 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/model/ConsumerModel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.model; +package org.apache.dubbo.common.model; import org.apache.dubbo.common.utils.Assert; import org.apache.dubbo.common.utils.CollectionUtils; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java b/dubbo-common/src/main/java/org/apache/dubbo/common/model/MethodModel.java similarity index 92% rename from dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java rename to dubbo-common/src/main/java/org/apache/dubbo/common/model/MethodModel.java index 58fd2a0939..adc89338fc 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/model/MethodModel.java @@ -14,15 +14,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.model; +package org.apache.dubbo.common.model; import org.apache.dubbo.common.utils.ReflectUtils; import java.lang.reflect.Method; import java.lang.reflect.Type; -import static org.apache.dubbo.rpc.Constants.$INVOKE; -import static org.apache.dubbo.rpc.Constants.$INVOKE_ASYNC; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE_ASYNC; /** * diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java b/dubbo-common/src/main/java/org/apache/dubbo/common/model/ProviderModel.java similarity index 97% rename from dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java rename to dubbo-common/src/main/java/org/apache/dubbo/common/model/ProviderModel.java index 41f6f7cc63..3cee816002 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/model/ProviderModel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.model; +package org.apache.dubbo.common.model; import java.util.Set; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java b/dubbo-common/src/main/java/org/apache/dubbo/common/model/ServiceModel.java similarity index 98% rename from dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java rename to dubbo-common/src/main/java/org/apache/dubbo/common/model/ServiceModel.java index 94e0ad223d..e28adee44f 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/model/ServiceModel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.rpc.model; +package org.apache.dubbo.common.model; import org.apache.dubbo.common.utils.CollectionUtils; diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java index fa97ff5cf2..bfc3afe1e7 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java @@ -17,7 +17,7 @@ package org.apache.dubbo.config; -import org.apache.dubbo.rpc.model.ConsumerModel; +import org.apache.dubbo.common.model.ConsumerModel; import org.apache.dubbo.service.Person; import com.alibaba.dubbo.config.ArgumentConfig; @@ -43,7 +43,6 @@ import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; public class MethodConfigTest { @Test diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java index 5786b5d923..3bf74bb6e9 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java @@ -24,13 +24,13 @@ import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.model.ConsumerModel; import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.MethodUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.support.Parameter; -import org.apache.dubbo.rpc.model.ConsumerModel; import java.io.Serializable; import java.lang.reflect.Method; diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java index b72560897c..630d0bba3c 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java @@ -21,6 +21,7 @@ import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.config.Environment; import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.Assert; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConfigUtils; @@ -40,7 +41,6 @@ import org.apache.dubbo.rpc.Filter; import org.apache.dubbo.rpc.InvokerListener; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.cluster.Cluster; -import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.support.MockInvoker; import java.io.IOException; @@ -53,31 +53,30 @@ import java.util.Map; import java.util.Set; import static org.apache.dubbo.common.config.ConfigurationUtils.parseProperties; -import static org.apache.dubbo.rpc.cluster.Constants.TAG_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE; +import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SEPARATOR; import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN; +import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL; import static org.apache.dubbo.common.constants.CommonConstants.FILE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PID_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; import static org.apache.dubbo.common.constants.CommonConstants.RELEASE_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; -import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY; -import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL; -import static org.apache.dubbo.config.Constants.LAYER_KEY; -import static org.apache.dubbo.config.Constants.LISTENER_KEY; -import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; -import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY; -import static org.apache.dubbo.config.Constants.REGISTRIES_SUFFIX; import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY; import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_SECONDS_KEY; -import static org.apache.dubbo.monitor.Constants.LOGSTAT_PROTOCOL; -import static org.apache.dubbo.registry.Constants.REGISTER_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL; +import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader; +import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY; +import static org.apache.dubbo.config.Constants.LAYER_KEY; +import static org.apache.dubbo.config.Constants.LISTENER_KEY; +import static org.apache.dubbo.config.Constants.REGISTRIES_SUFFIX; +import static org.apache.dubbo.monitor.Constants.LOGSTAT_PROTOCOL; +import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY; +import static org.apache.dubbo.registry.Constants.REGISTER_KEY; import static org.apache.dubbo.registry.Constants.SUBSCRIBE_KEY; import static org.apache.dubbo.remoting.Constants.DUBBO_VERSION_KEY; import static org.apache.dubbo.rpc.Constants.INVOKER_LISTENER_KEY; @@ -86,7 +85,8 @@ import static org.apache.dubbo.rpc.Constants.PROXY_KEY; import static org.apache.dubbo.rpc.Constants.REFERENCE_FILTER_KEY; import static org.apache.dubbo.rpc.Constants.RETURN_PREFIX; import static org.apache.dubbo.rpc.Constants.THROW_PREFIX; -import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader; +import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.TAG_KEY; /** * AbstractDefaultConfig diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java index c4a66a4b35..08a8c02334 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java @@ -20,6 +20,8 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ConsumerModel; import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConfigUtils; @@ -37,8 +39,6 @@ import org.apache.dubbo.rpc.cluster.Cluster; import org.apache.dubbo.rpc.cluster.directory.StaticDirectory; import org.apache.dubbo.rpc.cluster.support.ClusterUtils; import org.apache.dubbo.rpc.cluster.support.RegistryAwareCluster; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.rpc.protocol.injvm.InjvmProtocol; import org.apache.dubbo.rpc.service.GenericService; import org.apache.dubbo.rpc.support.ProtocolUtils; @@ -74,6 +74,8 @@ import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY; import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL; import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; +; + /** * ReferenceConfig * 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 2b16496d5a..cc1212bf60 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 @@ -22,6 +22,8 @@ import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.config.Environment; import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConfigUtils; @@ -38,8 +40,6 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.cluster.ConfiguratorFactory; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.service.GenericService; import org.apache.dubbo.rpc.support.ProtocolUtils; diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java index 20460d53b1..0d552c02cc 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java @@ -18,6 +18,7 @@ package org.apache.dubbo.monitor.dubbo; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.URLBuilder; +import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.monitor.Monitor; import org.apache.dubbo.monitor.MonitorService; @@ -25,7 +26,6 @@ import org.apache.dubbo.monitor.support.AbstractMonitorFactory; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; -import org.apache.dubbo.rpc.model.ApplicationModel; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL; import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java index 2f26743706..34c59bf806 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.store.DataStore; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.monitor.MetricsService; @@ -31,7 +32,6 @@ import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.support.RpcUtils; import com.alibaba.fastjson.JSON; diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java index ca23068abb..7ff5f50e11 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java @@ -16,13 +16,13 @@ */ package org.apache.dubbo.qos.command.impl; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ConsumerModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.BaseCommand; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.qos.command.annotation.Cmd; import org.apache.dubbo.qos.textui.TTable; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ConsumerModel; -import org.apache.dubbo.rpc.model.ProviderModel; import java.util.Collection; diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java index cf2862e36e..33bc702ce9 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java @@ -19,6 +19,8 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.BaseCommand; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.qos.command.annotation.Cmd; @@ -26,8 +28,6 @@ import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.RegistryFactory; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; import java.util.Collection; import java.util.Set; diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java index 6bcf089464..c91dbdadf2 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java @@ -19,6 +19,8 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.qos.command.BaseCommand; import org.apache.dubbo.qos.command.CommandContext; @@ -27,8 +29,6 @@ import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.RegistryFactory; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; import java.util.Collection; import java.util.Set; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java index 4b4fda2a71..be0855987a 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java @@ -17,14 +17,14 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ConsumerModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.registry.integration.RegistryDirectory; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ConsumerModel; -import org.apache.dubbo.rpc.model.ProviderModel; import org.junit.jupiter.api.Test; import org.mockito.Mockito; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java index 6f6be8e0bb..7d71b4ef23 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java @@ -17,13 +17,13 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; import org.junit.jupiter.api.Test; import org.mockito.Mockito; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java index b419f0ca9f..8c833151ef 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java @@ -17,13 +17,13 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; import org.junit.jupiter.api.Test; diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index f74931b329..250119f962 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -22,6 +22,7 @@ import org.apache.dubbo.common.Version; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.Assert; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; @@ -43,7 +44,6 @@ import org.apache.dubbo.rpc.cluster.RouterFactory; import org.apache.dubbo.rpc.cluster.directory.AbstractDirectory; import org.apache.dubbo.rpc.cluster.directory.StaticDirectory; import org.apache.dubbo.rpc.cluster.support.ClusterUtils; -import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.protocol.InvokerWrapper; import java.util.ArrayList; @@ -58,22 +58,19 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import static org.apache.dubbo.rpc.cluster.Constants.ROUTER_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY; import static org.apache.dubbo.common.constants.CommonConstants.DISABLED_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL; import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL; -import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.APP_DYNAMIC_CONFIGURATORS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.COMPATIBLE_CONFIG_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY; -import static org.apache.dubbo.registry.Constants.CONFIGURATORS_SUFFIX; import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_CONFIGURATORS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL; @@ -81,6 +78,9 @@ import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATE import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTE_PROTOCOL; +import static org.apache.dubbo.registry.Constants.CONFIGURATORS_SUFFIX; +import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.ROUTER_KEY; /** diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java index b9ad1b3ce5..320edfee8e 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java @@ -22,6 +22,7 @@ import org.apache.dubbo.common.config.ConfigurationUtils; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.common.utils.StringUtils; @@ -40,7 +41,6 @@ import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.cluster.Cluster; import org.apache.dubbo.rpc.cluster.Configurator; -import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.protocol.InvokerWrapper; import java.util.ArrayList; @@ -52,58 +52,58 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutorService; import static java.util.concurrent.Executors.newSingleThreadExecutor; -import static org.apache.dubbo.rpc.cluster.Constants.LOADBALANCE_KEY; -import static org.apache.dubbo.rpc.cluster.Constants.WARMUP_KEY; -import static org.apache.dubbo.rpc.cluster.Constants.WEIGHT_KEY; import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE; import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.HIDE_KEY_PREFIX; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; import static org.apache.dubbo.common.constants.CommonConstants.RELEASE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; +import static org.apache.dubbo.common.constants.FilterConstants.VALIDATION_KEY; import static org.apache.dubbo.common.constants.QosConstants.ACCEPT_FOREIGN_IP; -import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY; -import static org.apache.dubbo.rpc.cluster.Constants.EXPORT_KEY; import static org.apache.dubbo.common.constants.QosConstants.QOS_ENABLE; import static org.apache.dubbo.common.constants.QosConstants.QOS_PORT; -import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; -import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY; -import static org.apache.dubbo.common.constants.FilterConstants.VALIDATION_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY; -import static org.apache.dubbo.registry.Constants.CONFIGURATORS_SUFFIX; import static org.apache.dubbo.common.constants.RegistryConstants.CONSUMERS_CATEGORY; -import static org.apache.dubbo.registry.Constants.CONSUMER_PROTOCOL; -import static org.apache.dubbo.registry.Constants.DEFAULT_REGISTRY; -import static org.apache.dubbo.registry.Constants.EXTRA_KEYS_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.OVERRIDE_PROTOCOL; import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY; -import static org.apache.dubbo.registry.Constants.PROVIDER_PROTOCOL; -import static org.apache.dubbo.registry.Constants.REGISTER_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY; +import static org.apache.dubbo.common.utils.UrlUtils.classifyUrls; +import static org.apache.dubbo.registry.Constants.CONFIGURATORS_SUFFIX; +import static org.apache.dubbo.registry.Constants.CONSUMER_PROTOCOL; +import static org.apache.dubbo.registry.Constants.DEFAULT_REGISTRY; +import static org.apache.dubbo.registry.Constants.EXTRA_KEYS_KEY; +import static org.apache.dubbo.registry.Constants.PROVIDER_PROTOCOL; +import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY; +import static org.apache.dubbo.registry.Constants.REGISTER_KEY; import static org.apache.dubbo.registry.Constants.SIMPLIFIED_KEY; 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.remoting.Constants.CHECK_KEY; import static org.apache.dubbo.remoting.Constants.CODEC_KEY; -import static org.apache.dubbo.remoting.Constants.EXCHANGER_KEY; -import static org.apache.dubbo.remoting.Constants.SERIALIZATION_KEY; import static org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY; import static org.apache.dubbo.remoting.Constants.DUBBO_VERSION_KEY; +import static org.apache.dubbo.remoting.Constants.EXCHANGER_KEY; +import static org.apache.dubbo.remoting.Constants.SERIALIZATION_KEY; import static org.apache.dubbo.rpc.Constants.DEPRECATED_KEY; import static org.apache.dubbo.rpc.Constants.INTERFACES; import static org.apache.dubbo.rpc.Constants.MOCK_KEY; import static org.apache.dubbo.rpc.Constants.TOKEN_KEY; -import static org.apache.dubbo.common.utils.UrlUtils.classifyUrls; +import static org.apache.dubbo.rpc.cluster.Constants.EXPORT_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.LOADBALANCE_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.WARMUP_KEY; +import static org.apache.dubbo.rpc.cluster.Constants.WEIGHT_KEY; /** * RegistryProtocol diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java index e64a1bf8a4..4463c1eb76 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java @@ -50,6 +50,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE; import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE; import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE; @@ -62,7 +63,6 @@ import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTE_PROTOCOL; -import static org.apache.dubbo.rpc.Constants.$INVOKE; import static org.apache.dubbo.rpc.Constants.MOCK_KEY; import static org.apache.dubbo.rpc.cluster.Constants.INVOCATION_NEED_MOCK; import static org.apache.dubbo.rpc.cluster.Constants.LOADBALANCE_KEY; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Constants.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Constants.java index 5085887e87..5466909f0c 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Constants.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Constants.java @@ -107,8 +107,6 @@ public interface Constants { * To decide whether to make connection when the client is created */ String LAZY_CONNECT_KEY = "lazy"; - String $INVOKE = "$invoke"; - String $INVOKE_ASYNC = "$invokeAsync"; String INPUT_KEY = "input"; String OUTPUT_KEY = "output"; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java index d7eb9234d5..7299acb4dd 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java @@ -17,8 +17,8 @@ package org.apache.dubbo.rpc; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.rpc.model.ApplicationModel; import java.io.Serializable; import java.lang.reflect.Method; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java index 037734e6cf..7d78bac5f1 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java @@ -42,8 +42,8 @@ import org.apache.dubbo.rpc.support.ProtocolUtils; import java.io.IOException; import java.lang.reflect.Method; -import static org.apache.dubbo.rpc.Constants.$INVOKE; -import static org.apache.dubbo.rpc.Constants.$INVOKE_ASYNC; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE_ASYNC; import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; import static org.apache.dubbo.rpc.Constants.GENERIC_SERIALIZATION_BEAN; import static org.apache.dubbo.rpc.Constants.GENERIC_SERIALIZATION_NATIVE_JAVA; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java index 06710bc1d5..241eaf24e2 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java @@ -40,8 +40,8 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; -import static org.apache.dubbo.rpc.Constants.$INVOKE; -import static org.apache.dubbo.rpc.Constants.$INVOKE_ASYNC; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE_ASYNC; import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; /** diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java index 2d70a9ed8e..a75f7e1ffe 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java @@ -30,8 +30,8 @@ import java.lang.reflect.Type; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicLong; -import static org.apache.dubbo.rpc.Constants.$INVOKE; -import static org.apache.dubbo.rpc.Constants.$INVOKE_ASYNC; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE_ASYNC; import static org.apache.dubbo.rpc.Constants.ASYNC_KEY; import static org.apache.dubbo.rpc.Constants.AUTO_ATTACH_INVOCATIONID_KEY; import static org.apache.dubbo.rpc.Constants.ID_KEY; diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java index f3702673e2..138c967333 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericFilterTest.java @@ -36,7 +36,7 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -import static org.apache.dubbo.rpc.Constants.$INVOKE; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE; import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; import static org.apache.dubbo.rpc.Constants.GENERIC_SERIALIZATION_NATIVE_JAVA; import static org.mockito.ArgumentMatchers.any; diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java index 0d6e9fa406..c37fffc833 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java @@ -36,7 +36,7 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -import static org.apache.dubbo.rpc.Constants.$INVOKE; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE; import static org.apache.dubbo.rpc.Constants.GENERIC_KEY; import static org.mockito.Mockito.any; import static org.mockito.Mockito.when; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java index 6d0d995222..8f93f9e66c 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java @@ -19,6 +19,9 @@ package org.apache.dubbo.rpc.protocol.dubbo; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.MethodModel; +import org.apache.dubbo.common.model.ServiceModel; import org.apache.dubbo.common.serialize.Cleanable; import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.utils.Assert; @@ -29,9 +32,6 @@ import org.apache.dubbo.remoting.Decodeable; import org.apache.dubbo.remoting.exchange.Request; import org.apache.dubbo.remoting.transport.CodecSupport; import org.apache.dubbo.rpc.RpcInvocation; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.MethodModel; -import org.apache.dubbo.rpc.model.ServiceModel; import java.io.IOException; import java.io.InputStream; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java index 5d34270a86..9714b41db0 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java @@ -20,18 +20,18 @@ import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ConsumerModel; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.ListenableFilter; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ConsumerModel; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import static org.apache.dubbo.rpc.Constants.$INVOKE; +import static org.apache.dubbo.common.constants.CommonConstants.$INVOKE; /** * EventFilter diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java index 1534bb2ee5..cc23846d23 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java @@ -17,6 +17,9 @@ package org.apache.dubbo.rpc.protocol.dubbo.telnet; import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.MethodModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; @@ -24,9 +27,6 @@ import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.telnet.TelnetHandler; import org.apache.dubbo.remoting.telnet.support.Help; import org.apache.dubbo.rpc.AppResponse; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.MethodModel; -import org.apache.dubbo.rpc.model.ProviderModel; import com.alibaba.fastjson.JSON; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java index f0953ea627..10167a8b66 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java @@ -17,15 +17,15 @@ package org.apache.dubbo.rpc.protocol.dubbo.telnet; import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ConsumerModel; +import org.apache.dubbo.common.model.MethodModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.telnet.TelnetHandler; import org.apache.dubbo.remoting.telnet.support.Help; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ConsumerModel; -import org.apache.dubbo.rpc.model.MethodModel; -import org.apache.dubbo.rpc.model.ProviderModel; import java.lang.reflect.Method; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java index ffc175fd21..25869c2a7e 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java @@ -18,12 +18,12 @@ package org.apache.dubbo.rpc.protocol.dubbo; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ConsumerModel; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcContext; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; import org.junit.jupiter.api.AfterEach; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java index e14b5dc023..931029eaa6 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java @@ -17,13 +17,13 @@ package org.apache.dubbo.rpc.protocol.dubbo.telnet; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.telnet.TelnetHandler; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java index 420cbd4676..f8052ca236 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java @@ -16,12 +16,12 @@ */ package org.apache.dubbo.rpc.protocol.dubbo.telnet; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.telnet.TelnetHandler; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java index fbff17bc53..d8680e42a1 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java @@ -16,12 +16,12 @@ */ package org.apache.dubbo.rpc.protocol.dubbo.telnet; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.telnet.TelnetHandler; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java index c433b00ad8..86486e4a36 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java @@ -17,12 +17,12 @@ package org.apache.dubbo.rpc.protocol.rest; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.http.HttpBinder; import org.apache.dubbo.remoting.http.servlet.BootstrapListener; import org.apache.dubbo.remoting.http.servlet.ServletManager; import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.protocol.AbstractProxyProtocol; import org.apache.http.HeaderElement; diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index f3d77af865..881366fa07 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -18,6 +18,8 @@ package org.apache.dubbo.rpc.protocol.rest; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.model.ApplicationModel; +import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; @@ -27,8 +29,6 @@ import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; -import org.apache.dubbo.rpc.model.ApplicationModel; -import org.apache.dubbo.rpc.model.ProviderModel; import org.hamcrest.CoreMatchers; import org.junit.jupiter.api.AfterEach; From 086949c8fb2204bfa51e989b1fb7b8ccc6a3bc39 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Tue, 4 Jun 2019 17:29:03 +0800 Subject: [PATCH 07/28] performance tuning: getMethodparameter refactor (#4244) --- .../java/org/apache/dubbo/common/URL.java | 81 +++++++++++++++---- .../org/apache/dubbo/common/URLBuilder.java | 57 ++++++++++--- .../org/apache/dubbo/rpc/RpcInvocation.java | 16 ++-- 3 files changed, 121 insertions(+), 33 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java index 5dac550f9e..741b7cb841 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java @@ -19,8 +19,6 @@ package org.apache.dubbo.common; import org.apache.dubbo.common.config.Configuration; import org.apache.dubbo.common.config.InmemoryConfiguration; import org.apache.dubbo.common.constants.RemotingConstants; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.MethodModel; import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; @@ -39,7 +37,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; @@ -50,6 +47,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.HOST_KEY; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PASSWORD_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY; @@ -180,7 +178,7 @@ class URL implements Serializable { int port, String path, Map parameters) { - this (protocol, username, password, host, port, path, parameters, toMethodParameters(parameters, path)); + this (protocol, username, password, host, port, path, parameters, toMethodParameters(parameters)); } public URL(String protocol, @@ -302,22 +300,31 @@ class URL implements Serializable { return new URL(protocol, username, password, host, port, path, parameters); } - public static Map> toMethodParameters(Map parameters, String path) { + public static Map> toMethodParameters(Map parameters) { Map> methodParameters = new HashMap<>(); if (parameters != null) { - for (Map.Entry entry : parameters.entrySet()) { - String key = entry.getKey(); - int methodSeparator = key.indexOf("."); - if (methodSeparator > 0) { - String method = key.substring(0, methodSeparator); - String realKey = key.substring(methodSeparator + 1); - ApplicationModel.getServiceModel(path).ifPresent(serviceModel -> { - Set methodModels = serviceModel.getMethods(method); - if (CollectionUtils.isNotEmpty(methodModels)) { - Map subParameter = methodParameters.computeIfAbsent(method, k -> new HashMap<>()); - subParameter.put(realKey, entry.getValue()); + String methodsString = parameters.get(METHODS_KEY); + if (StringUtils.isNotEmpty(methodsString)) { + String[] methods = methodsString.split(","); + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + for (String method : methods) { + String methodPrefix = method + "."; + if (key.startsWith(methodPrefix)) { + String realKey = key.substring(methodPrefix.length()); + URL.putMethodParameter(method, realKey, entry.getValue(), methodParameters); } - }); + } + } + } else { + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + int methodSeparator = key.indexOf("."); + if (methodSeparator > 0) { + String method = key.substring(0, methodSeparator); + String realKey = key.substring(methodSeparator + 1); + URL.putMethodParameter(method, realKey, entry.getValue(), methodParameters); + } } } } @@ -1088,6 +1095,7 @@ class URL implements Serializable { Map map = new HashMap<>(getParameters()); map.put(key, value); + return new URL(protocol, username, password, host, port, path, map); } @@ -1101,9 +1109,43 @@ class URL implements Serializable { } Map map = new HashMap<>(getParameters()); map.put(key, value); + return new URL(protocol, username, password, host, port, path, map); } + public URL addMethodParameter(String method, String key, String value) { + if (StringUtils.isEmpty(method) + || StringUtils.isEmpty(key) + || StringUtils.isEmpty(value)) { + return this; + } + + Map map = new HashMap<>(getParameters()); + map.put(method + "." + key, value); + Map> methodMap = toMethodParameters(map); + URL.putMethodParameter(method, key, value, methodMap); + + return new URL(protocol, username, password, host, port, path, map, methodMap); + } + + public URL addMethodParameterIfAbsent(String method, String key, String value) { + if (StringUtils.isEmpty(method) + || StringUtils.isEmpty(key) + || StringUtils.isEmpty(value)) { + return this; + } + if (hasMethodParameter(method, key)) { + return this; + } + + Map map = new HashMap<>(getParameters()); + map.put(method + "." + key, value); + Map> methodMap = toMethodParameters(map); + URL.putMethodParameter(method, key, value, methodMap); + + return new URL(protocol, username, password, host, port, path, map, methodMap); + } + /** * Add parameters to a new url. * @@ -1616,4 +1658,9 @@ class URL implements Serializable { return true; } + public static void putMethodParameter(String method, String key, String value, Map> methodParameters) { + Map subParameter = methodParameters.computeIfAbsent(method, k -> new HashMap<>()); + subParameter.put(key, value); + } + } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java index b1c1a3d694..3097c28af8 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java @@ -84,7 +84,7 @@ public final class URLBuilder { String host, int port, String path, Map parameters) { - this(protocol, username, password, host, port, path, parameters, URL.toMethodParameters(parameters, path)); + this(protocol, username, password, host, port, path, parameters, URL.toMethodParameters(parameters)); } public URLBuilder(String protocol, @@ -112,6 +112,7 @@ public final class URLBuilder { int port = url.getPort(); String path = url.getPath(); Map parameters = new HashMap<>(url.getParameters()); + Map> methodParameters = new HashMap<>(url.getMethodParameters()); return new URLBuilder( protocol, username, @@ -119,7 +120,8 @@ public final class URLBuilder { host, port, path, - parameters); + parameters, + methodParameters); } public URL build() { @@ -252,10 +254,6 @@ public final class URLBuilder { if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { return this; } - // if value doesn't change, return immediately - if (value.equals(parameters.get(key))) { // value != null - return this; - } parameters.put(key, value); return this; @@ -265,9 +263,7 @@ public final class URLBuilder { if (StringUtils.isEmpty(method) || StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { return this; } - Map keyParameter = methodParameters.computeIfAbsent(method, m -> new HashMap<>()); - - keyParameter.put(key, value); + URL.putMethodParameter(method, key, value, methodParameters); return this; } @@ -282,6 +278,17 @@ public final class URLBuilder { return this; } + public URLBuilder addMethodParameterIfAbsent(String method, String key, String value) { + if (StringUtils.isEmpty(method) || StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { + return this; + } + if (hasMethodParameter(method, key)) { + return this; + } + URL.putMethodParameter(method, key, value, methodParameters); + return this; + } + public URLBuilder addParameters(Map parameters) { if (CollectionUtils.isEmptyMap(parameters)) { return this; @@ -378,7 +385,39 @@ public final class URLBuilder { return value != null && value.length() > 0; } + public boolean hasMethodParameter(String method, String key) { + if (method == null) { + String suffix = "." + key; + for (String fullKey : parameters.keySet()) { + if (fullKey.endsWith(suffix)) { + return true; + } + } + return false; + } + if (key == null) { + String prefix = method + "."; + for (String fullKey : parameters.keySet()) { + if (fullKey.startsWith(prefix)) { + return true; + } + } + return false; + } + String value = getMethodParameter(method, key); + return value != null && value.length() > 0; + } + public String getParameter(String key) { return parameters.get(key); } + + public String getMethodParameter(String method, String key) { + Map keyMap = methodParameters.get(method); + String value = null; + if (keyMap != null) { + value = keyMap.get(key); + } + return value; + } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java index 7299acb4dd..ad46cd75b7 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java @@ -121,13 +121,15 @@ public class RpcInvocation implements Invocation, Serializable { this.arguments = arguments == null ? new Object[0] : arguments; this.attachments = attachments == null ? new HashMap() : attachments; this.invoker = invoker; - ApplicationModel.getServiceModel(serviceName).ifPresent(serviceModel -> - serviceModel.getMethod(methodName, parameterTypes) - .ifPresent(methodModel -> { - this.parameterTypesDesc = methodModel.getParamDesc(); - this.returnTypes = methodModel.getReturnTypes(); - }) - ); + if (StringUtils.isNotEmpty(serviceName)) { + ApplicationModel.getServiceModel(serviceName).ifPresent(serviceModel -> + serviceModel.getMethod(methodName, parameterTypes) + .ifPresent(methodModel -> { + this.parameterTypesDesc = methodModel.getParamDesc(); + this.returnTypes = methodModel.getReturnTypes(); + }) + ); + } } @Override From faec551df75cb22c65970c0d83d446e306184926 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Tue, 4 Jun 2019 17:49:00 +0800 Subject: [PATCH 08/28] performance tuning: Applicationmodel revert (#4245) --- .../apache/dubbo/common/beanutil/Bean.java | 6 +-- .../beanutil/JavaBeanSerializeUtilTest.java | 10 ++--- .../dubbo/common/utils/PojoUtilsTest.java | 19 +++++---- .../dubbo/{common => rpc}/model/Person.java | 2 +- .../model/SerializablePerson.java | 2 +- .../dubbo/{common => rpc}/model/User.java | 2 +- .../{common => rpc}/model/media/Image.java | 2 +- .../{common => rpc}/model/media/Media.java | 2 +- .../model/person/BigPerson.java | 2 +- .../model/person/FullAddress.java | 2 +- .../model/person/PersonInfo.java | 2 +- .../model/person/PersonStatus.java | 42 +++++++++---------- .../{common => rpc}/model/person/Phone.java | 2 +- .../apache/dubbo/config/MethodConfigTest.java | 2 +- .../apache/dubbo/config/AbstractConfig.java | 2 +- .../dubbo/config/AbstractInterfaceConfig.java | 2 +- .../apache/dubbo/config/ReferenceConfig.java | 4 +- .../apache/dubbo/config/ServiceConfig.java | 4 +- .../monitor/dubbo/DubboMonitorFactory.java | 2 +- .../dubbo/monitor/dubbo/MetricsFilter.java | 2 +- .../org/apache/dubbo/qos/command/impl/Ls.java | 6 +-- .../dubbo/qos/command/impl/Offline.java | 4 +- .../apache/dubbo/qos/command/impl/Online.java | 4 +- .../apache/dubbo/qos/command/impl/LsTest.java | 6 +-- .../dubbo/qos/command/impl/OfflineTest.java | 4 +- .../dubbo/qos/command/impl/OnlineTest.java | 4 +- .../integration/RegistryDirectory.java | 2 +- .../integration/RegistryProtocol.java | 2 +- .../org/apache/dubbo/rpc/RpcInvocation.java | 2 +- .../dubbo/rpc}/model/ApplicationModel.java | 2 +- .../dubbo/rpc}/model/ConsumerModel.java | 2 +- .../apache/dubbo/rpc}/model/MethodModel.java | 2 +- .../dubbo/rpc}/model/ProviderModel.java | 2 +- .../apache/dubbo/rpc}/model/ServiceModel.java | 2 +- .../dubbo/DecodeableRpcInvocation.java | 6 +-- .../protocol/dubbo/filter/FutureFilter.java | 4 +- .../dubbo/telnet/InvokeTelnetHandler.java | 6 +-- .../dubbo/telnet/ListTelnetHandler.java | 8 ++-- .../protocol/dubbo/ImplicitCallBackTest.java | 4 +- .../telnet/InvokerTelnetHandlerTest.java | 4 +- .../dubbo/telnet/ListTelnetHandlerTest.java | 4 +- .../dubbo/telnet/SelectTelnetHandlerTest.java | 4 +- .../dubbo/rpc/protocol/rest/RestProtocol.java | 2 +- .../rpc/protocol/rest/RestProtocolTest.java | 4 +- 44 files changed, 102 insertions(+), 101 deletions(-) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/Person.java (98%) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/SerializablePerson.java (98%) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/User.java (98%) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/media/Image.java (98%) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/media/Media.java (99%) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/person/BigPerson.java (98%) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/person/FullAddress.java (99%) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/person/PersonInfo.java (99%) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/person/PersonStatus.java (92%) rename dubbo-common/src/test/java/org/apache/dubbo/{common => rpc}/model/person/Phone.java (98%) rename {dubbo-common/src/main/java/org/apache/dubbo/common => dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc}/model/ApplicationModel.java (99%) rename {dubbo-common/src/main/java/org/apache/dubbo/common => dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc}/model/ConsumerModel.java (99%) rename {dubbo-common/src/main/java/org/apache/dubbo/common => dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc}/model/MethodModel.java (98%) rename {dubbo-common/src/main/java/org/apache/dubbo/common => dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc}/model/ProviderModel.java (97%) rename {dubbo-common/src/main/java/org/apache/dubbo/common => dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc}/model/ServiceModel.java (98%) diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/Bean.java b/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/Bean.java index 2ca32b2303..e17a538ae6 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/Bean.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/Bean.java @@ -1,8 +1,8 @@ package org.apache.dubbo.common.beanutil; -import org.apache.dubbo.common.model.person.FullAddress; -import org.apache.dubbo.common.model.person.PersonStatus; -import org.apache.dubbo.common.model.person.Phone; +import org.apache.dubbo.rpc.model.person.FullAddress; +import org.apache.dubbo.rpc.model.person.PersonStatus; +import org.apache.dubbo.rpc.model.person.Phone; import java.util.Collection; import java.util.Date; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtilTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtilTest.java index 68c7100f32..c98ea15b85 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtilTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtilTest.java @@ -16,12 +16,12 @@ */ package org.apache.dubbo.common.beanutil; -import org.apache.dubbo.common.model.person.BigPerson; -import org.apache.dubbo.common.model.person.FullAddress; -import org.apache.dubbo.common.model.person.PersonInfo; -import org.apache.dubbo.common.model.person.PersonStatus; -import org.apache.dubbo.common.model.person.Phone; import org.apache.dubbo.common.utils.PojoUtilsTest; +import org.apache.dubbo.rpc.model.person.BigPerson; +import org.apache.dubbo.rpc.model.person.FullAddress; +import org.apache.dubbo.rpc.model.person.PersonInfo; +import org.apache.dubbo.rpc.model.person.PersonStatus; +import org.apache.dubbo.rpc.model.person.Phone; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java index 3595efb99f..f435e0685f 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java @@ -16,14 +16,15 @@ */ package org.apache.dubbo.common.utils; -import org.apache.dubbo.common.model.Person; -import org.apache.dubbo.common.model.SerializablePerson; -import org.apache.dubbo.common.model.User; -import org.apache.dubbo.common.model.person.BigPerson; -import org.apache.dubbo.common.model.person.FullAddress; -import org.apache.dubbo.common.model.person.PersonInfo; -import org.apache.dubbo.common.model.person.PersonStatus; -import org.apache.dubbo.common.model.person.Phone; +import org.apache.dubbo.rpc.model.Person; +import org.apache.dubbo.rpc.model.SerializablePerson; +import org.apache.dubbo.rpc.model.User; +import org.apache.dubbo.rpc.model.person.BigPerson; +import org.apache.dubbo.rpc.model.person.FullAddress; +import org.apache.dubbo.rpc.model.person.PersonInfo; +import org.apache.dubbo.rpc.model.person.PersonStatus; +import org.apache.dubbo.rpc.model.person.Phone; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -40,13 +41,13 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; -import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; public class PojoUtilsTest { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/Person.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/Person.java similarity index 98% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/Person.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/Person.java index 5a551a7f36..8cd13e46fe 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/Person.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/Person.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model; +package org.apache.dubbo.rpc.model; import java.util.Arrays; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/SerializablePerson.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/SerializablePerson.java similarity index 98% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/SerializablePerson.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/SerializablePerson.java index d78c5928ea..fa3015a4c6 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/SerializablePerson.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/SerializablePerson.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model; +package org.apache.dubbo.rpc.model; import java.io.Serializable; import java.util.Arrays; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/User.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/User.java similarity index 98% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/User.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/User.java index 33d20bbc6c..c49130cf5b 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/User.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/User.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.dubbo.common.model; +package org.apache.dubbo.rpc.model; import java.util.Objects; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/media/Image.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/media/Image.java similarity index 98% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/media/Image.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/media/Image.java index a59eff35be..423c6a4938 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/media/Image.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/media/Image.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model.media; +package org.apache.dubbo.rpc.model.media; public class Image implements java.io.Serializable { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/media/Media.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/media/Media.java similarity index 99% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/media/Media.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/media/Media.java index 042a3a5273..bb2be00b29 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/media/Media.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/media/Media.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model.media; +package org.apache.dubbo.rpc.model.media; import java.util.List; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/BigPerson.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/BigPerson.java similarity index 98% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/person/BigPerson.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/BigPerson.java index 4ce79008fb..9e0856c7f6 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/BigPerson.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/BigPerson.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model.person; +package org.apache.dubbo.rpc.model.person; import java.io.Serializable; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/FullAddress.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/FullAddress.java similarity index 99% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/person/FullAddress.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/FullAddress.java index 38921670f9..95363895c7 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/FullAddress.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/FullAddress.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model.person; +package org.apache.dubbo.rpc.model.person; import java.io.Serializable; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/PersonInfo.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/PersonInfo.java similarity index 99% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/person/PersonInfo.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/PersonInfo.java index 15f1238fe8..57f156b660 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/PersonInfo.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/PersonInfo.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model.person; +package org.apache.dubbo.rpc.model.person; import java.io.Serializable; import java.util.List; diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/PersonStatus.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/PersonStatus.java similarity index 92% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/person/PersonStatus.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/PersonStatus.java index 727a155c70..8f432f48d5 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/PersonStatus.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/PersonStatus.java @@ -1,22 +1,22 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.dubbo.common.model.person; - -public enum PersonStatus { - ENABLED, - DISABLED +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.rpc.model.person; + +public enum PersonStatus { + ENABLED, + DISABLED } \ No newline at end of file diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/Phone.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/Phone.java similarity index 98% rename from dubbo-common/src/test/java/org/apache/dubbo/common/model/person/Phone.java rename to dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/Phone.java index 41fd38a92d..ebc00ad495 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/model/person/Phone.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/person/Phone.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model.person; +package org.apache.dubbo.rpc.model.person; import java.io.Serializable; diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java index bfc3afe1e7..aaf680b97f 100644 --- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java @@ -17,7 +17,7 @@ package org.apache.dubbo.config; -import org.apache.dubbo.common.model.ConsumerModel; +import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.service.Person; import com.alibaba.dubbo.config.ArgumentConfig; diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java index 3bf74bb6e9..5786b5d923 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java @@ -24,13 +24,13 @@ import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.model.ConsumerModel; import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.MethodUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.config.support.Parameter; +import org.apache.dubbo.rpc.model.ConsumerModel; import java.io.Serializable; import java.lang.reflect.Method; diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java index 630d0bba3c..8130a5fc04 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java @@ -21,7 +21,6 @@ import org.apache.dubbo.common.URLBuilder; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.config.Environment; import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.Assert; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConfigUtils; @@ -41,6 +40,7 @@ import org.apache.dubbo.rpc.Filter; import org.apache.dubbo.rpc.InvokerListener; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.cluster.Cluster; +import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.support.MockInvoker; import java.io.IOException; diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java index 08a8c02334..ffef86dc81 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java @@ -20,8 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ConsumerModel; import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConfigUtils; @@ -39,6 +37,8 @@ import org.apache.dubbo.rpc.cluster.Cluster; import org.apache.dubbo.rpc.cluster.directory.StaticDirectory; import org.apache.dubbo.rpc.cluster.support.ClusterUtils; import org.apache.dubbo.rpc.cluster.support.RegistryAwareCluster; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.rpc.protocol.injvm.InjvmProtocol; import org.apache.dubbo.rpc.service.GenericService; import org.apache.dubbo.rpc.support.ProtocolUtils; 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 cc1212bf60..2b16496d5a 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 @@ -22,8 +22,6 @@ import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.config.Environment; import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ConfigUtils; @@ -40,6 +38,8 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.cluster.ConfiguratorFactory; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.service.GenericService; import org.apache.dubbo.rpc.support.ProtocolUtils; diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java index 0d552c02cc..20460d53b1 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitorFactory.java @@ -18,7 +18,6 @@ package org.apache.dubbo.monitor.dubbo; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.URLBuilder; -import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.monitor.Monitor; import org.apache.dubbo.monitor.MonitorService; @@ -26,6 +25,7 @@ import org.apache.dubbo.monitor.support.AbstractMonitorFactory; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.ProxyFactory; +import org.apache.dubbo.rpc.model.ApplicationModel; import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL; import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; diff --git a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java index 34c59bf806..2f26743706 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java +++ b/dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/MetricsFilter.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.store.DataStore; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.monitor.MetricsService; @@ -32,6 +31,7 @@ import org.apache.dubbo.rpc.Protocol; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.support.RpcUtils; import com.alibaba.fastjson.JSON; diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java index 7ff5f50e11..ca23068abb 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Ls.java @@ -16,13 +16,13 @@ */ package org.apache.dubbo.qos.command.impl; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ConsumerModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.BaseCommand; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.qos.command.annotation.Cmd; import org.apache.dubbo.qos.textui.TTable; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ConsumerModel; +import org.apache.dubbo.rpc.model.ProviderModel; import java.util.Collection; diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java index 33bc702ce9..cf2862e36e 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java @@ -19,8 +19,6 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.BaseCommand; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.qos.command.annotation.Cmd; @@ -28,6 +26,8 @@ import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.RegistryFactory; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; import java.util.Collection; import java.util.Set; diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java index c91dbdadf2..6bcf089464 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java @@ -19,8 +19,6 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.qos.command.BaseCommand; import org.apache.dubbo.qos.command.CommandContext; @@ -29,6 +27,8 @@ import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.RegistryFactory; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; import java.util.Collection; import java.util.Set; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java index be0855987a..4b4fda2a71 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java @@ -17,14 +17,14 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ConsumerModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.registry.integration.RegistryDirectory; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ConsumerModel; +import org.apache.dubbo.rpc.model.ProviderModel; import org.junit.jupiter.api.Test; import org.mockito.Mockito; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java index 7d71b4ef23..6f6be8e0bb 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java @@ -17,13 +17,13 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; import org.junit.jupiter.api.Test; import org.mockito.Mockito; diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java index 8c833151ef..b419f0ca9f 100644 --- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java @@ -17,13 +17,13 @@ package org.apache.dubbo.qos.command.impl; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.qos.command.CommandContext; import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; import org.junit.jupiter.api.Test; diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index 250119f962..2f3a83567a 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -22,7 +22,6 @@ import org.apache.dubbo.common.Version; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.Assert; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; @@ -44,6 +43,7 @@ import org.apache.dubbo.rpc.cluster.RouterFactory; import org.apache.dubbo.rpc.cluster.directory.AbstractDirectory; import org.apache.dubbo.rpc.cluster.directory.StaticDirectory; import org.apache.dubbo.rpc.cluster.support.ClusterUtils; +import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.protocol.InvokerWrapper; import java.util.ArrayList; diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java index 320edfee8e..9afa7ab9fd 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java @@ -22,7 +22,6 @@ import org.apache.dubbo.common.config.ConfigurationUtils; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.common.utils.StringUtils; @@ -41,6 +40,7 @@ import org.apache.dubbo.rpc.ProxyFactory; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.cluster.Cluster; import org.apache.dubbo.rpc.cluster.Configurator; +import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.protocol.InvokerWrapper; import java.util.ArrayList; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java index ad46cd75b7..de6f728fd9 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java @@ -17,8 +17,8 @@ package org.apache.dubbo.rpc; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.StringUtils; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.io.Serializable; import java.lang.reflect.Method; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/model/ApplicationModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java similarity index 99% rename from dubbo-common/src/main/java/org/apache/dubbo/common/model/ApplicationModel.java rename to dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java index 0f5c688a94..7a7a4bef41 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/model/ApplicationModel.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model; +package org.apache.dubbo.rpc.model; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/model/ConsumerModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java similarity index 99% rename from dubbo-common/src/main/java/org/apache/dubbo/common/model/ConsumerModel.java rename to dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java index 1641b8ba10..135cadbd7a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/model/ConsumerModel.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ConsumerModel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model; +package org.apache.dubbo.rpc.model; import org.apache.dubbo.common.utils.Assert; import org.apache.dubbo.common.utils.CollectionUtils; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/model/MethodModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java similarity index 98% rename from dubbo-common/src/main/java/org/apache/dubbo/common/model/MethodModel.java rename to dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java index adc89338fc..1ef9a33319 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/model/MethodModel.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/MethodModel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model; +package org.apache.dubbo.rpc.model; import org.apache.dubbo.common.utils.ReflectUtils; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/model/ProviderModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java similarity index 97% rename from dubbo-common/src/main/java/org/apache/dubbo/common/model/ProviderModel.java rename to dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java index 3cee816002..41f6f7cc63 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/model/ProviderModel.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model; +package org.apache.dubbo.rpc.model; import java.util.Set; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/model/ServiceModel.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java similarity index 98% rename from dubbo-common/src/main/java/org/apache/dubbo/common/model/ServiceModel.java rename to dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java index e28adee44f..94e0ad223d 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/model/ServiceModel.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/model/ServiceModel.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dubbo.common.model; +package org.apache.dubbo.rpc.model; import org.apache.dubbo.common.utils.CollectionUtils; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java index 8f93f9e66c..6d0d995222 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java @@ -19,9 +19,6 @@ package org.apache.dubbo.rpc.protocol.dubbo; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.MethodModel; -import org.apache.dubbo.common.model.ServiceModel; import org.apache.dubbo.common.serialize.Cleanable; import org.apache.dubbo.common.serialize.ObjectInput; import org.apache.dubbo.common.utils.Assert; @@ -32,6 +29,9 @@ import org.apache.dubbo.remoting.Decodeable; import org.apache.dubbo.remoting.exchange.Request; import org.apache.dubbo.remoting.transport.CodecSupport; import org.apache.dubbo.rpc.RpcInvocation; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.MethodModel; +import org.apache.dubbo.rpc.model.ServiceModel; import java.io.IOException; import java.io.InputStream; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java index 9714b41db0..45a26cde97 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java @@ -20,13 +20,13 @@ import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ConsumerModel; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.ListenableFilter; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ConsumerModel; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java index cc23846d23..1534bb2ee5 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java @@ -17,9 +17,6 @@ package org.apache.dubbo.rpc.protocol.dubbo.telnet; import org.apache.dubbo.common.extension.Activate; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.MethodModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; @@ -27,6 +24,9 @@ import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.telnet.TelnetHandler; import org.apache.dubbo.remoting.telnet.support.Help; import org.apache.dubbo.rpc.AppResponse; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.MethodModel; +import org.apache.dubbo.rpc.model.ProviderModel; import com.alibaba.fastjson.JSON; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java index 10167a8b66..f0953ea627 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandler.java @@ -17,15 +17,15 @@ package org.apache.dubbo.rpc.protocol.dubbo.telnet; import org.apache.dubbo.common.extension.Activate; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ConsumerModel; -import org.apache.dubbo.common.model.MethodModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.telnet.TelnetHandler; import org.apache.dubbo.remoting.telnet.support.Help; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ConsumerModel; +import org.apache.dubbo.rpc.model.MethodModel; +import org.apache.dubbo.rpc.model.ProviderModel; import java.lang.reflect.Method; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java index 25869c2a7e..ffc175fd21 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ImplicitCallBackTest.java @@ -18,12 +18,12 @@ package org.apache.dubbo.rpc.protocol.dubbo; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ConsumerModel; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ConsumerModel; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; import org.junit.jupiter.api.AfterEach; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java index 931029eaa6..e14b5dc023 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java @@ -17,13 +17,13 @@ package org.apache.dubbo.rpc.protocol.dubbo.telnet; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.telnet.TelnetHandler; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java index f8052ca236..420cbd4676 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/ListTelnetHandlerTest.java @@ -16,12 +16,12 @@ */ package org.apache.dubbo.rpc.protocol.dubbo.telnet; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.telnet.TelnetHandler; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java index d8680e42a1..fbff17bc53 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/SelectTelnetHandlerTest.java @@ -16,12 +16,12 @@ */ package org.apache.dubbo.rpc.protocol.dubbo.telnet; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.telnet.TelnetHandler; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java index 86486e4a36..c433b00ad8 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java @@ -17,12 +17,12 @@ package org.apache.dubbo.rpc.protocol.rest; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.model.ApplicationModel; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.http.HttpBinder; import org.apache.dubbo.remoting.http.servlet.BootstrapListener; import org.apache.dubbo.remoting.http.servlet.ServletManager; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.protocol.AbstractProxyProtocol; import org.apache.http.HeaderElement; diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index 881366fa07..f3d77af865 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -18,8 +18,6 @@ package org.apache.dubbo.rpc.protocol.rest; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.common.model.ApplicationModel; -import org.apache.dubbo.common.model.ProviderModel; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; @@ -29,6 +27,8 @@ import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ProviderModel; import org.hamcrest.CoreMatchers; import org.junit.jupiter.api.AfterEach; From 4c358907023a7f055cff769549b8030ed1a926fb Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Tue, 4 Jun 2019 17:49:51 +0800 Subject: [PATCH 09/28] Performance tuning: keep heartbeat updated in io thread to avoid race condition. (#4246) --- .../exchange/support/header/AbstractTimerTask.java | 4 ++-- .../exchange/support/header/HeaderExchangeHandler.java | 10 ---------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java index befa688780..3779d9917f 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java @@ -45,11 +45,11 @@ public abstract class AbstractTimerTask implements TimerTask { } static Long lastRead(Channel channel) { - return (Long) channel.getAttribute(HeaderExchangeHandler.KEY_READ_TIMESTAMP); + return (Long) channel.getAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP); } static Long lastWrite(Channel channel) { - return (Long) channel.getAttribute(HeaderExchangeHandler.KEY_WRITE_TIMESTAMP); + return (Long) channel.getAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP); } static Long now() { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java index 220d5d78c8..23fe27cde6 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java @@ -44,10 +44,6 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { protected static final Logger logger = LoggerFactory.getLogger(HeaderExchangeHandler.class); - public static final String KEY_READ_TIMESTAMP = HeartbeatHandler.KEY_READ_TIMESTAMP; - - public static final String KEY_WRITE_TIMESTAMP = HeartbeatHandler.KEY_WRITE_TIMESTAMP; - private final ExchangeHandler handler; public HeaderExchangeHandler(ExchangeHandler handler) { @@ -125,8 +121,6 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { @Override public void connected(Channel channel) throws RemotingException { - channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis()); - channel.setAttribute(KEY_WRITE_TIMESTAMP, System.currentTimeMillis()); ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel); try { handler.connected(exchangeChannel); @@ -137,8 +131,6 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { @Override public void disconnected(Channel channel) throws RemotingException { - channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis()); - channel.setAttribute(KEY_WRITE_TIMESTAMP, System.currentTimeMillis()); ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel); try { handler.disconnected(exchangeChannel); @@ -152,7 +144,6 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { public void sent(Channel channel, Object message) throws RemotingException { Throwable exception = null; try { - channel.setAttribute(KEY_WRITE_TIMESTAMP, System.currentTimeMillis()); ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel); try { handler.sent(exchangeChannel, message); @@ -180,7 +171,6 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { @Override public void received(Channel channel, Object message) throws RemotingException { - channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis()); final ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel); try { if (message instanceof Request) { From 34e787119303341505a008838ca0d14b14da3284 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Tue, 4 Jun 2019 18:07:16 +0800 Subject: [PATCH 10/28] check before calling getMethodParameter --- .../src/main/java/org/apache/dubbo/common/URL.java | 7 +++++++ .../org/apache/dubbo/rpc/support/MockInvoker.java | 11 +++++++---- .../rpc/protocol/dubbo/CallbackServiceCodec.java | 10 +++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java index 741b7cb841..38008b69ad 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java @@ -1015,6 +1015,13 @@ class URL implements Serializable { return value != null && value.length() > 0; } + public boolean hasMethodParameter(String method) { + if (method == null) { + return false; + } + return getMethodParameters().containsKey(method); + } + public boolean isLocalHost() { return NetUtils.isLocalHost(host) || getParameter(LOCALHOST_KEY, false); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/MockInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/MockInvoker.java index 90f4d69b97..2badc04ba1 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/MockInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/MockInvoker.java @@ -39,12 +39,12 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import static org.apache.dubbo.rpc.Constants.MOCK_KEY; -import static org.apache.dubbo.rpc.Constants.RETURN_PREFIX; -import static org.apache.dubbo.rpc.Constants.THROW_PREFIX; import static org.apache.dubbo.rpc.Constants.FAIL_PREFIX; import static org.apache.dubbo.rpc.Constants.FORCE_PREFIX; +import static org.apache.dubbo.rpc.Constants.MOCK_KEY; import static org.apache.dubbo.rpc.Constants.RETURN_KEY; +import static org.apache.dubbo.rpc.Constants.RETURN_PREFIX; +import static org.apache.dubbo.rpc.Constants.THROW_PREFIX; final public class MockInvoker implements Invoker { private final static ProxyFactory PROXY_FACTORY = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); @@ -95,10 +95,13 @@ final public class MockInvoker implements Invoker { @Override public Result invoke(Invocation invocation) throws RpcException { - String mock = getUrl().getParameter(invocation.getMethodName() + "." + MOCK_KEY); if (invocation instanceof RpcInvocation) { ((RpcInvocation) invocation).setInvoker(this); } + String mock = null; + if (getUrl().hasMethodParameter(invocation.getMethodName())) { + mock = getUrl().getParameter(invocation.getMethodName() + "." + MOCK_KEY); + } if (StringUtils.isBlank(mock)) { mock = getUrl().getParameter(MOCK_KEY); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java index c82599c3a1..e151cb600f 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java @@ -41,13 +41,13 @@ import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CALLBACK_SERVICE_KEY; import static org.apache.dubbo.rpc.Constants.CALLBACK_INSTANCES_LIMIT_KEY; import static org.apache.dubbo.rpc.Constants.DEFAULT_CALLBACK_INSTANCES; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CALLBACK_SERVICE_PROXY_KEY; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.IS_CALLBACK_SERVICE; -import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CHANNEL_CALLBACK_KEY; import static org.apache.dubbo.rpc.Constants.IS_SERVER_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CALLBACK_SERVICE_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CALLBACK_SERVICE_PROXY_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CHANNEL_CALLBACK_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.IS_CALLBACK_SERVICE; /** * callback service helper @@ -65,7 +65,7 @@ class CallbackServiceCodec { private static byte isCallBack(URL url, String methodName, int argIndex) { // parameter callback rule: method-name.parameter-index(starting from 0).callback byte isCallback = CALLBACK_NONE; - if (url != null) { + if (url != null && url.hasMethodParameter(methodName)) { String callback = url.getParameter(methodName + "." + argIndex + ".callback"); if (callback != null) { if (callback.equalsIgnoreCase("true")) { From 5cab5d36d4573bd47180e8ee27212031e1d8ec08 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Tue, 4 Jun 2019 18:09:48 +0800 Subject: [PATCH 11/28] unify heartbeat stamp key --- .../exchange/support/header/HeartBeatTaskTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartBeatTaskTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartBeatTaskTest.java index 3249d46f16..9e0ee0fd98 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartBeatTaskTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeartBeatTaskTest.java @@ -21,6 +21,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.timer.HashedWheelTimer; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.exchange.Request; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -63,10 +64,8 @@ public class HeartBeatTaskTest { long now = System.currentTimeMillis(); url = url.addParameter(DUBBO_VERSION_KEY, "2.1.1"); - channel.setAttribute( - HeaderExchangeHandler.KEY_READ_TIMESTAMP, now); - channel.setAttribute( - HeaderExchangeHandler.KEY_WRITE_TIMESTAMP, now); + channel.setAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP, now); + channel.setAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP, now); heartbeatTimer.newTimeout(heartbeatTimerTask, 250, TimeUnit.MILLISECONDS); From 276c0a3e57453d575688a496c0da0cc3d5705aa2 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 5 Jun 2019 15:43:47 +0800 Subject: [PATCH 12/28] performance tuning: avoid duplicate channel status check (#4254) --- .../support/header/HeaderExchangeChannel.java | 6 ++ .../support/header/HeaderExchangeHandler.java | 69 ++++++++----------- .../transport/netty4/NettyChannel.java | 30 +++++++- .../transport/netty4/NettyClient.java | 6 +- .../transport/netty4/NettyClientHandler.java | 38 ++++------ .../transport/netty4/NettyCodecAdapter.java | 42 +++++------ .../transport/netty4/NettyServerHandler.java | 24 ++----- .../protocol/dubbo/DecodeableRpcResult.java | 1 - .../rpc/protocol/rest/RestProtocolTest.java | 20 +++--- 9 files changed, 111 insertions(+), 125 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java index f96207b604..b95461294f 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java @@ -76,6 +76,12 @@ final class HeaderExchangeChannel implements ExchangeChannel { } } + static void removeChannel(Channel ch) { + if (ch != null) { + ch.removeAttribute(CHANNEL_KEY); + } + } + @Override public void send(Object message) throws RemotingException { send(message, false); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java index 23fe27cde6..d60c9ca27a 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java @@ -108,8 +108,6 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { channel.send(res); } catch (RemotingException e) { logger.warn("Send result to consumer failed, channel is " + channel + ", msg is " + e); - } finally { - // HeaderExchangeChannel.removeChannelIfDisconnected(channel); } }); } catch (Throwable e) { @@ -122,11 +120,7 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { @Override public void connected(Channel channel) throws RemotingException { ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel); - try { - handler.connected(exchangeChannel); - } finally { - HeaderExchangeChannel.removeChannelIfDisconnected(channel); - } + handler.connected(exchangeChannel); } @Override @@ -136,7 +130,7 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { handler.disconnected(exchangeChannel); } finally { DefaultFuture.closeChannel(channel); - HeaderExchangeChannel.removeChannelIfDisconnected(channel); + HeaderExchangeChannel.removeChannel(channel); } } @@ -145,13 +139,10 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { Throwable exception = null; try { ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel); - try { - handler.sent(exchangeChannel, message); - } finally { - HeaderExchangeChannel.removeChannelIfDisconnected(channel); - } + handler.sent(exchangeChannel, message); } catch (Throwable t) { exception = t; + HeaderExchangeChannel.removeChannelIfDisconnected(channel); } if (message instanceof Request) { Request request = (Request) message; @@ -172,36 +163,32 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate { @Override public void received(Channel channel, Object message) throws RemotingException { final ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel); - try { - if (message instanceof Request) { - // handle request. - Request request = (Request) message; - if (request.isEvent()) { - handlerEvent(channel, request); - } else { - if (request.isTwoWay()) { - handleRequest(exchangeChannel, request); - } else { - handler.received(exchangeChannel, request.getData()); - } - } - } else if (message instanceof Response) { - handleResponse(channel, (Response) message); - } else if (message instanceof String) { - if (isClientSide(channel)) { - Exception e = new Exception("Dubbo client can not supported string message: " + message + " in channel: " + channel + ", url: " + channel.getUrl()); - logger.error(e.getMessage(), e); - } else { - String echo = handler.telnet(channel, (String) message); - if (echo != null && echo.length() > 0) { - channel.send(echo); - } - } + if (message instanceof Request) { + // handle request. + Request request = (Request) message; + if (request.isEvent()) { + handlerEvent(channel, request); } else { - handler.received(exchangeChannel, message); + if (request.isTwoWay()) { + handleRequest(exchangeChannel, request); + } else { + handler.received(exchangeChannel, request.getData()); + } } - } finally { - HeaderExchangeChannel.removeChannelIfDisconnected(channel); + } else if (message instanceof Response) { + handleResponse(channel, (Response) message); + } else if (message instanceof String) { + if (isClientSide(channel)) { + Exception e = new Exception("Dubbo client can not supported string message: " + message + " in channel: " + channel + ", url: " + channel.getUrl()); + logger.error(e.getMessage(), e); + } else { + String echo = handler.telnet(channel, (String) message); + if (echo != null && echo.length() > 0) { + channel.send(echo); + } + } + } else { + handler.received(exchangeChannel, message); } } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java index f8bbcf682e..d0dfa7c85e 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java @@ -30,6 +30,7 @@ import java.net.InetSocketAddress; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicBoolean; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; @@ -50,6 +51,9 @@ final class NettyChannel extends AbstractChannel { private final Channel channel; private final Map attributes = new ConcurrentHashMap(); + + private final AtomicBoolean active = new AtomicBoolean(false); + /** * The constructor of NettyChannel. * It is private so NettyChannel usually create by {@link NettyChannel#getOrAddChannel(Channel, URL, ChannelHandler)} @@ -82,6 +86,7 @@ final class NettyChannel extends AbstractChannel { if (ret == null) { NettyChannel nettyChannel = new NettyChannel(ch, url, handler); if (ch.isActive()) { + nettyChannel.markActive(true); ret = CHANNEL_MAP.putIfAbsent(ch, nettyChannel); } if (ret == null) { @@ -97,7 +102,19 @@ final class NettyChannel extends AbstractChannel { */ static void removeChannelIfDisconnected(Channel ch) { if (ch != null && !ch.isActive()) { - CHANNEL_MAP.remove(ch); + NettyChannel nettyChannel = CHANNEL_MAP.remove(ch); + if (nettyChannel != null) { + nettyChannel.markActive(false); + } + } + } + + static void removeChannel(Channel ch) { + if (ch != null) { + NettyChannel nettyChannel = CHANNEL_MAP.remove(ch); + if (nettyChannel != null) { + nettyChannel.markActive(false); + } } } @@ -113,7 +130,15 @@ final class NettyChannel extends AbstractChannel { @Override public boolean isConnected() { - return !isClosed() && channel.isActive(); + return !isClosed() && active.get(); + } + + public boolean isActive() { + return active.get(); + } + + public void markActive(boolean isActive) { + active.set(isActive); } /** @@ -142,6 +167,7 @@ final class NettyChannel extends AbstractChannel { throw cause; } } catch (Throwable e) { + removeChannelIfDisconnected(channel); throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e); } if (!success) { diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java index 3c32a9cb45..f5416002d9 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java @@ -40,10 +40,10 @@ import io.netty.handler.proxy.Socks5ProxyHandler; import io.netty.handler.timeout.IdleStateHandler; import io.netty.util.concurrent.DefaultThreadFactory; -import static java.util.concurrent.TimeUnit.MILLISECONDS; - import java.net.InetSocketAddress; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + /** * NettyClient. */ @@ -197,7 +197,7 @@ public class NettyClient extends AbstractClient { @Override protected org.apache.dubbo.remoting.Channel getChannel() { Channel c = channel; - if (c == null || !c.isActive()) { + if (c == null) { return null; } return NettyChannel.getOrAddChannel(c, getUrl(), this); diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java index 8ff2d98413..59db445d0c 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java @@ -55,11 +55,7 @@ public class NettyClientHandler extends ChannelDuplexHandler { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - try { - handler.connected(channel); - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); - } + handler.connected(channel); } @Override @@ -68,18 +64,14 @@ public class NettyClientHandler extends ChannelDuplexHandler { try { handler.disconnected(channel); } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); + NettyChannel.removeChannel(ctx.channel()); } } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - try { - handler.received(channel, msg); - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); - } + handler.received(channel, msg); } @Override @@ -92,21 +84,17 @@ public class NettyClientHandler extends ChannelDuplexHandler { // If our out bound event has an error (in most cases the encoder fails), // we need to have the request return directly instead of blocking the invoke process. promise.addListener(future -> { - try { - if (future.isSuccess()) { - // if our future is success, mark the future to sent. - handler.sent(channel, msg); - return; - } + if (future.isSuccess()) { + // if our future is success, mark the future to sent. + handler.sent(channel, msg); + return; + } - Throwable t = future.cause(); - if (t != null && isRequest) { - Request request = (Request) msg; - Response response = buildErrorResponse(request, t); - handler.received(channel, response); - } - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); + Throwable t = future.cause(); + if (t != null && isRequest) { + Request request = (Request) msg; + Response response = buildErrorResponse(request, t); + handler.received(channel, response); } }); } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyCodecAdapter.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyCodecAdapter.java index e0e4513c5c..d1cb65b880 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyCodecAdapter.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyCodecAdapter.java @@ -66,11 +66,7 @@ final public class NettyCodecAdapter { org.apache.dubbo.remoting.buffer.ChannelBuffer buffer = new NettyBackedChannelBuffer(out); Channel ch = ctx.channel(); NettyChannel channel = NettyChannel.getOrAddChannel(ch, url, handler); - try { - codec.encode(channel, buffer, msg); - } finally { - NettyChannel.removeChannelIfDisconnected(ch); - } + codec.encode(channel, buffer, msg); } } @@ -83,27 +79,23 @@ final public class NettyCodecAdapter { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - try { - // decode object. - do { - int saveReaderIndex = message.readerIndex(); - Object msg = codec.decode(channel, message); - if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { - message.readerIndex(saveReaderIndex); - break; - } else { - //is it possible to go here ? - if (saveReaderIndex == message.readerIndex()) { - throw new IOException("Decode without read data."); - } - if (msg != null) { - out.add(msg); - } + // decode object. + do { + int saveReaderIndex = message.readerIndex(); + Object msg = codec.decode(channel, message); + if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { + message.readerIndex(saveReaderIndex); + break; + } else { + //is it possible to go here ? + if (saveReaderIndex == message.readerIndex()) { + throw new IOException("Decode without read data."); } - } while (message.readable()); - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); - } + if (msg != null) { + out.add(msg); + } + } + } while (message.readable()); } } } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java index e62ab720f1..33ad9c9363 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServerHandler.java @@ -66,14 +66,10 @@ public class NettyServerHandler extends ChannelDuplexHandler { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - try { - if (channel != null) { - channels.put(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress()), channel); - } - handler.connected(channel); - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); + if (channel != null) { + channels.put(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress()), channel); } + handler.connected(channel); } @Override @@ -83,18 +79,14 @@ public class NettyServerHandler extends ChannelDuplexHandler { channels.remove(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress())); handler.disconnected(channel); } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); + NettyChannel.removeChannel(ctx.channel()); } } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - try { - handler.received(channel, msg); - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); - } + handler.received(channel, msg); } @@ -102,11 +94,7 @@ public class NettyServerHandler extends ChannelDuplexHandler { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { super.write(ctx, msg, promise); NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - try { - handler.sent(channel, msg); - } finally { - NettyChannel.removeChannelIfDisconnected(ctx.channel()); - } + handler.sent(channel, msg); } @Override diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java index 2a190ad896..1a2347c07a 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java @@ -74,7 +74,6 @@ public class DecodeableRpcResult extends AppResponse implements Codec, Decodeabl @Override public Object decode(Channel channel, InputStream input) throws IOException { - log.debug("Decoding in thread -- " + Thread.currentThread().getName()); ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType) .deserialize(channel.getUrl(), input); diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index f3d77af865..415e684408 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -55,7 +55,7 @@ public class RestProtocolTest { public void testRestProtocol() { URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.DemoService"); DemoServiceImpl server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(url.getPathKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(url.getPathKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(url.getPathKey(), providerModel); Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url)); @@ -75,7 +75,7 @@ public class RestProtocolTest { DemoServiceImpl server = new DemoServiceImpl(); Assertions.assertFalse(server.isCalled()); URL url = URL.valueOf("rest://127.0.0.1:5341/a/b/c?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.DemoService"); - ProviderModel providerModel = new ProviderModel(url.getPathKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(url.getPathKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(url.getPathKey(), providerModel); Exporter exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url)); @@ -93,7 +93,7 @@ public class RestProtocolTest { @Test public void testExport() { DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); RpcContext.getContext().setAttachment("timeout", "200"); @@ -110,7 +110,7 @@ public class RestProtocolTest { @Test public void testNettyServer() { DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); URL nettyUrl = exportUrl.addParameter(SERVER_KEY, "netty"); @@ -128,7 +128,7 @@ public class RestProtocolTest { public void testServletWithoutWebConfig() { Assertions.assertThrows(RpcException.class, () -> { DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(exportUrl.getPathKey(), providerModel); URL servletUrl = exportUrl.addParameter(SERVER_KEY, "servlet"); @@ -141,7 +141,7 @@ public class RestProtocolTest { public void testErrorHandler() { Assertions.assertThrows(RpcException.class, () -> { DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); URL nettyUrl = exportUrl.addParameter(SERVER_KEY, "netty"); @@ -156,7 +156,7 @@ public class RestProtocolTest { @Test public void testInvoke() { DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); @@ -171,7 +171,7 @@ public class RestProtocolTest { @Test public void testFilter() { DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); URL nettyUrl = exportUrl.addParameter(SERVER_KEY, "netty") @@ -190,7 +190,7 @@ public class RestProtocolTest { @Test public void testRpcContextFilter() { DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); // use RpcContextFilter @@ -214,7 +214,7 @@ public class RestProtocolTest { public void testRegFail() { Assertions.assertThrows(RuntimeException.class, () -> { DemoService server = new DemoServiceImpl(); - ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class); + ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, ApplicationModel.registerServiceModel(DemoService.class)); ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel); URL nettyUrl = exportUrl.addParameter(EXTENSION_KEY, "com.not.existing.Filter"); From 356a2f5df25c9ec647447b9e6bdcb9a3395688c3 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 5 Jun 2019 17:57:50 +0800 Subject: [PATCH 13/28] cache service key (#4255) --- .../java/org/apache/dubbo/common/URL.java | 19 ++++++++++++++----- .../rpc/protocol/thrift/ThriftCodecTest.java | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java index 38008b69ad..4062641ac6 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java @@ -128,6 +128,10 @@ class URL implements Serializable { private volatile transient String string; + private final transient String serviceKey; + + private final transient String serviceInterface; + protected URL() { this.protocol = null; this.username = null; @@ -137,6 +141,8 @@ class URL implements Serializable { this.path = null; this.parameters = null; this.methodParameters = null; + this.serviceKey = null; + this.serviceInterface = null; } public URL(String protocol, String host, int port) { @@ -210,6 +216,13 @@ class URL implements Serializable { } this.parameters = Collections.unmodifiableMap(parameters); this.methodParameters = Collections.unmodifiableMap(methodParameters); + + this.serviceInterface = getParameter(INTERFACE_KEY, path); + if (this.serviceInterface == null) { + this.serviceKey = null; + } else { + this.serviceKey = buildKey(serviceInterface, getParameter(GROUP_KEY), getParameter(VERSION_KEY)); + } } /** @@ -1443,11 +1456,7 @@ class URL implements Serializable { * @return */ public String getServiceKey() { - String inf = getServiceInterface(); - if (inf == null) { - return null; - } - return buildKey(inf, getParameter(GROUP_KEY), getParameter(VERSION_KEY)); + return serviceKey; } /** diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java index a8c66a71f8..36fbe6c7f7 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java @@ -133,7 +133,7 @@ public class ThriftCodecTest { Request request = createRequest(); - DefaultFuture future = DefaultFuture.newFuture(channel, request, 10); + DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null); TMessage message = new TMessage("echoString", TMessageType.REPLY, ThriftCodec.getSeqId()); @@ -210,7 +210,7 @@ public class ThriftCodecTest { Request request = createRequest(); - DefaultFuture future = DefaultFuture.newFuture(channel, request, 10); + DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null); TMessage message = new TMessage("echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId()); From b2dbc5ef5da92e3ee5eba06d64a8e00446dd65c7 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 5 Jun 2019 18:41:40 +0800 Subject: [PATCH 14/28] DefaultFuture (#4257) --- .../dubbo/demo/provider/DemoServiceImpl.java | 10 ------ .../dubbo/registry/dubbo/MockChannel.java | 8 ++--- .../dubbo/registry/dubbo/MockedClient.java | 14 ++++---- .../remoting/exchange/ExchangeChannel.java | 8 ++--- .../exchange/support/DefaultFuture.java | 36 +++++++++++-------- .../support/header/HeaderExchangeChannel.java | 18 +++++----- .../support/header/HeaderExchangeClient.java | 20 +++++------ .../java/org/apache/dubbo/remoting/Main.java | 10 +++--- .../remoting/PerformanceClientFixedTest.java | 5 ++- .../dubbo/remoting/PerformanceClientTest.java | 9 +++-- .../exchange/support/DefaultFutureTest.java | 9 +++-- .../header/HeaderExchangeChannelTest.java | 9 ++--- .../transport/mina/ClientToServerTest.java | 2 +- .../transport/netty/ClientToServerTest.java | 2 +- .../transport/netty4/ClientToServerTest.java | 2 +- .../protocol/dubbo/ChannelWrappedInvoker.java | 11 +----- .../rpc/protocol/dubbo/DubboInvoker.java | 4 +-- .../dubbo/LazyConnectExchangeClient.java | 18 +++++----- .../dubbo/ReferenceCountExchangeClient.java | 16 ++++----- .../rpc/protocol/thrift/ThriftInvoker.java | 4 +-- .../rpc/protocol/thrift/ThriftCodecTest.java | 5 +-- 21 files changed, 108 insertions(+), 112 deletions(-) diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java index 5e57f77e51..bceeae202d 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java @@ -30,22 +30,12 @@ public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); - } return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); } @Override public CompletableFuture sayHelloAsync(String name) { CompletableFuture cf = CompletableFuture.supplyAsync(() -> { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } return "async result"; }); return cf; diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java index e563da616c..9c43359172 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java @@ -86,21 +86,21 @@ public class MockChannel implements ExchangeChannel { return null; } - public CompletableFuture request(Object request) throws RemotingException { + public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { return null; } - public CompletableFuture request(Object request, int timeout) throws RemotingException { + public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { return null; } @Override - public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { return null; } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { + public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { return null; } diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java index ad06cd2096..bd62d3a62f 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java @@ -81,21 +81,21 @@ public class MockedClient implements ExchangeClient { this.sent = msg; } - public CompletableFuture request(Object msg) throws RemotingException { - return request(msg, null); + public CompletableFuture request(Object msg, CompletableFuture completableFuture) throws RemotingException { + return request(msg, null, completableFuture); } - public CompletableFuture request(Object msg, int timeout) throws RemotingException { - return this.request(msg, timeout, null); + public CompletableFuture request(Object msg, int timeout, CompletableFuture completableFuture) throws RemotingException { + return this.request(msg, timeout, null, completableFuture); } @Override - public CompletableFuture request(Object msg, ExecutorService executor) throws RemotingException { - return this.request(msg, 0, executor); + public CompletableFuture request(Object msg, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + return this.request(msg, 0, executor, completableFuture); } @Override - public CompletableFuture request(Object msg, int timeout, ExecutorService executor) throws RemotingException { + public CompletableFuture request(Object msg, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { this.invoked = msg; return new CompletableFuture() { public Object get() throws InterruptedException, ExecutionException { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java index c0cf131d8e..48a9f6ff2f 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java @@ -35,7 +35,7 @@ public interface ExchangeChannel extends Channel { * @throws RemotingException */ @Deprecated - CompletableFuture request(Object request) throws RemotingException; + CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException; /** * send request. @@ -46,7 +46,7 @@ public interface ExchangeChannel extends Channel { * @throws RemotingException */ @Deprecated - CompletableFuture request(Object request, int timeout) throws RemotingException; + CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException; /** * send request. @@ -55,7 +55,7 @@ public interface ExchangeChannel extends Channel { * @return response future * @throws RemotingException */ - CompletableFuture request(Object request, ExecutorService executor) throws RemotingException; + CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException; /** * send request. @@ -65,7 +65,7 @@ public interface ExchangeChannel extends Channel { * @return response future * @throws RemotingException */ - CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException; + CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException; /** * get message handler. diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index 0abecf2030..a23e8cd919 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -35,6 +35,7 @@ import java.util.Date; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; @@ -44,7 +45,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; /** * DefaultFuture. */ -public class DefaultFuture extends CompletableFuture { +public class DefaultFuture { private static final Logger logger = LoggerFactory.getLogger(DefaultFuture.class); @@ -52,6 +53,9 @@ public class DefaultFuture extends CompletableFuture { private static final Map FUTURES = new ConcurrentHashMap<>(); + // private static final Map PENDING_TASKS = new ConcurrentHashMap<>(); + private final CompletableFuture completableFuture; + public static final Timer TIME_OUT_TIMER = new HashedWheelTimer( new NamedThreadFactory("dubbo-future-timeout", true), 30, @@ -66,20 +70,18 @@ public class DefaultFuture extends CompletableFuture { private volatile long sent; private Timeout timeoutCheckTask; - private ExecutorService executor; + private final ExecutorService executor; public ExecutorService getExecutor() { return executor; } - public void setExecutor(ExecutorService executor) { - this.executor = executor; - } - - private DefaultFuture(Channel channel, Request request, int timeout) { + private DefaultFuture(Channel channel, Request request, int timeout, ExecutorService executor, CompletableFuture completableFuture) { this.channel = channel; this.request = request; this.id = request.getId(); + this.executor = executor; + this.completableFuture = completableFuture; this.timeout = timeout > 0 ? timeout : channel.getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT); // put into waiting map. FUTURES.put(id, this); @@ -104,9 +106,8 @@ public class DefaultFuture extends CompletableFuture { * @param timeout timeout * @return a new DefaultFuture */ - public static DefaultFuture newFuture(Channel channel, Request request, int timeout, ExecutorService executor) { - final DefaultFuture future = new DefaultFuture(channel, request, timeout); - future.setExecutor(executor); + public static DefaultFuture newFuture(Channel channel, Request request, int timeout, ExecutorService executor, CompletableFuture completableFuture) { + final DefaultFuture future = new DefaultFuture(channel, request, timeout, executor, completableFuture); // timeout check timeoutCheck(future); return future; @@ -176,7 +177,6 @@ public class DefaultFuture extends CompletableFuture { } } - @Override public boolean cancel(boolean mayInterruptIfRunning) { Response errorResult = new Response(id); errorResult.setStatus(Response.CLIENT_ERROR); @@ -187,6 +187,14 @@ public class DefaultFuture extends CompletableFuture { return true; } + public boolean isDone() { + return this.completableFuture.isDone(); + } + + public Object get() throws ExecutionException, InterruptedException { + return this.completableFuture.get(); + } + public void cancel() { this.cancel(true); } @@ -196,11 +204,11 @@ public class DefaultFuture extends CompletableFuture { throw new IllegalStateException("response cannot be null"); } if (res.getStatus() == Response.OK) { - this.complete(res.getResult()); + this.completableFuture.complete(res.getResult()); } else if (res.getStatus() == Response.CLIENT_TIMEOUT || res.getStatus() == Response.SERVER_TIMEOUT) { - this.completeExceptionally(new TimeoutException(res.getStatus() == Response.SERVER_TIMEOUT, channel, res.getErrorMessage())); + this.completableFuture.completeExceptionally(new TimeoutException(res.getStatus() == Response.SERVER_TIMEOUT, channel, res.getErrorMessage())); } else { - this.completeExceptionally(new RemotingException(channel, res.getErrorMessage())); + this.completableFuture.completeExceptionally(new RemotingException(channel, res.getErrorMessage())); } // the result is returning, but the caller thread may still waiting diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java index b95461294f..a82bf53171 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java @@ -106,22 +106,22 @@ final class HeaderExchangeChannel implements ExchangeChannel { } @Override - public CompletableFuture request(Object request) throws RemotingException { - return request(request, null); + public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { + return request(request, null, completableFuture); } @Override - public CompletableFuture request(Object request, int timeout) throws RemotingException { - return request(request, timeout, null); + public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { + return request(request, timeout, null, completableFuture); } @Override - public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { - return request(request, channel.getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT), executor); + public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + return request(request, channel.getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT), executor, completableFuture); } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { + public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { if (closed) { throw new RemotingException(this.getLocalAddress(), null, "Failed to send request " + request + ", cause: The channel " + this + " is closed!"); } @@ -130,14 +130,14 @@ final class HeaderExchangeChannel implements ExchangeChannel { req.setVersion(Version.getProtocolVersion()); req.setTwoWay(true); req.setData(request); - DefaultFuture future = DefaultFuture.newFuture(channel, req, timeout, executor); + DefaultFuture future = DefaultFuture.newFuture(channel, req, timeout, executor, completableFuture); try { channel.send(req); } catch (RemotingException e) { future.cancel(); throw e; } - return future; + return completableFuture; } @Override diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java index 6671d1a30b..8e04b99bbb 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java @@ -34,11 +34,11 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; -import static org.apache.dubbo.remoting.utils.UrlUtils.getHeartbeat; -import static org.apache.dubbo.remoting.utils.UrlUtils.getIdleTimeout; import static org.apache.dubbo.remoting.Constants.HEARTBEAT_CHECK_TICK; import static org.apache.dubbo.remoting.Constants.LEAST_HEARTBEAT_DURATION; import static org.apache.dubbo.remoting.Constants.TICKS_PER_WHEEL; +import static org.apache.dubbo.remoting.utils.UrlUtils.getHeartbeat; +import static org.apache.dubbo.remoting.utils.UrlUtils.getIdleTimeout; /** * DefaultMessageClient @@ -66,8 +66,8 @@ public class HeaderExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request) throws RemotingException { - return channel.request(request); + public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { + return channel.request(request, completableFuture); } @Override @@ -81,18 +81,18 @@ public class HeaderExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request, int timeout) throws RemotingException { - return channel.request(request, timeout); + public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { + return channel.request(request, timeout, completableFuture); } @Override - public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { - return channel.request(request, executor); + public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + return channel.request(request, executor, completableFuture); } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { - return channel.request(request, timeout, executor); + public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + return channel.request(request, timeout, executor, completableFuture); } @Override diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java index 4478f86ecf..9869bcc91f 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java @@ -62,7 +62,7 @@ public class Main { sb.append("(" + random.nextLong() + ")"); Main.Data d = new Main.Data(); d.setData(sb.toString()); - client.request(d).get(); + client.request(d, new CompletableFuture()).get(); } System.out.println("send finished."); } @@ -83,18 +83,18 @@ public class Main { private static void test(int port) throws Exception { ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port)); - MockResult result = (MockResult) client.request(new RpcMessage(DemoService.class.getName(), "plus", new Class[]{int.class, int.class}, new Object[]{55, 25})).get(); + MockResult result = (MockResult) client.request(new RpcMessage(DemoService.class.getName(), "plus", new Class[]{int.class, int.class}, new Object[]{55, 25}), new CompletableFuture()).get(); System.out.println("55+25=" + result.getResult()); for (int i = 0; i < 100; i++) - client.request(new RpcMessage(DemoService.class.getName(), "sayHello", new Class[]{String.class}, new Object[]{"qianlei" + i})); + client.request(new RpcMessage(DemoService.class.getName(), "sayHello", new Class[]{String.class}, new Object[]{"qianlei" + i}), new CompletableFuture()); for (int i = 0; i < 100; i++) - client.request(new Main.Data()); + client.request(new Main.Data(), new CompletableFuture()); System.out.println("=====test invoke====="); for (int i = 0; i < 100; i++) { - CompletableFuture future = client.request(new Main.Data()); + CompletableFuture future = client.request(new Main.Data(), new CompletableFuture()); System.out.println("invoke and get"); System.out.println("invoke result:" + future.get()); } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java index 93ccc76e51..ab0228fc2d 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java @@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Random; +import java.util.concurrent.CompletableFuture; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; @@ -120,7 +121,9 @@ public class PerformanceClientFixedTest { int index = rd.nextInt(connectionCount); ExchangeClient client = arrays.get(index); // ExchangeClient client = arrays.get(0); - String output = (String) client.request(messageBlock).get(); + CompletableFuture cf = new CompletableFuture(); + client.request(messageBlock, cf); + String output = (String) cf.get(); if (output.lastIndexOf(messageBlock) < 0) { System.out.println("send messageBlock;get " + output); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java index f4dfbc2a3e..b86b17d400 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java @@ -28,6 +28,7 @@ import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; @@ -72,8 +73,9 @@ public class PerformanceClientTest { exchangeClients[i] = Exchangers.connect(url); } - List serverEnvironment = (List) exchangeClients[0].request("environment").get(); - List serverScene = (List) exchangeClients[0].request("scene").get(); + CompletableFuture completableFuture = new CompletableFuture<>(); + List serverEnvironment = (List) exchangeClients[0].request("environment", completableFuture).get(); + List serverScene = (List) exchangeClients[0].request("scene", completableFuture).get(); // Create some data for test StringBuilder buf = new StringBuilder(length); @@ -101,7 +103,8 @@ public class PerformanceClientTest { count.incrementAndGet(); ExchangeClient client = exchangeClients[index.getAndIncrement() % connections]; long start = System.currentTimeMillis(); - String result = (String) client.request(data).get(); + CompletableFuture completableFuture = new CompletableFuture<>(); + String result = (String) client.request(data, completableFuture).get(); long end = System.currentTimeMillis(); if (!data.equals(result)) { throw new IllegalStateException("Invalid result " + result); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java index 0f19d157a7..01db770f64 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java @@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicInteger; public class DefaultFutureTest { @@ -95,7 +96,8 @@ public class DefaultFutureTest { // timeout after 5 seconds. Channel channel = new MockedChannel(); Request request = new Request(10); - DefaultFuture f = DefaultFuture.newFuture(channel, request, 5000, null); + CompletableFuture cf = new CompletableFuture(); + DefaultFuture f = DefaultFuture.newFuture(channel, request, 5000, null, cf); //mark the future is sent DefaultFuture.sent(channel, request); while (!f.isDone()) { @@ -106,7 +108,7 @@ public class DefaultFutureTest { // get operate will throw a timeout exception, because the future is timeout. try { - f.get(); + cf.get(); } catch (Exception e) { Assertions.assertTrue(e.getCause() instanceof TimeoutException, "catch exception is not timeout exception!"); System.out.println(e.getMessage()); @@ -119,7 +121,8 @@ public class DefaultFutureTest { private DefaultFuture defaultFuture(int timeout) { Channel channel = new MockedChannel(); Request request = new Request(index.getAndIncrement()); - return DefaultFuture.newFuture(channel, request, timeout, null); + CompletableFuture completableFuture = new CompletableFuture<>(); + return DefaultFuture.newFuture(channel, request, timeout, null, completableFuture); } } \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java index 2affe7d513..873540d203 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java @@ -29,6 +29,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import java.util.List; +import java.util.concurrent.CompletableFuture; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -143,7 +144,7 @@ public class HeaderExchangeChannelTest { Assertions.assertThrows(RemotingException.class, () -> { header.close(1000); Object requestob = new Object(); - header.request(requestob); + header.request(requestob, CompletableFuture.completedFuture(0)); }); } @@ -153,7 +154,7 @@ public class HeaderExchangeChannelTest { header = new HeaderExchangeChannel(channel); when(channel.getUrl()).thenReturn(url); Object requestob = new Object(); - header.request(requestob); + header.request(requestob, CompletableFuture.completedFuture(0)); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Request.class); verify(channel, times(1)).send(argumentCaptor.capture()); Assertions.assertEquals(argumentCaptor.getValue().getData(), requestob); @@ -170,7 +171,7 @@ public class HeaderExchangeChannelTest { }; header = new HeaderExchangeChannel(channel); Object requestob = new Object(); - header.request(requestob, 1000); + header.request(requestob, 1000, CompletableFuture.completedFuture(0)); }); } @@ -191,7 +192,7 @@ public class HeaderExchangeChannelTest { public void closeWithTimeoutTest02() { Assertions.assertFalse(channel.isClosed()); Request request = new Request(); - DefaultFuture.newFuture(channel, request, 100, null); + DefaultFuture.newFuture(channel, request, 100, null, CompletableFuture.completedFuture(0)); header.close(100); //return directly header.close(1000); diff --git a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java index 6414434f51..bea1860e83 100644 --- a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java +++ b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java @@ -65,7 +65,7 @@ public abstract class ClientToServerTest { @Test public void testFuture() throws Exception { - CompletableFuture future = client.request(new World("world")); + CompletableFuture future = client.request(new World("world"), new CompletableFuture()); Hello result = (Hello) future.get(); Assertions.assertEquals("hello,world", result.getName()); } diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java index 267f569406..45ecd37c2d 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java @@ -64,7 +64,7 @@ public abstract class ClientToServerTest { @Test public void testFuture() throws Exception { - CompletableFuture future = client.request(new World("world")); + CompletableFuture future = client.request(new World("world"), new CompletableFuture()); Hello result = (Hello) future.get(); Assertions.assertEquals("hello,world", result.getName()); } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java index 9b8db0027c..a67f92b23e 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java @@ -65,7 +65,7 @@ public abstract class ClientToServerTest { @Test public void testFuture() throws Exception { - CompletableFuture future = client.request(new World("world")); + CompletableFuture future = client.request(new World("world"), new CompletableFuture()); Hello result = (Hello) future.get(); Assertions.assertEquals("hello,world", result.getName()); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java index 101851240c..6758d04082 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java @@ -24,7 +24,6 @@ import org.apache.dubbo.remoting.TimeoutException; import org.apache.dubbo.remoting.exchange.ExchangeClient; import org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeClient; import org.apache.dubbo.remoting.transport.ClientDelegate; -import org.apache.dubbo.rpc.AppResponse; import org.apache.dubbo.rpc.AsyncRpcResult; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Result; @@ -34,7 +33,6 @@ import org.apache.dubbo.rpc.protocol.AbstractInvoker; import org.apache.dubbo.rpc.support.RpcUtils; import java.net.InetSocketAddress; -import java.util.concurrent.CompletableFuture; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; @@ -72,15 +70,8 @@ class ChannelWrappedInvoker extends AbstractInvoker { currentClient.send(inv, getUrl().getMethodParameter(invocation.getMethodName(), SENT_KEY, false)); return AsyncRpcResult.newDefaultAsyncResult(invocation); } else { - CompletableFuture responseFuture = currentClient.request(inv); AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); - responseFuture.whenComplete((appResponse, t) -> { - if (t != null) { - asyncRpcResult.completeExceptionally(t); - } else { - asyncRpcResult.complete((AppResponse) appResponse); - } - }); + currentClient.request(inv, asyncRpcResult); return asyncRpcResult; } } catch (RpcException e) { diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java index 4cf6259be5..ea7cb235ed 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java @@ -34,7 +34,6 @@ import org.apache.dubbo.rpc.protocol.AbstractInvoker; import org.apache.dubbo.rpc.support.RpcUtils; import java.util.Set; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.locks.ReentrantLock; @@ -98,8 +97,7 @@ public class DubboInvoker extends AbstractInvoker { AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); ExecutorService executor = getCallbackExecutor(getUrl(), inv); asyncRpcResult.setExecutor(executor); - CompletableFuture responseFuture = currentClient.request(inv, timeout, executor); - asyncRpcResult.subscribeTo(responseFuture); + currentClient.request(inv, timeout, executor, asyncRpcResult); return asyncRpcResult; } } catch (TimeoutException e) { 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 77eae2cefc..ffc3dfe56d 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 @@ -35,8 +35,8 @@ import java.util.concurrent.locks.Lock; 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.LAZY_CONNECT_INITIAL_STATE_KEY; import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DEFAULT_LAZY_CONNECT_INITIAL_STATE; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.LAZY_CONNECT_INITIAL_STATE_KEY; /** * dubbo protocol support class. @@ -88,10 +88,10 @@ final class LazyConnectExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request) throws RemotingException { + public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { warning(); initClient(); - return client.request(request); + return client.request(request, completableFuture); } @Override @@ -109,24 +109,24 @@ final class LazyConnectExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request, int timeout) throws RemotingException { + public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { warning(); initClient(); - return client.request(request, timeout); + return client.request(request, timeout, completableFuture); } @Override - public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { warning(); initClient(); - return client.request(request, executor); + return client.request(request, executor, completableFuture); } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { + public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { warning(); initClient(); - return client.request(request, timeout, executor); + return client.request(request, timeout, executor, completableFuture); } /** 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 ed3c61b2af..c25ed2c4b2 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 @@ -57,8 +57,8 @@ final class ReferenceCountExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request) throws RemotingException { - return client.request(request); + public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { + return client.request(request, completableFuture); } @Override @@ -77,18 +77,18 @@ final class ReferenceCountExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request, int timeout) throws RemotingException { - return client.request(request, timeout); + public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { + return client.request(request, timeout, completableFuture); } @Override - public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { - return client.request(request, executor); + public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + return client.request(request, executor, completableFuture); } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { - return client.request(request, timeout, executor); + public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + return client.request(request, timeout, executor, completableFuture); } @Override diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java index 3d0aee9d03..0125dddcb5 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java @@ -31,7 +31,6 @@ import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.protocol.AbstractInvoker; import java.util.Set; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.locks.ReentrantLock; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; @@ -93,8 +92,7 @@ public class ThriftInvoker extends AbstractInvoker { int timeout = getUrl().getMethodParameter(methodName, TIMEOUT_KEY, DEFAULT_TIMEOUT); AsyncRpcResult asyncRpcResult = new AsyncRpcResult(invocation); - CompletableFuture responseFuture = currentClient.request(inv, timeout); - asyncRpcResult.subscribeTo(responseFuture); + currentClient.request(inv, timeout, asyncRpcResult); return asyncRpcResult; } catch (TimeoutException e) { throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e); diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java index 36fbe6c7f7..405550a687 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java @@ -40,6 +40,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; +import java.util.concurrent.CompletableFuture; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; @@ -133,7 +134,7 @@ public class ThriftCodecTest { Request request = createRequest(); - DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null); + DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null, new CompletableFuture()); TMessage message = new TMessage("echoString", TMessageType.REPLY, ThriftCodec.getSeqId()); @@ -210,7 +211,7 @@ public class ThriftCodecTest { Request request = createRequest(); - DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null); + DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null, new CompletableFuture()); TMessage message = new TMessage("echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId()); From b37347bc706faac057e982d5d1a594af22e52fd4 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 5 Jun 2019 19:45:02 +0800 Subject: [PATCH 15/28] DefaultFuture --- .../java/org/apache/dubbo/rpc/protocol/dubbo/Constants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f8cc1cec27..56a195176b 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 @@ -32,7 +32,7 @@ public interface Constants { String DECODE_IN_IO_THREAD_KEY = "decode.in.io"; - boolean DEFAULT_DECODE_IN_IO_THREAD = true; + boolean DEFAULT_DECODE_IN_IO_THREAD = false; /** * callback inst id From 8f9a05a95d422ecb72015ff30e3e45e605cc5396 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 6 Jun 2019 11:09:43 +0800 Subject: [PATCH 16/28] test threadlessexecutor --- .../java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java index ea7cb235ed..617759fdcb 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java @@ -95,7 +95,7 @@ public class DubboInvoker extends AbstractInvoker { return AsyncRpcResult.newDefaultAsyncResult(invocation); } else { AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); - ExecutorService executor = getCallbackExecutor(getUrl(), inv); + ExecutorService executor = null; asyncRpcResult.setExecutor(executor); currentClient.request(inv, timeout, executor, asyncRpcResult); return asyncRpcResult; From d1a50ee1d7c080023ca0c255b76c96d5ee1ce696 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 6 Jun 2019 18:15:40 +0800 Subject: [PATCH 17/28] test flush --- .../apache/dubbo/remoting/transport/netty4/NettyChannel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java index d0dfa7c85e..6a65bea897 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java @@ -156,7 +156,7 @@ final class NettyChannel extends AbstractChannel { boolean success = true; int timeout = 0; try { - ChannelFuture future = channel.writeAndFlush(message); + ChannelFuture future = channel.write(message); if (sent) { // wait timeout ms timeout = getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT); From e412157d62614f567f2d0f68441293b9cd46c7da Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 6 Jun 2019 18:16:02 +0800 Subject: [PATCH 18/28] revert flush --- .../apache/dubbo/remoting/transport/netty4/NettyChannel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java index 6a65bea897..d0dfa7c85e 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannel.java @@ -156,7 +156,7 @@ final class NettyChannel extends AbstractChannel { boolean success = true; int timeout = 0; try { - ChannelFuture future = channel.write(message); + ChannelFuture future = channel.writeAndFlush(message); if (sent) { // wait timeout ms timeout = getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT); From b36ec4a89b073751d8ac521ee5e017d1043222bf Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Mon, 10 Jun 2019 15:58:32 +0800 Subject: [PATCH 19/28] threadless executor --- .../java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java index 617759fdcb..ea7cb235ed 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java @@ -95,7 +95,7 @@ public class DubboInvoker extends AbstractInvoker { return AsyncRpcResult.newDefaultAsyncResult(invocation); } else { AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); - ExecutorService executor = null; + ExecutorService executor = getCallbackExecutor(getUrl(), inv); asyncRpcResult.setExecutor(executor); currentClient.request(inv, timeout, executor, asyncRpcResult); return asyncRpcResult; From c32056c51d17df8747f500e4f3c5d7533f50fc76 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Mon, 10 Jun 2019 16:56:58 +0800 Subject: [PATCH 20/28] test threadless executor --- .../main/java/org/apache/dubbo/rpc/AsyncRpcResult.java | 10 ++++------ .../apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java | 5 ++++- .../apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java index 978bf7b888..e1c5b92fe5 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java @@ -24,8 +24,6 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import java.util.function.Function; /** @@ -131,10 +129,10 @@ public class AsyncRpcResult extends AbstractResult { return super.get(); } - @Override - public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - return this.get(); - } +// @Override +// public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { +// return this.get(); +// } @Override public Object recreate() throws Throwable { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java index 5c06b4e4d1..273122e4ce 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java @@ -27,6 +27,9 @@ import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; /** * This class will work as a wrapper wrapping outside of each protocol invoker. @@ -51,7 +54,7 @@ public class AsyncToSyncInvoker implements Invoker { try { if (InvokeMode.SYNC == ((RpcInvocation)invocation).getInvokeMode()) { - asyncResult.get(); + asyncResult.get(invoker.getUrl().getParameter(TIMEOUT_KEY, 1000), TimeUnit.MILLISECONDS); } } catch (InterruptedException e) { throw new RpcException("Interrupted unexpectedly while waiting for remoting result to return! method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java index ea7cb235ed..617759fdcb 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java @@ -95,7 +95,7 @@ public class DubboInvoker extends AbstractInvoker { return AsyncRpcResult.newDefaultAsyncResult(invocation); } else { AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); - ExecutorService executor = getCallbackExecutor(getUrl(), inv); + ExecutorService executor = null; asyncRpcResult.setExecutor(executor); currentClient.request(inv, timeout, executor, asyncRpcResult); return asyncRpcResult; From 701f921e86bbec44a95ceb06bc32fdac05c070d7 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Tue, 11 Jun 2019 13:41:23 +0800 Subject: [PATCH 21/28] with threadless executor --- .../main/java/org/apache/dubbo/rpc/AsyncRpcResult.java | 10 ++++++---- .../apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java | 5 +---- .../apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java index e1c5b92fe5..978bf7b888 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java @@ -24,6 +24,8 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.function.Function; /** @@ -129,10 +131,10 @@ public class AsyncRpcResult extends AbstractResult { return super.get(); } -// @Override -// public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { -// return this.get(); -// } + @Override + public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { + return this.get(); + } @Override public Object recreate() throws Throwable { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java index 273122e4ce..5c06b4e4d1 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java @@ -27,9 +27,6 @@ import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; - -import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; /** * This class will work as a wrapper wrapping outside of each protocol invoker. @@ -54,7 +51,7 @@ public class AsyncToSyncInvoker implements Invoker { try { if (InvokeMode.SYNC == ((RpcInvocation)invocation).getInvokeMode()) { - asyncResult.get(invoker.getUrl().getParameter(TIMEOUT_KEY, 1000), TimeUnit.MILLISECONDS); + asyncResult.get(); } } catch (InterruptedException e) { throw new RpcException("Interrupted unexpectedly while waiting for remoting result to return! method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java index 617759fdcb..ea7cb235ed 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java @@ -95,7 +95,7 @@ public class DubboInvoker extends AbstractInvoker { return AsyncRpcResult.newDefaultAsyncResult(invocation); } else { AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); - ExecutorService executor = null; + ExecutorService executor = getCallbackExecutor(getUrl(), inv); asyncRpcResult.setExecutor(executor); currentClient.request(inv, timeout, executor, asyncRpcResult); return asyncRpcResult; From dbb2499d1f2e52f0ff84c1b37b71ef4bacbc439d Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 12 Jun 2019 17:09:49 +0800 Subject: [PATCH 22/28] Don't merge, disable routers --- .../integration/RegistryDirectory.java | 77 +++++++++---------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index 2f3a83567a..c27ffd8cb0 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -18,7 +18,6 @@ package org.apache.dubbo.registry.integration; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.URLBuilder; -import org.apache.dubbo.common.Version; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; @@ -34,7 +33,6 @@ import org.apache.dubbo.remoting.Constants; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Protocol; -import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.cluster.Cluster; import org.apache.dubbo.rpc.cluster.Configurator; import org.apache.dubbo.rpc.cluster.Router; @@ -560,43 +558,44 @@ public class RegistryDirectory extends AbstractDirectory implements Notify @Override public List> doList(Invocation invocation) { - if (forbidden) { - // 1. No service provider 2. Service providers are disabled - throw new RpcException(RpcException.FORBIDDEN_EXCEPTION, "No provider available from registry " + - getUrl().getAddress() + " for service " + getConsumerUrl().getServiceKey() + " on consumer " + - NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + - ", please check status of providers(disabled, not registered or in blacklist)."); - } - - if (multiGroup) { - return this.invokers == null ? Collections.emptyList() : this.invokers; - } - - List> invokers = null; - try { - // Get invokers from cache, only runtime routers will be executed. - invokers = routerChain.route(getConsumerUrl(), invocation); - } catch (Throwable t) { - logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t); - } - - - // FIXME Is there any need of failing back to Constants.ANY_VALUE or the first available method invokers when invokers is null? - /*Map>> localMethodInvokerMap = this.methodInvokerMap; // local reference - if (localMethodInvokerMap != null && localMethodInvokerMap.size() > 0) { - String methodName = RpcUtils.getMethodName(invocation); - invokers = localMethodInvokerMap.get(methodName); - if (invokers == null) { - invokers = localMethodInvokerMap.get(Constants.ANY_VALUE); - } - if (invokers == null) { - Iterator>> iterator = localMethodInvokerMap.values().iterator(); - if (iterator.hasNext()) { - invokers = iterator.next(); - } - } - }*/ - return invokers == null ? Collections.emptyList() : invokers; +// if (forbidden) { +// // 1. No service provider 2. Service providers are disabled +// throw new RpcException(RpcException.FORBIDDEN_EXCEPTION, "No provider available from registry " + +// getUrl().getAddress() + " for service " + getConsumerUrl().getServiceKey() + " on consumer " + +// NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + +// ", please check status of providers(disabled, not registered or in blacklist)."); +// } +// +// if (multiGroup) { +// return this.invokers == null ? Collections.emptyList() : this.invokers; +// } +// +// List> invokers = null; +// try { +// // Get invokers from cache, only runtime routers will be executed. +// invokers = routerChain.route(getConsumerUrl(), invocation); +// } catch (Throwable t) { +// logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t); +// } +// +// +// // FIXME Is there any need of failing back to Constants.ANY_VALUE or the first available method invokers when invokers is null? +// /*Map>> localMethodInvokerMap = this.methodInvokerMap; // local reference +// if (localMethodInvokerMap != null && localMethodInvokerMap.size() > 0) { +// String methodName = RpcUtils.getMethodName(invocation); +// invokers = localMethodInvokerMap.get(methodName); +// if (invokers == null) { +// invokers = localMethodInvokerMap.get(Constants.ANY_VALUE); +// } +// if (invokers == null) { +// Iterator>> iterator = localMethodInvokerMap.values().iterator(); +// if (iterator.hasNext()) { +// invokers = iterator.next(); +// } +// } +// }*/ +// return invokers == null ? Collections.emptyList() : invokers; + return invokers; } @Override From 8f6d5954c5649782ce46d79d60c0c0826b6a3160 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Sat, 15 Jun 2019 13:32:19 +0800 Subject: [PATCH 23/28] remove method check --- .../rpc/proxy/InvokerInvocationHandler.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java index 9cd1f27ca2..e310ac7b48 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java @@ -38,19 +38,19 @@ public class InvokerInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); - Class[] parameterTypes = method.getParameterTypes(); - if (method.getDeclaringClass() == Object.class) { - return method.invoke(invoker, args); - } - if ("toString".equals(methodName) && parameterTypes.length == 0) { - return invoker.toString(); - } - if ("hashCode".equals(methodName) && parameterTypes.length == 0) { - return invoker.hashCode(); - } - if ("equals".equals(methodName) && parameterTypes.length == 1) { - return invoker.equals(args[0]); - } +// Class[] parameterTypes = method.getParameterTypes(); +// if (method.getDeclaringClass() == Object.class) { +// return method.invoke(invoker, args); +// } +// if ("toString".equals(methodName) && parameterTypes.length == 0) { +// return invoker.toString(); +// } +// if ("hashCode".equals(methodName) && parameterTypes.length == 0) { +// return invoker.hashCode(); +// } +// if ("equals".equals(methodName) && parameterTypes.length == 1) { +// return invoker.equals(args[0]); +// } return invoker.invoke(new RpcInvocation(method, invoker.getInterface().getName(), args)).recreate(); } From 6f638607b0f5827c077b60b562ecc22fc06c656b Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 26 Jun 2019 10:59:29 +0800 Subject: [PATCH 24/28] test thenApplyWithContext --- .../org/apache/dubbo/rpc/AsyncRpcResult.java | 9 ++- .../rpc/protocol/ProtocolFilterWrapper.java | 72 ++++--------------- 2 files changed, 17 insertions(+), 64 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java index 978bf7b888..debbfebc28 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java @@ -145,12 +145,11 @@ public class AsyncRpcResult extends AbstractResult { return getAppResponse().recreate(); } - @Override public Result thenApplyWithContext(Function fn) { - this.thenApply(fn.compose(beforeContext).andThen(afterContext)); - // You may need to return a new Result instance representing the next async stage, - // like thenApply will return a new CompletableFuture. - return this; + CompletableFuture future = this.thenApply(fn.compose(beforeContext).andThen(afterContext)); + AsyncRpcResult nextAsyncRpcResult = new AsyncRpcResult(this); + nextAsyncRpcResult.subscribeTo(future); + return nextAsyncRpcResult; } public void subscribeTo(CompletableFuture future) { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/ProtocolFilterWrapper.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/ProtocolFilterWrapper.java index f54d07688b..bfaf14d2a5 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/ProtocolFilterWrapper.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/ProtocolFilterWrapper.java @@ -48,12 +48,9 @@ public class ProtocolFilterWrapper implements Protocol { this.protocol = protocol; } - - private static Invoker buildInvokerChain(final Invoker invoker, String key, String group) { Invoker last = invoker; List filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group); - if (!filters.isEmpty()) { for (int i = filters.size() - 1; i >= 0; i--) { final Filter filter = filters.get(i); @@ -90,7 +87,18 @@ public class ProtocolFilterWrapper implements Protocol { } throw e; } - return asyncResult; + return asyncResult.thenApplyWithContext(r -> { + // onResponse callback + if (filter instanceof ListenableFilter) { + Filter.Listener listener = ((ListenableFilter) filter).listener(); + if (listener != null) { + listener.onResponse(r, invoker, invocation); + } + } else { + filter.onResponse(r, invoker, invocation); + } + return r; + }); } @Override @@ -105,8 +113,7 @@ public class ProtocolFilterWrapper implements Protocol { }; } } - - return new CallbackRegistrationInvoker<>(last, filters); + return last; } @Override @@ -135,57 +142,4 @@ public class ProtocolFilterWrapper implements Protocol { protocol.destroy(); } - static class CallbackRegistrationInvoker implements Invoker { - - private final Invoker filterInvoker; - private final List filters; - - public CallbackRegistrationInvoker(Invoker filterInvoker, List filters) { - this.filterInvoker = filterInvoker; - this.filters = filters; - } - - @Override - public Result invoke(Invocation invocation) throws RpcException { - Result asyncResult = filterInvoker.invoke(invocation); - - asyncResult.thenApplyWithContext(r -> { - for (int i = filters.size() - 1; i >= 0; i--) { - Filter filter = filters.get(i); - // onResponse callback - if (filter instanceof ListenableFilter) { - Filter.Listener listener = ((ListenableFilter) filter).listener(); - if (listener != null) { - listener.onResponse(r, filterInvoker, invocation); - } - } else { - filter.onResponse(r, filterInvoker, invocation); - } - } - return r; - }); - - return asyncResult; - } - - @Override - public Class getInterface() { - return filterInvoker.getInterface(); - } - - @Override - public URL getUrl() { - return filterInvoker.getUrl(); - } - - @Override - public boolean isAvailable() { - return filterInvoker.isAvailable(); - } - - @Override - public void destroy() { - filterInvoker.destroy(); - } - } } From 5434a52edebab138290c2f525cdeabff4e5caa54 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 26 Jun 2019 13:02:38 +0800 Subject: [PATCH 25/28] Revert "DefaultFuture (#4257)" This reverts commit b2dbc5ef5da92e3ee5eba06d64a8e00446dd65c7. --- .../dubbo/demo/provider/DemoServiceImpl.java | 10 ++++++ .../dubbo/registry/dubbo/MockChannel.java | 8 ++--- .../dubbo/registry/dubbo/MockedClient.java | 14 ++++---- .../remoting/exchange/ExchangeChannel.java | 8 ++--- .../exchange/support/DefaultFuture.java | 36 ++++++++----------- .../support/header/HeaderExchangeChannel.java | 18 +++++----- .../support/header/HeaderExchangeClient.java | 20 +++++------ .../java/org/apache/dubbo/remoting/Main.java | 10 +++--- .../remoting/PerformanceClientFixedTest.java | 5 +-- .../dubbo/remoting/PerformanceClientTest.java | 9 ++--- .../exchange/support/DefaultFutureTest.java | 9 ++--- .../header/HeaderExchangeChannelTest.java | 9 +++-- .../transport/mina/ClientToServerTest.java | 2 +- .../transport/netty/ClientToServerTest.java | 2 +- .../transport/netty4/ClientToServerTest.java | 2 +- .../protocol/dubbo/ChannelWrappedInvoker.java | 11 +++++- .../rpc/protocol/dubbo/DubboInvoker.java | 4 ++- .../dubbo/LazyConnectExchangeClient.java | 18 +++++----- .../dubbo/ReferenceCountExchangeClient.java | 16 ++++----- .../rpc/protocol/thrift/ThriftInvoker.java | 4 ++- .../rpc/protocol/thrift/ThriftCodecTest.java | 5 ++- 21 files changed, 112 insertions(+), 108 deletions(-) diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java index bceeae202d..5e57f77e51 100644 --- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java +++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/src/main/java/org/apache/dubbo/demo/provider/DemoServiceImpl.java @@ -30,12 +30,22 @@ public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); } @Override public CompletableFuture sayHelloAsync(String name) { CompletableFuture cf = CompletableFuture.supplyAsync(() -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } return "async result"; }); return cf; diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java index 9c43359172..e563da616c 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockChannel.java @@ -86,21 +86,21 @@ public class MockChannel implements ExchangeChannel { return null; } - public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object request) throws RemotingException { return null; } - public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object request, int timeout) throws RemotingException { return null; } @Override - public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { return null; } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { return null; } diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java index bd62d3a62f..ad06cd2096 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/MockedClient.java @@ -81,21 +81,21 @@ public class MockedClient implements ExchangeClient { this.sent = msg; } - public CompletableFuture request(Object msg, CompletableFuture completableFuture) throws RemotingException { - return request(msg, null, completableFuture); + public CompletableFuture request(Object msg) throws RemotingException { + return request(msg, null); } - public CompletableFuture request(Object msg, int timeout, CompletableFuture completableFuture) throws RemotingException { - return this.request(msg, timeout, null, completableFuture); + public CompletableFuture request(Object msg, int timeout) throws RemotingException { + return this.request(msg, timeout, null); } @Override - public CompletableFuture request(Object msg, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { - return this.request(msg, 0, executor, completableFuture); + public CompletableFuture request(Object msg, ExecutorService executor) throws RemotingException { + return this.request(msg, 0, executor); } @Override - public CompletableFuture request(Object msg, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object msg, int timeout, ExecutorService executor) throws RemotingException { this.invoked = msg; return new CompletableFuture() { public Object get() throws InterruptedException, ExecutionException { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java index 48a9f6ff2f..c0cf131d8e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/ExchangeChannel.java @@ -35,7 +35,7 @@ public interface ExchangeChannel extends Channel { * @throws RemotingException */ @Deprecated - CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException; + CompletableFuture request(Object request) throws RemotingException; /** * send request. @@ -46,7 +46,7 @@ public interface ExchangeChannel extends Channel { * @throws RemotingException */ @Deprecated - CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException; + CompletableFuture request(Object request, int timeout) throws RemotingException; /** * send request. @@ -55,7 +55,7 @@ public interface ExchangeChannel extends Channel { * @return response future * @throws RemotingException */ - CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException; + CompletableFuture request(Object request, ExecutorService executor) throws RemotingException; /** * send request. @@ -65,7 +65,7 @@ public interface ExchangeChannel extends Channel { * @return response future * @throws RemotingException */ - CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException; + CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException; /** * get message handler. diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index a23e8cd919..0abecf2030 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -35,7 +35,6 @@ import java.util.Date; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; @@ -45,7 +44,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; /** * DefaultFuture. */ -public class DefaultFuture { +public class DefaultFuture extends CompletableFuture { private static final Logger logger = LoggerFactory.getLogger(DefaultFuture.class); @@ -53,9 +52,6 @@ public class DefaultFuture { private static final Map FUTURES = new ConcurrentHashMap<>(); - // private static final Map PENDING_TASKS = new ConcurrentHashMap<>(); - private final CompletableFuture completableFuture; - public static final Timer TIME_OUT_TIMER = new HashedWheelTimer( new NamedThreadFactory("dubbo-future-timeout", true), 30, @@ -70,18 +66,20 @@ public class DefaultFuture { private volatile long sent; private Timeout timeoutCheckTask; - private final ExecutorService executor; + private ExecutorService executor; public ExecutorService getExecutor() { return executor; } - private DefaultFuture(Channel channel, Request request, int timeout, ExecutorService executor, CompletableFuture completableFuture) { + public void setExecutor(ExecutorService executor) { + this.executor = executor; + } + + private DefaultFuture(Channel channel, Request request, int timeout) { this.channel = channel; this.request = request; this.id = request.getId(); - this.executor = executor; - this.completableFuture = completableFuture; this.timeout = timeout > 0 ? timeout : channel.getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT); // put into waiting map. FUTURES.put(id, this); @@ -106,8 +104,9 @@ public class DefaultFuture { * @param timeout timeout * @return a new DefaultFuture */ - public static DefaultFuture newFuture(Channel channel, Request request, int timeout, ExecutorService executor, CompletableFuture completableFuture) { - final DefaultFuture future = new DefaultFuture(channel, request, timeout, executor, completableFuture); + public static DefaultFuture newFuture(Channel channel, Request request, int timeout, ExecutorService executor) { + final DefaultFuture future = new DefaultFuture(channel, request, timeout); + future.setExecutor(executor); // timeout check timeoutCheck(future); return future; @@ -177,6 +176,7 @@ public class DefaultFuture { } } + @Override public boolean cancel(boolean mayInterruptIfRunning) { Response errorResult = new Response(id); errorResult.setStatus(Response.CLIENT_ERROR); @@ -187,14 +187,6 @@ public class DefaultFuture { return true; } - public boolean isDone() { - return this.completableFuture.isDone(); - } - - public Object get() throws ExecutionException, InterruptedException { - return this.completableFuture.get(); - } - public void cancel() { this.cancel(true); } @@ -204,11 +196,11 @@ public class DefaultFuture { throw new IllegalStateException("response cannot be null"); } if (res.getStatus() == Response.OK) { - this.completableFuture.complete(res.getResult()); + this.complete(res.getResult()); } else if (res.getStatus() == Response.CLIENT_TIMEOUT || res.getStatus() == Response.SERVER_TIMEOUT) { - this.completableFuture.completeExceptionally(new TimeoutException(res.getStatus() == Response.SERVER_TIMEOUT, channel, res.getErrorMessage())); + this.completeExceptionally(new TimeoutException(res.getStatus() == Response.SERVER_TIMEOUT, channel, res.getErrorMessage())); } else { - this.completableFuture.completeExceptionally(new RemotingException(channel, res.getErrorMessage())); + this.completeExceptionally(new RemotingException(channel, res.getErrorMessage())); } // the result is returning, but the caller thread may still waiting diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java index a82bf53171..b95461294f 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannel.java @@ -106,22 +106,22 @@ final class HeaderExchangeChannel implements ExchangeChannel { } @Override - public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { - return request(request, null, completableFuture); + public CompletableFuture request(Object request) throws RemotingException { + return request(request, null); } @Override - public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { - return request(request, timeout, null, completableFuture); + public CompletableFuture request(Object request, int timeout) throws RemotingException { + return request(request, timeout, null); } @Override - public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { - return request(request, channel.getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT), executor, completableFuture); + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + return request(request, channel.getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT), executor); } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { if (closed) { throw new RemotingException(this.getLocalAddress(), null, "Failed to send request " + request + ", cause: The channel " + this + " is closed!"); } @@ -130,14 +130,14 @@ final class HeaderExchangeChannel implements ExchangeChannel { req.setVersion(Version.getProtocolVersion()); req.setTwoWay(true); req.setData(request); - DefaultFuture future = DefaultFuture.newFuture(channel, req, timeout, executor, completableFuture); + DefaultFuture future = DefaultFuture.newFuture(channel, req, timeout, executor); try { channel.send(req); } catch (RemotingException e) { future.cancel(); throw e; } - return completableFuture; + return future; } @Override diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java index 8e04b99bbb..6671d1a30b 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java @@ -34,11 +34,11 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; +import static org.apache.dubbo.remoting.utils.UrlUtils.getHeartbeat; +import static org.apache.dubbo.remoting.utils.UrlUtils.getIdleTimeout; import static org.apache.dubbo.remoting.Constants.HEARTBEAT_CHECK_TICK; import static org.apache.dubbo.remoting.Constants.LEAST_HEARTBEAT_DURATION; import static org.apache.dubbo.remoting.Constants.TICKS_PER_WHEEL; -import static org.apache.dubbo.remoting.utils.UrlUtils.getHeartbeat; -import static org.apache.dubbo.remoting.utils.UrlUtils.getIdleTimeout; /** * DefaultMessageClient @@ -66,8 +66,8 @@ public class HeaderExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { - return channel.request(request, completableFuture); + public CompletableFuture request(Object request) throws RemotingException { + return channel.request(request); } @Override @@ -81,18 +81,18 @@ public class HeaderExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { - return channel.request(request, timeout, completableFuture); + public CompletableFuture request(Object request, int timeout) throws RemotingException { + return channel.request(request, timeout); } @Override - public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { - return channel.request(request, executor, completableFuture); + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + return channel.request(request, executor); } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { - return channel.request(request, timeout, executor, completableFuture); + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { + return channel.request(request, timeout, executor); } @Override diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java index 9869bcc91f..4478f86ecf 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/Main.java @@ -62,7 +62,7 @@ public class Main { sb.append("(" + random.nextLong() + ")"); Main.Data d = new Main.Data(); d.setData(sb.toString()); - client.request(d, new CompletableFuture()).get(); + client.request(d).get(); } System.out.println("send finished."); } @@ -83,18 +83,18 @@ public class Main { private static void test(int port) throws Exception { ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port)); - MockResult result = (MockResult) client.request(new RpcMessage(DemoService.class.getName(), "plus", new Class[]{int.class, int.class}, new Object[]{55, 25}), new CompletableFuture()).get(); + MockResult result = (MockResult) client.request(new RpcMessage(DemoService.class.getName(), "plus", new Class[]{int.class, int.class}, new Object[]{55, 25})).get(); System.out.println("55+25=" + result.getResult()); for (int i = 0; i < 100; i++) - client.request(new RpcMessage(DemoService.class.getName(), "sayHello", new Class[]{String.class}, new Object[]{"qianlei" + i}), new CompletableFuture()); + client.request(new RpcMessage(DemoService.class.getName(), "sayHello", new Class[]{String.class}, new Object[]{"qianlei" + i})); for (int i = 0; i < 100; i++) - client.request(new Main.Data(), new CompletableFuture()); + client.request(new Main.Data()); System.out.println("=====test invoke====="); for (int i = 0; i < 100; i++) { - CompletableFuture future = client.request(new Main.Data(), new CompletableFuture()); + CompletableFuture future = client.request(new Main.Data()); System.out.println("invoke and get"); System.out.println("invoke result:" + future.get()); } diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java index ab0228fc2d..93ccc76e51 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientFixedTest.java @@ -25,7 +25,6 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Random; -import java.util.concurrent.CompletableFuture; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; @@ -121,9 +120,7 @@ public class PerformanceClientFixedTest { int index = rd.nextInt(connectionCount); ExchangeClient client = arrays.get(index); // ExchangeClient client = arrays.get(0); - CompletableFuture cf = new CompletableFuture(); - client.request(messageBlock, cf); - String output = (String) cf.get(); + String output = (String) client.request(messageBlock).get(); if (output.lastIndexOf(messageBlock) < 0) { System.out.println("send messageBlock;get " + output); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java index b86b17d400..f4dfbc2a3e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/PerformanceClientTest.java @@ -28,7 +28,6 @@ import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; @@ -73,9 +72,8 @@ public class PerformanceClientTest { exchangeClients[i] = Exchangers.connect(url); } - CompletableFuture completableFuture = new CompletableFuture<>(); - List serverEnvironment = (List) exchangeClients[0].request("environment", completableFuture).get(); - List serverScene = (List) exchangeClients[0].request("scene", completableFuture).get(); + List serverEnvironment = (List) exchangeClients[0].request("environment").get(); + List serverScene = (List) exchangeClients[0].request("scene").get(); // Create some data for test StringBuilder buf = new StringBuilder(length); @@ -103,8 +101,7 @@ public class PerformanceClientTest { count.incrementAndGet(); ExchangeClient client = exchangeClients[index.getAndIncrement() % connections]; long start = System.currentTimeMillis(); - CompletableFuture completableFuture = new CompletableFuture<>(); - String result = (String) client.request(data, completableFuture).get(); + String result = (String) client.request(data).get(); long end = System.currentTimeMillis(); if (!data.equals(result)) { throw new IllegalStateException("Invalid result " + result); diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java index 01db770f64..0f19d157a7 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/DefaultFutureTest.java @@ -27,7 +27,6 @@ import org.junit.jupiter.api.Test; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicInteger; public class DefaultFutureTest { @@ -96,8 +95,7 @@ public class DefaultFutureTest { // timeout after 5 seconds. Channel channel = new MockedChannel(); Request request = new Request(10); - CompletableFuture cf = new CompletableFuture(); - DefaultFuture f = DefaultFuture.newFuture(channel, request, 5000, null, cf); + DefaultFuture f = DefaultFuture.newFuture(channel, request, 5000, null); //mark the future is sent DefaultFuture.sent(channel, request); while (!f.isDone()) { @@ -108,7 +106,7 @@ public class DefaultFutureTest { // get operate will throw a timeout exception, because the future is timeout. try { - cf.get(); + f.get(); } catch (Exception e) { Assertions.assertTrue(e.getCause() instanceof TimeoutException, "catch exception is not timeout exception!"); System.out.println(e.getMessage()); @@ -121,8 +119,7 @@ public class DefaultFutureTest { private DefaultFuture defaultFuture(int timeout) { Channel channel = new MockedChannel(); Request request = new Request(index.getAndIncrement()); - CompletableFuture completableFuture = new CompletableFuture<>(); - return DefaultFuture.newFuture(channel, request, timeout, null, completableFuture); + return DefaultFuture.newFuture(channel, request, timeout, null); } } \ No newline at end of file diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java index 873540d203..2affe7d513 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeChannelTest.java @@ -29,7 +29,6 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import java.util.List; -import java.util.concurrent.CompletableFuture; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -144,7 +143,7 @@ public class HeaderExchangeChannelTest { Assertions.assertThrows(RemotingException.class, () -> { header.close(1000); Object requestob = new Object(); - header.request(requestob, CompletableFuture.completedFuture(0)); + header.request(requestob); }); } @@ -154,7 +153,7 @@ public class HeaderExchangeChannelTest { header = new HeaderExchangeChannel(channel); when(channel.getUrl()).thenReturn(url); Object requestob = new Object(); - header.request(requestob, CompletableFuture.completedFuture(0)); + header.request(requestob); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Request.class); verify(channel, times(1)).send(argumentCaptor.capture()); Assertions.assertEquals(argumentCaptor.getValue().getData(), requestob); @@ -171,7 +170,7 @@ public class HeaderExchangeChannelTest { }; header = new HeaderExchangeChannel(channel); Object requestob = new Object(); - header.request(requestob, 1000, CompletableFuture.completedFuture(0)); + header.request(requestob, 1000); }); } @@ -192,7 +191,7 @@ public class HeaderExchangeChannelTest { public void closeWithTimeoutTest02() { Assertions.assertFalse(channel.isClosed()); Request request = new Request(); - DefaultFuture.newFuture(channel, request, 100, null, CompletableFuture.completedFuture(0)); + DefaultFuture.newFuture(channel, request, 100, null); header.close(100); //return directly header.close(1000); diff --git a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java index bea1860e83..6414434f51 100644 --- a/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java +++ b/dubbo-remoting/dubbo-remoting-mina/src/test/java/org/apache/remoting/transport/mina/ClientToServerTest.java @@ -65,7 +65,7 @@ public abstract class ClientToServerTest { @Test public void testFuture() throws Exception { - CompletableFuture future = client.request(new World("world"), new CompletableFuture()); + CompletableFuture future = client.request(new World("world")); Hello result = (Hello) future.get(); Assertions.assertEquals("hello,world", result.getName()); } diff --git a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java index 45ecd37c2d..267f569406 100644 --- a/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java +++ b/dubbo-remoting/dubbo-remoting-netty/src/test/java/org/apache/dubbo/remoting/transport/netty/ClientToServerTest.java @@ -64,7 +64,7 @@ public abstract class ClientToServerTest { @Test public void testFuture() throws Exception { - CompletableFuture future = client.request(new World("world"), new CompletableFuture()); + CompletableFuture future = client.request(new World("world")); Hello result = (Hello) future.get(); Assertions.assertEquals("hello,world", result.getName()); } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java index a67f92b23e..9b8db0027c 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientToServerTest.java @@ -65,7 +65,7 @@ public abstract class ClientToServerTest { @Test public void testFuture() throws Exception { - CompletableFuture future = client.request(new World("world"), new CompletableFuture()); + CompletableFuture future = client.request(new World("world")); Hello result = (Hello) future.get(); Assertions.assertEquals("hello,world", result.getName()); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java index 6758d04082..101851240c 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java @@ -24,6 +24,7 @@ import org.apache.dubbo.remoting.TimeoutException; import org.apache.dubbo.remoting.exchange.ExchangeClient; import org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeClient; import org.apache.dubbo.remoting.transport.ClientDelegate; +import org.apache.dubbo.rpc.AppResponse; import org.apache.dubbo.rpc.AsyncRpcResult; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Result; @@ -33,6 +34,7 @@ import org.apache.dubbo.rpc.protocol.AbstractInvoker; import org.apache.dubbo.rpc.support.RpcUtils; import java.net.InetSocketAddress; +import java.util.concurrent.CompletableFuture; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; @@ -70,8 +72,15 @@ class ChannelWrappedInvoker extends AbstractInvoker { currentClient.send(inv, getUrl().getMethodParameter(invocation.getMethodName(), SENT_KEY, false)); return AsyncRpcResult.newDefaultAsyncResult(invocation); } else { + CompletableFuture responseFuture = currentClient.request(inv); AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); - currentClient.request(inv, asyncRpcResult); + responseFuture.whenComplete((appResponse, t) -> { + if (t != null) { + asyncRpcResult.completeExceptionally(t); + } else { + asyncRpcResult.complete((AppResponse) appResponse); + } + }); return asyncRpcResult; } } catch (RpcException e) { diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java index ea7cb235ed..4cf6259be5 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java @@ -34,6 +34,7 @@ import org.apache.dubbo.rpc.protocol.AbstractInvoker; import org.apache.dubbo.rpc.support.RpcUtils; import java.util.Set; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.locks.ReentrantLock; @@ -97,7 +98,8 @@ public class DubboInvoker extends AbstractInvoker { AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); ExecutorService executor = getCallbackExecutor(getUrl(), inv); asyncRpcResult.setExecutor(executor); - currentClient.request(inv, timeout, executor, asyncRpcResult); + CompletableFuture responseFuture = currentClient.request(inv, timeout, executor); + asyncRpcResult.subscribeTo(responseFuture); return asyncRpcResult; } } catch (TimeoutException e) { 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 ffc3dfe56d..77eae2cefc 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 @@ -35,8 +35,8 @@ import java.util.concurrent.locks.Lock; 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.LAZY_CONNECT_INITIAL_STATE_KEY; +import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DEFAULT_LAZY_CONNECT_INITIAL_STATE; /** * dubbo protocol support class. @@ -88,10 +88,10 @@ final class LazyConnectExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object request) throws RemotingException { warning(); initClient(); - return client.request(request, completableFuture); + return client.request(request); } @Override @@ -109,24 +109,24 @@ final class LazyConnectExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object request, int timeout) throws RemotingException { warning(); initClient(); - return client.request(request, timeout, completableFuture); + return client.request(request, timeout); } @Override - public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { warning(); initClient(); - return client.request(request, executor, completableFuture); + return client.request(request, executor); } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { warning(); initClient(); - return client.request(request, timeout, executor, completableFuture); + return client.request(request, timeout, executor); } /** 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 c25ed2c4b2..ed3c61b2af 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 @@ -57,8 +57,8 @@ final class ReferenceCountExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request, CompletableFuture completableFuture) throws RemotingException { - return client.request(request, completableFuture); + public CompletableFuture request(Object request) throws RemotingException { + return client.request(request); } @Override @@ -77,18 +77,18 @@ final class ReferenceCountExchangeClient implements ExchangeClient { } @Override - public CompletableFuture request(Object request, int timeout, CompletableFuture completableFuture) throws RemotingException { - return client.request(request, timeout, completableFuture); + public CompletableFuture request(Object request, int timeout) throws RemotingException { + return client.request(request, timeout); } @Override - public CompletableFuture request(Object request, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { - return client.request(request, executor, completableFuture); + public CompletableFuture request(Object request, ExecutorService executor) throws RemotingException { + return client.request(request, executor); } @Override - public CompletableFuture request(Object request, int timeout, ExecutorService executor, CompletableFuture completableFuture) throws RemotingException { - return client.request(request, timeout, executor, completableFuture); + public CompletableFuture request(Object request, int timeout, ExecutorService executor) throws RemotingException { + return client.request(request, timeout, executor); } @Override diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java index 0125dddcb5..3d0aee9d03 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java @@ -31,6 +31,7 @@ import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.protocol.AbstractInvoker; import java.util.Set; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.locks.ReentrantLock; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; @@ -92,7 +93,8 @@ public class ThriftInvoker extends AbstractInvoker { int timeout = getUrl().getMethodParameter(methodName, TIMEOUT_KEY, DEFAULT_TIMEOUT); AsyncRpcResult asyncRpcResult = new AsyncRpcResult(invocation); - currentClient.request(inv, timeout, asyncRpcResult); + CompletableFuture responseFuture = currentClient.request(inv, timeout); + asyncRpcResult.subscribeTo(responseFuture); return asyncRpcResult; } catch (TimeoutException e) { throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e); diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java index 405550a687..36fbe6c7f7 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodecTest.java @@ -40,7 +40,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; -import java.util.concurrent.CompletableFuture; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY; @@ -134,7 +133,7 @@ public class ThriftCodecTest { Request request = createRequest(); - DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null, new CompletableFuture()); + DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null); TMessage message = new TMessage("echoString", TMessageType.REPLY, ThriftCodec.getSeqId()); @@ -211,7 +210,7 @@ public class ThriftCodecTest { Request request = createRequest(); - DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null, new CompletableFuture()); + DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null); TMessage message = new TMessage("echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId()); From ee77897caf6ea9f9bb19e6d6ae4af771d39178ee Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 26 Jun 2019 13:39:49 +0800 Subject: [PATCH 26/28] use composition to replace CompletableStage interface --- .../java/com/alibaba/dubbo/rpc/Result.java | 28 ++++++++- .../org/apache/dubbo/rpc/AbstractResult.java | 4 +- .../org/apache/dubbo/rpc/AppResponse.java | 21 ++++++- .../org/apache/dubbo/rpc/AsyncRpcResult.java | 62 ++++++++++--------- .../org/apache/dubbo/rpc/FutureAdapter.java | 6 +- .../java/org/apache/dubbo/rpc/Result.java | 23 +++---- .../dubbo/rpc/protocol/AbstractInvoker.java | 2 +- .../dubbo/rpc/proxy/AbstractProxyInvoker.java | 7 +-- .../apache/dubbo/rpc/support/MyInvoker.java | 4 +- .../protocol/dubbo/ChannelWrappedInvoker.java | 12 +--- .../rpc/protocol/dubbo/DubboInvoker.java | 12 ++-- .../rpc/protocol/dubbo/DubboProtocol.java | 2 +- .../rpc/protocol/thrift/ThriftInvoker.java | 10 +-- .../rpc/protocol/thrift/ThriftProtocol.java | 2 +- 14 files changed, 113 insertions(+), 82 deletions(-) diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Result.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Result.java index 07f5df9bf5..324de43d34 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Result.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Result.java @@ -17,7 +17,13 @@ package com.alibaba.dubbo.rpc; +import org.apache.dubbo.rpc.AppResponse; + import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.function.Function; @Deprecated @@ -36,7 +42,27 @@ public interface Result extends org.apache.dubbo.rpc.Result { abstract class AbstractResult extends org.apache.dubbo.rpc.AbstractResult implements Result { @Override - public org.apache.dubbo.rpc.Result thenApplyWithContext(Function fn) { + public void setValue(Object value) { + + } + + @Override + public org.apache.dubbo.rpc.Result thenApplyWithContext(Function fn) { + return null; + } + + @Override + public CompletableFuture thenApply(Function fn) { + return null; + } + + @Override + public org.apache.dubbo.rpc.Result get() throws InterruptedException, ExecutionException { + return null; + } + + @Override + public org.apache.dubbo.rpc.Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return null; } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AbstractResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AbstractResult.java index 2db743d571..6515c08681 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AbstractResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AbstractResult.java @@ -16,10 +16,8 @@ */ package org.apache.dubbo.rpc; -import java.util.concurrent.CompletableFuture; - /** * */ -public abstract class AbstractResult extends CompletableFuture implements Result { +public abstract class AbstractResult implements Result { } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java index a02b16a300..74b623b0c2 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java @@ -20,6 +20,10 @@ import java.io.Serializable; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.function.Function; /** @@ -158,7 +162,22 @@ public class AppResponse extends AbstractResult implements Serializable { } @Override - public Result thenApplyWithContext(Function fn) { + public Result thenApplyWithContext(Function fn) { + throw new UnsupportedOperationException("AppResponse represents an concrete business response, there will be no status changes, you should get internal values directly."); + } + + @Override + public CompletableFuture thenApply(Function fn) { + throw new UnsupportedOperationException("AppResponse represents an concrete business response, there will be no status changes, you should get internal values directly."); + } + + @Override + public Result get() throws InterruptedException, ExecutionException { + throw new UnsupportedOperationException("AppResponse represents an concrete business response, there will be no status changes, you should get internal values directly."); + } + + @Override + public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { throw new UnsupportedOperationException("AppResponse represents an concrete business response, there will be no status changes, you should get internal values directly."); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java index debbfebc28..1379e81dec 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java @@ -55,7 +55,10 @@ public class AsyncRpcResult extends AbstractResult { private Invocation invocation; - public AsyncRpcResult(Invocation invocation) { + private CompletableFuture responseFuture; + + public AsyncRpcResult(CompletableFuture future, Invocation invocation) { + this.responseFuture = future; this.invocation = invocation; this.storedContext = RpcContext.getContext(); this.storedServerContext = RpcContext.getServerContext(); @@ -81,7 +84,7 @@ public class AsyncRpcResult extends AbstractResult { public void setValue(Object value) { AppResponse appResponse = new AppResponse(); appResponse.setValue(value); - this.complete(appResponse); + responseFuture.complete(appResponse); } @Override @@ -93,7 +96,7 @@ public class AsyncRpcResult extends AbstractResult { public void setException(Throwable t) { AppResponse appResponse = new AppResponse(); appResponse.setException(t); - this.complete(appResponse); + responseFuture.complete(appResponse); } @Override @@ -101,10 +104,18 @@ public class AsyncRpcResult extends AbstractResult { return getAppResponse().hasException(); } + public CompletableFuture getResponseFuture() { + return responseFuture; + } + + public void setResponseFuture(CompletableFuture responseFuture) { + this.responseFuture = responseFuture; + } + public Result getAppResponse() { try { - if (this.isDone()) { - return super.get(); + if (responseFuture.isDone()) { + return responseFuture.get(); } } catch (Exception e) { // This should never happen; @@ -128,7 +139,7 @@ public class AsyncRpcResult extends AbstractResult { ThreadlessExecutor threadlessExecutor = (ThreadlessExecutor) executor; threadlessExecutor.waitAndDrain(); } - return super.get(); + return responseFuture.get(); } @Override @@ -145,21 +156,14 @@ public class AsyncRpcResult extends AbstractResult { return getAppResponse().recreate(); } - public Result thenApplyWithContext(Function fn) { - CompletableFuture future = this.thenApply(fn.compose(beforeContext).andThen(afterContext)); - AsyncRpcResult nextAsyncRpcResult = new AsyncRpcResult(this); - nextAsyncRpcResult.subscribeTo(future); - return nextAsyncRpcResult; + public Result thenApplyWithContext(Function fn) { + this.responseFuture = responseFuture.thenApply(fn.compose(beforeContext).andThen(afterContext)); + return this; } - public void subscribeTo(CompletableFuture future) { - future.whenComplete((obj, t) -> { - if (t != null) { - this.completeExceptionally(t); - } else { - this.complete((Result) obj); - } - }); + @Override + public CompletableFuture thenApply(Function fn) { + return this.responseFuture.thenApply(fn); } @Override @@ -218,7 +222,7 @@ public class AsyncRpcResult extends AbstractResult { private RpcContext tmpContext; private RpcContext tmpServerContext; - private Function beforeContext = (appResponse) -> { + private Function beforeContext = (appResponse) -> { tmpContext = RpcContext.getContext(); tmpServerContext = RpcContext.getServerContext(); RpcContext.restoreContext(storedContext); @@ -226,7 +230,7 @@ public class AsyncRpcResult extends AbstractResult { return appResponse; }; - private Function afterContext = (appResponse) -> { + private Function afterContext = (appResponse) -> { RpcContext.restoreContext(tmpContext); RpcContext.restoreServerContext(tmpServerContext); return appResponse; @@ -236,9 +240,7 @@ public class AsyncRpcResult extends AbstractResult { * Some utility methods used to quickly generate default AsyncRpcResult instance. */ public static AsyncRpcResult newDefaultAsyncResult(AppResponse appResponse, Invocation invocation) { - AsyncRpcResult asyncRpcResult = new AsyncRpcResult(invocation); - asyncRpcResult.complete(appResponse); - return asyncRpcResult; + return new AsyncRpcResult(CompletableFuture.completedFuture(appResponse), invocation); } public static AsyncRpcResult newDefaultAsyncResult(Invocation invocation) { @@ -254,15 +256,15 @@ public class AsyncRpcResult extends AbstractResult { } public static AsyncRpcResult newDefaultAsyncResult(Object value, Throwable t, Invocation invocation) { - AsyncRpcResult asyncRpcResult = new AsyncRpcResult(invocation); - AppResponse appResponse = new AppResponse(); + CompletableFuture future = new CompletableFuture<>(); + AppResponse result = new AppResponse(); if (t != null) { - appResponse.setException(t); + result.setException(t); } else { - appResponse.setValue(value); + result.setValue(value); } - asyncRpcResult.complete(appResponse); - return asyncRpcResult; + future.complete(result); + return new AsyncRpcResult(future, invocation); } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/FutureAdapter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/FutureAdapter.java index d50f91e019..c3de6653ea 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/FutureAdapter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/FutureAdapter.java @@ -16,7 +16,6 @@ */ package org.apache.dubbo.rpc; -import java.lang.ref.SoftReference; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; @@ -30,10 +29,7 @@ public class FutureAdapter extends CompletableFuture { private CompletableFuture appResponseFuture; - private SoftReference invocationSoftReference; - - public FutureAdapter(CompletableFuture future, Invocation invocation) { - this.invocationSoftReference = new SoftReference<>(invocation); + public FutureAdapter(CompletableFuture future) { this.appResponseFuture = future; future.whenComplete((appResponse, t) -> { if (t != null) { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Result.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Result.java index 1f3a513831..205df22eea 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Result.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Result.java @@ -20,7 +20,10 @@ import java.io.Serializable; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.function.Function; @@ -38,7 +41,7 @@ import java.util.function.Function; * @see org.apache.dubbo.rpc.Invoker#invoke(Invocation) * @see AppResponse */ -public interface Result extends CompletionStage, Future, Serializable { +public interface Result extends Serializable { /** * Get invoke result. @@ -118,14 +121,6 @@ public interface Result extends CompletionStage, Future, Seriali void setAttachment(String key, String value); - /** - * Returns the specified {@code valueIfAbsent} when not complete, or - * returns the result value or throws an exception when complete. - * - * @see CompletableFuture#getNow(Object) - */ - Result getNow(Result valueIfAbsent); - /** * Add a callback which can be triggered when the RPC call finishes. *

@@ -135,9 +130,11 @@ public interface Result extends CompletionStage, Future, Seriali * @param fn * @return */ - Result thenApplyWithContext(Function fn); + Result thenApplyWithContext(Function fn); - default CompletableFuture completionFuture() { - return toCompletableFuture(); - } + CompletableFuture thenApply(Function fn); + + Result get() throws InterruptedException, ExecutionException; + + Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java index be32bb5498..cdff11ee79 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractInvoker.java @@ -179,7 +179,7 @@ public abstract class AbstractInvoker implements Invoker { } catch (Throwable e) { asyncResult = AsyncRpcResult.newDefaultAsyncResult(null, e, invocation); } - RpcContext.getContext().setFuture(new FutureAdapter(asyncResult, inv)); + RpcContext.getContext().setFuture(new FutureAdapter(asyncResult.getResponseFuture())); return asyncResult; } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java index 4008c447ba..6abac55280 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/AbstractProxyInvoker.java @@ -83,8 +83,7 @@ public abstract class AbstractProxyInvoker implements Invoker { try { Object value = doInvoke(proxy, invocation.getMethodName(), invocation.getParameterTypes(), invocation.getArguments()); CompletableFuture future = wrapWithFuture(value, invocation); - AsyncRpcResult asyncRpcResult = new AsyncRpcResult(invocation); - future.whenComplete((obj, t) -> { + CompletableFuture appResponseFuture = future.handle((obj, t) -> { AppResponse result = new AppResponse(); if (t != null) { if (t instanceof CompletionException) { @@ -95,9 +94,9 @@ public abstract class AbstractProxyInvoker implements Invoker { } else { result.setValue(obj); } - asyncRpcResult.complete(result); + return result; }); - return asyncRpcResult; + return new AsyncRpcResult(appResponseFuture, invocation); } catch (InvocationTargetException e) { if (RpcContext.getContext().isAsyncStarted() && !RpcContext.getContext().stopAsync()) { logger.error("Provider async started, but got an exception from the original method, cannot write the exception back to consumer because an async result may have returned the new thread.", e); diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MyInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MyInvoker.java index 57f1f1e6cc..1b74e6bc41 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MyInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MyInvoker.java @@ -24,6 +24,8 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcException; +import java.util.concurrent.CompletableFuture; + /** * MockInvoker.java */ @@ -66,7 +68,7 @@ public class MyInvoker implements Invoker { result.setException(new RuntimeException("mocked exception")); } - return AsyncRpcResult.newDefaultAsyncResult(result, invocation); + return new AsyncRpcResult(CompletableFuture.completedFuture(result), invocation); } @Override diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java index 101851240c..7f2a6cada0 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/ChannelWrappedInvoker.java @@ -72,16 +72,8 @@ class ChannelWrappedInvoker extends AbstractInvoker { currentClient.send(inv, getUrl().getMethodParameter(invocation.getMethodName(), SENT_KEY, false)); return AsyncRpcResult.newDefaultAsyncResult(invocation); } else { - CompletableFuture responseFuture = currentClient.request(inv); - AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); - responseFuture.whenComplete((appResponse, t) -> { - if (t != null) { - asyncRpcResult.completeExceptionally(t); - } else { - asyncRpcResult.complete((AppResponse) appResponse); - } - }); - return asyncRpcResult; + CompletableFuture appResponseFuture = currentClient.request(inv).thenApply(obj -> (AppResponse) obj); + return new AsyncRpcResult(appResponseFuture, inv); } } catch (RpcException e) { throw e; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java index 4cf6259be5..dc3d61bddb 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java @@ -23,7 +23,9 @@ import org.apache.dubbo.remoting.Constants; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.TimeoutException; import org.apache.dubbo.remoting.exchange.ExchangeClient; +import org.apache.dubbo.rpc.AppResponse; import org.apache.dubbo.rpc.AsyncRpcResult; +import org.apache.dubbo.rpc.FutureAdapter; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; @@ -35,7 +37,6 @@ import org.apache.dubbo.rpc.support.RpcUtils; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; import java.util.concurrent.locks.ReentrantLock; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; @@ -95,12 +96,9 @@ public class DubboInvoker extends AbstractInvoker { RpcContext.getContext().setFuture(null); return AsyncRpcResult.newDefaultAsyncResult(invocation); } else { - AsyncRpcResult asyncRpcResult = new AsyncRpcResult(inv); - ExecutorService executor = getCallbackExecutor(getUrl(), inv); - asyncRpcResult.setExecutor(executor); - CompletableFuture responseFuture = currentClient.request(inv, timeout, executor); - asyncRpcResult.subscribeTo(responseFuture); - return asyncRpcResult; + CompletableFuture appResponseFuture = currentClient.request(inv, timeout).thenApply(obj -> (AppResponse) obj); + RpcContext.getContext().setFuture(new FutureAdapter(appResponseFuture)); + return new AsyncRpcResult(appResponseFuture, inv); } } catch (TimeoutException e) { throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "Invoke remote method timeout. method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e); diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java index ae52a671fc..6779cc2ac2 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java @@ -150,7 +150,7 @@ public class DubboProtocol extends AbstractProtocol { } RpcContext.getContext().setRemoteAddress(channel.getRemoteAddress()); Result result = invoker.invoke(inv); - return result.completionFuture().thenApply(Function.identity()); + return result.thenApply(Function.identity()); } @Override diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java index 3d0aee9d03..9b56b4345b 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftInvoker.java @@ -22,10 +22,13 @@ import org.apache.dubbo.remoting.Constants; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.TimeoutException; import org.apache.dubbo.remoting.exchange.ExchangeClient; +import org.apache.dubbo.rpc.AppResponse; import org.apache.dubbo.rpc.AsyncRpcResult; +import org.apache.dubbo.rpc.FutureAdapter; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; +import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.protocol.AbstractInvoker; @@ -92,10 +95,9 @@ public class ThriftInvoker extends AbstractInvoker { try { int timeout = getUrl().getMethodParameter(methodName, TIMEOUT_KEY, DEFAULT_TIMEOUT); - AsyncRpcResult asyncRpcResult = new AsyncRpcResult(invocation); - CompletableFuture responseFuture = currentClient.request(inv, timeout); - asyncRpcResult.subscribeTo(responseFuture); - return asyncRpcResult; + CompletableFuture appResponseFuture = currentClient.request(inv, timeout).thenApply(obj -> (AppResponse) obj); + RpcContext.getContext().setFuture(new FutureAdapter(appResponseFuture)); + return new AsyncRpcResult(appResponseFuture, invocation); } catch (TimeoutException e) { throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e); } catch (RemotingException e) { diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocol.java b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocol.java index da8bec6a65..b48c4c9e9d 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocol.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftProtocol.java @@ -91,7 +91,7 @@ public class ThriftProtocol extends AbstractProtocol { RpcContext.getContext().setRemoteAddress(channel.getRemoteAddress()); Result result = exporter.getInvoker().invoke(inv); - return result.completionFuture().thenApply(Function.identity()); + return result.thenApply(Function.identity()); } throw new RemotingException(channel, From 6256ec303d21785728f2464009ba2380d6b18eb7 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 26 Jun 2019 14:07:08 +0800 Subject: [PATCH 27/28] call get(timeout, timeunit) --- .../org/apache/dubbo/rpc/AsyncRpcResult.java | 20 +------------------ .../rpc/protocol/AsyncToSyncInvoker.java | 6 +++++- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java index 1379e81dec..cf9a5b32f5 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java @@ -64,12 +64,6 @@ public class AsyncRpcResult extends AbstractResult { this.storedServerContext = RpcContext.getServerContext(); } - public AsyncRpcResult(AsyncRpcResult asyncRpcResult) { - this.invocation = asyncRpcResult.getInvocation(); - this.storedContext = asyncRpcResult.getStoredContext(); - this.storedServerContext = asyncRpcResult.getStoredServerContext(); - } - /** * Notice the return type of {@link #getValue} is the actual type of the RPC method, not {@link AppResponse} * @@ -144,7 +138,7 @@ public class AsyncRpcResult extends AbstractResult { @Override public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - return this.get(); + return responseFuture.get(timeout, unit); } @Override @@ -196,18 +190,6 @@ public class AsyncRpcResult extends AbstractResult { getAppResponse().setAttachment(key, value); } - public RpcContext getStoredContext() { - return storedContext; - } - - public RpcContext getStoredServerContext() { - return storedServerContext; - } - - public Invocation getInvocation() { - return invocation; - } - public Executor getExecutor() { return executor; } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java index 5c06b4e4d1..93fb5d88a7 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java @@ -27,6 +27,10 @@ import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT; +import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY; /** * This class will work as a wrapper wrapping outside of each protocol invoker. @@ -51,7 +55,7 @@ public class AsyncToSyncInvoker implements Invoker { try { if (InvokeMode.SYNC == ((RpcInvocation)invocation).getInvokeMode()) { - asyncResult.get(); + asyncResult.get(getUrl().getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT), TimeUnit.MILLISECONDS); } } catch (InterruptedException e) { throw new RpcException("Interrupted unexpectedly while waiting for remoting result to return! method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e); From 13c01efd12340f1bf065c6b1ca329c63e384bf65 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Wed, 26 Jun 2019 15:56:20 +0800 Subject: [PATCH 28/28] test register callback once --- .../rpc/protocol/ProtocolFilterWrapper.java | 72 +++++++++++++++---- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/ProtocolFilterWrapper.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/ProtocolFilterWrapper.java index bfaf14d2a5..f54d07688b 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/ProtocolFilterWrapper.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/ProtocolFilterWrapper.java @@ -48,9 +48,12 @@ public class ProtocolFilterWrapper implements Protocol { this.protocol = protocol; } + + private static Invoker buildInvokerChain(final Invoker invoker, String key, String group) { Invoker last = invoker; List filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group); + if (!filters.isEmpty()) { for (int i = filters.size() - 1; i >= 0; i--) { final Filter filter = filters.get(i); @@ -87,18 +90,7 @@ public class ProtocolFilterWrapper implements Protocol { } throw e; } - return asyncResult.thenApplyWithContext(r -> { - // onResponse callback - if (filter instanceof ListenableFilter) { - Filter.Listener listener = ((ListenableFilter) filter).listener(); - if (listener != null) { - listener.onResponse(r, invoker, invocation); - } - } else { - filter.onResponse(r, invoker, invocation); - } - return r; - }); + return asyncResult; } @Override @@ -113,7 +105,8 @@ public class ProtocolFilterWrapper implements Protocol { }; } } - return last; + + return new CallbackRegistrationInvoker<>(last, filters); } @Override @@ -142,4 +135,57 @@ public class ProtocolFilterWrapper implements Protocol { protocol.destroy(); } + static class CallbackRegistrationInvoker implements Invoker { + + private final Invoker filterInvoker; + private final List filters; + + public CallbackRegistrationInvoker(Invoker filterInvoker, List filters) { + this.filterInvoker = filterInvoker; + this.filters = filters; + } + + @Override + public Result invoke(Invocation invocation) throws RpcException { + Result asyncResult = filterInvoker.invoke(invocation); + + asyncResult.thenApplyWithContext(r -> { + for (int i = filters.size() - 1; i >= 0; i--) { + Filter filter = filters.get(i); + // onResponse callback + if (filter instanceof ListenableFilter) { + Filter.Listener listener = ((ListenableFilter) filter).listener(); + if (listener != null) { + listener.onResponse(r, filterInvoker, invocation); + } + } else { + filter.onResponse(r, filterInvoker, invocation); + } + } + return r; + }); + + return asyncResult; + } + + @Override + public Class getInterface() { + return filterInvoker.getInterface(); + } + + @Override + public URL getUrl() { + return filterInvoker.getUrl(); + } + + @Override + public boolean isAvailable() { + return filterInvoker.isAvailable(); + } + + @Override + public void destroy() { + filterInvoker.destroy(); + } + } }