danny0405 commented on code in PR #9579:
URL: https://github.com/apache/hudi/pull/9579#discussion_r1320942427


##########
hudi-cli/src/test/java/org/apache/hudi/cli/testutils/HoodieTestCommitMetadataGenerator.java:
##########
@@ -90,8 +91,7 @@ public static void createCommitFileWithMetadata(String 
basePath, String commitTi
     for (String name : commitFileNames) {
       HoodieCommitMetadata commitMetadata =
               generateCommitMetadata(basePath, commitTime, fileId1, fileId2, 
writes, updates, extraMetadata, true);
-      String content = commitMetadata.toJsonString();
-      createFileWithMetadata(basePath, configuration, name, content);
+      createFileWithMetadata(basePath, configuration, name, 
serializeCommitMetadata(convertCommitMetadata(commitMetadata)).get());
     }

Review Comment:
   The combination of `serializeCommitMetadata(convertCommitMetadata` looks 
everywhere, can we put the conversion of `convertCommitMetadata` inside 
`serializeCommitMetadata`



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/BaseHoodieWriteClient.java:
##########
@@ -1542,7 +1549,10 @@ private void commitTableChange(InternalSchema newSchema, 
HoodieTableMetaClient m
     HoodieCommitMetadata metadata = new HoodieCommitMetadata();
     metadata.setOperationType(WriteOperationType.ALTER_SCHEMA);
     try {
-      timeLine.transitionRequestedToInflight(requested, 
Option.of(metadata.toJsonString().getBytes(StandardCharsets.UTF_8)));
+      timeLine.transitionRequestedToInflight(requested,
+          metadata instanceof HoodieReplaceCommitMetadata
+              ? 
serializeReplaceCommitMetadata(convertReplaceCommitMetadata((HoodieReplaceCommitMetadata)
 metadata))

Review Comment:
   ditto.



##########
hudi-common/src/main/java/org/apache/hudi/common/table/timeline/MetadataConversionUtils.java:
##########
@@ -244,18 +246,44 @@ public static Option<HoodieCommitMetadata> 
getHoodieCommitMetadata(HoodieTableMe
     return Option.of(TimelineUtils.getCommitMetadata(hoodieInstant, timeline));
   }
 
-  public static org.apache.hudi.avro.model.HoodieCommitMetadata 
convertCommitMetadata(
-          HoodieCommitMetadata hoodieCommitMetadata) {
-    ObjectMapper mapper = new ObjectMapper();
-    // Need this to ignore other public get() methods
-    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-    org.apache.hudi.avro.model.HoodieCommitMetadata avroMetaData =
-            mapper.convertValue(hoodieCommitMetadata, 
org.apache.hudi.avro.model.HoodieCommitMetadata.class);
+  /**
+   * Convert commit metadata from json to avro.
+   */
+  public static <T extends SpecificRecordBase> T 
convertCommitMetadata(HoodieCommitMetadata hoodieCommitMetadata) {
+    if (hoodieCommitMetadata instanceof HoodieReplaceCommitMetadata) {
+      return (T) convertReplaceCommitMetadata((HoodieReplaceCommitMetadata) 
hoodieCommitMetadata);
+    }
+    hoodieCommitMetadata.getPartitionToWriteStats().remove(null);

Review Comment:
   Can we add some doc why there is a `null` key in the map.



##########
hudi-common/src/test/java/org/apache/hudi/common/table/view/TestHoodieTableFileSystemView.java:
##########
@@ -1432,7 +1439,12 @@ public void testReplaceWithTimeTravel() throws 
IOException {
         CommitUtils.buildMetadata(Collections.emptyList(), 
partitionToReplaceFileIds, Option.empty(), WriteOperationType.INSERT_OVERWRITE, 
"", HoodieTimeline.REPLACE_COMMIT_ACTION);
     commitTimeline = metaClient.getActiveTimeline();
     HoodieInstant instant2 = new HoodieInstant(true, 
HoodieTimeline.REPLACE_COMMIT_ACTION, commitTime2);
-    saveAsComplete(commitTimeline, instant2, 
Option.of(commitMetadata.toJsonString().getBytes(StandardCharsets.UTF_8)));
+    saveAsComplete(
+        commitTimeline,
+        instant2,
+        commitMetadata instanceof HoodieReplaceCommitMetadata
+            ? 
serializeReplaceCommitMetadata(convertReplaceCommitMetadata((HoodieReplaceCommitMetadata)
 commitMetadata))
+            : serializeCommitMetadata(convertCommitMetadata(commitMetadata)));
 

Review Comment:
   Fuse `serializeReplaceCommitMetadata` and `serializeCommitMetadata` into one.



##########
hudi-common/src/main/java/org/apache/avro/io/NoWrappingJsonEncoder.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.avro.io;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.parsing.Symbol;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class NoWrappingJsonEncoder extends JsonEncoder {
+  public NoWrappingJsonEncoder(Schema sc, OutputStream out) throws IOException 
{
+    super(sc, out);

Review Comment:
   Can we give some document.



##########
hudi-common/src/main/java/org/apache/hudi/common/model/HoodieCommitMetadata.java:
##########
@@ -509,9 +512,13 @@ public int hashCode() {
 
   public static <T> T fromBytes(byte[] bytes, Class<T> clazz) throws 
IOException {
     try {
-      return fromJsonString(new String(bytes, StandardCharsets.UTF_8), clazz);
+      return fromJsonString(
+          new String(
+              
convertCommitMetadataToJsonBytes(deserializeCommitMetadata(bytes), 
org.apache.hudi.avro.model.HoodieCommitMetadata.class),
+              StandardCharsets.UTF_8),

Review Comment:
   Can we make `deserializeCommitMetadata` returns JSON bytes directly ?



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/BaseHoodieWriteClient.java:
##########
@@ -283,8 +287,11 @@ protected void commit(HoodieTable table, String 
commitActionType, String instant
     }
     // update Metadata table
     writeTableMetadata(table, instantTime, metadata, writeStatuses);
-    activeTimeline.saveAsComplete(new HoodieInstant(true, commitActionType, 
instantTime),
-        Option.of(metadata.toJsonString().getBytes(StandardCharsets.UTF_8)));
+    activeTimeline.saveAsComplete(
+        new HoodieInstant(true, commitActionType, instantTime),
+        metadata instanceof HoodieReplaceCommitMetadata
+            ? 
serializeReplaceCommitMetadata(convertReplaceCommitMetadata((HoodieReplaceCommitMetadata)
 metadata))
+            : serializeCommitMetadata(convertCommitMetadata(metadata)));
   }

Review Comment:
   I see the nested invocation of 
`serializeReplaceCommitMetadata(convertReplaceCommitMetadata(` everywhere, if 
that is alway a combination, I suggest we move the conversion 
`convertReplaceCommitMetadata` inside the method 
`serializeReplaceCommitMetadata`.
   
   Also we can remove `serializeReplaceCommitMetadata` and fuse its logic into 
`serializeCommitMetadata`.



##########
hudi-common/src/main/java/org/apache/hudi/common/table/timeline/MetadataConversionUtils.java:
##########
@@ -244,18 +246,44 @@ public static Option<HoodieCommitMetadata> 
getHoodieCommitMetadata(HoodieTableMe
     return Option.of(TimelineUtils.getCommitMetadata(hoodieInstant, timeline));
   }
 
-  public static org.apache.hudi.avro.model.HoodieCommitMetadata 
convertCommitMetadata(
-          HoodieCommitMetadata hoodieCommitMetadata) {
-    ObjectMapper mapper = new ObjectMapper();
-    // Need this to ignore other public get() methods
-    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-    org.apache.hudi.avro.model.HoodieCommitMetadata avroMetaData =
-            mapper.convertValue(hoodieCommitMetadata, 
org.apache.hudi.avro.model.HoodieCommitMetadata.class);
+  /**
+   * Convert commit metadata from json to avro.
+   */
+  public static <T extends SpecificRecordBase> T 
convertCommitMetadata(HoodieCommitMetadata hoodieCommitMetadata) {
+    if (hoodieCommitMetadata instanceof HoodieReplaceCommitMetadata) {
+      return (T) convertReplaceCommitMetadata((HoodieReplaceCommitMetadata) 
hoodieCommitMetadata);
+    }
+    hoodieCommitMetadata.getPartitionToWriteStats().remove(null);
+    org.apache.hudi.avro.model.HoodieCommitMetadata avroMetaData = 
JsonUtils.getObjectMapper().convertValue(hoodieCommitMetadata, 
org.apache.hudi.avro.model.HoodieCommitMetadata.class);
     if (hoodieCommitMetadata.getCompacted()) {
       avroMetaData.setOperationType(WriteOperationType.COMPACT.name());
     }
-    // Do not archive Rolling Stats, cannot set to null since AVRO will throw 
null pointer
-    
avroMetaData.getExtraMetadata().put(HoodieRollingStatMetadata.ROLLING_STAT_METADATA_KEY,
 "");
-    return avroMetaData;
+    return (T) avroMetaData;
+  }
+
+  /**
+   * Convert replacecommit metadata from json to avro.
+   */
+  public static org.apache.hudi.avro.model.HoodieReplaceCommitMetadata 
convertReplaceCommitMetadata(HoodieReplaceCommitMetadata replaceCommitMetadata) 
{
+    replaceCommitMetadata.getPartitionToWriteStats().remove(null);
+    replaceCommitMetadata.getPartitionToReplaceFileIds().remove(null);
+    return JsonUtils.getObjectMapper().convertValue(replaceCommitMetadata, 
org.apache.hudi.avro.model.HoodieReplaceCommitMetadata.class);
+  }
+
+  /**
+   * Convert commit metadata from avro to json.
+   */
+  public static <T extends SpecificRecordBase> byte[] 
convertCommitMetadataToJsonBytes(T avroMetaData, Class<T> clazz) {
+    Schema avroSchema = clazz == 
org.apache.hudi.avro.model.HoodieReplaceCommitMetadata.class ? 
org.apache.hudi.avro.model.HoodieReplaceCommitMetadata.getClassSchema() :
+        org.apache.hudi.avro.model.HoodieCommitMetadata.getClassSchema();

Review Comment:
   I see the instance of check for replace commit metadata and commit metadata 
everywhere, can we avoid that?



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

Reply via email to