Skip to content

Commit 8349ca6

Browse files
rmdmattinglyRay Mattingly
andcommitted
HBASE-29071 StochasticLoadBalancer candidate generators should use a Map, rather than ordinal based indexing (#6598) (#6621)
Co-authored-by: Ray Mattingly <rmattingly@hubspot.com> Signed-off-by: Nick Dimiduk <ndimiduk@apache.org>
1 parent bb4d2c2 commit 8349ca6

12 files changed

Lines changed: 145 additions & 74 deletions

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ public synchronized void loadConf(Configuration configuration) {
6767
}
6868

6969
@Override
70-
protected List<CandidateGenerator> createCandidateGenerators() {
71-
List<CandidateGenerator> candidateGenerators = new ArrayList<>(2);
72-
candidateGenerators.add(GeneratorFunctionType.LOAD.ordinal(),
70+
protected Map<Class<? extends CandidateGenerator>, CandidateGenerator>
71+
createCandidateGenerators() {
72+
Map<Class<? extends CandidateGenerator>, CandidateGenerator> candidateGenerators =
73+
new HashMap<>(2);
74+
candidateGenerators.put(CacheAwareSkewnessCandidateGenerator.class,
7375
new CacheAwareSkewnessCandidateGenerator());
74-
candidateGenerators.add(GeneratorFunctionType.CACHE_RATIO.ordinal(),
75-
new CacheAwareCandidateGenerator());
76+
candidateGenerators.put(CacheAwareCandidateGenerator.class, new CacheAwareCandidateGenerator());
7677
return candidateGenerators;
7778
}
7879

@@ -403,8 +404,9 @@ protected void regionMoved(int region, int oldServer, int newServer) {
403404
});
404405
}
405406

406-
public final void updateWeight(double[] weights) {
407-
weights[GeneratorFunctionType.LOAD.ordinal()] += cost();
407+
@Override
408+
public final void updateWeight(Map<Class<? extends CandidateGenerator>, Double> weights) {
409+
weights.merge(LoadCandidateGenerator.class, cost(), Double::sum);
408410
}
409411
}
410412

@@ -472,8 +474,8 @@ private int getServerWithBestCacheRatioForRegion(int region) {
472474
}
473475

474476
@Override
475-
public final void updateWeight(double[] weights) {
476-
weights[GeneratorFunctionType.CACHE_RATIO.ordinal()] += cost();
477+
public void updateWeight(Map<Class<? extends CandidateGenerator>, Double> weights) {
478+
weights.merge(LoadCandidateGenerator.class, cost(), Double::sum);
477479
}
478480
}
479481
}

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.hbase.master.balancer;
1919

20+
import java.util.Map;
2021
import org.apache.yetus.audience.InterfaceAudience;
2122

2223
/**
@@ -85,6 +86,16 @@ protected void regionMoved(int region, int oldServer, int newServer) {
8586

8687
protected abstract double cost();
8788

89+
/**
90+
* Add the cost of this cost function to the weight of the candidate generator that is optimized
91+
* for this cost function. By default it is the RandomCandiateGenerator for a cost function.
92+
* Called once per init or after postAction.
93+
* @param weights the weights for every generator.
94+
*/
95+
public void updateWeight(Map<Class<? extends CandidateGenerator>, Double> weights) {
96+
weights.merge(RandomCandidateGenerator.class, cost(), Double::sum);
97+
}
98+
8899
/**
89100
* Scale the value between 0 and 1.
90101
* @param min Min value
@@ -106,14 +117,4 @@ protected static double scale(double min, double max, double value) {
106117

107118
return Math.max(0d, Math.min(1d, (value - min) / (max - min)));
108119
}
109-
110-
/**
111-
* Add the cost of this cost function to the weight of the candidate generator that is optimized
112-
* for this cost function. By default it is the RandomCandiateGenerator for a cost function.
113-
* Called once per init or after postAction.
114-
* @param weights the weights for every generator.
115-
*/
116-
public void updateWeight(double[] weights) {
117-
weights[StochasticLoadBalancer.GeneratorType.RANDOM.ordinal()] += cost();
118-
}
119120
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,25 @@ public class FavoredStochasticBalancer extends StochasticLoadBalancer
7575
private static final Logger LOG = LoggerFactory.getLogger(FavoredStochasticBalancer.class);
7676
private FavoredNodesManager fnm;
7777

