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 java.util.Map;
021import java.util.concurrent.ThreadLocalRandom;
022import org.apache.hadoop.hbase.client.RegionInfo;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * Generates a candidate action to be applied to the cluster for cost function search
027 */
028@InterfaceAudience.Private
029abstract class CandidateGenerator {
030
031  protected static final double MAX_WEIGHT = 1.0;
032
033  abstract BalanceAction generate(BalancerClusterState cluster);
034
035  /**
036   * From a list of regions pick a random one. Null can be returned which
037   * {@link StochasticLoadBalancer#balanceCluster(Map)} recognize as signal to try a region move
038   * rather than swap.
039   * @param cluster        The state of the cluster
040   * @param server         index of the server
041   * @param chanceOfNoSwap Chance that this will decide to try a move rather than a swap.
042   * @return a random {@link RegionInfo} or null if an asymmetrical move is suggested.
043   */
044  int pickRandomRegion(BalancerClusterState cluster, int server, double chanceOfNoSwap) {
045    // Check to see if this is just a move.
046    if (
047      cluster.regionsPerServer[server].length == 0
048        || ThreadLocalRandom.current().nextFloat() < chanceOfNoSwap
049    ) {
050      // signal a move only.
051      return -1;
052    }
053    int rand = ThreadLocalRandom.current().nextInt(cluster.regionsPerServer[server].length);
054    return cluster.regionsPerServer[server][rand];
055  }
056
057  int pickRandomServer(BalancerClusterState cluster) {
058    if (cluster.numServers < 1) {
059      return -1;
060    }
061
062    return ThreadLocalRandom.current().nextInt(cluster.numServers);
063  }
064
065  int pickRandomRack(BalancerClusterState cluster) {
066    if (cluster.numRacks < 1) {
067      return -1;
068    }
069
070    return ThreadLocalRandom.current().nextInt(cluster.numRacks);
071  }
072
073  int pickOtherRandomServer(BalancerClusterState cluster, int serverIndex) {
074    if (cluster.numServers < 2) {
075      return -1;
076    }
077    while (true) {
078      int otherServerIndex = pickRandomServer(cluster);
079      if (otherServerIndex != serverIndex) {
080        return otherServerIndex;
081      }
082    }
083  }
084
085  int pickOtherRandomRack(BalancerClusterState cluster, int rackIndex) {
086    if (cluster.numRacks < 2) {
087      return -1;
088    }
089    while (true) {
090      int otherRackIndex = pickRandomRack(cluster);
091      if (otherRackIndex != rackIndex) {
092        return otherRackIndex;
093      }
094    }
095  }
096
097  BalanceAction pickRandomRegions(BalancerClusterState cluster, int thisServer, int otherServer) {
098    if (thisServer < 0 || otherServer < 0) {
099      return BalanceAction.NULL_ACTION;
100    }
101
102    // Decide who is most likely to need another region
103    int thisRegionCount = cluster.getNumRegions(thisServer);
104    int otherRegionCount = cluster.getNumRegions(otherServer);
105
106    // Assign the chance based upon the above
107    double thisChance = (thisRegionCount > otherRegionCount) ? 0 : 0.5;
108    double otherChance = (thisRegionCount <= otherRegionCount) ? 0 : 0.5;
109
110    int thisRegion = pickRandomRegion(cluster, thisServer, thisChance);
111    int otherRegion = pickRandomRegion(cluster, otherServer, otherChance);
112
113    return getAction(thisServer, thisRegion, otherServer, otherRegion);
114  }
115
116  protected BalanceAction getAction(int fromServer, int fromRegion, int toServer, int toRegion) {
117    if (fromServer < 0 || toServer < 0) {
118      return BalanceAction.NULL_ACTION;
119    }
120    if (fromRegion >= 0 && toRegion >= 0) {
121      return new SwapRegionsAction(fromServer, fromRegion, toServer, toRegion);
122    } else if (fromRegion >= 0) {
123      return new MoveRegionAction(fromRegion, fromServer, toServer);
124    } else if (toRegion >= 0) {
125      return new MoveRegionAction(toRegion, toServer, fromServer);
126    } else {
127      return BalanceAction.NULL_ACTION;
128    }
129  }
130}