|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.runners.kafka.streams.translation; |
| 19 | + |
| 20 | +import java.io.ByteArrayOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import org.apache.beam.runners.kafka.streams.v1.KafkaStreamsPayloadProtos.KafkaStreamsPayload; |
| 23 | +import org.apache.beam.sdk.coders.Coder; |
| 24 | +import org.apache.beam.sdk.values.WindowedValue; |
| 25 | +import org.apache.beam.vendor.grpc.v1p69p0.com.google.protobuf.ByteString; |
| 26 | +import org.apache.beam.vendor.grpc.v1p69p0.com.google.protobuf.InvalidProtocolBufferException; |
| 27 | +import org.apache.kafka.common.errors.SerializationException; |
| 28 | +import org.apache.kafka.common.serialization.Deserializer; |
| 29 | +import org.apache.kafka.common.serialization.Serde; |
| 30 | +import org.apache.kafka.common.serialization.Serializer; |
| 31 | + |
| 32 | +/** |
| 33 | + * Kafka {@link Serde} for {@link KStreamsPayload}, enabling the envelope to cross topic boundaries |
| 34 | + * (e.g. the repartition topic a {@code GroupByKey} introduces). Until now {@link KStreamsPayload} |
| 35 | + * only flowed in-JVM via {@code ProcessorContext#forward}, so no serialization was needed. |
| 36 | + * |
| 37 | + * <p>The wire form is the {@link KafkaStreamsPayload} protobuf message — protobuf gives compatible |
| 38 | + * schema evolution and compact varint encoding. The data variant carries the {@link WindowedValue} |
| 39 | + * encoded with the {@link Coder} supplied for the topic's PCollection; the watermark variant |
| 40 | + * carries the coder-independent watermark report. A {@link KStreamsPayloadSerde} is therefore |
| 41 | + * parameterized by the data {@link Coder} (different topics carry different element types). |
| 42 | + * |
| 43 | + * <p>The serde assumes non-null payloads: the topics it is used on (repartition and watermark |
| 44 | + * fan-out) are not log-compacted, so no tombstone (null-valued) records occur. |
| 45 | + * |
| 46 | + * @param <T> the data element type carried by data payloads on this topic |
| 47 | + */ |
| 48 | +public final class KStreamsPayloadSerde<T> implements Serde<KStreamsPayload<T>> { |
| 49 | + |
| 50 | + private final Coder<WindowedValue<T>> dataCoder; |
| 51 | + |
| 52 | + public KStreamsPayloadSerde(Coder<WindowedValue<T>> dataCoder) { |
| 53 | + this.dataCoder = dataCoder; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Serializer<KStreamsPayload<T>> serializer() { |
| 58 | + return new PayloadSerializer(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Deserializer<KStreamsPayload<T>> deserializer() { |
| 63 | + return new PayloadDeserializer(); |
| 64 | + } |
| 65 | + |
| 66 | + private final class PayloadSerializer implements Serializer<KStreamsPayload<T>> { |
| 67 | + @Override |
| 68 | + public byte[] serialize(String topic, KStreamsPayload<T> payload) { |
| 69 | + KafkaStreamsPayload.Builder proto = KafkaStreamsPayload.newBuilder(); |
| 70 | + if (payload.isData()) { |
| 71 | + ByteArrayOutputStream encoded = new ByteArrayOutputStream(); |
| 72 | + try { |
| 73 | + dataCoder.encode(payload.getData(), encoded); |
| 74 | + } catch (IOException e) { |
| 75 | + throw new SerializationException("Failed to encode KStreamsPayload data element", e); |
| 76 | + } |
| 77 | + proto.setData( |
| 78 | + KafkaStreamsPayload.DataPayload.newBuilder() |
| 79 | + .setValue(ByteString.copyFrom(encoded.toByteArray()))); |
| 80 | + } else { |
| 81 | + WatermarkPayload watermark = payload.asWatermark(); |
| 82 | + proto.setWatermark( |
| 83 | + KafkaStreamsPayload.WatermarkPayload.newBuilder() |
| 84 | + .setMillis(watermark.getWatermarkMillis()) |
| 85 | + .setSourcePartition(watermark.getSourcePartition()) |
| 86 | + .setTotalPartitions(watermark.getTotalSourcePartitions())); |
| 87 | + } |
| 88 | + return proto.build().toByteArray(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private final class PayloadDeserializer implements Deserializer<KStreamsPayload<T>> { |
| 93 | + @Override |
| 94 | + public KStreamsPayload<T> deserialize(String topic, byte[] bytes) { |
| 95 | + KafkaStreamsPayload proto; |
| 96 | + try { |
| 97 | + proto = KafkaStreamsPayload.parseFrom(bytes); |
| 98 | + } catch (InvalidProtocolBufferException e) { |
| 99 | + throw new SerializationException("Failed to parse KStreamsPayload", e); |
| 100 | + } |
| 101 | + switch (proto.getPayloadCase()) { |
| 102 | + case DATA: |
| 103 | + try { |
| 104 | + return KStreamsPayload.data(dataCoder.decode(proto.getData().getValue().newInput())); |
| 105 | + } catch (IOException e) { |
| 106 | + throw new SerializationException("Failed to decode KStreamsPayload data element", e); |
| 107 | + } |
| 108 | + case WATERMARK: |
| 109 | + KafkaStreamsPayload.WatermarkPayload watermark = proto.getWatermark(); |
| 110 | + return KStreamsPayload.watermark( |
| 111 | + watermark.getMillis(), |
| 112 | + watermark.getSourcePartition(), |
| 113 | + watermark.getTotalPartitions()); |
| 114 | + case PAYLOAD_NOT_SET: |
| 115 | + default: |
| 116 | + throw new SerializationException( |
| 117 | + "KStreamsPayload has no payload variant set: " + proto.getPayloadCase()); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments