sebastienviale commented on code in PR #22895: URL: https://github.com/apache/kafka/pull/22895#discussion_r3853962882
########## streams/test-utils/src/main/java/org/apache/kafka/streams/SinglePartitionRuntime.java: ########## @@ -0,0 +1,544 @@ +/* + * 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.internals.LogContext; +import org.apache.kafka.streams.errors.TopologyException; +import org.apache.kafka.streams.internals.Runtime; +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.InternalTopologyBuilder; +import org.apache.kafka.streams.processor.internals.InternalProcessorContext; +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.state.ReadOnlyKeyValueStore; +import org.apache.kafka.streams.state.ReadOnlySessionStore; +import org.apache.kafka.streams.state.ReadOnlyWindowStore; +import org.apache.kafka.streams.state.TimestampedKeyValueStore; +import org.apache.kafka.streams.state.TimestampedWindowStore; +import org.apache.kafka.streams.state.VersionedKeyValueStore; +import org.apache.kafka.streams.state.internals.ThreadCache; +import org.apache.kafka.streams.TopologyConfig.TaskConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicLong; +import java.util.regex.Pattern; + +/** + * The default {@link Runtime}: a single {@link StreamTask} for the whole topology, one partition + * per topic. + */ +final class SinglePartitionRuntime implements Runtime { + + private static final Logger log = LoggerFactory.getLogger(SinglePartitionRuntime.class); + + private final StreamTask task; // null if the topology has only global processing + private final InternalTopologyBuilder internalTopologyBuilder; + private final GlobalStateManager globalStateManager; + private final Map<String, TopicPartition> partitionsByInputTopic; + private final Map<TopicPartition, AtomicLong> offsetsByTopicOrPatternPartition; + private final MockProducer<byte[], byte[]> producer; + private final Time wallClockTime; + private final Runtime.Host host; + + SinglePartitionRuntime(final StreamTask task, + final InternalTopologyBuilder internalTopologyBuilder, + final GlobalStateManager globalStateManager, + final Map<String, TopicPartition> partitionsByInputTopic, + final Map<TopicPartition, AtomicLong> offsetsByTopicOrPatternPartition, + final MockProducer<byte[], byte[]> producer, + final Time wallClockTime, + final Host host) { + this.task = task; + this.internalTopologyBuilder = internalTopologyBuilder; + this.globalStateManager = globalStateManager; + this.partitionsByInputTopic = partitionsByInputTopic; + this.offsetsByTopicOrPatternPartition = offsetsByTopicOrPatternPartition; + this.producer = producer; + this.wallClockTime = wallClockTime; + this.host = host; + } + + @SuppressWarnings("checkstyle:ParameterNumber") + static SinglePartitionRuntime create(final StreamsConfig streamsConfig, + final StreamsMetricsImpl streamsMetrics, + final ThreadCache cache, + final TaskConfig taskConfig, + final TaskId taskId, + final ProcessorTopology processorTopology, + final InternalTopologyBuilder internalTopologyBuilder, + final GlobalStateManager globalStateManager, + final Map<String, TopicPartition> partitionsByInputTopic, + final Map<TopicPartition, AtomicLong> offsetsByTopicOrPatternPartition, + final MockConsumer<byte[], byte[]> consumer, + final StreamsProducer testDriverProducer, + final MockProducer<byte[], byte[]> producer, + final StateDirectory stateDirectory, + final Time wallClockTime, + final LogContext logContext, + final Host host) { + return createInternal( + streamsConfig, + streamsMetrics, + cache, + taskConfig, + taskId, + processorTopology, + internalTopologyBuilder, + globalStateManager, + partitionsByInputTopic, + offsetsByTopicOrPatternPartition, + consumer, + testDriverProducer, + producer, + stateDirectory, + wallClockTime, + logContext, + host + ); + } + + @SuppressWarnings("checkstyle:ParameterNumber") + private static SinglePartitionRuntime createInternal(final StreamsConfig streamsConfig, + final StreamsMetricsImpl streamsMetrics, + final ThreadCache cache, + final TaskConfig taskConfig, + final TaskId taskId, + final ProcessorTopology processorTopology, + final InternalTopologyBuilder internalTopologyBuilder, + final GlobalStateManager globalStateManager, + final Map<String, TopicPartition> partitionsByInputTopic, + final Map<TopicPartition, AtomicLong> offsetsByTopicOrPatternPartition, + final MockConsumer<byte[], byte[]> consumer, + final StreamsProducer testDriverProducer, + final MockProducer<byte[], byte[]> producer, + final StateDirectory stateDirectory, + final Time wallClockTime, + final LogContext logContext, + final Host host) { + return createFromContext(new SinglePartitionRuntimeContext( + streamsConfig, + streamsMetrics, + cache, + taskConfig, + taskId, + processorTopology, + internalTopologyBuilder, + globalStateManager, + partitionsByInputTopic, + offsetsByTopicOrPatternPartition, + consumer, + testDriverProducer, + producer, + stateDirectory, + wallClockTime, + logContext, + host + )); + } + + private static SinglePartitionRuntime createFromContext(final SinglePartitionRuntimeContext context) { + final StreamTask task = buildTask(context); + + return new SinglePartitionRuntime( + task, + context.internalTopologyBuilder, + context.globalStateManager, + context.partitionsByInputTopic, + context.offsetsByTopicOrPatternPartition, + context.producer, + context.wallClockTime, + context.host); + } + + private static StreamTask buildTask(final SinglePartitionRuntimeContext context) { + if (context.partitionsByInputTopic.isEmpty()) { + return null; + } + + context.consumer.assign(context.partitionsByInputTopic.values()); + final Map<TopicPartition, Long> startOffsets = new HashMap<>(); + for (final TopicPartition topicPartition : context.partitionsByInputTopic.values()) { + startOffsets.put(topicPartition, 0L); + } + context.consumer.updateBeginningOffsets(startOffsets); + + final ProcessorStateManager stateManager = new ProcessorStateManager( + context.taskId, + Task.TaskType.ACTIVE, + StreamsConfig.EXACTLY_ONCE_V2.equals(context.streamsConfig.getString(StreamsConfig.PROCESSING_GUARANTEE_CONFIG)), + context.streamsConfig.getBoolean(StreamsConfig.TRANSACTIONAL_STATE_STORES_CONFIG), + context.logContext, + context.stateDirectory, + context.wallClockTime, + context.processorTopology.storeToChangelogTopic(), + new HashSet<>(context.partitionsByInputTopic.values())); + + final RecordCollector recordCollector = new RecordCollectorImpl( + context.logContext, + context.taskId, + context.testDriverProducer, + context.streamsConfig.productionExceptionHandler(), + context.streamsMetrics, + context.processorTopology + ); + + final InternalProcessorContext<?, ?> processorContext = new ProcessorContextImpl( + context.taskId, + context.streamsConfig, + stateManager, + context.streamsMetrics, + context.cache + ); + + final StreamTask task = new StreamTask( + context.taskId, + new HashSet<>(context.partitionsByInputTopic.values()), + context.processorTopology, + context.consumer, + context.taskConfig, + context.streamsMetrics, + context.stateDirectory, + context.cache, + context.wallClockTime, + stateManager, + recordCollector, + processorContext, + context.logContext, + false + ); + task.initializeIfNeeded(); + task.completeRestoration(noOpResetter -> { }); + for (final TopicPartition tp : task.inputPartitions()) { + task.updateNextOffsets(tp, new OffsetAndMetadata(0, Optional.empty(), "")); + } + return task; + } + + @SuppressWarnings("checkstyle:ParameterNumber") + private static class SinglePartitionRuntimeContext { Review Comment: done, add them into constructor -- 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]
