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