Skip to content

Commit 2ca6c63

Browse files
author
Ray Mattingly
committed
cleanup
1 parent e5ab24c commit 2ca6c63

20 files changed

+132
-92
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,11 +778,12 @@ public List<RegionPlan> doAction(BalanceAction action) {
778778
List<RegionPlan> mbRegionPlans = new ArrayList<>();
779779
for (int serverIndex : mba.getServerToRegionsToRemove().keySet()) {
780780
Set<Integer> regionsToRemove = mba.getServerToRegionsToRemove().get(serverIndex);
781-
removeRegions(regionsPerServer[serverIndex], regionsToRemove);
781+
regionsPerServer[serverIndex] =
782+
removeRegions(regionsPerServer[serverIndex], regionsToRemove);
782783
}
783784
for (int serverIndex : mba.getServerToRegionsToAdd().keySet()) {
784785
Set<Integer> regionsToAdd = mba.getServerToRegionsToRemove().get(serverIndex);
785-
addRegions(regionsPerServer[serverIndex], regionsToAdd);
786+
regionsPerServer[serverIndex] = addRegions(regionsPerServer[serverIndex], regionsToAdd);
786787
}
787788
for (MoveRegionAction moveRegionAction : mba.getMoveActions()) {
788789
mbRegionPlans.add(regionMoved(moveRegionAction.getRegion(),
@@ -978,8 +979,8 @@ int[] removeRegions(int[] regions, Set<Integer> regionIndicesToRemove) {
978979

979980
// If the newIndex is smaller than newSize, some regions were missing from the input array
980981
if (newIndex != newSize) {
981-
throw new IllegalStateException(
982-
"Region indices mismatch: some regions in the removal set were not found in the regions array");
982+
throw new IllegalStateException("Region indices mismatch: some regions in the removal "
983+
+ "set were not found in the regions array");
983984
}
984985

985986
return newRegions;

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

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Optional;
2626
import java.util.Set;
2727
import java.util.stream.Collectors;
28+
import org.apache.hadoop.conf.Configurable;
2829
import org.apache.hadoop.conf.Configuration;
2930
import org.apache.hadoop.hbase.master.RegionPlan;
3031
import org.apache.hadoop.hbase.util.ReflectionUtils;
@@ -47,7 +48,7 @@
4748
* make it easy to define that system tables will ideally be isolated on their own RegionServer.
4849
*/
4950
@InterfaceAudience.Private
50-
public final class BalancerConditionals {
51+
final class BalancerConditionals implements Configurable {
5152

5253
private static final Logger LOG = LoggerFactory.getLogger(BalancerConditionals.class);
5354

@@ -109,40 +110,6 @@ void clearConditionalWeightCaches() {
109110
.flatMap(Optional::stream).forEach(RegionPlanConditionalCandidateGenerator::clearWeightCache);
110111
}
111112

112-
void loadConf(Configuration conf) {
113-
this.conf = conf;
114-
ImmutableSet.Builder<Class<? extends RegionPlanConditional>> conditionalClasses =
115-
ImmutableSet.builder();
116-
117-
boolean isolateSystemTables =
118-
conf.getBoolean(ISOLATE_SYSTEM_TABLES_KEY, ISOLATE_SYSTEM_TABLES_DEFAULT);
119-
if (isolateSystemTables) {
120-
conditionalClasses.add(SystemTableIsolationConditional.class);
121-
}
122-
123-
boolean isolateMetaTable = conf.getBoolean(ISOLATE_META_TABLE_KEY, ISOLATE_META_TABLE_DEFAULT);
124-
if (isolateMetaTable) {
125-
conditionalClasses.add(MetaTableIsolationConditional.class);
126-
}
127-
128-
boolean distributeReplicas = conf.getBoolean(DISTRIBUTE_REPLICAS_CONDITIONALS_KEY,
129-
DISTRIBUTE_REPLICAS_CONDITIONALS_DEFAULT);
130-
if (distributeReplicas) {
131-
conditionalClasses.add(DistributeReplicasConditional.class);
132-
}
133-
134-
Class<?>[] classes = conf.getClasses(ADDITIONAL_CONDITIONALS_KEY);
135-
for (Class<?> clazz : classes) {
136-
if (!RegionPlanConditional.class.isAssignableFrom(clazz)) {
137-
LOG.warn("Class {} is not a RegionPlanConditional", clazz.getName());
138-
continue;
139-
}
140-
conditionalClasses.add(clazz.asSubclass(RegionPlanConditional.class));
141-
}
142-
this.conditionalClasses = conditionalClasses.build();
143-
loadClusterState(null);
144-
}
145-
146113
void loadClusterState(BalancerClusterState cluster) {
147114
conditionals = conditionalClasses.stream().map(clazz -> createConditional(clazz, conf, cluster))
148115
.filter(Objects::nonNull).collect(Collectors.toSet());
@@ -216,4 +183,44 @@ private RegionPlanConditional createConditional(Class<? extends RegionPlanCondit
216183
private boolean isConditionalBalancingEnabled() {
217184
return !conditionalClasses.isEmpty();
218185
}
186+
187+
@Override
188+
public void setConf(Configuration conf) {
189+
this.conf = conf;
190+
ImmutableSet.Builder<Class<? extends RegionPlanConditional>> conditionalClasses =
191+
ImmutableSet.builder();
192+
193+
boolean isolateSystemTables =
194+
conf.getBoolean(ISOLATE_SYSTEM_TABLES_KEY, ISOLATE_SYSTEM_TABLES_DEFAULT);
195+
if (isolateSystemTables) {
196+
conditionalClasses.add(SystemTableIsolationConditional.class);
197+
}
198+
199+
boolean isolateMetaTable = conf.getBoolean(ISOLATE_META_TABLE_KEY, ISOLATE_META_TABLE_DEFAULT);
200+
if (isolateMetaTable) {
201+
conditionalClasses.add(MetaTableIsolationConditional.class);
202+
}
203+
204+
boolean distributeReplicas = conf.getBoolean(DISTRIBUTE_REPLICAS_CONDITIONALS_KEY,
205+
DISTRIBUTE_REPLICAS_CONDITIONALS_DEFAULT);
206+
if (distributeReplicas) {
207+
conditionalClasses.add(DistributeReplicasConditional.class);
208+
}
209+
210+
Class<?>[] classes = conf.getClasses(ADDITIONAL_CONDITIONALS_KEY);
211+
for (Class<?> clazz : classes) {
212+
if (!RegionPlanConditional.class.isAssignableFrom(clazz)) {
213+
LOG.warn("Class {} is not a RegionPlanConditional", clazz.getName());
214+
continue;
215+
}
216+
conditionalClasses.add(clazz.asSubclass(RegionPlanConditional.class));
217+
}
218+
this.conditionalClasses = conditionalClasses.build();
219+
loadClusterState(null);
220+
}
221+
222+
@Override
223+
public Configuration getConf() {
224+
return conf;
225+
}
219226
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* CandidateGenerator to distribute colocated replicas across different servers.
3333
*/
3434
@InterfaceAudience.Private
35-
class DistributeReplicasCandidateGenerator extends RegionPlanConditionalCandidateGenerator {
35+
final class DistributeReplicasCandidateGenerator extends RegionPlanConditionalCandidateGenerator {
3636

3737
static DistributeReplicasCandidateGenerator INSTANCE = new DistributeReplicasCandidateGenerator();
3838

@@ -94,6 +94,7 @@ BalanceAction generateCandidate(BalancerClusterState cluster, boolean isWeighing
9494
if (isForced) {
9595
return possibleAction;
9696
} else if (willBeAccepted(cluster, possibleAction)) {
97+
cluster.doAction(possibleAction); // Update cluster state to reflect move
9798
moveRegionActions.add(possibleAction);
9899
break;
99100
}
@@ -112,7 +113,9 @@ BalanceAction generateCandidate(BalancerClusterState cluster, boolean isWeighing
112113
}
113114

114115
if (!moveRegionActions.isEmpty()) {
115-
return new MoveBatchAction(moveRegionActions);
116+
MoveBatchAction batchAction = new MoveBatchAction(moveRegionActions);
117+
undoBatchAction(cluster, batchAction); // Reset cluster state to before batch
118+
return batchAction;
116119
}
117120
// If no colocated replicas are found, return NULL_ACTION
118121
if (foundColocatedReplicas) {

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Set;
2424
import org.apache.hadoop.conf.Configuration;
2525
import org.apache.hadoop.hbase.client.RegionInfo;
26+
import org.apache.hadoop.hbase.client.RegionReplicaUtil;
2627
import org.apache.hadoop.hbase.master.RegionPlan;
2728
import org.apache.hadoop.hbase.util.Pair;
2829
import org.apache.yetus.audience.InterfaceAudience;
@@ -109,8 +110,8 @@ private boolean checkViolation(ReplicaKey movingReplicaKey, Set<RegionInfo> dest
109110

110111
/**
111112
* This is necessary because it would be too expensive to use
112-
* {@link org.apache.hadoop.hbase.client.RegionReplicaUtil#isReplicasForSameRegion(RegionInfo, RegionInfo)}
113-
* for every combo of regions.
113+
* {@link RegionReplicaUtil#isReplicasForSameRegion(RegionInfo, RegionInfo)} for every combo of
114+
* regions.
114115
*/
115116
static class ReplicaKey {
116117
private final Pair<ByteArrayWrapper, ByteArrayWrapper> startAndStopKeys;
@@ -122,8 +123,12 @@ static class ReplicaKey {
122123

123124
@Override
124125
public boolean equals(Object o) {
125-
if (this == o) return true;
126-
if (!(o instanceof ReplicaKey)) return false;
126+
if (this == o) {
127+
return true;
128+
}
129+
if (!(o instanceof ReplicaKey)) {
130+
return false;
131+
}
127132
ReplicaKey other = (ReplicaKey) o;
128133
return this.startAndStopKeys.equals(other.startAndStopKeys);
129134
}
@@ -143,8 +148,12 @@ static class ByteArrayWrapper {
143148

144149
@Override
145150
public boolean equals(Object o) {
146-
if (this == o) return true;
147-
if (!(o instanceof ByteArrayWrapper)) return false;
151+
if (this == o) {
152+
return true;
153+
}
154+
if (!(o instanceof ByteArrayWrapper)) {
155+
return false;
156+
}
148157
ByteArrayWrapper other = (ByteArrayWrapper) o;
149158
return Arrays.equals(this.bytes, other.bytes);
150159
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public void setFavoredNodesManager(FavoredNodesManager fnm) {
9191
/** Returns any candidate generator in random */
9292
@Override
9393
protected CandidateGenerator getRandomGenerator(BalancerClusterState cluster) {
94-
return candidateGenerators.get(ThreadLocalRandom.current().nextInt(candidateGenerators.size()));
94+
return candidateGenerators.values().stream().toList()
95+
.get(ThreadLocalRandom.current().nextInt(candidateGenerators.size()));
9596
}
9697

9798
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.apache.yetus.audience.InterfaceAudience;
2222

2323
@InterfaceAudience.Private
24-
public class MetaTableIsolationCandidateGenerator extends TableIsolationCandidateGenerator {
24+
public final class MetaTableIsolationCandidateGenerator extends TableIsolationCandidateGenerator {
2525

2626
static MetaTableIsolationCandidateGenerator INSTANCE = new MetaTableIsolationCandidateGenerator();
2727

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
package org.apache.hadoop.hbase.master.balancer;
1919

2020
import java.util.List;
21-
import org.apache.curator.shaded.com.google.common.collect.HashMultimap;
22-
import org.apache.curator.shaded.com.google.common.collect.Multimaps;
2321
import org.apache.yetus.audience.InterfaceAudience;
2422

23+
import org.apache.hbase.thirdparty.com.google.common.collect.HashMultimap;
24+
import org.apache.hbase.thirdparty.com.google.common.collect.Multimaps;
25+
2526
@InterfaceAudience.Private
2627
public class MoveBatchAction extends BalanceAction {
2728
private final List<MoveRegionAction> moveActions;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
@InterfaceAudience.Private
3232
@InterfaceStability.Evolving
33-
public abstract class RegionPlanConditional { // todo rename to BalancerConditional
33+
public abstract class RegionPlanConditional {
3434
private static final Logger LOG = LoggerFactory.getLogger(RegionPlanConditional.class);
3535
private BalancerClusterState cluster;
3636

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
package org.apache.hadoop.hbase.master.balancer;
1919

2020
import java.time.Duration;
21+
import org.apache.yetus.audience.InterfaceAudience;
22+
import org.apache.yetus.audience.InterfaceStability;
2123
import org.slf4j.Logger;
2224
import org.slf4j.LoggerFactory;
2325

26+
@InterfaceAudience.Private
27+
@InterfaceStability.Evolving
2428
public abstract class RegionPlanConditionalCandidateGenerator extends CandidateGenerator {
2529

2630
private static final Logger LOG =
@@ -46,6 +50,13 @@ boolean willBeAccepted(BalancerClusterState cluster, BalanceAction action) {
4650
return !BalancerConditionals.INSTANCE.isViolating(cluster, action);
4751
}
4852

53+
void undoBatchAction(BalancerClusterState cluster, MoveBatchAction batchAction) {
54+
for (int i = batchAction.getMoveActions().size() - 1; i >= 0; i--) {
55+
MoveRegionAction action = batchAction.getMoveActions().get(i);
56+
cluster.doAction(action.undoAction());
57+
}
58+
}
59+
4960
void clearWeightCache() {
5061
lastWeighedAt = -1;
5162
}

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,6 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
160160
protected Map<Class<? extends CandidateGenerator>, CandidateGenerator> candidateGenerators;
161161
private Map<Class<? extends CandidateGenerator>, Double> weightsOfGenerators;
162162

163-
public enum GeneratorType {
164-
RANDOM,
165-
LOAD,
166-
LOCALITY,
167-
RACK,
168-
SYSTEM_TABLE_ISOLATION,
169-
META_TABLE_ISOLATION,
170-
}
171-
172163
private final BalancerConditionals balancerConditionals = BalancerConditionals.INSTANCE;
173164

174165
/**
@@ -270,7 +261,7 @@ protected void loadConf(Configuration conf) {
270261
rackLocalityCost = new RackLocalityCostFunction(conf);
271262

272263
// Order is important here. We need to construct conditionals to load candidate generators
273-
balancerConditionals.loadConf(conf);
264+
balancerConditionals.setConf(conf);
274265
this.candidateGenerators = createCandidateGenerators();
275266

276267
regionReplicaHostCostFunction = new RegionReplicaHostCostFunction(conf);
@@ -466,20 +457,25 @@ protected CandidateGenerator getRandomGenerator(BalancerClusterState cluster) {
466457
}
467458

468459
double rand = ThreadLocalRandom.current().nextDouble();
469-
Class<? extends CandidateGenerator> greatestWeightClazz = null;
460+
CandidateGenerator greatestWeightGenerator = null;
470461
double greatestWeight = 0;
471-
for (Class<? extends CandidateGenerator> clazz : candidateGenerators.keySet()) {
462+
for (Map.Entry<Class<? extends CandidateGenerator>,
463+
CandidateGenerator> entry : candidateGenerators.entrySet()) {
464+
Class<? extends CandidateGenerator> clazz = entry.getKey();
472465
double generatorWeight = weightsOfGenerators.get(clazz);
473466
if (generatorWeight > greatestWeight) {
474467
greatestWeight = generatorWeight;
475-
greatestWeightClazz = clazz;
468+
greatestWeightGenerator = entry.getValue();
476469
}
477470
if (rand <= generatorWeight) {
478-
return candidateGenerators.get(clazz);
471+
return entry.getValue();
479472
}
480473
}
481474

482-
return candidateGenerators.getOrDefault(greatestWeightClazz, new RandomCandidateGenerator());
475+
if (greatestWeightGenerator != null) {
476+
return greatestWeightGenerator;
477+
}
478+
return candidateGenerators.values().stream().findAny().orElseThrow();
483479
}
484480

485481
@RestrictedApi(explanation = "Should only be called in tests", link = "",
@@ -497,6 +493,7 @@ private long calculateMaxSteps(BalancerClusterState cluster) {
497493
* approach the optimal state given enough steps.
498494
*/
499495
@Override
496+
@SuppressWarnings("checkstyle:MethodLength")
500497
protected List<RegionPlan> balanceTable(TableName tableName,
501498
Map<ServerName, List<RegionInfo>> loadOfOneTable) {
502499
// On clusters with lots of HFileLinks or lots of reference files,
@@ -636,7 +633,7 @@ protected List<RegionPlan> balanceTable(TableName tableName,
636633
StringBuilder logMessage = new StringBuilder("CandidateGenerator activity summary:\n");
637634
generatorToStepCount.forEach((generator, count) -> {
638635
long approvals = generatorToApprovedActionCount.getOrDefault(generator, 0L);
639-
logMessage.append(String.format(" - %s: %d steps, %d approvals\n", generator.getSimpleName(),
636+
logMessage.append(String.format(" - %s: %d steps, %d approvals%n", generator.getSimpleName(),
640637
count, approvals));
641638
});
642639
// Log the message

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.apache.yetus.audience.InterfaceAudience;
2222

2323
@InterfaceAudience.Private
24-
public class SystemTableIsolationCandidateGenerator extends TableIsolationCandidateGenerator {
24+
public final class SystemTableIsolationCandidateGenerator extends TableIsolationCandidateGenerator {
2525

2626
static SystemTableIsolationCandidateGenerator INSTANCE =
2727
new SystemTableIsolationCandidateGenerator();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public boolean isViolatingServer(RegionPlan regionPlan, Set<RegionInfo> serverRe
6666

6767
private boolean isRegionToIsolate(RegionInfo regionInfo) {
6868
boolean isRegionToIsolate = false;
69-
if (regionInfo.isMetaRegion() && regionInfo.isMetaRegion()) {
69+
if (BalancerConditionals.INSTANCE.isMetaTableIsolationEnabled() && regionInfo.isMetaRegion()) {
7070
isRegionToIsolate = true;
7171
} else if (regionInfo.getTable().isSystemTable()) {
7272
isRegionToIsolate = true;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ BalanceAction generateCandidate(BalancerClusterState cluster, boolean isWeighing
7373
if (isWeighing) {
7474
return possibleMove;
7575
}
76+
cluster.doAction(possibleMove); // Update cluster state to reflect move
7677
moves.add(possibleMove);
7778
break;
7879
}
@@ -86,7 +87,9 @@ BalanceAction generateCandidate(BalancerClusterState cluster, boolean isWeighing
8687
if (moves.isEmpty()) {
8788
return BalanceAction.NULL_ACTION;
8889
} else {
89-
return new MoveBatchAction(moves);
90+
MoveBatchAction batchAction = new MoveBatchAction(moves);
91+
undoBatchAction(cluster, batchAction); // Reset cluster state to before batch action
92+
return batchAction;
9093
}
9194
}
9295
}

0 commit comments

Comments
 (0)