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 * A cost function for region replicas for the rack distribution. We give a relatively high cost to
025 * hosting replicas of the same region in the same rack. We do not prevent the case though.
026 */
027@InterfaceAudience.Private
028class RegionReplicaRackCostFunction extends RegionReplicaGroupingCostFunction {
029
030  private static final String REGION_REPLICA_RACK_COST_KEY =
031    "hbase.master.balancer.stochastic.regionReplicaRackCostKey";
032  private static final float DEFAULT_REGION_REPLICA_RACK_COST_KEY = 10000;
033
034  public RegionReplicaRackCostFunction(Configuration conf) {
035    this.setMultiplier(
036      conf.getFloat(REGION_REPLICA_RACK_COST_KEY, DEFAULT_REGION_REPLICA_RACK_COST_KEY));
037  }
038
039  @Override
040  protected void loadCosts() {
041    if (cluster.numRacks <= 1) {
042      maxCost = 0;
043      return; // disabled for 1 rack
044    }
045    // max cost is the case where every region replica is hosted together regardless of rack
046    maxCost = getMaxCost(cluster);
047    costsPerGroup = new long[cluster.numRacks];
048    for (int i = 0; i < cluster.colocatedReplicaCountsPerRack.length; i++) {
049      costsPerGroup[i] = costPerGroup(cluster.colocatedReplicaCountsPerRack[i]);
050    }
051  }
052
053  @Override
054  protected void regionMoved(int region, int oldServer, int newServer) {
055    if (maxCost <= 0) {
056      return; // no need to compute
057    }
058    int oldRack = cluster.serverIndexToRackIndex[oldServer];
059    int newRack = cluster.serverIndexToRackIndex[newServer];
060    if (newRack != oldRack) {
061      costsPerGroup[oldRack] = costPerGroup(cluster.colocatedReplicaCountsPerRack[oldRack]);
062      costsPerGroup[newRack] = costPerGroup(cluster.colocatedReplicaCountsPerRack[newRack]);
063    }
064  }
065}