2828import java .util .Map ;
2929import java .util .Objects ;
3030import java .util .concurrent .ThreadLocalRandom ;
31+ import java .util .function .Supplier ;
3132import java .util .stream .Collectors ;
3233import org .apache .hadoop .conf .Configuration ;
3334import org .apache .hadoop .hbase .ClusterMetrics ;
3637import org .apache .hadoop .hbase .ServerMetrics ;
3738import org .apache .hadoop .hbase .ServerName ;
3839import org .apache .hadoop .hbase .TableName ;
39- import org .apache .hadoop .hbase .client .BalancerDecision ;
4040import org .apache .hadoop .hbase .client .RegionInfo ;
4141import org .apache .hadoop .hbase .master .RegionPlan ;
4242import org .apache .hadoop .hbase .master .balancer .BalancerClusterState .LocalityType ;
43- import org .apache .hadoop .hbase .namequeues .BalancerDecisionDetails ;
44- import org .apache .hadoop .hbase .namequeues .NamedQueueRecorder ;
45- import org .apache .hadoop .hbase .regionserver .compactions .OffPeakHours ;
4643import org .apache .hadoop .hbase .util .EnvironmentEdgeManager ;
4744import org .apache .hadoop .hbase .util .ReflectionUtils ;
4845import org .apache .yetus .audience .InterfaceAudience ;
@@ -151,11 +148,6 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
151148 private RegionReplicaHostCostFunction regionReplicaHostCostFunction ;
152149 private RegionReplicaRackCostFunction regionReplicaRackCostFunction ;
153150
154- /**
155- * Use to add balancer decision history to ring-buffer
156- */
157- NamedQueueRecorder namedQueueRecorder ;
158-
159151 /**
160152 * The constructor that pass a MetricsStochasticBalancer to BaseLoadBalancer to replace its
161153 * default MetricsBalancer
@@ -205,7 +197,7 @@ public synchronized void setConf(Configuration conf) {
205197 costFunctions = new ArrayList <>();
206198 addCostFunction (new RegionCountSkewCostFunction (conf ));
207199 addCostFunction (new PrimaryRegionCountSkewCostFunction (conf ));
208- addCostFunction (new MoveCostFunction (conf ));
200+ addCostFunction (new MoveCostFunction (conf , () -> provider ));
209201 addCostFunction (localityCost );
210202 addCostFunction (rackLocalityCost );
211203 addCostFunction (new TableSkewCostFunction (conf ));
@@ -221,13 +213,6 @@ public synchronized void setConf(Configuration conf) {
221213 curFunctionCosts = new Double [costFunctions .size ()];
222214 tempFunctionCosts = new Double [costFunctions .size ()];
223215
224- boolean isBalancerDecisionRecording = getConf ()
225- .getBoolean (BaseLoadBalancer .BALANCER_DECISION_BUFFER_ENABLED ,
226- BaseLoadBalancer .DEFAULT_BALANCER_DECISION_BUFFER_ENABLED );
227- if (this .namedQueueRecorder == null && isBalancerDecisionRecording ) {
228- this .namedQueueRecorder = NamedQueueRecorder .getInstance (getConf ());
229- }
230-
231216 LOG .info ("Loaded config; maxSteps=" + maxSteps + ", stepsPerRegion=" + stepsPerRegion +
232217 ", maxRunningTime=" + maxRunningTime + ", isByTable=" + isByTable + ", CostFunctions=" +
233218 Arrays .toString (getCostFunctionNames ()) + " etc." );
@@ -483,24 +468,9 @@ public synchronized List<RegionPlan> balanceTable(TableName tableName, Map<Serve
483468 }
484469
485470 private void sendRegionPlansToRingBuffer (List <RegionPlan > plans , double currentCost ,
486- double initCost , String initFunctionTotalCosts , long step ) {
487- if (this .namedQueueRecorder != null ) {
488- List <String > regionPlans = new ArrayList <>();
489- for (RegionPlan plan : plans ) {
490- regionPlans .add (
491- "table: " + plan .getRegionInfo ().getTable () + " , region: " + plan .getRegionName ()
492- + " , source: " + plan .getSource () + " , destination: " + plan .getDestination ());
493- }
494- BalancerDecision balancerDecision =
495- new BalancerDecision .Builder ()
496- .setInitTotalCost (initCost )
497- .setInitialFunctionCosts (initFunctionTotalCosts )
498- .setComputedTotalCost (currentCost )
499- .setFinalFunctionCosts (totalCostsPerFunc ())
500- .setComputedSteps (step )
501- .setRegionPlans (regionPlans ).build ();
502- namedQueueRecorder .addRecord (new BalancerDecisionDetails (balancerDecision ));
503- }
471+ double initCost , String initFunctionTotalCosts , long step ) {
472+ provider .recordBalancerDecision (plans , currentCost , initCost , initFunctionTotalCosts ,
473+ this ::totalCostsPerFunc , step );
504474 }
505475
506476 /**
@@ -837,10 +807,15 @@ static class MoveCostFunction extends CostFunction {
837807
838808 private final float maxMovesPercent ;
839809 private final Configuration conf ;
810+ // we call setConf before setClusterInfoProvider, so when we create MoveCostFunction, the
811+ // provider is still null, thus here we need to provide a supplier instead of passing the
812+ // provider directly.
813+ private final Supplier <ClusterInfoProvider > provider ;
840814
841- MoveCostFunction (Configuration conf ) {
815+ MoveCostFunction (Configuration conf , Supplier < ClusterInfoProvider > provider ) {
842816 super (conf );
843817 this .conf = conf ;
818+ this .provider = provider ;
844819 // What percent of the number of regions a single run of the balancer can move.
845820 maxMovesPercent = conf .getFloat (MAX_MOVES_PERCENT_KEY , DEFAULT_MAX_MOVE_PERCENT );
846821
@@ -853,7 +828,8 @@ static class MoveCostFunction extends CostFunction {
853828 protected double cost () {
854829 // Move cost multiplier should be the same cost or higher than the rest of the costs to ensure
855830 // that large benefits are need to overcome the cost of a move.
856- if (OffPeakHours .getInstance (conf ).isOffPeakHour ()) {
831+ ClusterInfoProvider p = provider .get ();
832+ if (p != null && p .isOffPeakHour ()) {
857833 this .setMultiplier (conf .getFloat (MOVE_COST_OFFPEAK_KEY , DEFAULT_MOVE_COST_OFFPEAK ));
858834 } else {
859835 this .setMultiplier (conf .getFloat (MOVE_COST_KEY , DEFAULT_MOVE_COST ));
0 commit comments