Skip to content

Commit d8b9791

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

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

hbase-common/src/main/java/org/apache/hadoop/hbase/HConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ public enum OperationStatusCode {
144144
public static final String HBASE_MASTER_BALANCER_MAX_RIT_PERCENT =
145145
"hbase.master.balancer.maxRitPercent";
146146

147+
public static final String HBASE_MASTER_BALANCER_RANDOM_PICK_REGIONS_WITH_CONSIDER_COUNT_BALANCE =
148+
"hbase.master.balancer.random.pick.regions.with.consider.count.balance";
149+
147150
/** Default value for the max percent of regions in transition */
148151
public static final double DEFAULT_HBASE_MASTER_BALANCER_MAX_RIT_PERCENT = 1.0;
149152

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import org.apache.hadoop.hbase.BaseConfigurable;
2627
import org.apache.hadoop.hbase.client.RegionInfo;
2728

2829
import org.apache.yetus.audience.InterfaceAudience;
30+
import static org.apache.hadoop.hbase.HConstants.HBASE_MASTER_BALANCER_RANDOM_PICK_REGIONS_WITH_CONSIDER_COUNT_BALANCE;
2931

3032
/**
3133
* Generates a candidate action to be applied to the cluster for cost function search
3234
*/
3335
@InterfaceAudience.Private
34-
abstract class CandidateGenerator {
36+
abstract class CandidateGenerator extends BaseConfigurable {
3537

3638
abstract BaseLoadBalancer.Cluster.Action generate(BaseLoadBalancer.Cluster cluster);
3739

@@ -104,14 +106,19 @@ BaseLoadBalancer.Cluster.Action pickRandomRegions(BaseLoadBalancer.Cluster clust
104106
if (thisServer < 0 || otherServer < 0) {
105107
return BaseLoadBalancer.Cluster.NullAction;
106108
}
107-
108-
// Decide who is most likely to need another region
109-
int thisRegionCount = cluster.getNumRegions(thisServer);
110-
int otherRegionCount = cluster.getNumRegions(otherServer);
111-
112-
// Assign the chance based upon the above
113-
double thisChance = (thisRegionCount > otherRegionCount) ? 0 : 0.5;
114-
double otherChance = (thisRegionCount <= otherRegionCount) ? 0 : 0.5;
109+
double thisChance = 0.5;
110+
double otherChance = 0.5;
111+
if (getConf() == null || getConf().getBoolean(
112+
HBASE_MASTER_BALANCER_RANDOM_PICK_REGIONS_WITH_CONSIDER_COUNT_BALANCE, true)) {
113+
114+
// Decide who is most likely to need another region
115+
int thisRegionCount = cluster.getNumRegions(thisServer);
116+
int otherRegionCount = cluster.getNumRegions(otherServer);
117+
118+
// Assign the chance based upon the above
119+
thisChance = (thisRegionCount > otherRegionCount) ? 0 : 0.5;
120+
otherChance = (thisRegionCount <= otherRegionCount) ? 0 : 0.5;
121+
}
115122

116123
int thisRegion = pickRandomRegion(cluster, thisServer, thisChance);
117124
int otherRegion = pickRandomRegion(cluster, otherServer, otherChance);
@@ -124,12 +131,12 @@ protected BaseLoadBalancer.Cluster.Action getAction(int fromServer, int fromRegi
124131
if (fromServer < 0 || toServer < 0) {
125132
return BaseLoadBalancer.Cluster.NullAction;
126133
}
127-
if (fromRegion > 0 && toRegion > 0) {
134+
if (fromRegion >= 0 && toRegion >= 0 && fromRegion != toRegion) {
128135
return new BaseLoadBalancer.Cluster.SwapRegionsAction(fromServer, fromRegion,
129136
toServer, toRegion);
130-
} else if (fromRegion > 0) {
137+
} else if (fromRegion >= 0) {
131138
return new BaseLoadBalancer.Cluster.MoveRegionAction(fromRegion, fromServer, toServer);
132-
} else if (toRegion > 0) {
139+
} else if (toRegion >= 0) {
133140
return new BaseLoadBalancer.Cluster.MoveRegionAction(toRegion, toServer, fromServer);
134141
} else {
135142
return BaseLoadBalancer.Cluster.NullAction;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ public synchronized void setConf(Configuration conf) {
189189

190190
if (this.candidateGenerators == null) {
191191
candidateGenerators = Lists.newArrayList();
192-
candidateGenerators.add(new RandomCandidateGenerator());
192+
RandomCandidateGenerator randomCandidateGenerator = new RandomCandidateGenerator();
193+
randomCandidateGenerator.setConf(conf);
194+
candidateGenerators.add(randomCandidateGenerator);
193195
candidateGenerators.add(new LoadCandidateGenerator());
194196
candidateGenerators.add(localityCandidateGenerator);
195197
candidateGenerators.add(new RegionReplicaRackCandidateGenerator());

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public static void beforeAllTests() throws IOException {
6767
BalancerTestBase.conf.setBoolean("hbase.master.balancer.stochastic.runMaxSteps", true);
6868
BalancerTestBase.conf.set(StochasticLoadBalancer.COST_FUNCTIONS_COST_FUNCTIONS_KEY,
6969
HeterogeneousRegionCountCostFunction.class.getName());
70+
BalancerTestBase.conf.setBoolean(
71+
HConstants.HBASE_MASTER_BALANCER_RANDOM_PICK_REGIONS_WITH_CONSIDER_COUNT_BALANCE,
72+
false);
7073
// Need to ensure test dir has been created.
7174
assertTrue(FileSystem.get(HTU.getConfiguration()).mkdirs(HTU.getDataTestDir()));
7275
RULES_FILE = HTU.getDataTestDir(

0 commit comments

Comments
 (0)