@Test public void replicasShouldBeOnSeparateHostLocations() { host1 = setUpHostState(hostId1, true, "a", "aa", "aaa", 23); host2 = setUpHostState(hostId2, true, "a", "aa", "aab", 100); host3 = setUpHostState(hostId3, true, "a", "ab", "abb", 100); host4 = setUpHostState(hostId4, true, "b", "ba", "baa", 2); host5 = setUpHostState(hostId5, true, "b", "bb", "bba", 14); host6 = setUpHostState(hostId6, true, "b", "bc", "bca", 20); host7 = setUpHostState(hostId7, true, "c", "ca", "caa", 0); host8 = setUpHostState(hostId8, true, "c", "ca", "cab", 0); host9 = setUpHostState(hostId9, true, "c", "ca", "cac", 0); List<JobTask> tasks = new ArrayList<>(); HostCandidateIterator impl = new HostCandidateIterator(spawn, tasks, scoreMap); JobTask task = setupJobTask(hostId9); List<String> newReplicas = impl.getNewReplicaHosts(1, task, null, true); assertEquals(Sets.newHashSet(hostId4), new HashSet<>(newReplicas)); task = setupJobTask(hostId9, hostId4); newReplicas = impl.getNewReplicaHosts(1, task, null, true); assertEquals(Sets.newHashSet(hostId1), new HashSet<>(newReplicas)); task = setupJobTask(hostId9, hostId4, hostId1); newReplicas = impl.getNewReplicaHosts(1, task, null, true); assertEquals(Sets.newHashSet(hostId5), new HashSet<>(newReplicas)); host1 = setUpHostState(hostId1, true, "a", "aa", "aaa", 0); host2 = setUpHostState(hostId2, true, "a", "aa", "aab", 0); host3 = setUpHostState(hostId3, true, "a", "ab", "abb", 1); host4 = setUpHostState(hostId4, true, "b", "ba", "baa", 0); host5 = setUpHostState(hostId5, true, "b", "bb", "bba", 0); host6 = setUpHostState(hostId6, true, "b", "bc", "bca", 0); host7 = setUpHostState(hostId7, true, "c", "ca", "caa", 2); host8 = setUpHostState(hostId8, true, "c", "ca", "cab", 3); host9 = setUpHostState(hostId9, true, "c", "ca", "cac", 0); task = setupJobTask(hostId9, hostId4, hostId1, hostId5); newReplicas = impl.getNewReplicaHosts(1, task, null, true); assertEquals(Sets.newHashSet(hostId3), new HashSet<>(newReplicas)); task = setupJobTask(hostId9, hostId4, hostId1, hostId5, hostId3); newReplicas = impl.getNewReplicaHosts(1, task, null, true); assertEquals(Sets.newHashSet(hostId7), new HashSet<>(newReplicas)); host1 = setUpHostState(hostId1, true, "a", "aa", "aaa", 0); host2 = setUpHostState(hostId2, true, "a", "aa", "aab", 2); host3 = setUpHostState(hostId3, true, "a", "ab", "abb", 0); host4 = setUpHostState(hostId4, true, "b", "ba", "baa", 0); host5 = setUpHostState(hostId5, true, "b", "bb", "bba", 0); host6 = setUpHostState(hostId6, true, "b", "bc", "bca", 11); host7 = setUpHostState(hostId7, true, "c", "ca", "caa", 0); host8 = setUpHostState(hostId8, true, "c", "ca", "cab", 3); host9 = setUpHostState(hostId9, true, "c", "ca", "cac", 0); task = setupJobTask(hostId9, hostId4, hostId1, hostId5, hostId3, hostId7); newReplicas = impl.getNewReplicaHosts(1, task, null, true); assertEquals(Sets.newHashSet(hostId6), new HashSet<>(newReplicas)); task = setupJobTask(hostId9, hostId4, hostId1, hostId5, hostId3, hostId7, hostId6); newReplicas = impl.getNewReplicaHosts(1, task, null, true); assertEquals(Sets.newHashSet(hostId2), new HashSet<>(newReplicas)); task = setupJobTask(hostId9, hostId4, hostId1, hostId5, hostId3, hostId7, hostId6, hostId2); newReplicas = impl.getNewReplicaHosts(1, task, null, true); assertEquals(Sets.newHashSet(hostId8), new HashSet<>(newReplicas)); }
public List<String> getNewReplicaHosts(int replicasToAdd, JobTask task, @Nullable String excludeHostUuid, boolean isReplica) { if(replicasToAdd < 1) { return new ArrayList<>(); } List<String> chosenHostIds = new ArrayList<>(replicasToAdd); Collection<HostLocation> locations = new ArrayList<>(); for (String replicaHostId : task.getAllTaskHosts()) { if ((replicaHostId != null) && !replicaHostId.equals(excludeHostUuid)) { locations.add(hostManager.getHostState(replicaHostId).getHostLocation()); } } for (int i = 0; i < replicasToAdd; i++) { int minScoreSoFar = Integer.MAX_VALUE; WithScore<HostState> bestHost = null; for (WithScore<HostState> hostAndScore : sortedHosts) { HostState host = hostAndScore.element; boolean excluded = host.getHostUuid().equals(excludeHostUuid); if (excluded || !host.canMirrorTasks() || (isReplica && !balancer.okToPutReplicaOnHost(host, task))) { continue; } HostLocation hostLocation = host.getHostLocation(); int score = 0; for (HostLocation location : locations) { score += hostLocation.assignScoreByHostLocation(location); } if (score == (this.minScore * locations.size())) { bestHost = hostAndScore; break; } if (score < minScoreSoFar) { bestHost = hostAndScore; minScoreSoFar = score; } } if (bestHost != null) { this.sortedHosts.remove(bestHost); this.sortedHosts.add(new WithScore<>(bestHost.element, bestHost.score + (double) this.taskScoreIncrement)); locations.add(bestHost.element.getHostLocation()); chosenHostIds.add(bestHost.element.getHostUuid()); } } return chosenHostIds; }
HostCandidateIterator { public List<String> getNewReplicaHosts(int replicasToAdd, JobTask task, @Nullable String excludeHostUuid, boolean isReplica) { if(replicasToAdd < 1) { return new ArrayList<>(); } List<String> chosenHostIds = new ArrayList<>(replicasToAdd); Collection<HostLocation> locations = new ArrayList<>(); for (String replicaHostId : task.getAllTaskHosts()) { if ((replicaHostId != null) && !replicaHostId.equals(excludeHostUuid)) { locations.add(hostManager.getHostState(replicaHostId).getHostLocation()); } } for (int i = 0; i < replicasToAdd; i++) { int minScoreSoFar = Integer.MAX_VALUE; WithScore<HostState> bestHost = null; for (WithScore<HostState> hostAndScore : sortedHosts) { HostState host = hostAndScore.element; boolean excluded = host.getHostUuid().equals(excludeHostUuid); if (excluded || !host.canMirrorTasks() || (isReplica && !balancer.okToPutReplicaOnHost(host, task))) { continue; } HostLocation hostLocation = host.getHostLocation(); int score = 0; for (HostLocation location : locations) { score += hostLocation.assignScoreByHostLocation(location); } if (score == (this.minScore * locations.size())) { bestHost = hostAndScore; break; } if (score < minScoreSoFar) { bestHost = hostAndScore; minScoreSoFar = score; } } if (bestHost != null) { this.sortedHosts.remove(bestHost); this.sortedHosts.add(new WithScore<>(bestHost.element, bestHost.score + (double) this.taskScoreIncrement)); locations.add(bestHost.element.getHostLocation()); chosenHostIds.add(bestHost.element.getHostUuid()); } } return chosenHostIds; } }
HostCandidateIterator { public List<String> getNewReplicaHosts(int replicasToAdd, JobTask task, @Nullable String excludeHostUuid, boolean isReplica) { if(replicasToAdd < 1) { return new ArrayList<>(); } List<String> chosenHostIds = new ArrayList<>(replicasToAdd); Collection<HostLocation> locations = new ArrayList<>(); for (String replicaHostId : task.getAllTaskHosts()) { if ((replicaHostId != null) && !replicaHostId.equals(excludeHostUuid)) { locations.add(hostManager.getHostState(replicaHostId).getHostLocation()); } } for (int i = 0; i < replicasToAdd; i++) { int minScoreSoFar = Integer.MAX_VALUE; WithScore<HostState> bestHost = null; for (WithScore<HostState> hostAndScore : sortedHosts) { HostState host = hostAndScore.element; boolean excluded = host.getHostUuid().equals(excludeHostUuid); if (excluded || !host.canMirrorTasks() || (isReplica && !balancer.okToPutReplicaOnHost(host, task))) { continue; } HostLocation hostLocation = host.getHostLocation(); int score = 0; for (HostLocation location : locations) { score += hostLocation.assignScoreByHostLocation(location); } if (score == (this.minScore * locations.size())) { bestHost = hostAndScore; break; } if (score < minScoreSoFar) { bestHost = hostAndScore; minScoreSoFar = score; } } if (bestHost != null) { this.sortedHosts.remove(bestHost); this.sortedHosts.add(new WithScore<>(bestHost.element, bestHost.score + (double) this.taskScoreIncrement)); locations.add(bestHost.element.getHostLocation()); chosenHostIds.add(bestHost.element.getHostUuid()); } } return chosenHostIds; } HostCandidateIterator(Spawn spawn, List<JobTask> tasks, Map<HostState, Double> scoreMap); }
HostCandidateIterator { public List<String> getNewReplicaHosts(int replicasToAdd, JobTask task, @Nullable String excludeHostUuid, boolean isReplica) { if(replicasToAdd < 1) { return new ArrayList<>(); } List<String> chosenHostIds = new ArrayList<>(replicasToAdd); Collection<HostLocation> locations = new ArrayList<>(); for (String replicaHostId : task.getAllTaskHosts()) { if ((replicaHostId != null) && !replicaHostId.equals(excludeHostUuid)) { locations.add(hostManager.getHostState(replicaHostId).getHostLocation()); } } for (int i = 0; i < replicasToAdd; i++) { int minScoreSoFar = Integer.MAX_VALUE; WithScore<HostState> bestHost = null; for (WithScore<HostState> hostAndScore : sortedHosts) { HostState host = hostAndScore.element; boolean excluded = host.getHostUuid().equals(excludeHostUuid); if (excluded || !host.canMirrorTasks() || (isReplica && !balancer.okToPutReplicaOnHost(host, task))) { continue; } HostLocation hostLocation = host.getHostLocation(); int score = 0; for (HostLocation location : locations) { score += hostLocation.assignScoreByHostLocation(location); } if (score == (this.minScore * locations.size())) { bestHost = hostAndScore; break; } if (score < minScoreSoFar) { bestHost = hostAndScore; minScoreSoFar = score; } } if (bestHost != null) { this.sortedHosts.remove(bestHost); this.sortedHosts.add(new WithScore<>(bestHost.element, bestHost.score + (double) this.taskScoreIncrement)); locations.add(bestHost.element.getHostLocation()); chosenHostIds.add(bestHost.element.getHostUuid()); } } return chosenHostIds; } HostCandidateIterator(Spawn spawn, List<JobTask> tasks, Map<HostState, Double> scoreMap); static TreeSet<WithScore<HostState>> generateSortedHosts(
            HostManager hostManager, List<JobTask> tasks, Map<HostState, Double> scoreMap, int taskScoreIncrement); List<String> getNewReplicaHosts(int replicasToAdd, JobTask task,
                                           @Nullable String excludeHostUuid, boolean isReplica); List<String> getNewReplicaHosts(int replicas, JobTask task); List<String> getNewReplicaHosts(JobTask task, @Nullable String excludeHost); }
