From 60465964039acd05f43f268cdb4f909a150a0f41 Mon Sep 17 00:00:00 2001 From: tedyu Date: Tue, 20 Oct 2015 02:35:40 -0700 Subject: [PATCH] HBASE-14604 Improve MoveCostFunction in StochasticLoadBalancer (Guanghao Zhang) --- .../master/balancer/BaseLoadBalancer.java | 11 ++++++ .../balancer/StochasticLoadBalancer.java | 2 +- .../balancer/TestStochasticLoadBalancer.java | 38 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.java index 962b241663f..33e8d9773bd 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.java @@ -54,6 +54,7 @@ import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster.Action.T import org.apache.hadoop.hbase.security.access.AccessControlLists; import org.apache.hadoop.util.StringUtils; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Lists; @@ -898,6 +899,16 @@ public abstract class BaseLoadBalancer implements LoadBalancer { } } + @VisibleForTesting + protected void setNumRegions(int numRegions) { + this.numRegions = numRegions; + } + + @VisibleForTesting + protected void setNumMovedRegions(int numMovedRegions) { + this.numMovedRegions = numMovedRegions; + } + @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="SBSC_USE_STRINGBUFFER_CONCATENATION", justification="Not important but should be fixed") @Override diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java index 249423f3e83..f9b3bafa12b 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java @@ -1026,7 +1026,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer { return 1000000; // return a number much greater than any of the other cost } - return scale(0, cluster.numRegions, moveCost); + return scale(0, Math.min(cluster.numRegions, maxMoves), moveCost); } } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancer.java index 85ecb29074c..7abbeb4d437 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancer.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancer.java @@ -121,6 +121,44 @@ public class TestStochasticLoadBalancer extends BalancerTestBase { } + @Test + public void testMoveCost() throws Exception { + Configuration conf = HBaseConfiguration.create(); + StochasticLoadBalancer.CostFunction + costFunction = new StochasticLoadBalancer.MoveCostFunction(conf); + for (int[] mockCluster : clusterStateMocks) { + BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster); + costFunction.init(cluster); + double cost = costFunction.cost(); + assertEquals(0.0f, cost, 0.001); + + // cluster region number is smaller than maxMoves=600 + cluster.setNumRegions(200); + cluster.setNumMovedRegions(10); + cost = costFunction.cost(); + assertEquals(0.05f, cost, 0.001); + cluster.setNumMovedRegions(100); + cost = costFunction.cost(); + assertEquals(0.5f, cost, 0.001); + cluster.setNumMovedRegions(200); + cost = costFunction.cost(); + assertEquals(1.0f, cost, 0.001); + + + // cluster region number is bigger than maxMoves=2500 + cluster.setNumRegions(10000); + cluster.setNumMovedRegions(250); + cost = costFunction.cost(); + assertEquals(0.1f, cost, 0.001); + cluster.setNumMovedRegions(1250); + cost = costFunction.cost(); + assertEquals(0.5f, cost, 0.001); + cluster.setNumMovedRegions(2500); + cost = costFunction.cost(); + assertEquals(1.0f, cost, 0.01); + } + } + @Test public void testSkewCost() { Configuration conf = HBaseConfiguration.create();