hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From git-site-r...@apache.org
Subject [22/40] hbase-site git commit: Published site at 2aae247e3f8f8a393b403a82593bdc3a1ba81193.
Date Mon, 10 Sep 2018 14:53:12 GMT
http://git-wip-us.apache.org/repos/asf/hbase-site/blob/80652933/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.html b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.html
index af3b364..d974429 100644
--- a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.html
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.html
@@ -749,869 +749,894 @@
 <span class="sourceLineNo">741</span>      int region = regionsToIndex.get(regionInfo);<a name="line.741"></a>
 <span class="sourceLineNo">742</span><a name="line.742"></a>
 <span class="sourceLineNo">743</span>      int primary = regionIndexToPrimaryIndex[region];<a name="line.743"></a>
-<span class="sourceLineNo">744</span><a name="line.744"></a>
-<span class="sourceLineNo">745</span>      // there is a subset relation for server &lt; host &lt; rack<a name="line.745"></a>
-<span class="sourceLineNo">746</span>      // check server first<a name="line.746"></a>
-<span class="sourceLineNo">747</span><a name="line.747"></a>
-<span class="sourceLineNo">748</span>      if (contains(primariesOfRegionsPerServer[server], primary)) {<a name="line.748"></a>
-<span class="sourceLineNo">749</span>        // check for whether there are other servers that we can place this region<a name="line.749"></a>
-<span class="sourceLineNo">750</span>        for (int i = 0; i &lt; primariesOfRegionsPerServer.length; i++) {<a name="line.750"></a>
-<span class="sourceLineNo">751</span>          if (i != server &amp;&amp; !contains(primariesOfRegionsPerServer[i], primary)) {<a name="line.751"></a>
-<span class="sourceLineNo">752</span>            return true; // meaning there is a better server<a name="line.752"></a>
-<span class="sourceLineNo">753</span>          }<a name="line.753"></a>
-<span class="sourceLineNo">754</span>        }<a name="line.754"></a>
-<span class="sourceLineNo">755</span>        return false; // there is not a better server to place this<a name="line.755"></a>
-<span class="sourceLineNo">756</span>      }<a name="line.756"></a>
-<span class="sourceLineNo">757</span><a name="line.757"></a>
-<span class="sourceLineNo">758</span>      // check host<a name="line.758"></a>
-<span class="sourceLineNo">759</span>      if (multiServersPerHost) { // these arrays would only be allocated if we have more than one server per host<a name="line.759"></a>
-<span class="sourceLineNo">760</span>        int host = serverIndexToHostIndex[server];<a name="line.760"></a>
-<span class="sourceLineNo">761</span>        if (contains(primariesOfRegionsPerHost[host], primary)) {<a name="line.761"></a>
-<span class="sourceLineNo">762</span>          // check for whether there are other hosts that we can place this region<a name="line.762"></a>
-<span class="sourceLineNo">763</span>          for (int i = 0; i &lt; primariesOfRegionsPerHost.length; i++) {<a name="line.763"></a>
-<span class="sourceLineNo">764</span>            if (i != host &amp;&amp; !contains(primariesOfRegionsPerHost[i], primary)) {<a name="line.764"></a>
-<span class="sourceLineNo">765</span>              return true; // meaning there is a better host<a name="line.765"></a>
-<span class="sourceLineNo">766</span>            }<a name="line.766"></a>
-<span class="sourceLineNo">767</span>          }<a name="line.767"></a>
-<span class="sourceLineNo">768</span>          return false; // there is not a better host to place this<a name="line.768"></a>
-<span class="sourceLineNo">769</span>        }<a name="line.769"></a>
-<span class="sourceLineNo">770</span>      }<a name="line.770"></a>
-<span class="sourceLineNo">771</span><a name="line.771"></a>
-<span class="sourceLineNo">772</span>      // check rack<a name="line.772"></a>
-<span class="sourceLineNo">773</span>      if (numRacks &gt; 1) {<a name="line.773"></a>
-<span class="sourceLineNo">774</span>        int rack = serverIndexToRackIndex[server];<a name="line.774"></a>
-<span class="sourceLineNo">775</span>        if (contains(primariesOfRegionsPerRack[rack], primary)) {<a name="line.775"></a>
-<span class="sourceLineNo">776</span>          // check for whether there are other racks that we can place this region<a name="line.776"></a>
-<span class="sourceLineNo">777</span>          for (int i = 0; i &lt; primariesOfRegionsPerRack.length; i++) {<a name="line.777"></a>
-<span class="sourceLineNo">778</span>            if (i != rack &amp;&amp; !contains(primariesOfRegionsPerRack[i], primary)) {<a name="line.778"></a>
-<span class="sourceLineNo">779</span>              return true; // meaning there is a better rack<a name="line.779"></a>
-<span class="sourceLineNo">780</span>            }<a name="line.780"></a>
-<span class="sourceLineNo">781</span>          }<a name="line.781"></a>
-<span class="sourceLineNo">782</span>          return false; // there is not a better rack to place this<a name="line.782"></a>
-<span class="sourceLineNo">783</span>        }<a name="line.783"></a>
-<span class="sourceLineNo">784</span>      }<a name="line.784"></a>
-<span class="sourceLineNo">785</span>      return false;<a name="line.785"></a>
-<span class="sourceLineNo">786</span>    }<a name="line.786"></a>
-<span class="sourceLineNo">787</span><a name="line.787"></a>
-<span class="sourceLineNo">788</span>    void doAssignRegion(RegionInfo regionInfo, ServerName serverName) {<a name="line.788"></a>
-<span class="sourceLineNo">789</span>      if (!serversToIndex.containsKey(serverName.getHostAndPort())) {<a name="line.789"></a>
-<span class="sourceLineNo">790</span>        return;<a name="line.790"></a>
-<span class="sourceLineNo">791</span>      }<a name="line.791"></a>
-<span class="sourceLineNo">792</span>      int server = serversToIndex.get(serverName.getHostAndPort());<a name="line.792"></a>
-<span class="sourceLineNo">793</span>      int region = regionsToIndex.get(regionInfo);<a name="line.793"></a>
-<span class="sourceLineNo">794</span>      doAction(new AssignRegionAction(region, server));<a name="line.794"></a>
-<span class="sourceLineNo">795</span>    }<a name="line.795"></a>
-<span class="sourceLineNo">796</span><a name="line.796"></a>
-<span class="sourceLineNo">797</span>    void regionMoved(int region, int oldServer, int newServer) {<a name="line.797"></a>
-<span class="sourceLineNo">798</span>      regionIndexToServerIndex[region] = newServer;<a name="line.798"></a>
-<span class="sourceLineNo">799</span>      if (initialRegionIndexToServerIndex[region] == newServer) {<a name="line.799"></a>
-<span class="sourceLineNo">800</span>        numMovedRegions--; //region moved back to original location<a name="line.800"></a>
-<span class="sourceLineNo">801</span>      } else if (oldServer &gt;= 0 &amp;&amp; initialRegionIndexToServerIndex[region] == oldServer) {<a name="line.801"></a>
-<span class="sourceLineNo">802</span>        numMovedRegions++; //region moved from original location<a name="line.802"></a>
-<span class="sourceLineNo">803</span>      }<a name="line.803"></a>
-<span class="sourceLineNo">804</span>      int tableIndex = regionIndexToTableIndex[region];<a name="line.804"></a>
-<span class="sourceLineNo">805</span>      if (oldServer &gt;= 0) {<a name="line.805"></a>
-<span class="sourceLineNo">806</span>        numRegionsPerServerPerTable[oldServer][tableIndex]--;<a name="line.806"></a>
-<span class="sourceLineNo">807</span>      }<a name="line.807"></a>
-<span class="sourceLineNo">808</span>      numRegionsPerServerPerTable[newServer][tableIndex]++;<a name="line.808"></a>
-<span class="sourceLineNo">809</span><a name="line.809"></a>
-<span class="sourceLineNo">810</span>      //check whether this caused maxRegionsPerTable in the new Server to be updated<a name="line.810"></a>
-<span class="sourceLineNo">811</span>      if (numRegionsPerServerPerTable[newServer][tableIndex] &gt; numMaxRegionsPerTable[tableIndex]) {<a name="line.811"></a>
-<span class="sourceLineNo">812</span>        numMaxRegionsPerTable[tableIndex] = numRegionsPerServerPerTable[newServer][tableIndex];<a name="line.812"></a>
-<span class="sourceLineNo">813</span>      } else if (oldServer &gt;= 0 &amp;&amp; (numRegionsPerServerPerTable[oldServer][tableIndex] + 1)<a name="line.813"></a>
-<span class="sourceLineNo">814</span>          == numMaxRegionsPerTable[tableIndex]) {<a name="line.814"></a>
-<span class="sourceLineNo">815</span>        //recompute maxRegionsPerTable since the previous value was coming from the old server<a name="line.815"></a>
-<span class="sourceLineNo">816</span>        numMaxRegionsPerTable[tableIndex] = 0;<a name="line.816"></a>
-<span class="sourceLineNo">817</span>        for (int[] aNumRegionsPerServerPerTable : numRegionsPerServerPerTable) {<a name="line.817"></a>
-<span class="sourceLineNo">818</span>          if (aNumRegionsPerServerPerTable[tableIndex] &gt; numMaxRegionsPerTable[tableIndex]) {<a name="line.818"></a>
-<span class="sourceLineNo">819</span>            numMaxRegionsPerTable[tableIndex] = aNumRegionsPerServerPerTable[tableIndex];<a name="line.819"></a>
-<span class="sourceLineNo">820</span>          }<a name="line.820"></a>
-<span class="sourceLineNo">821</span>        }<a name="line.821"></a>
-<span class="sourceLineNo">822</span>      }<a name="line.822"></a>
-<span class="sourceLineNo">823</span><a name="line.823"></a>
-<span class="sourceLineNo">824</span>      // update for servers<a name="line.824"></a>
-<span class="sourceLineNo">825</span>      int primary = regionIndexToPrimaryIndex[region];<a name="line.825"></a>
-<span class="sourceLineNo">826</span>      if (oldServer &gt;= 0) {<a name="line.826"></a>
-<span class="sourceLineNo">827</span>        primariesOfRegionsPerServer[oldServer] = removeRegion(<a name="line.827"></a>
-<span class="sourceLineNo">828</span>          primariesOfRegionsPerServer[oldServer], primary);<a name="line.828"></a>
-<span class="sourceLineNo">829</span>      }<a name="line.829"></a>
-<span class="sourceLineNo">830</span>      primariesOfRegionsPerServer[newServer] = addRegionSorted(<a name="line.830"></a>
-<span class="sourceLineNo">831</span>        primariesOfRegionsPerServer[newServer], primary);<a name="line.831"></a>
-<span class="sourceLineNo">832</span><a name="line.832"></a>
-<span class="sourceLineNo">833</span>      // update for hosts<a name="line.833"></a>
-<span class="sourceLineNo">834</span>      if (multiServersPerHost) {<a name="line.834"></a>
-<span class="sourceLineNo">835</span>        int oldHost = oldServer &gt;= 0 ? serverIndexToHostIndex[oldServer] : -1;<a name="line.835"></a>
-<span class="sourceLineNo">836</span>        int newHost = serverIndexToHostIndex[newServer];<a name="line.836"></a>
-<span class="sourceLineNo">837</span>        if (newHost != oldHost) {<a name="line.837"></a>
-<span class="sourceLineNo">838</span>          regionsPerHost[newHost] = addRegion(regionsPerHost[newHost], region);<a name="line.838"></a>
-<span class="sourceLineNo">839</span>          primariesOfRegionsPerHost[newHost] = addRegionSorted(primariesOfRegionsPerHost[newHost], primary);<a name="line.839"></a>
-<span class="sourceLineNo">840</span>          if (oldHost &gt;= 0) {<a name="line.840"></a>
-<span class="sourceLineNo">841</span>            regionsPerHost[oldHost] = removeRegion(regionsPerHost[oldHost], region);<a name="line.841"></a>
-<span class="sourceLineNo">842</span>            primariesOfRegionsPerHost[oldHost] = removeRegion(<a name="line.842"></a>
-<span class="sourceLineNo">843</span>              primariesOfRegionsPerHost[oldHost], primary); // will still be sorted<a name="line.843"></a>
-<span class="sourceLineNo">844</span>          }<a name="line.844"></a>
-<span class="sourceLineNo">845</span>        }<a name="line.845"></a>
-<span class="sourceLineNo">846</span>      }<a name="line.846"></a>
-<span class="sourceLineNo">847</span><a name="line.847"></a>
-<span class="sourceLineNo">848</span>      // update for racks<a name="line.848"></a>
-<span class="sourceLineNo">849</span>      if (numRacks &gt; 1) {<a name="line.849"></a>
-<span class="sourceLineNo">850</span>        int oldRack = oldServer &gt;= 0 ? serverIndexToRackIndex[oldServer] : -1;<a name="line.850"></a>
-<span class="sourceLineNo">851</span>        int newRack = serverIndexToRackIndex[newServer];<a name="line.851"></a>
-<span class="sourceLineNo">852</span>        if (newRack != oldRack) {<a name="line.852"></a>
-<span class="sourceLineNo">853</span>          regionsPerRack[newRack] = addRegion(regionsPerRack[newRack], region);<a name="line.853"></a>
-<span class="sourceLineNo">854</span>          primariesOfRegionsPerRack[newRack] = addRegionSorted(primariesOfRegionsPerRack[newRack], primary);<a name="line.854"></a>
-<span class="sourceLineNo">855</span>          if (oldRack &gt;= 0) {<a name="line.855"></a>
-<span class="sourceLineNo">856</span>            regionsPerRack[oldRack] = removeRegion(regionsPerRack[oldRack], region);<a name="line.856"></a>
-<span class="sourceLineNo">857</span>            primariesOfRegionsPerRack[oldRack] = removeRegion(<a name="line.857"></a>
-<span class="sourceLineNo">858</span>              primariesOfRegionsPerRack[oldRack], primary); // will still be sorted<a name="line.858"></a>
-<span class="sourceLineNo">859</span>          }<a name="line.859"></a>
-<span class="sourceLineNo">860</span>        }<a name="line.860"></a>
-<span class="sourceLineNo">861</span>      }<a name="line.861"></a>
-<span class="sourceLineNo">862</span>    }<a name="line.862"></a>
-<span class="sourceLineNo">863</span><a name="line.863"></a>
-<span class="sourceLineNo">864</span>    int[] removeRegion(int[] regions, int regionIndex) {<a name="line.864"></a>
-<span class="sourceLineNo">865</span>      //TODO: this maybe costly. Consider using linked lists<a name="line.865"></a>
-<span class="sourceLineNo">866</span>      int[] newRegions = new int[regions.length - 1];<a name="line.866"></a>
-<span class="sourceLineNo">867</span>      int i = 0;<a name="line.867"></a>
-<span class="sourceLineNo">868</span>      for (i = 0; i &lt; regions.length; i++) {<a name="line.868"></a>
-<span class="sourceLineNo">869</span>        if (regions[i] == regionIndex) {<a name="line.869"></a>
-<span class="sourceLineNo">870</span>          break;<a name="line.870"></a>
-<span class="sourceLineNo">871</span>        }<a name="line.871"></a>
-<span class="sourceLineNo">872</span>        newRegions[i] = regions[i];<a name="line.872"></a>
-<span class="sourceLineNo">873</span>      }<a name="line.873"></a>
-<span class="sourceLineNo">874</span>      System.arraycopy(regions, i+1, newRegions, i, newRegions.length - i);<a name="line.874"></a>
-<span class="sourceLineNo">875</span>      return newRegions;<a name="line.875"></a>
-<span class="sourceLineNo">876</span>    }<a name="line.876"></a>
-<span class="sourceLineNo">877</span><a name="line.877"></a>
-<span class="sourceLineNo">878</span>    int[] addRegion(int[] regions, int regionIndex) {<a name="line.878"></a>
-<span class="sourceLineNo">879</span>      int[] newRegions = new int[regions.length + 1];<a name="line.879"></a>
-<span class="sourceLineNo">880</span>      System.arraycopy(regions, 0, newRegions, 0, regions.length);<a name="line.880"></a>
-<span class="sourceLineNo">881</span>      newRegions[newRegions.length - 1] = regionIndex;<a name="line.881"></a>
-<span class="sourceLineNo">882</span>      return newRegions;<a name="line.882"></a>
-<span class="sourceLineNo">883</span>    }<a name="line.883"></a>
-<span class="sourceLineNo">884</span><a name="line.884"></a>
-<span class="sourceLineNo">885</span>    int[] addRegionSorted(int[] regions, int regionIndex) {<a name="line.885"></a>
-<span class="sourceLineNo">886</span>      int[] newRegions = new int[regions.length + 1];<a name="line.886"></a>
-<span class="sourceLineNo">887</span>      int i = 0;<a name="line.887"></a>
-<span class="sourceLineNo">888</span>      for (i = 0; i &lt; regions.length; i++) { // find the index to insert<a name="line.888"></a>
-<span class="sourceLineNo">889</span>        if (regions[i] &gt; regionIndex) {<a name="line.889"></a>
-<span class="sourceLineNo">890</span>          break;<a name="line.890"></a>
-<span class="sourceLineNo">891</span>        }<a name="line.891"></a>
-<span class="sourceLineNo">892</span>      }<a name="line.892"></a>
-<span class="sourceLineNo">893</span>      System.arraycopy(regions, 0, newRegions, 0, i); // copy first half<a name="line.893"></a>
-<span class="sourceLineNo">894</span>      System.arraycopy(regions, i, newRegions, i+1, regions.length - i); // copy second half<a name="line.894"></a>
-<span class="sourceLineNo">895</span>      newRegions[i] = regionIndex;<a name="line.895"></a>
-<span class="sourceLineNo">896</span><a name="line.896"></a>
-<span class="sourceLineNo">897</span>      return newRegions;<a name="line.897"></a>
-<span class="sourceLineNo">898</span>    }<a name="line.898"></a>
-<span class="sourceLineNo">899</span><a name="line.899"></a>
-<span class="sourceLineNo">900</span>    int[] replaceRegion(int[] regions, int regionIndex, int newRegionIndex) {<a name="line.900"></a>
-<span class="sourceLineNo">901</span>      int i = 0;<a name="line.901"></a>
-<span class="sourceLineNo">902</span>      for (i = 0; i &lt; regions.length; i++) {<a name="line.902"></a>
-<span class="sourceLineNo">903</span>        if (regions[i] == regionIndex) {<a name="line.903"></a>
-<span class="sourceLineNo">904</span>          regions[i] = newRegionIndex;<a name="line.904"></a>
-<span class="sourceLineNo">905</span>          break;<a name="line.905"></a>
-<span class="sourceLineNo">906</span>        }<a name="line.906"></a>
-<span class="sourceLineNo">907</span>      }<a name="line.907"></a>
-<span class="sourceLineNo">908</span>      return regions;<a name="line.908"></a>
-<span class="sourceLineNo">909</span>    }<a name="line.909"></a>
-<span class="sourceLineNo">910</span><a name="line.910"></a>
-<span class="sourceLineNo">911</span>    void sortServersByRegionCount() {<a name="line.911"></a>
-<span class="sourceLineNo">912</span>      Arrays.sort(serverIndicesSortedByRegionCount, numRegionsComparator);<a name="line.912"></a>
-<span class="sourceLineNo">913</span>    }<a name="line.913"></a>
-<span class="sourceLineNo">914</span><a name="line.914"></a>
-<span class="sourceLineNo">915</span>    int getNumRegions(int server) {<a name="line.915"></a>
-<span class="sourceLineNo">916</span>      return regionsPerServer[server].length;<a name="line.916"></a>
-<span class="sourceLineNo">917</span>    }<a name="line.917"></a>
-<span class="sourceLineNo">918</span><a name="line.918"></a>
-<span class="sourceLineNo">919</span>    boolean contains(int[] arr, int val) {<a name="line.919"></a>
-<span class="sourceLineNo">920</span>      return Arrays.binarySearch(arr, val) &gt;= 0;<a name="line.920"></a>
-<span class="sourceLineNo">921</span>    }<a name="line.921"></a>
-<span class="sourceLineNo">922</span><a name="line.922"></a>
-<span class="sourceLineNo">923</span>    private Comparator&lt;Integer&gt; numRegionsComparator = Comparator.comparingInt(this::getNumRegions);<a name="line.923"></a>
-<span class="sourceLineNo">924</span><a name="line.924"></a>
-<span class="sourceLineNo">925</span>    int getLowestLocalityRegionOnServer(int serverIndex) {<a name="line.925"></a>
-<span class="sourceLineNo">926</span>      if (regionFinder != null) {<a name="line.926"></a>
-<span class="sourceLineNo">927</span>        float lowestLocality = 1.0f;<a name="line.927"></a>
-<span class="sourceLineNo">928</span>        int lowestLocalityRegionIndex = -1;<a name="line.928"></a>
-<span class="sourceLineNo">929</span>        if (regionsPerServer[serverIndex].length == 0) {<a name="line.929"></a>
-<span class="sourceLineNo">930</span>          // No regions on that region server<a name="line.930"></a>
-<span class="sourceLineNo">931</span>          return -1;<a name="line.931"></a>
-<span class="sourceLineNo">932</span>        }<a name="line.932"></a>
-<span class="sourceLineNo">933</span>        for (int j = 0; j &lt; regionsPerServer[serverIndex].length; j++) {<a name="line.933"></a>
-<span class="sourceLineNo">934</span>          int regionIndex = regionsPerServer[serverIndex][j];<a name="line.934"></a>
-<span class="sourceLineNo">935</span>          HDFSBlocksDistribution distribution = regionFinder<a name="line.935"></a>
-<span class="sourceLineNo">936</span>              .getBlockDistribution(regions[regionIndex]);<a name="line.936"></a>
-<span class="sourceLineNo">937</span>          float locality = distribution.getBlockLocalityIndex(servers[serverIndex].getHostname());<a name="line.937"></a>
-<span class="sourceLineNo">938</span>          // skip empty region<a name="line.938"></a>
-<span class="sourceLineNo">939</span>          if (distribution.getUniqueBlocksTotalWeight() == 0) {<a name="line.939"></a>
-<span class="sourceLineNo">940</span>            continue;<a name="line.940"></a>
-<span class="sourceLineNo">941</span>          }<a name="line.941"></a>
-<span class="sourceLineNo">942</span>          if (locality &lt; lowestLocality) {<a name="line.942"></a>
-<span class="sourceLineNo">943</span>            lowestLocality = locality;<a name="line.943"></a>
-<span class="sourceLineNo">944</span>            lowestLocalityRegionIndex = j;<a name="line.944"></a>
-<span class="sourceLineNo">945</span>          }<a name="line.945"></a>
-<span class="sourceLineNo">946</span>        }<a name="line.946"></a>
-<span class="sourceLineNo">947</span>        if (lowestLocalityRegionIndex == -1) {<a name="line.947"></a>
-<span class="sourceLineNo">948</span>          return -1;<a name="line.948"></a>
-<span class="sourceLineNo">949</span>        }<a name="line.949"></a>
-<span class="sourceLineNo">950</span>        if (LOG.isTraceEnabled()) {<a name="line.950"></a>
-<span class="sourceLineNo">951</span>          LOG.trace("Lowest locality region is "<a name="line.951"></a>
-<span class="sourceLineNo">952</span>              + regions[regionsPerServer[serverIndex][lowestLocalityRegionIndex]]<a name="line.952"></a>
-<span class="sourceLineNo">953</span>                  .getRegionNameAsString() + " with locality " + lowestLocality<a name="line.953"></a>
-<span class="sourceLineNo">954</span>              + " and its region server contains " + regionsPerServer[serverIndex].length<a name="line.954"></a>
-<span class="sourceLineNo">955</span>              + " regions");<a name="line.955"></a>
-<span class="sourceLineNo">956</span>        }<a name="line.956"></a>
-<span class="sourceLineNo">957</span>        return regionsPerServer[serverIndex][lowestLocalityRegionIndex];<a name="line.957"></a>
-<span class="sourceLineNo">958</span>      } else {<a name="line.958"></a>
-<span class="sourceLineNo">959</span>        return -1;<a name="line.959"></a>
-<span class="sourceLineNo">960</span>      }<a name="line.960"></a>
-<span class="sourceLineNo">961</span>    }<a name="line.961"></a>
-<span class="sourceLineNo">962</span><a name="line.962"></a>
-<span class="sourceLineNo">963</span>    float getLocalityOfRegion(int region, int server) {<a name="line.963"></a>
-<span class="sourceLineNo">964</span>      if (regionFinder != null) {<a name="line.964"></a>
-<span class="sourceLineNo">965</span>        HDFSBlocksDistribution distribution = regionFinder.getBlockDistribution(regions[region]);<a name="line.965"></a>
-<span class="sourceLineNo">966</span>        return distribution.getBlockLocalityIndex(servers[server].getHostname());<a name="line.966"></a>
-<span class="sourceLineNo">967</span>      } else {<a name="line.967"></a>
-<span class="sourceLineNo">968</span>        return 0f;<a name="line.968"></a>
-<span class="sourceLineNo">969</span>      }<a name="line.969"></a>
-<span class="sourceLineNo">970</span>    }<a name="line.970"></a>
-<span class="sourceLineNo">971</span><a name="line.971"></a>
-<span class="sourceLineNo">972</span>    @VisibleForTesting<a name="line.972"></a>
-<span class="sourceLineNo">973</span>    protected void setNumRegions(int numRegions) {<a name="line.973"></a>
-<span class="sourceLineNo">974</span>      this.numRegions = numRegions;<a name="line.974"></a>
-<span class="sourceLineNo">975</span>    }<a name="line.975"></a>
-<span class="sourceLineNo">976</span><a name="line.976"></a>
-<span class="sourceLineNo">977</span>    @VisibleForTesting<a name="line.977"></a>
-<span class="sourceLineNo">978</span>    protected void setNumMovedRegions(int numMovedRegions) {<a name="line.978"></a>
-<span class="sourceLineNo">979</span>      this.numMovedRegions = numMovedRegions;<a name="line.979"></a>
-<span class="sourceLineNo">980</span>    }<a name="line.980"></a>
-<span class="sourceLineNo">981</span><a name="line.981"></a>
-<span class="sourceLineNo">982</span>    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="SBSC_USE_STRINGBUFFER_CONCATENATION",<a name="line.982"></a>
-<span class="sourceLineNo">983</span>        justification="Not important but should be fixed")<a name="line.983"></a>
-<span class="sourceLineNo">984</span>    @Override<a name="line.984"></a>
-<span class="sourceLineNo">985</span>    public String toString() {<a name="line.985"></a>
-<span class="sourceLineNo">986</span>      StringBuilder desc = new StringBuilder("Cluster={servers=[");<a name="line.986"></a>
-<span class="sourceLineNo">987</span>      for(ServerName sn:servers) {<a name="line.987"></a>
-<span class="sourceLineNo">988</span>        desc.append(sn.getHostAndPort()).append(", ");<a name="line.988"></a>
-<span class="sourceLineNo">989</span>      }<a name="line.989"></a>
-<span class="sourceLineNo">990</span>      desc.append("], serverIndicesSortedByRegionCount=")<a name="line.990"></a>
-<span class="sourceLineNo">991</span>          .append(Arrays.toString(serverIndicesSortedByRegionCount))<a name="line.991"></a>
-<span class="sourceLineNo">992</span>          .append(", regionsPerServer=").append(Arrays.deepToString(regionsPerServer));<a name="line.992"></a>
-<span class="sourceLineNo">993</span><a name="line.993"></a>
-<span class="sourceLineNo">994</span>      desc.append(", numMaxRegionsPerTable=").append(Arrays.toString(numMaxRegionsPerTable))<a name="line.994"></a>
-<span class="sourceLineNo">995</span>          .append(", numRegions=").append(numRegions).append(", numServers=").append(numServers)<a name="line.995"></a>
-<span class="sourceLineNo">996</span>          .append(", numTables=").append(numTables).append(", numMovedRegions=")<a name="line.996"></a>
-<span class="sourceLineNo">997</span>          .append(numMovedRegions).append('}');<a name="line.997"></a>
-<span class="sourceLineNo">998</span>      return desc.toString();<a name="line.998"></a>
-<span class="sourceLineNo">999</span>    }<a name="line.999"></a>
-<span class="sourceLineNo">1000</span>  }<a name="line.1000"></a>
-<span class="sourceLineNo">1001</span><a name="line.1001"></a>
-<span class="sourceLineNo">1002</span>  // slop for regions<a name="line.1002"></a>
-<span class="sourceLineNo">1003</span>  protected float slop;<a name="line.1003"></a>
-<span class="sourceLineNo">1004</span>  // overallSlop to control simpleLoadBalancer's cluster level threshold<a name="line.1004"></a>
-<span class="sourceLineNo">1005</span>  protected float overallSlop;<a name="line.1005"></a>
-<span class="sourceLineNo">1006</span>  protected Configuration config = HBaseConfiguration.create();<a name="line.1006"></a>
-<span class="sourceLineNo">1007</span>  protected RackManager rackManager;<a name="line.1007"></a>
-<span class="sourceLineNo">1008</span>  private static final Random RANDOM = new Random(System.currentTimeMillis());<a name="line.1008"></a>
-<span class="sourceLineNo">1009</span>  private static final Logger LOG = LoggerFactory.getLogger(BaseLoadBalancer.class);<a name="line.1009"></a>
-<span class="sourceLineNo">1010</span>  protected MetricsBalancer metricsBalancer = null;<a name="line.1010"></a>
-<span class="sourceLineNo">1011</span>  protected ClusterMetrics clusterStatus = null;<a name="line.1011"></a>
-<span class="sourceLineNo">1012</span>  protected ServerName masterServerName;<a name="line.1012"></a>
-<span class="sourceLineNo">1013</span>  protected MasterServices services;<a name="line.1013"></a>
-<span class="sourceLineNo">1014</span>  protected boolean tablesOnMaster;<a name="line.1014"></a>
-<span class="sourceLineNo">1015</span>  protected boolean onlySystemTablesOnMaster;<a name="line.1015"></a>
-<span class="sourceLineNo">1016</span><a name="line.1016"></a>
-<span class="sourceLineNo">1017</span>  @Override<a name="line.1017"></a>
-<span class="sourceLineNo">1018</span>  public void setConf(Configuration conf) {<a name="line.1018"></a>
-<span class="sourceLineNo">1019</span>    this.config = conf;<a name="line.1019"></a>
-<span class="sourceLineNo">1020</span>    setSlop(conf);<a name="line.1020"></a>
-<span class="sourceLineNo">1021</span>    if (slop &lt; 0) slop = 0;<a name="line.1021"></a>
-<span class="sourceLineNo">1022</span>    else if (slop &gt; 1) slop = 1;<a name="line.1022"></a>
-<span class="sourceLineNo">1023</span><a name="line.1023"></a>
-<span class="sourceLineNo">1024</span>    if (overallSlop &lt; 0) overallSlop = 0;<a name="line.1024"></a>
-<span class="sourceLineNo">1025</span>    else if (overallSlop &gt; 1) overallSlop = 1;<a name="line.1025"></a>
-<span class="sourceLineNo">1026</span><a name="line.1026"></a>
-<span class="sourceLineNo">1027</span>    this.tablesOnMaster = LoadBalancer.isTablesOnMaster(this.config);<a name="line.1027"></a>
-<span class="sourceLineNo">1028</span>    this.onlySystemTablesOnMaster = LoadBalancer.isSystemTablesOnlyOnMaster(this.config);<a name="line.1028"></a>
-<span class="sourceLineNo">1029</span>    // If system tables on master, implies tablesOnMaster = true.<a name="line.1029"></a>
-<span class="sourceLineNo">1030</span>    if (this.onlySystemTablesOnMaster &amp;&amp; !this.tablesOnMaster) {<a name="line.1030"></a>
-<span class="sourceLineNo">1031</span>      LOG.warn("Set " + TABLES_ON_MASTER + "=true because " + SYSTEM_TABLES_ON_MASTER + "=true");<a name="line.1031"></a>
-<span class="sourceLineNo">1032</span>      this.tablesOnMaster = true;<a name="line.1032"></a>
-<span class="sourceLineNo">1033</span>    }<a name="line.1033"></a>
-<span class="sourceLineNo">1034</span>    this.rackManager = new RackManager(getConf());<a name="line.1034"></a>
-<span class="sourceLineNo">1035</span>    if (useRegionFinder) {<a name="line.1035"></a>
-<span class="sourceLineNo">1036</span>      regionFinder.setConf(conf);<a name="line.1036"></a>
-<span class="sourceLineNo">1037</span>    }<a name="line.1037"></a>
-<span class="sourceLineNo">1038</span>    // Print out base configs. Don't print overallSlop since it for simple balancer exclusively.<a name="line.1038"></a>
-<span class="sourceLineNo">1039</span>    LOG.info("slop=" + this.slop + ", tablesOnMaster=" + this.tablesOnMaster +<a name="line.1039"></a>
-<span class="sourceLineNo">1040</span>      ", systemTablesOnMaster=" + this.onlySystemTablesOnMaster);<a name="line.1040"></a>
-<span class="sourceLineNo">1041</span>  }<a name="line.1041"></a>
-<span class="sourceLineNo">1042</span><a name="line.1042"></a>
-<span class="sourceLineNo">1043</span>  protected void setSlop(Configuration conf) {<a name="line.1043"></a>
-<span class="sourceLineNo">1044</span>    this.slop = conf.getFloat("hbase.regions.slop", (float) 0.2);<a name="line.1044"></a>
-<span class="sourceLineNo">1045</span>    this.overallSlop = conf.getFloat("hbase.regions.overallSlop", slop);<a name="line.1045"></a>
-<span class="sourceLineNo">1046</span>  }<a name="line.1046"></a>
-<span class="sourceLineNo">1047</span><a name="line.1047"></a>
-<span class="sourceLineNo">1048</span>  /**<a name="line.1048"></a>
-<span class="sourceLineNo">1049</span>   * Check if a region belongs to some system table.<a name="line.1049"></a>
-<span class="sourceLineNo">1050</span>   * If so, the primary replica may be expected to be put on the master regionserver.<a name="line.1050"></a>
-<span class="sourceLineNo">1051</span>   */<a name="line.1051"></a>
-<span class="sourceLineNo">1052</span>  public boolean shouldBeOnMaster(RegionInfo region) {<a name="line.1052"></a>
-<span class="sourceLineNo">1053</span>    return this.onlySystemTablesOnMaster &amp;&amp; region.getTable().isSystemTable();<a name="line.1053"></a>
-<span class="sourceLineNo">1054</span>  }<a name="line.1054"></a>
-<span class="sourceLineNo">1055</span><a name="line.1055"></a>
-<span class="sourceLineNo">1056</span>  /**<a name="line.1056"></a>
-<span class="sourceLineNo">1057</span>   * Balance the regions that should be on master regionserver.<a name="line.1057"></a>
-<span class="sourceLineNo">1058</span>   */<a name="line.1058"></a>
-<span class="sourceLineNo">1059</span>  protected List&lt;RegionPlan&gt; balanceMasterRegions(Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; clusterMap) {<a name="line.1059"></a>
-<span class="sourceLineNo">1060</span>    if (masterServerName == null || clusterMap == null || clusterMap.size() &lt;= 1) return null;<a name="line.1060"></a>
-<span class="sourceLineNo">1061</span>    List&lt;RegionPlan&gt; plans = null;<a name="line.1061"></a>
-<span class="sourceLineNo">1062</span>    List&lt;RegionInfo&gt; regions = clusterMap.get(masterServerName);<a name="line.1062"></a>
-<span class="sourceLineNo">1063</span>    if (regions != null) {<a name="line.1063"></a>
-<span class="sourceLineNo">1064</span>      Iterator&lt;ServerName&gt; keyIt = null;<a name="line.1064"></a>
-<span class="sourceLineNo">1065</span>      for (RegionInfo region: regions) {<a name="line.1065"></a>
-<span class="sourceLineNo">1066</span>        if (shouldBeOnMaster(region)) continue;<a name="line.1066"></a>
-<span class="sourceLineNo">1067</span><a name="line.1067"></a>
-<span class="sourceLineNo">1068</span>        // Find a non-master regionserver to host the region<a name="line.1068"></a>
-<span class="sourceLineNo">1069</span>        if (keyIt == null || !keyIt.hasNext()) {<a name="line.1069"></a>
-<span class="sourceLineNo">1070</span>          keyIt = clusterMap.keySet().iterator();<a name="line.1070"></a>
-<span class="sourceLineNo">1071</span>        }<a name="line.1071"></a>
-<span class="sourceLineNo">1072</span>        ServerName dest = keyIt.next();<a name="line.1072"></a>
-<span class="sourceLineNo">1073</span>        if (masterServerName.equals(dest)) {<a name="line.1073"></a>
-<span class="sourceLineNo">1074</span>          if (!keyIt.hasNext()) {<a name="line.1074"></a>
-<span class="sourceLineNo">1075</span>            keyIt = clusterMap.keySet().iterator();<a name="line.1075"></a>
-<span class="sourceLineNo">1076</span>          }<a name="line.1076"></a>
-<span class="sourceLineNo">1077</span>          dest = keyIt.next();<a name="line.1077"></a>
-<span class="sourceLineNo">1078</span>        }<a name="line.1078"></a>
-<span class="sourceLineNo">1079</span><a name="line.1079"></a>
-<span class="sourceLineNo">1080</span>        // Move this region away from the master regionserver<a name="line.1080"></a>
-<span class="sourceLineNo">1081</span>        RegionPlan plan = new RegionPlan(region, masterServerName, dest);<a name="line.1081"></a>
-<span class="sourceLineNo">1082</span>        if (plans == null) {<a name="line.1082"></a>
-<span class="sourceLineNo">1083</span>          plans = new ArrayList&lt;&gt;();<a name="line.1083"></a>
-<span class="sourceLineNo">1084</span>        }<a name="line.1084"></a>
-<span class="sourceLineNo">1085</span>        plans.add(plan);<a name="line.1085"></a>
-<span class="sourceLineNo">1086</span>      }<a name="line.1086"></a>
-<span class="sourceLineNo">1087</span>    }<a name="line.1087"></a>
-<span class="sourceLineNo">1088</span>    for (Map.Entry&lt;ServerName, List&lt;RegionInfo&gt;&gt; server: clusterMap.entrySet()) {<a name="line.1088"></a>
-<span class="sourceLineNo">1089</span>      if (masterServerName.equals(server.getKey())) continue;<a name="line.1089"></a>
-<span class="sourceLineNo">1090</span>      for (RegionInfo region: server.getValue()) {<a name="line.1090"></a>
-<span class="sourceLineNo">1091</span>        if (!shouldBeOnMaster(region)) continue;<a name="line.1091"></a>
-<span class="sourceLineNo">1092</span><a name="line.1092"></a>
-<span class="sourceLineNo">1093</span>        // Move this region to the master regionserver<a name="line.1093"></a>
-<span class="sourceLineNo">1094</span>        RegionPlan plan = new RegionPlan(region, server.getKey(), masterServerName);<a name="line.1094"></a>
-<span class="sourceLineNo">1095</span>        if (plans == null) {<a name="line.1095"></a>
-<span class="sourceLineNo">1096</span>          plans = new ArrayList&lt;&gt;();<a name="line.1096"></a>
-<span class="sourceLineNo">1097</span>        }<a name="line.1097"></a>
-<span class="sourceLineNo">1098</span>        plans.add(plan);<a name="line.1098"></a>
-<span class="sourceLineNo">1099</span>      }<a name="line.1099"></a>
-<span class="sourceLineNo">1100</span>    }<a name="line.1100"></a>
-<span class="sourceLineNo">1101</span>    return plans;<a name="line.1101"></a>
-<span class="sourceLineNo">1102</span>  }<a name="line.1102"></a>
-<span class="sourceLineNo">1103</span><a name="line.1103"></a>
-<span class="sourceLineNo">1104</span>  /**<a name="line.1104"></a>
-<span class="sourceLineNo">1105</span>   * If master is configured to carry system tables only, in here is<a name="line.1105"></a>
-<span class="sourceLineNo">1106</span>   * where we figure what to assign it.<a name="line.1106"></a>
-<span class="sourceLineNo">1107</span>   */<a name="line.1107"></a>
-<span class="sourceLineNo">1108</span>  protected Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignMasterSystemRegions(<a name="line.1108"></a>
-<span class="sourceLineNo">1109</span>      Collection&lt;RegionInfo&gt; regions, List&lt;ServerName&gt; servers) {<a name="line.1109"></a>
-<span class="sourceLineNo">1110</span>    if (servers == null || regions == null || regions.isEmpty()) {<a name="line.1110"></a>
-<span class="sourceLineNo">1111</span>      return null;<a name="line.1111"></a>
-<span class="sourceLineNo">1112</span>    }<a name="line.1112"></a>
-<span class="sourceLineNo">1113</span>    Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignments = new TreeMap&lt;&gt;();<a name="line.1113"></a>
-<span class="sourceLineNo">1114</span>    if (this.onlySystemTablesOnMaster) {<a name="line.1114"></a>
-<span class="sourceLineNo">1115</span>      if (masterServerName != null &amp;&amp; servers.contains(masterServerName)) {<a name="line.1115"></a>
-<span class="sourceLineNo">1116</span>        assignments.put(masterServerName, new ArrayList&lt;&gt;());<a name="line.1116"></a>
-<span class="sourceLineNo">1117</span>        for (RegionInfo region : regions) {<a name="line.1117"></a>
-<span class="sourceLineNo">1118</span>          if (shouldBeOnMaster(region)) {<a name="line.1118"></a>
-<span class="sourceLineNo">1119</span>            assignments.get(masterServerName).add(region);<a name="line.1119"></a>
-<span class="sourceLineNo">1120</span>          }<a name="line.1120"></a>
-<span class="sourceLineNo">1121</span>        }<a name="line.1121"></a>
-<span class="sourceLineNo">1122</span>      }<a name="line.1122"></a>
-<span class="sourceLineNo">1123</span>    }<a name="line.1123"></a>
-<span class="sourceLineNo">1124</span>    return assignments;<a name="line.1124"></a>
-<span class="sourceLineNo">1125</span>  }<a name="line.1125"></a>
-<span class="sourceLineNo">1126</span><a name="line.1126"></a>
-<span class="sourceLineNo">1127</span>  @Override<a name="line.1127"></a>
-<span class="sourceLineNo">1128</span>  public Configuration getConf() {<a name="line.1128"></a>
-<span class="sourceLineNo">1129</span>    return this.config;<a name="line.1129"></a>
-<span class="sourceLineNo">1130</span>  }<a name="line.1130"></a>
-<span class="sourceLineNo">1131</span><a name="line.1131"></a>
-<span class="sourceLineNo">1132</span>  @Override<a name="line.1132"></a>
-<span class="sourceLineNo">1133</span>  public synchronized void setClusterMetrics(ClusterMetrics st) {<a name="line.1133"></a>
-<span class="sourceLineNo">1134</span>    this.clusterStatus = st;<a name="line.1134"></a>
-<span class="sourceLineNo">1135</span>    if (useRegionFinder) {<a name="line.1135"></a>
-<span class="sourceLineNo">1136</span>      regionFinder.setClusterMetrics(st);<a name="line.1136"></a>
-<span class="sourceLineNo">1137</span>    }<a name="line.1137"></a>
-<span class="sourceLineNo">1138</span>  }<a name="line.1138"></a>
-<span class="sourceLineNo">1139</span><a name="line.1139"></a>
-<span class="sourceLineNo">1140</span>  @Override<a name="line.1140"></a>
-<span class="sourceLineNo">1141</span>  public void setClusterLoad(Map&lt;TableName, Map&lt;ServerName, List&lt;RegionInfo&gt;&gt;&gt; clusterLoad){<a name="line.1141"></a>
-<span class="sourceLineNo">1142</span><a name="line.1142"></a>
-<span class="sourceLineNo">1143</span>  }<a name="line.1143"></a>
-<span class="sourceLineNo">1144</span><a name="line.1144"></a>
-<span class="sourceLineNo">1145</span>  @Override<a name="line.1145"></a>
-<span class="sourceLineNo">1146</span>  public void setMasterServices(MasterServices masterServices) {<a name="line.1146"></a>
-<span class="sourceLineNo">1147</span>    masterServerName = masterServices.getServerName();<a name="line.1147"></a>
-<span class="sourceLineNo">1148</span>    this.services = masterServices;<a name="line.1148"></a>
-<span class="sourceLineNo">1149</span>    if (useRegionFinder) {<a name="line.1149"></a>
-<span class="sourceLineNo">1150</span>      this.regionFinder.setServices(masterServices);<a name="line.1150"></a>
-<span class="sourceLineNo">1151</span>    }<a name="line.1151"></a>
-<span class="sourceLineNo">1152</span>  }<a name="line.1152"></a>
-<span class="sourceLineNo">1153</span><a name="line.1153"></a>
-<span class="sourceLineNo">1154</span>  @Override<a name="line.1154"></a>
-<span class="sourceLineNo">1155</span>  public void postMasterStartupInitialize() {<a name="line.1155"></a>
-<span class="sourceLineNo">1156</span>    if (services != null &amp;&amp; regionFinder != null) {<a name="line.1156"></a>
-<span class="sourceLineNo">1157</span>      try {<a name="line.1157"></a>
-<span class="sourceLineNo">1158</span>        Set&lt;RegionInfo&gt; regions =<a name="line.1158"></a>
-<span class="sourceLineNo">1159</span>            services.getAssignmentManager().getRegionStates().getRegionAssignments().keySet();<a name="line.1159"></a>
-<span class="sourceLineNo">1160</span>        regionFinder.refreshAndWait(regions);<a name="line.1160"></a>
-<span class="sourceLineNo">1161</span>      } catch (Exception e) {<a name="line.1161"></a>
-<span class="sourceLineNo">1162</span>        LOG.warn("Refreshing region HDFS Block dist failed with exception, ignoring", e);<a name="line.1162"></a>
-<span class="sourceLineNo">1163</span>      }<a name="line.1163"></a>
-<span class="sourceLineNo">1164</span>    }<a name="line.1164"></a>
-<span class="sourceLineNo">1165</span>  }<a name="line.1165"></a>
-<span class="sourceLineNo">1166</span><a name="line.1166"></a>
-<span class="sourceLineNo">1167</span>  public void setRackManager(RackManager rackManager) {<a name="line.1167"></a>
-<span class="sourceLineNo">1168</span>    this.rackManager = rackManager;<a name="line.1168"></a>
-<span class="sourceLineNo">1169</span>  }<a name="line.1169"></a>
-<span class="sourceLineNo">1170</span><a name="line.1170"></a>
-<span class="sourceLineNo">1171</span>  protected boolean needsBalance(Cluster c) {<a name="line.1171"></a>
-<span class="sourceLineNo">1172</span>    ClusterLoadState cs = new ClusterLoadState(c.clusterState);<a name="line.1172"></a>
-<span class="sourceLineNo">1173</span>    if (cs.getNumServers() &lt; MIN_SERVER_BALANCE) {<a name="line.1173"></a>
-<span class="sourceLineNo">1174</span>      if (LOG.isDebugEnabled()) {<a name="line.1174"></a>
-<span class="sourceLineNo">1175</span>        LOG.debug("Not running balancer because only " + cs.getNumServers()<a name="line.1175"></a>
-<span class="sourceLineNo">1176</span>            + " active regionserver(s)");<a name="line.1176"></a>
-<span class="sourceLineNo">1177</span>      }<a name="line.1177"></a>
-<span class="sourceLineNo">1178</span>      return false;<a name="line.1178"></a>
-<span class="sourceLineNo">1179</span>    }<a name="line.1179"></a>
-<span class="sourceLineNo">1180</span>    if(areSomeRegionReplicasColocated(c)) return true;<a name="line.1180"></a>
-<span class="sourceLineNo">1181</span>    // Check if we even need to do any load balancing<a name="line.1181"></a>
-<span class="sourceLineNo">1182</span>    // HBASE-3681 check sloppiness first<a name="line.1182"></a>
-<span class="sourceLineNo">1183</span>    float average = cs.getLoadAverage(); // for logging<a name="line.1183"></a>
-<span class="sourceLineNo">1184</span>    int floor = (int) Math.floor(average * (1 - slop));<a name="line.1184"></a>
-<span class="sourceLineNo">1185</span>    int ceiling = (int) Math.ceil(average * (1 + slop));<a name="line.1185"></a>
-<span class="sourceLineNo">1186</span>    if (!(cs.getMaxLoad() &gt; ceiling || cs.getMinLoad() &lt; floor)) {<a name="line.1186"></a>
-<span class="sourceLineNo">1187</span>      NavigableMap&lt;ServerAndLoad, List&lt;RegionInfo&gt;&gt; serversByLoad = cs.getServersByLoad();<a name="line.1187"></a>
-<span class="sourceLineNo">1188</span>      if (LOG.isTraceEnabled()) {<a name="line.1188"></a>
-<span class="sourceLineNo">1189</span>        // If nothing to balance, then don't say anything unless trace-level logging.<a name="line.1189"></a>
-<span class="sourceLineNo">1190</span>        LOG.trace("Skipping load balancing because balanced cluster; " +<a name="line.1190"></a>
-<span class="sourceLineNo">1191</span>          "servers=" + cs.getNumServers() +<a name="line.1191"></a>
-<span class="sourceLineNo">1192</span>          " regions=" + cs.getNumRegions() + " average=" + average +<a name="line.1192"></a>
-<span class="sourceLineNo">1193</span>          " mostloaded=" + serversByLoad.lastKey().getLoad() +<a name="line.1193"></a>
-<span class="sourceLineNo">1194</span>          " leastloaded=" + serversByLoad.firstKey().getLoad());<a name="line.1194"></a>
-<span class="sourceLineNo">1195</span>      }<a name="line.1195"></a>
-<span class="sourceLineNo">1196</span>      return false;<a name="line.1196"></a>
-<span class="sourceLineNo">1197</span>    }<a name="line.1197"></a>
-<span class="sourceLineNo">1198</span>    return true;<a name="line.1198"></a>
-<span class="sourceLineNo">1199</span>  }<a name="line.1199"></a>
-<span class="sourceLineNo">1200</span><a name="line.1200"></a>
-<span class="sourceLineNo">1201</span>  /**<a name="line.1201"></a>
-<span class="sourceLineNo">1202</span>   * Subclasses should implement this to return true if the cluster has nodes that hosts<a name="line.1202"></a>
-<span class="sourceLineNo">1203</span>   * multiple replicas for the same region, or, if there are multiple racks and the same<a name="line.1203"></a>
-<span class="sourceLineNo">1204</span>   * rack hosts replicas of the same region<a name="line.1204"></a>
-<span class="sourceLineNo">1205</span>   * @param c Cluster information<a name="line.1205"></a>
-<span class="sourceLineNo">1206</span>   * @return whether region replicas are currently co-located<a name="line.1206"></a>
-<span class="sourceLineNo">1207</span>   */<a name="line.1207"></a>
-<span class="sourceLineNo">1208</span>  protected boolean areSomeRegionReplicasColocated(Cluster c) {<a name="line.1208"></a>
-<span class="sourceLineNo">1209</span>    return false;<a name="line.1209"></a>
-<span class="sourceLineNo">1210</span>  }<a name="line.1210"></a>
-<span class="sourceLineNo">1211</span><a name="line.1211"></a>
-<span class="sourceLineNo">1212</span>  /**<a name="line.1212"></a>
-<span class="sourceLineNo">1213</span>   * Generates a bulk assignment plan to be used on cluster startup using a<a name="line.1213"></a>
-<span class="sourceLineNo">1214</span>   * simple round-robin assignment.<a name="line.1214"></a>
-<span class="sourceLineNo">1215</span>   * &lt;p&gt;<a name="line.1215"></a>
-<span class="sourceLineNo">1216</span>   * Takes a list of all the regions and all the servers in the cluster and<a name="line.1216"></a>
-<span class="sourceLineNo">1217</span>   * returns a map of each server to the regions that it should be assigned.<a name="line.1217"></a>
-<span class="sourceLineNo">1218</span>   * &lt;p&gt;<a name="line.1218"></a>
-<span class="sourceLineNo">1219</span>   * Currently implemented as a round-robin assignment. Same invariant as load<a name="line.1219"></a>
-<span class="sourceLineNo">1220</span>   * balancing, all servers holding floor(avg) or ceiling(avg).<a name="line.1220"></a>
-<span class="sourceLineNo">1221</span>   *<a name="line.1221"></a>
-<span class="sourceLineNo">1222</span>   * TODO: Use block locations from HDFS to place regions with their blocks<a name="line.1222"></a>
-<span class="sourceLineNo">1223</span>   *<a name="line.1223"></a>
-<span class="sourceLineNo">1224</span>   * @param regions all regions<a name="line.1224"></a>
-<span class="sourceLineNo">1225</span>   * @param servers all servers<a name="line.1225"></a>
-<span class="sourceLineNo">1226</span>   * @return map of server to the regions it should take, or null if no<a name="line.1226"></a>
-<span class="sourceLineNo">1227</span>   *         assignment is possible (ie. no regions or no servers)<a name="line.1227"></a>
-<span class="sourceLineNo">1228</span>   */<a name="line.1228"></a>
-<span class="sourceLineNo">1229</span>  @Override<a name="line.1229"></a>
-<span class="sourceLineNo">1230</span>  public Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; roundRobinAssignment(List&lt;RegionInfo&gt; regions,<a name="line.1230"></a>
-<span class="sourceLineNo">1231</span>      List&lt;ServerName&gt; servers) throws HBaseIOException {<a name="line.1231"></a>
-<span class="sourceLineNo">1232</span>    metricsBalancer.incrMiscInvocations();<a name="line.1232"></a>
-<span class="sourceLineNo">1233</span>    Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignments = assignMasterSystemRegions(regions, servers);<a name="line.1233"></a>
-<span class="sourceLineNo">1234</span>    if (assignments != null &amp;&amp; !assignments.isEmpty()) {<a name="line.1234"></a>
-<span class="sourceLineNo">1235</span>      servers = new ArrayList&lt;&gt;(servers);<a name="line.1235"></a>
-<span class="sourceLineNo">1236</span>      // Guarantee not to put other regions on master<a name="line.1236"></a>
-<span class="sourceLineNo">1237</span>      servers.remove(masterServerName);<a name="line.1237"></a>
-<span class="sourceLineNo">1238</span>      List&lt;RegionInfo&gt; masterRegions = assignments.get(masterServerName);<a name="line.1238"></a>
-<span class="sourceLineNo">1239</span>      if (!masterRegions.isEmpty()) {<a name="line.1239"></a>
-<span class="sourceLineNo">1240</span>        regions = new ArrayList&lt;&gt;(regions);<a name="line.1240"></a>
-<span class="sourceLineNo">1241</span>        regions.removeAll(masterRegions);<a name="line.1241"></a>
-<span class="sourceLineNo">1242</span>      }<a name="line.1242"></a>
-<span class="sourceLineNo">1243</span>    }<a name="line.1243"></a>
-<span class="sourceLineNo">1244</span>    if (regions == null || regions.isEmpty()) {<a name="line.1244"></a>
-<span class="sourceLineNo">1245</span>      return assignments;<a name="line.1245"></a>
-<span class="sourceLineNo">1246</span>    }<a name="line.1246"></a>
-<span class="sourceLineNo">1247</span><a name="line.1247"></a>
-<span class="sourceLineNo">1248</span>    int numServers = servers == null ? 0 : servers.size();<a name="line.1248"></a>
-<span class="sourceLineNo">1249</span>    if (numServers == 0) {<a name="line.1249"></a>
-<span class="sourceLineNo">1250</span>      LOG.warn("Wanted to do round robin assignment but no servers to assign to");<a name="line.1250"></a>
-<span class="sourceLineNo">1251</span>      return null;<a name="line.1251"></a>
-<span class="sourceLineNo">1252</span>    }<a name="line.1252"></a>
-<span class="sourceLineNo">1253</span><a name="line.1253"></a>
-<span class="sourceLineNo">1254</span>    // TODO: instead of retainAssignment() and roundRobinAssignment(), we should just run the<a name="line.1254"></a>
-<span class="sourceLineNo">1255</span>    // normal LB.balancerCluster() with unassignedRegions. We only need to have a candidate<a name="line.1255"></a>
-<span class="sourceLineNo">1256</span>    // generator for AssignRegionAction. The LB will ensure the regions are mostly local<a name="line.1256"></a>
-<span class="sourceLineNo">1257</span>    // and balanced. This should also run fast with fewer number of iterations.<a name="line.1257"></a>
-<span class="sourceLineNo">1258</span><a name="line.1258"></a>
-<span class="sourceLineNo">1259</span>    if (numServers == 1) { // Only one server, nothing fancy we can do here<a name="line.1259"></a>
-<span class="sourceLineNo">1260</span>      ServerName server = servers.get(0);<a name="line.1260"></a>
-<span class="sourceLineNo">1261</span>      assignments.put(server, new ArrayList&lt;&gt;(regions));<a name="line.1261"></a>
-<span class="sourceLineNo">1262</span>      return assignments;<a name="line.1262"></a>
-<span class="sourceLineNo">1263</span>    }<a name="line.1263"></a>
-<span class="sourceLineNo">1264</span><a name="line.1264"></a>
-<span class="sourceLineNo">1265</span>    Cluster cluster = createCluster(servers, regions);<a name="line.1265"></a>
-<span class="sourceLineNo">1266</span>    List&lt;RegionInfo&gt; unassignedRegions = new ArrayList&lt;&gt;();<a name="line.1266"></a>
-<span class="sourceLineNo">1267</span><a name="line.1267"></a>
-<span class="sourceLineNo">1268</span>    roundRobinAssignment(cluster, regions, unassignedRegions,<a name="line.1268"></a>
-<span class="sourceLineNo">1269</span>      servers, assignments);<a name="line.1269"></a>
-<span class="sourceLineNo">1270</span><a name="line.1270"></a>
-<span class="sourceLineNo">1271</span>    List&lt;RegionInfo&gt; lastFewRegions = new ArrayList&lt;&gt;();<a name="line.1271"></a>
-<span class="sourceLineNo">1272</span>    // assign the remaining by going through the list and try to assign to servers one-by-one<a name="line.1272"></a>
-<span class="sourceLineNo">1273</span>    int serverIdx = RANDOM.nextInt(numServers);<a name="line.1273"></a>
-<span class="sourceLineNo">1274</span>    OUTER : for (RegionInfo region : unassignedRegions) {<a name="line.1274"></a>
-<span class="sourceLineNo">1275</span>      boolean assigned = false;<a name="line.1275"></a>
-<span class="sourceLineNo">1276</span>      INNER : for (int j = 0; j &lt; numServers; j++) { // try all servers one by one<a name="line.1276"></a>
-<span class="sourceLineNo">1277</span>        ServerName serverName = servers.get((j + serverIdx) % numServers);<a name="line.1277"></a>
-<span class="sourceLineNo">1278</span>        if (!cluster.wouldLowerAvailability(region, serverName)) {<a name="line.1278"></a>
-<span class="sourceLineNo">1279</span>          List&lt;RegionInfo&gt; serverRegions =<a name="line.1279"></a>
-<span class="sourceLineNo">1280</span>              assignments.computeIfAbsent(serverName, k -&gt; new ArrayList&lt;&gt;());<a name="line.1280"></a>
-<span class="sourceLineNo">1281</span>          if (!RegionReplicaUtil.isDefaultReplica(region.getReplicaId())) {<a name="line.1281"></a>
-<span class="sourceLineNo">1282</span>            // if the region is not a default replica<a name="line.1282"></a>
-<span class="sourceLineNo">1283</span>            // check if the assignments map has the other replica region on this server<a name="line.1283"></a>
-<span class="sourceLineNo">1284</span>            for (RegionInfo hri : serverRegions) {<a name="line.1284"></a>
-<span class="sourceLineNo">1285</span>              if (RegionReplicaUtil.isReplicasForSameRegion(region, hri)) {<a name="line.1285"></a>
-<span class="sourceLineNo">1286</span>                if (LOG.isTraceEnabled()) {<a name="line.1286"></a>
-<span class="sourceLineNo">1287</span>                  LOG.trace("Skipping the server, " + serverName<a name="line.1287"></a>
-<span class="sourceLineNo">1288</span>                      + " , got the same server for the region " + region);<a name="line.1288"></a>
-<span class="sourceLineNo">1289</span>                }<a name="line.1289"></a>
-<span class="sourceLineNo">1290</span>                // do not allow this case. The unassignedRegions we got because the<a name="line.1290"></a>
-<span class="sourceLineNo">1291</span>                // replica region in this list was not assigned because of lower availablity issue.<a name="line.1291"></a>
-<span class="sourceLineNo">1292</span>                // So when we assign here we should ensure that as far as possible the server being<a name="line.1292"></a>
-<span class="sourceLineNo">1293</span>                // selected does not have the server where the replica region was not assigned.<a name="line.1293"></a>
-<span class="sourceLineNo">1294</span>                continue INNER; // continue the inner loop, ie go to the next server<a name="line.1294"></a>
-<span class="sourceLineNo">1295</span>              }<a name="line.1295"></a>
-<span class="sourceLineNo">1296</span>            }<a name="line.1296"></a>
-<span class="sourceLineNo">1297</span>          }<a name="line.1297"></a>
-<span class="sourceLineNo">1298</span>          serverRegions.add(region);<a name="line.1298"></a>
-<span class="sourceLineNo">1299</span>          cluster.doAssignRegion(region, serverName);<a name="line.1299"></a>
-<span class="sourceLineNo">1300</span>          serverIdx = (j + serverIdx + 1) % numServers; //remain from next server<a name="line.1300"></a>
-<span class="sourceLineNo">1301</span>          assigned = true;<a name="line.1301"></a>
-<span class="sourceLineNo">1302</span>          break;<a name="line.1302"></a>
-<span class="sourceLineNo">1303</span>        }<a name="line.1303"></a>
-<span class="sourceLineNo">1304</span>      }<a name="line.1304"></a>
-<span class="sourceLineNo">1305</span>      if (!assigned) {<a name="line.1305"></a>
-<span class="sourceLineNo">1306</span>        lastFewRegions.add(region);<a name="line.1306"></a>
-<span class="sourceLineNo">1307</span>      }<a name="line.1307"></a>
-<span class="sourceLineNo">1308</span>    }<a name="line.1308"></a>
-<span class="sourceLineNo">1309</span>    // just sprinkle the rest of the regions on random regionservers. The balanceCluster will<a name="line.1309"></a>
-<span class="sourceLineNo">1310</span>    // make it optimal later. we can end up with this if numReplicas &gt; numServers.<a name="line.1310"></a>
-<span class="sourceLineNo">1311</span>    for (RegionInfo region : lastFewRegions) {<a name="line.1311"></a>
-<span class="sourceLineNo">1312</span>      int i = RANDOM.nextInt(numServers);<a name="line.1312"></a>
-<span class="sourceLineNo">1313</span>      ServerName server = servers.get(i);<a name="line.1313"></a>
-<span class="sourceLineNo">1314</span>      List&lt;RegionInfo&gt; serverRegions = assignments.computeIfAbsent(server, k -&gt; new ArrayList&lt;&gt;());<a name="line.1314"></a>
-<span class="sourceLineNo">1315</span>      serverRegions.add(region);<a name="line.1315"></a>
-<span class="sourceLineNo">1316</span>      cluster.doAssignRegion(region, server);<a name="line.1316"></a>
-<span class="sourceLineNo">1317</span>    }<a name="line.1317"></a>
-<span class="sourceLineNo">1318</span>    return assignments;<a name="line.1318"></a>
-<span class="sourceLineNo">1319</span>  }<a name="line.1319"></a>
-<span class="sourceLineNo">1320</span><a name="line.1320"></a>
-<span class="sourceLineNo">1321</span>  protected Cluster createCluster(List&lt;ServerName&gt; servers, Collection&lt;RegionInfo&gt; regions) {<a name="line.1321"></a>
+<span class="sourceLineNo">744</span>      // there is a subset relation for server &lt; host &lt; rack<a name="line.744"></a>
+<span class="sourceLineNo">745</span>      // check server first<a name="line.745"></a>
+<span class="sourceLineNo">746</span><a name="line.746"></a>
+<span class="sourceLineNo">747</span>      if (contains(primariesOfRegionsPerServer[server], primary)) {<a name="line.747"></a>
+<span class="sourceLineNo">748</span>        // check for whether there are other servers that we can place this region<a name="line.748"></a>
+<span class="sourceLineNo">749</span>        for (int i = 0; i &lt; primariesOfRegionsPerServer.length; i++) {<a name="line.749"></a>
+<span class="sourceLineNo">750</span>          if (i != server &amp;&amp; !contains(primariesOfRegionsPerServer[i], primary)) {<a name="line.750"></a>
+<span class="sourceLineNo">751</span>            return true; // meaning there is a better server<a name="line.751"></a>
+<span class="sourceLineNo">752</span>          }<a name="line.752"></a>
+<span class="sourceLineNo">753</span>        }<a name="line.753"></a>
+<span class="sourceLineNo">754</span>        return false; // there is not a better server to place this<a name="line.754"></a>
+<span class="sourceLineNo">755</span>      }<a name="line.755"></a>
+<span class="sourceLineNo">756</span><a name="line.756"></a>
+<span class="sourceLineNo">757</span>      // check host<a name="line.757"></a>
+<span class="sourceLineNo">758</span>      if (multiServersPerHost) { // these arrays would only be allocated if we have more than one server per host<a name="line.758"></a>
+<span class="sourceLineNo">759</span>        int host = serverIndexToHostIndex[server];<a name="line.759"></a>
+<span class="sourceLineNo">760</span>        if (contains(primariesOfRegionsPerHost[host], primary)) {<a name="line.760"></a>
+<span class="sourceLineNo">761</span>          // check for whether there are other hosts that we can place this region<a name="line.761"></a>
+<span class="sourceLineNo">762</span>          for (int i = 0; i &lt; primariesOfRegionsPerHost.length; i++) {<a name="line.762"></a>
+<span class="sourceLineNo">763</span>            if (i != host &amp;&amp; !contains(primariesOfRegionsPerHost[i], primary)) {<a name="line.763"></a>
+<span class="sourceLineNo">764</span>              return true; // meaning there is a better host<a name="line.764"></a>
+<span class="sourceLineNo">765</span>            }<a name="line.765"></a>
+<span class="sourceLineNo">766</span>          }<a name="line.766"></a>
+<span class="sourceLineNo">767</span>          return false; // there is not a better host to place this<a name="line.767"></a>
+<span class="sourceLineNo">768</span>        }<a name="line.768"></a>
+<span class="sourceLineNo">769</span>      }<a name="line.769"></a>
+<span class="sourceLineNo">770</span><a name="line.770"></a>
+<span class="sourceLineNo">771</span>      // check rack<a name="line.771"></a>
+<span class="sourceLineNo">772</span>      if (numRacks &gt; 1) {<a name="line.772"></a>
+<span class="sourceLineNo">773</span>        int rack = serverIndexToRackIndex[server];<a name="line.773"></a>
+<span class="sourceLineNo">774</span>        if (contains(primariesOfRegionsPerRack[rack], primary)) {<a name="line.774"></a>
+<span class="sourceLineNo">775</span>          // check for whether there are other racks that we can place this region<a name="line.775"></a>
+<span class="sourceLineNo">776</span>          for (int i = 0; i &lt; primariesOfRegionsPerRack.length; i++) {<a name="line.776"></a>
+<span class="sourceLineNo">777</span>            if (i != rack &amp;&amp; !contains(primariesOfRegionsPerRack[i], primary)) {<a name="line.777"></a>
+<span class="sourceLineNo">778</span>              return true; // meaning there is a better rack<a name="line.778"></a>
+<span class="sourceLineNo">779</span>            }<a name="line.779"></a>
+<span class="sourceLineNo">780</span>          }<a name="line.780"></a>
+<span class="sourceLineNo">781</span>          return false; // there is not a better rack to place this<a name="line.781"></a>
+<span class="sourceLineNo">782</span>        }<a name="line.782"></a>
+<span class="sourceLineNo">783</span>      }<a name="line.783"></a>
+<span class="sourceLineNo">784</span>      return false;<a name="line.784"></a>
+<span class="sourceLineNo">785</span>    }<a name="line.785"></a>
+<span class="sourceLineNo">786</span><a name="line.786"></a>
+<span class="sourceLineNo">787</span>    void doAssignRegion(RegionInfo regionInfo, ServerName serverName) {<a name="line.787"></a>
+<span class="sourceLineNo">788</span>      if (!serversToIndex.containsKey(serverName.getHostAndPort())) {<a name="line.788"></a>
+<span class="sourceLineNo">789</span>        return;<a name="line.789"></a>
+<span class="sourceLineNo">790</span>      }<a name="line.790"></a>
+<span class="sourceLineNo">791</span>      int server = serversToIndex.get(serverName.getHostAndPort());<a name="line.791"></a>
+<span class="sourceLineNo">792</span>      int region = regionsToIndex.get(regionInfo);<a name="line.792"></a>
+<span class="sourceLineNo">793</span>      doAction(new AssignRegionAction(region, server));<a name="line.793"></a>
+<span class="sourceLineNo">794</span>    }<a name="line.794"></a>
+<span class="sourceLineNo">795</span><a name="line.795"></a>
+<span class="sourceLineNo">796</span>    void regionMoved(int region, int oldServer, int newServer) {<a name="line.796"></a>
+<span class="sourceLineNo">797</span>      regionIndexToServerIndex[region] = newServer;<a name="line.797"></a>
+<span class="sourceLineNo">798</span>      if (initialRegionIndexToServerIndex[region] == newServer) {<a name="line.798"></a>
+<span class="sourceLineNo">799</span>        numMovedRegions--; //region moved back to original location<a name="line.799"></a>
+<span class="sourceLineNo">800</span>      } else if (oldServer &gt;= 0 &amp;&amp; initialRegionIndexToServerIndex[region] == oldServer) {<a name="line.800"></a>
+<span class="sourceLineNo">801</span>        numMovedRegions++; //region moved from original location<a name="line.801"></a>
+<span class="sourceLineNo">802</span>      }<a name="line.802"></a>
+<span class="sourceLineNo">803</span>      int tableIndex = regionIndexToTableIndex[region];<a name="line.803"></a>
+<span class="sourceLineNo">804</span>      if (oldServer &gt;= 0) {<a name="line.804"></a>
+<span class="sourceLineNo">805</span>        numRegionsPerServerPerTable[oldServer][tableIndex]--;<a name="line.805"></a>
+<span class="sourceLineNo">806</span>      }<a name="line.806"></a>
+<span class="sourceLineNo">807</span>      numRegionsPerServerPerTable[newServer][tableIndex]++;<a name="line.807"></a>
+<span class="sourceLineNo">808</span><a name="line.808"></a>
+<span class="sourceLineNo">809</span>      //check whether this caused maxRegionsPerTable in the new Server to be updated<a name="line.809"></a>
+<span class="sourceLineNo">810</span>      if (numRegionsPerServerPerTable[newServer][tableIndex] &gt; numMaxRegionsPerTable[tableIndex]) {<a name="line.810"></a>
+<span class="sourceLineNo">811</span>        numMaxRegionsPerTable[tableIndex] = numRegionsPerServerPerTable[newServer][tableIndex];<a name="line.811"></a>
+<span class="sourceLineNo">812</span>      } else if (oldServer &gt;= 0 &amp;&amp; (numRegionsPerServerPerTable[oldServer][tableIndex] + 1)<a name="line.812"></a>
+<span class="sourceLineNo">813</span>          == numMaxRegionsPerTable[tableIndex]) {<a name="line.813"></a>
+<span class="sourceLineNo">814</span>        //recompute maxRegionsPerTable since the previous value was coming from the old server<a name="line.814"></a>
+<span class="sourceLineNo">815</span>        numMaxRegionsPerTable[tableIndex] = 0;<a name="line.815"></a>
+<span class="sourceLineNo">816</span>        for (int[] aNumRegionsPerServerPerTable : numRegionsPerServerPerTable) {<a name="line.816"></a>
+<span class="sourceLineNo">817</span>          if (aNumRegionsPerServerPerTable[tableIndex] &gt; numMaxRegionsPerTable[tableIndex]) {<a name="line.817"></a>
+<span class="sourceLineNo">818</span>            numMaxRegionsPerTable[tableIndex] = aNumRegionsPerServerPerTable[tableIndex];<a name="line.818"></a>
+<span class="sourceLineNo">819</span>          }<a name="line.819"></a>
+<span class="sourceLineNo">820</span>        }<a name="line.820"></a>
+<span class="sourceLineNo">821</span>      }<a name="line.821"></a>
+<span class="sourceLineNo">822</span><a name="line.822"></a>
+<span class="sourceLineNo">823</span>      // update for servers<a name="line.823"></a>
+<span class="sourceLineNo">824</span>      int primary = regionIndexToPrimaryIndex[region];<a name="line.824"></a>
+<span class="sourceLineNo">825</span>      if (oldServer &gt;= 0) {<a name="line.825"></a>
+<span class="sourceLineNo">826</span>        primariesOfRegionsPerServer[oldServer] = removeRegion(<a name="line.826"></a>
+<span class="sourceLineNo">827</span>          primariesOfRegionsPerServer[oldServer], primary);<a name="line.827"></a>
+<span class="sourceLineNo">828</span>      }<a name="line.828"></a>
+<span class="sourceLineNo">829</span>      primariesOfRegionsPerServer[newServer] = addRegionSorted(<a name="line.829"></a>
+<span class="sourceLineNo">830</span>        primariesOfRegionsPerServer[newServer], primary);<a name="line.830"></a>
+<span class="sourceLineNo">831</span><a name="line.831"></a>
+<span class="sourceLineNo">832</span>      // update for hosts<a name="line.832"></a>
+<span class="sourceLineNo">833</span>      if (multiServersPerHost) {<a name="line.833"></a>
+<span class="sourceLineNo">834</span>        int oldHost = oldServer &gt;= 0 ? serverIndexToHostIndex[oldServer] : -1;<a name="line.834"></a>
+<span class="sourceLineNo">835</span>        int newHost = serverIndexToHostIndex[newServer];<a name="line.835"></a>
+<span class="sourceLineNo">836</span>        if (newHost != oldHost) {<a name="line.836"></a>
+<span class="sourceLineNo">837</span>          regionsPerHost[newHost] = addRegion(regionsPerHost[newHost], region);<a name="line.837"></a>
+<span class="sourceLineNo">838</span>          primariesOfRegionsPerHost[newHost] = addRegionSorted(primariesOfRegionsPerHost[newHost], primary);<a name="line.838"></a>
+<span class="sourceLineNo">839</span>          if (oldHost &gt;= 0) {<a name="line.839"></a>
+<span class="sourceLineNo">840</span>            regionsPerHost[oldHost] = removeRegion(regionsPerHost[oldHost], region);<a name="line.840"></a>
+<span class="sourceLineNo">841</span>            primariesOfRegionsPerHost[oldHost] = removeRegion(<a name="line.841"></a>
+<span class="sourceLineNo">842</span>              primariesOfRegionsPerHost[oldHost], primary); // will still be sorted<a name="line.842"></a>
+<span class="sourceLineNo">843</span>          }<a name="line.843"></a>
+<span class="sourceLineNo">844</span>        }<a name="line.844"></a>
+<span class="sourceLineNo">845</span>      }<a name="line.845"></a>
+<span class="sourceLineNo">846</span><a name="line.846"></a>
+<span class="sourceLineNo">847</span>      // update for racks<a name="line.847"></a>
+<span class="sourceLineNo">848</span>      if (numRacks &gt; 1) {<a name="line.848"></a>
+<span class="sourceLineNo">849</span>        int oldRack = oldServer &gt;= 0 ? serverIndexToRackIndex[oldServer] : -1;<a name="line.849"></a>
+<span class="sourceLineNo">850</span>        int newRack = serverIndexToRackIndex[newServer];<a name="line.850"></a>
+<span class="sourceLineNo">851</span>        if (newRack != oldRack) {<a name="line.851"></a>
+<span class="sourceLineNo">852</span>          regionsPerRack[newRack] = addRegion(regionsPerRack[newRack], region);<a name="line.852"></a>
+<span class="sourceLineNo">853</span>          primariesOfRegionsPerRack[newRack] = addRegionSorted(primariesOfRegionsPerRack[newRack], primary);<a name="line.853"></a>
+<span class="sourceLineNo">854</span>          if (oldRack &gt;= 0) {<a name="line.854"></a>
+<span class="sourceLineNo">855</span>            regionsPerRack[oldRack] = removeRegion(regionsPerRack[oldRack], region);<a name="line.855"></a>
+<span class="sourceLineNo">856</span>            primariesOfRegionsPerRack[oldRack] = removeRegion(<a name="line.856"></a>
+<span class="sourceLineNo">857</span>              primariesOfRegionsPerRack[oldRack], primary); // will still be sorted<a name="line.857"></a>
+<span class="sourceLineNo">858</span>          }<a name="line.858"></a>
+<span class="sourceLineNo">859</span>        }<a name="line.859"></a>
+<span class="sourceLineNo">860</span>      }<a name="line.860"></a>
+<span class="sourceLineNo">861</span>    }<a name="line.861"></a>
+<span class="sourceLineNo">862</span><a name="line.862"></a>
+<span class="sourceLineNo">863</span>    int[] removeRegion(int[] regions, int regionIndex) {<a name="line.863"></a>
+<span class="sourceLineNo">864</span>      //TODO: this maybe costly. Consider using linked lists<a name="line.864"></a>
+<span class="sourceLineNo">865</span>      int[] newRegions = new int[regions.length - 1];<a name="line.865"></a>
+<span class="sourceLineNo">866</span>      int i = 0;<a name="line.866"></a>
+<span class="sourceLineNo">867</span>      for (i = 0; i &lt; regions.length; i++) {<a name="line.867"></a>
+<span class="sourceLineNo">868</span>        if (regions[i] == regionIndex) {<a name="line.868"></a>
+<span class="sourceLineNo">869</span>          break;<a name="line.869"></a>
+<span class="sourceLineNo">870</span>        }<a name="line.870"></a>
+<span class="sourceLineNo">871</span>        newRegions[i] = regions[i];<a name="line.871"></a>
+<span class="sourceLineNo">872</span>      }<a name="line.872"></a>
+<span class="sourceLineNo">873</span>      System.arraycopy(regions, i+1, newRegions, i, newRegions.length - i);<a name="line.873"></a>
+<span class="sourceLineNo">874</span>      return newRegions;<a name="line.874"></a>
+<span class="sourceLineNo">875</span>    }<a name="line.875"></a>
+<span class="sourceLineNo">876</span><a name="line.876"></a>
+<span class="sourceLineNo">877</span>    int[] addRegion(int[] regions, int regionIndex) {<a name="line.877"></a>
+<span class="sourceLineNo">878</span>      int[] newRegions = new int[regions.length + 1];<a name="line.878"></a>
+<span class="sourceLineNo">879</span>      System.arraycopy(regions, 0, newRegions, 0, regions.length);<a name="line.879"></a>
+<span class="sourceLineNo">880</span>      newRegions[newRegions.length - 1] = regionIndex;<a name="line.880"></a>
+<span class="sourceLineNo">881</span>      return newRegions;<a name="line.881"></a>
+<span class="sourceLineNo">882</span>    }<a name="line.882"></a>
+<span class="sourceLineNo">883</span><a name="line.883"></a>
+<span class="sourceLineNo">884</span>    int[] addRegionSorted(int[] regions, int regionIndex) {<a name="line.884"></a>
+<span class="sourceLineNo">885</span>      int[] newRegions = new int[regions.length + 1];<a name="line.885"></a>
+<span class="sourceLineNo">886</span>      int i = 0;<a name="line.886"></a>
+<span class="sourceLineNo">887</span>      for (i = 0; i &lt; regions.length; i++) { // find the index to insert<a name="line.887"></a>
+<span class="sourceLineNo">888</span>        if (regions[i] &gt; regionIndex) {<a name="line.888"></a>
+<span class="sourceLineNo">889</span>          break;<a name="line.889"></a>
+<span class="sourceLineNo">890</span>        }<a name="line.890"></a>
+<span class="sourceLineNo">891</span>      }<a name="line.891"></a>
+<span class="sourceLineNo">892</span>      System.arraycopy(regions, 0, newRegions, 0, i); // copy first half<a name="line.892"></a>
+<span class="sourceLineNo">893</span>      System.arraycopy(regions, i, newRegions, i+1, regions.length - i); // copy second half<a name="line.893"></a>
+<span class="sourceLineNo">894</span>      newRegions[i] = regionIndex;<a name="line.894"></a>
+<span class="sourceLineNo">895</span><a name="line.895"></a>
+<span class="sourceLineNo">896</span>      return newRegions;<a name="line.896"></a>
+<span class="sourceLineNo">897</span>    }<a name="line.897"></a>
+<span class="sourceLineNo">898</span><a name="line.898"></a>
+<span class="sourceLineNo">899</span>    int[] replaceRegion(int[] regions, int regionIndex, int newRegionIndex) {<a name="line.899"></a>
+<span class="sourceLineNo">900</span>      int i = 0;<a name="line.900"></a>
+<span class="sourceLineNo">901</span>      for (i = 0; i &lt; regions.length; i++) {<a name="line.901"></a>
+<span class="sourceLineNo">902</span>        if (regions[i] == regionIndex) {<a name="line.902"></a>
+<span class="sourceLineNo">903</span>          regions[i] = newRegionIndex;<a name="line.903"></a>
+<span class="sourceLineNo">904</span>          break;<a name="line.904"></a>
+<span class="sourceLineNo">905</span>        }<a name="line.905"></a>
+<span class="sourceLineNo">906</span>      }<a name="line.906"></a>
+<span class="sourceLineNo">907</span>      return regions;<a name="line.907"></a>
+<span class="sourceLineNo">908</span>    }<a name="line.908"></a>
+<span class="sourceLineNo">909</span><a name="line.909"></a>
+<span class="sourceLineNo">910</span>    void sortServersByRegionCount() {<a name="line.910"></a>
+<span class="sourceLineNo">911</span>      Arrays.sort(serverIndicesSortedByRegionCount, numRegionsComparator);<a name="line.911"></a>
+<span class="sourceLineNo">912</span>    }<a name="line.912"></a>
+<span class="sourceLineNo">913</span><a name="line.913"></a>
+<span class="sourceLineNo">914</span>    int getNumRegions(int server) {<a name="line.914"></a>
+<span class="sourceLineNo">915</span>      return regionsPerServer[server].length;<a name="line.915"></a>
+<span class="sourceLineNo">916</span>    }<a name="line.916"></a>
+<span class="sourceLineNo">917</span><a name="line.917"></a>
+<span class="sourceLineNo">918</span>    boolean contains(int[] arr, int val) {<a name="line.918"></a>
+<span class="sourceLineNo">919</span>      return Arrays.binarySearch(arr, val) &gt;= 0;<a name="line.919"></a>
+<span class="sourceLineNo">920</span>    }<a name="line.920"></a>
+<span class="sourceLineNo">921</span><a name="line.921"></a>
+<span class="sourceLineNo">922</span>    private Comparator&lt;Integer&gt; numRegionsComparator = Comparator.comparingInt(this::getNumRegions);<a name="line.922"></a>
+<span class="sourceLineNo">923</span><a name="line.923"></a>
+<span class="sourceLineNo">924</span>    int getLowestLocalityRegionOnServer(int serverIndex) {<a name="line.924"></a>
+<span class="sourceLineNo">925</span>      if (regionFinder != null) {<a name="line.925"></a>
+<span class="sourceLineNo">926</span>        float lowestLocality = 1.0f;<a name="line.926"></a>
+<span class="sourceLineNo">927</span>        int lowestLocalityRegionIndex = -1;<a name="line.927"></a>
+<span class="sourceLineNo">928</span>        if (regionsPerServer[serverIndex].length == 0) {<a name="line.928"></a>
+<span class="sourceLineNo">929</span>          // No regions on that region server<a name="line.929"></a>
+<span class="sourceLineNo">930</span>          return -1;<a name="line.930"></a>
+<span class="sourceLineNo">931</span>        }<a name="line.931"></a>
+<span class="sourceLineNo">932</span>        for (int j = 0; j &lt; regionsPerServer[serverIndex].length; j++) {<a name="line.932"></a>
+<span class="sourceLineNo">933</span>          int regionIndex = regionsPerServer[serverIndex][j];<a name="line.933"></a>
+<span class="sourceLineNo">934</span>          HDFSBlocksDistribution distribution = regionFinder<a name="line.934"></a>
+<span class="sourceLineNo">935</span>              .getBlockDistribution(regions[regionIndex]);<a name="line.935"></a>
+<span class="sourceLineNo">936</span>          float locality = distribution.getBlockLocalityIndex(servers[serverIndex].getHostname());<a name="line.936"></a>
+<span class="sourceLineNo">937</span>          // skip empty region<a name="line.937"></a>
+<span class="sourceLineNo">938</span>          if (distribution.getUniqueBlocksTotalWeight() == 0) {<a name="line.938"></a>
+<span class="sourceLineNo">939</span>            continue;<a name="line.939"></a>
+<span class="sourceLineNo">940</span>          }<a name="line.940"></a>
+<span class="sourceLineNo">941</span>          if (locality &lt; lowestLocality) {<a name="line.941"></a>
+<span class="sourceLineNo">942</span>            lowestLocality = locality;<a name="line.942"></a>
+<span class="sourceLineNo">943</span>            lowestLocalityRegionIndex = j;<a name="line.943"></a>
+<span class="sourceLineNo">944</span>          }<a name="line.944"></a>
+<span class="sourceLineNo">945</span>        }<a name="line.945"></a>
+<span class="sourceLineNo">946</span>        if (lowestLocalityRegionIndex == -1) {<a name="line.946"></a>
+<span class="sourceLineNo">947</span>          return -1;<a name="line.947"></a>
+<span class="sourceLineNo">948</span>        }<a name="line.948"></a>
+<span class="sourceLineNo">949</span>        if (LOG.isTraceEnabled()) {<a name="line.949"></a>
+<span class="sourceLineNo">950</span>          LOG.trace("Lowest locality region is "<a name="line.950"></a>
+<span class="sourceLineNo">951</span>              + regions[regionsPerServer[serverIndex][lowestLocalityRegionIndex]]<a name="line.951"></a>
+<span class="sourceLineNo">952</span>                  .getRegionNameAsString() + " with locality " + lowestLocality<a name="line.952"></a>
+<span class="sourceLineNo">953</span>              + " and its region server contains " + regionsPerServer[serverIndex].length<a name="line.953"></a>
+<span class="sourceLineNo">954</span>              + " regions");<a name="line.954"></a>
+<span class="sourceLineNo">955</span>        }<a name="line.955"></a>
+<span class="sourceLineNo">956</span>        return regionsPerServer[serverIndex][lowestLocalityRegionIndex];<a name="line.956"></a>
+<span class="sourceLineNo">957</span>      } else {<a name="line.957"></a>
+<span class="sourceLineNo">958</span>        return -1;<a name="line.958"></a>
+<span class="sourceLineNo">959</span>      }<a name="line.959"></a>
+<span class="sourceLineNo">960</span>    }<a name="line.960"></a>
+<span class="sourceLineNo">961</span><a name="line.961"></a>
+<span class="sourceLineNo">962</span>    float getLocalityOfRegion(int region, int server) {<a name="line.962"></a>
+<span class="sourceLineNo">963</span>      if (regionFinder != null) {<a name="line.963"></a>
+<span class="sourceLineNo">964</span>        HDFSBlocksDistribution distribution = regionFinder.getBlockDistribution(regions[region]);<a name="line.964"></a>
+<span class="sourceLineNo">965</span>        return distribution.getBlockLocalityIndex(servers[server].getHostname());<a name="line.965"></a>
+<span class="sourceLineNo">966</span>      } else {<a name="line.966"></a>
+<span class="sourceLineNo">967</span>        return 0f;<a name="line.967"></a>
+<span class="sourceLineNo">968</span>      }<a name="line.968"></a>
+<span class="sourceLineNo">969</span>    }<a name="line.969"></a>
+<span class="sourceLineNo">970</span><a name="line.970"></a>
+<span class="sourceLineNo">971</span>    @VisibleForTesting<a name="line.971"></a>
+<span class="sourceLineNo">972</span>    protected void setNumRegions(int numRegions) {<a name="line.972"></a>
+<span class="sourceLineNo">973</span>      this.numRegions = numRegions;<a name="line.973"></a>
+<span class="sourceLineNo">974</span>    }<a name="line.974"></a>
+<span class="sourceLineNo">975</span><a name="line.975"></a>
+<span class="sourceLineNo">976</span>    @VisibleForTesting<a name="line.976"></a>
+<span class="sourceLineNo">977</span>    protected void setNumMovedRegions(int numMovedRegions) {<a name="line.977"></a>
+<span class="sourceLineNo">978</span>      this.numMovedRegions = numMovedRegions;<a name="line.978"></a>
+<span class="sourceLineNo">979</span>    }<a name="line.979"></a>
+<span class="sourceLineNo">980</span><a name="line.980"></a>
+<span class="sourceLineNo">981</span>    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="SBSC_USE_STRINGBUFFER_CONCATENATION",<a name="line.981"></a>
+<span class="sourceLineNo">982</span>        justification="Not important but should be fixed")<a name="line.982"></a>
+<span class="sourceLineNo">983</span>    @Override<a name="line.983"></a>
+<span class="sourceLineNo">984</span>    public String toString() {<a name="line.984"></a>
+<span class="sourceLineNo">985</span>      StringBuilder desc = new StringBuilder("Cluster={servers=[");<a name="line.985"></a>
+<span class="sourceLineNo">986</span>      for(ServerName sn:servers) {<a name="line.986"></a>
+<span class="sourceLineNo">987</span>        desc.append(sn.getHostAndPort()).append(", ");<a name="line.987"></a>
+<span class="sourceLineNo">988</span>      }<a name="line.988"></a>
+<span class="sourceLineNo">989</span>      desc.append("], serverIndicesSortedByRegionCount=")<a name="line.989"></a>
+<span class="sourceLineNo">990</span>          .append(Arrays.toString(serverIndicesSortedByRegionCount))<a name="line.990"></a>
+<span class="sourceLineNo">991</span>          .append(", regionsPerServer=").append(Arrays.deepToString(regionsPerServer));<a name="line.991"></a>
+<span class="sourceLineNo">992</span><a name="line.992"></a>
+<span class="sourceLineNo">993</span>      desc.append(", numMaxRegionsPerTable=").append(Arrays.toString(numMaxRegionsPerTable))<a name="line.993"></a>
+<span class="sourceLineNo">994</span>          .append(", numRegions=").append(numRegions).append(", numServers=").append(numServers)<a name="line.994"></a>
+<span class="sourceLineNo">995</span>          .append(", numTables=").append(numTables).append(", numMovedRegions=")<a name="line.995"></a>
+<span class="sourceLineNo">996</span>          .append(numMovedRegions).append('}');<a name="line.996"></a>
+<span class="sourceLineNo">997</span>      return desc.toString();<a name="line.997"></a>
+<span class="sourceLineNo">998</span>    }<a name="line.998"></a>
+<span class="sourceLineNo">999</span>  }<a name="line.999"></a>
+<span class="sourceLineNo">1000</span><a name="line.1000"></a>
+<span class="sourceLineNo">1001</span>  // slop for regions<a name="line.1001"></a>
+<span class="sourceLineNo">1002</span>  protected float slop;<a name="line.1002"></a>
+<span class="sourceLineNo">1003</span>  // overallSlop to control simpleLoadBalancer's cluster level threshold<a name="line.1003"></a>
+<span class="sourceLineNo">1004</span>  protected float overallSlop;<a name="line.1004"></a>
+<span class="sourceLineNo">1005</span>  protected Configuration config = HBaseConfiguration.create();<a name="line.1005"></a>
+<span class="sourceLineNo">1006</span>  protected RackManager rackManager;<a name="line.1006"></a>
+<span class="sourceLineNo">1007</span>  private static final Random RANDOM = new Random(System.currentTimeMillis());<a name="line.1007"></a>
+<span class="sourceLineNo">1008</span>  private static final Logger LOG = LoggerFactory.getLogger(BaseLoadBalancer.class);<a name="line.1008"></a>
+<span class="sourceLineNo">1009</span>  protected MetricsBalancer metricsBalancer = null;<a name="line.1009"></a>
+<span class="sourceLineNo">1010</span>  protected ClusterMetrics clusterStatus = null;<a name="line.1010"></a>
+<span class="sourceLineNo">1011</span>  protected ServerName masterServerName;<a name="line.1011"></a>
+<span class="sourceLineNo">1012</span>  protected MasterServices services;<a name="line.1012"></a>
+<span class="sourceLineNo">1013</span>  protected boolean tablesOnMaster;<a name="line.1013"></a>
+<span class="sourceLineNo">1014</span>  protected boolean onlySystemTablesOnMaster;<a name="line.1014"></a>
+<span class="sourceLineNo">1015</span><a name="line.1015"></a>
+<span class="sourceLineNo">1016</span>  @Override<a name="line.1016"></a>
+<span class="sourceLineNo">1017</span>  public void setConf(Configuration conf) {<a name="line.1017"></a>
+<span class="sourceLineNo">1018</span>    this.config = conf;<a name="line.1018"></a>
+<span class="sourceLineNo">1019</span>    setSlop(conf);<a name="line.1019"></a>
+<span class="sourceLineNo">1020</span>    if (slop &lt; 0) slop = 0;<a name="line.1020"></a>
+<span class="sourceLineNo">1021</span>    else if (slop &gt; 1) slop = 1;<a name="line.1021"></a>
+<span class="sourceLineNo">1022</span><a name="line.1022"></a>
+<span class="sourceLineNo">1023</span>    if (overallSlop &lt; 0) overallSlop = 0;<a name="line.1023"></a>
+<span class="sourceLineNo">1024</span>    else if (overallSlop &gt; 1) overallSlop = 1;<a name="line.1024"></a>
+<span class="sourceLineNo">1025</span><a name="line.1025"></a>
+<span class="sourceLineNo">1026</span>    this.tablesOnMaster = LoadBalancer.isTablesOnMaster(this.config);<a name="line.1026"></a>
+<span class="sourceLineNo">1027</span>    this.onlySystemTablesOnMaster = LoadBalancer.isSystemTablesOnlyOnMaster(this.config);<a name="line.1027"></a>
+<span class="sourceLineNo">1028</span>    // If system tables on master, 

<TRUNCATED>

Mime
View raw message