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.concurrent.ThreadLocalRandom;
021import org.apache.yetus.audience.InterfaceAudience;
022
023@InterfaceAudience.Private
024class LoadCandidateGenerator extends CandidateGenerator {
025
026  @Override
027  BalanceAction generate(BalancerClusterState cluster) {
028    cluster.sortServersByRegionCount();
029    int thisServer = pickMostLoadedServer(cluster, -1);
030    int otherServer = pickLeastLoadedServer(cluster, thisServer);
031    return pickRandomRegions(cluster, thisServer, otherServer);
032  }
033
034  private int pickLeastLoadedServer(final BalancerClusterState cluster, int thisServer) {
035    Integer[] servers = cluster.serverIndicesSortedByRegionCount;
036
037    int selectedIndex = -1;
038    double currentLargestRandom = -1;
039    for (int i = 0; i < servers.length; i++) {
040      if (servers[i] == null || servers[i] == thisServer) {
041        continue;
042      }
043      if (
044        selectedIndex != -1
045          && cluster.getNumRegionsComparator().compare(servers[i], servers[selectedIndex]) != 0
046      ) {
047        // Exhausted servers of the same region count
048        break;
049      }
050      // we don't know how many servers have the same region count, we will randomly select one
051      // using a simplified inline reservoir sampling by assignmening a random number to stream
052      // data and choose the greatest one. (http://gregable.com/2007/10/reservoir-sampling.html)
053      double currentRandom = ThreadLocalRandom.current().nextDouble();
054      if (currentRandom > currentLargestRandom) {
055        selectedIndex = i;
056        currentLargestRandom = currentRandom;
057      }
058    }
059    return selectedIndex == -1 ? -1 : servers[selectedIndex];
060  }
061
062  private int pickMostLoadedServer(final BalancerClusterState cluster, int thisServer) {
063    Integer[] servers = cluster.serverIndicesSortedByRegionCount;
064
065    int selectedIndex = -1;
066    double currentLargestRandom = -1;
067    for (int i = servers.length - 1; i >= 0; i--) {
068      if (servers[i] == null || servers[i] == thisServer) {
069        continue;
070      }
071      if (
072        selectedIndex != -1
073          && cluster.getNumRegionsComparator().compare(servers[i], servers[selectedIndex]) != 0
074      ) {
075        // Exhausted servers of the same region count
076        break;
077      }
078      // we don't know how many servers have the same region count, we will randomly select one
079      // using a simplified inline reservoir sampling by assignmening a random number to stream
080      // data and choose the greatest one. (http://gregable.com/2007/10/reservoir-sampling.html)
081      double currentRandom = ThreadLocalRandom.current().nextDouble();
082      if (currentRandom > currentLargestRandom) {
083        selectedIndex = i;
084        currentLargestRandom = currentRandom;
085      }
086    }
087    return selectedIndex == -1 ? -1 : servers[selectedIndex];
088  }
089
090}