wchevreuil commented on a change in pull request #677: HBASE-23073 Add an optional costFunction to balance regions according to a capacity rule URL: https://github.com/apache/hbase/pull/677#discussion_r331002282
########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/master/balancer/HeterogeneousRegionCountCostFunction.java ########## @@ -0,0 +1,288 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.ServerName; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is an optional Cost function designed to allow region count skew across RegionServers. + * A rule file is loaded from the local FS or HDFS before balancing. It contains lines of rules. + * A rule is composed of a regexp for hostname, and a limit. For example, we could have: + * <p> + * * rs[0-9] 200 + * * rs1[0-9] 50 + * </p> + * RegionServers with hostname matching the first rules will have a limit of 200, and the others 50. + * If there's no match, a default is set. + * The costFunction is trying to fill all RegionServers linearly, meaning that if the global usage + * is at 50%, then all RegionServers should hold half of their capacity in terms of regions. + * In order to use this CostFunction, you need to set the following options: + * <ul> + * <li>hbase.master.balancer.stochastic.additionalCostFunctions</li> + * <li>hbase.master.balancer.stochastic.heterogeneousRegionCountRulesFile</li> + * <li>hbase.master.balancer.stochastic.heterogeneousRegionCountDefault</li> + * </ul> + */ +@InterfaceAudience.Private +public class HeterogeneousRegionCountCostFunction extends StochasticLoadBalancer.CostFunction { + + /** + * configuration used for the path where the rule file is stored. + */ + static final String HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE = + "hbase.master.balancer.heterogeneousRegionCountRulesFile"; + private static final Logger LOG = LoggerFactory.getLogger( + HeterogeneousRegionCountCostFunction.class); + /** + * Default rule to apply when the rule file is not found. Default to 200. + */ + private static final String HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_DEFAULT = + "hbase.master.balancer.heterogeneousRegionCountDefault"; + /** + * Cost for the function. Default to 500, can be changed. + */ + private static final String REGION_COUNT_SKEW_COST_KEY = + "hbase.master.balancer.stochastic.heterogeneousRegionCountCost"; + private static final float DEFAULT_REGION_COUNT_SKEW_COST = 5000; + private final String rulesPath; + + /** + * Contains the rules, key is the regexp for ServerName, value is the limit + */ + private final Map<Pattern, Integer> limitPerRule; + + /** + * This is a cache, used to not go through all the limitPerRule map when searching for limit + */ + private final Map<ServerName, Integer> limitPerRS; + private int defaultNumberOfRegions; + + /** + * Total capacity of regions for the cluster, based on the online RS and their associated rules + */ + private int totalCapacity = 0; + + + public HeterogeneousRegionCountCostFunction(final Configuration conf) { + super(conf); + this.limitPerRS = new HashMap<>(); + this.limitPerRule = new HashMap<>(); + this.setMultiplier(conf.getFloat(REGION_COUNT_SKEW_COST_KEY, DEFAULT_REGION_COUNT_SKEW_COST)); + + this.rulesPath = conf.get(HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_FILE); + + this.defaultNumberOfRegions = conf.getInt( + HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_DEFAULT, 200); + if (this.defaultNumberOfRegions < 0) { + LOG.warn("invalid configuration '" + HBASE_MASTER_BALANCER_HETEROGENEOUS_RULES_DEFAULT + +"'. Setting default to 200"); + this.defaultNumberOfRegions = 200; + } + + if (conf.getFloat( + StochasticLoadBalancer.RegionCountSkewCostFunction.REGION_COUNT_SKEW_COST_KEY, + StochasticLoadBalancer.RegionCountSkewCostFunction.DEFAULT_REGION_COUNT_SKEW_COST) > 0) + { + LOG.warn("regionCountCost is not set to 0, " + + " this will interfere with the HeterogeneousRegionCountCostFunction!"); + } Review comment: Why is this a problem? Shouldn't we abort this cost function loading if this will cause it to behave improperly? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services