Source:
<code>private List<DatastreamTask> getDatastreamTasks(List<Datastream> datastreams, Map<String, Set<DatastreamTask>> currentAssignment, int maxTasksPerDatastream) { Set<DatastreamTask> allTasks = new HashSet<>(); List<DatastreamTask> currentlyAssignedDatastreamTasks = new ArrayList<>(); currentAssignment.values().forEach(currentlyAssignedDatastreamTasks::addAll); for (Datastream datastream : datastreams) { Set<DatastreamTask> tasksForDatastream = currentlyAssignedDatastreamTasks.stream() .filter( x -> x.getDatastreams().stream().map(Datastream::getName).anyMatch(y -> y.contains(datastream.getName()))) .collect(Collectors.toSet()); if (tasksForDatastream.isEmpty()) { tasksForDatastream = createTasksForDatastream(datastream, maxTasksPerDatastream); } else { if (datastream.hasSource() && datastream.getSource().hasPartitions()) { int numberOfDatastreamPartitions = datastream.getSource().getPartitions(); Set<Integer> assignedPartitions = new HashSet<>(); int count = 0; for (DatastreamTask task : tasksForDatastream) { count += task.getPartitions().size(); assignedPartitions.addAll(task.getPartitions()); } if (count != numberOfDatastreamPartitions || assignedPartitions.size() != numberOfDatastreamPartitions) { LOG.error("Corrupted partition information for datastream {}. Expected number of partitions {}, actual {}: {}", datastream.getName(), numberOfDatastreamPartitions, count, assignedPartitions); <START> tasksForDatastream = createTasksForDatastream(datastream, maxTasksPerDatastream); <END> } } } allTasks.addAll(tasksForDatastream); } return new ArrayList<>(allTasks); }</code><technical_language>This is tricky, this resetting state of existing tasks well. if 8 tasks, task missing of some race in rebalancer. end resetting state for 8 tasks this result in lot of dup events in stream AFAIK. If case, for i recommend 1. Creating task missing. 2. throw yellow alert (Does EKG suffice @lhaiesp?) next day dev oncall a this problem</technical_language>

Target:
private List<DatastreamTask> getDatastreamTasks(List<Datastream> datastreams, Map<String, Set<DatastreamTask>> currentAssignment, int maxTasksPerDatastream) { Set<DatastreamTask> allTasks = new HashSet<>(); List<DatastreamTask> currentlyAssignedDatastreamTasks = new ArrayList<>(); currentAssignment.values().forEach(currentlyAssignedDatastreamTasks::addAll); for (Datastream datastream : datastreams) { Set<DatastreamTask> tasksForDatastream = currentlyAssignedDatastreamTasks.stream() .filter( x -> x.getDatastreams().stream().map(Datastream::getName).anyMatch(y -> y.contains(datastream.getName()))) .collect(Collectors.toSet()); if (tasksForDatastream.isEmpty()) { tasksForDatastream = createTasksForDatastream(datastream, maxTasksPerDatastream); } else { if (datastream.hasSource() && datastream.getSource().hasPartitions()) { int numberOfDatastreamPartitions = datastream.getSource().getPartitions(); Set<Integer> assignedPartitions = new HashSet<>(); int count = 0; for (DatastreamTask task : tasksForDatastream) { count += task.getPartitions().size(); assignedPartitions.addAll(task.getPartitions()); } if (count != numberOfDatastreamPartitions || assignedPartitions.size() != numberOfDatastreamPartitions) { LOG.error("Corrupted partition information for datastream {}. Expected number of partitions {}, actual {}: {}", datastream.getName(), numberOfDatastreamPartitions, count, assignedPartitions); } } } allTasks.addAll(tasksForDatastream); } return new ArrayList<>(allTasks); }