tvalentyn commented on code in PR #39141: URL: https://github.com/apache/beam/pull/39141#discussion_r3491876815
########## runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/GroupByKeyProcessor.java: ########## @@ -0,0 +1,189 @@ +/* + * 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.List; +import org.apache.beam.sdk.coders.Coder; +import org.apache.beam.sdk.coders.CoderException; +import org.apache.beam.sdk.coders.IterableCoder; +import org.apache.beam.sdk.transforms.windowing.BoundedWindow; +import org.apache.beam.sdk.transforms.windowing.GlobalWindow; +import org.apache.beam.sdk.util.CoderUtils; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.WindowedValue; +import org.apache.beam.sdk.values.WindowedValues; +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.Nullable; +import org.joda.time.Instant; + +/** + * Executes a {@code GroupByKey} (GlobalWindow, default trigger, no allowed lateness). + * + * <p>Records arrive on the repartition topic keyed by the encoded Beam key, so every value of a key + * is co-located here. Each value is appended to a per-key buffer in a Kafka Streams state store. + * Watermark reports are fed to a {@link WatermarkManager}; when the input watermark reaches {@link + * BoundedWindow#TIMESTAMP_MAX_VALUE} (the end of the global window) every buffered key is emitted + * once as {@code KV<K, Iterable<V>>} and the buffer cleared, then the watermark is forwarded + * downstream. + * + * <p>Buffering whole value lists and re-encoding on each append is O(n^2) per key; fine for this + * first GroupByKey, and replaced when this moves to runner-core {@code GroupAlsoByWindow}. + */ +class GroupByKeyProcessor + implements Processor<byte[], KStreamsPayload<?>, byte[], KStreamsPayload<?>> { + + private final String stateStoreName; + private final Coder<Object> keyCoder; + private final IterableCoder<@Nullable Object> bufferCoder; + + private final WatermarkManager watermarkManager = new WatermarkManager(); + private Instant lastForwardedWatermark = BoundedWindow.TIMESTAMP_MIN_VALUE; + // The global window fires exactly once, when the watermark first reaches its end. Later watermark + // reports (e.g. the same terminal watermark broadcast across repartition partitions) must not + // re-fire. + private boolean fired = false; Review Comment: for this initial implementation, do we need to worry about persisting the state of `fired`/`lastForwardedWatermark`, etc, so that we are resilient to runner task restarts? -- 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]
