sebastienviale commented on code in PR #22725:
URL: https://github.com/apache/kafka/pull/22725#discussion_r3615119347


##########
streams/test-utils/src/main/java/org/apache/kafka/streams/MultiPartitionRuntime.java:
##########
@@ -0,0 +1,579 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.streams;
+
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.MockConsumer;
+import org.apache.kafka.clients.consumer.OffsetAndMetadata;
+import org.apache.kafka.clients.producer.MockProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.header.Headers;
+import org.apache.kafka.common.header.internals.RecordHeaders;
+import org.apache.kafka.common.record.TimestampType;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.common.utils.internals.LogContext;
+import org.apache.kafka.streams.TopologyConfig.TaskConfig;
+import org.apache.kafka.streams.processor.StateStore;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.GlobalStateManager;
+import org.apache.kafka.streams.processor.internals.InternalProcessorContext;
+import org.apache.kafka.streams.processor.internals.InternalTopologyBuilder;
+import org.apache.kafka.streams.processor.internals.ProcessorContextImpl;
+import org.apache.kafka.streams.processor.internals.ProcessorRecordContext;
+import org.apache.kafka.streams.processor.internals.ProcessorStateManager;
+import org.apache.kafka.streams.processor.internals.ProcessorTopology;
+import org.apache.kafka.streams.processor.internals.RecordCollector;
+import org.apache.kafka.streams.processor.internals.RecordCollectorImpl;
+import org.apache.kafka.streams.processor.internals.StateDirectory;
+import org.apache.kafka.streams.processor.internals.StreamTask;
+import org.apache.kafka.streams.processor.internals.StreamsProducer;
+import org.apache.kafka.streams.processor.internals.Task;
+import org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl;
+import org.apache.kafka.streams.processor.internals.metrics.TaskMetrics;
+import org.apache.kafka.streams.state.internals.ThreadCache;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Owns the multi-partition task graph and record routing for {@link 
TopologyTestDriver}. It builds
+ * one {@link StreamTask} per {@code (subtopologyId, partition)} pair from a
+ * {@link MultiPartitionTopologyPlan} and drives processing, punctuation, 
state-store access and
+ * shutdown of those tasks. It collaborates with the driver through the {@link 
Host} callbacks for
+ * the work that remains the driver's responsibility (transactional commit, 
global-state updates,
+ * global-partition lookups and recording output records).
+ */
+final class MultiPartitionRuntime {
+
+    private static final Logger log = 
LoggerFactory.getLogger(MultiPartitionRuntime.class);
+
+    private final MultiPartitionTopologyPlan plan;
+    private final InternalTopologyBuilder internalTopologyBuilder;
+    private final MockConsumer<byte[], byte[]> consumer;
+    private final MockProducer<byte[], byte[]> producer;
+    private final StreamsProducer testDriverProducer;
+    private final GlobalStateManager globalStateManager;
+    private final StreamsConfig streamsConfig;
+    private final TaskConfig taskConfig;
+    private final StreamsMetricsImpl streamsMetrics;
+    private final ThreadCache cache;
+    private final StateDirectory stateDirectory;
+    private final LogContext logContext;
+    private final Time wallClockTime;
+    private final Host host;
+
+    private final TreeMap<TaskId, StreamTask> tasks = new TreeMap<>();
+    private final Map<TopicPartition, TaskId> taskByTopicPartition = new 
HashMap<>();
+    private final Map<String, Map<Integer, Queue<ProducerRecord<byte[], 
byte[]>>>> outputByTopicPartition = new HashMap<>();
+    private final Map<String, Integer> nullKeyRoundRobinByTopic = new 
HashMap<>();
+    private final Map<TopicPartition, AtomicLong> offsets = new HashMap<>();
+
+    interface Host {
+        void commit(Map<TopicPartition, OffsetAndMetadata> offsets);
+        void processGlobalRecord(TopicPartition partition, long timestamp, 
byte[] key, byte[] value, Headers headers);
+        TopicPartition globalPartitionOrNull(String topic);
+        void recordOutput(String topic, ProducerRecord<byte[], byte[]> record);
+    }
+
+    MultiPartitionRuntime(final MultiPartitionTopologyPlan plan,
+                          final InternalTopologyBuilder 
internalTopologyBuilder,
+                          final MockConsumer<byte[], byte[]> consumer,
+                          final MockProducer<byte[], byte[]> producer,
+                          final StreamsProducer testDriverProducer,
+                          final GlobalStateManager globalStateManager,
+                          final StreamsConfig streamsConfig,
+                          final TaskConfig taskConfig,
+                          final StreamsMetricsImpl streamsMetrics,
+                          final ThreadCache cache,
+                          final StateDirectory stateDirectory,
+                          final LogContext logContext,
+                          final Time wallClockTime,
+                          final Host host) {
+        this.plan = plan;
+        this.internalTopologyBuilder = internalTopologyBuilder;
+        this.consumer = consumer;
+        this.producer = producer;
+        this.testDriverProducer = testDriverProducer;
+        this.globalStateManager = globalStateManager;
+        this.streamsConfig = streamsConfig;
+        this.taskConfig = taskConfig;
+        this.streamsMetrics = streamsMetrics;
+        this.cache = cache;
+        this.stateDirectory = stateDirectory;
+        this.logContext = logContext;
+        this.wallClockTime = wallClockTime;
+        this.host = host;
+    }
+
+    /**
+     * Build one {@link StreamTask} per {@code (subtopologyId, partition)} 
pair using the structures
+     * computed by the plan. All tasks share the driver's single {@link 
#consumer} and the
+     * {@link #testDriverProducer} as their record collector's producer.
+     */
+    void build() {
+        final List<TopicPartition> allSourcePartitions = new ArrayList<>();
+        final String threadId = Thread.currentThread().getName();
+
+        for (final int sid : plan.subtopologyIds()) {
+            final ProcessorTopology pt = plan.subtopology(sid);
+            if (pt.sourceTopics().isEmpty()) {
+                continue;
+            }
+            final int numPartitions = plan.partitionsOfSubtopology(sid);
+
+            // Register an offset counter for every (source-topic, partition) 
the sub-topology consumes.
+            for (final String src : pt.sourceTopics()) {
+                final int n = plan.partitionsOfTopic(src);
+                for (int p = 0; p < n; p++) {
+                    final TopicPartition tp = new TopicPartition(src, p);
+                    offsets.putIfAbsent(tp, new AtomicLong());
+                    allSourcePartitions.add(tp);
+                }
+            }
+
+            for (int p = 0; p < numPartitions; p++) {
+                // Build a fresh ProcessorTopology per task: ProcessorNode 
state (sources, processors,
+                // store handles) is single-init and would otherwise throw 
"The processor is not closed"
+                // when the second task tries to initialize the same instance.
+                final ProcessorTopology freshPt = 
internalTopologyBuilder.buildSubtopology(sid);
+                buildOneTask(sid, p, freshPt, threadId);
+            }
+        }
+
+        if (!allSourcePartitions.isEmpty()) {
+            consumer.assign(allSourcePartitions);
+            final Map<TopicPartition, Long> startOffsets = new HashMap<>();
+            for (final TopicPartition tp : allSourcePartitions) {
+                startOffsets.put(tp, 0L);
+            }
+            consumer.updateBeginningOffsets(startOffsets);
+            consumer.updateEndOffsets(startOffsets);
+        }
+    }
+
+    private void buildOneTask(final int sid,
+                              final int partition,
+                              final ProcessorTopology pt,
+                              final String threadId) {
+        final TaskId taskId = new TaskId(sid, partition);
+        TaskMetrics.droppedRecordsSensor(threadId, taskId.toString(), 
streamsMetrics);
+
+        // This task owns partition {@code p} of each source topic that has at 
least p+1 partitions.
+        final Set<TopicPartition> inputPartitions = new HashSet<>();
+        for (final String src : pt.sourceTopics()) {
+            final int n = plan.partitionsOfTopic(src);
+            if (partition < n) {
+                final TopicPartition tp = new TopicPartition(src, partition);
+                inputPartitions.add(tp);
+                taskByTopicPartition.put(tp, taskId);
+            }
+        }
+        if (inputPartitions.isEmpty()) {
+            return;
+        }
+
+        final ProcessorStateManager stateManager = new ProcessorStateManager(
+            taskId,
+            Task.TaskType.ACTIVE,
+            
StreamsConfig.EXACTLY_ONCE_V2.equals(streamsConfig.getString(StreamsConfig.PROCESSING_GUARANTEE_CONFIG)),
+            
streamsConfig.getBoolean(StreamsConfig.TRANSACTIONAL_STATE_STORES_CONFIG),
+            logContext,
+            stateDirectory,
+            pt.storeToChangelogTopic(),
+            new HashSet<>(inputPartitions));
+        final RecordCollector recordCollector = new RecordCollectorImpl(
+            logContext,
+            taskId,
+            testDriverProducer,
+            streamsConfig.productionExceptionHandler(),
+            streamsMetrics,
+            pt
+        );
+        final InternalProcessorContext<?, ?> context = new 
ProcessorContextImpl(
+            taskId,
+            streamsConfig,
+            stateManager,
+            streamsMetrics,
+            cache
+        );
+        final StreamTask task = new StreamTask(
+            taskId,
+            new HashSet<>(inputPartitions),
+            pt,
+            consumer,
+            taskConfig,
+            streamsMetrics,
+            stateDirectory,
+            cache,
+            wallClockTime,
+            stateManager,
+            recordCollector,
+            context,
+            logContext,
+            false
+        );
+        task.initializeIfNeeded();
+        task.completeRestoration(noOpResetter -> { });
+        task.processorContext().setRecordContext(null);
+        tasks.put(taskId, task);
+    }
+
+    /**
+     * Resolve the partition a record routes to.
+     * Explicit partition wins; otherwise {@code 
Utils.toPositive(Utils.murmur2(keyBytes)) % n} matches
+     * {@code BuiltInPartitioner.partitionForKey}; null key or n == 1 routes 
to partition 0.
+     */
+    private int resolvePartition(final String topic, final byte[] keyBytes, 
final int explicit) {
+        final int n = Math.max(1, plan.partitionsOfTopic(topic));
+        // A negative explicit partition is the "unset" sentinel (TestRecord 
default): route by key instead.
+        if (explicit >= 0) {
+            if (explicit >= n) {
+                throw new IllegalArgumentException(
+                    "Partition " + explicit + " is out of range for topic '" + 
topic
+                        + "' (has " + n + " partitions). Declare a higher 
count via declareTopic() if needed.");
+            }
+            return explicit;
+        }
+        if (n == 1) {
+            return 0;
+        }
+        if (keyBytes == null) {
+            // Distribute null-key records round-robin across the topic's 
partitions.
+            final int count = nullKeyRoundRobinByTopic.merge(topic, 1, 
Integer::sum);
+            return (count - 1) % n;
+        }
+        return Utils.toPositive(Utils.murmur2(keyBytes)) % n;
+    }
+
+    /**
+     * Multi-sub-topology pipe path. Routes the record to the task owning the 
resolved
+     * (topic, partition) and drains every task to quiescence before returning.
+     */
+    void pipeRecord(final String topicName,
+                    final long timestamp,
+                    final byte[] key,
+                    final byte[] value,
+                    final Headers headers,
+                    final int explicitPartition) {
+        final boolean isTaskInput = (plan.subtopologyForInputTopic(topicName) 
!= null);
+        final TopicPartition globalPartition = 
host.globalPartitionOrNull(topicName);
+        final boolean isGlobal = globalPartition != null;
+        if (!isTaskInput && !isGlobal) {
+            throw new IllegalArgumentException("Unknown topic: " + topicName);
+        }
+        if (isTaskInput) {
+            final int partition = resolvePartition(topicName, key, 
explicitPartition);
+            enqueueTaskRecord(topicName, new TopicPartition(topicName, 
partition),
+                timestamp, key, value, headers);
+            completeAllProcessableWork();
+        }
+        if (isGlobal) {
+            host.processGlobalRecord(globalPartition, timestamp, key, value, 
headers);
+        }
+    }
+
+    private void enqueueTaskRecord(final String topic,
+                                   final TopicPartition tp,
+                                   final long timestamp,
+                                   final byte[] key,
+                                   final byte[] value,
+                                   final Headers headers) {
+        final TaskId taskId = taskByTopicPartition.get(tp);
+        if (taskId == null) {
+            throw new IllegalStateException(
+                "No task owns " + tp + ". This typically means init() was not 
called or the topic "
+                    + "was not declared with enough partitions.");
+        }
+        final StreamTask owner = tasks.get(taskId);
+        if (owner == null) {
+            throw new IllegalStateException("Task " + taskId + " is registered 
but no StreamTask exists for it.");
+        }
+        final long offset = offsets
+            .computeIfAbsent(tp, k -> new AtomicLong())
+            .getAndIncrement();
+        owner.addRecords(tp, Collections.singleton(new ConsumerRecord<>(
+            topic, tp.partition(), offset, timestamp, 
TimestampType.CREATE_TIME,
+            key == null ? ConsumerRecord.NULL_SIZE : key.length,
+            value == null ? ConsumerRecord.NULL_SIZE : value.length,
+            key, value, headers, Optional.empty())));
+    }
+
+    /**
+     * Drain every multi-sub-topology task to quiescence, picking the 
processable task with the lowest
+     * current stream time on each iteration to mirror {@code PartitionGroup} 
ordering across tasks.
+     */
+    void completeAllProcessableWork() {
+        captureOutputs();
+        if (tasks.isEmpty()) {

Review Comment:
   Indeed, no reason to be empty



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/MultiPartitionRuntime.java:
##########
@@ -0,0 +1,579 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.streams;
+
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.MockConsumer;
+import org.apache.kafka.clients.consumer.OffsetAndMetadata;
+import org.apache.kafka.clients.producer.MockProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.header.Headers;
+import org.apache.kafka.common.header.internals.RecordHeaders;
+import org.apache.kafka.common.record.TimestampType;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.common.utils.internals.LogContext;
+import org.apache.kafka.streams.TopologyConfig.TaskConfig;
+import org.apache.kafka.streams.processor.StateStore;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.GlobalStateManager;
+import org.apache.kafka.streams.processor.internals.InternalProcessorContext;
+import org.apache.kafka.streams.processor.internals.InternalTopologyBuilder;
+import org.apache.kafka.streams.processor.internals.ProcessorContextImpl;
+import org.apache.kafka.streams.processor.internals.ProcessorRecordContext;
+import org.apache.kafka.streams.processor.internals.ProcessorStateManager;
+import org.apache.kafka.streams.processor.internals.ProcessorTopology;
+import org.apache.kafka.streams.processor.internals.RecordCollector;
+import org.apache.kafka.streams.processor.internals.RecordCollectorImpl;
+import org.apache.kafka.streams.processor.internals.StateDirectory;
+import org.apache.kafka.streams.processor.internals.StreamTask;
+import org.apache.kafka.streams.processor.internals.StreamsProducer;
+import org.apache.kafka.streams.processor.internals.Task;
+import org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl;
+import org.apache.kafka.streams.processor.internals.metrics.TaskMetrics;
+import org.apache.kafka.streams.state.internals.ThreadCache;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Owns the multi-partition task graph and record routing for {@link 
TopologyTestDriver}. It builds
+ * one {@link StreamTask} per {@code (subtopologyId, partition)} pair from a
+ * {@link MultiPartitionTopologyPlan} and drives processing, punctuation, 
state-store access and
+ * shutdown of those tasks. It collaborates with the driver through the {@link 
Host} callbacks for
+ * the work that remains the driver's responsibility (transactional commit, 
global-state updates,
+ * global-partition lookups and recording output records).
+ */
+final class MultiPartitionRuntime {
+
+    private static final Logger log = 
LoggerFactory.getLogger(MultiPartitionRuntime.class);
+
+    private final MultiPartitionTopologyPlan plan;
+    private final InternalTopologyBuilder internalTopologyBuilder;
+    private final MockConsumer<byte[], byte[]> consumer;
+    private final MockProducer<byte[], byte[]> producer;
+    private final StreamsProducer testDriverProducer;
+    private final GlobalStateManager globalStateManager;
+    private final StreamsConfig streamsConfig;
+    private final TaskConfig taskConfig;
+    private final StreamsMetricsImpl streamsMetrics;
+    private final ThreadCache cache;
+    private final StateDirectory stateDirectory;
+    private final LogContext logContext;
+    private final Time wallClockTime;
+    private final Host host;
+
+    private final TreeMap<TaskId, StreamTask> tasks = new TreeMap<>();
+    private final Map<TopicPartition, TaskId> taskByTopicPartition = new 
HashMap<>();
+    private final Map<String, Map<Integer, Queue<ProducerRecord<byte[], 
byte[]>>>> outputByTopicPartition = new HashMap<>();
+    private final Map<String, Integer> nullKeyRoundRobinByTopic = new 
HashMap<>();
+    private final Map<TopicPartition, AtomicLong> offsets = new HashMap<>();
+
+    interface Host {
+        void commit(Map<TopicPartition, OffsetAndMetadata> offsets);
+        void processGlobalRecord(TopicPartition partition, long timestamp, 
byte[] key, byte[] value, Headers headers);
+        TopicPartition globalPartitionOrNull(String topic);
+        void recordOutput(String topic, ProducerRecord<byte[], byte[]> record);
+    }
+
+    MultiPartitionRuntime(final MultiPartitionTopologyPlan plan,
+                          final InternalTopologyBuilder 
internalTopologyBuilder,
+                          final MockConsumer<byte[], byte[]> consumer,
+                          final MockProducer<byte[], byte[]> producer,
+                          final StreamsProducer testDriverProducer,
+                          final GlobalStateManager globalStateManager,
+                          final StreamsConfig streamsConfig,
+                          final TaskConfig taskConfig,
+                          final StreamsMetricsImpl streamsMetrics,
+                          final ThreadCache cache,
+                          final StateDirectory stateDirectory,
+                          final LogContext logContext,
+                          final Time wallClockTime,
+                          final Host host) {
+        this.plan = plan;
+        this.internalTopologyBuilder = internalTopologyBuilder;
+        this.consumer = consumer;
+        this.producer = producer;
+        this.testDriverProducer = testDriverProducer;
+        this.globalStateManager = globalStateManager;
+        this.streamsConfig = streamsConfig;
+        this.taskConfig = taskConfig;
+        this.streamsMetrics = streamsMetrics;
+        this.cache = cache;
+        this.stateDirectory = stateDirectory;
+        this.logContext = logContext;
+        this.wallClockTime = wallClockTime;
+        this.host = host;
+    }
+
+    /**
+     * Build one {@link StreamTask} per {@code (subtopologyId, partition)} 
pair using the structures
+     * computed by the plan. All tasks share the driver's single {@link 
#consumer} and the
+     * {@link #testDriverProducer} as their record collector's producer.
+     */
+    void build() {
+        final List<TopicPartition> allSourcePartitions = new ArrayList<>();
+        final String threadId = Thread.currentThread().getName();
+
+        for (final int sid : plan.subtopologyIds()) {
+            final ProcessorTopology pt = plan.subtopology(sid);
+            if (pt.sourceTopics().isEmpty()) {
+                continue;
+            }
+            final int numPartitions = plan.partitionsOfSubtopology(sid);
+
+            // Register an offset counter for every (source-topic, partition) 
the sub-topology consumes.
+            for (final String src : pt.sourceTopics()) {
+                final int n = plan.partitionsOfTopic(src);
+                for (int p = 0; p < n; p++) {
+                    final TopicPartition tp = new TopicPartition(src, p);
+                    offsets.putIfAbsent(tp, new AtomicLong());
+                    allSourcePartitions.add(tp);
+                }
+            }
+
+            for (int p = 0; p < numPartitions; p++) {
+                // Build a fresh ProcessorTopology per task: ProcessorNode 
state (sources, processors,
+                // store handles) is single-init and would otherwise throw 
"The processor is not closed"
+                // when the second task tries to initialize the same instance.
+                final ProcessorTopology freshPt = 
internalTopologyBuilder.buildSubtopology(sid);
+                buildOneTask(sid, p, freshPt, threadId);
+            }
+        }
+
+        if (!allSourcePartitions.isEmpty()) {
+            consumer.assign(allSourcePartitions);
+            final Map<TopicPartition, Long> startOffsets = new HashMap<>();
+            for (final TopicPartition tp : allSourcePartitions) {
+                startOffsets.put(tp, 0L);
+            }
+            consumer.updateBeginningOffsets(startOffsets);
+            consumer.updateEndOffsets(startOffsets);
+        }
+    }
+
+    private void buildOneTask(final int sid,
+                              final int partition,
+                              final ProcessorTopology pt,
+                              final String threadId) {
+        final TaskId taskId = new TaskId(sid, partition);
+        TaskMetrics.droppedRecordsSensor(threadId, taskId.toString(), 
streamsMetrics);
+
+        // This task owns partition {@code p} of each source topic that has at 
least p+1 partitions.
+        final Set<TopicPartition> inputPartitions = new HashSet<>();
+        for (final String src : pt.sourceTopics()) {
+            final int n = plan.partitionsOfTopic(src);
+            if (partition < n) {
+                final TopicPartition tp = new TopicPartition(src, partition);
+                inputPartitions.add(tp);
+                taskByTopicPartition.put(tp, taskId);
+            }
+        }
+        if (inputPartitions.isEmpty()) {
+            return;
+        }
+
+        final ProcessorStateManager stateManager = new ProcessorStateManager(
+            taskId,
+            Task.TaskType.ACTIVE,
+            
StreamsConfig.EXACTLY_ONCE_V2.equals(streamsConfig.getString(StreamsConfig.PROCESSING_GUARANTEE_CONFIG)),
+            
streamsConfig.getBoolean(StreamsConfig.TRANSACTIONAL_STATE_STORES_CONFIG),
+            logContext,
+            stateDirectory,
+            pt.storeToChangelogTopic(),
+            new HashSet<>(inputPartitions));
+        final RecordCollector recordCollector = new RecordCollectorImpl(
+            logContext,
+            taskId,
+            testDriverProducer,
+            streamsConfig.productionExceptionHandler(),
+            streamsMetrics,
+            pt
+        );
+        final InternalProcessorContext<?, ?> context = new 
ProcessorContextImpl(
+            taskId,
+            streamsConfig,
+            stateManager,
+            streamsMetrics,
+            cache
+        );
+        final StreamTask task = new StreamTask(
+            taskId,
+            new HashSet<>(inputPartitions),
+            pt,
+            consumer,
+            taskConfig,
+            streamsMetrics,
+            stateDirectory,
+            cache,
+            wallClockTime,
+            stateManager,
+            recordCollector,
+            context,
+            logContext,
+            false
+        );
+        task.initializeIfNeeded();
+        task.completeRestoration(noOpResetter -> { });
+        task.processorContext().setRecordContext(null);
+        tasks.put(taskId, task);
+    }
+
+    /**
+     * Resolve the partition a record routes to.
+     * Explicit partition wins; otherwise {@code 
Utils.toPositive(Utils.murmur2(keyBytes)) % n} matches
+     * {@code BuiltInPartitioner.partitionForKey}; null key or n == 1 routes 
to partition 0.
+     */
+    private int resolvePartition(final String topic, final byte[] keyBytes, 
final int explicit) {
+        final int n = Math.max(1, plan.partitionsOfTopic(topic));
+        // A negative explicit partition is the "unset" sentinel (TestRecord 
default): route by key instead.
+        if (explicit >= 0) {
+            if (explicit >= n) {
+                throw new IllegalArgumentException(
+                    "Partition " + explicit + " is out of range for topic '" + 
topic
+                        + "' (has " + n + " partitions). Declare a higher 
count via declareTopic() if needed.");
+            }
+            return explicit;
+        }
+        if (n == 1) {
+            return 0;
+        }
+        if (keyBytes == null) {
+            // Distribute null-key records round-robin across the topic's 
partitions.
+            final int count = nullKeyRoundRobinByTopic.merge(topic, 1, 
Integer::sum);
+            return (count - 1) % n;
+        }
+        return Utils.toPositive(Utils.murmur2(keyBytes)) % n;
+    }
+
+    /**
+     * Multi-sub-topology pipe path. Routes the record to the task owning the 
resolved
+     * (topic, partition) and drains every task to quiescence before returning.
+     */
+    void pipeRecord(final String topicName,
+                    final long timestamp,
+                    final byte[] key,
+                    final byte[] value,
+                    final Headers headers,
+                    final int explicitPartition) {
+        final boolean isTaskInput = (plan.subtopologyForInputTopic(topicName) 
!= null);
+        final TopicPartition globalPartition = 
host.globalPartitionOrNull(topicName);
+        final boolean isGlobal = globalPartition != null;
+        if (!isTaskInput && !isGlobal) {

Review Comment:
   Good point. These two cases should be mutually exclusive. I will add a guard 
to fail fast if a topic is both an input topic and a global store topic.



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/MultiPartitionRuntime.java:
##########
@@ -0,0 +1,579 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.streams;
+
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.MockConsumer;
+import org.apache.kafka.clients.consumer.OffsetAndMetadata;
+import org.apache.kafka.clients.producer.MockProducer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.header.Headers;
+import org.apache.kafka.common.header.internals.RecordHeaders;
+import org.apache.kafka.common.record.TimestampType;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.common.utils.internals.LogContext;
+import org.apache.kafka.streams.TopologyConfig.TaskConfig;
+import org.apache.kafka.streams.processor.StateStore;
+import org.apache.kafka.streams.processor.TaskId;
+import org.apache.kafka.streams.processor.internals.GlobalStateManager;
+import org.apache.kafka.streams.processor.internals.InternalProcessorContext;
+import org.apache.kafka.streams.processor.internals.InternalTopologyBuilder;
+import org.apache.kafka.streams.processor.internals.ProcessorContextImpl;
+import org.apache.kafka.streams.processor.internals.ProcessorRecordContext;
+import org.apache.kafka.streams.processor.internals.ProcessorStateManager;
+import org.apache.kafka.streams.processor.internals.ProcessorTopology;
+import org.apache.kafka.streams.processor.internals.RecordCollector;
+import org.apache.kafka.streams.processor.internals.RecordCollectorImpl;
+import org.apache.kafka.streams.processor.internals.StateDirectory;
+import org.apache.kafka.streams.processor.internals.StreamTask;
+import org.apache.kafka.streams.processor.internals.StreamsProducer;
+import org.apache.kafka.streams.processor.internals.Task;
+import org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl;
+import org.apache.kafka.streams.processor.internals.metrics.TaskMetrics;
+import org.apache.kafka.streams.state.internals.ThreadCache;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Owns the multi-partition task graph and record routing for {@link 
TopologyTestDriver}. It builds
+ * one {@link StreamTask} per {@code (subtopologyId, partition)} pair from a
+ * {@link MultiPartitionTopologyPlan} and drives processing, punctuation, 
state-store access and
+ * shutdown of those tasks. It collaborates with the driver through the {@link 
Host} callbacks for
+ * the work that remains the driver's responsibility (transactional commit, 
global-state updates,
+ * global-partition lookups and recording output records).
+ */
+final class MultiPartitionRuntime {
+
+    private static final Logger log = 
LoggerFactory.getLogger(MultiPartitionRuntime.class);
+
+    private final MultiPartitionTopologyPlan plan;
+    private final InternalTopologyBuilder internalTopologyBuilder;
+    private final MockConsumer<byte[], byte[]> consumer;
+    private final MockProducer<byte[], byte[]> producer;
+    private final StreamsProducer testDriverProducer;
+    private final GlobalStateManager globalStateManager;
+    private final StreamsConfig streamsConfig;
+    private final TaskConfig taskConfig;
+    private final StreamsMetricsImpl streamsMetrics;
+    private final ThreadCache cache;
+    private final StateDirectory stateDirectory;
+    private final LogContext logContext;
+    private final Time wallClockTime;
+    private final Host host;
+
+    private final TreeMap<TaskId, StreamTask> tasks = new TreeMap<>();
+    private final Map<TopicPartition, TaskId> taskByTopicPartition = new 
HashMap<>();
+    private final Map<String, Map<Integer, Queue<ProducerRecord<byte[], 
byte[]>>>> outputByTopicPartition = new HashMap<>();
+    private final Map<String, Integer> nullKeyRoundRobinByTopic = new 
HashMap<>();
+    private final Map<TopicPartition, AtomicLong> offsets = new HashMap<>();
+
+    interface Host {
+        void commit(Map<TopicPartition, OffsetAndMetadata> offsets);
+        void processGlobalRecord(TopicPartition partition, long timestamp, 
byte[] key, byte[] value, Headers headers);
+        TopicPartition globalPartitionOrNull(String topic);
+        void recordOutput(String topic, ProducerRecord<byte[], byte[]> record);
+    }
+
+    MultiPartitionRuntime(final MultiPartitionTopologyPlan plan,
+                          final InternalTopologyBuilder 
internalTopologyBuilder,
+                          final MockConsumer<byte[], byte[]> consumer,
+                          final MockProducer<byte[], byte[]> producer,
+                          final StreamsProducer testDriverProducer,
+                          final GlobalStateManager globalStateManager,
+                          final StreamsConfig streamsConfig,
+                          final TaskConfig taskConfig,
+                          final StreamsMetricsImpl streamsMetrics,
+                          final ThreadCache cache,
+                          final StateDirectory stateDirectory,
+                          final LogContext logContext,
+                          final Time wallClockTime,
+                          final Host host) {
+        this.plan = plan;
+        this.internalTopologyBuilder = internalTopologyBuilder;
+        this.consumer = consumer;
+        this.producer = producer;
+        this.testDriverProducer = testDriverProducer;
+        this.globalStateManager = globalStateManager;
+        this.streamsConfig = streamsConfig;
+        this.taskConfig = taskConfig;
+        this.streamsMetrics = streamsMetrics;
+        this.cache = cache;
+        this.stateDirectory = stateDirectory;
+        this.logContext = logContext;
+        this.wallClockTime = wallClockTime;
+        this.host = host;
+    }
+
+    /**
+     * Build one {@link StreamTask} per {@code (subtopologyId, partition)} 
pair using the structures
+     * computed by the plan. All tasks share the driver's single {@link 
#consumer} and the
+     * {@link #testDriverProducer} as their record collector's producer.
+     */
+    void build() {
+        final List<TopicPartition> allSourcePartitions = new ArrayList<>();
+        final String threadId = Thread.currentThread().getName();
+
+        for (final int sid : plan.subtopologyIds()) {
+            final ProcessorTopology pt = plan.subtopology(sid);
+            if (pt.sourceTopics().isEmpty()) {
+                continue;
+            }
+            final int numPartitions = plan.partitionsOfSubtopology(sid);
+
+            // Register an offset counter for every (source-topic, partition) 
the sub-topology consumes.
+            for (final String src : pt.sourceTopics()) {
+                final int n = plan.partitionsOfTopic(src);
+                for (int p = 0; p < n; p++) {
+                    final TopicPartition tp = new TopicPartition(src, p);
+                    offsets.putIfAbsent(tp, new AtomicLong());
+                    allSourcePartitions.add(tp);
+                }
+            }
+
+            for (int p = 0; p < numPartitions; p++) {
+                // Build a fresh ProcessorTopology per task: ProcessorNode 
state (sources, processors,
+                // store handles) is single-init and would otherwise throw 
"The processor is not closed"
+                // when the second task tries to initialize the same instance.
+                final ProcessorTopology freshPt = 
internalTopologyBuilder.buildSubtopology(sid);
+                buildOneTask(sid, p, freshPt, threadId);
+            }
+        }
+
+        if (!allSourcePartitions.isEmpty()) {
+            consumer.assign(allSourcePartitions);
+            final Map<TopicPartition, Long> startOffsets = new HashMap<>();
+            for (final TopicPartition tp : allSourcePartitions) {
+                startOffsets.put(tp, 0L);
+            }
+            consumer.updateBeginningOffsets(startOffsets);
+            consumer.updateEndOffsets(startOffsets);
+        }
+    }
+
+    private void buildOneTask(final int sid,
+                              final int partition,
+                              final ProcessorTopology pt,
+                              final String threadId) {
+        final TaskId taskId = new TaskId(sid, partition);
+        TaskMetrics.droppedRecordsSensor(threadId, taskId.toString(), 
streamsMetrics);
+
+        // This task owns partition {@code p} of each source topic that has at 
least p+1 partitions.
+        final Set<TopicPartition> inputPartitions = new HashSet<>();
+        for (final String src : pt.sourceTopics()) {
+            final int n = plan.partitionsOfTopic(src);
+            if (partition < n) {
+                final TopicPartition tp = new TopicPartition(src, partition);
+                inputPartitions.add(tp);
+                taskByTopicPartition.put(tp, taskId);
+            }
+        }
+        if (inputPartitions.isEmpty()) {
+            return;
+        }
+
+        final ProcessorStateManager stateManager = new ProcessorStateManager(
+            taskId,
+            Task.TaskType.ACTIVE,
+            
StreamsConfig.EXACTLY_ONCE_V2.equals(streamsConfig.getString(StreamsConfig.PROCESSING_GUARANTEE_CONFIG)),
+            
streamsConfig.getBoolean(StreamsConfig.TRANSACTIONAL_STATE_STORES_CONFIG),
+            logContext,
+            stateDirectory,
+            pt.storeToChangelogTopic(),
+            new HashSet<>(inputPartitions));
+        final RecordCollector recordCollector = new RecordCollectorImpl(
+            logContext,
+            taskId,
+            testDriverProducer,
+            streamsConfig.productionExceptionHandler(),
+            streamsMetrics,
+            pt
+        );
+        final InternalProcessorContext<?, ?> context = new 
ProcessorContextImpl(
+            taskId,
+            streamsConfig,
+            stateManager,
+            streamsMetrics,
+            cache
+        );
+        final StreamTask task = new StreamTask(
+            taskId,
+            new HashSet<>(inputPartitions),
+            pt,
+            consumer,
+            taskConfig,
+            streamsMetrics,
+            stateDirectory,
+            cache,
+            wallClockTime,
+            stateManager,
+            recordCollector,
+            context,
+            logContext,
+            false
+        );
+        task.initializeIfNeeded();
+        task.completeRestoration(noOpResetter -> { });
+        task.processorContext().setRecordContext(null);
+        tasks.put(taskId, task);
+    }
+
+    /**
+     * Resolve the partition a record routes to.
+     * Explicit partition wins; otherwise {@code 
Utils.toPositive(Utils.murmur2(keyBytes)) % n} matches
+     * {@code BuiltInPartitioner.partitionForKey}; null key or n == 1 routes 
to partition 0.
+     */
+    private int resolvePartition(final String topic, final byte[] keyBytes, 
final int explicit) {
+        final int n = Math.max(1, plan.partitionsOfTopic(topic));
+        // A negative explicit partition is the "unset" sentinel (TestRecord 
default): route by key instead.
+        if (explicit >= 0) {
+            if (explicit >= n) {
+                throw new IllegalArgumentException(
+                    "Partition " + explicit + " is out of range for topic '" + 
topic
+                        + "' (has " + n + " partitions). Declare a higher 
count via declareTopic() if needed.");
+            }
+            return explicit;
+        }
+        if (n == 1) {
+            return 0;
+        }
+        if (keyBytes == null) {
+            // Distribute null-key records round-robin across the topic's 
partitions.
+            final int count = nullKeyRoundRobinByTopic.merge(topic, 1, 
Integer::sum);
+            return (count - 1) % n;
+        }
+        return Utils.toPositive(Utils.murmur2(keyBytes)) % n;
+    }
+
+    /**
+     * Multi-sub-topology pipe path. Routes the record to the task owning the 
resolved
+     * (topic, partition) and drains every task to quiescence before returning.
+     */
+    void pipeRecord(final String topicName,
+                    final long timestamp,
+                    final byte[] key,
+                    final byte[] value,
+                    final Headers headers,
+                    final int explicitPartition) {
+        final boolean isTaskInput = (plan.subtopologyForInputTopic(topicName) 
!= null);
+        final TopicPartition globalPartition = 
host.globalPartitionOrNull(topicName);
+        final boolean isGlobal = globalPartition != null;
+        if (!isTaskInput && !isGlobal) {
+            throw new IllegalArgumentException("Unknown topic: " + topicName);
+        }
+        if (isTaskInput) {
+            final int partition = resolvePartition(topicName, key, 
explicitPartition);
+            enqueueTaskRecord(topicName, new TopicPartition(topicName, 
partition),
+                timestamp, key, value, headers);
+            completeAllProcessableWork();
+        }
+        if (isGlobal) {

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to