hudi-agent commented on code in PR #18776:
URL: https://github.com/apache/hudi/pull/18776#discussion_r3267048836


##########
hudi-hadoop-common/src/main/java/org/apache/hudi/parquet/io/HoodieParquetBinaryCopyBase.java:
##########
@@ -147,17 +154,41 @@ protected void initFileWriter(Path outPutFile, 
CompressionCodecName newCodecName
 
   @Override
   public void close() throws IOException {
+    if (writer == null) {
+      return;
+    }
     Map<String, String> extraMetaData = finalizeMetadata();
     extraMetaData = extraMetaData == null ? new HashMap<>() : extraMetaData;
     extraMetaData.remove("parquet.avro.schema");
     extraMetaData.remove("org.apache.spark.sql.parquet.row.metadata");
-    writer.end(extraMetaData);
-    // Release the buffer
-    reusableBlockBuffer = null;
+    try {
+      writer.end(extraMetaData);
+    } finally {
+      writer = null;
+      // Release the buffer
+      reusableBlockBuffer = null;
+    }
   }
 
   protected abstract Map<String, String> finalizeMetadata();
 
+  private void closeParquetFileWriterQuietly(ParquetFileWriter 
parquetFileWriter) {
+    if (parquetFileWriter == null) {
+      return;
+    }
+    Method closeMethod;

Review Comment:
   🤖 nit: the reflection here is non-obvious — it might be worth adding a 
one-line comment like `// ParquetFileWriter doesn't implement Closeable in all 
versions, so we attempt close() reflectively` so future readers understand why 
a direct cast to AutoCloseable wasn't used.
   
   <sub><i>- AI-generated; verify before applying. React 👍/👎 to flag 
quality.</i></sub>



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/util/AutoClosableUtils.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+/**
+ * Utility methods for closing {@link AutoCloseable} resources.
+ */
+public final class AutoClosableUtils {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(AutoClosableUtils.class);
+
+  private AutoClosableUtils() {
+  }
+
+  public static void closeWithSuppressed(AutoCloseable closeable, Throwable 
failure) throws IOException {
+    if (closeable == null) {
+      return;
+    }
+    try {
+      closeable.close();
+    } catch (IOException ioe) {
+      if (failure != null) {
+        failure.addSuppressed(ioe);
+      } else {
+        throw ioe;
+      }
+    } catch (RuntimeException re) {
+      if (failure != null) {
+        failure.addSuppressed(re);
+      } else {
+        throw re;
+      }
+    } catch (Exception e) {
+      if (failure != null) {
+        failure.addSuppressed(e);
+      } else {
+        throw new IOException("Failed to close resource", e);
+      }
+    }
+  }
+
+  public static void closeQuietlyWithSuppressed(AutoCloseable closeable, 
Throwable failure) {
+    try {
+      closeWithSuppressed(closeable, failure);
+    } catch (IOException | RuntimeException e) {
+      if (failure != null) {

Review Comment:
   🤖 Minor: this `if (failure != null)` branch looks unreachable — 
`closeWithSuppressed` only throws when `failure == null` (when failure is 
non-null it always routes exceptions to `failure.addSuppressed`). The catch 
block here is only entered when failure is null, so the LOG.warn branch is the 
only reachable one. Could simplify to just `LOG.warn(...)`.
   
   <sub><i>- AI-generated; verify before applying. React 👍/👎 to flag 
quality.</i></sub>



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