diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java index 87bfb20ff82..00b1ddb2f64 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java @@ -1797,7 +1797,8 @@ public class HMaster extends HRegionServer implements MasterServices { boolean isByTable = getConfiguration().getBoolean("hbase.master.loadbalance.bytable", false); Map>> assignments = this.assignmentManager.getRegionStates() - .getAssignmentsForBalancer(tableStateManager, isByTable); + .getAssignmentsForBalancer(tableStateManager, this.serverManager.getOnlineServersList(), + isByTable); for (Map> serverMap : assignments.values()) { serverMap.keySet().removeAll(this.serverManager.getDrainingServersList()); } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/RegionStates.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/RegionStates.java index ae0ca9afe6d..3eba00d687a 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/RegionStates.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/RegionStates.java @@ -539,7 +539,7 @@ public class RegionStates { * @return A clone of current assignments. */ public Map>> getAssignmentsForBalancer( - TableStateManager tableStateManager, boolean isByTable) { + TableStateManager tableStateManager, List onlineServers, boolean isByTable) { final Map>> result = new HashMap<>(); if (isByTable) { for (RegionStateNode node : regionsMap.values()) { @@ -559,16 +559,21 @@ public class RegionStates { } // Add online servers with no assignment for the table. for (Map> table : result.values()) { - for (ServerName serverName : serverMap.keySet()) { + for (ServerName serverName : onlineServers) { table.computeIfAbsent(serverName, key -> new ArrayList<>()); } } } else { final HashMap> ensemble = new HashMap<>(serverMap.size()); - for (ServerStateNode serverNode : serverMap.values()) { - ensemble.put(serverNode.getServerName(), serverNode.getRegionInfoList().stream() - .filter(region -> !isTableDisabled(tableStateManager, region.getTable())) - .collect(Collectors.toList())); + for (ServerName serverName : onlineServers) { + ServerStateNode serverNode = serverMap.get(serverName); + if (serverNode != null) { + ensemble.put(serverNode.getServerName(), serverNode.getRegionInfoList().stream() + .filter(region -> !isTableDisabled(tableStateManager, region.getTable())) + .collect(Collectors.toList())); + } else { + ensemble.put(serverName, new ArrayList<>()); + } } // Use a fake table name to represent the whole cluster's assignments result.put(HConstants.ENSEMBLE_TABLE_NAME, ensemble); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancerWithDisabledTable.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancer.java similarity index 82% rename from hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancerWithDisabledTable.java rename to hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancer.java index 190b54a84be..7d656fe2f1b 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancerWithDisabledTable.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancer.java @@ -32,6 +32,7 @@ import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.RegionInfo; import org.apache.hadoop.hbase.master.assignment.AssignmentManager; +import org.apache.hadoop.hbase.master.assignment.RegionStates; import org.apache.hadoop.hbase.testclassification.LargeTests; import org.apache.hadoop.hbase.testclassification.MasterTests; import org.junit.After; @@ -46,11 +47,11 @@ import org.junit.rules.TestName; * Test balancer with disabled table */ @Category({ MasterTests.class, LargeTests.class }) -public class TestBalancerWithDisabledTable { +public class TestBalancer { @ClassRule public static final HBaseClassTestRule CLASS_RULE = - HBaseClassTestRule.forClass(TestBalancerWithDisabledTable.class); + HBaseClassTestRule.forClass(TestBalancer.class); private final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); @@ -78,14 +79,21 @@ public class TestBalancerWithDisabledTable { HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster(); AssignmentManager assignmentManager = master.getAssignmentManager(); + RegionStates regionStates = assignmentManager.getRegionStates(); + ServerName sn1 = ServerName.parseServerName("asf903.gq1.ygridcore.net,52690,1517835491385"); + regionStates.getOrCreateServer(sn1); + TableStateManager tableStateManager = master.getTableStateManager(); + ServerManager serverManager = master.getServerManager(); Map>> assignments = - assignmentManager.getRegionStates().getAssignmentsForBalancer(tableStateManager, true); + assignmentManager.getRegionStates() + .getAssignmentsForBalancer(tableStateManager, serverManager.getOnlineServersList(), true); assertFalse(assignments.containsKey(disableTableName)); assertTrue(assignments.containsKey(tableName)); + assertFalse(assignments.get(tableName).containsKey(sn1)); - assignments = - assignmentManager.getRegionStates().getAssignmentsForBalancer(tableStateManager, false); + assignments = assignmentManager.getRegionStates() + .getAssignmentsForBalancer(tableStateManager, serverManager.getOnlineServersList(), false); Map>> tableNameMap = new HashMap<>(); for (Map.Entry> entry : assignments .get(HConstants.ENSEMBLE_TABLE_NAME).entrySet()) { @@ -100,5 +108,6 @@ public class TestBalancerWithDisabledTable { } assertFalse(tableNameMap.containsKey(disableTableName)); assertTrue(tableNameMap.containsKey(tableName)); + assertFalse(tableNameMap.get(tableName).containsKey(sn1)); } }