Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rebalancing-tasks bug and added tests #900

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ public Map<String, Set<DatastreamTask>> assign(List<DatastreamGroup> datastreams
int minTasksPerInstance = tasksTotal / instances.size();

// some rebalance to increase the task count in instances below the minTasksPerInstance
while (newAssignment.get(instancesBySize.get(0)).size() < minTasksPerInstance) {
while (newAssignment.get(instancesBySize.get(0)).size() + _imbalanceThreshold < newAssignment.get(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot to parse in one line. Consider pulling out into a couple vars so the logic is more obvious.

instancesBySize.get(instancesBySize.size() - 1)).size()) {
String smallInstance = instancesBySize.get(0);
String largeInstance = instancesBySize.get(instancesBySize.size() - 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -499,6 +501,80 @@ public void testExtraTasksAreNotAssignedDuringReassignment() {
}
}

@Test
public void testReBalancingTasksWithThreshold() {
String[] instances = new String[]{"instance1", "instance2", "instance3"};
reBalancingTasksWithThresholdHelper(instances, new int[]{9, 9, 13}, 1);
shrinandthakkar marked this conversation as resolved.
Show resolved Hide resolved
reBalancingTasksWithThresholdHelper(instances, new int[]{9, 9, 13}, 2);
reBalancingTasksWithThresholdHelper(instances, new int[]{9, 9, 13}, 3);

reBalancingTasksWithThresholdHelper(instances, new int[]{1, 10, 20}, 1);
reBalancingTasksWithThresholdHelper(instances, new int[]{1, 10, 20}, 2);
reBalancingTasksWithThresholdHelper(instances, new int[]{1, 10, 20}, 3);

reBalancingTasksWithThresholdHelper(instances, new int[]{5, 5, 5}, 1);
reBalancingTasksWithThresholdHelper(instances, new int[]{5, 5, 5}, 2);
reBalancingTasksWithThresholdHelper(instances, new int[]{5, 5, 5}, 3);

instances = new String[]{"instance1", "instance2"};
reBalancingTasksWithThresholdHelper(instances, new int[]{9, 13}, 1);
reBalancingTasksWithThresholdHelper(instances, new int[]{9, 13}, 2);
reBalancingTasksWithThresholdHelper(instances, new int[]{9, 13}, 3);

reBalancingTasksWithThresholdHelper(instances, new int[]{1, 10}, 1);
reBalancingTasksWithThresholdHelper(instances, new int[]{1, 10}, 2);
reBalancingTasksWithThresholdHelper(instances, new int[]{1, 10}, 3);

reBalancingTasksWithThresholdHelper(instances, new int[]{5, 5}, 1);
reBalancingTasksWithThresholdHelper(instances, new int[]{5, 5}, 2);
reBalancingTasksWithThresholdHelper(instances, new int[]{5, 5}, 3);

instances = new String[]{"instance1"};
reBalancingTasksWithThresholdHelper(instances, new int[]{9}, 1);
reBalancingTasksWithThresholdHelper(instances, new int[]{9}, 2);
reBalancingTasksWithThresholdHelper(instances, new int[]{9}, 3);
}

// this helper function tests the rebalancing of tasks with an imbalance threshold across the instances
// with StickyMulticastStrategy
private void reBalancingTasksWithThresholdHelper(String[] instances, int[] taskDistribution, int imbalanceThreshold) {
List<DatastreamGroup> datastreams = generateDatastreams("ds", 1);
StickyMulticastStrategy strategy = new StickyMulticastStrategy(Optional.empty(), imbalanceThreshold);

// create a dummy current assignment following the parameterized task distribution; based on which the
// new assignment will be generated.
HashMap<String, Set<DatastreamTask>> currentDummyAssignment = new HashMap<>();
for (int index = 0; index < instances.length; index += 1) {
currentDummyAssignment.put(instances[index],
getDummyTasksSet(taskDistribution[index], datastreams.get(0).getDatastreams()));
}

int totalNumberTasks = currentDummyAssignment.values().stream().mapToInt(Collection::size).sum();
// setting the total count of tasks as the max tasks for our single datastream, so that the new assignment
// distribution looks similar to the current assignment.
datastreams.get(0).getDatastreams().get(0).getMetadata().put(CFG_MAX_TASKS, Integer.toString(totalNumberTasks));

Map<String, Set<DatastreamTask>> assignment =
strategy.assign(datastreams, Arrays.asList(instances), currentDummyAssignment);

Arrays.sort(instances, Comparator.comparing(x -> assignment.get(x).size()));

int minTasksAssignedToInstance = assignment.get(instances[0]).size();
int maxTasksAssignedToInstance = assignment.get(instances[instances.length - 1]).size();

Assert.assertTrue(maxTasksAssignedToInstance - minTasksAssignedToInstance <= imbalanceThreshold);
}

// returns a dummy set with #numTasks tasks
private HashSet<DatastreamTask> getDummyTasksSet(int numTasks, List<Datastream> datastreams) {
HashSet<DatastreamTask> assignedTasks = new HashSet<>();
while (numTasks > 0) {
assignedTasks.add(new DatastreamTaskImpl(datastreams));
numTasks -= 1;
}
return assignedTasks;
}

private static String assignmentToString(Map<String, Set<DatastreamTask>> assignment) {
StringBuilder builder = new StringBuilder();
assignment.keySet().stream().sorted().forEach(instance -> {
Expand Down