001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master.balancer; 019 020import org.apache.hadoop.conf.Configuration; 021import org.apache.yetus.audience.InterfaceAudience; 022 023/** 024 * Compute the cost of a potential cluster configuration based upon how evenly distributed tables 025 * are. 026 */ 027@InterfaceAudience.Private 028class TableSkewCostFunction extends CostFunction { 029 030 private static final String TABLE_SKEW_COST_KEY = 031 "hbase.master.balancer.stochastic.tableSkewCost"; 032 private static final float DEFAULT_TABLE_SKEW_COST = 35; 033 DoubleArrayCost[] costsPerTable; 034 035 TableSkewCostFunction(Configuration conf) { 036 this.setMultiplier(conf.getFloat(TABLE_SKEW_COST_KEY, DEFAULT_TABLE_SKEW_COST)); 037 } 038 039 @Override 040 void prepare(BalancerClusterState cluster) { 041 super.prepare(cluster); 042 costsPerTable = new DoubleArrayCost[cluster.numTables]; 043 for (int tableIdx = 0; tableIdx < cluster.numTables; tableIdx++) { 044 costsPerTable[tableIdx] = new DoubleArrayCost(); 045 costsPerTable[tableIdx].prepare(cluster.numServers); 046 final int tableIndex = tableIdx; 047 costsPerTable[tableIdx].applyCostsChange(costs -> { 048 // Keep a cached deep copy for change-only recomputation 049 for (int i = 0; i < cluster.numServers; i++) { 050 costs[i] = cluster.numRegionsPerServerPerTable[tableIndex][i]; 051 } 052 }); 053 } 054 } 055 056 @Override 057 protected void regionMoved(int region, int oldServer, int newServer) { 058 int tableIdx = cluster.regionIndexToTableIndex[region]; 059 costsPerTable[tableIdx].applyCostsChange(costs -> { 060 costs[oldServer] = cluster.numRegionsPerServerPerTable[tableIdx][oldServer]; 061 costs[newServer] = cluster.numRegionsPerServerPerTable[tableIdx][newServer]; 062 }); 063 } 064 065 @Override 066 protected double cost() { 067 double cost = 0; 068 for (int tableIdx = 0; tableIdx < cluster.numTables; tableIdx++) { 069 cost += costsPerTable[tableIdx].cost(); 070 } 071 return cost; 072 } 073}