@Test public void selectHostsBasedOnHostScore() { HostLocation location = new HostLocation("Unknown", "Unknown", "Unknown"); host1.setHostLocation(location); host2.setHostLocation(location); host3.setHostLocation(location); host4.setHostLocation(location); host5.setHostLocation(location); host6.setHostLocation(location); hostManager.getHostLocationSummary() .updateHostLocationSummary(Arrays.asList(host1, host2, host3, host4, host5, host6)); assertSame(hostManager.getHostLocationSummary().getPriorityLevel(), AvailabilityDomain.NONE); 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> replicasForTask1 = hostCandidateIterator.getNewReplicaHosts(5, task1); assertEquals(5, replicasForTask1.size()); assertEquals(hostId5, replicasForTask1.get(0)); assertEquals(hostId4, replicasForTask1.get(1)); assertEquals(hostId2, replicasForTask1.get(2)); assertEquals(hostId3, replicasForTask1.get(3)); assertEquals(hostId5, replicasForTask1.get(4)); }
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);  }