hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mi...@apache.org
Subject [26/51] [partial] hbase-site git commit: Published site at 29a192ef3cbe3b9cc12a6ee38f39e1199ac9790f.
Date Thu, 11 Feb 2016 17:34:32 GMT
http://git-wip-us.apache.org/repos/asf/hbase-site/blob/8bb348c6/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/SimpleLoadBalancer.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/SimpleLoadBalancer.html b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/SimpleLoadBalancer.html
index 4345bcc..a682fea 100644
--- a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/SimpleLoadBalancer.html
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/SimpleLoadBalancer.html
@@ -54,402 +54,398 @@
 <span class="sourceLineNo">046</span> * &lt;p&gt;Cluster-wide load balancing will occur only when there are no regions in<a name="line.46"></a>
 <span class="sourceLineNo">047</span> * transition and according to a fixed period of a time using {@link #balanceCluster(Map)}.<a name="line.47"></a>
 <span class="sourceLineNo">048</span> *<a name="line.48"></a>
-<span class="sourceLineNo">049</span> * &lt;p&gt;Inline region placement with {@link #immediateAssignment} can be used when<a name="line.49"></a>
-<span class="sourceLineNo">050</span> * the Master needs to handle closed regions that it currently does not have<a name="line.50"></a>
-<span class="sourceLineNo">051</span> * a destination set for.  This can happen during master failover.<a name="line.51"></a>
-<span class="sourceLineNo">052</span> *<a name="line.52"></a>
-<span class="sourceLineNo">053</span> * &lt;p&gt;On cluster startup, bulk assignment can be used to determine<a name="line.53"></a>
-<span class="sourceLineNo">054</span> * locations for all Regions in a cluster.<a name="line.54"></a>
-<span class="sourceLineNo">055</span> *<a name="line.55"></a>
-<span class="sourceLineNo">056</span> * &lt;p&gt;This classes produces plans for the <a name="line.56"></a>
-<span class="sourceLineNo">057</span> * {@link org.apache.hadoop.hbase.master.AssignmentManager} to execute.<a name="line.57"></a>
-<span class="sourceLineNo">058</span> */<a name="line.58"></a>
-<span class="sourceLineNo">059</span>@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)<a name="line.59"></a>
-<span class="sourceLineNo">060</span>public class SimpleLoadBalancer extends BaseLoadBalancer {<a name="line.60"></a>
-<span class="sourceLineNo">061</span>  private static final Log LOG = LogFactory.getLog(SimpleLoadBalancer.class);<a name="line.61"></a>
-<span class="sourceLineNo">062</span>  private static final Random RANDOM = new Random(System.currentTimeMillis());<a name="line.62"></a>
+<span class="sourceLineNo">049</span> * &lt;p&gt;On cluster startup, bulk assignment can be used to determine<a name="line.49"></a>
+<span class="sourceLineNo">050</span> * locations for all Regions in a cluster.<a name="line.50"></a>
+<span class="sourceLineNo">051</span> *<a name="line.51"></a>
+<span class="sourceLineNo">052</span> * &lt;p&gt;This classes produces plans for the<a name="line.52"></a>
+<span class="sourceLineNo">053</span> * {@link org.apache.hadoop.hbase.master.AssignmentManager} to execute.<a name="line.53"></a>
+<span class="sourceLineNo">054</span> */<a name="line.54"></a>
+<span class="sourceLineNo">055</span>@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)<a name="line.55"></a>
+<span class="sourceLineNo">056</span>public class SimpleLoadBalancer extends BaseLoadBalancer {<a name="line.56"></a>
+<span class="sourceLineNo">057</span>  private static final Log LOG = LogFactory.getLog(SimpleLoadBalancer.class);<a name="line.57"></a>
+<span class="sourceLineNo">058</span>  private static final Random RANDOM = new Random(System.currentTimeMillis());<a name="line.58"></a>
+<span class="sourceLineNo">059</span><a name="line.59"></a>
+<span class="sourceLineNo">060</span>  private RegionInfoComparator riComparator = new RegionInfoComparator();<a name="line.60"></a>
+<span class="sourceLineNo">061</span>  private RegionPlan.RegionPlanComparator rpComparator = new RegionPlan.RegionPlanComparator();<a name="line.61"></a>
+<span class="sourceLineNo">062</span><a name="line.62"></a>
 <span class="sourceLineNo">063</span><a name="line.63"></a>
-<span class="sourceLineNo">064</span>  private RegionInfoComparator riComparator = new RegionInfoComparator();<a name="line.64"></a>
-<span class="sourceLineNo">065</span>  private RegionPlan.RegionPlanComparator rpComparator = new RegionPlan.RegionPlanComparator();<a name="line.65"></a>
-<span class="sourceLineNo">066</span><a name="line.66"></a>
-<span class="sourceLineNo">067</span><a name="line.67"></a>
-<span class="sourceLineNo">068</span>  /**<a name="line.68"></a>
-<span class="sourceLineNo">069</span>   * Stores additional per-server information about the regions added/removed<a name="line.69"></a>
-<span class="sourceLineNo">070</span>   * during the run of the balancing algorithm.<a name="line.70"></a>
-<span class="sourceLineNo">071</span>   *<a name="line.71"></a>
-<span class="sourceLineNo">072</span>   * For servers that shed regions, we need to track which regions we have already<a name="line.72"></a>
-<span class="sourceLineNo">073</span>   * shed. &lt;b&gt;nextRegionForUnload&lt;/b&gt; contains the index in the list of regions on<a name="line.73"></a>
-<span class="sourceLineNo">074</span>   * the server that is the next to be shed.<a name="line.74"></a>
-<span class="sourceLineNo">075</span>   */<a name="line.75"></a>
-<span class="sourceLineNo">076</span>  static class BalanceInfo {<a name="line.76"></a>
-<span class="sourceLineNo">077</span><a name="line.77"></a>
-<span class="sourceLineNo">078</span>    private final int nextRegionForUnload;<a name="line.78"></a>
-<span class="sourceLineNo">079</span>    private int numRegionsAdded;<a name="line.79"></a>
-<span class="sourceLineNo">080</span><a name="line.80"></a>
-<span class="sourceLineNo">081</span>    public BalanceInfo(int nextRegionForUnload, int numRegionsAdded) {<a name="line.81"></a>
-<span class="sourceLineNo">082</span>      this.nextRegionForUnload = nextRegionForUnload;<a name="line.82"></a>
-<span class="sourceLineNo">083</span>      this.numRegionsAdded = numRegionsAdded;<a name="line.83"></a>
+<span class="sourceLineNo">064</span>  /**<a name="line.64"></a>
+<span class="sourceLineNo">065</span>   * Stores additional per-server information about the regions added/removed<a name="line.65"></a>
+<span class="sourceLineNo">066</span>   * during the run of the balancing algorithm.<a name="line.66"></a>
+<span class="sourceLineNo">067</span>   *<a name="line.67"></a>
+<span class="sourceLineNo">068</span>   * For servers that shed regions, we need to track which regions we have already<a name="line.68"></a>
+<span class="sourceLineNo">069</span>   * shed. &lt;b&gt;nextRegionForUnload&lt;/b&gt; contains the index in the list of regions on<a name="line.69"></a>
+<span class="sourceLineNo">070</span>   * the server that is the next to be shed.<a name="line.70"></a>
+<span class="sourceLineNo">071</span>   */<a name="line.71"></a>
+<span class="sourceLineNo">072</span>  static class BalanceInfo {<a name="line.72"></a>
+<span class="sourceLineNo">073</span><a name="line.73"></a>
+<span class="sourceLineNo">074</span>    private final int nextRegionForUnload;<a name="line.74"></a>
+<span class="sourceLineNo">075</span>    private int numRegionsAdded;<a name="line.75"></a>
+<span class="sourceLineNo">076</span><a name="line.76"></a>
+<span class="sourceLineNo">077</span>    public BalanceInfo(int nextRegionForUnload, int numRegionsAdded) {<a name="line.77"></a>
+<span class="sourceLineNo">078</span>      this.nextRegionForUnload = nextRegionForUnload;<a name="line.78"></a>
+<span class="sourceLineNo">079</span>      this.numRegionsAdded = numRegionsAdded;<a name="line.79"></a>
+<span class="sourceLineNo">080</span>    }<a name="line.80"></a>
+<span class="sourceLineNo">081</span><a name="line.81"></a>
+<span class="sourceLineNo">082</span>    int getNextRegionForUnload() {<a name="line.82"></a>
+<span class="sourceLineNo">083</span>      return nextRegionForUnload;<a name="line.83"></a>
 <span class="sourceLineNo">084</span>    }<a name="line.84"></a>
 <span class="sourceLineNo">085</span><a name="line.85"></a>
-<span class="sourceLineNo">086</span>    int getNextRegionForUnload() {<a name="line.86"></a>
-<span class="sourceLineNo">087</span>      return nextRegionForUnload;<a name="line.87"></a>
+<span class="sourceLineNo">086</span>    int getNumRegionsAdded() {<a name="line.86"></a>
+<span class="sourceLineNo">087</span>      return numRegionsAdded;<a name="line.87"></a>
 <span class="sourceLineNo">088</span>    }<a name="line.88"></a>
 <span class="sourceLineNo">089</span><a name="line.89"></a>
-<span class="sourceLineNo">090</span>    int getNumRegionsAdded() {<a name="line.90"></a>
-<span class="sourceLineNo">091</span>      return numRegionsAdded;<a name="line.91"></a>
+<span class="sourceLineNo">090</span>    void setNumRegionsAdded(int numAdded) {<a name="line.90"></a>
+<span class="sourceLineNo">091</span>      this.numRegionsAdded = numAdded;<a name="line.91"></a>
 <span class="sourceLineNo">092</span>    }<a name="line.92"></a>
-<span class="sourceLineNo">093</span><a name="line.93"></a>
-<span class="sourceLineNo">094</span>    void setNumRegionsAdded(int numAdded) {<a name="line.94"></a>
-<span class="sourceLineNo">095</span>      this.numRegionsAdded = numAdded;<a name="line.95"></a>
-<span class="sourceLineNo">096</span>    }<a name="line.96"></a>
-<span class="sourceLineNo">097</span>  }<a name="line.97"></a>
-<span class="sourceLineNo">098</span><a name="line.98"></a>
-<span class="sourceLineNo">099</span>  /**<a name="line.99"></a>
-<span class="sourceLineNo">100</span>   * Generate a global load balancing plan according to the specified map of<a name="line.100"></a>
-<span class="sourceLineNo">101</span>   * server information to the most loaded regions of each server.<a name="line.101"></a>
-<span class="sourceLineNo">102</span>   *<a name="line.102"></a>
-<span class="sourceLineNo">103</span>   * The load balancing invariant is that all servers are within 1 region of the<a name="line.103"></a>
-<span class="sourceLineNo">104</span>   * average number of regions per server.  If the average is an integer number,<a name="line.104"></a>
-<span class="sourceLineNo">105</span>   * all servers will be balanced to the average.  Otherwise, all servers will<a name="line.105"></a>
-<span class="sourceLineNo">106</span>   * have either floor(average) or ceiling(average) regions.<a name="line.106"></a>
-<span class="sourceLineNo">107</span>   *<a name="line.107"></a>
-<span class="sourceLineNo">108</span>   * HBASE-3609 Modeled regionsToMove using Guava's MinMaxPriorityQueue so that<a name="line.108"></a>
-<span class="sourceLineNo">109</span>   *   we can fetch from both ends of the queue.<a name="line.109"></a>
-<span class="sourceLineNo">110</span>   * At the beginning, we check whether there was empty region server<a name="line.110"></a>
-<span class="sourceLineNo">111</span>   *   just discovered by Master. If so, we alternately choose new / old<a name="line.111"></a>
-<span class="sourceLineNo">112</span>   *   regions from head / tail of regionsToMove, respectively. This alternation<a name="line.112"></a>
-<span class="sourceLineNo">113</span>   *   avoids clustering young regions on the newly discovered region server.<a name="line.113"></a>
-<span class="sourceLineNo">114</span>   *   Otherwise, we choose new regions from head of regionsToMove.<a name="line.114"></a>
-<span class="sourceLineNo">115</span>   *<a name="line.115"></a>
-<span class="sourceLineNo">116</span>   * Another improvement from HBASE-3609 is that we assign regions from<a name="line.116"></a>
-<span class="sourceLineNo">117</span>   *   regionsToMove to underloaded servers in round-robin fashion.<a name="line.117"></a>
-<span class="sourceLineNo">118</span>   *   Previously one underloaded server would be filled before we move onto<a name="line.118"></a>
-<span class="sourceLineNo">119</span>   *   the next underloaded server, leading to clustering of young regions.<a name="line.119"></a>
-<span class="sourceLineNo">120</span>   *<a name="line.120"></a>
-<span class="sourceLineNo">121</span>   * Finally, we randomly shuffle underloaded servers so that they receive<a name="line.121"></a>
-<span class="sourceLineNo">122</span>   *   offloaded regions relatively evenly across calls to balanceCluster().<a name="line.122"></a>
-<span class="sourceLineNo">123</span>   *<a name="line.123"></a>
-<span class="sourceLineNo">124</span>   * The algorithm is currently implemented as such:<a name="line.124"></a>
+<span class="sourceLineNo">093</span>  }<a name="line.93"></a>
+<span class="sourceLineNo">094</span><a name="line.94"></a>
+<span class="sourceLineNo">095</span>  /**<a name="line.95"></a>
+<span class="sourceLineNo">096</span>   * Generate a global load balancing plan according to the specified map of<a name="line.96"></a>
+<span class="sourceLineNo">097</span>   * server information to the most loaded regions of each server.<a name="line.97"></a>
+<span class="sourceLineNo">098</span>   *<a name="line.98"></a>
+<span class="sourceLineNo">099</span>   * The load balancing invariant is that all servers are within 1 region of the<a name="line.99"></a>
+<span class="sourceLineNo">100</span>   * average number of regions per server.  If the average is an integer number,<a name="line.100"></a>
+<span class="sourceLineNo">101</span>   * all servers will be balanced to the average.  Otherwise, all servers will<a name="line.101"></a>
+<span class="sourceLineNo">102</span>   * have either floor(average) or ceiling(average) regions.<a name="line.102"></a>
+<span class="sourceLineNo">103</span>   *<a name="line.103"></a>
+<span class="sourceLineNo">104</span>   * HBASE-3609 Modeled regionsToMove using Guava's MinMaxPriorityQueue so that<a name="line.104"></a>
+<span class="sourceLineNo">105</span>   *   we can fetch from both ends of the queue.<a name="line.105"></a>
+<span class="sourceLineNo">106</span>   * At the beginning, we check whether there was empty region server<a name="line.106"></a>
+<span class="sourceLineNo">107</span>   *   just discovered by Master. If so, we alternately choose new / old<a name="line.107"></a>
+<span class="sourceLineNo">108</span>   *   regions from head / tail of regionsToMove, respectively. This alternation<a name="line.108"></a>
+<span class="sourceLineNo">109</span>   *   avoids clustering young regions on the newly discovered region server.<a name="line.109"></a>
+<span class="sourceLineNo">110</span>   *   Otherwise, we choose new regions from head of regionsToMove.<a name="line.110"></a>
+<span class="sourceLineNo">111</span>   *<a name="line.111"></a>
+<span class="sourceLineNo">112</span>   * Another improvement from HBASE-3609 is that we assign regions from<a name="line.112"></a>
+<span class="sourceLineNo">113</span>   *   regionsToMove to underloaded servers in round-robin fashion.<a name="line.113"></a>
+<span class="sourceLineNo">114</span>   *   Previously one underloaded server would be filled before we move onto<a name="line.114"></a>
+<span class="sourceLineNo">115</span>   *   the next underloaded server, leading to clustering of young regions.<a name="line.115"></a>
+<span class="sourceLineNo">116</span>   *<a name="line.116"></a>
+<span class="sourceLineNo">117</span>   * Finally, we randomly shuffle underloaded servers so that they receive<a name="line.117"></a>
+<span class="sourceLineNo">118</span>   *   offloaded regions relatively evenly across calls to balanceCluster().<a name="line.118"></a>
+<span class="sourceLineNo">119</span>   *<a name="line.119"></a>
+<span class="sourceLineNo">120</span>   * The algorithm is currently implemented as such:<a name="line.120"></a>
+<span class="sourceLineNo">121</span>   *<a name="line.121"></a>
+<span class="sourceLineNo">122</span>   * &lt;ol&gt;<a name="line.122"></a>
+<span class="sourceLineNo">123</span>   * &lt;li&gt;Determine the two valid numbers of regions each server should have,<a name="line.123"></a>
+<span class="sourceLineNo">124</span>   *     &lt;b&gt;MIN&lt;/b&gt;=floor(average) and &lt;b&gt;MAX&lt;/b&gt;=ceiling(average).<a name="line.124"></a>
 <span class="sourceLineNo">125</span>   *<a name="line.125"></a>
-<span class="sourceLineNo">126</span>   * &lt;ol&gt;<a name="line.126"></a>
-<span class="sourceLineNo">127</span>   * &lt;li&gt;Determine the two valid numbers of regions each server should have,<a name="line.127"></a>
-<span class="sourceLineNo">128</span>   *     &lt;b&gt;MIN&lt;/b&gt;=floor(average) and &lt;b&gt;MAX&lt;/b&gt;=ceiling(average).<a name="line.128"></a>
-<span class="sourceLineNo">129</span>   *<a name="line.129"></a>
-<span class="sourceLineNo">130</span>   * &lt;li&gt;Iterate down the most loaded servers, shedding regions from each so<a name="line.130"></a>
-<span class="sourceLineNo">131</span>   *     each server hosts exactly &lt;b&gt;MAX&lt;/b&gt; regions.  Stop once you reach a<a name="line.131"></a>
-<span class="sourceLineNo">132</span>   *     server that already has &amp;lt;= &lt;b&gt;MAX&lt;/b&gt; regions.<a name="line.132"></a>
-<span class="sourceLineNo">133</span>   *     &lt;p&gt;<a name="line.133"></a>
-<span class="sourceLineNo">134</span>   *     Order the regions to move from most recent to least.<a name="line.134"></a>
+<span class="sourceLineNo">126</span>   * &lt;li&gt;Iterate down the most loaded servers, shedding regions from each so<a name="line.126"></a>
+<span class="sourceLineNo">127</span>   *     each server hosts exactly &lt;b&gt;MAX&lt;/b&gt; regions.  Stop once you reach a<a name="line.127"></a>
+<span class="sourceLineNo">128</span>   *     server that already has &amp;lt;= &lt;b&gt;MAX&lt;/b&gt; regions.<a name="line.128"></a>
+<span class="sourceLineNo">129</span>   *     &lt;p&gt;<a name="line.129"></a>
+<span class="sourceLineNo">130</span>   *     Order the regions to move from most recent to least.<a name="line.130"></a>
+<span class="sourceLineNo">131</span>   *<a name="line.131"></a>
+<span class="sourceLineNo">132</span>   * &lt;li&gt;Iterate down the least loaded servers, assigning regions so each server<a name="line.132"></a>
+<span class="sourceLineNo">133</span>   *     has exactly &lt;b&gt;MIN&lt;/b&gt; regions.  Stop once you reach a server that<a name="line.133"></a>
+<span class="sourceLineNo">134</span>   *     already has &amp;gt;= &lt;b&gt;MIN&lt;/b&gt; regions.<a name="line.134"></a>
 <span class="sourceLineNo">135</span>   *<a name="line.135"></a>
-<span class="sourceLineNo">136</span>   * &lt;li&gt;Iterate down the least loaded servers, assigning regions so each server<a name="line.136"></a>
-<span class="sourceLineNo">137</span>   *     has exactly &lt;b&gt;MIN&lt;/b&gt; regions.  Stop once you reach a server that<a name="line.137"></a>
-<span class="sourceLineNo">138</span>   *     already has &amp;gt;= &lt;b&gt;MIN&lt;/b&gt; regions.<a name="line.138"></a>
-<span class="sourceLineNo">139</span>   *<a name="line.139"></a>
-<span class="sourceLineNo">140</span>   *     Regions being assigned to underloaded servers are those that were shed<a name="line.140"></a>
-<span class="sourceLineNo">141</span>   *     in the previous step.  It is possible that there were not enough<a name="line.141"></a>
-<span class="sourceLineNo">142</span>   *     regions shed to fill each underloaded server to &lt;b&gt;MIN&lt;/b&gt;.  If so we<a name="line.142"></a>
-<span class="sourceLineNo">143</span>   *     end up with a number of regions required to do so, &lt;b&gt;neededRegions&lt;/b&gt;.<a name="line.143"></a>
+<span class="sourceLineNo">136</span>   *     Regions being assigned to underloaded servers are those that were shed<a name="line.136"></a>
+<span class="sourceLineNo">137</span>   *     in the previous step.  It is possible that there were not enough<a name="line.137"></a>
+<span class="sourceLineNo">138</span>   *     regions shed to fill each underloaded server to &lt;b&gt;MIN&lt;/b&gt;.  If so we<a name="line.138"></a>
+<span class="sourceLineNo">139</span>   *     end up with a number of regions required to do so, &lt;b&gt;neededRegions&lt;/b&gt;.<a name="line.139"></a>
+<span class="sourceLineNo">140</span>   *<a name="line.140"></a>
+<span class="sourceLineNo">141</span>   *     It is also possible that we were able to fill each underloaded but ended<a name="line.141"></a>
+<span class="sourceLineNo">142</span>   *     up with regions that were unassigned from overloaded servers but that<a name="line.142"></a>
+<span class="sourceLineNo">143</span>   *     still do not have assignment.<a name="line.143"></a>
 <span class="sourceLineNo">144</span>   *<a name="line.144"></a>
-<span class="sourceLineNo">145</span>   *     It is also possible that we were able to fill each underloaded but ended<a name="line.145"></a>
-<span class="sourceLineNo">146</span>   *     up with regions that were unassigned from overloaded servers but that<a name="line.146"></a>
-<span class="sourceLineNo">147</span>   *     still do not have assignment.<a name="line.147"></a>
+<span class="sourceLineNo">145</span>   *     If neither of these conditions hold (no regions needed to fill the<a name="line.145"></a>
+<span class="sourceLineNo">146</span>   *     underloaded servers, no regions leftover from overloaded servers),<a name="line.146"></a>
+<span class="sourceLineNo">147</span>   *     we are done and return.  Otherwise we handle these cases below.<a name="line.147"></a>
 <span class="sourceLineNo">148</span>   *<a name="line.148"></a>
-<span class="sourceLineNo">149</span>   *     If neither of these conditions hold (no regions needed to fill the<a name="line.149"></a>
-<span class="sourceLineNo">150</span>   *     underloaded servers, no regions leftover from overloaded servers),<a name="line.150"></a>
-<span class="sourceLineNo">151</span>   *     we are done and return.  Otherwise we handle these cases below.<a name="line.151"></a>
-<span class="sourceLineNo">152</span>   *<a name="line.152"></a>
-<span class="sourceLineNo">153</span>   * &lt;li&gt;If &lt;b&gt;neededRegions&lt;/b&gt; is non-zero (still have underloaded servers),<a name="line.153"></a>
-<span class="sourceLineNo">154</span>   *     we iterate the most loaded servers again, shedding a single server from<a name="line.154"></a>
-<span class="sourceLineNo">155</span>   *     each (this brings them from having &lt;b&gt;MAX&lt;/b&gt; regions to having<a name="line.155"></a>
-<span class="sourceLineNo">156</span>   *     &lt;b&gt;MIN&lt;/b&gt; regions).<a name="line.156"></a>
+<span class="sourceLineNo">149</span>   * &lt;li&gt;If &lt;b&gt;neededRegions&lt;/b&gt; is non-zero (still have underloaded servers),<a name="line.149"></a>
+<span class="sourceLineNo">150</span>   *     we iterate the most loaded servers again, shedding a single server from<a name="line.150"></a>
+<span class="sourceLineNo">151</span>   *     each (this brings them from having &lt;b&gt;MAX&lt;/b&gt; regions to having<a name="line.151"></a>
+<span class="sourceLineNo">152</span>   *     &lt;b&gt;MIN&lt;/b&gt; regions).<a name="line.152"></a>
+<span class="sourceLineNo">153</span>   *<a name="line.153"></a>
+<span class="sourceLineNo">154</span>   * &lt;li&gt;We now definitely have more regions that need assignment, either from<a name="line.154"></a>
+<span class="sourceLineNo">155</span>   *     the previous step or from the original shedding from overloaded servers.<a name="line.155"></a>
+<span class="sourceLineNo">156</span>   *     Iterate the least loaded servers filling each to &lt;b&gt;MIN&lt;/b&gt;.<a name="line.156"></a>
 <span class="sourceLineNo">157</span>   *<a name="line.157"></a>
-<span class="sourceLineNo">158</span>   * &lt;li&gt;We now definitely have more regions that need assignment, either from<a name="line.158"></a>
-<span class="sourceLineNo">159</span>   *     the previous step or from the original shedding from overloaded servers.<a name="line.159"></a>
-<span class="sourceLineNo">160</span>   *     Iterate the least loaded servers filling each to &lt;b&gt;MIN&lt;/b&gt;.<a name="line.160"></a>
+<span class="sourceLineNo">158</span>   * &lt;li&gt;If we still have more regions that need assignment, again iterate the<a name="line.158"></a>
+<span class="sourceLineNo">159</span>   *     least loaded servers, this time giving each one (filling them to<a name="line.159"></a>
+<span class="sourceLineNo">160</span>   *     &lt;b&gt;MAX&lt;/b&gt;) until we run out.<a name="line.160"></a>
 <span class="sourceLineNo">161</span>   *<a name="line.161"></a>
-<span class="sourceLineNo">162</span>   * &lt;li&gt;If we still have more regions that need assignment, again iterate the<a name="line.162"></a>
-<span class="sourceLineNo">163</span>   *     least loaded servers, this time giving each one (filling them to<a name="line.163"></a>
-<span class="sourceLineNo">164</span>   *     &lt;b&gt;MAX&lt;/b&gt;) until we run out.<a name="line.164"></a>
-<span class="sourceLineNo">165</span>   *<a name="line.165"></a>
-<span class="sourceLineNo">166</span>   * &lt;li&gt;All servers will now either host &lt;b&gt;MIN&lt;/b&gt; or &lt;b&gt;MAX&lt;/b&gt; regions.<a name="line.166"></a>
-<span class="sourceLineNo">167</span>   *<a name="line.167"></a>
-<span class="sourceLineNo">168</span>   *     In addition, any server hosting &amp;gt;= &lt;b&gt;MAX&lt;/b&gt; regions is guaranteed<a name="line.168"></a>
-<span class="sourceLineNo">169</span>   *     to end up with &lt;b&gt;MAX&lt;/b&gt; regions at the end of the balancing.  This<a name="line.169"></a>
-<span class="sourceLineNo">170</span>   *     ensures the minimal number of regions possible are moved.<a name="line.170"></a>
-<span class="sourceLineNo">171</span>   * &lt;/ol&gt;<a name="line.171"></a>
-<span class="sourceLineNo">172</span>   *<a name="line.172"></a>
-<span class="sourceLineNo">173</span>   * TODO: We can at-most reassign the number of regions away from a particular<a name="line.173"></a>
-<span class="sourceLineNo">174</span>   *       server to be how many they report as most loaded.<a name="line.174"></a>
-<span class="sourceLineNo">175</span>   *       Should we just keep all assignment in memory?  Any objections?<a name="line.175"></a>
-<span class="sourceLineNo">176</span>   *       Does this mean we need HeapSize on HMaster?  Or just careful monitor?<a name="line.176"></a>
-<span class="sourceLineNo">177</span>   *       (current thinking is we will hold all assignments in memory)<a name="line.177"></a>
-<span class="sourceLineNo">178</span>   *<a name="line.178"></a>
-<span class="sourceLineNo">179</span>   * @param clusterMap Map of regionservers and their load/region information to<a name="line.179"></a>
-<span class="sourceLineNo">180</span>   *                   a list of their most loaded regions<a name="line.180"></a>
-<span class="sourceLineNo">181</span>   * @return a list of regions to be moved, including source and destination,<a name="line.181"></a>
-<span class="sourceLineNo">182</span>   *         or null if cluster is already balanced<a name="line.182"></a>
-<span class="sourceLineNo">183</span>   */<a name="line.183"></a>
-<span class="sourceLineNo">184</span>  @Override<a name="line.184"></a>
-<span class="sourceLineNo">185</span>  public List&lt;RegionPlan&gt; balanceCluster(<a name="line.185"></a>
-<span class="sourceLineNo">186</span>      Map&lt;ServerName, List&lt;HRegionInfo&gt;&gt; clusterMap) {<a name="line.186"></a>
-<span class="sourceLineNo">187</span>    List&lt;RegionPlan&gt; regionsToReturn = balanceMasterRegions(clusterMap);<a name="line.187"></a>
-<span class="sourceLineNo">188</span>    if (regionsToReturn != null || clusterMap == null || clusterMap.size() &lt;= 1) {<a name="line.188"></a>
-<span class="sourceLineNo">189</span>      return regionsToReturn;<a name="line.189"></a>
-<span class="sourceLineNo">190</span>    }<a name="line.190"></a>
-<span class="sourceLineNo">191</span>    if (masterServerName != null &amp;&amp; clusterMap.containsKey(masterServerName)) {<a name="line.191"></a>
-<span class="sourceLineNo">192</span>      if (clusterMap.size() &lt;= 2) {<a name="line.192"></a>
-<span class="sourceLineNo">193</span>        return null;<a name="line.193"></a>
-<span class="sourceLineNo">194</span>      }<a name="line.194"></a>
-<span class="sourceLineNo">195</span>      clusterMap = new HashMap&lt;ServerName, List&lt;HRegionInfo&gt;&gt;(clusterMap);<a name="line.195"></a>
-<span class="sourceLineNo">196</span>      clusterMap.remove(masterServerName);<a name="line.196"></a>
-<span class="sourceLineNo">197</span>    }<a name="line.197"></a>
-<span class="sourceLineNo">198</span><a name="line.198"></a>
-<span class="sourceLineNo">199</span>    long startTime = System.currentTimeMillis();<a name="line.199"></a>
-<span class="sourceLineNo">200</span><a name="line.200"></a>
-<span class="sourceLineNo">201</span>    // construct a Cluster object with clusterMap and rest of the<a name="line.201"></a>
-<span class="sourceLineNo">202</span>    // argument as defaults<a name="line.202"></a>
-<span class="sourceLineNo">203</span>    Cluster c = new Cluster(clusterMap, null, this.regionFinder, this.rackManager);<a name="line.203"></a>
-<span class="sourceLineNo">204</span>    if (!this.needsBalance(c)) return null;<a name="line.204"></a>
-<span class="sourceLineNo">205</span><a name="line.205"></a>
-<span class="sourceLineNo">206</span>    ClusterLoadState cs = new ClusterLoadState(clusterMap);<a name="line.206"></a>
-<span class="sourceLineNo">207</span>    int numServers = cs.getNumServers();<a name="line.207"></a>
-<span class="sourceLineNo">208</span>    NavigableMap&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; serversByLoad = cs.getServersByLoad();<a name="line.208"></a>
-<span class="sourceLineNo">209</span>    int numRegions = cs.getNumRegions();<a name="line.209"></a>
-<span class="sourceLineNo">210</span>    float average = cs.getLoadAverage();<a name="line.210"></a>
-<span class="sourceLineNo">211</span>    int max = (int)Math.ceil(average);<a name="line.211"></a>
-<span class="sourceLineNo">212</span>    int min = (int)average;<a name="line.212"></a>
-<span class="sourceLineNo">213</span><a name="line.213"></a>
-<span class="sourceLineNo">214</span>    // Using to check balance result.<a name="line.214"></a>
-<span class="sourceLineNo">215</span>    StringBuilder strBalanceParam = new StringBuilder();<a name="line.215"></a>
-<span class="sourceLineNo">216</span>    strBalanceParam.append("Balance parameter: numRegions=").append(numRegions)<a name="line.216"></a>
-<span class="sourceLineNo">217</span>        .append(", numServers=").append(numServers).append(", max=").append(max)<a name="line.217"></a>
-<span class="sourceLineNo">218</span>        .append(", min=").append(min);<a name="line.218"></a>
-<span class="sourceLineNo">219</span>    LOG.debug(strBalanceParam.toString());<a name="line.219"></a>
-<span class="sourceLineNo">220</span><a name="line.220"></a>
-<span class="sourceLineNo">221</span>    // Balance the cluster<a name="line.221"></a>
-<span class="sourceLineNo">222</span>    // TODO: Look at data block locality or a more complex load to do this<a name="line.222"></a>
-<span class="sourceLineNo">223</span>    MinMaxPriorityQueue&lt;RegionPlan&gt; regionsToMove =<a name="line.223"></a>
-<span class="sourceLineNo">224</span>      MinMaxPriorityQueue.orderedBy(rpComparator).create();<a name="line.224"></a>
-<span class="sourceLineNo">225</span>    regionsToReturn = new ArrayList&lt;RegionPlan&gt;();<a name="line.225"></a>
-<span class="sourceLineNo">226</span><a name="line.226"></a>
-<span class="sourceLineNo">227</span>    // Walk down most loaded, pruning each to the max<a name="line.227"></a>
-<span class="sourceLineNo">228</span>    int serversOverloaded = 0;<a name="line.228"></a>
-<span class="sourceLineNo">229</span>    // flag used to fetch regions from head and tail of list, alternately<a name="line.229"></a>
-<span class="sourceLineNo">230</span>    boolean fetchFromTail = false;<a name="line.230"></a>
-<span class="sourceLineNo">231</span>    Map&lt;ServerName, BalanceInfo&gt; serverBalanceInfo =<a name="line.231"></a>
-<span class="sourceLineNo">232</span>      new TreeMap&lt;ServerName, BalanceInfo&gt;();<a name="line.232"></a>
-<span class="sourceLineNo">233</span>    for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server:<a name="line.233"></a>
-<span class="sourceLineNo">234</span>        serversByLoad.descendingMap().entrySet()) {<a name="line.234"></a>
-<span class="sourceLineNo">235</span>      ServerAndLoad sal = server.getKey();<a name="line.235"></a>
-<span class="sourceLineNo">236</span>      int load = sal.getLoad();<a name="line.236"></a>
-<span class="sourceLineNo">237</span>      if (load &lt;= max) {<a name="line.237"></a>
-<span class="sourceLineNo">238</span>        serverBalanceInfo.put(sal.getServerName(), new BalanceInfo(0, 0));<a name="line.238"></a>
-<span class="sourceLineNo">239</span>        break;<a name="line.239"></a>
-<span class="sourceLineNo">240</span>      }<a name="line.240"></a>
-<span class="sourceLineNo">241</span>      serversOverloaded++;<a name="line.241"></a>
-<span class="sourceLineNo">242</span>      List&lt;HRegionInfo&gt; regions = server.getValue();<a name="line.242"></a>
-<span class="sourceLineNo">243</span>      int numToOffload = Math.min(load - max, regions.size());<a name="line.243"></a>
-<span class="sourceLineNo">244</span>      // account for the out-of-band regions which were assigned to this server<a name="line.244"></a>
-<span class="sourceLineNo">245</span>      // after some other region server crashed<a name="line.245"></a>
-<span class="sourceLineNo">246</span>      Collections.sort(regions, riComparator);<a name="line.246"></a>
-<span class="sourceLineNo">247</span>      int numTaken = 0;<a name="line.247"></a>
-<span class="sourceLineNo">248</span>      for (int i = 0; i &lt;= numToOffload; ) {<a name="line.248"></a>
-<span class="sourceLineNo">249</span>        HRegionInfo hri = regions.get(i); // fetch from head<a name="line.249"></a>
-<span class="sourceLineNo">250</span>        if (fetchFromTail) {<a name="line.250"></a>
-<span class="sourceLineNo">251</span>          hri = regions.get(regions.size() - 1 - i);<a name="line.251"></a>
-<span class="sourceLineNo">252</span>        }<a name="line.252"></a>
-<span class="sourceLineNo">253</span>        i++;<a name="line.253"></a>
-<span class="sourceLineNo">254</span>        // Don't rebalance special regions.<a name="line.254"></a>
-<span class="sourceLineNo">255</span>        if (shouldBeOnMaster(hri)<a name="line.255"></a>
-<span class="sourceLineNo">256</span>            &amp;&amp; masterServerName.equals(sal.getServerName())) continue;<a name="line.256"></a>
-<span class="sourceLineNo">257</span>        regionsToMove.add(new RegionPlan(hri, sal.getServerName(), null));<a name="line.257"></a>
-<span class="sourceLineNo">258</span>        numTaken++;<a name="line.258"></a>
-<span class="sourceLineNo">259</span>        if (numTaken &gt;= numToOffload) break;<a name="line.259"></a>
-<span class="sourceLineNo">260</span>      }<a name="line.260"></a>
-<span class="sourceLineNo">261</span>      serverBalanceInfo.put(sal.getServerName(),<a name="line.261"></a>
-<span class="sourceLineNo">262</span>        new BalanceInfo(numToOffload, (-1)*numTaken));<a name="line.262"></a>
-<span class="sourceLineNo">263</span>    }<a name="line.263"></a>
-<span class="sourceLineNo">264</span>    int totalNumMoved = regionsToMove.size();<a name="line.264"></a>
+<span class="sourceLineNo">162</span>   * &lt;li&gt;All servers will now either host &lt;b&gt;MIN&lt;/b&gt; or &lt;b&gt;MAX&lt;/b&gt; regions.<a name="line.162"></a>
+<span class="sourceLineNo">163</span>   *<a name="line.163"></a>
+<span class="sourceLineNo">164</span>   *     In addition, any server hosting &amp;gt;= &lt;b&gt;MAX&lt;/b&gt; regions is guaranteed<a name="line.164"></a>
+<span class="sourceLineNo">165</span>   *     to end up with &lt;b&gt;MAX&lt;/b&gt; regions at the end of the balancing.  This<a name="line.165"></a>
+<span class="sourceLineNo">166</span>   *     ensures the minimal number of regions possible are moved.<a name="line.166"></a>
+<span class="sourceLineNo">167</span>   * &lt;/ol&gt;<a name="line.167"></a>
+<span class="sourceLineNo">168</span>   *<a name="line.168"></a>
+<span class="sourceLineNo">169</span>   * TODO: We can at-most reassign the number of regions away from a particular<a name="line.169"></a>
+<span class="sourceLineNo">170</span>   *       server to be how many they report as most loaded.<a name="line.170"></a>
+<span class="sourceLineNo">171</span>   *       Should we just keep all assignment in memory?  Any objections?<a name="line.171"></a>
+<span class="sourceLineNo">172</span>   *       Does this mean we need HeapSize on HMaster?  Or just careful monitor?<a name="line.172"></a>
+<span class="sourceLineNo">173</span>   *       (current thinking is we will hold all assignments in memory)<a name="line.173"></a>
+<span class="sourceLineNo">174</span>   *<a name="line.174"></a>
+<span class="sourceLineNo">175</span>   * @param clusterMap Map of regionservers and their load/region information to<a name="line.175"></a>
+<span class="sourceLineNo">176</span>   *                   a list of their most loaded regions<a name="line.176"></a>
+<span class="sourceLineNo">177</span>   * @return a list of regions to be moved, including source and destination,<a name="line.177"></a>
+<span class="sourceLineNo">178</span>   *         or null if cluster is already balanced<a name="line.178"></a>
+<span class="sourceLineNo">179</span>   */<a name="line.179"></a>
+<span class="sourceLineNo">180</span>  @Override<a name="line.180"></a>
+<span class="sourceLineNo">181</span>  public List&lt;RegionPlan&gt; balanceCluster(<a name="line.181"></a>
+<span class="sourceLineNo">182</span>      Map&lt;ServerName, List&lt;HRegionInfo&gt;&gt; clusterMap) {<a name="line.182"></a>
+<span class="sourceLineNo">183</span>    List&lt;RegionPlan&gt; regionsToReturn = balanceMasterRegions(clusterMap);<a name="line.183"></a>
+<span class="sourceLineNo">184</span>    if (regionsToReturn != null || clusterMap == null || clusterMap.size() &lt;= 1) {<a name="line.184"></a>
+<span class="sourceLineNo">185</span>      return regionsToReturn;<a name="line.185"></a>
+<span class="sourceLineNo">186</span>    }<a name="line.186"></a>
+<span class="sourceLineNo">187</span>    if (masterServerName != null &amp;&amp; clusterMap.containsKey(masterServerName)) {<a name="line.187"></a>
+<span class="sourceLineNo">188</span>      if (clusterMap.size() &lt;= 2) {<a name="line.188"></a>
+<span class="sourceLineNo">189</span>        return null;<a name="line.189"></a>
+<span class="sourceLineNo">190</span>      }<a name="line.190"></a>
+<span class="sourceLineNo">191</span>      clusterMap = new HashMap&lt;ServerName, List&lt;HRegionInfo&gt;&gt;(clusterMap);<a name="line.191"></a>
+<span class="sourceLineNo">192</span>      clusterMap.remove(masterServerName);<a name="line.192"></a>
+<span class="sourceLineNo">193</span>    }<a name="line.193"></a>
+<span class="sourceLineNo">194</span><a name="line.194"></a>
+<span class="sourceLineNo">195</span>    long startTime = System.currentTimeMillis();<a name="line.195"></a>
+<span class="sourceLineNo">196</span><a name="line.196"></a>
+<span class="sourceLineNo">197</span>    // construct a Cluster object with clusterMap and rest of the<a name="line.197"></a>
+<span class="sourceLineNo">198</span>    // argument as defaults<a name="line.198"></a>
+<span class="sourceLineNo">199</span>    Cluster c = new Cluster(clusterMap, null, this.regionFinder, this.rackManager);<a name="line.199"></a>
+<span class="sourceLineNo">200</span>    if (!this.needsBalance(c)) return null;<a name="line.200"></a>
+<span class="sourceLineNo">201</span><a name="line.201"></a>
+<span class="sourceLineNo">202</span>    ClusterLoadState cs = new ClusterLoadState(clusterMap);<a name="line.202"></a>
+<span class="sourceLineNo">203</span>    int numServers = cs.getNumServers();<a name="line.203"></a>
+<span class="sourceLineNo">204</span>    NavigableMap&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; serversByLoad = cs.getServersByLoad();<a name="line.204"></a>
+<span class="sourceLineNo">205</span>    int numRegions = cs.getNumRegions();<a name="line.205"></a>
+<span class="sourceLineNo">206</span>    float average = cs.getLoadAverage();<a name="line.206"></a>
+<span class="sourceLineNo">207</span>    int max = (int)Math.ceil(average);<a name="line.207"></a>
+<span class="sourceLineNo">208</span>    int min = (int)average;<a name="line.208"></a>
+<span class="sourceLineNo">209</span><a name="line.209"></a>
+<span class="sourceLineNo">210</span>    // Using to check balance result.<a name="line.210"></a>
+<span class="sourceLineNo">211</span>    StringBuilder strBalanceParam = new StringBuilder();<a name="line.211"></a>
+<span class="sourceLineNo">212</span>    strBalanceParam.append("Balance parameter: numRegions=").append(numRegions)<a name="line.212"></a>
+<span class="sourceLineNo">213</span>        .append(", numServers=").append(numServers).append(", max=").append(max)<a name="line.213"></a>
+<span class="sourceLineNo">214</span>        .append(", min=").append(min);<a name="line.214"></a>
+<span class="sourceLineNo">215</span>    LOG.debug(strBalanceParam.toString());<a name="line.215"></a>
+<span class="sourceLineNo">216</span><a name="line.216"></a>
+<span class="sourceLineNo">217</span>    // Balance the cluster<a name="line.217"></a>
+<span class="sourceLineNo">218</span>    // TODO: Look at data block locality or a more complex load to do this<a name="line.218"></a>
+<span class="sourceLineNo">219</span>    MinMaxPriorityQueue&lt;RegionPlan&gt; regionsToMove =<a name="line.219"></a>
+<span class="sourceLineNo">220</span>      MinMaxPriorityQueue.orderedBy(rpComparator).create();<a name="line.220"></a>
+<span class="sourceLineNo">221</span>    regionsToReturn = new ArrayList&lt;RegionPlan&gt;();<a name="line.221"></a>
+<span class="sourceLineNo">222</span><a name="line.222"></a>
+<span class="sourceLineNo">223</span>    // Walk down most loaded, pruning each to the max<a name="line.223"></a>
+<span class="sourceLineNo">224</span>    int serversOverloaded = 0;<a name="line.224"></a>
+<span class="sourceLineNo">225</span>    // flag used to fetch regions from head and tail of list, alternately<a name="line.225"></a>
+<span class="sourceLineNo">226</span>    boolean fetchFromTail = false;<a name="line.226"></a>
+<span class="sourceLineNo">227</span>    Map&lt;ServerName, BalanceInfo&gt; serverBalanceInfo =<a name="line.227"></a>
+<span class="sourceLineNo">228</span>      new TreeMap&lt;ServerName, BalanceInfo&gt;();<a name="line.228"></a>
+<span class="sourceLineNo">229</span>    for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server:<a name="line.229"></a>
+<span class="sourceLineNo">230</span>        serversByLoad.descendingMap().entrySet()) {<a name="line.230"></a>
+<span class="sourceLineNo">231</span>      ServerAndLoad sal = server.getKey();<a name="line.231"></a>
+<span class="sourceLineNo">232</span>      int load = sal.getLoad();<a name="line.232"></a>
+<span class="sourceLineNo">233</span>      if (load &lt;= max) {<a name="line.233"></a>
+<span class="sourceLineNo">234</span>        serverBalanceInfo.put(sal.getServerName(), new BalanceInfo(0, 0));<a name="line.234"></a>
+<span class="sourceLineNo">235</span>        break;<a name="line.235"></a>
+<span class="sourceLineNo">236</span>      }<a name="line.236"></a>
+<span class="sourceLineNo">237</span>      serversOverloaded++;<a name="line.237"></a>
+<span class="sourceLineNo">238</span>      List&lt;HRegionInfo&gt; regions = server.getValue();<a name="line.238"></a>
+<span class="sourceLineNo">239</span>      int numToOffload = Math.min(load - max, regions.size());<a name="line.239"></a>
+<span class="sourceLineNo">240</span>      // account for the out-of-band regions which were assigned to this server<a name="line.240"></a>
+<span class="sourceLineNo">241</span>      // after some other region server crashed<a name="line.241"></a>
+<span class="sourceLineNo">242</span>      Collections.sort(regions, riComparator);<a name="line.242"></a>
+<span class="sourceLineNo">243</span>      int numTaken = 0;<a name="line.243"></a>
+<span class="sourceLineNo">244</span>      for (int i = 0; i &lt;= numToOffload; ) {<a name="line.244"></a>
+<span class="sourceLineNo">245</span>        HRegionInfo hri = regions.get(i); // fetch from head<a name="line.245"></a>
+<span class="sourceLineNo">246</span>        if (fetchFromTail) {<a name="line.246"></a>
+<span class="sourceLineNo">247</span>          hri = regions.get(regions.size() - 1 - i);<a name="line.247"></a>
+<span class="sourceLineNo">248</span>        }<a name="line.248"></a>
+<span class="sourceLineNo">249</span>        i++;<a name="line.249"></a>
+<span class="sourceLineNo">250</span>        // Don't rebalance special regions.<a name="line.250"></a>
+<span class="sourceLineNo">251</span>        if (shouldBeOnMaster(hri)<a name="line.251"></a>
+<span class="sourceLineNo">252</span>            &amp;&amp; masterServerName.equals(sal.getServerName())) continue;<a name="line.252"></a>
+<span class="sourceLineNo">253</span>        regionsToMove.add(new RegionPlan(hri, sal.getServerName(), null));<a name="line.253"></a>
+<span class="sourceLineNo">254</span>        numTaken++;<a name="line.254"></a>
+<span class="sourceLineNo">255</span>        if (numTaken &gt;= numToOffload) break;<a name="line.255"></a>
+<span class="sourceLineNo">256</span>      }<a name="line.256"></a>
+<span class="sourceLineNo">257</span>      serverBalanceInfo.put(sal.getServerName(),<a name="line.257"></a>
+<span class="sourceLineNo">258</span>        new BalanceInfo(numToOffload, (-1)*numTaken));<a name="line.258"></a>
+<span class="sourceLineNo">259</span>    }<a name="line.259"></a>
+<span class="sourceLineNo">260</span>    int totalNumMoved = regionsToMove.size();<a name="line.260"></a>
+<span class="sourceLineNo">261</span><a name="line.261"></a>
+<span class="sourceLineNo">262</span>    // Walk down least loaded, filling each to the min<a name="line.262"></a>
+<span class="sourceLineNo">263</span>    int neededRegions = 0; // number of regions needed to bring all up to min<a name="line.263"></a>
+<span class="sourceLineNo">264</span>    fetchFromTail = false;<a name="line.264"></a>
 <span class="sourceLineNo">265</span><a name="line.265"></a>
-<span class="sourceLineNo">266</span>    // Walk down least loaded, filling each to the min<a name="line.266"></a>
-<span class="sourceLineNo">267</span>    int neededRegions = 0; // number of regions needed to bring all up to min<a name="line.267"></a>
-<span class="sourceLineNo">268</span>    fetchFromTail = false;<a name="line.268"></a>
-<span class="sourceLineNo">269</span><a name="line.269"></a>
-<span class="sourceLineNo">270</span>    Map&lt;ServerName, Integer&gt; underloadedServers = new HashMap&lt;ServerName, Integer&gt;();<a name="line.270"></a>
-<span class="sourceLineNo">271</span>    int maxToTake = numRegions - min;<a name="line.271"></a>
-<span class="sourceLineNo">272</span>    for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server:<a name="line.272"></a>
-<span class="sourceLineNo">273</span>        serversByLoad.entrySet()) {<a name="line.273"></a>
-<span class="sourceLineNo">274</span>      if (maxToTake == 0) break; // no more to take<a name="line.274"></a>
-<span class="sourceLineNo">275</span>      int load = server.getKey().getLoad();<a name="line.275"></a>
-<span class="sourceLineNo">276</span>      if (load &gt;= min &amp;&amp; load &gt; 0) {<a name="line.276"></a>
-<span class="sourceLineNo">277</span>        continue; // look for other servers which haven't reached min<a name="line.277"></a>
-<span class="sourceLineNo">278</span>      }<a name="line.278"></a>
-<span class="sourceLineNo">279</span>      int regionsToPut = min - load;<a name="line.279"></a>
-<span class="sourceLineNo">280</span>      if (regionsToPut == 0)<a name="line.280"></a>
-<span class="sourceLineNo">281</span>      {<a name="line.281"></a>
-<span class="sourceLineNo">282</span>        regionsToPut = 1;<a name="line.282"></a>
-<span class="sourceLineNo">283</span>      }<a name="line.283"></a>
-<span class="sourceLineNo">284</span>      maxToTake -= regionsToPut;<a name="line.284"></a>
-<span class="sourceLineNo">285</span>      underloadedServers.put(server.getKey().getServerName(), regionsToPut);<a name="line.285"></a>
-<span class="sourceLineNo">286</span>    }<a name="line.286"></a>
-<span class="sourceLineNo">287</span>    // number of servers that get new regions<a name="line.287"></a>
-<span class="sourceLineNo">288</span>    int serversUnderloaded = underloadedServers.size();<a name="line.288"></a>
-<span class="sourceLineNo">289</span>    int incr = 1;<a name="line.289"></a>
-<span class="sourceLineNo">290</span>    List&lt;ServerName&gt; sns =<a name="line.290"></a>
-<span class="sourceLineNo">291</span>      Arrays.asList(underloadedServers.keySet().toArray(new ServerName[serversUnderloaded]));<a name="line.291"></a>
-<span class="sourceLineNo">292</span>    Collections.shuffle(sns, RANDOM);<a name="line.292"></a>
-<span class="sourceLineNo">293</span>    while (regionsToMove.size() &gt; 0) {<a name="line.293"></a>
-<span class="sourceLineNo">294</span>      int cnt = 0;<a name="line.294"></a>
-<span class="sourceLineNo">295</span>      int i = incr &gt; 0 ? 0 : underloadedServers.size()-1;<a name="line.295"></a>
-<span class="sourceLineNo">296</span>      for (; i &gt;= 0 &amp;&amp; i &lt; underloadedServers.size(); i += incr) {<a name="line.296"></a>
-<span class="sourceLineNo">297</span>        if (regionsToMove.isEmpty()) break;<a name="line.297"></a>
-<span class="sourceLineNo">298</span>        ServerName si = sns.get(i);<a name="line.298"></a>
-<span class="sourceLineNo">299</span>        int numToTake = underloadedServers.get(si);<a name="line.299"></a>
-<span class="sourceLineNo">300</span>        if (numToTake == 0) continue;<a name="line.300"></a>
-<span class="sourceLineNo">301</span><a name="line.301"></a>
-<span class="sourceLineNo">302</span>        addRegionPlan(regionsToMove, fetchFromTail, si, regionsToReturn);<a name="line.302"></a>
-<span class="sourceLineNo">303</span><a name="line.303"></a>
-<span class="sourceLineNo">304</span>        underloadedServers.put(si, numToTake-1);<a name="line.304"></a>
-<span class="sourceLineNo">305</span>        cnt++;<a name="line.305"></a>
-<span class="sourceLineNo">306</span>        BalanceInfo bi = serverBalanceInfo.get(si);<a name="line.306"></a>
-<span class="sourceLineNo">307</span>        if (bi == null) {<a name="line.307"></a>
-<span class="sourceLineNo">308</span>          bi = new BalanceInfo(0, 0);<a name="line.308"></a>
-<span class="sourceLineNo">309</span>          serverBalanceInfo.put(si, bi);<a name="line.309"></a>
-<span class="sourceLineNo">310</span>        }<a name="line.310"></a>
-<span class="sourceLineNo">311</span>        bi.setNumRegionsAdded(bi.getNumRegionsAdded()+1);<a name="line.311"></a>
-<span class="sourceLineNo">312</span>      }<a name="line.312"></a>
-<span class="sourceLineNo">313</span>      if (cnt == 0) break;<a name="line.313"></a>
-<span class="sourceLineNo">314</span>      // iterates underloadedServers in the other direction<a name="line.314"></a>
-<span class="sourceLineNo">315</span>      incr = -incr;<a name="line.315"></a>
+<span class="sourceLineNo">266</span>    Map&lt;ServerName, Integer&gt; underloadedServers = new HashMap&lt;ServerName, Integer&gt;();<a name="line.266"></a>
+<span class="sourceLineNo">267</span>    int maxToTake = numRegions - min;<a name="line.267"></a>
+<span class="sourceLineNo">268</span>    for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server:<a name="line.268"></a>
+<span class="sourceLineNo">269</span>        serversByLoad.entrySet()) {<a name="line.269"></a>
+<span class="sourceLineNo">270</span>      if (maxToTake == 0) break; // no more to take<a name="line.270"></a>
+<span class="sourceLineNo">271</span>      int load = server.getKey().getLoad();<a name="line.271"></a>
+<span class="sourceLineNo">272</span>      if (load &gt;= min &amp;&amp; load &gt; 0) {<a name="line.272"></a>
+<span class="sourceLineNo">273</span>        continue; // look for other servers which haven't reached min<a name="line.273"></a>
+<span class="sourceLineNo">274</span>      }<a name="line.274"></a>
+<span class="sourceLineNo">275</span>      int regionsToPut = min - load;<a name="line.275"></a>
+<span class="sourceLineNo">276</span>      if (regionsToPut == 0)<a name="line.276"></a>
+<span class="sourceLineNo">277</span>      {<a name="line.277"></a>
+<span class="sourceLineNo">278</span>        regionsToPut = 1;<a name="line.278"></a>
+<span class="sourceLineNo">279</span>      }<a name="line.279"></a>
+<span class="sourceLineNo">280</span>      maxToTake -= regionsToPut;<a name="line.280"></a>
+<span class="sourceLineNo">281</span>      underloadedServers.put(server.getKey().getServerName(), regionsToPut);<a name="line.281"></a>
+<span class="sourceLineNo">282</span>    }<a name="line.282"></a>
+<span class="sourceLineNo">283</span>    // number of servers that get new regions<a name="line.283"></a>
+<span class="sourceLineNo">284</span>    int serversUnderloaded = underloadedServers.size();<a name="line.284"></a>
+<span class="sourceLineNo">285</span>    int incr = 1;<a name="line.285"></a>
+<span class="sourceLineNo">286</span>    List&lt;ServerName&gt; sns =<a name="line.286"></a>
+<span class="sourceLineNo">287</span>      Arrays.asList(underloadedServers.keySet().toArray(new ServerName[serversUnderloaded]));<a name="line.287"></a>
+<span class="sourceLineNo">288</span>    Collections.shuffle(sns, RANDOM);<a name="line.288"></a>
+<span class="sourceLineNo">289</span>    while (regionsToMove.size() &gt; 0) {<a name="line.289"></a>
+<span class="sourceLineNo">290</span>      int cnt = 0;<a name="line.290"></a>
+<span class="sourceLineNo">291</span>      int i = incr &gt; 0 ? 0 : underloadedServers.size()-1;<a name="line.291"></a>
+<span class="sourceLineNo">292</span>      for (; i &gt;= 0 &amp;&amp; i &lt; underloadedServers.size(); i += incr) {<a name="line.292"></a>
+<span class="sourceLineNo">293</span>        if (regionsToMove.isEmpty()) break;<a name="line.293"></a>
+<span class="sourceLineNo">294</span>        ServerName si = sns.get(i);<a name="line.294"></a>
+<span class="sourceLineNo">295</span>        int numToTake = underloadedServers.get(si);<a name="line.295"></a>
+<span class="sourceLineNo">296</span>        if (numToTake == 0) continue;<a name="line.296"></a>
+<span class="sourceLineNo">297</span><a name="line.297"></a>
+<span class="sourceLineNo">298</span>        addRegionPlan(regionsToMove, fetchFromTail, si, regionsToReturn);<a name="line.298"></a>
+<span class="sourceLineNo">299</span><a name="line.299"></a>
+<span class="sourceLineNo">300</span>        underloadedServers.put(si, numToTake-1);<a name="line.300"></a>
+<span class="sourceLineNo">301</span>        cnt++;<a name="line.301"></a>
+<span class="sourceLineNo">302</span>        BalanceInfo bi = serverBalanceInfo.get(si);<a name="line.302"></a>
+<span class="sourceLineNo">303</span>        if (bi == null) {<a name="line.303"></a>
+<span class="sourceLineNo">304</span>          bi = new BalanceInfo(0, 0);<a name="line.304"></a>
+<span class="sourceLineNo">305</span>          serverBalanceInfo.put(si, bi);<a name="line.305"></a>
+<span class="sourceLineNo">306</span>        }<a name="line.306"></a>
+<span class="sourceLineNo">307</span>        bi.setNumRegionsAdded(bi.getNumRegionsAdded()+1);<a name="line.307"></a>
+<span class="sourceLineNo">308</span>      }<a name="line.308"></a>
+<span class="sourceLineNo">309</span>      if (cnt == 0) break;<a name="line.309"></a>
+<span class="sourceLineNo">310</span>      // iterates underloadedServers in the other direction<a name="line.310"></a>
+<span class="sourceLineNo">311</span>      incr = -incr;<a name="line.311"></a>
+<span class="sourceLineNo">312</span>    }<a name="line.312"></a>
+<span class="sourceLineNo">313</span>    for (Integer i : underloadedServers.values()) {<a name="line.313"></a>
+<span class="sourceLineNo">314</span>      // If we still want to take some, increment needed<a name="line.314"></a>
+<span class="sourceLineNo">315</span>      neededRegions += i;<a name="line.315"></a>
 <span class="sourceLineNo">316</span>    }<a name="line.316"></a>
-<span class="sourceLineNo">317</span>    for (Integer i : underloadedServers.values()) {<a name="line.317"></a>
-<span class="sourceLineNo">318</span>      // If we still want to take some, increment needed<a name="line.318"></a>
-<span class="sourceLineNo">319</span>      neededRegions += i;<a name="line.319"></a>
-<span class="sourceLineNo">320</span>    }<a name="line.320"></a>
-<span class="sourceLineNo">321</span><a name="line.321"></a>
-<span class="sourceLineNo">322</span>    // If none needed to fill all to min and none left to drain all to max,<a name="line.322"></a>
-<span class="sourceLineNo">323</span>    // we are done<a name="line.323"></a>
-<span class="sourceLineNo">324</span>    if (neededRegions == 0 &amp;&amp; regionsToMove.isEmpty()) {<a name="line.324"></a>
-<span class="sourceLineNo">325</span>      long endTime = System.currentTimeMillis();<a name="line.325"></a>
-<span class="sourceLineNo">326</span>      LOG.info("Calculated a load balance in " + (endTime-startTime) + "ms. " +<a name="line.326"></a>
-<span class="sourceLineNo">327</span>          "Moving " + totalNumMoved + " regions off of " +<a name="line.327"></a>
-<span class="sourceLineNo">328</span>          serversOverloaded + " overloaded servers onto " +<a name="line.328"></a>
-<span class="sourceLineNo">329</span>          serversUnderloaded + " less loaded servers");<a name="line.329"></a>
-<span class="sourceLineNo">330</span>      return regionsToReturn;<a name="line.330"></a>
-<span class="sourceLineNo">331</span>    }<a name="line.331"></a>
-<span class="sourceLineNo">332</span><a name="line.332"></a>
-<span class="sourceLineNo">333</span>    // Need to do a second pass.<a name="line.333"></a>
-<span class="sourceLineNo">334</span>    // Either more regions to assign out or servers that are still underloaded<a name="line.334"></a>
-<span class="sourceLineNo">335</span><a name="line.335"></a>
-<span class="sourceLineNo">336</span>    // If we need more to fill min, grab one from each most loaded until enough<a name="line.336"></a>
-<span class="sourceLineNo">337</span>    if (neededRegions != 0) {<a name="line.337"></a>
-<span class="sourceLineNo">338</span>      // Walk down most loaded, grabbing one from each until we get enough<a name="line.338"></a>
-<span class="sourceLineNo">339</span>      for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server :<a name="line.339"></a>
-<span class="sourceLineNo">340</span>        serversByLoad.descendingMap().entrySet()) {<a name="line.340"></a>
-<span class="sourceLineNo">341</span>        BalanceInfo balanceInfo =<a name="line.341"></a>
-<span class="sourceLineNo">342</span>          serverBalanceInfo.get(server.getKey().getServerName());<a name="line.342"></a>
-<span class="sourceLineNo">343</span>        int idx =<a name="line.343"></a>
-<span class="sourceLineNo">344</span>          balanceInfo == null ? 0 : balanceInfo.getNextRegionForUnload();<a name="line.344"></a>
-<span class="sourceLineNo">345</span>        if (idx &gt;= server.getValue().size()) break;<a name="line.345"></a>
-<span class="sourceLineNo">346</span>        HRegionInfo region = server.getValue().get(idx);<a name="line.346"></a>
-<span class="sourceLineNo">347</span>        if (region.isMetaRegion()) continue; // Don't move meta regions.<a name="line.347"></a>
-<span class="sourceLineNo">348</span>        regionsToMove.add(new RegionPlan(region, server.getKey().getServerName(), null));<a name="line.348"></a>
-<span class="sourceLineNo">349</span>        totalNumMoved++;<a name="line.349"></a>
-<span class="sourceLineNo">350</span>        if (--neededRegions == 0) {<a name="line.350"></a>
-<span class="sourceLineNo">351</span>          // No more regions needed, done shedding<a name="line.351"></a>
-<span class="sourceLineNo">352</span>          break;<a name="line.352"></a>
-<span class="sourceLineNo">353</span>        }<a name="line.353"></a>
-<span class="sourceLineNo">354</span>      }<a name="line.354"></a>
-<span class="sourceLineNo">355</span>    }<a name="line.355"></a>
-<span class="sourceLineNo">356</span><a name="line.356"></a>
-<span class="sourceLineNo">357</span>    // Now we have a set of regions that must be all assigned out<a name="line.357"></a>
-<span class="sourceLineNo">358</span>    // Assign each underloaded up to the min, then if leftovers, assign to max<a name="line.358"></a>
-<span class="sourceLineNo">359</span><a name="line.359"></a>
-<span class="sourceLineNo">360</span>    // Walk down least loaded, assigning to each to fill up to min<a name="line.360"></a>
-<span class="sourceLineNo">361</span>    for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server :<a name="line.361"></a>
-<span class="sourceLineNo">362</span>        serversByLoad.entrySet()) {<a name="line.362"></a>
-<span class="sourceLineNo">363</span>      int regionCount = server.getKey().getLoad();<a name="line.363"></a>
-<span class="sourceLineNo">364</span>      if (regionCount &gt;= min) break;<a name="line.364"></a>
-<span class="sourceLineNo">365</span>      BalanceInfo balanceInfo = serverBalanceInfo.get(server.getKey().getServerName());<a name="line.365"></a>
-<span class="sourceLineNo">366</span>      if(balanceInfo != null) {<a name="line.366"></a>
-<span class="sourceLineNo">367</span>        regionCount += balanceInfo.getNumRegionsAdded();<a name="line.367"></a>
-<span class="sourceLineNo">368</span>      }<a name="line.368"></a>
-<span class="sourceLineNo">369</span>      if(regionCount &gt;= min) {<a name="line.369"></a>
-<span class="sourceLineNo">370</span>        continue;<a name="line.370"></a>
-<span class="sourceLineNo">371</span>      }<a name="line.371"></a>
-<span class="sourceLineNo">372</span>      int numToTake = min - regionCount;<a name="line.372"></a>
-<span class="sourceLineNo">373</span>      int numTaken = 0;<a name="line.373"></a>
-<span class="sourceLineNo">374</span>      while(numTaken &lt; numToTake &amp;&amp; 0 &lt; regionsToMove.size()) {<a name="line.374"></a>
-<span class="sourceLineNo">375</span>        addRegionPlan(regionsToMove, fetchFromTail,<a name="line.375"></a>
-<span class="sourceLineNo">376</span>          server.getKey().getServerName(), regionsToReturn);<a name="line.376"></a>
-<span class="sourceLineNo">377</span>        numTaken++;<a name="line.377"></a>
-<span class="sourceLineNo">378</span>      }<a name="line.378"></a>
-<span class="sourceLineNo">379</span>    }<a name="line.379"></a>
-<span class="sourceLineNo">380</span><a name="line.380"></a>
-<span class="sourceLineNo">381</span>    // If we still have regions to dish out, assign underloaded to max<a name="line.381"></a>
-<span class="sourceLineNo">382</span>    if (0 &lt; regionsToMove.size()) {<a name="line.382"></a>
-<span class="sourceLineNo">383</span>      for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server :<a name="line.383"></a>
-<span class="sourceLineNo">384</span>        serversByLoad.entrySet()) {<a name="line.384"></a>
-<span class="sourceLineNo">385</span>        int regionCount = server.getKey().getLoad();<a name="line.385"></a>
-<span class="sourceLineNo">386</span>        BalanceInfo balanceInfo = serverBalanceInfo.get(server.getKey().getServerName());<a name="line.386"></a>
-<span class="sourceLineNo">387</span>        if(balanceInfo != null) {<a name="line.387"></a>
-<span class="sourceLineNo">388</span>          regionCount += balanceInfo.getNumRegionsAdded();<a name="line.388"></a>
-<span class="sourceLineNo">389</span>        }<a name="line.389"></a>
-<span class="sourceLineNo">390</span>        if(regionCount &gt;= max) {<a name="line.390"></a>
-<span class="sourceLineNo">391</span>          break;<a name="line.391"></a>
-<span class="sourceLineNo">392</span>        }<a name="line.392"></a>
-<span class="sourceLineNo">393</span>        addRegionPlan(regionsToMove, fetchFromTail,<a name="line.393"></a>
-<span class="sourceLineNo">394</span>          server.getKey().getServerName(), regionsToReturn);<a name="line.394"></a>
-<span class="sourceLineNo">395</span>        if (regionsToMove.isEmpty()) {<a name="line.395"></a>
-<span class="sourceLineNo">396</span>          break;<a name="line.396"></a>
-<span class="sourceLineNo">397</span>        }<a name="line.397"></a>
-<span class="sourceLineNo">398</span>      }<a name="line.398"></a>
-<span class="sourceLineNo">399</span>    }<a name="line.399"></a>
-<span class="sourceLineNo">400</span><a name="line.400"></a>
-<span class="sourceLineNo">401</span>    long endTime = System.currentTimeMillis();<a name="line.401"></a>
-<span class="sourceLineNo">402</span><a name="line.402"></a>
-<span class="sourceLineNo">403</span>    if (!regionsToMove.isEmpty() || neededRegions != 0) {<a name="line.403"></a>
-<span class="sourceLineNo">404</span>      // Emit data so can diagnose how balancer went astray.<a name="line.404"></a>
-<span class="sourceLineNo">405</span>      LOG.warn("regionsToMove=" + totalNumMoved +<a name="line.405"></a>
-<span class="sourceLineNo">406</span>        ", numServers=" + numServers + ", serversOverloaded=" + serversOverloaded +<a name="line.406"></a>
-<span class="sourceLineNo">407</span>        ", serversUnderloaded=" + serversUnderloaded);<a name="line.407"></a>
-<span class="sourceLineNo">408</span>      StringBuilder sb = new StringBuilder();<a name="line.408"></a>
-<span class="sourceLineNo">409</span>      for (Map.Entry&lt;ServerName, List&lt;HRegionInfo&gt;&gt; e: clusterMap.entrySet()) {<a name="line.409"></a>
-<span class="sourceLineNo">410</span>        if (sb.length() &gt; 0) sb.append(", ");<a name="line.410"></a>
-<span class="sourceLineNo">411</span>        sb.append(e.getKey().toString());<a name="line.411"></a>
-<span class="sourceLineNo">412</span>        sb.append(" ");<a name="line.412"></a>
-<span class="sourceLineNo">413</span>        sb.append(e.getValue().size());<a name="line.413"></a>
-<span class="sourceLineNo">414</span>      }<a name="line.414"></a>
-<span class="sourceLineNo">415</span>      LOG.warn("Input " + sb.toString());<a name="line.415"></a>
-<span class="sourceLineNo">416</span>    }<a name="line.416"></a>
-<span class="sourceLineNo">417</span><a name="line.417"></a>
-<span class="sourceLineNo">418</span>    // All done!<a name="line.418"></a>
-<span class="sourceLineNo">419</span>    LOG.info("Done. Calculated a load balance in " + (endTime-startTime) + "ms. " +<a name="line.419"></a>
-<span class="sourceLineNo">420</span>        "Moving " + totalNumMoved + " regions off of " +<a name="line.420"></a>
-<span class="sourceLineNo">421</span>        serversOverloaded + " overloaded servers onto " +<a name="line.421"></a>
-<span class="sourceLineNo">422</span>        serversUnderloaded + " less loaded servers");<a name="line.422"></a>
-<span class="sourceLineNo">423</span><a name="line.423"></a>
-<span class="sourceLineNo">424</span>    return regionsToReturn;<a name="line.424"></a>
-<span class="sourceLineNo">425</span>  }<a name="line.425"></a>
-<span class="sourceLineNo">426</span><a name="line.426"></a>
-<span class="sourceLineNo">427</span>  /**<a name="line.427"></a>
-<span class="sourceLineNo">428</span>   * Add a region from the head or tail to the List of regions to return.<a name="line.428"></a>
-<span class="sourceLineNo">429</span>   */<a name="line.429"></a>
-<span class="sourceLineNo">430</span>  private void addRegionPlan(final MinMaxPriorityQueue&lt;RegionPlan&gt; regionsToMove,<a name="line.430"></a>
-<span class="sourceLineNo">431</span>      final boolean fetchFromTail, final ServerName sn, List&lt;RegionPlan&gt; regionsToReturn) {<a name="line.431"></a>
-<span class="sourceLineNo">432</span>    RegionPlan rp = null;<a name="line.432"></a>
-<span class="sourceLineNo">433</span>    if (!fetchFromTail) rp = regionsToMove.remove();<a name="line.433"></a>
-<span class="sourceLineNo">434</span>    else rp = regionsToMove.removeLast();<a name="line.434"></a>
-<span class="sourceLineNo">435</span>    rp.setDestination(sn);<a name="line.435"></a>
-<span class="sourceLineNo">436</span>    regionsToReturn.add(rp);<a name="line.436"></a>
-<span class="sourceLineNo">437</span>  }<a name="line.437"></a>
-<span class="sourceLineNo">438</span><a name="line.438"></a>
-<span class="sourceLineNo">439</span>  @Override<a name="line.439"></a>
-<span class="sourceLineNo">440</span>  public List&lt;RegionPlan&gt; balanceCluster(TableName tableName,<a name="line.440"></a>
-<span class="sourceLineNo">441</span>      Map&lt;ServerName, List&lt;HRegionInfo&gt;&gt; clusterState) throws HBaseIOException {<a name="line.441"></a>
-<span class="sourceLineNo">442</span>    return balanceCluster(clusterState);<a name="line.442"></a>
-<span class="sourceLineNo">443</span>  }<a name="line.443"></a>
-<span class="sourceLineNo">444</span>}<a name="line.444"></a>
+<span class="sourceLineNo">317</span><a name="line.317"></a>
+<span class="sourceLineNo">318</span>    // If none needed to fill all to min and none left to drain all to max,<a name="line.318"></a>
+<span class="sourceLineNo">319</span>    // we are done<a name="line.319"></a>
+<span class="sourceLineNo">320</span>    if (neededRegions == 0 &amp;&amp; regionsToMove.isEmpty()) {<a name="line.320"></a>
+<span class="sourceLineNo">321</span>      long endTime = System.currentTimeMillis();<a name="line.321"></a>
+<span class="sourceLineNo">322</span>      LOG.info("Calculated a load balance in " + (endTime-startTime) + "ms. " +<a name="line.322"></a>
+<span class="sourceLineNo">323</span>          "Moving " + totalNumMoved + " regions off of " +<a name="line.323"></a>
+<span class="sourceLineNo">324</span>          serversOverloaded + " overloaded servers onto " +<a name="line.324"></a>
+<span class="sourceLineNo">325</span>          serversUnderloaded + " less loaded servers");<a name="line.325"></a>
+<span class="sourceLineNo">326</span>      return regionsToReturn;<a name="line.326"></a>
+<span class="sourceLineNo">327</span>    }<a name="line.327"></a>
+<span class="sourceLineNo">328</span><a name="line.328"></a>
+<span class="sourceLineNo">329</span>    // Need to do a second pass.<a name="line.329"></a>
+<span class="sourceLineNo">330</span>    // Either more regions to assign out or servers that are still underloaded<a name="line.330"></a>
+<span class="sourceLineNo">331</span><a name="line.331"></a>
+<span class="sourceLineNo">332</span>    // If we need more to fill min, grab one from each most loaded until enough<a name="line.332"></a>
+<span class="sourceLineNo">333</span>    if (neededRegions != 0) {<a name="line.333"></a>
+<span class="sourceLineNo">334</span>      // Walk down most loaded, grabbing one from each until we get enough<a name="line.334"></a>
+<span class="sourceLineNo">335</span>      for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server :<a name="line.335"></a>
+<span class="sourceLineNo">336</span>        serversByLoad.descendingMap().entrySet()) {<a name="line.336"></a>
+<span class="sourceLineNo">337</span>        BalanceInfo balanceInfo =<a name="line.337"></a>
+<span class="sourceLineNo">338</span>          serverBalanceInfo.get(server.getKey().getServerName());<a name="line.338"></a>
+<span class="sourceLineNo">339</span>        int idx =<a name="line.339"></a>
+<span class="sourceLineNo">340</span>          balanceInfo == null ? 0 : balanceInfo.getNextRegionForUnload();<a name="line.340"></a>
+<span class="sourceLineNo">341</span>        if (idx &gt;= server.getValue().size()) break;<a name="line.341"></a>
+<span class="sourceLineNo">342</span>        HRegionInfo region = server.getValue().get(idx);<a name="line.342"></a>
+<span class="sourceLineNo">343</span>        if (region.isMetaRegion()) continue; // Don't move meta regions.<a name="line.343"></a>
+<span class="sourceLineNo">344</span>        regionsToMove.add(new RegionPlan(region, server.getKey().getServerName(), null));<a name="line.344"></a>
+<span class="sourceLineNo">345</span>        totalNumMoved++;<a name="line.345"></a>
+<span class="sourceLineNo">346</span>        if (--neededRegions == 0) {<a name="line.346"></a>
+<span class="sourceLineNo">347</span>          // No more regions needed, done shedding<a name="line.347"></a>
+<span class="sourceLineNo">348</span>          break;<a name="line.348"></a>
+<span class="sourceLineNo">349</span>        }<a name="line.349"></a>
+<span class="sourceLineNo">350</span>      }<a name="line.350"></a>
+<span class="sourceLineNo">351</span>    }<a name="line.351"></a>
+<span class="sourceLineNo">352</span><a name="line.352"></a>
+<span class="sourceLineNo">353</span>    // Now we have a set of regions that must be all assigned out<a name="line.353"></a>
+<span class="sourceLineNo">354</span>    // Assign each underloaded up to the min, then if leftovers, assign to max<a name="line.354"></a>
+<span class="sourceLineNo">355</span><a name="line.355"></a>
+<span class="sourceLineNo">356</span>    // Walk down least loaded, assigning to each to fill up to min<a name="line.356"></a>
+<span class="sourceLineNo">357</span>    for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server :<a name="line.357"></a>
+<span class="sourceLineNo">358</span>        serversByLoad.entrySet()) {<a name="line.358"></a>
+<span class="sourceLineNo">359</span>      int regionCount = server.getKey().getLoad();<a name="line.359"></a>
+<span class="sourceLineNo">360</span>      if (regionCount &gt;= min) break;<a name="line.360"></a>
+<span class="sourceLineNo">361</span>      BalanceInfo balanceInfo = serverBalanceInfo.get(server.getKey().getServerName());<a name="line.361"></a>
+<span class="sourceLineNo">362</span>      if(balanceInfo != null) {<a name="line.362"></a>
+<span class="sourceLineNo">363</span>        regionCount += balanceInfo.getNumRegionsAdded();<a name="line.363"></a>
+<span class="sourceLineNo">364</span>      }<a name="line.364"></a>
+<span class="sourceLineNo">365</span>      if(regionCount &gt;= min) {<a name="line.365"></a>
+<span class="sourceLineNo">366</span>        continue;<a name="line.366"></a>
+<span class="sourceLineNo">367</span>      }<a name="line.367"></a>
+<span class="sourceLineNo">368</span>      int numToTake = min - regionCount;<a name="line.368"></a>
+<span class="sourceLineNo">369</span>      int numTaken = 0;<a name="line.369"></a>
+<span class="sourceLineNo">370</span>      while(numTaken &lt; numToTake &amp;&amp; 0 &lt; regionsToMove.size()) {<a name="line.370"></a>
+<span class="sourceLineNo">371</span>        addRegionPlan(regionsToMove, fetchFromTail,<a name="line.371"></a>
+<span class="sourceLineNo">372</span>          server.getKey().getServerName(), regionsToReturn);<a name="line.372"></a>
+<span class="sourceLineNo">373</span>        numTaken++;<a name="line.373"></a>
+<span class="sourceLineNo">374</span>      }<a name="line.374"></a>
+<span class="sourceLineNo">375</span>    }<a name="line.375"></a>
+<span class="sourceLineNo">376</span><a name="line.376"></a>
+<span class="sourceLineNo">377</span>    // If we still have regions to dish out, assign underloaded to max<a name="line.377"></a>
+<span class="sourceLineNo">378</span>    if (0 &lt; regionsToMove.size()) {<a name="line.378"></a>
+<span class="sourceLineNo">379</span>      for (Map.Entry&lt;ServerAndLoad, List&lt;HRegionInfo&gt;&gt; server :<a name="line.379"></a>
+<span class="sourceLineNo">380</span>        serversByLoad.entrySet()) {<a name="line.380"></a>
+<span class="sourceLineNo">381</span>        int regionCount = server.getKey().getLoad();<a name="line.381"></a>
+<span class="sourceLineNo">382</span>        BalanceInfo balanceInfo = serverBalanceInfo.get(server.getKey().getServerName());<a name="line.382"></a>
+<span class="sourceLineNo">383</span>        if(balanceInfo != null) {<a name="line.383"></a>
+<span class="sourceLineNo">384</span>          regionCount += balanceInfo.getNumRegionsAdded();<a name="line.384"></a>
+<span class="sourceLineNo">385</span>        }<a name="line.385"></a>
+<span class="sourceLineNo">386</span>        if(regionCount &gt;= max) {<a name="line.386"></a>
+<span class="sourceLineNo">387</span>          break;<a name="line.387"></a>
+<span class="sourceLineNo">388</span>        }<a name="line.388"></a>
+<span class="sourceLineNo">389</span>        addRegionPlan(regionsToMove, fetchFromTail,<a name="line.389"></a>
+<span class="sourceLineNo">390</span>          server.getKey().getServerName(), regionsToReturn);<a name="line.390"></a>
+<span class="sourceLineNo">391</span>        if (regionsToMove.isEmpty()) {<a name="line.391"></a>
+<span class="sourceLineNo">392</span>          break;<a name="line.392"></a>
+<span class="sourceLineNo">393</span>        }<a name="line.393"></a>
+<span class="sourceLineNo">394</span>      }<a name="line.394"></a>
+<span class="sourceLineNo">395</span>    }<a name="line.395"></a>
+<span class="sourceLineNo">396</span><a name="line.396"></a>
+<span class="sourceLineNo">397</span>    long endTime = System.currentTimeMillis();<a name="line.397"></a>
+<span class="sourceLineNo">398</span><a name="line.398"></a>
+<span class="sourceLineNo">399</span>    if (!regionsToMove.isEmpty() || neededRegions != 0) {<a name="line.399"></a>
+<span class="sourceLineNo">400</span>      // Emit data so can diagnose how balancer went astray.<a name="line.400"></a>
+<span class="sourceLineNo">401</span>      LOG.warn("regionsToMove=" + totalNumMoved +<a name="line.401"></a>
+<span class="sourceLineNo">402</span>        ", numServers=" + numServers + ", serversOverloaded=" + serversOverloaded +<a name="line.402"></a>
+<span class="sourceLineNo">403</span>        ", serversUnderloaded=" + serversUnderloaded);<a name="line.403"></a>
+<span class="sourceLineNo">404</span>      StringBuilder sb = new StringBuilder();<a name="line.404"></a>
+<span class="sourceLineNo">405</span>      for (Map.Entry&lt;ServerName, List&lt;HRegionInfo&gt;&gt; e: clusterMap.entrySet()) {<a name="line.405"></a>
+<span class="sourceLineNo">406</span>        if (sb.length() &gt; 0) sb.append(", ");<a name="line.406"></a>
+<span class="sourceLineNo">407</span>        sb.append(e.getKey().toString());<a name="line.407"></a>
+<span class="sourceLineNo">408</span>        sb.append(" ");<a name="line.408"></a>
+<span class="sourceLineNo">409</span>        sb.append(e.getValue().size());<a name="line.409"></a>
+<span class="sourceLineNo">410</span>      }<a name="line.410"></a>
+<span class="sourceLineNo">411</span>      LOG.warn("Input " + sb.toString());<a name="line.411"></a>
+<span class="sourceLineNo">412</span>    }<a name="line.412"></a>
+<span class="sourceLineNo">413</span><a name="line.413"></a>
+<span class="sourceLineNo">414</span>    // All done!<a name="line.414"></a>
+<span class="sourceLineNo">415</span>    LOG.info("Done. Calculated a load balance in " + (endTime-startTime) + "ms. " +<a name="line.415"></a>
+<span class="sourceLineNo">416</span>        "Moving " + totalNumMoved + " regions off of " +<a name="line.416"></a>
+<span class="sourceLineNo">417</span>        serversOverloaded + " overloaded servers onto " +<a name="line.417"></a>
+<span class="sourceLineNo">418</span>        serversUnderloaded + " less loaded servers");<a name="line.418"></a>
+<span class="sourceLineNo">419</span><a name="line.419"></a>
+<span class="sourceLineNo">420</span>    return regionsToReturn;<a name="line.420"></a>
+<span class="sourceLineNo">421</span>  }<a name="line.421"></a>
+<span class="sourceLineNo">422</span><a name="line.422"></a>
+<span class="sourceLineNo">423</span>  /**<a name="line.423"></a>
+<span class="sourceLineNo">424</span>   * Add a region from the head or tail to the List of regions to return.<a name="line.424"></a>
+<span class="sourceLineNo">425</span>   */<a name="line.425"></a>
+<span class="sourceLineNo">426</span>  private void addRegionPlan(final MinMaxPriorityQueue&lt;RegionPlan&gt; regionsToMove,<a name="line.426"></a>
+<span class="sourceLineNo">427</span>      final boolean fetchFromTail, final ServerName sn, List&lt;RegionPlan&gt; regionsToReturn) {<a name="line.427"></a>
+<span class="sourceLineNo">428</span>    RegionPlan rp = null;<a name="line.428"></a>
+<span class="sourceLineNo">429</span>    if (!fetchFromTail) rp = regionsToMove.remove();<a name="line.429"></a>
+<span class="sourceLineNo">430</span>    else rp = regionsToMove.removeLast();<a name="line.430"></a>
+<span class="sourceLineNo">431</span>    rp.setDestination(sn);<a name="line.431"></a>
+<span class="sourceLineNo">432</span>    regionsToReturn.add(rp);<a name="line.432"></a>
+<span class="sourceLineNo">433</span>  }<a name="line.433"></a>
+<span class="sourceLineNo">434</span><a name="line.434"></a>
+<span class="sourceLineNo">435</span>  @Override<a name="line.435"></a>
+<span class="sourceLineNo">436</span>  public List&lt;RegionPlan&gt; balanceCluster(TableName tableName,<a name="line.436"></a>
+<span class="sourceLineNo">437</span>      Map&lt;ServerName, List&lt;HRegionInfo&gt;&gt; clusterState) throws HBaseIOException {<a name="line.437"></a>
+<span class="sourceLineNo">438</span>    return balanceCluster(clusterState);<a name="line.438"></a>
+<span class="sourceLineNo">439</span>  }<a name="line.439"></a>
+<span class="sourceLineNo">440</span>}<a name="line.440"></a>
 
 
 


Mime
View raw message