je-ik commented on code in PR #39546:
URL: https://github.com/apache/beam/pull/39546#discussion_r3688872876
##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/FlattenProcessor.java:
##########
@@ -99,8 +99,7 @@ public void process(Record<byte[], KStreamsPayload<?>>
record) {
Instant advanced = watermarkAggregator.advance();
if (advanced.isAfter(lastForwardedWatermark)) {
lastForwardedWatermark = advanced;
- // Stamped with this Flatten's own transform id; Flatten is a single
instance for now, so the
- // report is for its only partition (0 of 1).
+ // Stamped as the only source a consumer will see; a shuffle downstream
restamps.
Review Comment:
The "stamp" here resembles "timestamp", is this more a "marker"?
##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/KafkaStreamsPipelineOptions.java:
##########
@@ -55,6 +55,21 @@ public interface KafkaStreamsPipelineOptions extends
PortablePipelineOptions {
void setMaxBundleTimeMs(int maxBundleTimeMs);
+ @Description(
+ "Number of partitions for the topics the runner creates for a pipeline
(the bootstrap topic"
+ + " of each Impulse and Read, and the repartition topic of each
GroupByKey). This is the"
+ + " parallelism the shuffled parts of the pipeline can reach.")
+ @Default.Integer(1)
+ int getTopicPartitions();
+
+ void setTopicPartitions(int topicPartitions);
+
+ @Description("Replication factor for the topics the runner creates for a
pipeline.")
+ @Default.Short(1)
+ short getTopicReplicationFactor();
Review Comment:
We should probably enable a complete set of topic-level configurations here.
We can postpone it, but it would be good to create an issue. We might pass a
JSON-serialized dictionary and/or publish the most-common options as separate
methods.
##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/FlattenTranslator.java:
##########
@@ -70,6 +73,7 @@ public void translate(
String parentProcessor =
context.getProcessorNameForPCollection(inputPCollectionId);
parentProcessors.add(parentProcessor);
upstreamTransformIds.add(parentProcessor);
+ partitionCount = Math.max(partitionCount,
context.getPartitionCount(inputPCollectionId));
Review Comment:
Can `getPartitionCount` be zero or negative? Under which circumstances?
##########
runners/kafka-streams/src/test/java/org/apache/beam/runners/kafka/streams/translation/PortableWindowingStrategyTest.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.beam.runners.kafka.streams.translation;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import org.apache.beam.model.pipeline.v1.RunnerApi;
+import org.apache.beam.runners.kafka.streams.KafkaStreamsTestRunner;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.coders.StringUtf8Coder;
+import org.apache.beam.sdk.coders.VarIntCoder;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.GroupByKey;
+import org.apache.beam.sdk.transforms.Impulse;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.windowing.FixedWindows;
+import org.apache.beam.sdk.transforms.windowing.SlidingWindows;
+import org.apache.beam.sdk.transforms.windowing.Window;
+import org.apache.beam.sdk.transforms.windowing.WindowFn;
+import org.apache.beam.sdk.util.construction.PipelineTranslation;
+import org.apache.beam.sdk.util.construction.RehydratedComponents;
+import org.apache.beam.sdk.util.construction.WindowingStrategyTranslation;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.WindowingStrategy;
+import org.joda.time.Duration;
+import org.junit.Test;
+
+/**
+ * Checks that the runner's windowing is driven by the language-neutral
windowing strategy in the
+ * pipeline proto, not by anything specific to the Java SDK.
+ *
+ * <p>This matters because the runner executes GroupAlsoByWindow itself (see
{@link
+ * WindowedGroupByKeyProcessor}), so it has to reconstruct the WindowFn from
the proto. Beam encodes
+ * the standard WindowFns as a URN plus a parameter payload — {@code
+ * beam:window_fn:fixed_windows:v1} and friends — and only falls back to a
serialized Java object
+ * for a custom WindowFn. An SDK in another language emits exactly those same
standard URNs, so if
+ * the runner works from the URN form it works for any SDK, and if it had come
to depend on the
+ * Java-serialized form it would only ever have worked for Java.
+ *
+ * <p>These tests assert the proto a windowed pipeline produces carries the
standard URN, and that
+ * hydrating it back — which is what the translator does — yields the right
WindowFn. They do not
+ * replace running a pipeline from another SDK end to end, which additionally
exercises the coders
+ * and the SDK harness; that needs the job server against a real broker.
+ */
+public class PortableWindowingStrategyTest {
Review Comment:
The portable part here would be if we implemented own WindowFn, because that
would have to go through the SDK harness. Otherwise the "classic" windowFns can
be interpreted directly by the ReduceFnRunner.
##########
runners/kafka-streams/src/test/java/org/apache/beam/runners/kafka/streams/KafkaStreamsRunnerBrokerIT.java:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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.beam.runners.kafka.streams;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.nio.file.Files;
+import java.util.UUID;
+import org.apache.beam.model.pipeline.v1.RunnerApi;
+import org.apache.beam.runners.fnexecution.provisioning.JobInfo;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.PipelineResult;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.coders.StringUtf8Coder;
+import org.apache.beam.sdk.coders.VarIntCoder;
+import org.apache.beam.sdk.metrics.Counter;
+import org.apache.beam.sdk.metrics.MetricNameFilter;
+import org.apache.beam.sdk.metrics.MetricQueryResults;
+import org.apache.beam.sdk.metrics.MetricResult;
+import org.apache.beam.sdk.metrics.Metrics;
+import org.apache.beam.sdk.metrics.MetricsFilter;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.options.PortablePipelineOptions;
+import org.apache.beam.sdk.testing.CrashingRunner;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.GroupByKey;
+import org.apache.beam.sdk.transforms.Impulse;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.util.construction.Environments;
+import org.apache.beam.sdk.util.construction.PipelineOptionsTranslation;
+import org.apache.beam.sdk.util.construction.PipelineTranslation;
+import org.apache.beam.sdk.util.construction.SplittableParDo;
+import org.apache.beam.sdk.values.KV;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Iterables;
+import org.joda.time.Duration;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.testcontainers.containers.KafkaContainer;
+import org.testcontainers.utility.DockerImageName;
+
+/**
+ * Runs a pipeline through the production {@link KafkaStreamsPipelineRunner}
against a real Kafka
+ * broker, rather than through the {@code TopologyTestDriver} the rest of the
suite uses.
+ *
+ * <p>The test driver stands in for a broker well enough for translation and
windowing logic, but it
+ * runs one instance in one thread and fakes the topics. Everything that only
exists on a real
+ * cluster is untested by it: the runner creating its own bootstrap and
repartition topics, records
+ * actually round-tripping through a repartition topic, exactly-once
processing, the state stores'
+ * changelog, and the Kafka Streams application lifecycle. This test covers
that path.
+ *
+ * <p>It needs Docker and so is not part of the default build; the {@code
brokerIntegrationTest}
+ * Gradle task runs it.
+ */
+@RunWith(JUnit4.class)
+public class KafkaStreamsRunnerBrokerIT {
+
+ private static final String NAMESPACE = "brokerIT";
+ private static final String GROUPS_COUNTER = "groups";
+
+ /** How long to wait for the streaming application to work through the
pipeline. */
+ private static final Duration TIMEOUT = Duration.standardMinutes(2);
+
+ private static KafkaContainer kafka;
+
+ @BeforeClass
+ public static void startBroker() {
+ kafka = new
KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.6.1"));
Review Comment:
Could we use official Apache image?
##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/KafkaStreamsPipelineOptions.java:
##########
@@ -55,6 +55,21 @@ public interface KafkaStreamsPipelineOptions extends
PortablePipelineOptions {
void setMaxBundleTimeMs(int maxBundleTimeMs);
+ @Description(
+ "Number of partitions for the topics the runner creates for a pipeline
(the bootstrap topic"
+ + " of each Impulse and Read, and the repartition topic of each
GroupByKey). This is the"
+ + " parallelism the shuffled parts of the pipeline can reach.")
+ @Default.Integer(1)
+ int getTopicPartitions();
Review Comment:
This should be named so that it is clear from the command line where it will
be used. `--topicPartitions` is not too clear, how about
`--internalParallelism`?
##########
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/KafkaStreamsTopicManager.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.beam.runners.kafka.streams;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.apache.kafka.common.errors.TopicExistsException;
+import org.apache.kafka.streams.Topology;
+import org.apache.kafka.streams.TopologyDescription;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Creates the topics a translated pipeline needs before the Kafka Streams
application starts.
+ *
+ * <p>The runner shuffles data through topics it names itself: a bootstrap
topic per Impulse and per
+ * primitive Read, and a repartition topic per GroupByKey. Kafka Streams does
create the internal
+ * topics it manages on its own, but these are declared with explicit names
through {@code
+ * addSource} and {@code addSink}, so to Kafka Streams they are ordinary user
topics — it will not
+ * create them, and refuses to start with {@code MissingSourceTopicException}
if a source topic is
+ * absent. Relying on the broker's {@code auto.create.topics.enable} is not an
option either: it is
+ * off on many clusters, and a topic auto-created on first fetch gets the
broker's default partition
+ * count rather than the pipeline's.
+ *
+ * <p>Only topics carrying one of the runner's own prefixes are created. Any
other topic in the
+ * topology belongs to the user (a source or sink they named), and creating
those implicitly would
+ * hide a misconfiguration behind an empty topic.
+ */
+class KafkaStreamsTopicManager {
Review Comment:
I'm thinking if this should be implicit, or if we should add a life-cycle
management:
```bash
# just create topics
$ java -cp ... my.class --runner=KafkaStreams [... other opts ...]
--create-topics
# run
$ java -cp ... my.class --runner=KafkaStreams [... other opts ...]
# delete runner-created topics
$ java -cp ... my.class --runner=KafkaStreams [... other opts ...]
--delete-topics
```
But this is definitely out of scope of this PR.
--
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]