Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Mattingly committed Dec 28, 2024
1 parent e5ab24c commit 7d18dc9
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -778,11 +778,12 @@ public List<RegionPlan> doAction(BalanceAction action) {
List<RegionPlan> mbRegionPlans = new ArrayList<>();
for (int serverIndex : mba.getServerToRegionsToRemove().keySet()) {
Set<Integer> regionsToRemove = mba.getServerToRegionsToRemove().get(serverIndex);
removeRegions(regionsPerServer[serverIndex], regionsToRemove);
regionsPerServer[serverIndex] =
removeRegions(regionsPerServer[serverIndex], regionsToRemove);
}
for (int serverIndex : mba.getServerToRegionsToAdd().keySet()) {
Set<Integer> regionsToAdd = mba.getServerToRegionsToRemove().get(serverIndex);
addRegions(regionsPerServer[serverIndex], regionsToAdd);
regionsPerServer[serverIndex] = addRegions(regionsPerServer[serverIndex], regionsToAdd);
}
for (MoveRegionAction moveRegionAction : mba.getMoveActions()) {
mbRegionPlans.add(regionMoved(moveRegionAction.getRegion(),
Expand Down Expand Up @@ -978,8 +979,8 @@ int[] removeRegions(int[] regions, Set<Integer> regionIndicesToRemove) {

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

return newRegions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* CandidateGenerator to distribute colocated replicas across different servers.
*/
@InterfaceAudience.Private
class DistributeReplicasCandidateGenerator extends RegionPlanConditionalCandidateGenerator {
final class DistributeReplicasCandidateGenerator extends RegionPlanConditionalCandidateGenerator {

static DistributeReplicasCandidateGenerator INSTANCE = new DistributeReplicasCandidateGenerator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.hadoop.hbase.client.RegionReplicaUtil;
import org.apache.hadoop.hbase.master.RegionPlan;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.yetus.audience.InterfaceAudience;
Expand Down Expand Up @@ -109,8 +110,8 @@ private boolean checkViolation(ReplicaKey movingReplicaKey, Set<RegionInfo> dest

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ReplicaKey)) return false;
if (this == o) {
return true;
}
if (!(o instanceof ReplicaKey)) {
return false;
}
ReplicaKey other = (ReplicaKey) o;
return this.startAndStopKeys.equals(other.startAndStopKeys);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public void setFavoredNodesManager(FavoredNodesManager fnm) {
/** Returns any candidate generator in random */
@Override
protected CandidateGenerator getRandomGenerator(BalancerClusterState cluster) {
return candidateGenerators.get(ThreadLocalRandom.current().nextInt(candidateGenerators.size()));
return candidateGenerators.values().stream().toList()
.get(ThreadLocalRandom.current().nextInt(candidateGenerators.size()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.yetus.audience.InterfaceAudience;

@InterfaceAudience.Private
public class MetaTableIsolationCandidateGenerator extends TableIsolationCandidateGenerator {
public final class MetaTableIsolationCandidateGenerator extends TableIsolationCandidateGenerator {

static MetaTableIsolationCandidateGenerator INSTANCE = new MetaTableIsolationCandidateGenerator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package org.apache.hadoop.hbase.master.balancer;

import java.util.List;
import org.apache.curator.shaded.com.google.common.collect.HashMultimap;
import org.apache.curator.shaded.com.google.common.collect.Multimaps;
import org.apache.yetus.audience.InterfaceAudience;

import org.apache.hbase.thirdparty.com.google.common.collect.HashMultimap;
import org.apache.hbase.thirdparty.com.google.common.collect.Multimaps;

@InterfaceAudience.Private
public class MoveBatchAction extends BalanceAction {
private final List<MoveRegionAction> moveActions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

@InterfaceAudience.Private
@InterfaceStability.Evolving
public abstract class RegionPlanConditional { // todo rename to BalancerConditional
public abstract class RegionPlanConditional {
private static final Logger LOG = LoggerFactory.getLogger(RegionPlanConditional.class);
private BalancerClusterState cluster;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,6 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
protected Map<Class<? extends CandidateGenerator>, CandidateGenerator> candidateGenerators;
private Map<Class<? extends CandidateGenerator>, Double> weightsOfGenerators;

public enum GeneratorType {
RANDOM,
LOAD,
LOCALITY,
RACK,
SYSTEM_TABLE_ISOLATION,
META_TABLE_ISOLATION,
}

private final BalancerConditionals balancerConditionals = BalancerConditionals.INSTANCE;

/**
Expand Down Expand Up @@ -466,20 +457,25 @@ protected CandidateGenerator getRandomGenerator(BalancerClusterState cluster) {
}

double rand = ThreadLocalRandom.current().nextDouble();
Class<? extends CandidateGenerator> greatestWeightClazz = null;
CandidateGenerator greatestWeightGenerator = null;
double greatestWeight = 0;
for (Class<? extends CandidateGenerator> clazz : candidateGenerators.keySet()) {
for (Map.Entry<Class<? extends CandidateGenerator>,
CandidateGenerator> entry : candidateGenerators.entrySet()) {
Class<? extends CandidateGenerator> clazz = entry.getKey();
double generatorWeight = weightsOfGenerators.get(clazz);
if (generatorWeight > greatestWeight) {
greatestWeight = generatorWeight;
greatestWeightClazz = clazz;
greatestWeightGenerator = entry.getValue();
}
if (rand <= generatorWeight) {
return candidateGenerators.get(clazz);
return entry.getValue();
}
}

return candidateGenerators.getOrDefault(greatestWeightClazz, new RandomCandidateGenerator());
if (greatestWeightGenerator != null) {
return greatestWeightGenerator;
}
return candidateGenerators.values().stream().findAny().orElseThrow();
}

@RestrictedApi(explanation = "Should only be called in tests", link = "",
Expand All @@ -497,6 +493,7 @@ private long calculateMaxSteps(BalancerClusterState cluster) {
* approach the optimal state given enough steps.
*/
@Override
@SuppressWarnings("checkstyle:MethodLength")
protected List<RegionPlan> balanceTable(TableName tableName,
Map<ServerName, List<RegionInfo>> loadOfOneTable) {
// On clusters with lots of HFileLinks or lots of reference files,
Expand Down Expand Up @@ -636,7 +633,7 @@ protected List<RegionPlan> balanceTable(TableName tableName,
StringBuilder logMessage = new StringBuilder("CandidateGenerator activity summary:\n");
generatorToStepCount.forEach((generator, count) -> {
long approvals = generatorToApprovedActionCount.getOrDefault(generator, 0L);
logMessage.append(String.format(" - %s: %d steps, %d approvals\n", generator.getSimpleName(),
logMessage.append(String.format(" - %s: %d steps, %d approvals%n", generator.getSimpleName(),
count, approvals));
});
// Log the message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.yetus.audience.InterfaceAudience;

@InterfaceAudience.Private
public class SystemTableIsolationCandidateGenerator extends TableIsolationCandidateGenerator {
public final class SystemTableIsolationCandidateGenerator extends TableIsolationCandidateGenerator {

static SystemTableIsolationCandidateGenerator INSTANCE =
new SystemTableIsolationCandidateGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public final class CandidateGeneratorTestUtil {

private static final Logger LOG = LoggerFactory.getLogger(CandidateGeneratorTestUtil.class);

private CandidateGeneratorTestUtil() {
}

static void runBalancerToExhaustion(Configuration conf,
Map<ServerName, List<RegionInfo>> serverToRegions,
Set<Function<BalancerClusterState, Boolean>> expectations) {
Expand Down Expand Up @@ -144,9 +147,8 @@ static void printClusterDistribution(BalancerClusterState cluster, long actionsT
}

/**
* Partitions the given serverToRegions map by table, returning a structure of Map<TableName,
* Map<ServerName, List<RegionInfo>>>. The tables are derived from the RegionInfo objects found in
* serverToRegions.
* Partitions the given serverToRegions map by table The tables are derived from the RegionInfo
* objects found in serverToRegions.
* @param serverToRegions The map of servers to their assigned regions.
* @return A map of tables to their server-to-region assignments.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import static org.apache.hadoop.hbase.master.balancer.CandidateGeneratorTestUtil.isTableIsolated;
import static org.apache.hadoop.hbase.master.balancer.CandidateGeneratorTestUtil.runBalancerToExhaustion;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.ServerName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

import static org.apache.hadoop.hbase.master.balancer.CandidateGeneratorTestUtil.runBalancerToExhaustion;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.ServerName;
Expand Down

0 comments on commit 7d18dc9

Please sign in to comment.