78+
public void setFavoredNodesManager(FavoredNodesManager fnm) {
79+
this.fnm = fnm;
80+
}
81+
7882
@Override
79-
protected List<CandidateGenerator> createCandidateGenerators() {
80-
List<CandidateGenerator> fnPickers = new ArrayList<>(2);
81-
fnPickers.add(new FavoredNodeLoadPicker());
82-
fnPickers.add(new FavoredNodeLocalityPicker());
83+
protected Map<Class<? extends CandidateGenerator>, CandidateGenerator>
84+
createCandidateGenerators() {
85+
Map<Class<? extends CandidateGenerator>, CandidateGenerator> fnPickers = new HashMap<>(2);
86+
fnPickers.put(FavoredNodeLoadPicker.class, new FavoredNodeLoadPicker());
87+
fnPickers.put(FavoredNodeLocalityPicker.class, new FavoredNodeLocalityPicker());
8388
return fnPickers;
8489
}
8590

8691
/** Returns any candidate generator in random */
8792
@Override
8893
protected CandidateGenerator getRandomGenerator() {
89-
return candidateGenerators.get(ThreadLocalRandom.current().nextInt(candidateGenerators.size()));
94+
Class<? extends CandidateGenerator> clazz = shuffledGeneratorClasses.get()
95+
.get(ThreadLocalRandom.current().nextInt(candidateGenerators.size()));
96+
return candidateGenerators.get(clazz);
9097
}
9198

9299
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.hbase.master.balancer;
1919

20+
import java.util.Map;
2021
import org.apache.hadoop.conf.Configuration;
2122
import org.apache.hadoop.hbase.master.balancer.BalancerClusterState.LocalityType;
2223
import org.apache.yetus.audience.InterfaceAudience;
@@ -89,7 +90,7 @@ private double getWeightedLocality(int region, int entity) {
8990
}
9091

9192
@Override
92-
public final void updateWeight(double[] weights) {
93-
weights[StochasticLoadBalancer.GeneratorType.LOCALITY.ordinal()] += cost();
93+
public final void updateWeight(Map<Class<? extends CandidateGenerator>, Double> weights) {
94+
weights.merge(LocalityBasedCandidateGenerator.class, cost(), Double::sum);
9495
}
9596
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.hbase.master.balancer;
1919

20+
import java.util.Map;
2021
import org.apache.hadoop.conf.Configuration;
2122
import org.apache.yetus.audience.InterfaceAudience;
2223

@@ -62,7 +63,7 @@ protected void regionMoved(int region, int oldServer, int newServer) {
6263
}
6364

6465
@Override
65-
public final void updateWeight(double[] weights) {
66-
weights[StochasticLoadBalancer.GeneratorType.LOAD.ordinal()] += cost();
66+
public final void updateWeight(Map<Class<? extends CandidateGenerator>, Double> weights) {
67+
weights.merge(LoadCandidateGenerator.class, cost(), Double::sum);
6768
}
6869
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.hbase.master.balancer;
1919

20+
import java.util.Map;
2021
import java.util.concurrent.atomic.AtomicLong;
2122
import org.agrona.collections.Hashing;
2223
import org.agrona.collections.Int2IntCounterMap;
@@ -72,6 +73,11 @@ protected double cost() {
7273
return scale(0, maxCost, totalCost);
7374
}
7475

76+
@Override
77+
public final void updateWeight(Map<Class<? extends CandidateGenerator>, Double> weights) {
78+
weights.merge(RegionReplicaRackCandidateGenerator.class, cost(), Double::sum);
79+
}
80+
7581
/**
7682
* For each primary region, it computes the total number of replicas in the array (numReplicas)
7783
* and returns a sum of numReplicas-1 squared. For example, if the server hosts regions a, b, c,
@@ -91,9 +97,4 @@ protected final long costPerGroup(Int2IntCounterMap colocatedReplicaCounts) {
9197
});
9298
return cost.longValue();
9399
}
94-
95-
@Override
96-
public final void updateWeight(double[] weights) {
97-
weights[StochasticLoadBalancer.GeneratorType.RACK.ordinal()] += cost();
98-
}
99100
}

0 commit comments

Comments
 (0)