je-ik commented on code in PR #38689: URL: https://github.com/apache/beam/pull/38689#discussion_r3315519551
########## runners/kafka-streams/src/main/java/org/apache/beam/runners/kafka/streams/translation/KStreamsPayload.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.Objects; +import org.apache.beam.sdk.values.WindowedValue; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Sum-type envelope flowing between Kafka Streams processors in the Beam Kafka Streams runner. + * + * <p>Every record value emitted by a runner-introduced processor is one of: + * + * <ul> + * <li>A {@link #isData() data} element wrapping a {@link WindowedValue}, or + * <li>A {@link #isWatermark() watermark} signal carrying an event-time milliseconds value. + * </ul> + * + * <p>The envelope lets a single Kafka Streams output channel carry both Beam data and the watermark + * / synchronization primitives that Kafka Streams does not natively support. Future control + * messages (e.g. the {@code (epoch, assigned_partitions)} propagation from design doc §5) can be + * added here as additional variants. + * + * <p>This class is intentionally in-JVM only for now; serialization across topic boundaries + * (repartition or sink topics) will be introduced when the first translator that emits to a topic + * lands, at which point a corresponding Kafka {@link org.apache.kafka.common.serialization.Serde} + * will be added. + * + * @param <T> element type carried by data variants + */ +public final class KStreamsPayload<T> { + + private enum Kind { + DATA, + WATERMARK + } + + private final Kind kind; + private final @Nullable WindowedValue<T> data; + private final long watermarkMillis; + + private KStreamsPayload(Kind kind, @Nullable WindowedValue<T> data, long watermarkMillis) { + this.kind = kind; + this.data = data; + this.watermarkMillis = watermarkMillis; + } + + /** Returns a data payload wrapping the given {@link WindowedValue}. */ + public static <T> KStreamsPayload<T> data(WindowedValue<T> value) { + return new KStreamsPayload<>(Kind.DATA, value, 0L); + } + + /** Returns a watermark payload carrying the given event-time milliseconds. */ + public static <T> KStreamsPayload<T> watermark(long watermarkMillis) { + return new KStreamsPayload<>(Kind.WATERMARK, null, watermarkMillis); + } + + public boolean isData() { + return kind == Kind.DATA; + } + + public boolean isWatermark() { + return kind == Kind.WATERMARK; + } + + /** + * Returns the wrapped data element. Caller must check {@link #isData()} first; calling this on a + * watermark payload throws. + */ + public WindowedValue<T> getData() { + if (kind != Kind.DATA || data == null) { + throw new IllegalStateException("Payload is not a data element: kind=" + kind); + } + return data; + } + + /** + * Returns the watermark event-time milliseconds. Caller must check {@link #isWatermark()} first; + * calling this on a data payload throws. + */ + public long getWatermarkMillis() { + if (kind != Kind.WATERMARK) { + throw new IllegalStateException("Payload is not a watermark: kind=" + kind); + } + return watermarkMillis; + } + + @Override + public boolean equals(@Nullable Object o) { + if (this == o) { + return true; + } + if (!(o instanceof KStreamsPayload)) { + return false; + } + KStreamsPayload<?> that = (KStreamsPayload<?>) o; + return kind == that.kind + && watermarkMillis == that.watermarkMillis + && Objects.equals(data, that.data); + } + + @Override + public int hashCode() { + return Objects.hash(kind, data, watermarkMillis); + } + + @Override + public String toString() { + return kind == Kind.DATA ? "Data{" + data + "}" : "Watermark{" + watermarkMillis + "}"; Review Comment: You can use MoreObjects.toStringHelper() for these kind of formatting. -- 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]
