hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From git-site-r...@apache.org
Subject [25/46] hbase-site git commit: Published site at 05d22ed960204183fbd9bd16f1dbca6410ccde76.
Date Fri, 19 Oct 2018 14:53:36 GMT
http://git-wip-us.apache.org/repos/asf/hbase-site/blob/3649682c/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.Type.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.Type.html b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.Type.html
index c307d41..d82bfd8 100644
--- a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.Type.html
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.Type.html
@@ -1020,8 +1020,8 @@
 <span class="sourceLineNo">1012</span>  protected ClusterMetrics clusterStatus = null;<a name="line.1012"></a>
 <span class="sourceLineNo">1013</span>  protected ServerName masterServerName;<a name="line.1013"></a>
 <span class="sourceLineNo">1014</span>  protected MasterServices services;<a name="line.1014"></a>
-<span class="sourceLineNo">1015</span>  protected boolean tablesOnMaster;<a name="line.1015"></a>
-<span class="sourceLineNo">1016</span>  protected boolean onlySystemTablesOnMaster;<a name="line.1016"></a>
+<span class="sourceLineNo">1015</span>  protected boolean onlySystemTablesOnMaster;<a name="line.1015"></a>
+<span class="sourceLineNo">1016</span>  protected boolean maintenanceMode;<a name="line.1016"></a>
 <span class="sourceLineNo">1017</span><a name="line.1017"></a>
 <span class="sourceLineNo">1018</span>  @Override<a name="line.1018"></a>
 <span class="sourceLineNo">1019</span>  public void setConf(Configuration conf) {<a name="line.1019"></a>
@@ -1033,627 +1033,626 @@
 <span class="sourceLineNo">1025</span>    if (overallSlop &lt; 0) overallSlop = 0;<a name="line.1025"></a>
 <span class="sourceLineNo">1026</span>    else if (overallSlop &gt; 1) overallSlop = 1;<a name="line.1026"></a>
 <span class="sourceLineNo">1027</span><a name="line.1027"></a>
-<span class="sourceLineNo">1028</span>    this.tablesOnMaster = LoadBalancer.isTablesOnMaster(this.config);<a name="line.1028"></a>
-<span class="sourceLineNo">1029</span>    this.onlySystemTablesOnMaster = LoadBalancer.isSystemTablesOnlyOnMaster(this.config);<a name="line.1029"></a>
-<span class="sourceLineNo">1030</span>    // If system tables on master, implies tablesOnMaster = true.<a name="line.1030"></a>
-<span class="sourceLineNo">1031</span>    if (this.onlySystemTablesOnMaster &amp;&amp; !this.tablesOnMaster) {<a name="line.1031"></a>
-<span class="sourceLineNo">1032</span>      LOG.warn("Set " + TABLES_ON_MASTER + "=true because " + SYSTEM_TABLES_ON_MASTER + "=true");<a name="line.1032"></a>
-<span class="sourceLineNo">1033</span>      this.tablesOnMaster = true;<a name="line.1033"></a>
-<span class="sourceLineNo">1034</span>    }<a name="line.1034"></a>
-<span class="sourceLineNo">1035</span>    this.rackManager = new RackManager(getConf());<a name="line.1035"></a>
-<span class="sourceLineNo">1036</span>    if (useRegionFinder) {<a name="line.1036"></a>
-<span class="sourceLineNo">1037</span>      regionFinder.setConf(conf);<a name="line.1037"></a>
-<span class="sourceLineNo">1038</span>    }<a name="line.1038"></a>
-<span class="sourceLineNo">1039</span>    // Print out base configs. Don't print overallSlop since it for simple balancer exclusively.<a name="line.1039"></a>
-<span class="sourceLineNo">1040</span>    LOG.info("slop=" + this.slop + ", tablesOnMaster=" + this.tablesOnMaster +<a name="line.1040"></a>
-<span class="sourceLineNo">1041</span>      ", systemTablesOnMaster=" + this.onlySystemTablesOnMaster);<a name="line.1041"></a>
+<span class="sourceLineNo">1028</span>    this.onlySystemTablesOnMaster = LoadBalancer.isSystemTablesOnlyOnMaster(this.config);<a name="line.1028"></a>
+<span class="sourceLineNo">1029</span><a name="line.1029"></a>
+<span class="sourceLineNo">1030</span>    this.rackManager = new RackManager(getConf());<a name="line.1030"></a>
+<span class="sourceLineNo">1031</span>    if (useRegionFinder) {<a name="line.1031"></a>
+<span class="sourceLineNo">1032</span>      regionFinder.setConf(conf);<a name="line.1032"></a>
+<span class="sourceLineNo">1033</span>    }<a name="line.1033"></a>
+<span class="sourceLineNo">1034</span>    // Print out base configs. Don't print overallSlop since it for simple balancer exclusively.<a name="line.1034"></a>
+<span class="sourceLineNo">1035</span>    LOG.info("slop={}, systemTablesOnMaster={}",<a name="line.1035"></a>
+<span class="sourceLineNo">1036</span>        this.slop, this.onlySystemTablesOnMaster);<a name="line.1036"></a>
+<span class="sourceLineNo">1037</span>  }<a name="line.1037"></a>
+<span class="sourceLineNo">1038</span><a name="line.1038"></a>
+<span class="sourceLineNo">1039</span>  protected void setSlop(Configuration conf) {<a name="line.1039"></a>
+<span class="sourceLineNo">1040</span>    this.slop = conf.getFloat("hbase.regions.slop", (float) 0.2);<a name="line.1040"></a>
+<span class="sourceLineNo">1041</span>    this.overallSlop = conf.getFloat("hbase.regions.overallSlop", slop);<a name="line.1041"></a>
 <span class="sourceLineNo">1042</span>  }<a name="line.1042"></a>
 <span class="sourceLineNo">1043</span><a name="line.1043"></a>
