@Test public void chooseFarthestHostWithLowestScore() { JobTask task1 = setupJobTask(hostId1, hostId6); JobTask task2 = setupJobTask(hostId2, hostId4, hostId5, hostId6); Map<HostState, Double> scoreMap = new HashMap<>(); scoreMap.put(host1, 20d); scoreMap.put(host2, 50d); scoreMap.put(host3, 50d); scoreMap.put(host4, 30d); scoreMap.put(host5, 10d); scoreMap.put(host6, 20d); HostCandidateIterator hostCandidateIterator = new HostCandidateIterator(spawn, Collections.singletonList(task1), scoreMap); List<String> hostIdsForTask1 = hostCandidateIterator.getNewReplicaHosts(2, task1); assertEquals(2, hostIdsForTask1.size()); assertEquals(hostIdsForTask1.get(0), hostId2); assertEquals(hostIdsForTask1.get(1), hostId5); hostCandidateIterator = new HostCandidateIterator(spawn, Collections.singletonList(task2), scoreMap); List<String> hostIdsForTask2 = hostCandidateIterator.getNewReplicaHosts(1, task2); assertEquals(1, hostIdsForTask2.size()); assertEquals(hostIdsForTask2.get(0), hostId1); }
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);  }