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>
This commit is contained in:
haxiaolin 2021-03-15 21:28:22 +08:00 committed by Duo Zhang
parent f9d80f18cb
commit 2ecf9232bf
3 changed files with 33 additions and 3 deletions

View File

@ -124,12 +124,12 @@ abstract class CandidateGenerator {
if (fromServer < 0 || toServer < 0) {
return BaseLoadBalancer.Cluster.NullAction;
}
if (fromRegion > 0 && toRegion > 0) {
if (fromRegion >= 0 && toRegion >= 0) {
return new BaseLoadBalancer.Cluster.SwapRegionsAction(fromServer, fromRegion,
toServer, toRegion);
} else if (fromRegion > 0) {
} else if (fromRegion >= 0) {
return new BaseLoadBalancer.Cluster.MoveRegionAction(fromRegion, fromServer, toServer);
} else if (toRegion > 0) {
} else if (toRegion >= 0) {
return new BaseLoadBalancer.Cluster.MoveRegionAction(toRegion, toServer, fromServer);
} else {
return BaseLoadBalancer.Cluster.NullAction;

View File

@ -261,6 +261,13 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
this.candidateGenerators = customCandidateGenerators;
}
/**
* Exposed for Testing!
*/
public List<CandidateGenerator> getCandidateGenerators() {
return this.candidateGenerators;
}
@Override
protected void setSlop(Configuration conf) {
this.slop = conf.getFloat("hbase.regions.slop", 0.001F);

View File

@ -76,6 +76,7 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends BalancerTestBas
RULES_FILE);
BalancerTestBase.loadBalancer = new StochasticLoadBalancer();
BalancerTestBase.loadBalancer.setConf(BalancerTestBase.conf);
BalancerTestBase.loadBalancer.getCandidateGenerators().add(new FairRandomCandidateGenerator());
}
@Test
@ -279,4 +280,26 @@ public class TestStochasticLoadBalancerHeterogeneousCost extends BalancerTestBas
ServerName sn = ServerName.valueOf(host, port, startCode);
return new ServerAndLoad(sn, 0);
}
static class FairRandomCandidateGenerator extends
StochasticLoadBalancer.RandomCandidateGenerator {
@Override
public BaseLoadBalancer.Cluster.Action pickRandomRegions(BaseLoadBalancer.Cluster cluster,
int thisServer, int otherServer) {
if (thisServer < 0 || otherServer < 0) {
return BaseLoadBalancer.Cluster.NullAction;
}
int thisRegion = pickRandomRegion(cluster, thisServer, 0.5);
int otherRegion = pickRandomRegion(cluster, otherServer, 0.5);
return getAction(thisServer, thisRegion, otherServer, otherRegion);
}
@Override
BaseLoadBalancer.Cluster.Action generate(BaseLoadBalancer.Cluster cluster) {
return super.generate(cluster);
}
}
}