Browse Source

[Improvement][Master] LowerWeightRoundRobin doSelect may be null (#12159)

xuhhui 2 years ago
parent
commit
55388be21f

+ 9 - 1
dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobin.java

@@ -18,10 +18,13 @@
 package org.apache.dolphinscheduler.server.master.dispatch.host.assign;
 package org.apache.dolphinscheduler.server.master.dispatch.host.assign;
 
 
 import java.util.Collection;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.Comparator;
 import java.util.List;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
+import org.springframework.util.CollectionUtils;
+
 import com.google.common.collect.Lists;
 import com.google.common.collect.Lists;
 
 
 /**
 /**
@@ -49,11 +52,16 @@ public class LowerWeightRoundRobin extends AbstractSelector<HostWeight> {
                 lowWeight = hostWeight.getCurrentWeight();
                 lowWeight = hostWeight.getCurrentWeight();
             }
             }
         }
         }
-        lowerNode.setCurrentWeight(lowerNode.getCurrentWeight() + totalWeight);
+        if (lowerNode != null) {
+            lowerNode.setCurrentWeight(lowerNode.getCurrentWeight() + totalWeight);
+        }
         return lowerNode;
         return lowerNode;
     }
     }
 
 
     private List<HostWeight> canAssignTaskHost(Collection<HostWeight> sources) {
     private List<HostWeight> canAssignTaskHost(Collection<HostWeight> sources) {
+        if (CollectionUtils.isEmpty(sources)) {
+            return Collections.emptyList();
+        }
         List<HostWeight> zeroWaitingTask = sources.stream().filter(h -> h.getWaitingTaskCount() == 0).collect(Collectors.toList());
         List<HostWeight> zeroWaitingTask = sources.stream().filter(h -> h.getWaitingTaskCount() == 0).collect(Collectors.toList());
         if (!zeroWaitingTask.isEmpty()) {
         if (!zeroWaitingTask.isEmpty()) {
             return zeroWaitingTask;
             return zeroWaitingTask;

+ 15 - 0
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobinTest.java

@@ -72,4 +72,19 @@ public class LowerWeightRoundRobinTest {
         result = roundRobin.select(sources);
         result = roundRobin.select(sources);
         Assert.assertEquals("192.158.2.1", result.getHost().getIp());
         Assert.assertEquals("192.158.2.1", result.getHost().getIp());
     }
     }
+
+    @Test
+    public void testDoSelect() {
+        Collection<HostWeight> sources = new ArrayList<>();
+        LowerWeightRoundRobin roundRobin = new LowerWeightRoundRobin();
+        HostWeight result;
+        result = roundRobin.doSelect(sources);
+        Assert.assertEquals(null, result);
+
+        sources.add(new HostWeight(HostWorker.of("192.158.2.1:11", 100, "default"), 0.06, 0.44, 3.14, 1, System.currentTimeMillis() - 60 * 8 * 1000));
+        sources.add(new HostWeight(HostWorker.of("192.158.2.2:22", 100, "default"), 0.06, 0.56, 3.24, 2, System.currentTimeMillis() - 60 * 5 * 1000));
+        sources.add(new HostWeight(HostWorker.of("192.158.2.3:33", 100, "default"), 0.06, 0.80, 3.15, 1, System.currentTimeMillis() - 60 * 2 * 1000));
+        result = roundRobin.doSelect(sources);
+        Assert.assertEquals("192.158.2.1", result.getHost().getIp());
+    }
 }
 }