HostCandidateIterator { public List<String> getNewReplicaHosts(int replicasToAdd, JobTask task, @Nullable String excludeHostUuid, boolean isReplica) { if(replicasToAdd < 1) { return new ArrayList<>(); } List<String> chosenHostIds = new ArrayList<>(replicasToAdd); Collection<HostLocation> locations = new ArrayList<>(); for (String replicaHostId : task.getAllTaskHosts()) { if ((replicaHostId != null) && !replicaHostId.equals(excludeHostUuid)) { locations.add(hostManager.getHostState(replicaHostId).getHostLocation()); } } for (int i = 0; i < replicasToAdd; i++) { int minScoreSoFar = Integer.MAX_VALUE; WithScore<HostState> bestHost = null; for (WithScore<HostState> hostAndScore : sortedHosts) { HostState host = hostAndScore.element; boolean excluded = host.getHostUuid().equals(excludeHostUuid); if (excluded || !host.canMirrorTasks() || (isReplica && !balancer.okToPutReplicaOnHost(host, task))) { continue; } HostLocation hostLocation = host.getHostLocation(); int score = 0; for (HostLocation location : locations) { score += hostLocation.assignScoreByHostLocation(location); } if (score == (this.minScore * locations.size())) { bestHost = hostAndScore; break; } if (score < minScoreSoFar) { bestHost = hostAndScore; minScoreSoFar = score; } } if (bestHost != null) { this.sortedHosts.remove(bestHost); this.sortedHosts.add(new WithScore<>(bestHost.element, bestHost.score + (double) this.taskScoreIncrement)); locations.add(bestHost.element.getHostLocation()); chosenHostIds.add(bestHost.element.getHostUuid()); } } return chosenHostIds; } HostCandidateIterator(Spawn spawn, List<JobTask> tasks, Map<HostState, Double> scoreMap); static TreeSet<WithScore<HostState>> generateSortedHosts(
            HostManager hostManager, List<JobTask> tasks, Map<HostState, Double> scoreMap, int taskScoreIncrement); List<String> getNewReplicaHosts(int replicasToAdd, JobTask task,
                                           @Nullable String excludeHostUuid, boolean isReplica); List<String> getNewReplicaHosts(int replicas, JobTask task); List<String> getNewReplicaHosts(JobTask task, @Nullable String excludeHost);  }