You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by wc...@apache.org on 2019/08/28 08:57:17 UTC
[hbase] branch branch-1 updated: HBASE-22618 added the possibility
to load custom cost functions
This is an automated email from the ASF dual-hosted git repository.
wchevreuil pushed a commit to branch branch-1
in repository https://gitbox.apache.org/repos/asf/hbase.git
The following commit(s) were added to refs/heads/branch-1 by this push:
new 73e0304 HBASE-22618 added the possibility to load custom cost functions
73e0304 is described below
commit 73e03045673f5525055dca71a2e87dfa050a7e85
Author: Pierre Zemb <co...@pierrezemb.fr>
AuthorDate: Sat Aug 24 16:16:56 2019 +0200
HBASE-22618 added the possibility to load custom cost functions
Signed-off-by: Wellington Chevreuil <wc...@apache.org>
---
.../master/balancer/StochasticLoadBalancer.java | 109 ++++++++++++++-------
.../hbase/master/balancer/DummyCostFunction.java | 31 ++++++
.../balancer/TestStochasticLoadBalancer.java | 12 +++
3 files changed, 118 insertions(+), 34 deletions(-)
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 065aa3e..9a6c992 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
@@ -52,6 +52,7 @@ import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster.Locality
import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster.MoveRegionAction;
import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster.SwapRegionsAction;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
+import org.apache.hadoop.hbase.util.ReflectionUtils;
import com.google.common.base.Optional;
@@ -81,6 +82,13 @@ import com.google.common.base.Optional;
* <li>hbase.master.balancer.stochastic.storefileSizeCost</li>
* </ul>
*
+ * <p>You can also add custom Cost function by setting the the following configuration value:</p>
+ * <ul>
+ * <li>hbase.master.balancer.stochastic.additionalCostFunctions</li>
+ * </ul>
+ *
+ * <p>All custom Cost Functions needs to extends {@link StochasticLoadBalancer.CostFunction}</p>
+ *
* <p>In addition to the above configurations, the balancer can be tuned by the following
* configuration values:</p>
* <ul>
@@ -116,6 +124,8 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
private static final String TABLE_FUNCTION_SEP = "_";
protected static final String MIN_COST_NEED_BALANCE_KEY =
"hbase.master.balancer.stochastic.minCostNeedBalance";
+ protected static final String COST_FUNCTIONS_COST_FUNCTIONS_KEY =
+ "hbase.master.balancer.stochastic.additionalCostFunctions";
private static final Random RANDOM = new Random(System.currentTimeMillis());
private static final Log LOG = LogFactory.getLog(StochasticLoadBalancer.class);
@@ -132,7 +142,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
private CandidateGenerator[] candidateGenerators;
private CostFromRegionLoadFunction[] regionLoadFunctions;
- private CostFunction[] costFunctions; // FindBugs: Wants this protected; IS2_INCONSISTENT_SYNC
+ private List<CostFunction> costFunctions; // FindBugs: Wants this protected; IS2_INCONSISTENT_SYNC
// to save and report costs to JMX
private Double curOverallCost = 0d;
@@ -203,24 +213,55 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
regionReplicaHostCostFunction = new RegionReplicaHostCostFunction(conf);
regionReplicaRackCostFunction = new RegionReplicaRackCostFunction(conf);
- costFunctions = new CostFunction[]{
- new RegionCountSkewCostFunction(conf),
- new PrimaryRegionCountSkewCostFunction(conf),
- new MoveCostFunction(conf),
- localityCost,
- rackLocalityCost,
- new TableSkewCostFunction(conf),
- regionReplicaHostCostFunction,
- regionReplicaRackCostFunction,
- regionLoadFunctions[0],
- regionLoadFunctions[1],
- regionLoadFunctions[2],
- regionLoadFunctions[3],
- };
+ costFunctions = new ArrayList<>();
+ costFunctions.add(new RegionCountSkewCostFunction(conf));
+ costFunctions.add(new PrimaryRegionCountSkewCostFunction(conf));
+ costFunctions.add(new MoveCostFunction(conf));
+ costFunctions.add(localityCost);
+ costFunctions.add(rackLocalityCost);
+ costFunctions.add(new TableSkewCostFunction(conf));
+ costFunctions.add(regionReplicaHostCostFunction);
+ costFunctions.add(regionReplicaRackCostFunction);
+ costFunctions.add(regionLoadFunctions[0]);
+ costFunctions.add(regionLoadFunctions[1]);
+ costFunctions.add(regionLoadFunctions[2]);
+ costFunctions.add(regionLoadFunctions[3]);
+ loadCustomCostFunctions(conf);
+
+ curFunctionCosts = new Double[costFunctions.size()];
+ tempFunctionCosts = new Double[costFunctions.size()];
+
+ LOG.info("Loaded config; maxSteps=" + maxSteps + ", stepsPerRegion=" + stepsPerRegion +
+ ", maxRunningTime=" + maxRunningTime + ", isByTable=" + isByTable + ", etc." +
+ ", maxRunningTime=" + maxRunningTime + ", isByTable=" + isByTable + ", CostFunctions=" +
+ Arrays.toString(getCostFunctionNames()) + " etc.");
+ }
+
+ private void loadCustomCostFunctions(Configuration conf) {
+ String[] functionsNames = conf.getStrings(COST_FUNCTIONS_COST_FUNCTIONS_KEY);
- curFunctionCosts= new Double[costFunctions.length];
- tempFunctionCosts= new Double[costFunctions.length];
+ if (null == functionsNames) {
+ return;
+ }
+
+ for(String functionName: functionsNames) {
+
+ Class<? extends CostFunction> klass = null;
+ try {
+ klass = (Class<? extends CostFunction>) Class.forName(functionName);
+ if (klass == null) {
+ continue;
+ }
+ CostFunction reflected = ReflectionUtils.newInstance(klass, conf);
+ LOG.info("Successfully loaded custom CostFunction '" +
+ reflected.getClass().getSimpleName() + "'");
+
+ this.costFunctions.add(reflected);
+ } catch (ClassNotFoundException e) {
+ LOG.warn("Cannot load class " + functionName + "': " + e.getMessage());
+ }
+ }
}
@Override
@@ -475,8 +516,8 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
"Overall", "Overall cost", overall);
// each cost function
- for (int i = 0; i < costFunctions.length; i++) {
- CostFunction costFunction = costFunctions[i];
+ for (int i = 0; i < costFunctions.size(); i++) {
+ CostFunction costFunction = costFunctions.get(i);
String costFunctionName = costFunction.getClass().getSimpleName();
Double costPercent = (overall == 0) ? 0 : (subCosts[i] / overall);
// TODO: cost function may need a specific description
@@ -579,9 +620,9 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
*/
public String[] getCostFunctionNames() {
if (costFunctions == null) return null;
- String[] ret = new String[costFunctions.length];
- for (int i = 0; i < costFunctions.length; i++) {
- CostFunction c = costFunctions[i];
+ String[] ret = new String[costFunctions.size()];
+ for (int i = 0; i < costFunctions.size(); i++) {
+ CostFunction c = costFunctions.get(i);
ret[i] = c.getClass().getSimpleName();
}
@@ -600,8 +641,8 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
protected double computeCost(Cluster cluster, double previousCost) {
double total = 0;
- for (int i = 0; i < costFunctions.length; i++) {
- CostFunction c = costFunctions[i];
+ for (int i = 0; i < costFunctions.size(); i++) {
+ CostFunction c = costFunctions.get(i);
this.tempFunctionCosts[i] = 0.0;
if (c.getMultiplier() <= 0) {
@@ -984,13 +1025,13 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
/**
* Base class of StochasticLoadBalancer's Cost Functions.
*/
- abstract static class CostFunction {
+ public abstract static class CostFunction {
private float multiplier = 0;
protected Cluster cluster;
- CostFunction(Configuration c) {
+ public CostFunction(Configuration c) {
}
boolean isNeeded() {
@@ -1039,7 +1080,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
protected void regionMoved(int region, int oldServer, int newServer) {
}
- abstract double cost();
+ protected abstract double cost();
/**
* Function to compute a scaled cost using {@link DescriptiveStatistics}. It
@@ -1134,7 +1175,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
}
@Override
- double cost() {
+ protected double cost() {
// Try and size the max number of Moves, but always be prepared to move some.
int maxMoves = Math.max((int) (cluster.numRegions * maxMovesPercent),
DEFAULT_MAX_MOVES);
@@ -1169,7 +1210,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
}
@Override
- double cost() {
+ protected double cost() {
if (stats == null || stats.length != cluster.numServers) {
stats = new double[cluster.numServers];
}
@@ -1201,7 +1242,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
}
@Override
- double cost() {
+ protected double cost() {
if (!cluster.hasRegionReplicas) {
return 0;
}
@@ -1238,7 +1279,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
}
@Override
- double cost() {
+ protected double cost() {
double max = cluster.numRegions;
double min = ((double) cluster.numRegions) / cluster.numServers;
double value = 0;
@@ -1321,7 +1362,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
}
@Override
- double cost() {
+ protected double cost() {
return 1 - locality;
}
@@ -1399,7 +1440,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
}
@Override
- double cost() {
+ protected double cost() {
if (clusterStatus == null || loads == null) {
return 0;
}
@@ -1572,7 +1613,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
}
@Override
- double cost() {
+ protected double cost() {
if (maxCost <= 0) {
return 0;
}
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/DummyCostFunction.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/DummyCostFunction.java
new file mode 100644
index 0000000..680ffed
--- /dev/null
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/DummyCostFunction.java
@@ -0,0 +1,31 @@
+/**
+ * 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.hadoop.hbase.master.balancer;
+
+import org.apache.hadoop.conf.Configuration;
+
+public class DummyCostFunction extends StochasticLoadBalancer.CostFunction {
+ public DummyCostFunction(Configuration c) {
+ super(c);
+ }
+
+ @Override
+ protected double cost() {
+ return 0;
+ }
+}
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 40bec19..31d327e 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
@@ -669,6 +669,18 @@ public class TestStochasticLoadBalancer extends BalancerTestBase {
testWithCluster(serverMap, rm, false, true);
}
+ @Test
+ public void testAdditionalCostFunction() {
+ conf.set(StochasticLoadBalancer.COST_FUNCTIONS_COST_FUNCTIONS_KEY,
+ DummyCostFunction.class.getName());
+
+ loadBalancer.setConf(conf);
+ System.out.println(Arrays.toString(loadBalancer.getCostFunctionNames()));
+ assertTrue(Arrays.
+ asList(loadBalancer.getCostFunctionNames()).
+ contains(DummyCostFunction.class.getSimpleName()));
+ }
+
// This mock allows us to test the LocalityCostFunction
private class MockCluster extends BaseLoadBalancer.Cluster {