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


##########
hudi-common/src/main/java/org/apache/hudi/common/fs/FSUtils.java:
##########
@@ -846,6 +846,50 @@ public static List<FileStatus> getFileStatusAtLevel(
     return result;
   }
 
+  /**
+   * Renames the source file to destination, using the provided file system. 
If the rename failed,
+   * an attempt will be made to delete the source file.
+   */
+  public static void renameFile(FileSystem fs, Path src, Path dst) {
+    try {
+      if (fs.exists(dst)) {
+        throw new HoodieIOException("Target file " + dst + " already exists");
+      }
+      if (!fs.rename(src, dst)) {
+        handleException(fs, src, dst);
+      }
+    } catch (IOException e) {
+      handleException(fs, src, dst);
+    }
+  }
+
+  /**
+   * Handles the exception for renaming.
+   */
+  private static void handleException(FileSystem fs, Path src, Path dst) {
+    HoodieIOException ioe = new HoodieIOException("Failed to rename " + src + 
" to " + dst);
+    IOException ex = tryDelete(fs, src);
+    if (ex != null) {
+      ioe.addSuppressed(ex);
+    }
+    throw ioe;
+  }
+
+  /**
+   * Deletes the file from the file system.
+   *
+   * @param path the file to be deleted.
+   * @return IOException caught, if any. Null otherwise.
+   */
+  private static IOException tryDelete(FileSystem fs, Path path) {

Review Comment:
   Removed.



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