boneanxs commented on code in PR #9472:
URL: https://github.com/apache/hudi/pull/9472#discussion_r1300858914


##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/cluster/ReplaceCommitValidateUtil.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.hudi.table.action.cluster;
+
+import org.apache.hudi.common.model.HoodieReplaceCommitMetadata;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.JsonUtils;
+import org.apache.hudi.exception.HoodieException;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Stream;
+
+import static org.apache.hudi.common.util.FileIOUtils.LOG;
+
+public class ReplaceCommitValidateUtil {
+  public static final String REPLACE_COMMIT_FILE_IDS = "replaceCommitFileIds";
+  public static void validateReplaceCommit(HoodieTableMetaClient metaClient) {
+    metaClient.reloadActiveTimeline();
+    Set<String> replaceFileids = new HashSet<>();
+
+    // Verify pending and completed replace commit
+    
Stream.concat(metaClient.getActiveTimeline().getCompletedReplaceTimeline().getInstants().stream(),
+        
metaClient.getActiveTimeline().filterInflights().filterPendingReplaceTimeline().getInstants().stream()).map(instant
 -> {
+          try {
+            HoodieReplaceCommitMetadata replaceCommitMetadata =
+                
HoodieReplaceCommitMetadata.fromBytes(metaClient.getActiveTimeline().getInstantDetails(instant).get(),
+                HoodieReplaceCommitMetadata.class);
+            if (!instant.isCompleted()) {
+              return 
JsonUtils.getObjectMapper().readValue(replaceCommitMetadata.getExtraMetadata().getOrDefault(REPLACE_COMMIT_FILE_IDS,
 "non-existent key"), String[].class);
+            } else {
+              return 
replaceCommitMetadata.getPartitionToReplaceFileIds().values().stream()
+                  .flatMap(List::stream)
+                  .toArray(String[]::new);
+            }
+          } catch (IOException e) {
+            // If the key does not exist or there is a JSON parsing error, LOG 
reports an error and ignores it.
+            LOG.error("Error when reading replace commit meta", e);
+            return null;
+          }
+        }).filter(Objects::nonNull)
+        .forEach(fileIdArray -> {
+          Arrays.stream(fileIdArray)
+              .filter(fileId -> !replaceFileids.add(fileId))
+              .findFirst()
+              .ifPresent(s -> {
+                throw new HoodieException("Replace commit involves duplicate 
file id!");

Review Comment:
   Can make the exception more clear



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/cluster/ReplaceCommitValidateUtil.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.hudi.table.action.cluster;
+
+import org.apache.hudi.common.model.HoodieReplaceCommitMetadata;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.JsonUtils;
+import org.apache.hudi.exception.HoodieException;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Stream;
+
+import static org.apache.hudi.common.util.FileIOUtils.LOG;
+
+public class ReplaceCommitValidateUtil {
+  public static final String REPLACE_COMMIT_FILE_IDS = "replaceCommitFileIds";
+  public static void validateReplaceCommit(HoodieTableMetaClient metaClient) {
+    metaClient.reloadActiveTimeline();
+    Set<String> replaceFileids = new HashSet<>();
+
+    // Verify pending and completed replace commit
+    
Stream.concat(metaClient.getActiveTimeline().getCompletedReplaceTimeline().getInstants().stream(),
+        
metaClient.getActiveTimeline().filterInflights().filterPendingReplaceTimeline().getInstants().stream()).map(instant
 -> {
+          try {
+            HoodieReplaceCommitMetadata replaceCommitMetadata =
+                
HoodieReplaceCommitMetadata.fromBytes(metaClient.getActiveTimeline().getInstantDetails(instant).get(),
+                HoodieReplaceCommitMetadata.class);
+            if (!instant.isCompleted()) {
+              return 
JsonUtils.getObjectMapper().readValue(replaceCommitMetadata.getExtraMetadata().getOrDefault(REPLACE_COMMIT_FILE_IDS,
 "non-existent key"), String[].class);
+            } else {
+              return 
replaceCommitMetadata.getPartitionToReplaceFileIds().values().stream()
+                  .flatMap(List::stream)
+                  .toArray(String[]::new);
+            }
+          } catch (IOException e) {
+            // If the key does not exist or there is a JSON parsing error, LOG 
reports an error and ignores it.
+            LOG.error("Error when reading replace commit meta", e);
+            return null;
+          }
+        }).filter(Objects::nonNull)
+        .forEach(fileIdArray -> {
+          Arrays.stream(fileIdArray)
+              .filter(fileId -> !replaceFileids.add(fileId))
+              .findFirst()
+              .ifPresent(s -> {
+                throw new HoodieException("Replace commit involves duplicate 
file id!");
+              });
+        });
+  }
+}

Review Comment:
   Missing empty line here



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/cluster/ReplaceCommitValidateUtil.java:
##########
@@ -0,0 +1,49 @@
+package org.apache.hudi.table.action.cluster;
+
+import org.apache.hudi.common.model.HoodieReplaceCommitMetadata;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.JsonUtils;
+import org.apache.hudi.exception.HoodieException;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Stream;
+
+import static org.apache.hudi.common.util.FileIOUtils.LOG;
+
+public class ReplaceCommitValidateUtil {
+  public static void validateReplaceCommit(HoodieTableMetaClient metaClient) {
+    metaClient.reloadActiveTimeline();
+    Set<String> replaceFileids = new HashSet<>();
+
+    // Verify pending and completed replace commit
+    
Stream.concat(metaClient.getActiveTimeline().getCompletedReplaceTimeline().getInstants().stream(),
+        
metaClient.getActiveTimeline().filterInflights().getInstants().stream()).parallel().map(instant
 -> {
+      try {
+        HoodieReplaceCommitMetadata replaceCommitMetadata =
+            
HoodieReplaceCommitMetadata.fromBytes(metaClient.getActiveTimeline().getInstantDetails(instant).get(),
+                HoodieReplaceCommitMetadata.class);
+        if (!instant.isCompleted()) {
+          return 
JsonUtils.getObjectMapper().readValue(replaceCommitMetadata.getExtraMetadata().getOrDefault("replaceCommitFileIds",
 ""), String[].class);
+        } else {
+          return 
replaceCommitMetadata.getPartitionToReplaceFileIds().values().stream()
+              .flatMap(List::stream)
+              .toArray(String[]::new);
+        }
+      } catch (IOException e) {

Review Comment:
   I think we better throw the exception here instead of return null



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/cluster/ReplaceCommitValidateUtil.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.hudi.table.action.cluster;
+
+import org.apache.hudi.common.model.HoodieReplaceCommitMetadata;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.JsonUtils;
+import org.apache.hudi.exception.HoodieException;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Stream;
+
+import static org.apache.hudi.common.util.FileIOUtils.LOG;
+
+public class ReplaceCommitValidateUtil {
+  public static final String REPLACE_COMMIT_FILE_IDS = "replaceCommitFileIds";
+  public static void validateReplaceCommit(HoodieTableMetaClient metaClient) {
+    metaClient.reloadActiveTimeline();
+    Set<String> replaceFileids = new HashSet<>();
+
+    // Verify pending and completed replace commit
+    
Stream.concat(metaClient.getActiveTimeline().getCompletedReplaceTimeline().getInstants().stream(),
+        
metaClient.getActiveTimeline().filterInflights().filterPendingReplaceTimeline().getInstants().stream()).map(instant
 -> {

Review Comment:
   `getInstants().stream()` -> `getInstantsAsStream()`



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/cluster/ReplaceCommitValidateUtil.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.hudi.table.action.cluster;
+
+import org.apache.hudi.common.model.HoodieReplaceCommitMetadata;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.JsonUtils;
+import org.apache.hudi.exception.HoodieException;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Stream;
+
+import static org.apache.hudi.common.util.FileIOUtils.LOG;
+
+public class ReplaceCommitValidateUtil {
+  public static final String REPLACE_COMMIT_FILE_IDS = "replaceCommitFileIds";

Review Comment:
   I'm thinking we can directly use 
`replaceCommitMetadata.partitionToReplaceFileIds` given the inflight replace 
commits actually don't use it. This way we don't need to validate it 
differently for `inflight` and `complete`. 



-- 
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: commits-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to