@Test public void testRetainAssignment() throws Exception { List<HServerInfo> servers = randomServers(10, 10); List<HRegionInfo> regions = randomRegions(100); Map<HRegionInfo, HServerAddress> existing = new TreeMap<HRegionInfo, HServerAddress>(); for (int i=0;i<regions.size();i++) { existing.put(regions.get(i), servers.get(i % servers.size()).getServerAddress()); } Map<HServerInfo, List<HRegionInfo>> assignment = LoadBalancer.retainAssignment(existing, servers); assertRetainedAssignment(existing, servers, assignment); List<HServerInfo> servers2 = new ArrayList<HServerInfo>(servers); servers2.add(randomServer(10)); servers2.add(randomServer(10)); assignment = LoadBalancer.retainAssignment(existing, servers2); assertRetainedAssignment(existing, servers2, assignment); List<HServerInfo> servers3 = new ArrayList<HServerInfo>(servers); servers3.remove(servers3.size()-1); servers3.remove(servers3.size()-2); assignment = LoadBalancer.retainAssignment(existing, servers3); assertRetainedAssignment(existing, servers3, assignment); }
public static Map<HServerInfo, List<HRegionInfo>> retainAssignment( Map<HRegionInfo, HServerAddress> regions, List<HServerInfo> servers) { Map<HServerInfo, List<HRegionInfo>> assignments = new TreeMap<HServerInfo, List<HRegionInfo>>(); Map<HServerAddress, HServerInfo> serverMap = new TreeMap<HServerAddress, HServerInfo>(); for (HServerInfo server : servers) { serverMap.put(server.getServerAddress(), server); assignments.put(server, new ArrayList<HRegionInfo>()); } for (Map.Entry<HRegionInfo, HServerAddress> region : regions.entrySet()) { HServerAddress hsa = region.getValue(); HServerInfo server = hsa == null? null: serverMap.get(hsa); if (server != null) { assignments.get(server).add(region.getKey()); } else { assignments.get(servers.get(RANDOM.nextInt(assignments.size()))).add( region.getKey()); } } return assignments; }
LoadBalancer { public static Map<HServerInfo, List<HRegionInfo>> retainAssignment( Map<HRegionInfo, HServerAddress> regions, List<HServerInfo> servers) { Map<HServerInfo, List<HRegionInfo>> assignments = new TreeMap<HServerInfo, List<HRegionInfo>>(); Map<HServerAddress, HServerInfo> serverMap = new TreeMap<HServerAddress, HServerInfo>(); for (HServerInfo server : servers) { serverMap.put(server.getServerAddress(), server); assignments.put(server, new ArrayList<HRegionInfo>()); } for (Map.Entry<HRegionInfo, HServerAddress> region : regions.entrySet()) { HServerAddress hsa = region.getValue(); HServerInfo server = hsa == null? null: serverMap.get(hsa); if (server != null) { assignments.get(server).add(region.getKey()); } else { assignments.get(servers.get(RANDOM.nextInt(assignments.size()))).add( region.getKey()); } } return assignments; } }
LoadBalancer { public static Map<HServerInfo, List<HRegionInfo>> retainAssignment( Map<HRegionInfo, HServerAddress> regions, List<HServerInfo> servers) { Map<HServerInfo, List<HRegionInfo>> assignments = new TreeMap<HServerInfo, List<HRegionInfo>>(); Map<HServerAddress, HServerInfo> serverMap = new TreeMap<HServerAddress, HServerInfo>(); for (HServerInfo server : servers) { serverMap.put(server.getServerAddress(), server); assignments.put(server, new ArrayList<HRegionInfo>()); } for (Map.Entry<HRegionInfo, HServerAddress> region : regions.entrySet()) { HServerAddress hsa = region.getValue(); HServerInfo server = hsa == null? null: serverMap.get(hsa); if (server != null) { assignments.get(server).add(region.getKey()); } else { assignments.get(servers.get(RANDOM.nextInt(assignments.size()))).add( region.getKey()); } } return assignments; } LoadBalancer(Configuration conf); }
LoadBalancer { public static Map<HServerInfo, List<HRegionInfo>> retainAssignment( Map<HRegionInfo, HServerAddress> regions, List<HServerInfo> servers) { Map<HServerInfo, List<HRegionInfo>> assignments = new TreeMap<HServerInfo, List<HRegionInfo>>(); Map<HServerAddress, HServerInfo> serverMap = new TreeMap<HServerAddress, HServerInfo>(); for (HServerInfo server : servers) { serverMap.put(server.getServerAddress(), server); assignments.put(server, new ArrayList<HRegionInfo>()); } for (Map.Entry<HRegionInfo, HServerAddress> region : regions.entrySet()) { HServerAddress hsa = region.getValue(); HServerInfo server = hsa == null? null: serverMap.get(hsa); if (server != null) { assignments.get(server).add(region.getKey()); } else { assignments.get(servers.get(RANDOM.nextInt(assignments.size()))).add( region.getKey()); } } return assignments; } LoadBalancer(Configuration conf); List<RegionPlan> balanceCluster(
      Map<HServerInfo,List<HRegionInfo>> clusterState); static Map<HServerInfo,List<HRegionInfo>> roundRobinAssignment(
      List<HRegionInfo> regions, List<HServerInfo> servers); static Map<HServerInfo, List<HRegionInfo>> retainAssignment(
      Map<HRegionInfo, HServerAddress> regions, List<HServerInfo> servers); static Map<HRegionInfo,HServerInfo> immediateAssignment(
      List<HRegionInfo> regions, List<HServerInfo> servers); static HServerInfo randomAssignment(List<HServerInfo> servers); }
LoadBalancer { public static Map<HServerInfo, List<HRegionInfo>> retainAssignment( Map<HRegionInfo, HServerAddress> regions, List<HServerInfo> servers) { Map<HServerInfo, List<HRegionInfo>> assignments = new TreeMap<HServerInfo, List<HRegionInfo>>(); Map<HServerAddress, HServerInfo> serverMap = new TreeMap<HServerAddress, HServerInfo>(); for (HServerInfo server : servers) { serverMap.put(server.getServerAddress(), server); assignments.put(server, new ArrayList<HRegionInfo>()); } for (Map.Entry<HRegionInfo, HServerAddress> region : regions.entrySet()) { HServerAddress hsa = region.getValue(); HServerInfo server = hsa == null? null: serverMap.get(hsa); if (server != null) { assignments.get(server).add(region.getKey()); } else { assignments.get(servers.get(RANDOM.nextInt(assignments.size()))).add( region.getKey()); } } return assignments; } LoadBalancer(Configuration conf); List<RegionPlan> balanceCluster(
      Map<HServerInfo,List<HRegionInfo>> clusterState); static Map<HServerInfo,List<HRegionInfo>> roundRobinAssignment(
      List<HRegionInfo> regions, List<HServerInfo> servers); static Map<HServerInfo, List<HRegionInfo>> retainAssignment(
      Map<HRegionInfo, HServerAddress> regions, List<HServerInfo> servers); static Map<HRegionInfo,HServerInfo> immediateAssignment(
      List<HRegionInfo> regions, List<HServerInfo> servers); static HServerInfo randomAssignment(List<HServerInfo> servers);  }