@Test public void sortedHostsCreatedCorrectly() { scoreMap.put(host1, 20d); scoreMap.put(host2, 50d); scoreMap.put(host3, 40d); scoreMap.put(host4, 30d); scoreMap.put(host5, 10d); scoreMap.put(host6, 20d); List<JobTask> tasks = new ArrayList<>(); tasks.add(setupJobTask(hostId1, hostId2, hostId3)); tasks.add(setupJobTask(hostId1, hostId2, hostId3)); tasks.add(setupJobTask(hostId1, hostId2, hostId3)); tasks.add(setupJobTask(hostId4, hostId5, hostId1)); TreeSet<WithScore<HostState>> result = HostCandidateIterator.generateSortedHosts(hostManager, tasks, scoreMap, 40); List<WithScore<HostState>> resultList = new ArrayList<>(result); assertEquals(hostId6, resultList.get(0).element.getHostUuid()); assertEquals(hostId5, resultList.get(1).element.getHostUuid()); assertEquals(hostId4, resultList.get(2).element.getHostUuid()); assertEquals(hostId3, resultList.get(3).element.getHostUuid()); assertEquals(hostId2, resultList.get(4).element.getHostUuid()); assertEquals(hostId1, resultList.get(5).element.getHostUuid()); assertEquals(20d, resultList.get(0).score, 0); assertEquals(50d, resultList.get(1).score, 0); assertEquals(70d, resultList.get(2).score, 0); assertEquals(160d, resultList.get(3).score, 0); assertEquals(170d, resultList.get(4).score, 0); assertEquals(180d, resultList.get(5).score, 0); }
public static TreeSet<WithScore<HostState>> generateSortedHosts( HostManager hostManager, List<JobTask> tasks, Map<HostState, Double> scoreMap, int taskScoreIncrement) { TreeSet<WithScore<HostState>> sortedHosts = new TreeSet<>(hostAndScoreComparator); Map<HostState, Double> copyOfScoreMap = new HashMap<>(scoreMap); if(tasks != null) { for (JobTask task : tasks) { for (String replicaHostId : task.getAllTaskHosts()) { HostState host = hostManager.getHostState(replicaHostId); if(host != null) { Double score = copyOfScoreMap.getOrDefault(host, 0d); copyOfScoreMap.put(host, score + (double) taskScoreIncrement); } } } } for (Map.Entry<HostState, Double> entry : copyOfScoreMap.entrySet()) { sortedHosts.add(new WithScore<>(entry.getKey(), entry.getValue())); } return sortedHosts; }
HostCandidateIterator { public static TreeSet<WithScore<HostState>> generateSortedHosts( HostManager hostManager, List<JobTask> tasks, Map<HostState, Double> scoreMap, int taskScoreIncrement) { TreeSet<WithScore<HostState>> sortedHosts = new TreeSet<>(hostAndScoreComparator); Map<HostState, Double> copyOfScoreMap = new HashMap<>(scoreMap); if(tasks != null) { for (JobTask task : tasks) { for (String replicaHostId : task.getAllTaskHosts()) { HostState host = hostManager.getHostState(replicaHostId); if(host != null) { Double score = copyOfScoreMap.getOrDefault(host, 0d); copyOfScoreMap.put(host, score + (double) taskScoreIncrement); } } } } for (Map.Entry<HostState, Double> entry : copyOfScoreMap.entrySet()) { sortedHosts.add(new WithScore<>(entry.getKey(), entry.getValue())); } return sortedHosts; } }
HostCandidateIterator { public static TreeSet<WithScore<HostState>> generateSortedHosts( HostManager hostManager, List<JobTask> tasks, Map<HostState, Double> scoreMap, int taskScoreIncrement) { TreeSet<WithScore<HostState>> sortedHosts = new TreeSet<>(hostAndScoreComparator); Map<HostState, Double> copyOfScoreMap = new HashMap<>(scoreMap); if(tasks != null) { for (JobTask task : tasks) { for (String replicaHostId : task.getAllTaskHosts()) { HostState host = hostManager.getHostState(replicaHostId); if(host != null) { Double score = copyOfScoreMap.getOrDefault(host, 0d); copyOfScoreMap.put(host, score + (double) taskScoreIncrement); } } } } for (Map.Entry<HostState, Double> entry : copyOfScoreMap.entrySet()) { sortedHosts.add(new WithScore<>(entry.getKey(), entry.getValue())); } return sortedHosts; } HostCandidateIterator(Spawn spawn, List<JobTask> tasks, Map<HostState, Double> scoreMap); }
HostCandidateIterator { public static TreeSet<WithScore<HostState>> generateSortedHosts( HostManager hostManager, List<JobTask> tasks, Map<HostState, Double> scoreMap, int taskScoreIncrement) { TreeSet<WithScore<HostState>> sortedHosts = new TreeSet<>(hostAndScoreComparator); Map<HostState, Double> copyOfScoreMap = new HashMap<>(scoreMap); if(tasks != null) { for (JobTask task : tasks) { for (String replicaHostId : task.getAllTaskHosts()) { HostState host = hostManager.getHostState(replicaHostId); if(host != null) { Double score = copyOfScoreMap.getOrDefault(host, 0d); copyOfScoreMap.put(host, score + (double) taskScoreIncrement); } } } } for (Map.Entry<HostState, Double> entry : copyOfScoreMap.entrySet()) { sortedHosts.add(new WithScore<>(entry.getKey(), entry.getValue())); } return sortedHosts; } 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 static TreeSet<WithScore<HostState>> generateSortedHosts( HostManager hostManager, List<JobTask> tasks, Map<HostState, Double> scoreMap, int taskScoreIncrement) { TreeSet<WithScore<HostState>> sortedHosts = new TreeSet<>(hostAndScoreComparator); Map<HostState, Double> copyOfScoreMap = new HashMap<>(scoreMap); if(tasks != null) { for (JobTask task : tasks) { for (String replicaHostId : task.getAllTaskHosts()) { HostState host = hostManager.getHostState(replicaHostId); if(host != null) { Double score = copyOfScoreMap.getOrDefault(host, 0d); copyOfScoreMap.put(host, score + (double) taskScoreIncrement); } } } } for (Map.Entry<HostState, Double> entry : copyOfScoreMap.entrySet()) { sortedHosts.add(new WithScore<>(entry.getKey(), entry.getValue())); } return sortedHosts; } 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);  }