@Test public void testBulkAssignment() throws Exception { List<ServerName> tmp = getListOfServerNames(randomServers(5, 0)); List<HRegionInfo> hris = randomRegions(20); hris.add(HRegionInfo.FIRST_META_REGIONINFO); tmp.add(master); Map<ServerName, List<HRegionInfo>> plans = loadBalancer.roundRobinAssignment(hris, tmp); assertTrue(plans.get(master).contains(HRegionInfo.FIRST_META_REGIONINFO)); assertEquals(1, plans.get(master).size()); int totalRegion = 0; for (List<HRegionInfo> regions: plans.values()) { totalRegion += regions.size(); } assertEquals(hris.size(), totalRegion); for (int[] mock : regionsAndServersMocks) { LOG.debug("testBulkAssignment with " + mock[0] + " regions and " + mock[1] + " servers"); List<HRegionInfo> regions = randomRegions(mock[0]); List<ServerAndLoad> servers = randomServers(mock[1], 0); List<ServerName> list = getListOfServerNames(servers); Map<ServerName, List<HRegionInfo>> assignments = loadBalancer.roundRobinAssignment(regions, list); 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(list); } }
@Override public Map<ServerName, List<HRegionInfo>> roundRobinAssignment(List<HRegionInfo> regions, List<ServerName> servers) { metricsBalancer.incrMiscInvocations(); if (regions == null || regions.isEmpty()) { return null; } List<ServerName> backupMasters = normalizeServers(servers); int numServers = servers == null ? 0 : servers.size(); int numBackupMasters = backupMasters == null ? 0 : backupMasters.size(); if (numServers == 0 && numBackupMasters == 0) { LOG.warn("Wanted to do round robin assignment but no servers to assign to"); return null; } Map<ServerName, List<HRegionInfo>> assignments = new TreeMap<ServerName, List<HRegionInfo>>(); if (numServers + numBackupMasters == 1) { ServerName server = numServers > 0 ? servers.get(0) : backupMasters.get(0); assignments.put(server, new ArrayList<HRegionInfo>(regions)); return assignments; } List<HRegionInfo> masterRegions = null; if (numServers > 0 && servers.contains(masterServerName)) { masterRegions = new ArrayList<HRegionInfo>(); if (numServers == 1) { numServers = 0; } } int total = regions.size(); int numRegions = total * numBackupMasters / (numServers * backupMasterWeight + numBackupMasters); if (numRegions > 0) { roundRobinAssignment(regions, 0, numRegions, backupMasters, masterRegions, assignments); } int remainder = total - numRegions; if (remainder > 0) { roundRobinAssignment(regions, numRegions, remainder, servers, masterRegions, assignments); } if (masterRegions != null && !masterRegions.isEmpty()) { assignments.put(masterServerName, masterRegions); } return assignments; }
BaseLoadBalancer implements LoadBalancer { @Override public Map<ServerName, List<HRegionInfo>> roundRobinAssignment(List<HRegionInfo> regions, List<ServerName> servers) { metricsBalancer.incrMiscInvocations(); if (regions == null || regions.isEmpty()) { return null; } List<ServerName> backupMasters = normalizeServers(servers); int numServers = servers == null ? 0 : servers.size(); int numBackupMasters = backupMasters == null ? 0 : backupMasters.size(); if (numServers == 0 && numBackupMasters == 0) { LOG.warn("Wanted to do round robin assignment but no servers to assign to"); return null; } Map<ServerName, List<HRegionInfo>> assignments = new TreeMap<ServerName, List<HRegionInfo>>(); if (numServers + numBackupMasters == 1) { ServerName server = numServers > 0 ? servers.get(0) : backupMasters.get(0); assignments.put(server, new ArrayList<HRegionInfo>(regions)); return assignments; } List<HRegionInfo> masterRegions = null; if (numServers > 0 && servers.contains(masterServerName)) { masterRegions = new ArrayList<HRegionInfo>(); if (numServers == 1) { numServers = 0; } } int total = regions.size(); int numRegions = total * numBackupMasters / (numServers * backupMasterWeight + numBackupMasters); if (numRegions > 0) { roundRobinAssignment(regions, 0, numRegions, backupMasters, masterRegions, assignments); } int remainder = total - numRegions; if (remainder > 0) { roundRobinAssignment(regions, numRegions, remainder, servers, masterRegions, assignments); } if (masterRegions != null && !masterRegions.isEmpty()) { assignments.put(masterServerName, masterRegions); } return assignments; } }
BaseLoadBalancer implements LoadBalancer { @Override public Map<ServerName, List<HRegionInfo>> roundRobinAssignment(List<HRegionInfo> regions, List<ServerName> servers) { metricsBalancer.incrMiscInvocations(); if (regions == null || regions.isEmpty()) { return null; } List<ServerName> backupMasters = normalizeServers(servers); int numServers = servers == null ? 0 : servers.size(); int numBackupMasters = backupMasters == null ? 0 : backupMasters.size(); if (numServers == 0 && numBackupMasters == 0) { LOG.warn("Wanted to do round robin assignment but no servers to assign to"); return null; } Map<ServerName, List<HRegionInfo>> assignments = new TreeMap<ServerName, List<HRegionInfo>>(); if (numServers + numBackupMasters == 1) { ServerName server = numServers > 0 ? servers.get(0) : backupMasters.get(0); assignments.put(server, new ArrayList<HRegionInfo>(regions)); return assignments; } List<HRegionInfo> masterRegions = null; if (numServers > 0 && servers.contains(masterServerName)) { masterRegions = new ArrayList<HRegionInfo>(); if (numServers == 1) { numServers = 0; } } int total = regions.size(); int numRegions = total * numBackupMasters / (numServers * backupMasterWeight + numBackupMasters); if (numRegions > 0) { roundRobinAssignment(regions, 0, numRegions, backupMasters, masterRegions, assignments); } int remainder = total - numRegions; if (remainder > 0) { roundRobinAssignment(regions, numRegions, remainder, servers, masterRegions, assignments); } if (masterRegions != null && !masterRegions.isEmpty()) { assignments.put(masterServerName, masterRegions); } return assignments; }  }
BaseLoadBalancer implements LoadBalancer { @Override public Map<ServerName, List<HRegionInfo>> roundRobinAssignment(List<HRegionInfo> regions, List<ServerName> servers) { metricsBalancer.incrMiscInvocations(); if (regions == null || regions.isEmpty()) { return null; } List<ServerName> backupMasters = normalizeServers(servers); int numServers = servers == null ? 0 : servers.size(); int numBackupMasters = backupMasters == null ? 0 : backupMasters.size(); if (numServers == 0 && numBackupMasters == 0) { LOG.warn("Wanted to do round robin assignment but no servers to assign to"); return null; } Map<ServerName, List<HRegionInfo>> assignments = new TreeMap<ServerName, List<HRegionInfo>>(); if (numServers + numBackupMasters == 1) { ServerName server = numServers > 0 ? servers.get(0) : backupMasters.get(0); assignments.put(server, new ArrayList<HRegionInfo>(regions)); return assignments; } List<HRegionInfo> masterRegions = null; if (numServers > 0 && servers.contains(masterServerName)) { masterRegions = new ArrayList<HRegionInfo>(); if (numServers == 1) { numServers = 0; } } int total = regions.size(); int numRegions = total * numBackupMasters / (numServers * backupMasterWeight + numBackupMasters); if (numRegions > 0) { roundRobinAssignment(regions, 0, numRegions, backupMasters, masterRegions, assignments); } int remainder = total - numRegions; if (remainder > 0) { roundRobinAssignment(regions, numRegions, remainder, servers, masterRegions, assignments); } if (masterRegions != null && !masterRegions.isEmpty()) { assignments.put(masterServerName, masterRegions); } return assignments; }  @Override void setConf(Configuration conf); void excludeServer(ServerName serverName); Set<ServerName> getExcludedServers(); @Override Configuration getConf(); @Override void setClusterStatus(ClusterStatus st); @Override void setMasterServices(MasterServices masterServices); @Override Map<ServerName, List<HRegionInfo>> roundRobinAssignment(List<HRegionInfo> regions,
      List<ServerName> servers); @Override Map<HRegionInfo, ServerName> immediateAssignment(List<HRegionInfo> regions,
      List<ServerName> servers); @Override ServerName randomAssignment(HRegionInfo regionInfo, List<ServerName> servers); @Override Map<ServerName, List<HRegionInfo>> retainAssignment(Map<HRegionInfo, ServerName> regions,
      List<ServerName> servers); @Override void initialize(); @Override void regionOnline(HRegionInfo regionInfo, ServerName sn); @Override void regionOffline(HRegionInfo regionInfo); @Override boolean isStopped(); @Override void stop(String why); }
