slfan1989 commented on code in PR #10284:
URL: https://github.com/apache/ozone/pull/10284#discussion_r3252586161


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerService.java:
##########
@@ -332,17 +332,58 @@ private synchronized DiskBalancerInfo 
readDiskBalancerInfoFile(
   private synchronized void writeDiskBalancerInfoTo(
       DiskBalancerInfo diskBalancerInfo, File path)
       throws IOException {
-    if (path.exists()) {
-      if (!path.delete() || !path.createNewFile()) {
-        throw new IOException("Unable to overwrite the DiskBalancerInfo 
file.");
+    writeDiskBalancerInfoAtomically(
+        diskBalancerInfo, path, DiskBalancerYaml::createDiskBalancerInfoFile);
+  }
+
+  @VisibleForTesting
+  static void writeDiskBalancerInfoAtomically(DiskBalancerInfo 
diskBalancerInfo,
+      File path, DiskBalancerInfoWriter writer) throws IOException {
+    Path target = path.toPath().toAbsolutePath();
+    Path parent = target.getParent();
+    if (parent == null) {
+      throw new IOException(
+          "Unable to determine parent directory for DiskBalancerInfo file: "
+              + target);
+    }
+    try {
+      Files.createDirectories(parent);
+    } catch (IOException e) {
+      throw new IOException(
+          "Unable to create DiskBalancerInfo directories: " + parent, e);
+    }
+
+    final Path tempFile;
+    try {
+      tempFile = Files.createTempFile(parent, path.getName(), ".tmp");
+    } catch (IOException e) {
+      throw new IOException(
+          "Unable to create temporary DiskBalancerInfo file under: "
+              + parent, e);
+    }
+    boolean moved = false;
+    try {
+      writer.write(diskBalancerInfo, tempFile.toFile());
+      try {
+        Files.move(tempFile, target, StandardCopyOption.ATOMIC_MOVE,
+            StandardCopyOption.REPLACE_EXISTING);
+      } catch (IOException e) {
+        throw new IOException(
+            "Unable to overwrite the DiskBalancerInfo file: " + target, e);
       }

Review Comment:
   Updated. Removed the redundant temp-file and atomic move wrapper. 
DiskBalancerService now only creates the parent directory and relies on 
DiskBalancerYaml/YamlUtils, which already uses AtomicFileOutputStream for 
atomic writes.
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to