@Test (timeout=180000) public void testClusterServersWithSameHostPort() { // tests whether the BaseLoadBalancer.Cluster can be constructed with servers // sharing same host and port List<ServerName> servers = getListOfServerNames(randomServers(10, 10)); List<HRegionInfo> regions = randomRegions(101); Map<ServerName, List<HRegionInfo>> clusterState = new HashMap<ServerName, List<HRegionInfo>>(); assignRegions(regions, servers, clusterState); // construct another list of servers, but sharing same hosts and ports List<ServerName> oldServers = new ArrayList<ServerName>(servers.size()); for (ServerName sn : servers) { // The old server would have had same host and port, but different start code! oldServers.add(ServerName.valueOf(sn.getHostname(), sn.getPort(), sn.getStartcode() - 10)); } regions = randomRegions(9); // some more regions assignRegions(regions, oldServers, clusterState); // should not throw exception: BaseLoadBalancer.Cluster cluster = new Cluster(clusterState, null, null, null); assertEquals(101 + 9, cluster.numRegions); assertEquals(10, cluster.numServers); // only 10 servers because they share the same host + port }
@Test public void testClusterServersWithSameHostPort() { // tests whether the BaseLoadBalancer.Cluster can be constructed with servers // sharing same host and port List<ServerName> servers = getListOfServerNames(randomServers(10, 10)); List<HRegionInfo> regions = randomRegions(101); Map<ServerName, List<HRegionInfo>> clusterState = new HashMap<ServerName, List<HRegionInfo>>(); assignRegions(regions, servers, clusterState); // construct another list of servers, but sharing same hosts and ports List<ServerName> oldServers = new ArrayList<ServerName>(servers.size()); for (ServerName sn : servers) { // The old server would have had same host and port, but different start code! oldServers.add(ServerName.valueOf(sn.getHostname(), sn.getPort(), sn.getStartcode() - 10)); } regions = randomRegions(9); // some more regions assignRegions(regions, oldServers, clusterState); // should not throw exception: BaseLoadBalancer.Cluster cluster = new Cluster(clusterState, null, null); assertEquals(101 + 9, cluster.numRegions); assertEquals(10, cluster.numServers); // only 10 servers because they share the same host + port }
@Test public void testCostAfterUndoAction() { final int runs = 10; loadBalancer.setConf(conf); for (int[] mockCluster : clusterStateMocks) { BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster); loadBalancer.initCosts(cluster); for (int i = 0; i != runs; ++i) { final double expectedCost = loadBalancer.computeCost(cluster, Double.MAX_VALUE); Cluster.Action action = loadBalancer.nextAction(cluster); cluster.doAction(action); loadBalancer.updateCostsWithAction(cluster, action); Cluster.Action undoAction = action.undoAction(); cluster.doAction(undoAction); loadBalancer.updateCostsWithAction(cluster, undoAction); final double actualCost = loadBalancer.computeCost(cluster, Double.MAX_VALUE); assertEquals(expectedCost, actualCost, 0); } } }
@Test (timeout=180000) public void testClusterServersWithSameHostPort() { // tests whether the BaseLoadBalancer.Cluster can be constructed with servers // sharing same host and port List<ServerName> servers = getListOfServerNames(randomServers(10, 10)); List<RegionInfo> regions = randomRegions(101); Map<ServerName, List<RegionInfo>> clusterState = new HashMap<>(); assignRegions(regions, servers, clusterState); // construct another list of servers, but sharing same hosts and ports List<ServerName> oldServers = new ArrayList<>(servers.size()); for (ServerName sn : servers) { // The old server would have had same host and port, but different start code! oldServers.add(ServerName.valueOf(sn.getHostname(), sn.getPort(), sn.getStartcode() - 10)); } regions = randomRegions(9); // some more regions assignRegions(regions, oldServers, clusterState); // should not throw exception: BaseLoadBalancer.Cluster cluster = new Cluster(clusterState, null, null, null); assertEquals(101 + 9, cluster.numRegions); assertEquals(10, cluster.numServers); // only 10 servers because they share the same host + port }
@Test public void testClusterServersWithSameHostPort() { // tests whether the BaseLoadBalancer.Cluster can be constructed with servers // sharing same host and port List<ServerName> servers = getListOfServerNames(randomServers(10, 10)); List<HRegionInfo> regions = randomRegions(101); Map<ServerName, List<HRegionInfo>> clusterState = new HashMap<ServerName, List<HRegionInfo>>(); assignRegions(regions, servers, clusterState); // construct another list of servers, but sharing same hosts and ports List<ServerName> oldServers = new ArrayList<ServerName>(servers.size()); for (ServerName sn : servers) { // The old server would have had same host and port, but different start code! oldServers.add(ServerName.valueOf(sn.getHostname(), sn.getPort(), sn.getStartcode() - 10)); } regions = randomRegions(9); // some more regions assignRegions(regions, oldServers, clusterState); // should not throw exception: BaseLoadBalancer.Cluster cluster = new Cluster(null, clusterState, null, null, null); assertEquals(101 + 9, cluster.numRegions); assertEquals(10, cluster.numServers); // only 10 servers because they share the same host + port }
@Test public void testTableSkewCost() { Configuration conf = HBaseConfiguration.create(); StochasticLoadBalancer.CostFunction costFunction = new StochasticLoadBalancer.TableSkewCostFunction(conf); for (int[] mockCluster : clusterStateMocks) { BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster); costFunction.init(cluster); double cost = costFunction.cost(); assertTrue(cost >= 0); assertTrue(cost <= 1.01); } }
@Test public void testReplicaCost() { Configuration conf = HBaseConfiguration.create(); StochasticLoadBalancer.CostFunction costFunction = new StochasticLoadBalancer.RegionReplicaHostCostFunction(conf); for (int[] mockCluster : clusterStateMocks) { BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster); costFunction.init(cluster); double cost = costFunction.cost(); assertTrue(cost >= 0); assertTrue(cost <= 1.01); } }
@Test public void testNeedsBalanceForColocatedReplicas() { // check for the case where there are two hosts and with one rack, and where // both the replicas are hosted on the same server List<HRegionInfo> regions = randomRegions(1); ServerName s1 = ServerName.valueOf("host1", 1000, 11111); ServerName s2 = ServerName.valueOf("host11", 1000, 11111); Map<ServerName, List<HRegionInfo>> map = new HashMap<ServerName, List<HRegionInfo>>(); map.put(s1, regions); regions.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(0), 1)); // until the step above s1 holds two replicas of a region regions = randomRegions(1); map.put(s2, regions); assertTrue(loadBalancer.needsBalance(new Cluster(map, null, null, null))); // check for the case where there are two hosts on the same rack and there are two racks // and both the replicas are on the same rack map.clear(); regions = randomRegions(1); List<HRegionInfo> regionsOnS2 = new ArrayList<HRegionInfo>(1); regionsOnS2.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(0), 1)); map.put(s1, regions); map.put(s2, regionsOnS2); // add another server so that the cluster has some host on another rack map.put(ServerName.valueOf("host2", 1000, 11111), randomRegions(1)); assertTrue(loadBalancer.needsBalance(new Cluster(map, null, null, new ForTestRackManagerOne()))); }
@Test public void testMoveCost() throws Exception { Configuration conf = HBaseConfiguration.create(); StochasticLoadBalancer.CostFunction costFunction = new StochasticLoadBalancer.MoveCostFunction(conf); for (int[] mockCluster : clusterStateMocks) { BaseLoadBalancer.Cluster cluster = mockCluster(mockCluster); costFunction.init(cluster); double cost = costFunction.cost(); assertEquals(0.0f, cost, 0.001); // cluster region number is smaller than maxMoves=600 cluster.setNumRegions(200); cluster.setNumMovedRegions(10); cost = costFunction.cost(); assertEquals(0.05f, cost, 0.001); cluster.setNumMovedRegions(100); cost = costFunction.cost(); assertEquals(0.5f, cost, 0.001); cluster.setNumMovedRegions(200); cost = costFunction.cost(); assertEquals(1.0f, cost, 0.001); // cluster region number is bigger than maxMoves=2500 cluster.setNumRegions(10000); cluster.setNumMovedRegions(250); cost = costFunction.cost(); assertEquals(0.1f, cost, 0.001); cluster.setNumMovedRegions(1250); cost = costFunction.cost(); assertEquals(0.5f, cost, 0.001); cluster.setNumMovedRegions(2500); cost = costFunction.cost(); assertEquals(1.0f, cost, 0.01); } }
@Test public void testNeedsBalanceForColocatedReplicas() { // check for the case where there are two hosts and with one rack, and where // both the replicas are hosted on the same server List<RegionInfo> regions = randomRegions(1); ServerName s1 = ServerName.valueOf("host1", 1000, 11111); ServerName s2 = ServerName.valueOf("host11", 1000, 11111); Map<ServerName, List<RegionInfo>> map = new HashMap<>(); map.put(s1, regions); regions.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(0), 1)); // until the step above s1 holds two replicas of a region regions = randomRegions(1); map.put(s2, regions); assertTrue(loadBalancer.needsBalance(new Cluster(map, null, null, null))); // check for the case where there are two hosts on the same rack and there are two racks // and both the replicas are on the same rack map.clear(); regions = randomRegions(1); List<RegionInfo> regionsOnS2 = new ArrayList<>(1); regionsOnS2.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(0), 1)); map.put(s1, regions); map.put(s2, regionsOnS2); // add another server so that the cluster has some host on another rack map.put(ServerName.valueOf("host2", 1000, 11111), randomRegions(1)); assertTrue( loadBalancer.needsBalance(new Cluster(map, null, null, new ForTestRackManagerOne()))); }
@Test public void testReplicaCostForReplicas() { Configuration conf = HBaseConfiguration.create(); StochasticLoadBalancer.CostFunction costFunction = new StochasticLoadBalancer.RegionReplicaHostCostFunction(conf); int [] servers = new int[] {3,3,3,3,3}; TreeMap<ServerName, List<HRegionInfo>> clusterState = mockClusterServers(servers); BaseLoadBalancer.Cluster cluster; cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWithoutReplicas = costFunction.cost(); assertEquals(0, costWithoutReplicas, 0); // replicate the region from first server to the last server HRegionInfo replica1 = RegionReplicaUtil.getRegionInfoForReplica( clusterState.firstEntry().getValue().get(0),1); clusterState.lastEntry().getValue().add(replica1); cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWith1ReplicaDifferentServer = costFunction.cost(); assertEquals(0, costWith1ReplicaDifferentServer, 0); // add a third replica to the last server HRegionInfo replica2 = RegionReplicaUtil.getRegionInfoForReplica(replica1, 2); clusterState.lastEntry().getValue().add(replica2); cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWith1ReplicaSameServer = costFunction.cost(); assertTrue(costWith1ReplicaDifferentServer < costWith1ReplicaSameServer); // test with replication = 4 for following: HRegionInfo replica3; Iterator<Entry<ServerName, List<HRegionInfo>>> it; Entry<ServerName, List<HRegionInfo>> entry; clusterState = mockClusterServers(servers); it = clusterState.entrySet().iterator(); entry = it.next(); //first server HRegionInfo hri = entry.getValue().get(0); replica1 = RegionReplicaUtil.getRegionInfoForReplica(hri, 1); replica2 = RegionReplicaUtil.getRegionInfoForReplica(hri, 2); replica3 = RegionReplicaUtil.getRegionInfoForReplica(hri, 3); entry.getValue().add(replica1); entry.getValue().add(replica2); it.next().getValue().add(replica3); //2nd server cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWith3ReplicasSameServer = costFunction.cost(); clusterState = mockClusterServers(servers); hri = clusterState.firstEntry().getValue().get(0); replica1 = RegionReplicaUtil.getRegionInfoForReplica(hri, 1); replica2 = RegionReplicaUtil.getRegionInfoForReplica(hri, 2); replica3 = RegionReplicaUtil.getRegionInfoForReplica(hri, 3); clusterState.firstEntry().getValue().add(replica1); clusterState.lastEntry().getValue().add(replica2); clusterState.lastEntry().getValue().add(replica3); cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWith2ReplicasOnTwoServers = costFunction.cost(); assertTrue(costWith2ReplicasOnTwoServers < costWith3ReplicasSameServer); }
@Test (timeout=180000) public void testClusterRegionLocations() { // tests whether region locations are handled correctly in Cluster List<ServerName> servers = getListOfServerNames(randomServers(10, 10)); List<HRegionInfo> regions = randomRegions(101); Map<ServerName, List<HRegionInfo>> clusterState = new HashMap<ServerName, List<HRegionInfo>>(); assignRegions(regions, servers, clusterState); // mock block locality for some regions RegionLocationFinder locationFinder = mock(RegionLocationFinder.class); // block locality: region:0 => {server:0} // region:1 => {server:0, server:1} // region:42 => {server:4, server:9, server:5} when(locationFinder.getTopBlockLocations(regions.get(0))).thenReturn( Lists.newArrayList(servers.get(0))); when(locationFinder.getTopBlockLocations(regions.get(1))).thenReturn( Lists.newArrayList(servers.get(0), servers.get(1))); when(locationFinder.getTopBlockLocations(regions.get(42))).thenReturn( Lists.newArrayList(servers.get(4), servers.get(9), servers.get(5))); when(locationFinder.getTopBlockLocations(regions.get(43))).thenReturn( Lists.newArrayList(ServerName.valueOf("foo", 0, 0))); // this server does not exists in clusterStatus BaseLoadBalancer.Cluster cluster = new Cluster(clusterState, null, locationFinder, null); int r0 = ArrayUtils.indexOf(cluster.regions, regions.get(0)); // this is ok, it is just a test int r1 = ArrayUtils.indexOf(cluster.regions, regions.get(1)); int r10 = ArrayUtils.indexOf(cluster.regions, regions.get(10)); int r42 = ArrayUtils.indexOf(cluster.regions, regions.get(42)); int r43 = ArrayUtils.indexOf(cluster.regions, regions.get(43)); int s0 = cluster.serversToIndex.get(servers.get(0).getHostAndPort()); int s1 = cluster.serversToIndex.get(servers.get(1).getHostAndPort()); int s4 = cluster.serversToIndex.get(servers.get(4).getHostAndPort()); int s5 = cluster.serversToIndex.get(servers.get(5).getHostAndPort()); int s9 = cluster.serversToIndex.get(servers.get(9).getHostAndPort()); // region 0 locations assertEquals(1, cluster.regionLocations[r0].length); assertEquals(s0, cluster.regionLocations[r0][0]); // region 1 locations assertEquals(2, cluster.regionLocations[r1].length); assertEquals(s0, cluster.regionLocations[r1][0]); assertEquals(s1, cluster.regionLocations[r1][1]); // region 10 locations assertEquals(0, cluster.regionLocations[r10].length); // region 42 locations assertEquals(3, cluster.regionLocations[r42].length); assertEquals(s4, cluster.regionLocations[r42][0]); assertEquals(s9, cluster.regionLocations[r42][1]); assertEquals(s5, cluster.regionLocations[r42][2]); // region 43 locations assertEquals(1, cluster.regionLocations[r43].length); assertEquals(-1, cluster.regionLocations[r43][0]); }
@Test public void testClusterRegionLocations() { // tests whether region locations are handled correctly in Cluster List<ServerName> servers = getListOfServerNames(randomServers(10, 10)); List<HRegionInfo> regions = randomRegions(101); Map<ServerName, List<HRegionInfo>> clusterState = new HashMap<ServerName, List<HRegionInfo>>(); assignRegions(regions, servers, clusterState); // mock block locality for some regions RegionLocationFinder locationFinder = mock(RegionLocationFinder.class); // block locality: region:0 => {server:0} // region:1 => {server:0, server:1} // region:42 => {server:4, server:9, server:5} when(locationFinder.getTopBlockLocations(regions.get(0))).thenReturn( Lists.newArrayList(servers.get(0))); when(locationFinder.getTopBlockLocations(regions.get(1))).thenReturn( Lists.newArrayList(servers.get(0), servers.get(1))); when(locationFinder.getTopBlockLocations(regions.get(42))).thenReturn( Lists.newArrayList(servers.get(4), servers.get(9), servers.get(5))); when(locationFinder.getTopBlockLocations(regions.get(43))).thenReturn( Lists.newArrayList(ServerName.valueOf("foo", 0, 0))); // this server does not exists in clusterStatus BaseLoadBalancer.Cluster cluster = new Cluster(clusterState, null, locationFinder); int r0 = ArrayUtils.indexOf(cluster.regions, regions.get(0)); // this is ok, it is just a test int r1 = ArrayUtils.indexOf(cluster.regions, regions.get(1)); int r10 = ArrayUtils.indexOf(cluster.regions, regions.get(10)); int r42 = ArrayUtils.indexOf(cluster.regions, regions.get(42)); int r43 = ArrayUtils.indexOf(cluster.regions, regions.get(43)); int s0 = cluster.serversToIndex.get(servers.get(0).getHostAndPort()); int s1 = cluster.serversToIndex.get(servers.get(1).getHostAndPort()); int s4 = cluster.serversToIndex.get(servers.get(4).getHostAndPort()); int s5 = cluster.serversToIndex.get(servers.get(5).getHostAndPort()); int s9 = cluster.serversToIndex.get(servers.get(9).getHostAndPort()); // region 0 locations assertEquals(1, cluster.regionLocations[r0].length); assertEquals(s0, cluster.regionLocations[r0][0]); // region 1 locations assertEquals(2, cluster.regionLocations[r1].length); assertEquals(s0, cluster.regionLocations[r1][0]); assertEquals(s1, cluster.regionLocations[r1][1]); // region 10 locations assertEquals(0, cluster.regionLocations[r10].length); // region 42 locations assertEquals(3, cluster.regionLocations[r42].length); assertEquals(s4, cluster.regionLocations[r42][0]); assertEquals(s9, cluster.regionLocations[r42][1]); assertEquals(s5, cluster.regionLocations[r42][2]); // region 43 locations assertEquals(1, cluster.regionLocations[r43].length); assertEquals(-1, cluster.regionLocations[r43][0]); }
@Test (timeout=180000) public void testClusterRegionLocations() { // tests whether region locations are handled correctly in Cluster List<ServerName> servers = getListOfServerNames(randomServers(10, 10)); List<RegionInfo> regions = randomRegions(101); Map<ServerName, List<RegionInfo>> clusterState = new HashMap<>(); assignRegions(regions, servers, clusterState); // mock block locality for some regions RegionLocationFinder locationFinder = mock(RegionLocationFinder.class); // block locality: region:0 => {server:0} // region:1 => {server:0, server:1} // region:42 => {server:4, server:9, server:5} when(locationFinder.getTopBlockLocations(regions.get(0))).thenReturn( Lists.newArrayList(servers.get(0))); when(locationFinder.getTopBlockLocations(regions.get(1))).thenReturn( Lists.newArrayList(servers.get(0), servers.get(1))); when(locationFinder.getTopBlockLocations(regions.get(42))).thenReturn( Lists.newArrayList(servers.get(4), servers.get(9), servers.get(5))); when(locationFinder.getTopBlockLocations(regions.get(43))).thenReturn( Lists.newArrayList(ServerName.valueOf("foo", 0, 0))); // this server does not exists in clusterStatus BaseLoadBalancer.Cluster cluster = new Cluster(clusterState, null, locationFinder, null); int r0 = ArrayUtils.indexOf(cluster.regions, regions.get(0)); // this is ok, it is just a test int r1 = ArrayUtils.indexOf(cluster.regions, regions.get(1)); int r10 = ArrayUtils.indexOf(cluster.regions, regions.get(10)); int r42 = ArrayUtils.indexOf(cluster.regions, regions.get(42)); int r43 = ArrayUtils.indexOf(cluster.regions, regions.get(43)); int s0 = cluster.serversToIndex.get(servers.get(0).getHostAndPort()); int s1 = cluster.serversToIndex.get(servers.get(1).getHostAndPort()); int s4 = cluster.serversToIndex.get(servers.get(4).getHostAndPort()); int s5 = cluster.serversToIndex.get(servers.get(5).getHostAndPort()); int s9 = cluster.serversToIndex.get(servers.get(9).getHostAndPort()); // region 0 locations assertEquals(1, cluster.regionLocations[r0].length); assertEquals(s0, cluster.regionLocations[r0][0]); // region 1 locations assertEquals(2, cluster.regionLocations[r1].length); assertEquals(s0, cluster.regionLocations[r1][0]); assertEquals(s1, cluster.regionLocations[r1][1]); // region 10 locations assertEquals(0, cluster.regionLocations[r10].length); // region 42 locations assertEquals(3, cluster.regionLocations[r42].length); assertEquals(s4, cluster.regionLocations[r42][0]); assertEquals(s9, cluster.regionLocations[r42][1]); assertEquals(s5, cluster.regionLocations[r42][2]); // region 43 locations assertEquals(1, cluster.regionLocations[r43].length); assertEquals(-1, cluster.regionLocations[r43][0]); }
@Test public void testReplicaCostForReplicas() { Configuration conf = HBaseConfiguration.create(); StochasticLoadBalancer.CostFunction costFunction = new StochasticLoadBalancer.RegionReplicaHostCostFunction(conf); int[] servers = new int[] { 3, 3, 3, 3, 3 }; TreeMap<ServerName, List<RegionInfo>> clusterState = mockClusterServers(servers); BaseLoadBalancer.Cluster cluster; cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWithoutReplicas = costFunction.cost(); assertEquals(0, costWithoutReplicas, 0); // replicate the region from first server to the last server RegionInfo replica1 = RegionReplicaUtil.getRegionInfoForReplica(clusterState.firstEntry().getValue().get(0), 1); clusterState.lastEntry().getValue().add(replica1); cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWith1ReplicaDifferentServer = costFunction.cost(); assertEquals(0, costWith1ReplicaDifferentServer, 0); // add a third replica to the last server RegionInfo replica2 = RegionReplicaUtil.getRegionInfoForReplica(replica1, 2); clusterState.lastEntry().getValue().add(replica2); cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWith1ReplicaSameServer = costFunction.cost(); assertTrue(costWith1ReplicaDifferentServer < costWith1ReplicaSameServer); // test with replication = 4 for following: RegionInfo replica3; Iterator<Entry<ServerName, List<RegionInfo>>> it; Entry<ServerName, List<RegionInfo>> entry; clusterState = mockClusterServers(servers); it = clusterState.entrySet().iterator(); entry = it.next(); // first server RegionInfo hri = entry.getValue().get(0); replica1 = RegionReplicaUtil.getRegionInfoForReplica(hri, 1); replica2 = RegionReplicaUtil.getRegionInfoForReplica(hri, 2); replica3 = RegionReplicaUtil.getRegionInfoForReplica(hri, 3); entry.getValue().add(replica1); entry.getValue().add(replica2); it.next().getValue().add(replica3); // 2nd server cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWith3ReplicasSameServer = costFunction.cost(); clusterState = mockClusterServers(servers); hri = clusterState.firstEntry().getValue().get(0); replica1 = RegionReplicaUtil.getRegionInfoForReplica(hri, 1); replica2 = RegionReplicaUtil.getRegionInfoForReplica(hri, 2); replica3 = RegionReplicaUtil.getRegionInfoForReplica(hri, 3); clusterState.firstEntry().getValue().add(replica1); clusterState.lastEntry().getValue().add(replica2); clusterState.lastEntry().getValue().add(replica3); cluster = new BaseLoadBalancer.Cluster(clusterState, null, null, null); costFunction.init(cluster); double costWith2ReplicasOnTwoServers = costFunction.cost(); assertTrue(costWith2ReplicasOnTwoServers < costWith3ReplicasSameServer); }
@Test public void testClusterRegionLocations() { // tests whether region locations are handled correctly in Cluster List<ServerName> servers = getListOfServerNames(randomServers(10, 10)); List<HRegionInfo> regions = randomRegions(101); Map<ServerName, List<HRegionInfo>> clusterState = new HashMap<ServerName, List<HRegionInfo>>(); assignRegions(regions, servers, clusterState); // mock block locality for some regions RegionLocationFinder locationFinder = mock(RegionLocationFinder.class); // block locality: region:0 => {server:0} // region:1 => {server:0, server:1} // region:42 => {server:4, server:9, server:5} when(locationFinder.getTopBlockLocations(regions.get(0))).thenReturn( Lists.newArrayList(servers.get(0))); when(locationFinder.getTopBlockLocations(regions.get(1))).thenReturn( Lists.newArrayList(servers.get(0), servers.get(1))); when(locationFinder.getTopBlockLocations(regions.get(42))).thenReturn( Lists.newArrayList(servers.get(4), servers.get(9), servers.get(5))); when(locationFinder.getTopBlockLocations(regions.get(43))).thenReturn( Lists.newArrayList(ServerName.valueOf("foo", 0, 0))); // this server does not exists in clusterStatus BaseLoadBalancer.Cluster cluster = new Cluster(null, clusterState, null, locationFinder, null); int r0 = ArrayUtils.indexOf(cluster.regions, regions.get(0)); // this is ok, it is just a test int r1 = ArrayUtils.indexOf(cluster.regions, regions.get(1)); int r10 = ArrayUtils.indexOf(cluster.regions, regions.get(10)); int r42 = ArrayUtils.indexOf(cluster.regions, regions.get(42)); int r43 = ArrayUtils.indexOf(cluster.regions, regions.get(43)); int s0 = cluster.serversToIndex.get(servers.get(0).getHostAndPort()); int s1 = cluster.serversToIndex.get(servers.get(1).getHostAndPort()); int s4 = cluster.serversToIndex.get(servers.get(4).getHostAndPort()); int s5 = cluster.serversToIndex.get(servers.get(5).getHostAndPort()); int s9 = cluster.serversToIndex.get(servers.get(9).getHostAndPort()); // region 0 locations assertEquals(1, cluster.regionLocations[r0].length); assertEquals(s0, cluster.regionLocations[r0][0]); // region 1 locations assertEquals(2, cluster.regionLocations[r1].length); assertEquals(s0, cluster.regionLocations[r1][0]); assertEquals(s1, cluster.regionLocations[r1][1]); // region 10 locations assertEquals(0, cluster.regionLocations[r10].length); // region 42 locations assertEquals(3, cluster.regionLocations[r42].length); assertEquals(s4, cluster.regionLocations[r42][0]); assertEquals(s9, cluster.regionLocations[r42][1]); assertEquals(s5, cluster.regionLocations[r42][2]); // region 43 locations assertEquals(1, cluster.regionLocations[r43].length); assertEquals(-1, cluster.regionLocations[r43][0]); }