Skip to content

Commit fbe3446

Browse files
committed
HBASE-25635 CandidateGenerator may miss some region balance actions
1 parent 830d289 commit fbe3446

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.apache.hadoop.hbase.client.RegionInfo;
2727

2828
import org.apache.yetus.audience.InterfaceAudience;
29-
3029
/**
3130
* Generates a candidate action to be applied to the cluster for cost function search
3231
*/
@@ -124,12 +123,12 @@ protected BaseLoadBalancer.Cluster.Action getAction(int fromServer, int fromRegi
124123
if (fromServer < 0 || toServer < 0) {
125124
return BaseLoadBalancer.Cluster.NullAction;
126125
}
127-
if (fromRegion > 0 && toRegion > 0) {
126+
if (fromRegion >= 0 && toRegion >= 0) {
128127
return new BaseLoadBalancer.Cluster.SwapRegionsAction(fromServer, fromRegion,
129128
toServer, toRegion);
130-
} else if (fromRegion > 0) {
129+
} else if (fromRegion >= 0) {
131130
return new BaseLoadBalancer.Cluster.MoveRegionAction(fromRegion, fromServer, toServer);
132-
} else if (toRegion > 0) {
131+
} else if (toRegion >= 0) {
133132
return new BaseLoadBalancer.Cluster.MoveRegionAction(toRegion, toServer, fromServer);
134133
} else {
135134
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
@@ -263,6 +263,13 @@ protected void setCandidateGenerators(List<CandidateGenerator> customCandidateGe
263263
this.candidateGenerators = customCandidateGenerators;
264264
}
265265

266+
/**
267+
* Exposed for Testing!
268+
*/
269+
public List<CandidateGenerator> getCandidateGenerators() {
270+
return this.candidateGenerators;
271+
}
272+
266273
@Override
267274
protected void setSlop(Configuration conf) {
268275
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)