diff --git a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobin.java b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobin.java index f099d81473..3388129b2a 100644 --- a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobin.java +++ b/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; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; +import org.springframework.util.CollectionUtils; + import com.google.common.collect.Lists; /** @@ -49,11 +52,16 @@ public class LowerWeightRoundRobin extends AbstractSelector { lowWeight = hostWeight.getCurrentWeight(); } } - lowerNode.setCurrentWeight(lowerNode.getCurrentWeight() + totalWeight); + if (lowerNode != null) { + lowerNode.setCurrentWeight(lowerNode.getCurrentWeight() + totalWeight); + } return lowerNode; } private List canAssignTaskHost(Collection sources) { + if (CollectionUtils.isEmpty(sources)) { + return Collections.emptyList(); + } List zeroWaitingTask = sources.stream().filter(h -> h.getWaitingTaskCount() == 0).collect(Collectors.toList()); if (!zeroWaitingTask.isEmpty()) { return zeroWaitingTask; diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobinTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobinTest.java index fcb8fbc541..e2ff180ba3 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobinTest.java +++ b/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); Assert.assertEquals("192.158.2.1", result.getHost().getIp()); } + + @Test + public void testDoSelect() { + Collection 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()); + } }