Skip to content

Commit 2ecf923

Browse files
sunhellyApache9
authored andcommitted
HBASE-25635 CandidateGenerator may miss some region balance actions (#3024)
Signed-off-by: Viraj Jasani <vjasani@apache.org> Signed-off-by: Duo Zhang <zhangduo@apache.org>
1 parent f9d80f1 commit 2ecf923

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ protected BaseLoadBalancer.Cluster.Action getAction(int fromServer, int fromRegi
124124
if (fromServer < 0 || toServer < 0) {
125125
return BaseLoadBalancer.Cluster.NullAction;
126126
}
127-
if (fromRegion > 0 && toRegion > 0) {
127+
if (fromRegion >= 0 && toRegion >= 0) {
128128
return new BaseLoadBalancer.Cluster.SwapRegionsAction(fromServer, fromRegion,
129129
toServer, toRegion);
130-
} else if (fromRegion > 0) {
130+
} else if (fromRegion >= 0) {
131131
return new BaseLoadBalancer.Cluster.MoveRegionAction(fromRegion, fromServer, toServer);
132-
} else if (toRegion > 0) {
132+
} else if (toRegion >= 0) {
133133
return new BaseLoadBalancer.Cluster.MoveRegionAction(toRegion, toServer, fromServer);
134134
} else {
135135
return BaseLoadBalancer.Cluster.NullAction;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ protected void setCandidateGenerators(List<CandidateGenerator> customCandidateGe
261261
this.candidateGenerators = customCandidateGenerators;
262262
}
263263

264+
/**
265+
* Exposed for Testing!
266+
*/
267+
public List<CandidateGenerator> getCandidateGenerators() {
268+
return this.candidateGenerators;
269+
}
270+
264271
@Override
265272
protected void setSlop(Configuration conf) {
266273
this.slop = conf.getFloat("hbase.regions.slop", 0.001F);

hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestStochasticLoadBalancerHeterogeneousCost.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static void beforeAllTests() throws IOException {
7676
RULES_FILE);
7777
BalancerTestBase.loadBalancer = new StochasticLoadBalancer();
7878
BalancerTestBase.loadBalancer.setConf(BalancerTestBase.conf);
79+
BalancerTestBase.loadBalancer.getCandidateGenerators().add(new FairRandomCandidateGenerator());
7980
}
8081

8182
@Test
@@ -279,4 +280,26 @@ private ServerAndLoad createServer(final String host) {
279280
ServerName sn = ServerName.valueOf(host, port, startCode);
280281
return new ServerAndLoad(sn, 0);
281282
}
283+
284+
static class FairRandomCandidateGenerator extends
285+
StochasticLoadBalancer.RandomCandidateGenerator {
286+
287+
@Override
288+
public BaseLoadBalancer.Cluster.Action pickRandomRegions(BaseLoadBalancer.Cluster cluster,
289+
int thisServer, int otherServer) {
290+
if (thisServer < 0 || otherServer < 0) {
291+
return BaseLoadBalancer.Cluster.NullAction;
292+
}
293+
294+
int thisRegion = pickRandomRegion(cluster, thisServer, 0.5);
295+
int otherRegion = pickRandomRegion(cluster, otherServer, 0.5);
296+
297+
return getAction(thisServer, thisRegion, otherServer, otherRegion);
298+
}
299+
300+
@Override
301+
BaseLoadBalancer.Cluster.Action generate(BaseLoadBalancer.Cluster cluster) {
302+
return super.generate(cluster);
303+
}
304+
}
282305
}

0 commit comments

Comments
 (0)