From e719d4d3c3136a0a0f9a938de2b06cf7fab5198e Mon Sep 17 00:00:00 2001 From: Xiang LI Date: Tue, 6 Mar 2018 18:24:44 +0800 Subject: [PATCH] HBASE-20133 Calculate correct assignment and build region movement plans for mis-placed regions in one pass --- .../hbase/rsgroup/RSGroupBasedLoadBalancer.java | 51 ++++++++++------------ 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupBasedLoadBalancer.java b/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupBasedLoadBalancer.java index 392cbab5ce..854440f24b 100644 --- a/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupBasedLoadBalancer.java +++ b/hbase-rsgroup/src/main/java/org/apache/hadoop/hbase/rsgroup/RSGroupBasedLoadBalancer.java @@ -118,14 +118,16 @@ public class RSGroupBasedLoadBalancer implements RSGroupableBalancer { " is not online, unable to perform balance"); } - Map> correctedState = correctAssignments(clusterState); - List regionPlans = new ArrayList<>(); + List regionPlans = new ArrayList<>(); // to return - List misplacedRegions = correctedState.get(LoadBalancer.BOGUS_SERVER_NAME); - for (RegionInfo regionInfo : misplacedRegions) { - ServerName serverName = findServerForRegion(clusterState, regionInfo); - regionPlans.add(new RegionPlan(regionInfo, serverName, null)); - } + // Calculate correct assignment + // and add RegionPlan for mis-placed regions into the movement list + Map> correctedState = + correctAssignments(clusterState, regionPlans); + + // Add RegionPlan + // for the regions which have been placed according to the region server group assignment + // into the movement list try { List rsgi = rsGroupInfoManager.listRSGroups(); for (RSGroupInfo info: rsgi) { @@ -150,6 +152,8 @@ public class RSGroupBasedLoadBalancer implements RSGroupableBalancer { LOG.warn("Exception while balancing cluster.", exp); regionPlans.clear(); } + + // Return the whole movement list return regionPlans; } @@ -334,40 +338,29 @@ public class RSGroupBasedLoadBalancer implements RSGroupableBalancer { return misplacedRegions; } - private ServerName findServerForRegion( - Map> existingAssignments, RegionInfo region) { - for (Map.Entry> entry : existingAssignments.entrySet()) { - if (entry.getValue().contains(region)) { - return entry.getKey(); - } - } - - throw new IllegalStateException("Could not find server for region " - + region.getShortNameToLog()); - } - private Map> correctAssignments( - Map> existingAssignments) + Map> existingAssignments, List regionPlans) throws HBaseIOException{ Map> correctAssignments = new TreeMap<>(); - correctAssignments.put(LoadBalancer.BOGUS_SERVER_NAME, new LinkedList<>()); + for (Map.Entry> assignments : existingAssignments.entrySet()){ - ServerName sName = assignments.getKey(); - correctAssignments.put(sName, new LinkedList<>()); + ServerName currentHostServer = assignments.getKey(); + correctAssignments.put(currentHostServer, new LinkedList<>()); List regions = assignments.getValue(); for (RegionInfo region : regions) { - RSGroupInfo info = null; + RSGroupInfo targetRSGInfo = null; try { - info = rsGroupInfoManager.getRSGroup( + targetRSGInfo = rsGroupInfoManager.getRSGroup( rsGroupInfoManager.getRSGroupOfTable(region.getTable())); } catch (IOException exp) { LOG.debug("RSGroup information null for region of table " + region.getTable(), exp); } - if ((info == null) || (!info.containsServer(sName.getAddress()))) { - correctAssignments.get(LoadBalancer.BOGUS_SERVER_NAME).add(region); - } else { - correctAssignments.get(sName).add(region); + if (targetRSGInfo == null || + !targetRSGInfo.containsServer(currentHostServer.getAddress())) { // region is mis-placed + regionPlans.add(new RegionPlan(region, currentHostServer, null)); + } else { // region is placed as expected + correctAssignments.get(currentHostServer).add(region); } } } -- 2.14.1