This is an automated email from the ASF dual-hosted git repository. jerrick pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push: new e22ac38 Optimize init loadbalance. (#2309) e22ac38 is described below commit e22ac38158becc11c4543ff9ff0bf930f79217d0 Author: 时无两丶 <442367...@qq.com> AuthorDate: Fri Aug 17 10:02:57 2018 +0800 Optimize init loadbalance. (#2309) * Optimize init loadbalance before 'doSelect' method called --- .../cluster/support/AbstractClusterInvoker.java | 43 +++++++++++++++------- .../support/AbstractClusterInvokerTest.java | 17 ++++++--- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java index 2e3ab14..090f286 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.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.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -94,17 +95,18 @@ public abstract class AbstractClusterInvoker<T> implements Invoker<T> { /** * Select a invoker using loadbalance policy.</br> - * a)Firstly, select an invoker using loadbalance. If this invoker is in previously selected list, or, + * a) Firstly, select an invoker using loadbalance. If this invoker is in previously selected list, or, * if this invoker is unavailable, then continue step b (reselect), otherwise return the first selected invoker</br> - * b)Reslection, the validation rule for reselection: selected > available. This rule guarantees that + * <p> + * b) Reslection, the validation rule for reselection: selected > available. This rule guarantees that * the selected invoker has the minimum chance to be one in the previously selected list, and also * guarantees this invoker is available. * * @param loadbalance load balance policy - * @param invocation - * @param invokers invoker candidates - * @param selected exclude selected invokers or not - * @return + * @param invocation invocation + * @param invokers invoker candidates + * @param selected exclude selected invokers or not + * @return the invoker which will final to do invoke. * @throws RpcException */ protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException { @@ -138,9 +140,6 @@ public abstract class AbstractClusterInvoker<T> implements Invoker<T> { return null; if (invokers.size() == 1) return invokers.get(0); - if (loadbalance == null) { - loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE); - } Invoker<T> invoker = loadbalance.select(invokers, getUrl(), invocation); //If the `invoker` is in the `selected` or invoker is unavailable && availablecheck is true, reselect. @@ -226,7 +225,6 @@ public abstract class AbstractClusterInvoker<T> implements Invoker<T> { @Override public Result invoke(final Invocation invocation) throws RpcException { checkWhetherDestroyed(); - LoadBalance loadbalance = null; // binding attachments into invocation. Map<String, String> contextAttachments = RpcContext.getContext().getAttachments(); @@ -235,10 +233,7 @@ public abstract class AbstractClusterInvoker<T> implements Invoker<T> { } List<Invoker<T>> invokers = list(invocation); - if (invokers != null && !invokers.isEmpty()) { - loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl() - .getMethodParameter(RpcUtils.getMethodName(invocation), Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE)); - } + LoadBalance loadbalance = initLoadBalance(invokers, invocation); RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation); return doInvoke(invocation, invokers, loadbalance); } @@ -276,4 +271,24 @@ public abstract class AbstractClusterInvoker<T> implements Invoker<T> { List<Invoker<T>> invokers = directory.list(invocation); return invokers; } + + /** + * Init LoadBalance. + * <p> + * if invokers is not empty, init from the first invoke's url and invocation + * if invokes is empty, init a default LoadBalance(RandomLoadBalance) + * </p> + * + * @param invokers invokers + * @param invocation invocation + * @return LoadBalance instance. if not need init, return null. + */ + protected LoadBalance initLoadBalance(List<Invoker<T>> invokers, Invocation invocation) { + if (CollectionUtils.isNotEmpty(invokers)) { + return ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl() + .getMethodParameter(RpcUtils.getMethodName(invocation), Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE)); + } else { + return ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE); + } + } } 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 c249131..247a719 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 @@ -51,7 +51,6 @@ import static org.mockito.Mockito.mock; /** * AbstractClusterInvokerTest - * */ @SuppressWarnings("rawtypes") public class AbstractClusterInvokerTest { @@ -161,14 +160,16 @@ public class AbstractClusterInvokerTest { @Test public void testSelect_Invokersize0() throws Exception { + LoadBalance l = cluster.initLoadBalance(invokers, invocation); + Assert.assertNotNull("cluster.initLoadBalance returns null!", l); { - Invoker invoker = cluster.select(null, null, null, null); + Invoker invoker = cluster.select(l, null, null, null); Assert.assertEquals(null, invoker); } { invokers.clear(); selectedInvokers.clear(); - Invoker invoker = cluster.select(null, null, invokers, null); + Invoker invoker = cluster.select(l, null, invokers, null); Assert.assertEquals(null, invoker); } } @@ -177,7 +178,9 @@ public class AbstractClusterInvokerTest { public void testSelect_Invokersize1() throws Exception { invokers.clear(); invokers.add(invoker1); - Invoker invoker = cluster.select(null, null, invokers, null); + LoadBalance l = cluster.initLoadBalance(invokers, invocation); + Assert.assertNotNull("cluster.initLoadBalance returns null!", l); + Invoker invoker = cluster.select(l, null, invokers, null); Assert.assertEquals(invoker1, invoker); } @@ -186,16 +189,18 @@ public class AbstractClusterInvokerTest { invokers.clear(); invokers.add(invoker2); invokers.add(invoker4); + LoadBalance l = cluster.initLoadBalance(invokers, invocation); + Assert.assertNotNull("cluster.initLoadBalance returns null!", l); { selectedInvokers.clear(); selectedInvokers.add(invoker4); - Invoker invoker = cluster.select(null, invocation, invokers, selectedInvokers); + Invoker invoker = cluster.select(l, invocation, invokers, selectedInvokers); Assert.assertEquals(invoker2, invoker); } { selectedInvokers.clear(); selectedInvokers.add(invoker2); - Invoker invoker = cluster.select(null, invocation, invokers, selectedInvokers); + Invoker invoker = cluster.select(l, invocation, invokers, selectedInvokers); Assert.assertEquals(invoker4, invoker); } }