satishd commented on a change in pull request #10271:
URL: https://github.com/apache/kafka/pull/10271#discussion_r616851984



##########
File path: 
storage/src/main/java/org/apache/kafka/server/log/remote/metadata/storage/serialization/RemoteLogMetadataContextSerdes.java
##########
@@ -0,0 +1,123 @@
+/*
+ * 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.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serde;
+import org.apache.kafka.common.serialization.Serializer;
+import 
org.apache.kafka.server.log.remote.metadata.storage.RemoteLogMetadataContext;
+import 
org.apache.kafka.server.log.remote.metadata.storage.generated.RemoteLogSegmentMetadataRecord;
+import 
org.apache.kafka.server.log.remote.metadata.storage.generated.RemoteLogSegmentMetadataUpdateRecord;
+import 
org.apache.kafka.server.log.remote.metadata.storage.generated.RemotePartitionDeleteMetadataRecord;
+
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This class provides serialization and deserialization for {@link 
RemoteLogMetadataContext}. This is the root serdes
+ * for the messages that are stored in internal remote log metadata topic.
+ */
+public class RemoteLogMetadataContextSerdes implements 
Serde<RemoteLogMetadataContext> {
+
+    public static final byte REMOTE_LOG_SEGMENT_METADATA_API_KEY = (byte) new 
RemoteLogSegmentMetadataRecord().apiKey();
+    public static final byte REMOTE_LOG_SEGMENT_METADATA_UPDATE_API_KEY = 
(byte) new RemoteLogSegmentMetadataUpdateRecord().apiKey();
+    public static final byte REMOTE_PARTITION_DELETE_API_KEY = (byte) new 
RemotePartitionDeleteMetadataRecord().apiKey();
+
+    private static final Map<Byte, RemoteLogMetadataSerdes> KEY_TO_SERDES = 
createInternalSerde();
+
+    private final Deserializer<RemoteLogMetadataContext> rootDeserializer;
+    private final Serializer<RemoteLogMetadataContext> rootSerializer;
+
+    public RemoteLogMetadataContextSerdes() {
+        rootSerializer = (topic, data) -> serialize(data);

Review comment:
       There are only two layers currently exist for serialization. Even with 
ApiMessageSerde, there will be at least two layers but the ApiMessageSerde can 
be reused in future. 
   
   I plan to rename RemoteLogMetadataSerde to RemoteLogMetadataTransform 
    - to avoid confusion with serde methods like seriaize/deserialize.
    - as it is really a transform of POJO to APIMessageAndVersion or vice versa.
   
   For ApiMessageSerde, I plan to reuse ApiMessageAndVersion as you suggested 
earlier. This will be similar to the below code. 
   
   ```
   
   /**
    * This abstract 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 implements 
Serde<ApiMessageAndVersion> {
   
       private final Serializer<ApiMessageAndVersion> serializer;
       private final Deserializer<ApiMessageAndVersion> deserializer;
   
       public AbstractApiMessageAndVersionSerde() {
           serializer = (topic, data) -> doSerialize(data);
           deserializer = (topic, data) -> doDeserialize(data);
       }
   
       private byte[] doSerialize(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());
           writable.writeUnsignedVarint(version);
   
           // Write the message
           message.write(writable, cache, version);
   
           return writable.buffer().array();
       }
   
       private ApiMessageAndVersion doDeserialize(byte[] data) {
   
           ByteBufferAccessor readable = new 
ByteBufferAccessor(ByteBuffer.wrap(data));
   
           short apiKey = (short) readable.readUnsignedVarint();
           short version = (short) readable.readUnsignedVarint();
   
           ApiMessage message = apiMessageFor(apiKey);
           message.read(readable, version);
   
           return new ApiMessageAndVersion(message, version);
       }
   
       /**
        * Return {@code ApiMessage} instance for the given {@code apiKey}. This 
is used while deserializing the bytes
        * payload into the respective {@code ApiMessage} in {@link 
#doDeserialize(byte[])} method.
        *
        * @param apiKey apiKey for which a {@code ApiMessage} to be created.
        */
       public abstract ApiMessage apiMessageFor(short apiKey);
   
       @Override
       public Serializer<ApiMessageAndVersion> serializer() {
           return serializer;
       }
   
       @Override
       public Deserializer<ApiMessageAndVersion> deserializer() {
           return deserializer;
       }
   
   }
   
   ```
   
   ```
   
   /**
    * This interface is about transforming metadata objects into the respective 
{@link ApiMessageAndVersion} or vice versa.
    * <p></p>
    * Those metadata objects can be {@link 
org.apache.kafka.server.log.remote.storage.RemoteLogSegmentMetadata},
    * {@link 
org.apache.kafka.server.log.remote.storage.RemoteLogSegmentMetadataUpdate}, or 
{@link 
org.apache.kafka.server.log.remote.storage.RemotePartitionDeleteMetadata}.
    * <p>
    * @param <T> metadata type.
    *
    * @see RemoteLogSegmentMetadataTransform
    * @see RemoteLogSegmentMetadataUpdateTransform
    * @see RemotePartitionDeleteMetadataTransform
    */
   public interface RemoteLogMetadataTransform<T> {
   
       /**
        * Transforms the given {@code metadata} object into the respective 
{@code ApiMessageAndVersion} object.
        *
        * @param metadata metadata object to be serialized.
        * @return transformed {@code ApiMessageAndVersion} object.
        */
       ApiMessageAndVersion toApiMessageAndVersion(T metadata);
   
       /**
        * Return the deserialized object for the given payload and the version.
        *
        * @param apiMessageAndVersion ApiMessageAndVersion object to be 
transformed.
        * @return transformed {@code T} metadata object.
        */
       T fromApiMessageAndVersion(ApiMessageAndVersion apiMessageAndVersion);
   
   }
   ```




-- 
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


Reply via email to