-<span class="sourceLineNo">1044</span>  protected void setSlop(Configuration conf) {<a name="line.1044"></a>
-<span class="sourceLineNo">1045</span>    this.slop = conf.getFloat("hbase.regions.slop", (float) 0.2);<a name="line.1045"></a>
-<span class="sourceLineNo">1046</span>    this.overallSlop = conf.getFloat("hbase.regions.overallSlop", slop);<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>  /**<a name="line.1049"></a>
-<span class="sourceLineNo">1050</span>   * Check if a region belongs to some system table.<a name="line.1050"></a>
-<span class="sourceLineNo">1051</span>   * If so, the primary replica may be expected to be put on the master regionserver.<a name="line.1051"></a>
-<span class="sourceLineNo">1052</span>   */<a name="line.1052"></a>
-<span class="sourceLineNo">1053</span>  public boolean shouldBeOnMaster(RegionInfo region) {<a name="line.1053"></a>
-<span class="sourceLineNo">1054</span>    return this.onlySystemTablesOnMaster &amp;&amp; region.getTable().isSystemTable();<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>  /**<a name="line.1057"></a>
-<span class="sourceLineNo">1058</span>   * Balance the regions that should be on master regionserver.<a name="line.1058"></a>
-<span class="sourceLineNo">1059</span>   */<a name="line.1059"></a>
-<span class="sourceLineNo">1060</span>  protected List&lt;RegionPlan&gt; balanceMasterRegions(Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; clusterMap) {<a name="line.1060"></a>
-<span class="sourceLineNo">1061</span>    if (masterServerName == null || clusterMap == null || clusterMap.size() &lt;= 1) return null;<a name="line.1061"></a>
-<span class="sourceLineNo">1062</span>    List&lt;RegionPlan&gt; plans = null;<a name="line.1062"></a>
-<span class="sourceLineNo">1063</span>    List&lt;RegionInfo&gt; regions = clusterMap.get(masterServerName);<a name="line.1063"></a>
-<span class="sourceLineNo">1064</span>    if (regions != null) {<a name="line.1064"></a>
-<span class="sourceLineNo">1065</span>      Iterator&lt;ServerName&gt; keyIt = null;<a name="line.1065"></a>
-<span class="sourceLineNo">1066</span>      for (RegionInfo region: regions) {<a name="line.1066"></a>
-<span class="sourceLineNo">1067</span>        if (shouldBeOnMaster(region)) continue;<a name="line.1067"></a>
-<span class="sourceLineNo">1068</span><a name="line.1068"></a>
-<span class="sourceLineNo">1069</span>        // Find a non-master regionserver to host the region<a name="line.1069"></a>
-<span class="sourceLineNo">1070</span>        if (keyIt == null || !keyIt.hasNext()) {<a name="line.1070"></a>
-<span class="sourceLineNo">1071</span>          keyIt = clusterMap.keySet().iterator();<a name="line.1071"></a>
-<span class="sourceLineNo">1072</span>        }<a name="line.1072"></a>
-<span class="sourceLineNo">1073</span>        ServerName dest = keyIt.next();<a name="line.1073"></a>
-<span class="sourceLineNo">1074</span>        if (masterServerName.equals(dest)) {<a name="line.1074"></a>
-<span class="sourceLineNo">1075</span>          if (!keyIt.hasNext()) {<a name="line.1075"></a>
-<span class="sourceLineNo">1076</span>            keyIt = clusterMap.keySet().iterator();<a name="line.1076"></a>
-<span class="sourceLineNo">1077</span>          }<a name="line.1077"></a>
-<span class="sourceLineNo">1078</span>          dest = keyIt.next();<a name="line.1078"></a>
-<span class="sourceLineNo">1079</span>        }<a name="line.1079"></a>
-<span class="sourceLineNo">1080</span><a name="line.1080"></a>
-<span class="sourceLineNo">1081</span>        // Move this region away from the master regionserver<a name="line.1081"></a>
-<span class="sourceLineNo">1082</span>        RegionPlan plan = new RegionPlan(region, masterServerName, dest);<a name="line.1082"></a>
-<span class="sourceLineNo">1083</span>        if (plans == null) {<a name="line.1083"></a>
-<span class="sourceLineNo">1084</span>          plans = new ArrayList&lt;&gt;();<a name="line.1084"></a>
-<span class="sourceLineNo">1085</span>        }<a name="line.1085"></a>
-<span class="sourceLineNo">1086</span>        plans.add(plan);<a name="line.1086"></a>
-<span class="sourceLineNo">1087</span>      }<a name="line.1087"></a>
-<span class="sourceLineNo">1088</span>    }<a name="line.1088"></a>
-<span class="sourceLineNo">1089</span>    for (Map.Entry&lt;ServerName, List&lt;RegionInfo&gt;&gt; server: clusterMap.entrySet()) {<a name="line.1089"></a>
-<span class="sourceLineNo">1090</span>      if (masterServerName.equals(server.getKey())) continue;<a name="line.1090"></a>
-<span class="sourceLineNo">1091</span>      for (RegionInfo region: server.getValue()) {<a name="line.1091"></a>
-<span class="sourceLineNo">1092</span>        if (!shouldBeOnMaster(region)) continue;<a name="line.1092"></a>
-<span class="sourceLineNo">1093</span><a name="line.1093"></a>
-<span class="sourceLineNo">1094</span>        // Move this region to the master regionserver<a name="line.1094"></a>
-<span class="sourceLineNo">1095</span>        RegionPlan plan = new RegionPlan(region, server.getKey(), masterServerName);<a name="line.1095"></a>
-<span class="sourceLineNo">1096</span>        if (plans == null) {<a name="line.1096"></a>
-<span class="sourceLineNo">1097</span>          plans = new ArrayList&lt;&gt;();<a name="line.1097"></a>
-<span class="sourceLineNo">1098</span>        }<a name="line.1098"></a>
-<span class="sourceLineNo">1099</span>        plans.add(plan);<a name="line.1099"></a>
-<span class="sourceLineNo">1100</span>      }<a name="line.1100"></a>
-<span class="sourceLineNo">1101</span>    }<a name="line.1101"></a>
-<span class="sourceLineNo">1102</span>    return plans;<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>  /**<a name="line.1105"></a>
-<span class="sourceLineNo">1106</span>   * If master is configured to carry system tables only, in here is<a name="line.1106"></a>
-<span class="sourceLineNo">1107</span>   * where we figure what to assign it.<a name="line.1107"></a>
-<span class="sourceLineNo">1108</span>   */<a name="line.1108"></a>
-<span class="sourceLineNo">1109</span>  protected Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignMasterSystemRegions(<a name="line.1109"></a>
-<span class="sourceLineNo">1110</span>      Collection&lt;RegionInfo&gt; regions, List&lt;ServerName&gt; servers) {<a name="line.1110"></a>
-<span class="sourceLineNo">1111</span>    if (servers == null || regions == null || regions.isEmpty()) {<a name="line.1111"></a>
-<span class="sourceLineNo">1112</span>      return null;<a name="line.1112"></a>
-<span class="sourceLineNo">1113</span>    }<a name="line.1113"></a>
-<span class="sourceLineNo">1114</span>    Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignments = new TreeMap&lt;&gt;();<a name="line.1114"></a>
-<span class="sourceLineNo">1115</span>    if (this.onlySystemTablesOnMaster) {<a name="line.1115"></a>
-<span class="sourceLineNo">1116</span>      if (masterServerName != null &amp;&amp; servers.contains(masterServerName)) {<a name="line.1116"></a>
-<span class="sourceLineNo">1117</span>        assignments.put(masterServerName, new ArrayList&lt;&gt;());<a name="line.1117"></a>
-<span class="sourceLineNo">1118</span>        for (RegionInfo region : regions) {<a name="line.1118"></a>
-<span class="sourceLineNo">1119</span>          if (shouldBeOnMaster(region)) {<a name="line.1119"></a>
-<span class="sourceLineNo">1120</span>            assignments.get(masterServerName).add(region);<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>    }<a name="line.1124"></a>
-<span class="sourceLineNo">1125</span>    return assignments;<a name="line.1125"></a>
-<span class="sourceLineNo">1126</span>  }<a name="line.1126"></a>
-<span class="sourceLineNo">1127</span><a name="line.1127"></a>
-<span class="sourceLineNo">1128</span>  @Override<a name="line.1128"></a>
-<span class="sourceLineNo">1129</span>  public Configuration getConf() {<a name="line.1129"></a>
-<span class="sourceLineNo">1130</span>    return this.config;<a name="line.1130"></a>
-<span class="sourceLineNo">1131</span>  }<a name="line.1131"></a>
-<span class="sourceLineNo">1132</span><a name="line.1132"></a>
-<span class="sourceLineNo">1133</span>  @Override<a name="line.1133"></a>
-<span class="sourceLineNo">1134</span>  public synchronized void setClusterMetrics(ClusterMetrics st) {<a name="line.1134"></a>
-<span class="sourceLineNo">1135</span>    this.clusterStatus = st;<a name="line.1135"></a>
-<span class="sourceLineNo">1136</span>    if (useRegionFinder) {<a name="line.1136"></a>
-<span class="sourceLineNo">1137</span>      regionFinder.setClusterMetrics(st);<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><a name="line.1140"></a>
-<span class="sourceLineNo">1141</span>  @Override<a name="line.1141"></a>
-<span class="sourceLineNo">1142</span>  public void setClusterLoad(Map&lt;TableName, Map&lt;ServerName, List&lt;RegionInfo&gt;&gt;&gt; clusterLoad){<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><a name="line.1145"></a>
-<span class="sourceLineNo">1146</span>  @Override<a name="line.1146"></a>
-<span class="sourceLineNo">1147</span>  public void setMasterServices(MasterServices masterServices) {<a name="line.1147"></a>
-<span class="sourceLineNo">1148</span>    masterServerName = masterServices.getServerName();<a name="line.1148"></a>
-<span class="sourceLineNo">1149</span>    this.services = masterServices;<a name="line.1149"></a>
-<span class="sourceLineNo">1150</span>    if (useRegionFinder) {<a name="line.1150"></a>
-<span class="sourceLineNo">1151</span>      this.regionFinder.setServices(masterServices);<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><a name="line.1154"></a>
-<span class="sourceLineNo">1155</span>  @Override<a name="line.1155"></a>
-<span class="sourceLineNo">1156</span>  public void postMasterStartupInitialize() {<a name="line.1156"></a>
-<span class="sourceLineNo">1157</span>    if (services != null &amp;&amp; regionFinder != null) {<a name="line.1157"></a>
-<span class="sourceLineNo">1158</span>      try {<a name="line.1158"></a>
-<span class="sourceLineNo">1159</span>        Set&lt;RegionInfo&gt; regions =<a name="line.1159"></a>
-<span class="sourceLineNo">1160</span>            services.getAssignmentManager().getRegionStates().getRegionAssignments().keySet();<a name="line.1160"></a>
-<span class="sourceLineNo">1161</span>        regionFinder.refreshAndWait(regions);<a name="line.1161"></a>
-<span class="sourceLineNo">1162</span>      } catch (Exception e) {<a name="line.1162"></a>
-<span class="sourceLineNo">1163</span>        LOG.warn("Refreshing region HDFS Block dist failed with exception, ignoring", e);<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><a name="line.1167"></a>
-<span class="sourceLineNo">1168</span>  public void setRackManager(RackManager rackManager) {<a name="line.1168"></a>
-<span class="sourceLineNo">1169</span>    this.rackManager = rackManager;<a name="line.1169"></a>
-<span class="sourceLineNo">1170</span>  }<a name="line.1170"></a>
-<span class="sourceLineNo">1171</span><a name="line.1171"></a>
-<span class="sourceLineNo">1172</span>  protected boolean needsBalance(Cluster c) {<a name="line.1172"></a>
-<span class="sourceLineNo">1173</span>    ClusterLoadState cs = new ClusterLoadState(c.clusterState);<a name="line.1173"></a>
-<span class="sourceLineNo">1174</span>    if (cs.getNumServers() &lt; MIN_SERVER_BALANCE) {<a name="line.1174"></a>
-<span class="sourceLineNo">1175</span>      if (LOG.isDebugEnabled()) {<a name="line.1175"></a>
-<span class="sourceLineNo">1176</span>        LOG.debug("Not running balancer because only " + cs.getNumServers()<a name="line.1176"></a>
-<span class="sourceLineNo">1177</span>            + " active regionserver(s)");<a name="line.1177"></a>
-<span class="sourceLineNo">1178</span>      }<a name="line.1178"></a>
-<span class="sourceLineNo">1179</span>      return false;<a name="line.1179"></a>
-<span class="sourceLineNo">1180</span>    }<a name="line.1180"></a>
-<span class="sourceLineNo">1181</span>    if(areSomeRegionReplicasColocated(c)) return true;<a name="line.1181"></a>
-<span class="sourceLineNo">1182</span>    // Check if we even need to do any load balancing<a name="line.1182"></a>
-<span class="sourceLineNo">1183</span>    // HBASE-3681 check sloppiness first<a name="line.1183"></a>
-<span class="sourceLineNo">1184</span>    float average = cs.getLoadAverage(); // for logging<a name="line.1184"></a>
-<span class="sourceLineNo">1185</span>    int floor = (int) Math.floor(average * (1 - slop));<a name="line.1185"></a>
-<span class="sourceLineNo">1186</span>    int ceiling = (int) Math.ceil(average * (1 + slop));<a name="line.1186"></a>
-<span class="sourceLineNo">1187</span>    if (!(cs.getMaxLoad() &gt; ceiling || cs.getMinLoad() &lt; floor)) {<a name="line.1187"></a>
-<span class="sourceLineNo">1188</span>      NavigableMap&lt;ServerAndLoad, List&lt;RegionInfo&gt;&gt; serversByLoad = cs.getServersByLoad();<a name="line.1188"></a>
-<span class="sourceLineNo">1189</span>      if (LOG.isTraceEnabled()) {<a name="line.1189"></a>
-<span class="sourceLineNo">1190</span>        // If nothing to balance, then don't say anything unless trace-level logging.<a name="line.1190"></a>
-<span class="sourceLineNo">1191</span>        LOG.trace("Skipping load balancing because balanced cluster; " +<a name="line.1191"></a>
-<span class="sourceLineNo">1192</span>          "servers=" + cs.getNumServers() +<a name="line.1192"></a>
-<span class="sourceLineNo">1193</span>          " regions=" + cs.getNumRegions() + " average=" + average +<a name="line.1193"></a>
-<span class="sourceLineNo">1194</span>          " mostloaded=" + serversByLoad.lastKey().getLoad() +<a name="line.1194"></a>
-<span class="sourceLineNo">1195</span>          " leastloaded=" + serversByLoad.firstKey().getLoad());<a name="line.1195"></a>
-<span class="sourceLineNo">1196</span>      }<a name="line.1196"></a>
-<span class="sourceLineNo">1197</span>      return false;<a name="line.1197"></a>
-<span class="sourceLineNo">1198</span>    }<a name="line.1198"></a>
-<span class="sourceLineNo">1199</span>    return true;<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>  /**<a name="line.1202"></a>
-<span class="sourceLineNo">1203</span>   * Subclasses should implement this to return true if the cluster has nodes that hosts<a name="line.1203"></a>
-<span class="sourceLineNo">1204</span>   * multiple replicas for the same region, or, if there are multiple racks and the same<a name="line.1204"></a>
-<span class="sourceLineNo">1205</span>   * rack hosts replicas of the same region<a name="line.1205"></a>
-<span class="sourceLineNo">1206</span>   * @param c Cluster information<a name="line.1206"></a>
-<span class="sourceLineNo">1207</span>   * @return whether region replicas are currently co-located<a name="line.1207"></a>
-<span class="sourceLineNo">1208</span>   */<a name="line.1208"></a>
-<span class="sourceLineNo">1209</span>  protected boolean areSomeRegionReplicasColocated(Cluster c) {<a name="line.1209"></a>
-<span class="sourceLineNo">1210</span>    return false;<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>  /**<a name="line.1213"></a>
-<span class="sourceLineNo">1214</span>   * Generates a bulk assignment plan to be used on cluster startup using a<a name="line.1214"></a>
-<span class="sourceLineNo">1215</span>   * simple round-robin assignment.<a name="line.1215"></a>
-<span class="sourceLineNo">1216</span>   * &lt;p&gt;<a name="line.1216"></a>
-<span class="sourceLineNo">1217</span>   * Takes a list of all the regions and all the servers in the cluster and<a name="line.1217"></a>
-<span class="sourceLineNo">1218</span>   * returns a map of each server to the regions that it should be assigned.<a name="line.1218"></a>
-<span class="sourceLineNo">1219</span>   * &lt;p&gt;<a name="line.1219"></a>
-<span class="sourceLineNo">1220</span>   * Currently implemented as a round-robin assignment. Same invariant as load<a name="line.1220"></a>
-<span class="sourceLineNo">1221</span>   * balancing, all servers holding floor(avg) or ceiling(avg).<a name="line.1221"></a>
-<span class="sourceLineNo">1222</span>   *<a name="line.1222"></a>
-<span class="sourceLineNo">1223</span>   * TODO: Use block locations from HDFS to place regions with their blocks<a name="line.1223"></a>
-<span class="sourceLineNo">1224</span>   *<a name="line.1224"></a>
-<span class="sourceLineNo">1225</span>   * @param regions all regions<a name="line.1225"></a>
-<span class="sourceLineNo">1226</span>   * @param servers all servers<a name="line.1226"></a>
-<span class="sourceLineNo">1227</span>   * @return map of server to the regions it should take, or null if no<a name="line.1227"></a>
-<span class="sourceLineNo">1228</span>   *         assignment is possible (ie. no regions or no servers)<a name="line.1228"></a>
-<span class="sourceLineNo">1229</span>   */<a name="line.1229"></a>
-<span class="sourceLineNo">1230</span>  @Override<a name="line.1230"></a>
-<span class="sourceLineNo">1231</span>  public Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; roundRobinAssignment(List&lt;RegionInfo&gt; regions,<a name="line.1231"></a>
-<span class="sourceLineNo">1232</span>      List&lt;ServerName&gt; servers) throws HBaseIOException {<a name="line.1232"></a>
-<span class="sourceLineNo">1233</span>    metricsBalancer.incrMiscInvocations();<a name="line.1233"></a>
-<span class="sourceLineNo">1234</span>    Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignments = assignMasterSystemRegions(regions, servers);<a name="line.1234"></a>
-<span class="sourceLineNo">1235</span>    if (assignments != null &amp;&amp; !assignments.isEmpty()) {<a name="line.1235"></a>
-<span class="sourceLineNo">1236</span>      servers = new ArrayList&lt;&gt;(servers);<a name="line.1236"></a>
-<span class="sourceLineNo">1237</span>      // Guarantee not to put other regions on master<a name="line.1237"></a>
-<span class="sourceLineNo">1238</span>      servers.remove(masterServerName);<a name="line.1238"></a>
-<span class="sourceLineNo">1239</span>      List&lt;RegionInfo&gt; masterRegions = assignments.get(masterServerName);<a name="line.1239"></a>
-<span class="sourceLineNo">1240</span>      if (!masterRegions.isEmpty()) {<a name="line.1240"></a>
-<span class="sourceLineNo">1241</span>        regions = new ArrayList&lt;&gt;(regions);<a name="line.1241"></a>
-<span class="sourceLineNo">1242</span>        regions.removeAll(masterRegions);<a name="line.1242"></a>
-<span class="sourceLineNo">1243</span>      }<a name="line.1243"></a>
-<span class="sourceLineNo">1244</span>    }<a name="line.1244"></a>
-<span class="sourceLineNo">1245</span>    if (regions == null || regions.isEmpty()) {<a name="line.1245"></a>
-<span class="sourceLineNo">1246</span>      return assignments;<a name="line.1246"></a>
-<span class="sourceLineNo">1247</span>    }<a name="line.1247"></a>
-<span class="sourceLineNo">1248</span><a name="line.1248"></a>
-<span class="sourceLineNo">1249</span>    int numServers = servers == null ? 0 : servers.size();<a name="line.1249"></a>
-<span class="sourceLineNo">1250</span>    if (numServers == 0) {<a name="line.1250"></a>
-<span class="sourceLineNo">1251</span>      LOG.warn("Wanted to do round robin assignment but no servers to assign to");<a name="line.1251"></a>
-<span class="sourceLineNo">1252</span>      return null;<a name="line.1252"></a>
-<span class="sourceLineNo">1253</span>    }<a name="line.1253"></a>
-<span class="sourceLineNo">1254</span><a name="line.1254"></a>
-<span class="sourceLineNo">1255</span>    // TODO: instead of retainAssignment() and roundRobinAssignment(), we should just run the<a name="line.1255"></a>
-<span class="sourceLineNo">1256</span>    // normal LB.balancerCluster() with unassignedRegions. We only need to have a candidate<a name="line.1256"></a>
-<span class="sourceLineNo">1257</span>    // generator for AssignRegionAction. The LB will ensure the regions are mostly local<a name="line.1257"></a>
-<span class="sourceLineNo">1258</span>    // and balanced. This should also run fast with fewer number of iterations.<a name="line.1258"></a>
-<span class="sourceLineNo">1259</span><a name="line.1259"></a>
-<span class="sourceLineNo">1260</span>    if (numServers == 1) { // Only one server, nothing fancy we can do here<a name="line.1260"></a>
-<span class="sourceLineNo">1261</span>      ServerName server = servers.get(0);<a name="line.1261"></a>
-<span class="sourceLineNo">1262</span>      assignments.put(server, new ArrayList&lt;&gt;(regions));<a name="line.1262"></a>
-<span class="sourceLineNo">1263</span>      return assignments;<a name="line.1263"></a>
-<span class="sourceLineNo">1264</span>    }<a name="line.1264"></a>
-<span class="sourceLineNo">1265</span><a name="line.1265"></a>
-<span class="sourceLineNo">1266</span>    Cluster cluster = createCluster(servers, regions, false);<a name="line.1266"></a>
-<span class="sourceLineNo">1267</span>    List&lt;RegionInfo&gt; unassignedRegions = new ArrayList&lt;&gt;();<a name="line.1267"></a>
-<span class="sourceLineNo">1268</span><a name="line.1268"></a>
-<span class="sourceLineNo">1269</span>    roundRobinAssignment(cluster, regions, unassignedRegions,<a name="line.1269"></a>
-<span class="sourceLineNo">1270</span>      servers, assignments);<a name="line.1270"></a>
-<span class="sourceLineNo">1271</span><a name="line.1271"></a>
-<span class="sourceLineNo">1272</span>    List&lt;RegionInfo&gt; lastFewRegions = new ArrayList&lt;&gt;();<a name="line.1272"></a>
-<span class="sourceLineNo">1273</span>    // assign the remaining by going through the list and try to assign to servers one-by-one<a name="line.1273"></a>
-<span class="sourceLineNo">1274</span>    int serverIdx = RANDOM.nextInt(numServers);<a name="line.1274"></a>
-<span class="sourceLineNo">1275</span>    OUTER : for (RegionInfo region : unassignedRegions) {<a name="line.1275"></a>
-<span class="sourceLineNo">1276</span>      boolean assigned = false;<a name="line.1276"></a>
-<span class="sourceLineNo">1277</span>      INNER : for (int j = 0; j &lt; numServers; j++) { // try all servers one by one<a name="line.1277"></a>
-<span class="sourceLineNo">1278</span>        ServerName serverName = servers.get((j + serverIdx) % numServers);<a name="line.1278"></a>
-<span class="sourceLineNo">1279</span>        if (!cluster.wouldLowerAvailability(region, serverName)) {<a name="line.1279"></a>
-<span class="sourceLineNo">1280</span>          List&lt;RegionInfo&gt; serverRegions =<a name="line.1280"></a>
-<span class="sourceLineNo">1281</span>              assignments.computeIfAbsent(serverName, k -&gt; new ArrayList&lt;&gt;());<a name="line.1281"></a>
-<span class="sourceLineNo">1282</span>          if (!RegionReplicaUtil.isDefaultReplica(region.getReplicaId())) {<a name="line.1282"></a>
-<span class="sourceLineNo">1283</span>            // if the region is not a default replica<a name="line.1283"></a>
-<span class="sourceLineNo">1284</span>            // check if the assignments map has the other replica region on this server<a name="line.1284"></a>
-<span class="sourceLineNo">1285</span>            for (RegionInfo hri : serverRegions) {<a name="line.1285"></a>
-<span class="sourceLineNo">1286</span>              if (RegionReplicaUtil.isReplicasForSameRegion(region, hri)) {<a name="line.1286"></a>
-<span class="sourceLineNo">1287</span>                if (LOG.isTraceEnabled()) {<a name="line.1287"></a>
-<span class="sourceLineNo">1288</span>                  LOG.trace("Skipping the server, " + serverName<a name="line.1288"></a>
-<span class="sourceLineNo">1289</span>                      + " , got the same server for the region " + region);<a name="line.1289"></a>
-<span class="sourceLineNo">1290</span>                }<a name="line.1290"></a>
-<span class="sourceLineNo">1291</span>                // do not allow this case. The unassignedRegions we got because the<a name="line.1291"></a>
-<span class="sourceLineNo">1292</span>                // replica region in this list was not assigned because of lower availablity issue.<a name="line.1292"></a>
-<span class="sourceLineNo">1293</span>                // So when we assign here we should ensure that as far as possible the server being<a name="line.1293"></a>
-<span class="sourceLineNo">1294</span>                // selected does not have the server where the replica region was not assigned.<a name="line.1294"></a>
-<span class="sourceLineNo">1295</span>                continue INNER; // continue the inner loop, ie go to the next server<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>          }<a name="line.1298"></a>
-<span class="sourceLineNo">1299</span>          serverRegions.add(region);<a name="line.1299"></a>
-<span class="sourceLineNo">1300</span>          cluster.doAssignRegion(region, serverName);<a name="line.1300"></a>
-<span class="sourceLineNo">1301</span>          serverIdx = (j + serverIdx + 1) % numServers; //remain from next server<a name="line.1301"></a>
-<span class="sourceLineNo">1302</span>          assigned = true;<a name="line.1302"></a>
-<span class="sourceLineNo">1303</span>          break;<a name="line.1303"></a>
-<span class="sourceLineNo">1304</span>        }<a name="line.1304"></a>
-<span class="sourceLineNo">1305</span>      }<a name="line.1305"></a>
-<span class="sourceLineNo">1306</span>      if (!assigned) {<a name="line.1306"></a>
-<span class="sourceLineNo">1307</span>        lastFewRegions.add(region);<a name="line.1307"></a>
-<span class="sourceLineNo">1308</span>      }<a name="line.1308"></a>
-<span class="sourceLineNo">1309</span>    }<a name="line.1309"></a>
-<span class="sourceLineNo">1310</span>    // just sprinkle the rest of the regions on random regionservers. The balanceCluster will<a name="line.1310"></a>
-<span class="sourceLineNo">1311</span>    // make it optimal later. we can end up with this if numReplicas &gt; numServers.<a name="line.1311"></a>
-<span class="sourceLineNo">1312</span>    for (RegionInfo region : lastFewRegions) {<a name="line.1312"></a>
-<span class="sourceLineNo">1313</span>      int i = RANDOM.nextInt(numServers);<a name="line.1313"></a>
-<span class="sourceLineNo">1314</span>      ServerName server = servers.get(i);<a name="line.1314"></a>
-<span class="sourceLineNo">1315</span>      List&lt;RegionInfo&gt; serverRegions = assignments.computeIfAbsent(server, k -&gt; new ArrayList&lt;&gt;());<a name="line.1315"></a>
-<span class="sourceLineNo">1316</span>      serverRegions.add(region);<a name="line.1316"></a>
-<span class="sourceLineNo">1317</span>      cluster.doAssignRegion(region, server);<a name="line.1317"></a>
-<span class="sourceLineNo">1318</span>    }<a name="line.1318"></a>
-<span class="sourceLineNo">1319</span>    return assignments;<a name="line.1319"></a>
-<span class="sourceLineNo">1320</span>  }<a name="line.1320"></a>
-<span class="sourceLineNo">1321</span><a name="line.1321"></a>
-<span class="sourceLineNo">1322</span>  protected Cluster createCluster(List&lt;ServerName&gt; servers, Collection&lt;RegionInfo&gt; regions,<a name="line.1322"></a>
-<span class="sourceLineNo">1323</span>      boolean hasRegionReplica) {<a name="line.1323"></a>
-<span class="sourceLineNo">1324</span>    // Get the snapshot of the current assignments for the regions in question, and then create<a name="line.1324"></a>
-<span class="sourceLineNo">1325</span>    // a cluster out of it. Note that we might have replicas already assigned to some servers<a name="line.1325"></a>
-<span class="sourceLineNo">1326</span>    // earlier. So we want to get the snapshot to see those assignments, but this will only contain<a name="line.1326"></a>
-<span class="sourceLineNo">1327</span>    // replicas of the regions that are passed (for performance).<a name="line.1327"></a>
-<span class="sourceLineNo">1328</span>    Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; clusterState = null;<a name="line.1328"></a>
-<span class="sourceLineNo">1329</span>    if (!hasRegionReplica) {<a name="line.1329"></a>
-<span class="sourceLineNo">1330</span>      clusterState = getRegionAssignmentsByServer(regions);<a name="line.1330"></a>
-<span class="sourceLineNo">1331</span>    } else {<a name="line.1331"></a>
-<span class="sourceLineNo">1332</span>      // for the case where we have region replica it is better we get the entire cluster's snapshot<a name="line.1332"></a>
-<span class="sourceLineNo">1333</span>      clusterState = getRegionAssignmentsByServer(null);<a name="line.1333"></a>
-<span class="sourceLineNo">1334</span>    }<a name="line.1334"></a>
-<span class="sourceLineNo">1335</span><a name="line.1335"></a>
-<span class="sourceLineNo">1336</span>    for (ServerName server : servers) {<a name="line.1336"></a>
-<span class="sourceLineNo">1337</span>      if (!clusterState.containsKey(server)) {<a name="line.1337"></a>
-<span class="sourceLineNo">1338</span>        clusterState.put(server, EMPTY_REGION_LIST);<a name="line.1338"></a>
-<span class="sourceLineNo">1339</span>      }<a name="line.1339"></a>
-<span class="sourceLineNo">1340</span>    }<a name="line.1340"></a>
-<span class="sourceLineNo">1341</span>    return new Cluster(regions, clusterState, null, this.regionFinder,<a name="line.1341"></a>
-<span class="sourceLineNo">1342</span>        rackManager);<a name="line.1342"></a>
-<span class="sourceLineNo">1343</span>  }<a name="line.1343"></a>
-<span class="sourceLineNo">1344</span><a name="line.1344"></a>
-<span class="sourceLineNo">1345</span>  private List&lt;ServerName&gt; findIdleServers(List&lt;ServerName&gt; servers) {<a name="line.1345"></a>
-<span class="sourceLineNo">1346</span>    return this.services.getServerManager()<a name="line.1346"></a>
-<span class="sourceLineNo">1347</span>            .getOnlineServersListWithPredicator(servers, IDLE_SERVER_PREDICATOR);<a name="line.1347"></a>
-<span class="sourceLineNo">1348</span>  }<a name="line.1348"></a>
-<span class="sourceLineNo">1349</span><a name="line.1349"></a>
-<span class="sourceLineNo">1350</span>  /**<a name="line.1350"></a>
-<span class="sourceLineNo">1351</span>   * Used to assign a single region to a random server.<a name="line.1351"></a>
-<span class="sourceLineNo">1352</span>   */<a name="line.1352"></a>
-<span class="sourceLineNo">1353</span>  @Override<a name="line.1353"></a>
-<span class="sourceLineNo">1354</span>  public ServerName randomAssignment(RegionInfo regionInfo, List&lt;ServerName&gt; servers)<a name="line.1354"></a>
-<span class="sourceLineNo">1355</span>      throws HBaseIOException {<a name="line.1355"></a>
-<span class="sourceLineNo">1356</span>    metricsBalancer.incrMiscInvocations();<a name="line.1356"></a>
-<span class="sourceLineNo">1357</span>    if (servers != null &amp;&amp; servers.contains(masterServerName)) {<a name="line.1357"></a>
-<span class="sourceLineNo">1358</span>      if (shouldBeOnMaster(regionInfo)) {<a name="line.1358"></a>
-<span class="sourceLineNo">1359</span>        return masterServerName;<a name="line.1359"></a>
-<span class="sourceLineNo">1360</span>      }<a name="line.1360"></a>
-<span class="sourceLineNo">1361</span>      if (!LoadBalancer.isTablesOnMaster(getConf())) {<a name="line.1361"></a>
-<span class="sourceLineNo">1362</span>        // Guarantee we do not put any regions on master<a name="line.1362"></a>
-<span class="sourceLineNo">1363</span>        servers = new ArrayList&lt;&gt;(servers);<a name="line.1363"></a>
-<span class="sourceLineNo">1364</span>        servers.remove(masterServerName);<a name="line.1364"></a>
-<span class="sourceLineNo">1365</span>      }<a name="line.1365"></a>
-<span class="sourceLineNo">1366</span>    }<a name="line.1366"></a>
-<span class="sourceLineNo">1367</span><a name="line.1367"></a>
-<span class="sourceLineNo">1368</span>    int numServers = servers == null ? 0 : servers.size();<a name="line.1368"></a>
-<span class="sourceLineNo">1369</span>    if (numServers == 0) {<a name="line.1369"></a>
-<span class="sourceLineNo">1370</span>      LOG.warn("Wanted to retain assignment but no servers to assign to");<a name="line.1370"></a>
-<span class="sourceLineNo">1371</span>      return null;<a name="line.1371"></a>
-<span class="sourceLineNo">1372</span>    }<a name="line.1372"></a>
-<span class="sourceLineNo">1373</span>    if (numServers == 1) { // Only one server, nothing fancy we can do here<a name="line.1373"></a>
-<span class="sourceLineNo">1374</span>      return servers.get(0);<a name="line.1374"></a>
-<span class="sourceLineNo">1375</span>    }<a name="line.1375"></a>
-<span class="sourceLineNo">1376</span>    List&lt;ServerName&gt; idleServers = findIdleServers(servers);<a name="line.1376"></a>
-<span class="sourceLineNo">1377</span>    if (idleServers.size() == 1) {<a name="line.1377"></a>
-<span class="sourceLineNo">1378</span>      return idleServers.get(0);<a name="line.1378"></a>
-<span class="sourceLineNo">1379</span>    }<a name="line.1379"></a>
-<span class="sourceLineNo">1380</span>    final List&lt;ServerName&gt; finalServers = idleServers.isEmpty() ?<a name="line.1380"></a>
-<span class="sourceLineNo">1381</span>            servers : idleServers;<a name="line.1381"></a>
-<span class="sourceLineNo">1382</span>    List&lt;RegionInfo&gt; regions = Lists.newArrayList(regionInfo);<a name="line.1382"></a>
-<span class="sourceLineNo">1383</span>    Cluster cluster = createCluster(finalServers, regions, false);<a name="line.1383"></a>
-<span class="sourceLineNo">1384</span>    return randomAssignment(cluster, regionInfo, finalServers);<a name="line.1384"></a>
-<span class="sourceLineNo">1385</span>  }<a name="line.1385"></a>
-<span class="sourceLineNo">1386</span><a name="line.1386"></a>
-<span class="sourceLineNo">1387</span>  /**<a name="line.1387"></a>
-<span class="sourceLineNo">1388</span>   * Generates a bulk assignment startup plan, attempting to reuse the existing<a name="line.1388"></a>
-<span class="sourceLineNo">1389</span>   * assignment information from META, but adjusting for the specified list of<a name="line.1389"></a>
-<span class="sourceLineNo">1390</span>   * available/online servers available for assignment.<a name="line.1390"></a>
-<span class="sourceLineNo">1391</span>   * &lt;p&gt;<a name="line.1391"></a>
-<span class="sourceLineNo">1392</span>   * Takes a map of all regions to their existing assignment from META. Also<a name="line.1392"></a>
-<span class="sourceLineNo">1393</span>   * takes a list of online servers for regions to be assigned to. Attempts to<a name="line.1393"></a>
-<span class="sourceLineNo">1394</span>   * retain all assignment, so in some instances initial assignment will not be<a name="line.1394"></a>
-<span class="sourceLineNo">1395</span>   * completely balanced.<a name="line.1395"></a>
-<span class="sourceLineNo">1396</span>   * &lt;p&gt;<a name="line.1396"></a>
-<span class="sourceLineNo">1397</span>   * Any leftover regions without an existing server to be assigned to will be<a name="line.1397"></a>
-<span class="sourceLineNo">1398</span>   * assigned randomly to available servers.<a name="line.1398"></a>
-<span class="sourceLineNo">1399</span>   *<a name="line.1399"></a>
-<span class="sourceLineNo">1400</span>   * @param regions regions and existing assignment from meta<a name="line.1400"></a>
-<span class="sourceLineNo">1401</span>   * @param servers available servers<a name="line.1401"></a>
-<span class="sourceLineNo">1402</span>   * @return map of servers and regions to be assigned to them<a name="line.1402"></a>
-<span class="sourceLineNo">1403</span>   */<a name="line.1403"></a>
-<span class="sourceLineNo">1404</span>  @Override<a name="line.1404"></a>
-<span class="sourceLineNo">1405</span>  public Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; retainAssignment(Map&lt;RegionInfo, ServerName&gt; regions,<a name="line.1405"></a>
-<span class="sourceLineNo">1406</span>      List&lt;ServerName&gt; servers) throws HBaseIOException {<a name="line.1406"></a>
-<span class="sourceLineNo">1407</span>    // Update metrics<a name="line.1407"></a>
-<span class="sourceLineNo">1408</span>    metricsBalancer.incrMiscInvocations();<a name="line.1408"></a>
-<span class="sourceLineNo">1409</span>    Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignments = assignMasterSystemRegions(regions.keySet(), servers);<a name="line.1409"></a>
-<span class="sourceLineNo">1410</span>    if (assignments != null &amp;&amp; !assignments.isEmpty()) {<a name="line.1410"></a>
-<span class="sourceLineNo">1411</span>      servers = new ArrayList&lt;&gt;(servers);<a name="line.1411"></a>
-<span class="sourceLineNo">1412</span>      // Guarantee not to put other regions on master<a name="line.1412"></a>
-<span class="sourceLineNo">1413</span>      servers.remove(masterServerName);<a name="line.1413"></a>
-<span class="sourceLineNo">1414</span>      List&lt;RegionInfo&gt; masterRegions = assignments.get(masterServerName);<a name="line.1414"></a>
-<span class="sourceLineNo">1415</span>      regions = regions.entrySet().stream().filter(e -&gt; !masterRegions.contains(e.getKey()))<a name="line.1415"></a>
-<span class="sourceLineNo">1416</span>          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));<a name="line.1416"></a>
-<span class="sourceLineNo">1417</span>    }<a name="line.1417"></a>
-<span class="sourceLineNo">1418</span>    if (regions.isEmpty()) {<a name="line.1418"></a>
-<span class="sourceLineNo">1419</span>      return assignments;<a name="line.1419"></a>
-<span class="sourceLineNo">1420</span>    }<a name="line.1420"></a>
-<span class="sourceLineNo">1421</span><a name="line.1421"></a>
-<span class="sourceLineNo">1422</span>    int numServers = servers == null ? 0 : servers.size();<a name="line.1422"></a>
-<span class="sourceLineNo">1423</span>    if (numServers == 0) {<a name="line.1423"></a>
-<span class="sourceLineNo">1424</span>      LOG.warn("Wanted to do retain assignment but no servers to assign to");<a name="line.1424"></a>
-<span class="sourceLineNo">1425</span>      return null;<a name="line.1425"></a>
-<span class="sourceLineNo">1426</span>    }<a name="line.1426"></a>
-<span class="sourceLineNo">1427</span>    if (numServers == 1) { // Only one server, nothing fancy we can do here<a name="line.1427"></a>
-<span class="sourceLineNo">1428</span>      ServerName server = servers.get(0);<a name="line.1428"></a>
-<span class="sourceLineNo">1429</span>      assignments.put(server, new ArrayList&lt;&gt;(regions.keySet()));<a name="line.1429"></a>
-<span class="sourceLineNo">1430</span>      return assignments;<a name="line.1430"></a>
-<span class="sourceLineNo">1431</span>    }<a name="line.1431"></a>
-<span class="sourceLineNo">1432</span><a name="line.1432"></a>
-<span class="sourceLineNo">1433</span>    // Group all of the old assignments by their hostname.<a name="line.1433"></a>
-<span class="sourceLineNo">1434</span>    // We can't group directly by ServerName since the servers all have<a name="line.1434"></a>
-<span class="sourceLineNo">1435</span>    // new start-codes.<a name="line.1435"></a>
-<span class="sourceLineNo">1436</span><a name="line.1436"></a>
-<span class="sourceLineNo">1437</span>    // Group the servers by their hostname. It's possible we have multiple<a name="line.1437"></a>
-<span class="sourceLineNo">1438</span>    // servers on the same host on different ports.<a name="line.1438"></a>
-<span class="sourceLineNo">1439</span>    ArrayListMultimap&lt;String, ServerName&gt; serversByHostname = ArrayListMultimap.create();<a name="line.1439"></a>
-<span class="sourceLineNo">1440</span>    for (ServerName server : servers) {<a name="line.1440"></a>
-<span class="sourceLineNo">1441</span>      assignments.put(server, new ArrayList&lt;&gt;());<a name="line.1441"></a>
-<span class="sourceLineNo">1442</span>      serversByHostname.put(server.getHostnameLowerCase(), server);<a name="line.1442"></a>
-<span class="sourceLineNo">1443</span>    }<a name="line.1443"></a>
-<span class="sourceLineNo">1444</span><a name="line.1444"></a>
-<span class="sourceLineNo">1445</span>    // Collection of the hostnames that used to have regions<a name="line.1445"></a>
-<span class="sourceLineNo">1446</span>    // assigned, but for which we no longer have any RS running<a name="line.1446"></a>
-<span class="sourceLineNo">1447</span>    // after the cluster restart.<a name="line.1447"></a>
-<span class="sourceLineNo">1448</span>    Set&lt;String&gt; oldHostsNoLongerPresent = Sets.newTreeSet();<a name="line.1448"></a>
-<span class="sourceLineNo">1449</span><a name="line.1449"></a>
-<span class="sourceLineNo">1450</span>    // If the old servers aren't present, lets assign those regions later.<a name="line.1450"></a>
-<span class="sourceLineNo">1451</span>    List&lt;RegionInfo&gt; randomAssignRegions = Lists.newArrayList();<a name="line.1451"></a>
-<span class="sourceLineNo">1452</span><a name="line.1452"></a>
-<span class="sourceLineNo">1453</span>    int numRandomAssignments = 0;<a name="line.1453"></a>
-<span class="sourceLineNo">1454</span>    int numRetainedAssigments = 0;<a name="line.1454"></a>
-<span class="sourceLineNo">1455</span>    boolean hasRegionReplica = false;<a name="line.1455"></a>
-<span class="sourceLineNo">1456</span>    for (Map.Entry&lt;RegionInfo, ServerName&gt; entry : regions.entrySet()) {<a name="line.1456"></a>
-<span class="sourceLineNo">1457</span>      RegionInfo region = entry.getKey();<a name="line.1457"></a>
-<span class="sourceLineNo">1458</span>      ServerName oldServerName = entry.getValue();<a name="line.1458"></a>
-<span class="sourceLineNo">1459</span>      // In the current set of regions even if one has region replica let us go with<a name="line.1459"></a>
-<span class="sourceLineNo">1460</span>      // getting the entire snapshot<a name="line.1460"></a>
-<span class="sourceLineNo">1461</span>      if (this.services != null) { // for tests<a name="line.1461"></a>
-<span class="sourceLineNo">1462</span>        AssignmentManager am = this.services.getAssignmentManager();<a name="line.1462"></a>
-<span class="sourceLineNo">1463</span>        if (am != null) {<a name="line.1463"></a>
-<span class="sourceLineNo">1464</span>          RegionStates states = am.getRegionStates();<a name="line.1464"></a>
-<span class="sourceLineNo">1465</span>          if (!hasRegionReplica &amp;&amp; states != null &amp;&amp; states.isReplicaAvailableForRegion(region)) {<a name="line.1465"></a>
-<span class="sourceLineNo">1466</span>            hasRegionReplica = true;<a name="line.1466"></a>
-<span class="sourceLineNo">1467</span>          }<a name="line.1467"></a>
-<span class="sourceLineNo">1468</span>        }<a name="line.1468"></a>
-<span class="sourceLineNo">1469</span>      }<a name="line.1469"></a>
-<span class="sourceLineNo">1470</span>      List&lt;ServerName&gt; localServers = new ArrayList&lt;&gt;();<a name="line.1470"></a>
-<span class="sourceLineNo">1471</span>      if (oldServerName != null) {<a name="line.1471"></a>
-<span class="sourceLineNo">1472</span>        localServers = serversByHostname.get(oldServerName.getHostnameLowerCase());<a name="line.1472"></a>
-<span class="sourceLineNo">1473</span>      }<a name="line.1473"></a>
-<span class="sourceLineNo">1474</span>      if (localServers.isEmpty()) {<a name="line.1474"></a>
-<span class="sourceLineNo">1475</span>        // No servers on the new cluster match up with this hostname, assign randomly, later.<a name="line.1475"></a>
-<span class="sourceLineNo">1476</span>        randomAssignRegions.add(region);<a name="line.1476"></a>
-<span class="sourceLineNo">1477</span>        if (oldServerName != null) {<a name="line.1477"></a>
-<span class="sourceLineNo">1478</span>          oldHostsNoLongerPresent.add(oldServerName.getHostnameLowerCase());<a name="line.1478"></a>
-<span class="sourceLineNo">1479</span>        }<a name="line.1479"></a>
-<span class="sourceLineNo">1480</span>      } else if (localServers.size() == 1) {<a name="line.1480"></a>
-<span class="sourceLineNo">1481</span>        // the usual case - one new server on same host<a name="line.1481"></a>
-<span class="sourceLineNo">1482</span>        ServerName target = localServers.get(0);<a name="line.1482"></a>
-<span class="sourceLineNo">1483</span>        assignments.get(target).add(region);<a name="line.1483"></a>
-<span class="sourceLineNo">1484</span>        numRetainedAssigments++;<a name="line.1484"></a>
-<span class="sourceLineNo">1485</span>      } else {<a name="line.1485"></a>
-<span class="sourceLineNo">1486</span>        // multiple new servers in the cluster on this same host<a name="line.1486"></a>
-<span class="sourceLineNo">1487</span>        if (localServers.contains(oldServerName)) {<a name="line.1487"></a>
-<span class="sourceLineNo">1488</span>          assignments.get(oldServerName).add(region);<a name="line.1488"></a>
-<span class="sourceLineNo">1489</span>          numRetainedAssigments++;<a name="line.1489"></a>
-<span class="sourceLineNo">1490</span>        } else {<a name="line.1490"></a>
-<span class="sourceLineNo">1491</span>          ServerName target = null;<a name="line.1491"></a>
-<span class="sourceLineNo">1492</span>          for (ServerName tmp : localServers) {<a name="line.1492"></a>
-<span class="sourceLineNo">1493</span>            if (tmp.getPort() == oldServerName.getPort()) {<a name="line.1493"></a>
-<span class="sourceLineNo">1494</span>              target = tmp;<a name="line.1494"></a>
-<span class="sourceLineNo">1495</span>              assignments.get(tmp).add(region);<a name="line.1495"></a>
-<span class="sourceLineNo">1496</span>              numRetainedAssigments++;<a name="line.1496"></a>
-<span class="sourceLineNo">1497</span>              break;<a name="line.1497"></a>
-<span class="sourceLineNo">1498</span>            }<a name="line.1498"></a>
-<span class="sourceLineNo">1499</span>          }<a name="line.1499"></a>
-<span class="sourceLineNo">1500</span>          if (target == null) {<a name="line.1500"></a>
-<span class="sourceLineNo">1501</span>            randomAssignRegions.add(region);<a name="line.1501"></a>
-<span class="sourceLineNo">1502</span>          }<a name="line.1502"></a>
-<span class="sourceLineNo">1503</span>        }<a name="line.1503"></a>
-<span class="sourceLineNo">1504</span>      }<a name="line.1504"></a>
-<span class="sourceLineNo">1505</span>    }<a name="line.1505"></a>
-<span class="sourceLineNo">1506</span><a name="line.1506"></a>
-<span class="sourceLineNo">1507</span>    // If servers from prior assignment aren't present, then lets do randomAssignment on regions.<a name="line.1507"></a>
-<span class="sourceLineNo">1508</span>    if (randomAssignRegions.size() &gt; 0) {<a name="line.1508"></a>
-<span class="sourceLineNo">1509</span>      Cluster cluster = createCluster(servers, regions.keySet(), hasRegionReplica);<a name="line.1509"></a>
-<span class="sourceLineNo">1510</span>      for (Map.Entry&lt;ServerName, List&lt;RegionInfo&gt;&gt; entry : assignments.entrySet()) {<a name="line.1510"></a>
-<span class="sourceLineNo">1511</span>        ServerName sn = entry.getKey();<a name="line.1511"></a>
-<span class="sourceLineNo">1512</span>        for (RegionInfo region : entry.getValue()) {<a name="line.1512"></a>
-<span class="sourceLineNo">1513</span>          cluster.doAssignRegion(region, sn);<a name="line.1513"></a>
-<span class="sourceLineNo">1514</span>        }<a name="line.1514"></a>
-<span class="sourceLineNo">1515</span>      }<a name="line.1515"></a>
-<span class="sourceLineNo">1516</span>      for (RegionInfo region : randomAssignRegions) {<a name="line.1516"></a>
-<span class="sourceLineNo">1517</span>        ServerName target = randomAssignment(cluster, region, servers);<a name="line.1517"></a>
-<span class="sourceLineNo">1518</span>        assignments.get(target).add(region);<a name="line.1518"></a>
-<span class="sourceLineNo">1519</span>        numRandomAssignments++;<a name="line.1519"></a>
-<span class="sourceLineNo">1520</span>      }<a name="line.1520"></a>
-<span class="sourceLineNo">1521</span>    }<a name="line.1521"></a>
-<span class="sourceLineNo">1522</span><a name="line.1522"></a>
-<span class="sourceLineNo">1523</span>    String randomAssignMsg = "";<a name="line.1523"></a>
-<span class="sourceLineNo">1524</span>    if (numRandomAssignments &gt; 0) {<a name="line.1524"></a>
-<span class="sourceLineNo">1525</span>      randomAssignMsg =<a name="line.1525"></a>
-<span class="sourceLineNo">1526</span>          numRandomAssignments + " regions were assigned "<a name="line.1526"></a>
-<span class="sourceLineNo">1527</span>              + "to random hosts, since the old hosts for these regions are no "<a name="line.1527"></a>
-<span class="sourceLineNo">1528</span>              + "longer present in the cluster. These hosts were:\n  "<a name="line.1528"></a>
-<span class="sourceLineNo">1529</span>              + Joiner.on("\n  ").join(oldHostsNoLongerPresent);<a name="line.1529"></a>
-<span class="sourceLineNo">1530</span>    }<a name="line.1530"></a>
-<span class="sourceLineNo">1531</span><a name="line.1531"></a>
-<span class="sourceLineNo">1532</span>    LOG.info("Reassigned " + regions.size() + " regions. " + numRetainedAssigments<a name="line.1532"></a>
-<span class="sourceLineNo">1533</span>        + " retained the pre-restart assignment. " + randomAssignMsg);<a name="line.1533"></a>
-<span class="sourceLineNo">1534</span>    return assignments;<a name="line.1534"></a>
-<span class="sourceLineNo">1535</span>  }<a name="line.1535"></a>
-<span class="sourceLineNo">1536</span><a name="line.1536"></a>
-<span class="sourceLineNo">1537</span>  @Override<a name="line.1537"></a>
-<span class="sourceLineNo">1538</span>  public void initialize() throws HBaseIOException{<a name="line.1538"></a>
-<span class="sourceLineNo">1539</span>  }<a name="line.1539"></a>
-<span class="sourceLineNo">1540</span><a name="line.1540"></a>
-<span class="sourceLineNo">1541</span>  @Override<a name="line.1541"></a>
-<span class="sourceLineNo">1542</span>  public void regionOnline(RegionInfo regionInfo, ServerName sn) {<a name="line.1542"></a>
-<span class="sourceLineNo">1543</span>  }<a name="line.1543"></a>
-<span class="sourceLineNo">1544</span><a name="line.1544"></a>
-<span class="sourceLineNo">1545</span>  @Override<a name="line.1545"></a>
-<span class="sourceLineNo">1546</span>  public void regionOffline(RegionInfo regionInfo) {<a name="line.1546"></a>
-<span class="sourceLineNo">1547</span>  }<a name="line.1547"></a>
-<span class="sourceLineNo">1548</span><a name="line.1548"></a>
-<span class="sourceLineNo">1549</span>  @Override<a name="line.1549"></a>
-<span class="sourceLineNo">1550</span>  public boolean isStopped() {<a name="line.1550"></a>
-<span class="sourceLineNo">1551</span>    return stopped;<a name="line.1551"></a>
-<span class="sourceLineNo">1552</span>  }<a name="line.1552"></a>
-<span class="sourceLineNo">1553</span><a name="line.1553"></a>
-<span class="sourceLineNo">1554</span>  @Override<a name="line.1554"></a>
-<span class="sourceLineNo">1555</span>  public void stop(String why) {<a name="line.1555"></a>
-<span class="sourceLineNo">1556</span>    LOG.info("Load Balancer stop requested: "+why);<a name="line.1556"></a>
-<span class="sourceLineNo">1557</span>    stopped = true;<a name="line.1557"></a>
-<span class="sourceLineNo">1558</span>  }<a name="line.1558"></a>
-<span class="sourceLineNo">1559</span><a name="line.1559"></a>
-<span class="sourceLineNo">1560</span>  /**<a name="line.1560"></a>
-<span class="sourceLineNo">1561</span>  * Updates the balancer status tag reported to JMX<a name="line.1561"></a>
-<span class="sourceLineNo">1562</span>  */<a name="line.1562"></a>
-<span class="sourceLineNo">1563</span>  public void updateBalancerStatus(boolean status) {<a name="line.1563"></a>
-<span class="sourceLineNo">1564</span>    metricsBalancer.balancerStatus(status);<a name="line.1564"></a>
-<span class="sourceLineNo">1565</span>  }<a name="line.1565"></a>
-<span class="sourceLineNo">1566</span><a name="line.1566"></a>
-<span class="sourceLineNo">1567</span>  /**<a name="line.1567"></a>
-<span class="sourceLineNo">1568</span>   * Used to assign a single region to a random server.<a name="line.1568"></a>
-<span class="sourceLineNo">1569</span>   */<a name="line.1569"></a>
-<span class="sourceLineNo">1570</span>  private ServerName randomAssignment(Cluster cluster, RegionInfo regionInfo,<a name="line.1570"></a>
-<span class="sourceLineNo">1571</span>      List&lt;ServerName&gt; servers) {<a name="line.1571"></a>
-<span class="sourceLineNo">1572</span>    int numServers = servers.size(); // servers is not null, numServers &gt; 1<a name="line.1572"></a>
-<span class="sourceLineNo">1573</span>    ServerName sn = null;<a name="line.1573"></a>
-<span class="sourceLineNo">1574</span>    final int maxIterations = numServers * 4;<a name="line.1574"></a>
-<span class="sourceLineNo">1575</span>    int iterations = 0;<a name="line.1575"></a>
-<span class="sourceLineNo">1576</span>    List&lt;ServerName&gt; usedSNs = new ArrayList&lt;&gt;(servers.size());<a name="line.1576"></a>
-<span class="sourceLineNo">1577</span>    do {<a name="line.1577"></a>
-<span class="sourceLineNo">1578</span>      int i = RANDOM.nextInt(numServers);<a name="line.1578"></a>
-<span class="sourceLineNo">1579</span>      sn = servers.get(i);<a name="line.1579"></a>
-<span class="sourceLineNo">1580</span>      if (!usedSNs.contains(sn)) {<a name="line.1580"></a>
-<span class="sourceLineNo">1581</span>        usedSNs.add(sn);<a name="line.1581"></a>
-<span class="sourceLineNo">1582</span>      }<a name="line.1582"></a>
-<span class="sourceLineNo">1583</span>    } while (cluster.wouldLowerAvailability(regionInfo, sn)<a name="line.1583"></a>
-<span class="sourceLineNo">1584</span>        &amp;&amp; iterations++ &lt; maxIterations);<a name="line.1584"></a>
-<span class="sourceLineNo">1585</span>    if (iterations &gt;= maxIterations) {<a name="line.1585"></a>
-<span class="sourceLineNo">1586</span>      // We have reached the max. Means the servers that we collected is still lowering the<a name="line.1586"></a>
-<span class="sourceLineNo">1587</span>      // availability<a name="line.1587"></a>
-<span class="sourceLineNo">1588</span>      for (ServerName unusedServer : servers) {<a name="line.1588"></a>
-<span class="sourceLineNo">1589</span>        if (!usedSNs.contains(unusedServer)) {<a name="line.1589"></a>
-<span class="sourceLineNo">1590</span>          // check if any other unused server is there for us to use.<a name="line.1590"></a>
-<span class="sourceLineNo">1591</span>          // If so use it. Else we have not other go but to go with one of them<a name="line.1591"></a>
-<span class="sourceLineNo">1592</span>          if (!cluster.wouldLowerAvailability(regionInfo, unusedServer)) {<a name="line.1592"></a>
-<span class="sourceLineNo">1593</span>            sn = unusedServer;<a name="line.1593"></a>
-<span class="sourceLineNo">1594</span>            break;<a name="line.1594"></a>
-<span class="sourceLineNo">1595</span>          }<a name="line.1595"></a>
-<span class="sourceLineNo">1596</span>        }<a name="line.1596"></a>
-<span class="sourceLineNo">1597</span>      }<a name="line.1597"></a>
-<span class="sourceLineNo">1598</span>    }<a name="line.1598"></a>
-<span class="sourceLineNo">1599</span>    cluster.doAssignRegion(regionInfo, sn);<a name="line.1599"></a>
-<span class="sourceLineNo">1600</span>    return sn;<a name="line.1600"></a>
-<span class="sourceLineNo">1601</span>  }<a name="line.1601"></a>
-<span class="sourceLineNo">1602</span><a name="line.1602"></a>
-<span class="sourceLineNo">1603</span>  /**<a name="line.1603"></a>
-<span class="sourceLineNo">1604</span>   * Round robin a list of regions to a list of servers<a name="line.1604"></a>
-<span class="sourceLineNo">1605</span>   */<a name="line.1605"></a>
-<span class="sourceLineNo">1606</span>  private void roundRobinAssignment(Cluster cluster, List&lt;RegionInfo&gt; regions,<a name="line.1606"></a>
-<span class="sourceLineNo">1607</span>      List&lt;RegionInfo&gt; unassignedRegions, List&lt;ServerName&gt; servers,<a name="line.1607"></a>
-<span class="sourceLineNo">1608</span>      Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignments) {<a name="line.1608"></a>
-<span class="sourceLineNo">1609</span><a name="line.1609"></a>
-<span class="sourceLineNo">1610</span>    int numServers = servers.size();<a name="line.1610"></a>
-<span class="sourceLineNo">1611</span>    int numRegions = regions.size();<a name="line.1611"></a>
-<span class="sourceLineNo">1612</span>    int max = (int) Math.ceil((float) numRegions / numServers);<a name="line.1612"></a>
-<span class="sourceLineNo">1613</span>    int serverIdx = 0;<a name="line.1613"></a>
-<span class="sourceLineNo">1614</span>    if (numServers &gt; 1) {<a name="line.1614"></a>
-<span class="sourceLineNo">1615</span>      serverIdx = RANDOM.nextInt(numServers);<a name="line.1615"></a>
-<span class="sourceLineNo">1616</span>    }<a name="line.1616"></a>
-<span class="sourceLineNo">1617</span>    int regionIdx = 0;<a name="line.1617"></a>
-<span class="sourceLineNo">1618</span><a name="line.1618"></a>
-<span class="sourceLineNo">1619</span>    for (int j = 0; j &lt; numServers; j++) {<a name="line.1619"></a>
-<span class="sourceLineNo">1620</span>      ServerName server = servers.get((j + serverIdx) % numServers);<a name="line.1620"></a>
-<span class="sourceLineNo">1621</span>      List&lt;RegionInfo&gt; serverRegions = new ArrayList&lt;&gt;(max);<a name="line.1621"></a>
-<span class="sourceLineNo">1622</span>      for (int i = regionIdx; i &lt; numRegions; i += numServers) {<a name="line.1622"></a>
-<span class="sourceLineNo">1623</span>        RegionInfo region = regions.get(i % numRegions);<a name="line.1623"></a>
-<span class="sourceLineNo">1624</span>        if (cluster.wouldLowerAvailability(region, server)) {<a name="line.1624"></a>
-<span class="sourceLineNo">1625</span>          unassignedRegions.add(region);<a name="line.1625"></a>
-<span class="sourceLineNo">1626</span>        } else {<a name="line.1626"></a>
-<span class="sourceLineNo">1627</span>          serverRegions.add(region);<a name="line.1627"></a>
-<span class="sourceLineNo">1628</span>          cluster.doAssignRegion(region, server);<a name="line.1628"></a>
-<span class="sourceLineNo">1629</span>        }<a name="line.1629"></a>
-<span class="sourceLineNo">1630</span>      }<a name="line.1630"></a>
-<span class="sourceLineNo">1631</span>      assignments.put(server, serverRegions);<a name="line.1631"></a>
-<span class="sourceLineNo">1632</span>      regionIdx++;<a name="line.1632"></a>
-<span class="sourceLineNo">1633</span>    }<a name="line.1633"></a>
-<span class="sourceLineNo">1634</span>  }<a name="line.1634"></a>
-<span class="sourceLineNo">1635</span><a name="line.1635"></a>
-<span class="sourceLineNo">1636</span>  protected Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; getRegionAssignmentsByServer(<a name="line.1636"></a>
-<span class="sourceLineNo">1637</span>    Collection&lt;RegionInfo&gt; regions) {<a name="line.1637"></a>
-<span class="sourceLineNo">1638</span>    if (this.services != null &amp;&amp; this.services.getAssignmentManager() != null) {<a name="line.1638"></a>
-<span class="sourceLineNo">1639</span>      return this.services.getAssignmentManager().getSnapShotOfAssignment(regions);<a name="line.1639"></a>
-<span class="sourceLineNo">1640</span>    } else {<a name="line.1640"></a>
-<span class="sourceLineNo">1641</span>      return new HashMap&lt;&gt;();<a name="line.1641"></a>
-<span class="sourceLineNo">1642</span>    }<a name="line.1642"></a>
-<span class="sourceLineNo">1643</span>  }<a name="line.1643"></a>
-<span class="sourceLineNo">1644</span><a name="line.1644"></a>
-<span class="sourceLineNo">1645</span>  @Override<a name="line.1645"></a>
-<span class="sourceLineNo">1646</span>  public void onConfigurationChange(Configuration conf) {<a name="line.1646"></a>
-<span class="sourceLineNo">1647</span>  }<a name="line.1647"></a>
-<span class="sourceLineNo">1648</span>}<a name="line.1648"></a>
+<span class="sourceLineNo">1044</span>  /**<a name="line.1044"></a>
+<span class="sourceLineNo">1045</span>   * Check if a region belongs to some system table.<a name="line.1045"></a>
+<span class="sourceLineNo">1046</span>   * If so, the primary replica may be expected to be put on the master regionserver.<a name="line.1046"></a>
+<span class="sourceLineNo">1047</span>   */<a name="line.1047"></a>
+<span class="sourceLineNo">1048</span>  public boolean shouldBeOnMaster(RegionInfo region) {<a name="line.1048"></a>
+<span class="sourceLineNo">1049</span>    return (this.maintenanceMode || this.onlySystemTablesOnMaster)<a name="line.1049"></a>
+<span class="sourceLineNo">1050</span>        &amp;&amp; region.getTable().isSystemTable();<a name="line.1050"></a>
+<span class="sourceLineNo">1051</span>  }<a name="line.1051"></a>
+<span class="sourceLineNo">1052</span><a name="line.1052"></a>
+<span class="sourceLineNo">1053</span>  /**<a name="line.1053"></a>
+<span class="sourceLineNo">1054</span>   * Balance the regions that should be on master regionserver.<a name="line.1054"></a>
+<span class="sourceLineNo">1055</span>   */<a name="line.1055"></a>
+<span class="sourceLineNo">1056</span>  protected List&lt;RegionPlan&gt; balanceMasterRegions(Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; clusterMap) {<a name="line.1056"></a>
+<span class="sourceLineNo">1057</span>    if (masterServerName == null || clusterMap == null || clusterMap.size() &lt;= 1) return null;<a name="line.1057"></a>
+<span class="sourceLineNo">1058</span>    List&lt;RegionPlan&gt; plans = null;<a name="line.1058"></a>
+<span class="sourceLineNo">1059</span>    List&lt;RegionInfo&gt; regions = clusterMap.get(masterServerName);<a name="line.1059"></a>
+<span class="sourceLineNo">1060</span>    if (regions != null) {<a name="line.1060"></a>
+<span class="sourceLineNo">1061</span>      Iterator&lt;ServerName&gt; keyIt = null;<a name="line.1061"></a>
+<span class="sourceLineNo">1062</span>      for (RegionInfo region: regions) {<a name="line.1062"></a>
+<span class="sourceLineNo">1063</span>        if (shouldBeOnMaster(region)) continue;<a name="line.1063"></a>
+<span class="sourceLineNo">1064</span><a name="line.1064"></a>
+<span class="sourceLineNo">1065</span>        // Find a non-master regionserver to host the region<a name="line.1065"></a>
+<span class="sourceLineNo">1066</span>        if (keyIt == null || !keyIt.hasNext()) {<a name="line.1066"></a>
+<span class="sourceLineNo">1067</span>          keyIt = clusterMap.keySet().iterator();<a name="line.1067"></a>
+<span class="sourceLineNo">1068</span>        }<a name="line.1068"></a>
+<span class="sourceLineNo">1069</span>        ServerName dest = keyIt.next();<a name="line.1069"></a>
+<span class="sourceLineNo">1070</span>        if (masterServerName.equals(dest)) {<a name="line.1070"></a>
+<span class="sourceLineNo">1071</span>          if (!keyIt.hasNext()) {<a name="line.1071"></a>
+<span class="sourceLineNo">1072</span>            keyIt = clusterMap.keySet().iterator();<a name="line.1072"></a>
+<span class="sourceLineNo">1073</span>          }<a name="line.1073"></a>
+<span class="sourceLineNo">1074</span>          dest = keyIt.next();<a name="line.1074"></a>
+<span class="sourceLineNo">1075</span>        }<a name="line.1075"></a>
+<span class="sourceLineNo">1076</span><a name="line.1076"></a>
+<span class="sourceLineNo">1077</span>        // Move this region away from the master regionserver<a name="line.1077"></a>
+<span class="sourceLineNo">1078</span>        RegionPlan plan = new RegionPlan(region, masterServerName, dest);<a name="line.1078"></a>
+<span class="sourceLineNo">1079</span>        if (plans == null) {<a name="line.1079"></a>
+<span class="sourceLineNo">1080</span>          plans = new ArrayList&lt;&gt;();<a name="line.1080"></a>
+<span class="sourceLineNo">1081</span>        }<a name="line.1081"></a>
+<span class="sourceLineNo">1082</span>        plans.add(plan);<a name="line.1082"></a>
+<span class="sourceLineNo">1083</span>      }<a name="line.1083"></a>
+<span class="sourceLineNo">1084</span>    }<a name="line.1084"></a>
+<span class="sourceLineNo">1085</span>    for (Map.Entry&lt;ServerName, List&lt;RegionInfo&gt;&gt; server: clusterMap.entrySet()) {<a name="line.1085"></a>
+<span class="sourceLineNo">1086</span>      if (masterServerName.equals(server.getKey())) continue;<a name="line.1086"></a>
+<span class="sourceLineNo">1087</span>      for (RegionInfo region: server.getValue()) {<a name="line.1087"></a>
+<span class="sourceLineNo">1088</span>        if (!shouldBeOnMaster(region)) continue;<a name="line.1088"></a>
+<span class="sourceLineNo">1089</span><a name="line.1089"></a>
+<span class="sourceLineNo">1090</span>        // Move this region to the master regionserver<a name="line.1090"></a>
+<span class="sourceLineNo">1091</span>        RegionPlan plan = new RegionPlan(region, server.getKey(), masterServerName);<a name="line.1091"></a>
+<span class="sourceLineNo">1092</span>        if (plans == null) {<a name="line.1092"></a>
+<span class="sourceLineNo">1093</span>          plans = new ArrayList&lt;&gt;();<a name="line.1093"></a>
+<span class="sourceLineNo">1094</span>        }<a name="line.1094"></a>
+<span class="sourceLineNo">1095</span>        plans.add(plan);<a name="line.1095"></a>
+<span class="sourceLineNo">1096</span>      }<a name="line.1096"></a>
+<span class="sourceLineNo">1097</span>    }<a name="line.1097"></a>
+<span class="sourceLineNo">1098</span>    return plans;<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>  /**<a name="line.1101"></a>
+<span class="sourceLineNo">1102</span>   * If master is configured to carry system tables only, in here is<a name="line.1102"></a>
+<span class="sourceLineNo">1103</span>   * where we figure what to assign it.<a name="line.1103"></a>
+<span class="sourceLineNo">1104</span>   */<a name="line.1104"></a>
+<span class="sourceLineNo">1105</span>  protected Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignMasterSystemRegions(<a name="line.1105"></a>
+<span class="sourceLineNo">1106</span>      Collection&lt;RegionInfo&gt; regions, List&lt;ServerName&gt; servers) {<a name="line.1106"></a>
+<span class="sourceLineNo">1107</span>    if (servers == null || regions == null || regions.isEmpty()) {<a name="line.1107"></a>
+<span class="sourceLineNo">1108</span>      return null;<a name="line.1108"></a>
+<span class="sourceLineNo">1109</span>    }<a name="line.1109"></a>
+<span class="sourceLineNo">1110</span>    Map&lt;ServerName, List&lt;RegionInfo&gt;&gt; assignments = new TreeMap&lt;&gt;();<a name="line.1110"></a>
+<span class="sourceLineNo">1111</span>    if (this.maintenanceMode || this.onlySystemTablesOnMaster) {<a name="line.1111"></a>
+<span class="sourceLineNo">1112</span>      if (masterServerName != null &amp;&amp; servers.contains(masterServerName)) {<a name="line.1112"></a>
+<span class="sourceLineNo">1113</span>        assignments.put(masterServerName, new ArrayList&lt;&gt;());<a name="line.1113"></a>
+<span class="sourceLineNo">1114</span>        for (RegionInfo region : regions) {<a name="line.1114"></a>
+<span class="sourceLineNo">1115</span>          if (shouldBeOnMaster(region)) {<a name="line.1115"></a>
+<span class="sourceLineNo">1116</span>            assignments.get(masterServerName).add(region);<a name="line.1116"></a>
+<span class="sourceLineNo">1117</span>          }<a name="line.1117"></a>
+<span class="sourceLineNo">1118</span>        }<a name="line.1118"></a>
+<span class="sourceLineNo">1119</span>      }<a name="line.1119"></a>
+<span class="sourceLineNo">1120</span>    }<a name="line.1120"></a>
+<span class="sourceLineNo">1121</span>    return assignments;<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>  @Override<a name="line.1124"></a>
+<span class="sourceLineNo">1125</span>  public Configuration getConf() {<a name="line.1125"></a>
+<span class="sourceLineNo">1126</span>    return this.config;<a name="line.1126"></a>
+<span class="sourceLineNo">1127</span>  }<a name="line.1127"></a>
+<span class="sourceLineNo">1128</span><a name="line.1128"></a>
+<span class="sourceLineNo">1129</span>  @Override<a name="line.1129"></a>
+<span class="sourceLineNo">1130</span>  public synchronized void setClusterMetrics(ClusterMetrics st) {<a name="line.1130"></a>
+<span class="sourceLineNo">1131</span>    this.clusterStatus = st;<a name="line.1131"></a>
+<span class="sourceLineNo">1132</span>    if (useRegionFinder) {<a name="line.1132"></a>
+<span class="sourceLineNo">1133</span>      regionFinder.setClusterMetrics(st);<a name="line.1133"></a>
+<span class="sourceLineNo">1134</span>    }<a name="line.1134"></a>
+<span class="sourceLineNo">1135</span>  }<a name="line.1135"></a>
+<span class="sourceLineNo">1136</span><a name="line.1136"></a>
+<span class="sourceLineNo">1137</span>  @Override<a name="line.1137"></a>
+<span class="sourceLineNo">1138</span>  public void setClusterLoad(Map&lt;TableName, Map&lt;ServerName, List&lt;RegionInfo&gt;&gt;&gt; clusterLoad){<a name="line.1138"></a>
+<span class="sourceLineNo">1139</span><a name="line.1139"></a>
+<span class="sourceLineNo">1140</span>  }<a name="line.1140"></a>
+<span class="sourceLineNo">1141</span><a name="line.1141"></a>
+<span class="sourceLineNo">1142</span>  @Override<a name="line.1142"></a>
+<span class="sourceLineNo">1143</span>  public void setMasterServices(MasterServices masterServices) {<a name="line.1143"></a>
+<span class="sourceLineNo">1144</span>    masterServerName = masterServices.getServerName();<a name="line.1144"></a>
+<span class="sourceLineNo">1145</span>    this.services = masterServices;<a name="line.1145"></a>
+<span class="sourceLineNo">1146</span>    if (useRegionFinder) {<a name="line.1146"></a>
+<span class="sourceLineNo">1147</span>      this.regionFinder.setServices(masterServices);<a name="line.1147"></a>
+<span class="sourceLineNo">1148</span>    }<a name="line.1148"></a>
+<span class="sourceLineNo">1149</span>    if (this.services.isInMaintenanceMode()) {<a name="line.1149"></a>
+<span class="sourceLineNo">1150</span>      this.maintenanceMode = true;<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 (this.maintenanceMode || 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=

<TRUNCATED>

Mime
View raw message