From 8213da50d39ebea00d3d4dfdbcbca51107103157 Mon Sep 17 00:00:00 2001 From: CalvinKirs Date: Sun, 28 Jun 2020 10:20:59 +0800 Subject: [PATCH] [WIP] load balance #3054 (#3057) * Load balancing abstract * code smell * lower weight select --- .../host/assign/AbstractSelector.java | 45 +++++++++++++++++++ .../host/assign/LowerWeightRoundRobin.java | 4 +- .../dispatch/host/assign/RandomSelector.java | 15 +------ .../host/assign/RoundRobinSelector.java | 14 +----- 4 files changed, 51 insertions(+), 27 deletions(-) create mode 100644 dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/AbstractSelector.java diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/AbstractSelector.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/AbstractSelector.java new file mode 100644 index 000000000..8560da957 --- /dev/null +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/AbstractSelector.java @@ -0,0 +1,45 @@ +/* + * 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.dolphinscheduler.server.master.dispatch.host.assign; + +import org.apache.dolphinscheduler.common.utils.CollectionUtils; + +import java.util.Collection; + +/** + * AbstractSelector + */ +public abstract class AbstractSelector implements Selector{ + @Override + public T select(Collection source) { + + if (CollectionUtils.isEmpty(source)) { + throw new IllegalArgumentException("Empty source."); + } + + /** + * if only one , return directly + */ + if (source.size() == 1) { + return (T)source.toArray()[0]; + } + return doSelect(source); + } + + protected abstract T doSelect(Collection source); + +} diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobin.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobin.java index bdf0f412f..843397e20 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobin.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/LowerWeightRoundRobin.java @@ -22,7 +22,7 @@ import java.util.Collection; /** * lower weight round robin */ -public class LowerWeightRoundRobin implements Selector{ +public class LowerWeightRoundRobin extends AbstractSelector{ /** * select @@ -30,7 +30,7 @@ public class LowerWeightRoundRobin implements Selector{ * @return HostWeight */ @Override - public HostWeight select(Collection sources){ + public HostWeight doSelect(Collection sources){ int totalWeight = 0; int lowWeight = 0; HostWeight lowerNode = null; diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/RandomSelector.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/RandomSelector.java index be52fcb1c..e00d6f7a6 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/RandomSelector.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/RandomSelector.java @@ -24,23 +24,12 @@ import java.util.Random; * random selector * @param T */ -public class RandomSelector implements Selector { +public class RandomSelector extends AbstractSelector { private final Random random = new Random(); @Override - public T select(final Collection source) { - - if (source == null || source.size() == 0) { - throw new IllegalArgumentException("Empty source."); - } - - /** - * if only one , return directly - */ - if (source.size() == 1) { - return (T) source.toArray()[0]; - } + public T doSelect(final Collection source) { int size = source.size(); /** diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/RoundRobinSelector.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/RoundRobinSelector.java index 1eb30c8d5..06e469fe6 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/RoundRobinSelector.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/master/dispatch/host/assign/RoundRobinSelector.java @@ -26,22 +26,12 @@ import java.util.concurrent.atomic.AtomicInteger; * @param T */ @Service -public class RoundRobinSelector implements Selector { +public class RoundRobinSelector extends AbstractSelector { private final AtomicInteger index = new AtomicInteger(0); @Override - public T select(Collection source) { - if (source == null || source.size() == 0) { - throw new IllegalArgumentException("Empty source."); - } - - /** - * if only one , return directly - */ - if (source.size() == 1) { - return (T)source.toArray()[0]; - } + public T doSelect(Collection source) { int size = source.size(); /** -- GitLab