junrao commented on a change in pull request #10271: URL: https://github.com/apache/kafka/pull/10271#discussion_r617904827
########## File path: storage/src/main/java/org/apache/kafka/server/log/remote/metadata/storage/serialization/AbstractApiMessageAndVersionSerde.java ########## @@ -0,0 +1,77 @@ +/* + * 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.server.log.remote.metadata.storage.serialization; + +import org.apache.kafka.common.protocol.ApiMessage; +import org.apache.kafka.common.protocol.ByteBufferAccessor; +import org.apache.kafka.common.protocol.ObjectSerializationCache; +import org.apache.kafka.metadata.ApiMessageAndVersion; + +import java.nio.ByteBuffer; + +/** + * This class provides serialization/deserialization of {@code ApiMessageAndVersion}. + * <p></p> + * Implementors need to extend this class and implement {@link #apiMessageFor(short)} method to return a respective + * {@code ApiMessage} for the given {@code apiKey}. This is required to deserialize the bytes to build the respective + * {@code ApiMessage} instance. + */ +public abstract class AbstractApiMessageAndVersionSerde { + + public byte[] serialize(ApiMessageAndVersion messageAndVersion) { + ObjectSerializationCache cache = new ObjectSerializationCache(); + short version = messageAndVersion.version(); + ApiMessage message = messageAndVersion.message(); + + // Add header containing apiKey and apiVersion, + // headerSize is 1 byte for apiKey and 1 byte for apiVersion + int headerSize = 1 + 1; + int messageSize = message.size(cache, version); + ByteBufferAccessor writable = new ByteBufferAccessor(ByteBuffer.allocate(headerSize + messageSize)); + + // Write apiKey and version + writable.writeUnsignedVarint(message.apiKey()); Review comment: In https://github.com/apache/kafka/blob/trunk/raft/src/main/java/org/apache/kafka/raft/metadata/MetadataRecordSerde.java#L44, the serialization adds a frame version. The intention is to use that as the header version. Currently, the only fields in the header are apikey and version. The frame version allows us to change that in the future. We need to think through whether we should adopt the same strategy here. If we do, perhaps we could just reuse MetadataRecordSerde somehow. Also, whether we should share the apiKey space with the raft messages. -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org