public Map<TaskId, Set<TopicPartition>> partitionGroups(final Map<Subtopology, Set<String>> sourceTopicGroups,
                                                            final Map<Subtopology, Set<String>> changelogTopicGroups,
                                                            final Map<TaskId, Set<TopicPartition>> changelogPartitionGroups,
                                                            final Cluster metadata) {
        final Map<TaskId, Set<TopicPartition>> sourcePartitionGroups = new HashMap<>();

        for (final Map.Entry<Subtopology, Set<String>> entry : sourceTopicGroups.entrySet()) {
            final Subtopology subtopology = entry.getKey();
            final Set<String> sourceTopicGroup = entry.getValue();
            final Set<String> changelogTopicGroup = changelogTopicGroups.getOrDefault(subtopology, new HashSet<>());

            final int maxNumPartitions = maxNumPartitions(metadata, sourceTopicGroup);

            for (int partitionId = 0; partitionId < maxNumPartitions; partitionId++) {
                final Set<TopicPartition> sourcePartitionGroup = new HashSet<>(sourceTopicGroup.size());
                final Set<TopicPartition> changelogPartitionGroup = new HashSet<>(changelogTopicGroup.size());

                for (final String topic : sourceTopicGroup) {
                    final List<PartitionInfo> partitions = metadata.partitionsForTopic(topic);
                    if (partitionId < partitions.size()) {
                        sourcePartitionGroup.add(new TopicPartition(topic, partitionId));
                    }
                }

                for (final String topic : changelogTopicGroup) {
                    changelogPartitionGroup.add(new TopicPartition(topic, partitionId));
                }

                final TaskId taskId = new TaskId(subtopology.nodeGroupId, partitionId, subtopology.namedTopology);
                sourcePartitionGroups.put(taskId, Collections.unmodifiableSet(sourcePartitionGroup));
                changelogPartitionGroups.put(taskId, Collections.unmodifiableSet(changelogPartitionGroup));
            }
        }

        return Collections.unmodifiableMap(sourcePartitionGroups);
    }
Generate tasks with the assigned topic partitions.

@param sourceTopicGroups        group of source topics that need to be joined together
@param changelogTopicGroups     group of changelog topics that need to be joined together
@param changelogPartitionGroups the output grouping of the changelog topic partitions by task
                                id. This input will be mutated.
@param metadata                 metadata of the consuming cluster
@return The map from generated task ids to the assigned partitions
streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGrouper.java