bbejeck commented on code in PR #21511: URL: https://github.com/apache/kafka/pull/21511#discussion_r2843855694
########## streams/src/main/java/org/apache/kafka/streams/state/internals/AggregationWithHeadersDeserializer.java: ########## @@ -0,0 +1,149 @@ +/* + * 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.kafka.streams.state.internals; + +import org.apache.kafka.common.errors.SerializationException; +import org.apache.kafka.common.header.Headers; +import org.apache.kafka.common.serialization.Deserializer; +import org.apache.kafka.common.utils.ByteUtils; +import org.apache.kafka.streams.kstream.internals.WrappingNullableDeserializer; +import org.apache.kafka.streams.processor.internals.SerdeGetter; +import org.apache.kafka.streams.state.AggregationWithHeaders; + +import java.nio.ByteBuffer; +import java.util.Map; +import java.util.Objects; + +import static org.apache.kafka.streams.kstream.internals.WrappingNullableUtils.initNullableDeserializer; + +/** + * Deserializer for AggregationWithHeaders. + * Deserialization format (per KIP-1271): + * [headersSize(varint)][headersBytes][aggregation] + * <p> + * Where: + * - headersSize: Size of the headersBytes section in bytes, encoded as varint + * - headersBytes: + * - For null/empty headers: headersSize = 0, headersBytes is omitted (0 bytes) + * - For non-empty headers: headersSize > 0, serialized headers in the format [count(varint)][header1][header2]... to be processed by HeadersDeserializer. + * - aggregation: Serialized aggregation to be deserialized with the provided aggregation deserializer + * <p> + * This is used by KIP-1271 to deserialize aggregations with headers from session state stores. + */ +class AggregationWithHeadersDeserializer<AGG> implements WrappingNullableDeserializer<AggregationWithHeaders<AGG>, Void, AGG> { + private static final HeadersDeserializer HEADERS_DESERIALIZER = new HeadersDeserializer(); + + public final Deserializer<AGG> aggregationDeserializer; + private final HeadersDeserializer headersDeserializer; + + AggregationWithHeadersDeserializer(final Deserializer<AGG> aggregationDeserializer) { + Objects.requireNonNull(aggregationDeserializer); + this.aggregationDeserializer = aggregationDeserializer; + this.headersDeserializer = new HeadersDeserializer(); + } + + @Override + public void configure(final Map<String, ?> configs, final boolean isKey) { + aggregationDeserializer.configure(configs, isKey); + headersDeserializer.configure(configs, isKey); + } + + @Override + public AggregationWithHeaders<AGG> deserialize(final String topic, final byte[] aggregationWithHeaders) { + if (aggregationWithHeaders == null) { + return null; + } + + final ByteBuffer buffer = ByteBuffer.wrap(aggregationWithHeaders); + final int headersSize = ByteUtils.readVarint(buffer); + + final byte[] rawHeaders = readBytes(buffer, headersSize); + final Headers headers = headersDeserializer.deserialize(topic, rawHeaders); + final byte[] rawAggregation = readBytes(buffer, buffer.remaining()); + final AGG aggregation = aggregationDeserializer.deserialize(topic, headers, rawAggregation); + + return AggregationWithHeaders.make(aggregation, headers); + } + + @Override + public void close() { + aggregationDeserializer.close(); + headersDeserializer.close(); + } + + @Override + public void setIfUnset(final SerdeGetter getter) { + initNullableDeserializer(aggregationDeserializer, getter); + } + + + /** + * Reads the specified number of bytes from the buffer with validation. + * + * @param buffer the ByteBuffer to read from + * @param length the number of bytes to read + * @return the byte array containing the read bytes + * @throws SerializationException if buffer doesn't have enough bytes or length is negative + */ + private static byte[] readBytes(final ByteBuffer buffer, final int length) { Review Comment: What would be a good location for a `Utils` class for this - maybe just a standalone one at the root of `org.apache.kafka.streams.state.internals` ? -- 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]
