http://git-wip-us.apache.org/repos/asf/hbase-site/blob/293abb17/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.html
----------------------------------------------------------------------
diff --git 
a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.html
 
b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.html
index c372545..af3b364 100644
--- 
a/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.html
+++ 
b/devapidocs/src-html/org/apache/hadoop/hbase/master/balancer/BaseLoadBalancer.Cluster.Action.html
@@ -1279,322 +1279,339 @@
 <span class="sourceLineNo">1271</span>    List&lt;RegionInfo&gt; 
lastFewRegions = new ArrayList&lt;&gt;();<a name="line.1271"></a>
 <span class="sourceLineNo">1272</span>    // assign the remaining by going 
through the list and try to assign to servers one-by-one<a name="line.1272"></a>
 <span class="sourceLineNo">1273</span>    int serverIdx = 
RANDOM.nextInt(numServers);<a name="line.1273"></a>
-<span class="sourceLineNo">1274</span>    for (RegionInfo region : 
unassignedRegions) {<a name="line.1274"></a>
+<span class="sourceLineNo">1274</span>    OUTER : for (RegionInfo region : 
unassignedRegions) {<a name="line.1274"></a>
 <span class="sourceLineNo">1275</span>      boolean assigned = false;<a 
name="line.1275"></a>
-<span class="sourceLineNo">1276</span>      for (int j = 0; j &lt; numServers; 
j++) { // try all servers one by one<a name="line.1276"></a>
+<span class="sourceLineNo">1276</span>      INNER : for (int j = 0; j &lt; 
numServers; j++) { // try all servers one by one<a name="line.1276"></a>
 <span class="sourceLineNo">1277</span>        ServerName serverName = 
servers.get((j + serverIdx) % numServers);<a name="line.1277"></a>
 <span class="sourceLineNo">1278</span>        if 
(!cluster.wouldLowerAvailability(region, serverName)) {<a name="line.1278"></a>
 <span class="sourceLineNo">1279</span>          List&lt;RegionInfo&gt; 
serverRegions =<a name="line.1279"></a>
 <span class="sourceLineNo">1280</span>              
assignments.computeIfAbsent(serverName, k -&gt; new ArrayList&lt;&gt;());<a 
name="line.1280"></a>
-<span class="sourceLineNo">1281</span>          serverRegions.add(region);<a 
name="line.1281"></a>
-<span class="sourceLineNo">1282</span>          cluster.doAssignRegion(region, 
serverName);<a name="line.1282"></a>
-<span class="sourceLineNo">1283</span>          serverIdx = (j + serverIdx + 
1) % numServers; //remain from next server<a name="line.1283"></a>
-<span class="sourceLineNo">1284</span>          assigned = true;<a 
name="line.1284"></a>
-<span class="sourceLineNo">1285</span>          break;<a name="line.1285"></a>
-<span class="sourceLineNo">1286</span>        }<a name="line.1286"></a>
-<span class="sourceLineNo">1287</span>      }<a name="line.1287"></a>
-<span class="sourceLineNo">1288</span>      if (!assigned) {<a 
name="line.1288"></a>
-<span class="sourceLineNo">1289</span>        lastFewRegions.add(region);<a 
name="line.1289"></a>
-<span class="sourceLineNo">1290</span>      }<a name="line.1290"></a>
-<span class="sourceLineNo">1291</span>    }<a name="line.1291"></a>
-<span class="sourceLineNo">1292</span>    // just sprinkle the rest of the 
regions on random regionservers. The balanceCluster will<a name="line.1292"></a>
-<span class="sourceLineNo">1293</span>    // make it optimal later. we can end 
up with this if numReplicas &gt; numServers.<a name="line.1293"></a>
-<span class="sourceLineNo">1294</span>    for (RegionInfo region : 
lastFewRegions) {<a name="line.1294"></a>
-<span class="sourceLineNo">1295</span>      int i = 
RANDOM.nextInt(numServers);<a name="line.1295"></a>
-<span class="sourceLineNo">1296</span>      ServerName server = 
servers.get(i);<a name="line.1296"></a>
-<span class="sourceLineNo">1297</span>      List&lt;RegionInfo&gt; 
serverRegions = assignments.computeIfAbsent(server, k -&gt; new 
ArrayList&lt;&gt;());<a name="line.1297"></a>
-<span class="sourceLineNo">1298</span>      serverRegions.add(region);<a 
name="line.1298"></a>
-<span class="sourceLineNo">1299</span>      cluster.doAssignRegion(region, 
server);<a name="line.1299"></a>
-<span class="sourceLineNo">1300</span>    }<a name="line.1300"></a>
-<span class="sourceLineNo">1301</span>    return assignments;<a 
name="line.1301"></a>
-<span class="sourceLineNo">1302</span>  }<a name="line.1302"></a>
-<span class="sourceLineNo">1303</span><a name="line.1303"></a>
-<span class="sourceLineNo">1304</span>  protected Cluster 
createCluster(List&lt;ServerName&gt; servers, Collection&lt;RegionInfo&gt; 
regions) {<a name="line.1304"></a>
-<span class="sourceLineNo">1305</span>    // Get the snapshot of the current 
assignments for the regions in question, and then create<a name="line.1305"></a>
-<span class="sourceLineNo">1306</span>    // a cluster out of it. Note that we 
might have replicas already assigned to some servers<a name="line.1306"></a>
-<span class="sourceLineNo">1307</span>    // earlier. So we want to get the 
snapshot to see those assignments, but this will only contain<a 
name="line.1307"></a>
-<span class="sourceLineNo">1308</span>    // replicas of the regions that are 
passed (for performance).<a name="line.1308"></a>
-<span class="sourceLineNo">1309</span>    Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; clusterState = 
getRegionAssignmentsByServer(regions);<a name="line.1309"></a>
-<span class="sourceLineNo">1310</span><a name="line.1310"></a>
-<span class="sourceLineNo">1311</span>    for (ServerName server : servers) 
{<a name="line.1311"></a>
-<span class="sourceLineNo">1312</span>      if 
(!clusterState.containsKey(server)) {<a name="line.1312"></a>
-<span class="sourceLineNo">1313</span>        clusterState.put(server, 
EMPTY_REGION_LIST);<a name="line.1313"></a>
-<span class="sourceLineNo">1314</span>      }<a name="line.1314"></a>
-<span class="sourceLineNo">1315</span>    }<a name="line.1315"></a>
-<span class="sourceLineNo">1316</span>    return new Cluster(regions, 
clusterState, null, this.regionFinder,<a name="line.1316"></a>
-<span class="sourceLineNo">1317</span>        rackManager);<a 
name="line.1317"></a>
-<span class="sourceLineNo">1318</span>  }<a name="line.1318"></a>
-<span class="sourceLineNo">1319</span><a name="line.1319"></a>
-<span class="sourceLineNo">1320</span>  private List&lt;ServerName&gt; 
findIdleServers(List&lt;ServerName&gt; servers) {<a name="line.1320"></a>
-<span class="sourceLineNo">1321</span>    return 
this.services.getServerManager()<a name="line.1321"></a>
-<span class="sourceLineNo">1322</span>            
.getOnlineServersListWithPredicator(servers, IDLE_SERVER_PREDICATOR);<a 
name="line.1322"></a>
-<span class="sourceLineNo">1323</span>  }<a name="line.1323"></a>
-<span class="sourceLineNo">1324</span><a name="line.1324"></a>
-<span class="sourceLineNo">1325</span>  /**<a name="line.1325"></a>
-<span class="sourceLineNo">1326</span>   * Used to assign a single region to a 
random server.<a name="line.1326"></a>
-<span class="sourceLineNo">1327</span>   */<a name="line.1327"></a>
-<span class="sourceLineNo">1328</span>  @Override<a name="line.1328"></a>
-<span class="sourceLineNo">1329</span>  public ServerName 
randomAssignment(RegionInfo regionInfo, List&lt;ServerName&gt; servers)<a 
name="line.1329"></a>
-<span class="sourceLineNo">1330</span>      throws HBaseIOException {<a 
name="line.1330"></a>
-<span class="sourceLineNo">1331</span>    
metricsBalancer.incrMiscInvocations();<a name="line.1331"></a>
-<span class="sourceLineNo">1332</span>    if (servers != null &amp;&amp; 
servers.contains(masterServerName)) {<a name="line.1332"></a>
-<span class="sourceLineNo">1333</span>      if (shouldBeOnMaster(regionInfo)) 
{<a name="line.1333"></a>
-<span class="sourceLineNo">1334</span>        return masterServerName;<a 
name="line.1334"></a>
-<span class="sourceLineNo">1335</span>      }<a name="line.1335"></a>
-<span class="sourceLineNo">1336</span>      if 
(!LoadBalancer.isTablesOnMaster(getConf())) {<a name="line.1336"></a>
-<span class="sourceLineNo">1337</span>        // Guarantee we do not put any 
regions on master<a name="line.1337"></a>
-<span class="sourceLineNo">1338</span>        servers = new 
ArrayList&lt;&gt;(servers);<a name="line.1338"></a>
-<span class="sourceLineNo">1339</span>        
servers.remove(masterServerName);<a name="line.1339"></a>
-<span class="sourceLineNo">1340</span>      }<a name="line.1340"></a>
-<span class="sourceLineNo">1341</span>    }<a name="line.1341"></a>
-<span class="sourceLineNo">1342</span><a name="line.1342"></a>
-<span class="sourceLineNo">1343</span>    int numServers = servers == null ? 0 
: servers.size();<a name="line.1343"></a>
-<span class="sourceLineNo">1344</span>    if (numServers == 0) {<a 
name="line.1344"></a>
-<span class="sourceLineNo">1345</span>      LOG.warn("Wanted to retain 
assignment but no servers to assign to");<a name="line.1345"></a>
-<span class="sourceLineNo">1346</span>      return null;<a 
name="line.1346"></a>
-<span class="sourceLineNo">1347</span>    }<a name="line.1347"></a>
-<span class="sourceLineNo">1348</span>    if (numServers == 1) { // Only one 
server, nothing fancy we can do here<a name="line.1348"></a>
-<span class="sourceLineNo">1349</span>      return servers.get(0);<a 
name="line.1349"></a>
-<span class="sourceLineNo">1350</span>    }<a name="line.1350"></a>
-<span class="sourceLineNo">1351</span>    List&lt;ServerName&gt; idleServers = 
findIdleServers(servers);<a name="line.1351"></a>
-<span class="sourceLineNo">1352</span>    if (idleServers.size() == 1) {<a 
name="line.1352"></a>
-<span class="sourceLineNo">1353</span>      return idleServers.get(0);<a 
name="line.1353"></a>
-<span class="sourceLineNo">1354</span>    }<a name="line.1354"></a>
-<span class="sourceLineNo">1355</span>    final List&lt;ServerName&gt; 
finalServers = idleServers.isEmpty() ?<a name="line.1355"></a>
-<span class="sourceLineNo">1356</span>            servers : idleServers;<a 
name="line.1356"></a>
-<span class="sourceLineNo">1357</span>    List&lt;RegionInfo&gt; regions = 
Lists.newArrayList(regionInfo);<a name="line.1357"></a>
-<span class="sourceLineNo">1358</span>    Cluster cluster = 
createCluster(finalServers, regions);<a name="line.1358"></a>
-<span class="sourceLineNo">1359</span>    return randomAssignment(cluster, 
regionInfo, finalServers);<a name="line.1359"></a>
-<span class="sourceLineNo">1360</span>  }<a name="line.1360"></a>
-<span class="sourceLineNo">1361</span><a name="line.1361"></a>
-<span class="sourceLineNo">1362</span>  /**<a name="line.1362"></a>
-<span class="sourceLineNo">1363</span>   * Generates a bulk assignment startup 
plan, attempting to reuse the existing<a name="line.1363"></a>
-<span class="sourceLineNo">1364</span>   * assignment information from META, 
but adjusting for the specified list of<a name="line.1364"></a>
-<span class="sourceLineNo">1365</span>   * available/online servers available 
for assignment.<a name="line.1365"></a>
-<span class="sourceLineNo">1366</span>   * &lt;p&gt;<a name="line.1366"></a>
-<span class="sourceLineNo">1367</span>   * Takes a map of all regions to their 
existing assignment from META. Also<a name="line.1367"></a>
-<span class="sourceLineNo">1368</span>   * takes a list of online servers for 
regions to be assigned to. Attempts to<a name="line.1368"></a>
-<span class="sourceLineNo">1369</span>   * retain all assignment, so in some 
instances initial assignment will not be<a name="line.1369"></a>
-<span class="sourceLineNo">1370</span>   * completely balanced.<a 
name="line.1370"></a>
-<span class="sourceLineNo">1371</span>   * &lt;p&gt;<a name="line.1371"></a>
-<span class="sourceLineNo">1372</span>   * Any leftover regions without an 
existing server to be assigned to will be<a name="line.1372"></a>
-<span class="sourceLineNo">1373</span>   * assigned randomly to available 
servers.<a name="line.1373"></a>
-<span class="sourceLineNo">1374</span>   *<a name="line.1374"></a>
-<span class="sourceLineNo">1375</span>   * @param regions regions and existing 
assignment from meta<a name="line.1375"></a>
-<span class="sourceLineNo">1376</span>   * @param servers available servers<a 
name="line.1376"></a>
-<span class="sourceLineNo">1377</span>   * @return map of servers and regions 
to be assigned to them<a name="line.1377"></a>
-<span class="sourceLineNo">1378</span>   */<a name="line.1378"></a>
-<span class="sourceLineNo">1379</span>  @Override<a name="line.1379"></a>
-<span class="sourceLineNo">1380</span>  public Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; retainAssignment(Map&lt;RegionInfo, ServerName&gt; 
regions,<a name="line.1380"></a>
-<span class="sourceLineNo">1381</span>      List&lt;ServerName&gt; servers) 
throws HBaseIOException {<a name="line.1381"></a>
-<span class="sourceLineNo">1382</span>    // Update metrics<a 
name="line.1382"></a>
-<span class="sourceLineNo">1383</span>    
metricsBalancer.incrMiscInvocations();<a name="line.1383"></a>
-<span class="sourceLineNo">1384</span>    Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; assignments = 
assignMasterSystemRegions(regions.keySet(), servers);<a name="line.1384"></a>
-<span class="sourceLineNo">1385</span>    if (assignments != null &amp;&amp; 
!assignments.isEmpty()) {<a name="line.1385"></a>
-<span class="sourceLineNo">1386</span>      servers = new 
ArrayList&lt;&gt;(servers);<a name="line.1386"></a>
-<span class="sourceLineNo">1387</span>      // Guarantee not to put other 
regions on master<a name="line.1387"></a>
-<span class="sourceLineNo">1388</span>      
servers.remove(masterServerName);<a name="line.1388"></a>
-<span class="sourceLineNo">1389</span>      List&lt;RegionInfo&gt; 
masterRegions = assignments.get(masterServerName);<a name="line.1389"></a>
-<span class="sourceLineNo">1390</span>      regions = 
regions.entrySet().stream().filter(e -&gt; 
!masterRegions.contains(e.getKey()))<a name="line.1390"></a>
-<span class="sourceLineNo">1391</span>          
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));<a 
name="line.1391"></a>
-<span class="sourceLineNo">1392</span>    }<a name="line.1392"></a>
-<span class="sourceLineNo">1393</span>    if (regions.isEmpty()) {<a 
name="line.1393"></a>
-<span class="sourceLineNo">1394</span>      return assignments;<a 
name="line.1394"></a>
-<span class="sourceLineNo">1395</span>    }<a name="line.1395"></a>
-<span class="sourceLineNo">1396</span><a name="line.1396"></a>
-<span class="sourceLineNo">1397</span>    int numServers = servers == null ? 0 
: servers.size();<a name="line.1397"></a>
-<span class="sourceLineNo">1398</span>    if (numServers == 0) {<a 
name="line.1398"></a>
-<span class="sourceLineNo">1399</span>      LOG.warn("Wanted to do retain 
assignment but no servers to assign to");<a name="line.1399"></a>
-<span class="sourceLineNo">1400</span>      return null;<a 
name="line.1400"></a>
-<span class="sourceLineNo">1401</span>    }<a name="line.1401"></a>
-<span class="sourceLineNo">1402</span>    if (numServers == 1) { // Only one 
server, nothing fancy we can do here<a name="line.1402"></a>
-<span class="sourceLineNo">1403</span>      ServerName server = 
servers.get(0);<a name="line.1403"></a>
-<span class="sourceLineNo">1404</span>      assignments.put(server, new 
ArrayList&lt;&gt;(regions.keySet()));<a name="line.1404"></a>
-<span class="sourceLineNo">1405</span>      return assignments;<a 
name="line.1405"></a>
-<span class="sourceLineNo">1406</span>    }<a name="line.1406"></a>
-<span class="sourceLineNo">1407</span><a name="line.1407"></a>
-<span class="sourceLineNo">1408</span>    // Group all of the old assignments 
by their hostname.<a name="line.1408"></a>
-<span class="sourceLineNo">1409</span>    // We can't group directly by 
ServerName since the servers all have<a name="line.1409"></a>
-<span class="sourceLineNo">1410</span>    // new start-codes.<a 
name="line.1410"></a>
-<span class="sourceLineNo">1411</span><a name="line.1411"></a>
-<span class="sourceLineNo">1412</span>    // Group the servers by their 
hostname. It's possible we have multiple<a name="line.1412"></a>
-<span class="sourceLineNo">1413</span>    // servers on the same host on 
different ports.<a name="line.1413"></a>
-<span class="sourceLineNo">1414</span>    ArrayListMultimap&lt;String, 
ServerName&gt; serversByHostname = ArrayListMultimap.create();<a 
name="line.1414"></a>
-<span class="sourceLineNo">1415</span>    for (ServerName server : servers) 
{<a name="line.1415"></a>
-<span class="sourceLineNo">1416</span>      assignments.put(server, new 
ArrayList&lt;&gt;());<a name="line.1416"></a>
-<span class="sourceLineNo">1417</span>      
serversByHostname.put(server.getHostnameLowerCase(), server);<a 
name="line.1417"></a>
+<span class="sourceLineNo">1281</span>          if 
(!RegionReplicaUtil.isDefaultReplica(region.getReplicaId())) {<a 
name="line.1281"></a>
+<span class="sourceLineNo">1282</span>            // if the region is not a 
default replica<a name="line.1282"></a>
+<span class="sourceLineNo">1283</span>            // check if the assignments 
map has the other replica region on this server<a name="line.1283"></a>
+<span class="sourceLineNo">1284</span>            for (RegionInfo hri : 
serverRegions) {<a name="line.1284"></a>
+<span class="sourceLineNo">1285</span>              if 
(RegionReplicaUtil.isReplicasForSameRegion(region, hri)) {<a 
name="line.1285"></a>
+<span class="sourceLineNo">1286</span>                if 
(LOG.isTraceEnabled()) {<a name="line.1286"></a>
+<span class="sourceLineNo">1287</span>                  LOG.trace("Skipping 
the server, " + serverName<a name="line.1287"></a>
+<span class="sourceLineNo">1288</span>                      + " , got the same 
server for the region " + region);<a name="line.1288"></a>
+<span class="sourceLineNo">1289</span>                }<a name="line.1289"></a>
+<span class="sourceLineNo">1290</span>                // do not allow this 
case. The unassignedRegions we got because the<a name="line.1290"></a>
+<span class="sourceLineNo">1291</span>                // replica region in 
this list was not assigned because of lower availablity issue.<a 
name="line.1291"></a>
+<span class="sourceLineNo">1292</span>                // So when we assign 
here we should ensure that as far as possible the server being<a 
name="line.1292"></a>
+<span class="sourceLineNo">1293</span>                // selected does not 
have the server where the replica region was not assigned.<a 
name="line.1293"></a>
+<span class="sourceLineNo">1294</span>                continue INNER; // 
continue the inner loop, ie go to the next server<a name="line.1294"></a>
+<span class="sourceLineNo">1295</span>              }<a name="line.1295"></a>
+<span class="sourceLineNo">1296</span>            }<a name="line.1296"></a>
+<span class="sourceLineNo">1297</span>          }<a name="line.1297"></a>
+<span class="sourceLineNo">1298</span>          serverRegions.add(region);<a 
name="line.1298"></a>
+<span class="sourceLineNo">1299</span>          cluster.doAssignRegion(region, 
serverName);<a name="line.1299"></a>
+<span class="sourceLineNo">1300</span>          serverIdx = (j + serverIdx + 
1) % numServers; //remain from next server<a name="line.1300"></a>
+<span class="sourceLineNo">1301</span>          assigned = true;<a 
name="line.1301"></a>
+<span class="sourceLineNo">1302</span>          break;<a name="line.1302"></a>
+<span class="sourceLineNo">1303</span>        }<a name="line.1303"></a>
+<span class="sourceLineNo">1304</span>      }<a name="line.1304"></a>
+<span class="sourceLineNo">1305</span>      if (!assigned) {<a 
name="line.1305"></a>
+<span class="sourceLineNo">1306</span>        lastFewRegions.add(region);<a 
name="line.1306"></a>
+<span class="sourceLineNo">1307</span>      }<a name="line.1307"></a>
+<span class="sourceLineNo">1308</span>    }<a name="line.1308"></a>
+<span class="sourceLineNo">1309</span>    // just sprinkle the rest of the 
regions on random regionservers. The balanceCluster will<a name="line.1309"></a>
+<span class="sourceLineNo">1310</span>    // make it optimal later. we can end 
up with this if numReplicas &gt; numServers.<a name="line.1310"></a>
+<span class="sourceLineNo">1311</span>    for (RegionInfo region : 
lastFewRegions) {<a name="line.1311"></a>
+<span class="sourceLineNo">1312</span>      int i = 
RANDOM.nextInt(numServers);<a name="line.1312"></a>
+<span class="sourceLineNo">1313</span>      ServerName server = 
servers.get(i);<a name="line.1313"></a>
+<span class="sourceLineNo">1314</span>      List&lt;RegionInfo&gt; 
serverRegions = assignments.computeIfAbsent(server, k -&gt; new 
ArrayList&lt;&gt;());<a name="line.1314"></a>
+<span class="sourceLineNo">1315</span>      serverRegions.add(region);<a 
name="line.1315"></a>
+<span class="sourceLineNo">1316</span>      cluster.doAssignRegion(region, 
server);<a name="line.1316"></a>
+<span class="sourceLineNo">1317</span>    }<a name="line.1317"></a>
+<span class="sourceLineNo">1318</span>    return assignments;<a 
name="line.1318"></a>
+<span class="sourceLineNo">1319</span>  }<a name="line.1319"></a>
+<span class="sourceLineNo">1320</span><a name="line.1320"></a>
+<span class="sourceLineNo">1321</span>  protected Cluster 
createCluster(List&lt;ServerName&gt; servers, Collection&lt;RegionInfo&gt; 
regions) {<a name="line.1321"></a>
+<span class="sourceLineNo">1322</span>    // Get the snapshot of the current 
assignments for the regions in question, and then create<a name="line.1322"></a>
+<span class="sourceLineNo">1323</span>    // a cluster out of it. Note that we 
might have replicas already assigned to some servers<a name="line.1323"></a>
+<span class="sourceLineNo">1324</span>    // earlier. So we want to get the 
snapshot to see those assignments, but this will only contain<a 
name="line.1324"></a>
+<span class="sourceLineNo">1325</span>    // replicas of the regions that are 
passed (for performance).<a name="line.1325"></a>
+<span class="sourceLineNo">1326</span>    Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; clusterState = 
getRegionAssignmentsByServer(regions);<a name="line.1326"></a>
+<span class="sourceLineNo">1327</span><a name="line.1327"></a>
+<span class="sourceLineNo">1328</span>    for (ServerName server : servers) 
{<a name="line.1328"></a>
+<span class="sourceLineNo">1329</span>      if 
(!clusterState.containsKey(server)) {<a name="line.1329"></a>
+<span class="sourceLineNo">1330</span>        clusterState.put(server, 
EMPTY_REGION_LIST);<a name="line.1330"></a>
+<span class="sourceLineNo">1331</span>      }<a name="line.1331"></a>
+<span class="sourceLineNo">1332</span>    }<a name="line.1332"></a>
+<span class="sourceLineNo">1333</span>    return new Cluster(regions, 
clusterState, null, this.regionFinder,<a name="line.1333"></a>
+<span class="sourceLineNo">1334</span>        rackManager);<a 
name="line.1334"></a>
+<span class="sourceLineNo">1335</span>  }<a name="line.1335"></a>
+<span class="sourceLineNo">1336</span><a name="line.1336"></a>
+<span class="sourceLineNo">1337</span>  private List&lt;ServerName&gt; 
findIdleServers(List&lt;ServerName&gt; servers) {<a name="line.1337"></a>
+<span class="sourceLineNo">1338</span>    return 
this.services.getServerManager()<a name="line.1338"></a>
+<span class="sourceLineNo">1339</span>            
.getOnlineServersListWithPredicator(servers, IDLE_SERVER_PREDICATOR);<a 
name="line.1339"></a>
+<span class="sourceLineNo">1340</span>  }<a name="line.1340"></a>
+<span class="sourceLineNo">1341</span><a name="line.1341"></a>
+<span class="sourceLineNo">1342</span>  /**<a name="line.1342"></a>
+<span class="sourceLineNo">1343</span>   * Used to assign a single region to a 
random server.<a name="line.1343"></a>
+<span class="sourceLineNo">1344</span>   */<a name="line.1344"></a>
+<span class="sourceLineNo">1345</span>  @Override<a name="line.1345"></a>
+<span class="sourceLineNo">1346</span>  public ServerName 
randomAssignment(RegionInfo regionInfo, List&lt;ServerName&gt; servers)<a 
name="line.1346"></a>
+<span class="sourceLineNo">1347</span>      throws HBaseIOException {<a 
name="line.1347"></a>
+<span class="sourceLineNo">1348</span>    
metricsBalancer.incrMiscInvocations();<a name="line.1348"></a>
+<span class="sourceLineNo">1349</span>    if (servers != null &amp;&amp; 
servers.contains(masterServerName)) {<a name="line.1349"></a>
+<span class="sourceLineNo">1350</span>      if (shouldBeOnMaster(regionInfo)) 
{<a name="line.1350"></a>
+<span class="sourceLineNo">1351</span>        return masterServerName;<a 
name="line.1351"></a>
+<span class="sourceLineNo">1352</span>      }<a name="line.1352"></a>
+<span class="sourceLineNo">1353</span>      if 
(!LoadBalancer.isTablesOnMaster(getConf())) {<a name="line.1353"></a>
+<span class="sourceLineNo">1354</span>        // Guarantee we do not put any 
regions on master<a name="line.1354"></a>
+<span class="sourceLineNo">1355</span>        servers = new 
ArrayList&lt;&gt;(servers);<a name="line.1355"></a>
+<span class="sourceLineNo">1356</span>        
servers.remove(masterServerName);<a name="line.1356"></a>
+<span class="sourceLineNo">1357</span>      }<a name="line.1357"></a>
+<span class="sourceLineNo">1358</span>    }<a name="line.1358"></a>
+<span class="sourceLineNo">1359</span><a name="line.1359"></a>
+<span class="sourceLineNo">1360</span>    int numServers = servers == null ? 0 
: servers.size();<a name="line.1360"></a>
+<span class="sourceLineNo">1361</span>    if (numServers == 0) {<a 
name="line.1361"></a>
+<span class="sourceLineNo">1362</span>      LOG.warn("Wanted to retain 
assignment but no servers to assign to");<a name="line.1362"></a>
+<span class="sourceLineNo">1363</span>      return null;<a 
name="line.1363"></a>
+<span class="sourceLineNo">1364</span>    }<a name="line.1364"></a>
+<span class="sourceLineNo">1365</span>    if (numServers == 1) { // Only one 
server, nothing fancy we can do here<a name="line.1365"></a>
+<span class="sourceLineNo">1366</span>      return servers.get(0);<a 
name="line.1366"></a>
+<span class="sourceLineNo">1367</span>    }<a name="line.1367"></a>
+<span class="sourceLineNo">1368</span>    List&lt;ServerName&gt; idleServers = 
findIdleServers(servers);<a name="line.1368"></a>
+<span class="sourceLineNo">1369</span>    if (idleServers.size() == 1) {<a 
name="line.1369"></a>
+<span class="sourceLineNo">1370</span>      return idleServers.get(0);<a 
name="line.1370"></a>
+<span class="sourceLineNo">1371</span>    }<a name="line.1371"></a>
+<span class="sourceLineNo">1372</span>    final List&lt;ServerName&gt; 
finalServers = idleServers.isEmpty() ?<a name="line.1372"></a>
+<span class="sourceLineNo">1373</span>            servers : idleServers;<a 
name="line.1373"></a>
+<span class="sourceLineNo">1374</span>    List&lt;RegionInfo&gt; regions = 
Lists.newArrayList(regionInfo);<a name="line.1374"></a>
+<span class="sourceLineNo">1375</span>    Cluster cluster = 
createCluster(finalServers, regions);<a name="line.1375"></a>
+<span class="sourceLineNo">1376</span>    return randomAssignment(cluster, 
regionInfo, finalServers);<a name="line.1376"></a>
+<span class="sourceLineNo">1377</span>  }<a name="line.1377"></a>
+<span class="sourceLineNo">1378</span><a name="line.1378"></a>
+<span class="sourceLineNo">1379</span>  /**<a name="line.1379"></a>
+<span class="sourceLineNo">1380</span>   * Generates a bulk assignment startup 
plan, attempting to reuse the existing<a name="line.1380"></a>
+<span class="sourceLineNo">1381</span>   * assignment information from META, 
but adjusting for the specified list of<a name="line.1381"></a>
+<span class="sourceLineNo">1382</span>   * available/online servers available 
for assignment.<a name="line.1382"></a>
+<span class="sourceLineNo">1383</span>   * &lt;p&gt;<a name="line.1383"></a>
+<span class="sourceLineNo">1384</span>   * Takes a map of all regions to their 
existing assignment from META. Also<a name="line.1384"></a>
+<span class="sourceLineNo">1385</span>   * takes a list of online servers for 
regions to be assigned to. Attempts to<a name="line.1385"></a>
+<span class="sourceLineNo">1386</span>   * retain all assignment, so in some 
instances initial assignment will not be<a name="line.1386"></a>
+<span class="sourceLineNo">1387</span>   * completely balanced.<a 
name="line.1387"></a>
+<span class="sourceLineNo">1388</span>   * &lt;p&gt;<a name="line.1388"></a>
+<span class="sourceLineNo">1389</span>   * Any leftover regions without an 
existing server to be assigned to will be<a name="line.1389"></a>
+<span class="sourceLineNo">1390</span>   * assigned randomly to available 
servers.<a name="line.1390"></a>
+<span class="sourceLineNo">1391</span>   *<a name="line.1391"></a>
+<span class="sourceLineNo">1392</span>   * @param regions regions and existing 
assignment from meta<a name="line.1392"></a>
+<span class="sourceLineNo">1393</span>   * @param servers available servers<a 
name="line.1393"></a>
+<span class="sourceLineNo">1394</span>   * @return map of servers and regions 
to be assigned to them<a name="line.1394"></a>
+<span class="sourceLineNo">1395</span>   */<a name="line.1395"></a>
+<span class="sourceLineNo">1396</span>  @Override<a name="line.1396"></a>
+<span class="sourceLineNo">1397</span>  public Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; retainAssignment(Map&lt;RegionInfo, ServerName&gt; 
regions,<a name="line.1397"></a>
+<span class="sourceLineNo">1398</span>      List&lt;ServerName&gt; servers) 
throws HBaseIOException {<a name="line.1398"></a>
+<span class="sourceLineNo">1399</span>    // Update metrics<a 
name="line.1399"></a>
+<span class="sourceLineNo">1400</span>    
metricsBalancer.incrMiscInvocations();<a name="line.1400"></a>
+<span class="sourceLineNo">1401</span>    Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; assignments = 
assignMasterSystemRegions(regions.keySet(), servers);<a name="line.1401"></a>
+<span class="sourceLineNo">1402</span>    if (assignments != null &amp;&amp; 
!assignments.isEmpty()) {<a name="line.1402"></a>
+<span class="sourceLineNo">1403</span>      servers = new 
ArrayList&lt;&gt;(servers);<a name="line.1403"></a>
+<span class="sourceLineNo">1404</span>      // Guarantee not to put other 
regions on master<a name="line.1404"></a>
+<span class="sourceLineNo">1405</span>      
servers.remove(masterServerName);<a name="line.1405"></a>
+<span class="sourceLineNo">1406</span>      List&lt;RegionInfo&gt; 
masterRegions = assignments.get(masterServerName);<a name="line.1406"></a>
+<span class="sourceLineNo">1407</span>      regions = 
regions.entrySet().stream().filter(e -&gt; 
!masterRegions.contains(e.getKey()))<a name="line.1407"></a>
+<span class="sourceLineNo">1408</span>          
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));<a 
name="line.1408"></a>
+<span class="sourceLineNo">1409</span>    }<a name="line.1409"></a>
+<span class="sourceLineNo">1410</span>    if (regions.isEmpty()) {<a 
name="line.1410"></a>
+<span class="sourceLineNo">1411</span>      return assignments;<a 
name="line.1411"></a>
+<span class="sourceLineNo">1412</span>    }<a name="line.1412"></a>
+<span class="sourceLineNo">1413</span><a name="line.1413"></a>
+<span class="sourceLineNo">1414</span>    int numServers = servers == null ? 0 
: servers.size();<a name="line.1414"></a>
+<span class="sourceLineNo">1415</span>    if (numServers == 0) {<a 
name="line.1415"></a>
+<span class="sourceLineNo">1416</span>      LOG.warn("Wanted to do retain 
assignment but no servers to assign to");<a name="line.1416"></a>
+<span class="sourceLineNo">1417</span>      return null;<a 
name="line.1417"></a>
 <span class="sourceLineNo">1418</span>    }<a name="line.1418"></a>
-<span class="sourceLineNo">1419</span><a name="line.1419"></a>
-<span class="sourceLineNo">1420</span>    // Collection of the hostnames that 
used to have regions<a name="line.1420"></a>
-<span class="sourceLineNo">1421</span>    // assigned, but for which we no 
longer have any RS running<a name="line.1421"></a>
-<span class="sourceLineNo">1422</span>    // after the cluster restart.<a 
name="line.1422"></a>
-<span class="sourceLineNo">1423</span>    Set&lt;String&gt; 
oldHostsNoLongerPresent = Sets.newTreeSet();<a name="line.1423"></a>
+<span class="sourceLineNo">1419</span>    if (numServers == 1) { // Only one 
server, nothing fancy we can do here<a name="line.1419"></a>
+<span class="sourceLineNo">1420</span>      ServerName server = 
servers.get(0);<a name="line.1420"></a>
+<span class="sourceLineNo">1421</span>      assignments.put(server, new 
ArrayList&lt;&gt;(regions.keySet()));<a name="line.1421"></a>
+<span class="sourceLineNo">1422</span>      return assignments;<a 
name="line.1422"></a>
+<span class="sourceLineNo">1423</span>    }<a name="line.1423"></a>
 <span class="sourceLineNo">1424</span><a name="line.1424"></a>
-<span class="sourceLineNo">1425</span>    // If the old servers aren't 
present, lets assign those regions later.<a name="line.1425"></a>
-<span class="sourceLineNo">1426</span>    List&lt;RegionInfo&gt; 
randomAssignRegions = Lists.newArrayList();<a name="line.1426"></a>
-<span class="sourceLineNo">1427</span><a name="line.1427"></a>
-<span class="sourceLineNo">1428</span>    int numRandomAssignments = 0;<a 
name="line.1428"></a>
-<span class="sourceLineNo">1429</span>    int numRetainedAssigments = 0;<a 
name="line.1429"></a>
-<span class="sourceLineNo">1430</span><a name="line.1430"></a>
-<span class="sourceLineNo">1431</span>    for (Map.Entry&lt;RegionInfo, 
ServerName&gt; entry : regions.entrySet()) {<a name="line.1431"></a>
-<span class="sourceLineNo">1432</span>      RegionInfo region = 
entry.getKey();<a name="line.1432"></a>
-<span class="sourceLineNo">1433</span>      ServerName oldServerName = 
entry.getValue();<a name="line.1433"></a>
-<span class="sourceLineNo">1434</span>      List&lt;ServerName&gt; 
localServers = new ArrayList&lt;&gt;();<a name="line.1434"></a>
-<span class="sourceLineNo">1435</span>      if (oldServerName != null) {<a 
name="line.1435"></a>
-<span class="sourceLineNo">1436</span>        localServers = 
serversByHostname.get(oldServerName.getHostnameLowerCase());<a 
name="line.1436"></a>
-<span class="sourceLineNo">1437</span>      }<a name="line.1437"></a>
-<span class="sourceLineNo">1438</span>      if (localServers.isEmpty()) {<a 
name="line.1438"></a>
-<span class="sourceLineNo">1439</span>        // No servers on the new cluster 
match up with this hostname, assign randomly, later.<a name="line.1439"></a>
-<span class="sourceLineNo">1440</span>        
randomAssignRegions.add(region);<a name="line.1440"></a>
-<span class="sourceLineNo">1441</span>        if (oldServerName != null) {<a 
name="line.1441"></a>
-<span class="sourceLineNo">1442</span>          
oldHostsNoLongerPresent.add(oldServerName.getHostnameLowerCase());<a 
name="line.1442"></a>
-<span class="sourceLineNo">1443</span>        }<a name="line.1443"></a>
-<span class="sourceLineNo">1444</span>      } else if (localServers.size() == 
1) {<a name="line.1444"></a>
-<span class="sourceLineNo">1445</span>        // the usual case - one new 
server on same host<a name="line.1445"></a>
-<span class="sourceLineNo">1446</span>        ServerName target = 
localServers.get(0);<a name="line.1446"></a>
-<span class="sourceLineNo">1447</span>        
assignments.get(target).add(region);<a name="line.1447"></a>
-<span class="sourceLineNo">1448</span>        numRetainedAssigments++;<a 
name="line.1448"></a>
-<span class="sourceLineNo">1449</span>      } else {<a name="line.1449"></a>
-<span class="sourceLineNo">1450</span>        // multiple new servers in the 
cluster on this same host<a name="line.1450"></a>
-<span class="sourceLineNo">1451</span>        if 
(localServers.contains(oldServerName)) {<a name="line.1451"></a>
-<span class="sourceLineNo">1452</span>          
assignments.get(oldServerName).add(region);<a name="line.1452"></a>
-<span class="sourceLineNo">1453</span>          numRetainedAssigments++;<a 
name="line.1453"></a>
-<span class="sourceLineNo">1454</span>        } else {<a name="line.1454"></a>
-<span class="sourceLineNo">1455</span>          ServerName target = null;<a 
name="line.1455"></a>
-<span class="sourceLineNo">1456</span>          for (ServerName tmp : 
localServers) {<a name="line.1456"></a>
-<span class="sourceLineNo">1457</span>            if (tmp.getPort() == 
oldServerName.getPort()) {<a name="line.1457"></a>
-<span class="sourceLineNo">1458</span>              target = tmp;<a 
name="line.1458"></a>
-<span class="sourceLineNo">1459</span>              
assignments.get(tmp).add(region);<a name="line.1459"></a>
-<span class="sourceLineNo">1460</span>              numRetainedAssigments++;<a 
name="line.1460"></a>
-<span class="sourceLineNo">1461</span>              break;<a 
name="line.1461"></a>
-<span class="sourceLineNo">1462</span>            }<a name="line.1462"></a>
-<span class="sourceLineNo">1463</span>          }<a name="line.1463"></a>
-<span class="sourceLineNo">1464</span>          if (target == null) {<a 
name="line.1464"></a>
-<span class="sourceLineNo">1465</span>            
randomAssignRegions.add(region);<a name="line.1465"></a>
-<span class="sourceLineNo">1466</span>          }<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><a name="line.1470"></a>
-<span class="sourceLineNo">1471</span>    // If servers from prior assignment 
aren't present, then lets do randomAssignment on regions.<a 
name="line.1471"></a>
-<span class="sourceLineNo">1472</span>    if (randomAssignRegions.size() &gt; 
0) {<a name="line.1472"></a>
-<span class="sourceLineNo">1473</span>      Cluster cluster = 
createCluster(servers, regions.keySet());<a name="line.1473"></a>
-<span class="sourceLineNo">1474</span>      for (Map.Entry&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; entry : assignments.entrySet()) {<a 
name="line.1474"></a>
-<span class="sourceLineNo">1475</span>        ServerName sn = 
entry.getKey();<a name="line.1475"></a>
-<span class="sourceLineNo">1476</span>        for (RegionInfo region : 
entry.getValue()) {<a name="line.1476"></a>
-<span class="sourceLineNo">1477</span>          cluster.doAssignRegion(region, 
sn);<a name="line.1477"></a>
-<span class="sourceLineNo">1478</span>        }<a name="line.1478"></a>
-<span class="sourceLineNo">1479</span>      }<a name="line.1479"></a>
-<span class="sourceLineNo">1480</span>      for (RegionInfo region : 
randomAssignRegions) {<a name="line.1480"></a>
-<span class="sourceLineNo">1481</span>        ServerName target = 
randomAssignment(cluster, region, servers);<a name="line.1481"></a>
-<span class="sourceLineNo">1482</span>        
assignments.get(target).add(region);<a name="line.1482"></a>
-<span class="sourceLineNo">1483</span>        cluster.doAssignRegion(region, 
target);<a name="line.1483"></a>
-<span class="sourceLineNo">1484</span>        numRandomAssignments++;<a 
name="line.1484"></a>
+<span class="sourceLineNo">1425</span>    // Group all of the old assignments 
by their hostname.<a name="line.1425"></a>
+<span class="sourceLineNo">1426</span>    // We can't group directly by 
ServerName since the servers all have<a name="line.1426"></a>
+<span class="sourceLineNo">1427</span>    // new start-codes.<a 
name="line.1427"></a>
+<span class="sourceLineNo">1428</span><a name="line.1428"></a>
+<span class="sourceLineNo">1429</span>    // Group the servers by their 
hostname. It's possible we have multiple<a name="line.1429"></a>
+<span class="sourceLineNo">1430</span>    // servers on the same host on 
different ports.<a name="line.1430"></a>
+<span class="sourceLineNo">1431</span>    ArrayListMultimap&lt;String, 
ServerName&gt; serversByHostname = ArrayListMultimap.create();<a 
name="line.1431"></a>
+<span class="sourceLineNo">1432</span>    for (ServerName server : servers) 
{<a name="line.1432"></a>
+<span class="sourceLineNo">1433</span>      assignments.put(server, new 
ArrayList&lt;&gt;());<a name="line.1433"></a>
+<span class="sourceLineNo">1434</span>      
serversByHostname.put(server.getHostnameLowerCase(), server);<a 
name="line.1434"></a>
+<span class="sourceLineNo">1435</span>    }<a name="line.1435"></a>
+<span class="sourceLineNo">1436</span><a name="line.1436"></a>
+<span class="sourceLineNo">1437</span>    // Collection of the hostnames that 
used to have regions<a name="line.1437"></a>
+<span class="sourceLineNo">1438</span>    // assigned, but for which we no 
longer have any RS running<a name="line.1438"></a>
+<span class="sourceLineNo">1439</span>    // after the cluster restart.<a 
name="line.1439"></a>
+<span class="sourceLineNo">1440</span>    Set&lt;String&gt; 
oldHostsNoLongerPresent = Sets.newTreeSet();<a name="line.1440"></a>
+<span class="sourceLineNo">1441</span><a name="line.1441"></a>
+<span class="sourceLineNo">1442</span>    // If the old servers aren't 
present, lets assign those regions later.<a name="line.1442"></a>
+<span class="sourceLineNo">1443</span>    List&lt;RegionInfo&gt; 
randomAssignRegions = Lists.newArrayList();<a name="line.1443"></a>
+<span class="sourceLineNo">1444</span><a name="line.1444"></a>
+<span class="sourceLineNo">1445</span>    int numRandomAssignments = 0;<a 
name="line.1445"></a>
+<span class="sourceLineNo">1446</span>    int numRetainedAssigments = 0;<a 
name="line.1446"></a>
+<span class="sourceLineNo">1447</span><a name="line.1447"></a>
+<span class="sourceLineNo">1448</span>    for (Map.Entry&lt;RegionInfo, 
ServerName&gt; entry : regions.entrySet()) {<a name="line.1448"></a>
+<span class="sourceLineNo">1449</span>      RegionInfo region = 
entry.getKey();<a name="line.1449"></a>
+<span class="sourceLineNo">1450</span>      ServerName oldServerName = 
entry.getValue();<a name="line.1450"></a>
+<span class="sourceLineNo">1451</span>      List&lt;ServerName&gt; 
localServers = new ArrayList&lt;&gt;();<a name="line.1451"></a>
+<span class="sourceLineNo">1452</span>      if (oldServerName != null) {<a 
name="line.1452"></a>
+<span class="sourceLineNo">1453</span>        localServers = 
serversByHostname.get(oldServerName.getHostnameLowerCase());<a 
name="line.1453"></a>
+<span class="sourceLineNo">1454</span>      }<a name="line.1454"></a>
+<span class="sourceLineNo">1455</span>      if (localServers.isEmpty()) {<a 
name="line.1455"></a>
+<span class="sourceLineNo">1456</span>        // No servers on the new cluster 
match up with this hostname, assign randomly, later.<a name="line.1456"></a>
+<span class="sourceLineNo">1457</span>        
randomAssignRegions.add(region);<a name="line.1457"></a>
+<span class="sourceLineNo">1458</span>        if (oldServerName != null) {<a 
name="line.1458"></a>
+<span class="sourceLineNo">1459</span>          
oldHostsNoLongerPresent.add(oldServerName.getHostnameLowerCase());<a 
name="line.1459"></a>
+<span class="sourceLineNo">1460</span>        }<a name="line.1460"></a>
+<span class="sourceLineNo">1461</span>      } else if (localServers.size() == 
1) {<a name="line.1461"></a>
+<span class="sourceLineNo">1462</span>        // the usual case - one new 
server on same host<a name="line.1462"></a>
+<span class="sourceLineNo">1463</span>        ServerName target = 
localServers.get(0);<a name="line.1463"></a>
+<span class="sourceLineNo">1464</span>        
assignments.get(target).add(region);<a name="line.1464"></a>
+<span class="sourceLineNo">1465</span>        numRetainedAssigments++;<a 
name="line.1465"></a>
+<span class="sourceLineNo">1466</span>      } else {<a name="line.1466"></a>
+<span class="sourceLineNo">1467</span>        // multiple new servers in the 
cluster on this same host<a name="line.1467"></a>
+<span class="sourceLineNo">1468</span>        if 
(localServers.contains(oldServerName)) {<a name="line.1468"></a>
+<span class="sourceLineNo">1469</span>          
assignments.get(oldServerName).add(region);<a name="line.1469"></a>
+<span class="sourceLineNo">1470</span>          numRetainedAssigments++;<a 
name="line.1470"></a>
+<span class="sourceLineNo">1471</span>        } else {<a name="line.1471"></a>
+<span class="sourceLineNo">1472</span>          ServerName target = null;<a 
name="line.1472"></a>
+<span class="sourceLineNo">1473</span>          for (ServerName tmp : 
localServers) {<a name="line.1473"></a>
+<span class="sourceLineNo">1474</span>            if (tmp.getPort() == 
oldServerName.getPort()) {<a name="line.1474"></a>
+<span class="sourceLineNo">1475</span>              target = tmp;<a 
name="line.1475"></a>
+<span class="sourceLineNo">1476</span>              
assignments.get(tmp).add(region);<a name="line.1476"></a>
+<span class="sourceLineNo">1477</span>              numRetainedAssigments++;<a 
name="line.1477"></a>
+<span class="sourceLineNo">1478</span>              break;<a 
name="line.1478"></a>
+<span class="sourceLineNo">1479</span>            }<a name="line.1479"></a>
+<span class="sourceLineNo">1480</span>          }<a name="line.1480"></a>
+<span class="sourceLineNo">1481</span>          if (target == null) {<a 
name="line.1481"></a>
+<span class="sourceLineNo">1482</span>            
randomAssignRegions.add(region);<a name="line.1482"></a>
+<span class="sourceLineNo">1483</span>          }<a name="line.1483"></a>
+<span class="sourceLineNo">1484</span>        }<a name="line.1484"></a>
 <span class="sourceLineNo">1485</span>      }<a name="line.1485"></a>
 <span class="sourceLineNo">1486</span>    }<a name="line.1486"></a>
 <span class="sourceLineNo">1487</span><a name="line.1487"></a>
-<span class="sourceLineNo">1488</span>    String randomAssignMsg = "";<a 
name="line.1488"></a>
-<span class="sourceLineNo">1489</span>    if (numRandomAssignments &gt; 0) {<a 
name="line.1489"></a>
-<span class="sourceLineNo">1490</span>      randomAssignMsg =<a 
name="line.1490"></a>
-<span class="sourceLineNo">1491</span>          numRandomAssignments + " 
regions were assigned "<a name="line.1491"></a>
-<span class="sourceLineNo">1492</span>              + "to random hosts, since 
the old hosts for these regions are no "<a name="line.1492"></a>
-<span class="sourceLineNo">1493</span>              + "longer present in the 
cluster. These hosts were:\n  "<a name="line.1493"></a>
-<span class="sourceLineNo">1494</span>              + Joiner.on("\n  
").join(oldHostsNoLongerPresent);<a name="line.1494"></a>
-<span class="sourceLineNo">1495</span>    }<a name="line.1495"></a>
-<span class="sourceLineNo">1496</span><a name="line.1496"></a>
-<span class="sourceLineNo">1497</span>    LOG.info("Reassigned " + 
regions.size() + " regions. " + numRetainedAssigments<a name="line.1497"></a>
-<span class="sourceLineNo">1498</span>        + " retained the pre-restart 
assignment. " + randomAssignMsg);<a name="line.1498"></a>
-<span class="sourceLineNo">1499</span>    return assignments;<a 
name="line.1499"></a>
-<span class="sourceLineNo">1500</span>  }<a name="line.1500"></a>
-<span class="sourceLineNo">1501</span><a name="line.1501"></a>
-<span class="sourceLineNo">1502</span>  @Override<a name="line.1502"></a>
-<span class="sourceLineNo">1503</span>  public void initialize() throws 
HBaseIOException{<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>  @Override<a name="line.1506"></a>
-<span class="sourceLineNo">1507</span>  public void regionOnline(RegionInfo 
regionInfo, ServerName sn) {<a name="line.1507"></a>
-<span class="sourceLineNo">1508</span>  }<a name="line.1508"></a>
-<span class="sourceLineNo">1509</span><a name="line.1509"></a>
-<span class="sourceLineNo">1510</span>  @Override<a name="line.1510"></a>
-<span class="sourceLineNo">1511</span>  public void regionOffline(RegionInfo 
regionInfo) {<a name="line.1511"></a>
-<span class="sourceLineNo">1512</span>  }<a name="line.1512"></a>
+<span class="sourceLineNo">1488</span>    // If servers from prior assignment 
aren't present, then lets do randomAssignment on regions.<a 
name="line.1488"></a>
+<span class="sourceLineNo">1489</span>    if (randomAssignRegions.size() &gt; 
0) {<a name="line.1489"></a>
+<span class="sourceLineNo">1490</span>      Cluster cluster = 
createCluster(servers, regions.keySet());<a name="line.1490"></a>
+<span class="sourceLineNo">1491</span>      for (Map.Entry&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; entry : assignments.entrySet()) {<a 
name="line.1491"></a>
+<span class="sourceLineNo">1492</span>        ServerName sn = 
entry.getKey();<a name="line.1492"></a>
+<span class="sourceLineNo">1493</span>        for (RegionInfo region : 
entry.getValue()) {<a name="line.1493"></a>
+<span class="sourceLineNo">1494</span>          cluster.doAssignRegion(region, 
sn);<a name="line.1494"></a>
+<span class="sourceLineNo">1495</span>        }<a name="line.1495"></a>
+<span class="sourceLineNo">1496</span>      }<a name="line.1496"></a>
+<span class="sourceLineNo">1497</span>      for (RegionInfo region : 
randomAssignRegions) {<a name="line.1497"></a>
+<span class="sourceLineNo">1498</span>        ServerName target = 
randomAssignment(cluster, region, servers);<a name="line.1498"></a>
+<span class="sourceLineNo">1499</span>        
assignments.get(target).add(region);<a name="line.1499"></a>
+<span class="sourceLineNo">1500</span>        cluster.doAssignRegion(region, 
target);<a name="line.1500"></a>
+<span class="sourceLineNo">1501</span>        numRandomAssignments++;<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>    String randomAssignMsg = "";<a 
name="line.1505"></a>
+<span class="sourceLineNo">1506</span>    if (numRandomAssignments &gt; 0) {<a 
name="line.1506"></a>
+<span class="sourceLineNo">1507</span>      randomAssignMsg =<a 
name="line.1507"></a>
+<span class="sourceLineNo">1508</span>          numRandomAssignments + " 
regions were assigned "<a name="line.1508"></a>
+<span class="sourceLineNo">1509</span>              + "to random hosts, since 
the old hosts for these regions are no "<a name="line.1509"></a>
+<span class="sourceLineNo">1510</span>              + "longer present in the 
cluster. These hosts were:\n  "<a name="line.1510"></a>
+<span class="sourceLineNo">1511</span>              + Joiner.on("\n  
").join(oldHostsNoLongerPresent);<a name="line.1511"></a>
+<span class="sourceLineNo">1512</span>    }<a name="line.1512"></a>
 <span class="sourceLineNo">1513</span><a name="line.1513"></a>
-<span class="sourceLineNo">1514</span>  @Override<a name="line.1514"></a>
-<span class="sourceLineNo">1515</span>  public boolean isStopped() {<a 
name="line.1515"></a>
-<span class="sourceLineNo">1516</span>    return stopped;<a 
name="line.1516"></a>
+<span class="sourceLineNo">1514</span>    LOG.info("Reassigned " + 
regions.size() + " regions. " + numRetainedAssigments<a name="line.1514"></a>
+<span class="sourceLineNo">1515</span>        + " retained the pre-restart 
assignment. " + randomAssignMsg);<a name="line.1515"></a>
+<span class="sourceLineNo">1516</span>    return assignments;<a 
name="line.1516"></a>
 <span class="sourceLineNo">1517</span>  }<a name="line.1517"></a>
 <span class="sourceLineNo">1518</span><a name="line.1518"></a>
 <span class="sourceLineNo">1519</span>  @Override<a name="line.1519"></a>
-<span class="sourceLineNo">1520</span>  public void stop(String why) {<a 
name="line.1520"></a>
-<span class="sourceLineNo">1521</span>    LOG.info("Load Balancer stop 
requested: "+why);<a name="line.1521"></a>
-<span class="sourceLineNo">1522</span>    stopped = true;<a 
name="line.1522"></a>
-<span class="sourceLineNo">1523</span>  }<a name="line.1523"></a>
-<span class="sourceLineNo">1524</span><a name="line.1524"></a>
-<span class="sourceLineNo">1525</span>  /**<a name="line.1525"></a>
-<span class="sourceLineNo">1526</span>   * Used to assign a single region to a 
random server.<a name="line.1526"></a>
-<span class="sourceLineNo">1527</span>   */<a name="line.1527"></a>
-<span class="sourceLineNo">1528</span>  private ServerName 
randomAssignment(Cluster cluster, RegionInfo regionInfo,<a name="line.1528"></a>
-<span class="sourceLineNo">1529</span>      List&lt;ServerName&gt; servers) 
{<a name="line.1529"></a>
-<span class="sourceLineNo">1530</span>    int numServers = servers.size(); // 
servers is not null, numServers &gt; 1<a name="line.1530"></a>
-<span class="sourceLineNo">1531</span>    ServerName sn = null;<a 
name="line.1531"></a>
-<span class="sourceLineNo">1532</span>    final int maxIterations = numServers 
* 4;<a name="line.1532"></a>
-<span class="sourceLineNo">1533</span>    int iterations = 0;<a 
name="line.1533"></a>
-<span class="sourceLineNo">1534</span><a name="line.1534"></a>
-<span class="sourceLineNo">1535</span>    do {<a name="line.1535"></a>
-<span class="sourceLineNo">1536</span>      int i = 
RANDOM.nextInt(numServers);<a name="line.1536"></a>
-<span class="sourceLineNo">1537</span>      sn = servers.get(i);<a 
name="line.1537"></a>
-<span class="sourceLineNo">1538</span>    } while 
(cluster.wouldLowerAvailability(regionInfo, sn)<a name="line.1538"></a>
-<span class="sourceLineNo">1539</span>        &amp;&amp; iterations++ &lt; 
maxIterations);<a name="line.1539"></a>
-<span class="sourceLineNo">1540</span>    cluster.doAssignRegion(regionInfo, 
sn);<a name="line.1540"></a>
-<span class="sourceLineNo">1541</span>    return sn;<a name="line.1541"></a>
-<span class="sourceLineNo">1542</span>  }<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>   * Round robin a list of regions to a 
list of servers<a name="line.1545"></a>
-<span class="sourceLineNo">1546</span>   */<a name="line.1546"></a>
-<span class="sourceLineNo">1547</span>  private void 
roundRobinAssignment(Cluster cluster, List&lt;RegionInfo&gt; regions,<a 
name="line.1547"></a>
-<span class="sourceLineNo">1548</span>      List&lt;RegionInfo&gt; 
unassignedRegions, List&lt;ServerName&gt; servers,<a name="line.1548"></a>
-<span class="sourceLineNo">1549</span>      Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; assignments) {<a name="line.1549"></a>
-<span class="sourceLineNo">1550</span><a name="line.1550"></a>
-<span class="sourceLineNo">1551</span>    int numServers = servers.size();<a 
name="line.1551"></a>
-<span class="sourceLineNo">1552</span>    int numRegions = regions.size();<a 
name="line.1552"></a>
-<span class="sourceLineNo">1553</span>    int max = (int) Math.ceil((float) 
numRegions / numServers);<a name="line.1553"></a>
-<span class="sourceLineNo">1554</span>    int serverIdx = 0;<a 
name="line.1554"></a>
-<span class="sourceLineNo">1555</span>    if (numServers &gt; 1) {<a 
name="line.1555"></a>
-<span class="sourceLineNo">1556</span>      serverIdx = 
RANDOM.nextInt(numServers);<a name="line.1556"></a>
-<span class="sourceLineNo">1557</span>    }<a name="line.1557"></a>
-<span class="sourceLineNo">1558</span>    int regionIdx = 0;<a 
name="line.1558"></a>
-<span class="sourceLineNo">1559</span><a name="line.1559"></a>
-<span class="sourceLineNo">1560</span>    for (int j = 0; j &lt; numServers; 
j++) {<a name="line.1560"></a>
-<span class="sourceLineNo">1561</span>      ServerName server = servers.get((j 
+ serverIdx) % numServers);<a name="line.1561"></a>
-<span class="sourceLineNo">1562</span>      List&lt;RegionInfo&gt; 
serverRegions = new ArrayList&lt;&gt;(max);<a name="line.1562"></a>
-<span class="sourceLineNo">1563</span>      for (int i = regionIdx; i &lt; 
numRegions; i += numServers) {<a name="line.1563"></a>
-<span class="sourceLineNo">1564</span>        RegionInfo region = 
regions.get(i % numRegions);<a name="line.1564"></a>
-<span class="sourceLineNo">1565</span>        if 
(cluster.wouldLowerAvailability(region, server)) {<a name="line.1565"></a>
-<span class="sourceLineNo">1566</span>          
unassignedRegions.add(region);<a name="line.1566"></a>
-<span class="sourceLineNo">1567</span>        } else {<a name="line.1567"></a>
-<span class="sourceLineNo">1568</span>          serverRegions.add(region);<a 
name="line.1568"></a>
-<span class="sourceLineNo">1569</span>          cluster.doAssignRegion(region, 
server);<a name="line.1569"></a>
-<span class="sourceLineNo">1570</span>        }<a name="line.1570"></a>
-<span class="sourceLineNo">1571</span>      }<a name="line.1571"></a>
-<span class="sourceLineNo">1572</span>      assignments.put(server, 
serverRegions);<a name="line.1572"></a>
-<span class="sourceLineNo">1573</span>      regionIdx++;<a 
name="line.1573"></a>
+<span class="sourceLineNo">1520</span>  public void initialize() throws 
HBaseIOException{<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>  @Override<a name="line.1523"></a>
+<span class="sourceLineNo">1524</span>  public void regionOnline(RegionInfo 
regionInfo, ServerName sn) {<a name="line.1524"></a>
+<span class="sourceLineNo">1525</span>  }<a name="line.1525"></a>
+<span class="sourceLineNo">1526</span><a name="line.1526"></a>
+<span class="sourceLineNo">1527</span>  @Override<a name="line.1527"></a>
+<span class="sourceLineNo">1528</span>  public void regionOffline(RegionInfo 
regionInfo) {<a name="line.1528"></a>
+<span class="sourceLineNo">1529</span>  }<a name="line.1529"></a>
+<span class="sourceLineNo">1530</span><a name="line.1530"></a>
+<span class="sourceLineNo">1531</span>  @Override<a name="line.1531"></a>
+<span class="sourceLineNo">1532</span>  public boolean isStopped() {<a 
name="line.1532"></a>
+<span class="sourceLineNo">1533</span>    return stopped;<a 
name="line.1533"></a>
+<span class="sourceLineNo">1534</span>  }<a name="line.1534"></a>
+<span class="sourceLineNo">1535</span><a name="line.1535"></a>
+<span class="sourceLineNo">1536</span>  @Override<a name="line.1536"></a>
+<span class="sourceLineNo">1537</span>  public void stop(String why) {<a 
name="line.1537"></a>
+<span class="sourceLineNo">1538</span>    LOG.info("Load Balancer stop 
requested: "+why);<a name="line.1538"></a>
+<span class="sourceLineNo">1539</span>    stopped = true;<a 
name="line.1539"></a>
+<span class="sourceLineNo">1540</span>  }<a name="line.1540"></a>
+<span class="sourceLineNo">1541</span><a name="line.1541"></a>
+<span class="sourceLineNo">1542</span>  /**<a name="line.1542"></a>
+<span class="sourceLineNo">1543</span>   * Used to assign a single region to a 
random server.<a name="line.1543"></a>
+<span class="sourceLineNo">1544</span>   */<a name="line.1544"></a>
+<span class="sourceLineNo">1545</span>  private ServerName 
randomAssignment(Cluster cluster, RegionInfo regionInfo,<a name="line.1545"></a>
+<span class="sourceLineNo">1546</span>      List&lt;ServerName&gt; servers) 
{<a name="line.1546"></a>
+<span class="sourceLineNo">1547</span>    int numServers = servers.size(); // 
servers is not null, numServers &gt; 1<a name="line.1547"></a>
+<span class="sourceLineNo">1548</span>    ServerName sn = null;<a 
name="line.1548"></a>
+<span class="sourceLineNo">1549</span>    final int maxIterations = numServers 
* 4;<a name="line.1549"></a>
+<span class="sourceLineNo">1550</span>    int iterations = 0;<a 
name="line.1550"></a>
+<span class="sourceLineNo">1551</span><a name="line.1551"></a>
+<span class="sourceLineNo">1552</span>    do {<a name="line.1552"></a>
+<span class="sourceLineNo">1553</span>      int i = 
RANDOM.nextInt(numServers);<a name="line.1553"></a>
+<span class="sourceLineNo">1554</span>      sn = servers.get(i);<a 
name="line.1554"></a>
+<span class="sourceLineNo">1555</span>    } while 
(cluster.wouldLowerAvailability(regionInfo, sn)<a name="line.1555"></a>
+<span class="sourceLineNo">1556</span>        &amp;&amp; iterations++ &lt; 
maxIterations);<a name="line.1556"></a>
+<span class="sourceLineNo">1557</span>    cluster.doAssignRegion(regionInfo, 
sn);<a name="line.1557"></a>
+<span class="sourceLineNo">1558</span>    return sn;<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>  /**<a name="line.1561"></a>
+<span class="sourceLineNo">1562</span>   * Round robin a list of regions to a 
list of servers<a name="line.1562"></a>
+<span class="sourceLineNo">1563</span>   */<a name="line.1563"></a>
+<span class="sourceLineNo">1564</span>  private void 
roundRobinAssignment(Cluster cluster, List&lt;RegionInfo&gt; regions,<a 
name="line.1564"></a>
+<span class="sourceLineNo">1565</span>      List&lt;RegionInfo&gt; 
unassignedRegions, List&lt;ServerName&gt; servers,<a name="line.1565"></a>
+<span class="sourceLineNo">1566</span>      Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; assignments) {<a name="line.1566"></a>
+<span class="sourceLineNo">1567</span><a name="line.1567"></a>
+<span class="sourceLineNo">1568</span>    int numServers = servers.size();<a 
name="line.1568"></a>
+<span class="sourceLineNo">1569</span>    int numRegions = regions.size();<a 
name="line.1569"></a>
+<span class="sourceLineNo">1570</span>    int max = (int) Math.ceil((float) 
numRegions / numServers);<a name="line.1570"></a>
+<span class="sourceLineNo">1571</span>    int serverIdx = 0;<a 
name="line.1571"></a>
+<span class="sourceLineNo">1572</span>    if (numServers &gt; 1) {<a 
name="line.1572"></a>
+<span class="sourceLineNo">1573</span>      serverIdx = 
RANDOM.nextInt(numServers);<a name="line.1573"></a>
 <span class="sourceLineNo">1574</span>    }<a name="line.1574"></a>
-<span class="sourceLineNo">1575</span>  }<a name="line.1575"></a>
+<span class="sourceLineNo">1575</span>    int regionIdx = 0;<a 
name="line.1575"></a>
 <span class="sourceLineNo">1576</span><a name="line.1576"></a>
-<span class="sourceLineNo">1577</span>  protected Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; getRegionAssignmentsByServer(<a name="line.1577"></a>
-<span class="sourceLineNo">1578</span>    Collection&lt;RegionInfo&gt; 
regions) {<a name="line.1578"></a>
-<span class="sourceLineNo">1579</span>    if (this.services != null &amp;&amp; 
this.services.getAssignmentManager() != null) {<a name="line.1579"></a>
-<span class="sourceLineNo">1580</span>      return 
this.services.getAssignmentManager().getSnapShotOfAssignment(regions);<a 
name="line.1580"></a>
-<span class="sourceLineNo">1581</span>    } else {<a name="line.1581"></a>
-<span class="sourceLineNo">1582</span>      return new HashMap&lt;&gt;();<a 
name="line.1582"></a>
-<span class="sourceLineNo">1583</span>    }<a name="line.1583"></a>
-<span class="sourceLineNo">1584</span>  }<a name="line.1584"></a>
-<span class="sourceLineNo">1585</span><a name="line.1585"></a>
-<span class="sourceLineNo">1586</span>  @Override<a name="line.1586"></a>
-<span class="sourceLineNo">1587</span>  public void 
onConfigurationChange(Configuration conf) {<a name="line.1587"></a>
-<span class="sourceLineNo">1588</span>  }<a name="line.1588"></a>
-<span class="sourceLineNo">1589</span>}<a name="line.1589"></a>
+<span class="sourceLineNo">1577</span>    for (int j = 0; j &lt; numServers; 
j++) {<a name="line.1577"></a>
+<span class="sourceLineNo">1578</span>      ServerName server = servers.get((j 
+ serverIdx) % numServers);<a name="line.1578"></a>
+<span class="sourceLineNo">1579</span>      List&lt;RegionInfo&gt; 
serverRegions = new ArrayList&lt;&gt;(max);<a name="line.1579"></a>
+<span class="sourceLineNo">1580</span>      for (int i = regionIdx; i &lt; 
numRegions; i += numServers) {<a name="line.1580"></a>
+<span class="sourceLineNo">1581</span>        RegionInfo region = 
regions.get(i % numRegions);<a name="line.1581"></a>
+<span class="sourceLineNo">1582</span>        if 
(cluster.wouldLowerAvailability(region, server)) {<a name="line.1582"></a>
+<span class="sourceLineNo">1583</span>          
unassignedRegions.add(region);<a name="line.1583"></a>
+<span class="sourceLineNo">1584</span>        } else {<a name="line.1584"></a>
+<span class="sourceLineNo">1585</span>          serverRegions.add(region);<a 
name="line.1585"></a>
+<span class="sourceLineNo">1586</span>          cluster.doAssignRegion(region, 
server);<a name="line.1586"></a>
+<span class="sourceLineNo">1587</span>        }<a name="line.1587"></a>
+<span class="sourceLineNo">1588</span>      }<a name="line.1588"></a>
+<span class="sourceLineNo">1589</span>      assignments.put(server, 
serverRegions);<a name="line.1589"></a>
+<span class="sourceLineNo">1590</span>      regionIdx++;<a 
name="line.1590"></a>
+<span class="sourceLineNo">1591</span>    }<a name="line.1591"></a>
+<span class="sourceLineNo">1592</span>  }<a name="line.1592"></a>
+<span class="sourceLineNo">1593</span><a name="line.1593"></a>
+<span class="sourceLineNo">1594</span>  protected Map&lt;ServerName, 
List&lt;RegionInfo&gt;&gt; getRegionAssignmentsByServer(<a name="line.1594"></a>
+<span class="sourceLineNo">1595</span>    Collection&lt;RegionInfo&gt; 
regions) {<a name="line.1595"></a>
+<span class="sourceLineNo">1596</span>    if (this.services != null &amp;&amp; 
this.services.getAssignmentManager() != null) {<a name="line.1596"></a>
+<span class="sourceLineNo">1597</span>      return 
this.services.getAssignmentManager().getSnapShotOfAssignment(regions);<a 
name="line.1597"></a>
+<span class="sourceLineNo">1598</span>    } else {<a name="line.1598"></a>
+<span class="sourceLineNo">1599</span>      return new HashMap&lt;&gt;();<a 
name="line.1599"></a>
+<span class="sourceLineNo">1600</span>    }<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>  @Override<a name="line.1603"></a>
+<span class="sourceLineNo">1604</span>  public void 
onConfigurationChange(Configuration conf) {<a name="line.1604"></a>
+<span class="sourceLineNo">1605</span>  }<a name="line.1605"></a>
+<span class="sourceLineNo">1606</span>}<a name="line.1606"></a>
 
 
 

Reply via email to