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 690ec59b322..c12db247473 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 @@ -1713,21 +1713,22 @@ public class HMaster extends HRegionServer implements MasterServices { } boolean isByTable = getConfiguration().getBoolean("hbase.master.loadbalance.bytable", false); - Map>> assignmentsByTable = - this.assignmentManager.getRegionStates().getAssignmentsByTable(!isByTable); - - List plans = new ArrayList<>(); + Map>> assignments = + this.assignmentManager.getRegionStates().getAssignmentsForBalancer(isByTable); + for (Map> serverMap : assignments.values()) { + serverMap.keySet().removeAll(this.serverManager.getDrainingServersList()); + } //Give the balancer the current cluster state. this.balancer.setClusterMetrics(getClusterMetricsWithoutCoprocessor()); - this.balancer.setClusterLoad(assignmentsByTable); + this.balancer.setClusterLoad(assignments); - for (Map> serverMap : assignmentsByTable.values()) { - serverMap.keySet().removeAll(this.serverManager.getDrainingServersList()); - } - for (Entry>> e : assignmentsByTable.entrySet()) { + List plans = new ArrayList<>(); + for (Entry>> e : assignments.entrySet()) { List partialPlans = this.balancer.balanceCluster(e.getKey(), e.getValue()); - if (partialPlans != null) plans.addAll(partialPlans); + if (partialPlans != null) { + plans.addAll(partialPlans); + } } long balanceStartTime = System.currentTimeMillis(); 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 1470a5a8532..4728d1f4a64 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 @@ -528,55 +528,40 @@ public class RegionStates { * Can't let out original since it can change and at least the load balancer * wants to iterate this exported list. We need to synchronize on regions * since all access to this.servers is under a lock on this.regions. - * @param forceByCluster a flag to force to aggregate the server-load to the cluster level - * @return A clone of current assignments by table. + * + * @param isByTable If true, return the assignments by table. If false, + * return the assignments which aggregate the server-load to the cluster level. + * @return A clone of current assignments. */ - public Map>> getAssignmentsByTable( - final boolean forceByCluster) { - if (!forceByCluster) return getAssignmentsByTable(); - - final HashMap> ensemble = - new HashMap>(serverMap.size()); - for (ServerStateNode serverNode: serverMap.values()) { - ensemble.put(serverNode.getServerName(), serverNode.getRegionInfoList()); - } - - // TODO: can we use Collections.singletonMap(HConstants.ENSEMBLE_TABLE_NAME, ensemble)? - final Map>> result = - new HashMap>>(1); - result.put(HConstants.ENSEMBLE_TABLE_NAME, ensemble); - return result; - } - - public Map>> getAssignmentsByTable() { + public Map>> getAssignmentsForBalancer( + boolean isByTable) { final Map>> result = new HashMap<>(); - for (RegionStateNode node: regionsMap.values()) { - Map> tableResult = result.get(node.getTable()); - if (tableResult == null) { - tableResult = new HashMap>(); - result.put(node.getTable(), tableResult); - } - - final ServerName serverName = node.getRegionLocation(); - if (serverName == null) { - LOG.info("Skipping, no server for " + node); - continue; - } - List serverResult = tableResult.get(serverName); - if (serverResult == null) { - serverResult = new ArrayList(); - tableResult.put(serverName, serverResult); - } - - serverResult.add(node.getRegionInfo()); - } - // Add online servers with no assignment for the table. - for (Map> table: result.values()) { - for (ServerName svr : serverMap.keySet()) { - if (!table.containsKey(svr)) { - table.put(svr, new ArrayList()); - } + if (isByTable) { + for (RegionStateNode node : regionsMap.values()) { + Map> tableResult = + result.computeIfAbsent(node.getTable(), t -> new HashMap<>()); + final ServerName serverName = node.getRegionLocation(); + if (serverName == null) { + LOG.info("Skipping, no server for " + node); + continue; } + List serverResult = + tableResult.computeIfAbsent(serverName, s -> new ArrayList<>()); + serverResult.add(node.getRegionInfo()); + } + // Add online servers with no assignment for the table. + for (Map> table : result.values()) { + for (ServerName serverName : serverMap.keySet()) { + table.putIfAbsent(serverName, new ArrayList<>()); + } + } + } else { + final HashMap> ensemble = new HashMap<>(serverMap.size()); + for (ServerStateNode serverNode : serverMap.values()) { + ensemble.put(serverNode.getServerName(), serverNode.getRegionInfoList()); + } + // Use a fake table name to represent the whole cluster's assignments + result.put(HConstants.ENSEMBLE_TABLE_NAME, ensemble); } return result; }