Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
je-ik merged PR #39494: URL: https://github.com/apache/beam/pull/39494 -- 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]
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
je-ik commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3663870390
##
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/WindowedGroupByKeyProcessor.java:
##
@@ -0,0 +1,323 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.beam.model.pipeline.v1.RunnerApi;
+import org.apache.beam.runners.core.NullSideInputReader;
+import org.apache.beam.runners.core.ReduceFnRunner;
+import org.apache.beam.runners.core.StateInternals;
+import org.apache.beam.runners.core.SystemReduceFn;
+import org.apache.beam.runners.core.TimerInternals.TimerData;
+import org.apache.beam.runners.core.triggers.ExecutableTriggerStateMachine;
+import org.apache.beam.runners.core.triggers.TriggerStateMachines;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.util.construction.TriggerTranslation;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.WindowedValue;
+import org.apache.beam.sdk.values.WindowingStrategy;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.BaseEncoding;
+import org.apache.kafka.streams.processor.api.Processor;
+import org.apache.kafka.streams.processor.api.ProcessorContext;
+import org.apache.kafka.streams.processor.api.Record;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Executes a windowed {@code GroupByKey} by driving Beam's {@link
ReduceFnRunner} — the same
+ * windowing + triggering state machine the Flink and Spark portable runners
use — with Kafka
+ * Streams state and timers behind it.
+ *
+ * Records arrive on the repartition topic keyed by the encoded Beam key,
so every value of a key
+ * is co-located here. For each data record this builds a {@link
ReduceFnRunner} for that key over a
+ * {@link KafkaStreamsStateInternals} and {@link KafkaStreamsTimerInternals}
(both backed by
+ * persistent stores) and feeds the element in; the runner assigns it to
windows, updates the
+ * trigger state, and sets any timers it needs. When the aggregated input
watermark advances, every
+ * event-time timer whose fire time has passed is replayed through {@code
onTimers}, which is what
+ * makes windows emit their panes. The runner is stateless between records —
all durable state lives
+ * in the stores — so a fresh one per record is correct, mirroring {@code
+ * GroupAlsoByWindowViaWindowSetNewDoFn}.
+ *
+ * This first version supports the default trigger and non-merging windows
well; richer triggers,
+ * processing-time timers and session (merging) windows build on the same
machinery in a follow-up.
+ */
+class WindowedGroupByKeyProcessor
+implements Processor, byte[],
KStreamsPayload> {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(WindowedGroupByKeyProcessor.class);
+
+ private final String stateStoreName;
+ private final String holdsIndexStoreName;
+ private final String timerStoreName;
+ private final String timerIndexStoreName;
+ private final String transformId;
+ private final Coder keyCoder;
+ private final WindowingStrategy windowingStrategy;
+ private final Coder windowCoder;
+ private final RunnerApi.Trigger triggerProto;
+ private final SystemReduceFn, Iterable, W> reduceFn;
+ private final PipelineOptions options;
+
+ private final WatermarkAggregator watermarkAggregator;
+ private Instant lastForwardedWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
+ private Instant inputWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
je-ik commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3663870390
##
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/WindowedGroupByKeyProcessor.java:
##
@@ -0,0 +1,323 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.beam.model.pipeline.v1.RunnerApi;
+import org.apache.beam.runners.core.NullSideInputReader;
+import org.apache.beam.runners.core.ReduceFnRunner;
+import org.apache.beam.runners.core.StateInternals;
+import org.apache.beam.runners.core.SystemReduceFn;
+import org.apache.beam.runners.core.TimerInternals.TimerData;
+import org.apache.beam.runners.core.triggers.ExecutableTriggerStateMachine;
+import org.apache.beam.runners.core.triggers.TriggerStateMachines;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.util.construction.TriggerTranslation;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.WindowedValue;
+import org.apache.beam.sdk.values.WindowingStrategy;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.BaseEncoding;
+import org.apache.kafka.streams.processor.api.Processor;
+import org.apache.kafka.streams.processor.api.ProcessorContext;
+import org.apache.kafka.streams.processor.api.Record;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Executes a windowed {@code GroupByKey} by driving Beam's {@link
ReduceFnRunner} — the same
+ * windowing + triggering state machine the Flink and Spark portable runners
use — with Kafka
+ * Streams state and timers behind it.
+ *
+ * Records arrive on the repartition topic keyed by the encoded Beam key,
so every value of a key
+ * is co-located here. For each data record this builds a {@link
ReduceFnRunner} for that key over a
+ * {@link KafkaStreamsStateInternals} and {@link KafkaStreamsTimerInternals}
(both backed by
+ * persistent stores) and feeds the element in; the runner assigns it to
windows, updates the
+ * trigger state, and sets any timers it needs. When the aggregated input
watermark advances, every
+ * event-time timer whose fire time has passed is replayed through {@code
onTimers}, which is what
+ * makes windows emit their panes. The runner is stateless between records —
all durable state lives
+ * in the stores — so a fresh one per record is correct, mirroring {@code
+ * GroupAlsoByWindowViaWindowSetNewDoFn}.
+ *
+ * This first version supports the default trigger and non-merging windows
well; richer triggers,
+ * processing-time timers and session (merging) windows build on the same
machinery in a follow-up.
+ */
+class WindowedGroupByKeyProcessor
+implements Processor, byte[],
KStreamsPayload> {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(WindowedGroupByKeyProcessor.class);
+
+ private final String stateStoreName;
+ private final String holdsIndexStoreName;
+ private final String timerStoreName;
+ private final String timerIndexStoreName;
+ private final String transformId;
+ private final Coder keyCoder;
+ private final WindowingStrategy windowingStrategy;
+ private final Coder windowCoder;
+ private final RunnerApi.Trigger triggerProto;
+ private final SystemReduceFn, Iterable, W> reduceFn;
+ private final PipelineOptions options;
+
+ private final WatermarkAggregator watermarkAggregator;
+ private Instant lastForwardedWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
+ private Instant inputWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
junaiddshaukat commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3656513582
##
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/KafkaStreamsStateInternals.java:
##
@@ -0,0 +1,393 @@
+/*
+ * 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 java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.beam.runners.core.StateInternals;
+import org.apache.beam.runners.core.StateNamespace;
+import org.apache.beam.runners.core.StateTag;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.coders.InstantCoder;
+import org.apache.beam.sdk.coders.ListCoder;
+import org.apache.beam.sdk.state.BagState;
+import org.apache.beam.sdk.state.CombiningState;
+import org.apache.beam.sdk.state.MapState;
+import org.apache.beam.sdk.state.MultimapState;
+import org.apache.beam.sdk.state.OrderedListState;
+import org.apache.beam.sdk.state.ReadableState;
+import org.apache.beam.sdk.state.SetState;
+import org.apache.beam.sdk.state.State;
+import org.apache.beam.sdk.state.StateBinder;
+import org.apache.beam.sdk.state.StateContext;
+import org.apache.beam.sdk.state.StateSpec;
+import org.apache.beam.sdk.state.ValueState;
+import org.apache.beam.sdk.state.WatermarkHoldState;
+import org.apache.beam.sdk.transforms.Combine.CombineFn;
+import org.apache.beam.sdk.transforms.CombineWithContext;
+import org.apache.beam.sdk.transforms.windowing.TimestampCombiner;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.util.CombineFnUtil;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+
+/**
+ * A {@link StateInternals} for one key, backed by a Kafka Streams {@link
KeyValueStore}.
+ *
+ * Beam addresses a state cell by {@code (key, StateNamespace, StateTag)};
a windowed pipeline
+ * puts each window's state in its own namespace. Every cell is stored as one
entry in the shared
+ * per-transform store under a composite byte key {@code len(key)|key |
len(ns)|ns | len(tag)|tag},
+ * so all cells for one Beam key share a prefix and a whole key's state can be
range-scanned. The
+ * value is the cell's contents encoded with its Beam {@link Coder}. Writing
straight to the store
+ * (rather than buffering and flushing) keeps this restart-safe for free: the
store is changelogged
+ * and, under exactly-once, its writes commit atomically with the input
offsets.
+ *
+ * Modeled on the Spark runner's {@code SparkStateInternals}; the
difference is that each cell
+ * reads and writes its own store entry instead of an in-memory table, so
there is no separate
+ * persist step.
+ */
+class KafkaStreamsStateInternals implements StateInternals {
+
+ private final @NonNull K key;
+ private final byte[] encodedKey;
+ private final KeyValueStore store;
+
+ KafkaStreamsStateInternals(
+ @NonNull K key, byte[] encodedKey, KeyValueStore store) {
+this.key = key;
+this.encodedKey = encodedKey;
+this.store = store;
+ }
+
+ @Override
+ public Object getKey() {
+return key;
+ }
+
+ @Override
+ public T state(
+ StateNamespace namespace, StateTag address, StateContext c) {
+return address.getSpec().bind(address.getId(), new
KafkaStreamsStateBinder(namespace, c));
+ }
+
+ /** The composite store key for one cell: {@code len|key len|namespace
len|tagId}. */
+ private byte[] compositeKey(StateNamespace namespace, String id) {
Review Comment:
Done, it now builds into one exactly-sized array with System.arraycopy, and
caches the key | namespace prefix, which is reused across the several tags one
ReduceFnRunner turn touches. That removes the stream growth and the final copy.
I kept the length prefixes rather than a separator, though: an encoded key
is arbitrary coder output and a namespace stringKey can contain any byte too,
so key="a/b",
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
junaiddshaukat commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3656511259
##
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/KafkaStreamsTimerInternals.java:
##
@@ -0,0 +1,180 @@
+/*
+ * 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 java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.runners.core.StateNamespace;
+import org.apache.beam.runners.core.TimerInternals;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.state.TimeDomain;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+
+/**
+ * A {@link TimerInternals} for one key, backed by a shared Kafka Streams
timer {@link
+ * KeyValueStore} for a GroupByKey.
+ *
+ * Kafka Streams has no per-key timer service, so timers are persisted like
any other state. A
+ * timer is stored under a key built from its identity — {@code key | domain |
timerFamily | timerId
+ * | namespace} — so setting a timer with the same identity overwrites the
previous one and deleting
+ * it removes exactly that entry, matching {@link TimerInternals}' contract.
The value is the {@link
+ * TimerData} encoded with {@link TimerInternals.TimerDataCoderV2}, which
carries the fire time.
+ *
+ * Firing is driven by {@link WindowedGroupByKeyProcessor}: on a watermark
advance it scans the
+ * store for event-time timers whose fire time has passed and replays them
through {@link
+ * org.apache.beam.runners.core.ReduceFnRunner#onTimers}. Scanning the whole
store per advance is
+ * O(timers); acceptable for this first windowing support and replaceable with
a fire-time-ordered
+ * index later, the same way the initial GroupByKey buffered eagerly.
+ *
+ * This instance reports the current input-watermark and processing times
it was constructed with
+ * (the caller advances them as reports arrive); it never fires timers itself.
+ */
+class KafkaStreamsTimerInternals implements TimerInternals {
+
+ private final byte[] encodedKey;
+ private final KeyValueStore timerStore;
+ private final TimerInternals.TimerDataCoderV2 timerCoder;
+ private final Instant inputWatermarkTime;
+ private final Instant processingTime;
+
+ KafkaStreamsTimerInternals(
+ byte[] encodedKey,
+ KeyValueStore timerStore,
+ Coder windowCoder,
+ Instant inputWatermarkTime,
+ Instant processingTime) {
+this.encodedKey = encodedKey;
+this.timerStore = timerStore;
+this.timerCoder = TimerInternals.TimerDataCoderV2.of(windowCoder);
+this.inputWatermarkTime = inputWatermarkTime;
+this.processingTime = processingTime;
+ }
+
+ @Override
+ public void setTimer(
+ StateNamespace namespace,
+ String timerId,
+ String timerFamilyId,
+ Instant target,
+ Instant outputTimestamp,
+ TimeDomain timeDomain) {
+setTimer(TimerData.of(timerId, timerFamilyId, namespace, target,
outputTimestamp, timeDomain));
+ }
+
+ @Override
+ public void setTimer(TimerData timerData) {
+timerStore.put(timerStoreKey(encodedKey, timerData),
encodeTimer(timerData));
+ }
+
+ @Override
+ public void deleteTimer(
+ StateNamespace namespace, String timerId, String timerFamilyId,
TimeDomain timeDomain) {
+timerStore.delete(timerStoreKey(encodedKey, timerId, timerFamilyId,
timeDomain, namespace));
+ }
+
+ @Override
+ public void deleteTimer(StateNamespace namespace, String timerId, String
timerFamilyId) {
+throw new UnsupportedOperationException(
+"Deleting a timer without a time domain is not supported; the domain
is part of a timer's"
++ " store identity.");
+ }
+
+ @Override
+ public void deleteTimer(TimerData timerKey) {
+timerStore.delete(timerStoreKey(encodedKey, timerKey));
+ }
+
+ @Override
+ public Instant currentProcessingTime() {
+return processingTime;
+ }
+
+
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
junaiddshaukat commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3656509877
##
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/KafkaStreamsTimerInternals.java:
##
@@ -0,0 +1,180 @@
+/*
+ * 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 java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.runners.core.StateNamespace;
+import org.apache.beam.runners.core.TimerInternals;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.state.TimeDomain;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+
+/**
+ * A {@link TimerInternals} for one key, backed by a shared Kafka Streams
timer {@link
+ * KeyValueStore} for a GroupByKey.
+ *
+ * Kafka Streams has no per-key timer service, so timers are persisted like
any other state. A
+ * timer is stored under a key built from its identity — {@code key | domain |
timerFamily | timerId
+ * | namespace} — so setting a timer with the same identity overwrites the
previous one and deleting
+ * it removes exactly that entry, matching {@link TimerInternals}' contract.
The value is the {@link
+ * TimerData} encoded with {@link TimerInternals.TimerDataCoderV2}, which
carries the fire time.
+ *
+ * Firing is driven by {@link WindowedGroupByKeyProcessor}: on a watermark
advance it scans the
+ * store for event-time timers whose fire time has passed and replays them
through {@link
+ * org.apache.beam.runners.core.ReduceFnRunner#onTimers}. Scanning the whole
store per advance is
+ * O(timers); acceptable for this first windowing support and replaceable with
a fire-time-ordered
+ * index later, the same way the initial GroupByKey buffered eagerly.
+ *
+ * This instance reports the current input-watermark and processing times
it was constructed with
+ * (the caller advances them as reports arrive); it never fires timers itself.
+ */
+class KafkaStreamsTimerInternals implements TimerInternals {
+
+ private final byte[] encodedKey;
+ private final KeyValueStore timerStore;
+ private final TimerInternals.TimerDataCoderV2 timerCoder;
+ private final Instant inputWatermarkTime;
+ private final Instant processingTime;
+
+ KafkaStreamsTimerInternals(
+ byte[] encodedKey,
+ KeyValueStore timerStore,
+ Coder windowCoder,
+ Instant inputWatermarkTime,
+ Instant processingTime) {
+this.encodedKey = encodedKey;
+this.timerStore = timerStore;
+this.timerCoder = TimerInternals.TimerDataCoderV2.of(windowCoder);
+this.inputWatermarkTime = inputWatermarkTime;
+this.processingTime = processingTime;
+ }
+
+ @Override
+ public void setTimer(
+ StateNamespace namespace,
+ String timerId,
+ String timerFamilyId,
+ Instant target,
+ Instant outputTimestamp,
+ TimeDomain timeDomain) {
+setTimer(TimerData.of(timerId, timerFamilyId, namespace, target,
outputTimestamp, timeDomain));
+ }
+
+ @Override
+ public void setTimer(TimerData timerData) {
+timerStore.put(timerStoreKey(encodedKey, timerData),
encodeTimer(timerData));
+ }
+
+ @Override
+ public void deleteTimer(
+ StateNamespace namespace, String timerId, String timerFamilyId,
TimeDomain timeDomain) {
+timerStore.delete(timerStoreKey(encodedKey, timerId, timerFamilyId,
timeDomain, namespace));
+ }
+
+ @Override
+ public void deleteTimer(StateNamespace namespace, String timerId, String
timerFamilyId) {
+throw new UnsupportedOperationException(
+"Deleting a timer without a time domain is not supported; the domain
is part of a timer's"
++ " store identity.");
+ }
+
+ @Override
+ public void deleteTimer(TimerData timerKey) {
+timerStore.delete(timerStoreKey(encodedKey, timerKey));
+ }
+
+ @Override
+ public Instant currentProcessingTime() {
+return processingTime;
+ }
+
+
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
junaiddshaukat commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3656507901
##
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/WindowedGroupByKeyProcessor.java:
##
@@ -0,0 +1,284 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.beam.model.pipeline.v1.RunnerApi;
+import org.apache.beam.runners.core.NullSideInputReader;
+import org.apache.beam.runners.core.ReduceFnRunner;
+import org.apache.beam.runners.core.StateInternals;
+import org.apache.beam.runners.core.SystemReduceFn;
+import org.apache.beam.runners.core.TimerInternals.TimerData;
+import org.apache.beam.runners.core.triggers.ExecutableTriggerStateMachine;
+import org.apache.beam.runners.core.triggers.TriggerStateMachines;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.state.TimeDomain;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.util.construction.TriggerTranslation;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.WindowedValue;
+import org.apache.beam.sdk.values.WindowingStrategy;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.BaseEncoding;
+import org.apache.kafka.streams.processor.api.Processor;
+import org.apache.kafka.streams.processor.api.ProcessorContext;
+import org.apache.kafka.streams.processor.api.Record;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Executes a windowed {@code GroupByKey} by driving Beam's {@link
ReduceFnRunner} — the same
+ * windowing + triggering state machine the Flink and Spark portable runners
use — with Kafka
+ * Streams state and timers behind it.
+ *
+ * Records arrive on the repartition topic keyed by the encoded Beam key,
so every value of a key
+ * is co-located here. For each data record this builds a {@link
ReduceFnRunner} for that key over a
+ * {@link KafkaStreamsStateInternals} and {@link KafkaStreamsTimerInternals}
(both backed by
+ * persistent stores) and feeds the element in; the runner assigns it to
windows, updates the
+ * trigger state, and sets any timers it needs. When the aggregated input
watermark advances, every
+ * event-time timer whose fire time has passed is replayed through {@code
onTimers}, which is what
+ * makes windows emit their panes. The runner is stateless between records —
all durable state lives
+ * in the stores — so a fresh one per record is correct, mirroring {@code
+ * GroupAlsoByWindowViaWindowSetNewDoFn}.
+ *
+ * This first version supports the default trigger and non-merging windows
well; richer triggers,
+ * processing-time timers and session (merging) windows build on the same
machinery in a follow-up.
+ */
+class WindowedGroupByKeyProcessor
+implements Processor, byte[],
KStreamsPayload> {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(WindowedGroupByKeyProcessor.class);
+
+ private final String stateStoreName;
+ private final String timerStoreName;
+ private final String transformId;
+ private final Coder keyCoder;
+ private final WindowingStrategy windowingStrategy;
+ private final Coder windowCoder;
+ private final RunnerApi.Trigger triggerProto;
+ private final SystemReduceFn, Iterable, W> reduceFn;
+ private final PipelineOptions options;
+
+ private final WatermarkAggregator watermarkAggregator;
+ private Instant lastForwardedWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
+ private Instant inputWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
+
+ private @Nullable
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
junaiddshaukat commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3656505954
##
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/WindowedGroupByKeyProcessor.java:
##
@@ -0,0 +1,284 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.beam.model.pipeline.v1.RunnerApi;
+import org.apache.beam.runners.core.NullSideInputReader;
+import org.apache.beam.runners.core.ReduceFnRunner;
+import org.apache.beam.runners.core.StateInternals;
+import org.apache.beam.runners.core.SystemReduceFn;
+import org.apache.beam.runners.core.TimerInternals.TimerData;
+import org.apache.beam.runners.core.triggers.ExecutableTriggerStateMachine;
+import org.apache.beam.runners.core.triggers.TriggerStateMachines;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.state.TimeDomain;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.util.construction.TriggerTranslation;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.WindowedValue;
+import org.apache.beam.sdk.values.WindowingStrategy;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.BaseEncoding;
+import org.apache.kafka.streams.processor.api.Processor;
+import org.apache.kafka.streams.processor.api.ProcessorContext;
+import org.apache.kafka.streams.processor.api.Record;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Executes a windowed {@code GroupByKey} by driving Beam's {@link
ReduceFnRunner} — the same
+ * windowing + triggering state machine the Flink and Spark portable runners
use — with Kafka
+ * Streams state and timers behind it.
+ *
+ * Records arrive on the repartition topic keyed by the encoded Beam key,
so every value of a key
+ * is co-located here. For each data record this builds a {@link
ReduceFnRunner} for that key over a
+ * {@link KafkaStreamsStateInternals} and {@link KafkaStreamsTimerInternals}
(both backed by
+ * persistent stores) and feeds the element in; the runner assigns it to
windows, updates the
+ * trigger state, and sets any timers it needs. When the aggregated input
watermark advances, every
+ * event-time timer whose fire time has passed is replayed through {@code
onTimers}, which is what
+ * makes windows emit their panes. The runner is stateless between records —
all durable state lives
+ * in the stores — so a fresh one per record is correct, mirroring {@code
+ * GroupAlsoByWindowViaWindowSetNewDoFn}.
+ *
+ * This first version supports the default trigger and non-merging windows
well; richer triggers,
+ * processing-time timers and session (merging) windows build on the same
machinery in a follow-up.
+ */
+class WindowedGroupByKeyProcessor
+implements Processor, byte[],
KStreamsPayload> {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(WindowedGroupByKeyProcessor.class);
+
+ private final String stateStoreName;
+ private final String timerStoreName;
+ private final String transformId;
+ private final Coder keyCoder;
+ private final WindowingStrategy windowingStrategy;
+ private final Coder windowCoder;
+ private final RunnerApi.Trigger triggerProto;
+ private final SystemReduceFn, Iterable, W> reduceFn;
+ private final PipelineOptions options;
+
+ private final WatermarkAggregator watermarkAggregator;
+ private Instant lastForwardedWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
+ private Instant inputWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
+
+ private @Nullable
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
je-ik commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3655454892
##
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/WindowedGroupByKeyProcessor.java:
##
@@ -0,0 +1,284 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.beam.model.pipeline.v1.RunnerApi;
+import org.apache.beam.runners.core.NullSideInputReader;
+import org.apache.beam.runners.core.ReduceFnRunner;
+import org.apache.beam.runners.core.StateInternals;
+import org.apache.beam.runners.core.SystemReduceFn;
+import org.apache.beam.runners.core.TimerInternals.TimerData;
+import org.apache.beam.runners.core.triggers.ExecutableTriggerStateMachine;
+import org.apache.beam.runners.core.triggers.TriggerStateMachines;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.state.TimeDomain;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.util.construction.TriggerTranslation;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.WindowedValue;
+import org.apache.beam.sdk.values.WindowingStrategy;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.BaseEncoding;
+import org.apache.kafka.streams.processor.api.Processor;
+import org.apache.kafka.streams.processor.api.ProcessorContext;
+import org.apache.kafka.streams.processor.api.Record;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Executes a windowed {@code GroupByKey} by driving Beam's {@link
ReduceFnRunner} — the same
+ * windowing + triggering state machine the Flink and Spark portable runners
use — with Kafka
+ * Streams state and timers behind it.
+ *
+ * Records arrive on the repartition topic keyed by the encoded Beam key,
so every value of a key
+ * is co-located here. For each data record this builds a {@link
ReduceFnRunner} for that key over a
+ * {@link KafkaStreamsStateInternals} and {@link KafkaStreamsTimerInternals}
(both backed by
+ * persistent stores) and feeds the element in; the runner assigns it to
windows, updates the
+ * trigger state, and sets any timers it needs. When the aggregated input
watermark advances, every
+ * event-time timer whose fire time has passed is replayed through {@code
onTimers}, which is what
+ * makes windows emit their panes. The runner is stateless between records —
all durable state lives
+ * in the stores — so a fresh one per record is correct, mirroring {@code
+ * GroupAlsoByWindowViaWindowSetNewDoFn}.
+ *
+ * This first version supports the default trigger and non-merging windows
well; richer triggers,
+ * processing-time timers and session (merging) windows build on the same
machinery in a follow-up.
+ */
+class WindowedGroupByKeyProcessor
+implements Processor, byte[],
KStreamsPayload> {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(WindowedGroupByKeyProcessor.class);
+
+ private final String stateStoreName;
+ private final String timerStoreName;
+ private final String transformId;
+ private final Coder keyCoder;
+ private final WindowingStrategy windowingStrategy;
+ private final Coder windowCoder;
+ private final RunnerApi.Trigger triggerProto;
+ private final SystemReduceFn, Iterable, W> reduceFn;
+ private final PipelineOptions options;
+
+ private final WatermarkAggregator watermarkAggregator;
+ private Instant lastForwardedWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
+ private Instant inputWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE;
+
+ private @Nullable ProcessorContext>
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
je-ik commented on code in PR #39494:
URL: https://github.com/apache/beam/pull/39494#discussion_r3655276453
##
runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/KafkaStreamsStateInternals.java:
##
@@ -0,0 +1,393 @@
+/*
+ * 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 java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.beam.runners.core.StateInternals;
+import org.apache.beam.runners.core.StateNamespace;
+import org.apache.beam.runners.core.StateTag;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.coders.InstantCoder;
+import org.apache.beam.sdk.coders.ListCoder;
+import org.apache.beam.sdk.state.BagState;
+import org.apache.beam.sdk.state.CombiningState;
+import org.apache.beam.sdk.state.MapState;
+import org.apache.beam.sdk.state.MultimapState;
+import org.apache.beam.sdk.state.OrderedListState;
+import org.apache.beam.sdk.state.ReadableState;
+import org.apache.beam.sdk.state.SetState;
+import org.apache.beam.sdk.state.State;
+import org.apache.beam.sdk.state.StateBinder;
+import org.apache.beam.sdk.state.StateContext;
+import org.apache.beam.sdk.state.StateSpec;
+import org.apache.beam.sdk.state.ValueState;
+import org.apache.beam.sdk.state.WatermarkHoldState;
+import org.apache.beam.sdk.transforms.Combine.CombineFn;
+import org.apache.beam.sdk.transforms.CombineWithContext;
+import org.apache.beam.sdk.transforms.windowing.TimestampCombiner;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.util.CombineFnUtil;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Instant;
+
+/**
+ * A {@link StateInternals} for one key, backed by a Kafka Streams {@link
KeyValueStore}.
+ *
+ * Beam addresses a state cell by {@code (key, StateNamespace, StateTag)};
a windowed pipeline
+ * puts each window's state in its own namespace. Every cell is stored as one
entry in the shared
+ * per-transform store under a composite byte key {@code len(key)|key |
len(ns)|ns | len(tag)|tag},
+ * so all cells for one Beam key share a prefix and a whole key's state can be
range-scanned. The
+ * value is the cell's contents encoded with its Beam {@link Coder}. Writing
straight to the store
+ * (rather than buffering and flushing) keeps this restart-safe for free: the
store is changelogged
+ * and, under exactly-once, its writes commit atomically with the input
offsets.
+ *
+ * Modeled on the Spark runner's {@code SparkStateInternals}; the
difference is that each cell
+ * reads and writes its own store entry instead of an in-memory table, so
there is no separate
+ * persist step.
+ */
+class KafkaStreamsStateInternals implements StateInternals {
+
+ private final @NonNull K key;
+ private final byte[] encodedKey;
+ private final KeyValueStore store;
+
+ KafkaStreamsStateInternals(
+ @NonNull K key, byte[] encodedKey, KeyValueStore store) {
+this.key = key;
+this.encodedKey = encodedKey;
+this.store = store;
+ }
+
+ @Override
+ public Object getKey() {
+return key;
+ }
+
+ @Override
+ public T state(
+ StateNamespace namespace, StateTag address, StateContext c) {
+return address.getSpec().bind(address.getId(), new
KafkaStreamsStateBinder(namespace, c));
+ }
+
+ /** The composite store key for one cell: {@code len|key len|namespace
len|tagId}. */
+ private byte[] compositeKey(StateNamespace namespace, String id) {
Review Comment:
This method will be a hotspot (it is called for every state access), can we
optimize it a little by avoiding length-prefixing and instead just copy bytes
in memory, e.g. (pseudocode I did'nt check correctness, this is just an idea)
```java
byte[] namespaceBytes = namespae.stringKey().getBytes(UTF_8);
byte[] idBytes = id.getBytes(UTF_8);
int len = encodedKey.length + namespaceBytes.length + idBytes.length +
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
github-actions[bot] commented on PR #39494: URL: https://github.com/apache/beam/pull/39494#issuecomment-5084224880 Assigning reviewers: R: @kennknowles added as fallback since no labels match configuration Note: If you would like to opt out of this review, comment `assign to next reviewer`. Available commands: - `stop reviewer notifications` - opt out of the automated review tooling - `remind me after tests pass` - tag the comment author after tests pass - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers) The PR bot will only process comments in the main thread (not review comments). -- 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]
Re: [PR] [GSoC 2026] Kafka Streams runner: windowed GroupByKey via ReduceFnRunner [beam]
gemini-code-assist[bot] commented on PR #39494: URL: https://github.com/apache/beam/pull/39494#issuecomment-5084122729 > [!CAUTION] > The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. -- 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]
