Skip to content

Commit

Permalink
HBASE-28513 The StochasticLoadBalancer should support discrete evalua…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
Ray Mattingly committed Jan 17, 2025
1 parent d9ff32a commit 475383b
Show file tree
Hide file tree
Showing 20 changed files with 1,911 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum Type {
ASSIGN_REGION,
MOVE_REGION,
SWAP_REGIONS,
MOVE_BATCH,
NULL,
}

Expand All @@ -51,6 +52,10 @@ Type getType() {
return type;
}

long getStepCount() {
return 1;
}

@Override
public String toString() {
return type + ":";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,26 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.agrona.collections.Hashing;
import org.agrona.collections.Int2IntCounterMap;
import org.apache.hadoop.hbase.HDFSBlocksDistribution;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.hadoop.hbase.client.RegionReplicaUtil;
import org.apache.hadoop.hbase.master.RackManager;
import org.apache.hadoop.hbase.master.RegionPlan;
import org.apache.hadoop.hbase.net.Address;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hbase.thirdparty.com.google.common.base.Supplier;
import org.apache.hbase.thirdparty.com.google.common.base.Suppliers;
import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;

/**
* An efficient array based implementation similar to ClusterState for keeping the status of the
* cluster in terms of region assignment and distribution. LoadBalancers, such as
Expand Down Expand Up @@ -123,6 +130,14 @@ class BalancerClusterState {
// Maps regionName -> oldServerName -> cache ratio of the region on the old server
Map<String, Pair<ServerName, Float>> regionCacheRatioOnOldServerMap;

private Supplier<List<Integer>> shuffledServerIndicesSupplier =
Suppliers.memoizeWithExpiration(() -> {
Collection<Integer> serverIndices = serversToIndex.values();
List<Integer> shuffledServerIndices = new ArrayList<>(serverIndices);
Collections.shuffle(shuffledServerIndices);
return shuffledServerIndices;
}, 5, TimeUnit.SECONDS);

static class DefaultRackManager extends RackManager {
@Override
public String getRack(ServerName server) {
Expand Down Expand Up @@ -711,6 +726,44 @@ enum LocalityType {
RACK
}

public List<RegionPlan> convertActionToPlans(BalanceAction action) {
switch (action.getType()) {
case NULL:
break;
case ASSIGN_REGION:
// FindBugs: Having the assert quietens FB BC_UNCONFIRMED_CAST warnings
assert action instanceof AssignRegionAction : action.getClass();
AssignRegionAction ar = (AssignRegionAction) action;
return ImmutableList
.of(new RegionPlan(regions[ar.getRegion()], null, servers[ar.getServer()]));
case MOVE_REGION:
assert action instanceof MoveRegionAction : action.getClass();
MoveRegionAction mra = (MoveRegionAction) action;
return ImmutableList.of(new RegionPlan(regions[mra.getRegion()],
servers[mra.getFromServer()], servers[mra.getToServer()]));
case SWAP_REGIONS:
assert action instanceof SwapRegionsAction : action.getClass();
SwapRegionsAction a = (SwapRegionsAction) action;
return ImmutableList.of(
new RegionPlan(regions[a.getFromRegion()], servers[a.getFromServer()],
servers[a.getToServer()]),
new RegionPlan(regions[a.getToRegion()], servers[a.getToServer()],
servers[a.getFromServer()]));
case MOVE_BATCH:
assert action instanceof MoveBatchAction : action.getClass();
MoveBatchAction mba = (MoveBatchAction) action;
List<RegionPlan> mbRegionPlans = new ArrayList<>();
for (MoveRegionAction moveRegionAction : mba.getMoveActions()) {
mbRegionPlans.add(new RegionPlan(regions[moveRegionAction.getRegion()],
servers[moveRegionAction.getFromServer()], servers[moveRegionAction.getToServer()]));
}
return mbRegionPlans;
default:
throw new RuntimeException("Unknown action:" + action.getType());
}
return Collections.emptyList();
}

public void doAction(BalanceAction action) {
switch (action.getType()) {
case NULL:
Expand Down Expand Up @@ -742,8 +795,25 @@ public void doAction(BalanceAction action) {
regionMoved(a.getFromRegion(), a.getFromServer(), a.getToServer());
regionMoved(a.getToRegion(), a.getToServer(), a.getFromServer());
break;
case MOVE_BATCH:
assert action instanceof MoveBatchAction : action.getClass();
MoveBatchAction mba = (MoveBatchAction) action;
for (int serverIndex : mba.getServerToRegionsToRemove().keySet()) {
Set<Integer> regionsToRemove = mba.getServerToRegionsToRemove().get(serverIndex);
regionsPerServer[serverIndex] =
removeRegions(regionsPerServer[serverIndex], regionsToRemove);
}
for (int serverIndex : mba.getServerToRegionsToAdd().keySet()) {
Set<Integer> regionsToAdd = mba.getServerToRegionsToAdd().get(serverIndex);
regionsPerServer[serverIndex] = addRegions(regionsPerServer[serverIndex], regionsToAdd);
}
for (MoveRegionAction moveRegionAction : mba.getMoveActions()) {
regionMoved(moveRegionAction.getRegion(), moveRegionAction.getFromServer(),
moveRegionAction.getToServer());
}
break;
default:
throw new RuntimeException("Uknown action:" + action.getType());
throw new RuntimeException("Unknown action:" + action.getType());
}
}

Expand Down Expand Up @@ -905,6 +975,52 @@ int[] addRegion(int[] regions, int regionIndex) {
return newRegions;
}

int[] removeRegions(int[] regions, Set<Integer> regionIndicesToRemove) {
// Calculate the size of the new regions array
int newSize = regions.length - regionIndicesToRemove.size();
if (newSize < 0) {
throw new IllegalStateException(
"Region indices mismatch: more regions to remove than in the regions array");
}

int[] newRegions = new int[newSize];
int newIndex = 0;

// Copy only the regions not in the removal set
for (int region : regions) {
if (!regionIndicesToRemove.contains(region)) {
newRegions[newIndex++] = region;
}
}

// 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");
}

return newRegions;
}

int[] addRegions(int[] regions, Set<Integer> regionIndicesToAdd) {
int[] newRegions = new int[regions.length + regionIndicesToAdd.size()];

// Copy the existing regions to the new array
System.arraycopy(regions, 0, newRegions, 0, regions.length);

// Add the new regions at the end of the array
int newIndex = regions.length;
for (int regionIndex : regionIndicesToAdd) {
newRegions[newIndex++] = regionIndex;
}

return newRegions;
}

List<Integer> getShuffledServerIndices() {
return shuffledServerIndicesSupplier.get();
}

int[] addRegionSorted(int[] regions, int regionIndex) {
int[] newRegions = new int[regions.length + 1];
int i = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.master.balancer;

import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.master.RegionPlan;
import org.apache.hadoop.hbase.util.ReflectionUtils;
import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet;

/**
* Balancer conditionals supplement cost functions in the {@link StochasticLoadBalancer}. Cost
* functions are insufficient and difficult to work with when making discrete decisions; this is
* because they operate on a continuous scale, and each cost function's multiplier affects the
* relative importance of every other cost function. So it is difficult to meaningfully and clearly
* value many aspects of your region distribution via cost functions alone. Conditionals allow you
* to very clearly define discrete rules that your balancer would ideally follow. To clarify, a
* conditional violation will not block a region assignment because we would prefer to have uptime
* than have perfectly intentional balance. But conditionals allow you to, for example, define that
* a region's primary and secondary should not live on the same rack. Another example, conditionals
* make it easy to define that system tables will ideally be isolated on their own RegionServer
* (without needing to manage distinct RegionServer groups). Use of conditionals may cause an
* extremely unbalanced cluster to exceed its max balancer runtime. This is necessary because
* conditional candidate generation is quite expensive, and cutting it off early could prevent us
* from finding a solution.
*/
@InterfaceAudience.Private
final class BalancerConditionals implements Configurable {

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

static final BalancerConditionals INSTANCE = new BalancerConditionals();
public static final String DISTRIBUTE_REPLICAS_KEY =
"hbase.master.balancer.stochastic.conditionals.distributeReplicas";
public static final boolean DISTRIBUTE_REPLICAS_DEFAULT = false;

public static final String ADDITIONAL_CONDITIONALS_KEY =
"hbase.master.balancer.stochastic.additionalConditionals";

private Set<Class<? extends RegionPlanConditional>> conditionalClasses = Collections.emptySet();
private Set<RegionPlanConditional> conditionals = Collections.emptySet();
private Configuration conf;

private BalancerConditionals() {
}

boolean shouldRunBalancer(BalancerClusterState cluster) {
return isConditionalBalancingEnabled() && conditionals.stream()
.map(RegionPlanConditional::getCandidateGenerators).flatMap(Collection::stream)
.map(generator -> generator.getWeight(cluster)).anyMatch(weight -> weight > 0);
}

Set<Class<? extends RegionPlanConditional>> getConditionalClasses() {
return Set.copyOf(conditionalClasses);
}

Collection<RegionPlanConditional> getConditionals() {
return conditionals;
}

boolean isReplicaDistributionEnabled() {
return conditionalClasses.contains(DistributeReplicasConditional.class);
}

boolean shouldSkipSloppyServerEvaluation() {
return isConditionalBalancingEnabled();
}

boolean isConditionalBalancingEnabled() {
return !conditionalClasses.isEmpty();
}

void clearConditionalWeightCaches() {
conditionals.stream().map(RegionPlanConditional::getCandidateGenerators)
.flatMap(Collection::stream)
.forEach(RegionPlanConditionalCandidateGenerator::clearWeightCache);
}

void loadClusterState(BalancerClusterState cluster) {
conditionals = conditionalClasses.stream().map(clazz -> createConditional(clazz, conf, cluster))
.filter(Objects::nonNull).collect(Collectors.toSet());
}

/**
* Indicates whether the action is good for our conditional compliance.
* @param cluster The cluster state
* @param action The proposed action
* @return -1 if conditionals improve, 0 if neutral, 1 if conditionals degrade
*/
int getViolationCountChange(BalancerClusterState cluster, BalanceAction action) {
boolean isViolatingPre = isViolating(cluster, action.undoAction());
boolean isViolatingPost = isViolating(cluster, action);
if (isViolatingPre && isViolatingPost) {
return 0;
} else if (!isViolatingPre && isViolatingPost) {
return 1;
} else {
return -1;
}
}

/**
* Check if the proposed action violates conditionals
* @param cluster The cluster state
* @param action The proposed action
*/
boolean isViolating(BalancerClusterState cluster, BalanceAction action) {
conditionals.forEach(conditional -> conditional.refreshClusterState(cluster));
if (conditionals.isEmpty()) {
return false;
}
List<RegionPlan> regionPlans = cluster.convertActionToPlans(action);
for (RegionPlan regionPlan : regionPlans) {
if (isViolating(regionPlan)) {
return true;
}
}
return false;
}

private boolean isViolating(RegionPlan regionPlan) {
for (RegionPlanConditional conditional : conditionals) {
if (conditional.isViolating(regionPlan)) {
return true;
}
}
return false;
}

private RegionPlanConditional createConditional(Class<? extends RegionPlanConditional> clazz,
Configuration conf, BalancerClusterState cluster) {
if (conf == null) {
conf = new Configuration();
}
if (cluster == null) {
cluster = new BalancerClusterState(Collections.emptyMap(), null, null, null, null);
}
try {
Constructor<? extends RegionPlanConditional> ctor =
clazz.getDeclaredConstructor(Configuration.class, BalancerClusterState.class);
return ReflectionUtils.instantiate(clazz.getName(), ctor, conf, cluster);
} catch (NoSuchMethodException e) {
LOG.warn("Cannot find constructor with Configuration and "
+ "BalancerClusterState parameters for class '{}': {}", clazz.getName(), e.getMessage());
}
return null;
}

@Override
public void setConf(Configuration conf) {
this.conf = conf;
ImmutableSet.Builder<Class<? extends RegionPlanConditional>> conditionalClasses =
ImmutableSet.builder();

boolean distributeReplicas =
conf.getBoolean(DISTRIBUTE_REPLICAS_KEY, DISTRIBUTE_REPLICAS_DEFAULT);
if (distributeReplicas) {
conditionalClasses.add(DistributeReplicasConditional.class);
}

Class<?>[] classes = conf.getClasses(ADDITIONAL_CONDITIONALS_KEY);
for (Class<?> clazz : classes) {
if (!RegionPlanConditional.class.isAssignableFrom(clazz)) {
LOG.warn("Class {} is not a RegionPlanConditional", clazz.getName());
continue;
}
conditionalClasses.add(clazz.asSubclass(RegionPlanConditional.class));
}
this.conditionalClasses = conditionalClasses.build();
loadClusterState(null);
}

@Override
public Configuration getConf() {
return conf;
}
}
Loading

0 comments on commit 475383b

Please sign in to comment.