Skip to content

Commit d4c162f

Browse files
committed
HBASE-26337 Optimization for weighted random generators
1 parent f6e2ed9 commit d4c162f

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/CostFromRegionLoadFunction.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ protected final double cost() {
7070
return cost.cost();
7171
}
7272

73+
@Override
74+
protected final void updateWeight(double[] weights) {
75+
weights[StochasticLoadBalancer.GeneratorType.RANDOM.ordinal()] += cost.cost();
76+
}
77+
7378
protected double getRegionLoadCost(Collection<BalancerRegionLoad> regionLoadList) {
7479
double cost = 0;
7580
for (BalancerRegionLoad rl : regionLoadList) {
@@ -79,4 +84,4 @@ protected double getRegionLoadCost(Collection<BalancerRegionLoad> regionLoadList
7984
}
8085

8186
protected abstract double getCostFromRl(BalancerRegionLoad rl);
82-
}
87+
}

hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/CostFunction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ protected void regionMoved(int region, int oldServer, int newServer) {
8383

8484
protected abstract double cost();
8585

86+
protected abstract void updateWeight(double[] weights);
87+
8688
/**
8789
* Scale the value between 0 and 1.
8890
* @param min Min value

hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/LocalityBasedCostFunction.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,8 @@ private double getWeightedLocality(int region, int entity) {
8888
return cluster.getOrComputeWeightedLocality(region, entity, type);
8989
}
9090

91-
}
91+
@Override
92+
protected final void updateWeight(double[] weights) {
93+
weights[StochasticLoadBalancer.GeneratorType.LOCALITY.ordinal()] += cost();
94+
}
95+
}

hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/RegionCountSkewCostFunction.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ protected void regionMoved(int region, int oldServer, int newServer) {
7272
costs[newServer] = cluster.regionsPerServer[newServer].length;
7373
});
7474
}
75-
}
75+
76+
@Override
77+
protected final void updateWeight(double[] weights) {
78+
weights[StochasticLoadBalancer.GeneratorType.LOAD.ordinal()] += cost.cost();
79+
}
80+
}

hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/RegionReplicaGroupingCostFunction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ protected double cost() {
7474
return scale(0, maxCost, totalCost);
7575
}
7676

77+
@Override
78+
protected final void updateWeight(double[] weights) {
79+
weights[StochasticLoadBalancer.GeneratorType.RACK.ordinal()] += cost();
80+
}
81+
7782
/**
7883
* For each primary region, it computes the total number of replicas in the array (numReplicas)
7984
* and returns a sum of numReplicas-1 squared. For example, if the server hosts regions a, b, c,

hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
137137
private double curOverallCost = 0d;
138138
private double[] tempFunctionCosts;
139139
private double[] curFunctionCosts;
140+
private double[] weightOfGenerator;
140141

141142
// Keep locality based picker and cost function to alert them
142143
// when new services are offered
@@ -146,6 +147,10 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
146147
private RegionReplicaHostCostFunction regionReplicaHostCostFunction;
147148
private RegionReplicaRackCostFunction regionReplicaRackCostFunction;
148149

150+
public enum GeneratorType {
151+
RANDOM, LOAD, LOCALITY, RACK,
152+
}
153+
149154
/**
150155
* The constructor that pass a MetricsStochasticBalancer to BaseLoadBalancer to replace its
151156
* default MetricsBalancer
@@ -204,10 +209,11 @@ protected float getDefaultSlop() {
204209

205210
protected List<CandidateGenerator> createCandidateGenerators() {
206211
List<CandidateGenerator> candidateGenerators = new ArrayList<CandidateGenerator>(4);
207-
candidateGenerators.add(new RandomCandidateGenerator());
208-
candidateGenerators.add(new LoadCandidateGenerator());
209-
candidateGenerators.add(localityCandidateGenerator);
210-
candidateGenerators.add(new RegionReplicaRackCandidateGenerator());
212+
candidateGenerators.add(GeneratorType.RANDOM.ordinal(), new RandomCandidateGenerator());
213+
candidateGenerators.add(GeneratorType.LOAD.ordinal(), new LoadCandidateGenerator());
214+
candidateGenerators.add(GeneratorType.LOCALITY.ordinal(), localityCandidateGenerator);
215+
candidateGenerators.add(GeneratorType.RACK.ordinal(),
216+
new RegionReplicaRackCandidateGenerator());
211217
return candidateGenerators;
212218
}
213219

@@ -393,6 +399,27 @@ BalanceAction nextAction(BalancerClusterState cluster) {
393399
.generate(cluster);
394400
}
395401

402+
private CandidateGenerator getRandomGenerate() {
403+
double sum = 0;
404+
for (int i = 0; i < weightOfGenerator.length; i++) {
405+
sum += weightOfGenerator[i];
406+
weightOfGenerator[i] = sum;
407+
}
408+
if (sum == 0) {
409+
return candidateGenerators.get(0);
410+
}
411+
for (int i = 0; i < weightOfGenerator.length; i++) {
412+
weightOfGenerator[i] /= sum;
413+
}
414+
double rand = ThreadLocalRandom.current().nextDouble();
415+
for (int i = 0; i < weightOfGenerator.length; i++) {
416+
if (rand <= weightOfGenerator[i]) {
417+
return candidateGenerators.get(i);
418+
}
419+
}
420+
return candidateGenerators.get(candidateGenerators.size() - 1);
421+
}
422+
396423
@RestrictedApi(explanation = "Should only be called in tests", link = "",
397424
allowedOnPath = ".*/src/test/.*")
398425
void setRackManager(RackManager rackManager) {
@@ -476,6 +503,8 @@ protected List<RegionPlan> balanceTable(TableName tableName, Map<ServerName,
476503
long step;
477504

478505
for (step = 0; step < computedMaxSteps; step++) {
506+
// Initialize the weights of generator every time
507+
weightOfGenerator = new double[this.candidateGenerators.size()];
479508
BalanceAction action = nextAction(cluster);
480509

481510
if (action.getType() == BalanceAction.Type.NULL) {

0 commit comments

Comments
 (0)