BaseLoadBalancer implements LoadBalancer { @Override public Map<ServerName, List<HRegionInfo>> roundRobinAssignment(List<HRegionInfo> regions, List<ServerName> servers) { metricsBalancer.incrMiscInvocations(); if (regions == null || regions.isEmpty()) { return null; } List<ServerName> backupMasters = normalizeServers(servers); int numServers = servers == null ? 0 : servers.size(); int numBackupMasters = backupMasters == null ? 0 : backupMasters.size(); if (numServers == 0 && numBackupMasters == 0) { LOG.warn("Wanted to do round robin assignment but no servers to assign to"); return null; } Map<ServerName, List<HRegionInfo>> assignments = new TreeMap<ServerName, List<HRegionInfo>>(); if (numServers + numBackupMasters == 1) { ServerName server = numServers > 0 ? servers.get(0) : backupMasters.get(0); assignments.put(server, new ArrayList<HRegionInfo>(regions)); return assignments; } List<HRegionInfo> masterRegions = null; if (numServers > 0 && servers.contains(masterServerName)) { masterRegions = new ArrayList<HRegionInfo>(); if (numServers == 1) { numServers = 0; } } int total = regions.size(); int numRegions = total * numBackupMasters / (numServers * backupMasterWeight + numBackupMasters); if (numRegions > 0) { roundRobinAssignment(regions, 0, numRegions, backupMasters, masterRegions, assignments); } int remainder = total - numRegions; if (remainder > 0) { roundRobinAssignment(regions, numRegions, remainder, servers, masterRegions, assignments); } if (masterRegions != null && !masterRegions.isEmpty()) { assignments.put(masterServerName, masterRegions); } return assignments; }  @Override void setConf(Configuration conf); void excludeServer(ServerName serverName); Set<ServerName> getExcludedServers(); @Override Configuration getConf(); @Override void setClusterStatus(ClusterStatus st); @Override void setMasterServices(MasterServices masterServices); @Override Map<ServerName, List<HRegionInfo>> roundRobinAssignment(List<HRegionInfo> regions,
      List<ServerName> servers); @Override Map<HRegionInfo, ServerName> immediateAssignment(List<HRegionInfo> regions,
      List<ServerName> servers); @Override ServerName randomAssignment(HRegionInfo regionInfo, List<ServerName> servers); @Override Map<ServerName, List<HRegionInfo>> retainAssignment(Map<HRegionInfo, ServerName> regions,
      List<ServerName> servers); @Override void initialize(); @Override void regionOnline(HRegionInfo regionInfo, ServerName sn); @Override void regionOffline(HRegionInfo regionInfo); @Override boolean isStopped(); @Override void stop(String why); static final String BACKUP_MASTER_WEIGHT_KEY; static final int DEFAULT_BACKUP_MASTER_WEIGHT; }