@Test public void testBulkAssignment() throws Exception { for(int [] mock : regionsAndServersMocks) { LOG.debug("testBulkAssignment with " + mock[0] + " regions and " + mock[1] + " servers"); List<HRegionInfo> regions = randomRegions(mock[0]); List<HServerInfo> servers = randomServers(mock[1], 0); Map<HServerInfo,List<HRegionInfo>> assignments = LoadBalancer.roundRobinAssignment(regions, servers); float average = (float)regions.size()/servers.size(); int min = (int)Math.floor(average); int max = (int)Math.ceil(average); if(assignments != null && !assignments.isEmpty()) { for(List<HRegionInfo> regionList : assignments.values()) { assertTrue(regionList.size() == min || regionList.size() == max); } } returnRegions(regions); returnServers(servers); } }
public static Map<HServerInfo,List<HRegionInfo>> roundRobinAssignment( List<HRegionInfo> regions, List<HServerInfo> servers) { if(regions.size() == 0 || servers.size() == 0) { return null; } Map<HServerInfo,List<HRegionInfo>> assignments = new TreeMap<HServerInfo,List<HRegionInfo>>(); int numRegions = regions.size(); int numServers = servers.size(); int max = (int)Math.ceil((float)numRegions/numServers); int serverIdx = 0; if (numServers > 1) { serverIdx = RANDOM.nextInt(numServers); } int regionIdx = 0; for (int j = 0; j < numServers; j++) { HServerInfo server = servers.get((j+serverIdx) % numServers); List<HRegionInfo> serverRegions = new ArrayList<HRegionInfo>(max); for (int i=regionIdx; i<numRegions; i += numServers) { serverRegions.add(regions.get(i % numRegions)); } assignments.put(server, serverRegions); regionIdx++; } return assignments; }
LoadBalancer { public static Map<HServerInfo,List<HRegionInfo>> roundRobinAssignment( List<HRegionInfo> regions, List<HServerInfo> servers) { if(regions.size() == 0 || servers.size() == 0) { return null; } Map<HServerInfo,List<HRegionInfo>> assignments = new TreeMap<HServerInfo,List<HRegionInfo>>(); int numRegions = regions.size(); int numServers = servers.size(); int max = (int)Math.ceil((float)numRegions/numServers); int serverIdx = 0; if (numServers > 1) { serverIdx = RANDOM.nextInt(numServers); } int regionIdx = 0; for (int j = 0; j < numServers; j++) { HServerInfo server = servers.get((j+serverIdx) % numServers); List<HRegionInfo> serverRegions = new ArrayList<HRegionInfo>(max); for (int i=regionIdx; i<numRegions; i += numServers) { serverRegions.add(regions.get(i % numRegions)); } assignments.put(server, serverRegions); regionIdx++; } return assignments; } }
LoadBalancer { public static Map<HServerInfo,List<HRegionInfo>> roundRobinAssignment( List<HRegionInfo> regions, List<HServerInfo> servers) { if(regions.size() == 0 || servers.size() == 0) { return null; } Map<HServerInfo,List<HRegionInfo>> assignments = new TreeMap<HServerInfo,List<HRegionInfo>>(); int numRegions = regions.size(); int numServers = servers.size(); int max = (int)Math.ceil((float)numRegions/numServers); int serverIdx = 0; if (numServers > 1) { serverIdx = RANDOM.nextInt(numServers); } int regionIdx = 0; for (int j = 0; j < numServers; j++) { HServerInfo server = servers.get((j+serverIdx) % numServers); List<HRegionInfo> serverRegions = new ArrayList<HRegionInfo>(max); for (int i=regionIdx; i<numRegions; i += numServers) { serverRegions.add(regions.get(i % numRegions)); } assignments.put(server, serverRegions); regionIdx++; } return assignments; } LoadBalancer(Configuration conf); }
LoadBalancer { public static Map<HServerInfo,List<HRegionInfo>> roundRobinAssignment( List<HRegionInfo> regions, List<HServerInfo> servers) { if(regions.size() == 0 || servers.size() == 0) { return null; } Map<HServerInfo,List<HRegionInfo>> assignments = new TreeMap<HServerInfo,List<HRegionInfo>>(); int numRegions = regions.size(); int numServers = servers.size(); int max = (int)Math.ceil((float)numRegions/numServers); int serverIdx = 0; if (numServers > 1) { serverIdx = RANDOM.nextInt(numServers); } int regionIdx = 0; for (int j = 0; j < numServers; j++) { HServerInfo server = servers.get((j+serverIdx) % numServers); List<HRegionInfo> serverRegions = new ArrayList<HRegionInfo>(max); for (int i=regionIdx; i<numRegions; i += numServers) { serverRegions.add(regions.get(i % numRegions)); } assignments.put(server, serverRegions); regionIdx++; } 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>> roundRobinAssignment( List<HRegionInfo> regions, List<HServerInfo> servers) { if(regions.size() == 0 || servers.size() == 0) { return null; } Map<HServerInfo,List<HRegionInfo>> assignments = new TreeMap<HServerInfo,List<HRegionInfo>>(); int numRegions = regions.size(); int numServers = servers.size(); int max = (int)Math.ceil((float)numRegions/numServers); int serverIdx = 0; if (numServers > 1) { serverIdx = RANDOM.nextInt(numServers); } int regionIdx = 0; for (int j = 0; j < numServers; j++) { HServerInfo server = servers.get((j+serverIdx) % numServers); List<HRegionInfo> serverRegions = new ArrayList<HRegionInfo>(max); for (int i=regionIdx; i<numRegions; i += numServers) { serverRegions.add(regions.get(i % numRegions)); } assignments.put(server, serverRegions); regionIdx++; } 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);  }