You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by il...@apache.org on 2019/09/03 05:43:46 UTC

[dubbo] branch master updated: Simplify some code where feasible (#4980)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0250062  Simplify some code where feasible (#4980)
0250062 is described below

commit 0250062d5cefc3603a570f5f610e456737d3c221
Author: OLPMO <OL...@users.noreply.github.com>
AuthorDate: Tue Sep 3 13:43:06 2019 +0800

    Simplify some code where feasible (#4980)
    
    * Simplify some code where feasible
    
    * Remove unused import
---
 .../cluster/configurator/parser/ConfigParser.java  |  4 +-
 .../cluster/loadbalance/RoundRobinLoadBalance.java | 13 +-----
 .../rpc/cluster/support/ForkingClusterInvoker.java | 19 ++++-----
 .../loadbalance/RoundRobinLoadBalanceTest.java     | 31 +++++++--------
 .../support/FailoverClusterInvokerTest.java        | 18 ++++-----
 .../support/MergeableClusterInvokerTest.java       | 46 ++++++++++------------
 6 files changed, 53 insertions(+), 78 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParser.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParser.java
index 349c9f6..6f0b884 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParser.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParser.java
@@ -84,9 +84,7 @@ public class ConfigParser {
 
             List<String> apps = item.getApplications();
             if (apps != null && apps.size() > 0) {
-                apps.forEach(app -> {
-                    urls.add(URL.valueOf(urlBuilder.append("&application=").append(app).toString()));
-                });
+                apps.forEach(app -> urls.add(URL.valueOf(urlBuilder.append("&application=").append(app).toString())));
             } else {
                 urls.add(URL.valueOf(urlBuilder.toString()));
             }
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java
index ee42714..001c06d 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java
@@ -21,10 +21,8 @@ import org.apache.dubbo.rpc.Invocation;
 import org.apache.dubbo.rpc.Invoker;
 
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -124,15 +122,8 @@ public class RoundRobinLoadBalance extends AbstractLoadBalance {
             if (updateLock.compareAndSet(false, true)) {
                 try {
                     // copy -> modify -> update reference
-                    ConcurrentMap<String, WeightedRoundRobin> newMap = new ConcurrentHashMap<String, WeightedRoundRobin>();
-                    newMap.putAll(map);
-                    Iterator<Entry<String, WeightedRoundRobin>> it = newMap.entrySet().iterator();
-                    while (it.hasNext()) {
-                        Entry<String, WeightedRoundRobin> item = it.next();
-                        if (now - item.getValue().getLastUpdate() > RECYCLE_PERIOD) {
-                            it.remove();
-                        }
-                    }
+                    ConcurrentMap<String, WeightedRoundRobin> newMap = new ConcurrentHashMap<>(map);
+                    newMap.entrySet().removeIf(item -> now - item.getValue().getLastUpdate() > RECYCLE_PERIOD);
                     methodWeightMap.put(key, newMap);
                 } finally {
                     updateLock.set(false);
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java
index 2c352b2..d23115b 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java
@@ -83,17 +83,14 @@ public class ForkingClusterInvoker<T> extends AbstractClusterInvoker<T> {
             final AtomicInteger count = new AtomicInteger();
             final BlockingQueue<Object> ref = new LinkedBlockingQueue<>();
             for (final Invoker<T> invoker : selected) {
-                executor.execute(new Runnable() {
-                    @Override
-                    public void run() {
-                        try {
-                            Result result = invoker.invoke(invocation);
-                            ref.offer(result);
-                        } catch (Throwable e) {
-                            int value = count.incrementAndGet();
-                            if (value >= selected.size()) {
-                                ref.offer(e);
-                            }
+                executor.execute(() -> {
+                    try {
+                        Result result = invoker.invoke(invocation);
+                        ref.offer(result);
+                    } catch (Throwable e) {
+                        int value = count.incrementAndGet();
+                        if (value >= selected.size()) {
+                            ref.offer(e);
                         }
                     }
                 });
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java
index 86d4130..8d77aa4 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java
@@ -64,27 +64,24 @@ public class RoundRobinLoadBalanceTest extends LoadBalanceBaseTest {
         List<Thread> threads = new ArrayList<Thread>();
         int threadNum = 10;
         for (int i = 0; i < threadNum; i++) {
-            threads.add(new Thread() {
-                @Override
-                public void run() {
-                    while (!shouldBegin.get()) {
-                        try {
-                            sleep(5);
-                        } catch (InterruptedException e) {
-                        }
+            threads.add(new Thread(() -> {
+                while (!shouldBegin.get()) {
+                    try {
+                        Thread.sleep(5);
+                    } catch (InterruptedException e) {
                     }
-                    Map<Invoker, InvokeResult> resultMap = getWeightedInvokeResult(runs, RoundRobinLoadBalance.NAME);
-                    synchronized (totalMap) {
-                        for (Entry<Invoker, InvokeResult> entry : resultMap.entrySet()) {
-                            if (!totalMap.containsKey(entry.getKey())) {
-                                totalMap.put(entry.getKey(), entry.getValue());
-                            } else {
-                                totalMap.get(entry.getKey()).getCount().addAndGet(entry.getValue().getCount().get());
-                            }
+                }
+                Map<Invoker, InvokeResult> resultMap = getWeightedInvokeResult(runs, RoundRobinLoadBalance.NAME);
+                synchronized (totalMap) {
+                    for (Entry<Invoker, InvokeResult> entry : resultMap.entrySet()) {
+                        if (!totalMap.containsKey(entry.getKey())) {
+                            totalMap.put(entry.getKey(), entry.getValue());
+                        } else {
+                            totalMap.get(entry.getKey()).getCount().addAndGet(entry.getValue().getCount().get());
                         }
                     }
                 }
-            });
+            }));
         }
         for (Thread thread : threads) {
             thread.start();
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java
index dab60d2..4c17fba 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java
@@ -179,17 +179,15 @@ public class FailoverClusterInvokerTest {
         invokers.add(invoker1);
         invokers.add(invoker2);
 
-        Callable<Object> callable = new Callable<Object>() {
-            public Object call() throws Exception {
-                //Simulation: all invokers are destroyed
-                for (Invoker<Demo> invoker : invokers) {
-                    invoker.destroy();
-                }
-                invokers.clear();
-                MockInvoker<Demo> invoker3 = new MockInvoker<Demo>(Demo.class, url);
-                invokers.add(invoker3);
-                return null;
+        Callable<Object> callable = () -> {
+            //Simulation: all invokers are destroyed
+            for (Invoker<Demo> invoker : invokers) {
+                invoker.destroy();
             }
+            invokers.clear();
+            MockInvoker<Demo> invoker3 = new MockInvoker<Demo>(Demo.class, url);
+            invokers.add(invoker3);
+            return null;
         };
         invoker1.setCallable(callable);
         invoker2.setCallable(callable);
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java
index 3223b27..e87aa70 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java
@@ -111,36 +111,30 @@ public class MergeableClusterInvokerTest {
                 ;
         given(invocation.getInvoker()).willReturn(firstInvoker);
 
-        firstInvoker = (Invoker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{Invoker.class}, new InvocationHandler() {
-
-            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
-                if ("getUrl".equals(method.getName())) {
-                    return url.addParameter(GROUP_KEY, "first");
-                }
-                if ("getInterface".equals(method.getName())) {
-                    return MenuService.class;
-                }
-                if ("invoke".equals(method.getName())) {
-                    return AsyncRpcResult.newDefaultAsyncResult(firstMenu, invocation);
-                }
-                return null;
+        firstInvoker = (Invoker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{Invoker.class}, (proxy, method, args) -> {
+            if ("getUrl".equals(method.getName())) {
+                return url.addParameter(GROUP_KEY, "first");
             }
+            if ("getInterface".equals(method.getName())) {
+                return MenuService.class;
+            }
+            if ("invoke".equals(method.getName())) {
+                return AsyncRpcResult.newDefaultAsyncResult(firstMenu, invocation);
+            }
+            return null;
         });
 
-        secondInvoker = (Invoker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{Invoker.class}, new InvocationHandler() {
-
-            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
-                if ("getUrl".equals(method.getName())) {
-                    return url.addParameter(GROUP_KEY, "second");
-                }
-                if ("getInterface".equals(method.getName())) {
-                    return MenuService.class;
-                }
-                if ("invoke".equals(method.getName())) {
-                    return AsyncRpcResult.newDefaultAsyncResult(secondMenu, invocation);
-                }
-                return null;
+        secondInvoker = (Invoker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{Invoker.class}, (proxy, method, args) -> {
+            if ("getUrl".equals(method.getName())) {
+                return url.addParameter(GROUP_KEY, "second");
+            }
+            if ("getInterface".equals(method.getName())) {
+                return MenuService.class;
+            }
+            if ("invoke".equals(method.getName())) {
+                return AsyncRpcResult.newDefaultAsyncResult(secondMenu, invocation);
             }
+            return null;
         });
 
         given(directory.list(invocation)).willReturn(new ArrayList() {