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


##########
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:
   Temp file and atomic move to final location is not needed, because 
`YamlUtils` uses `AtomicFileOutputStream` internally, which performs the same.
   
   
https://github.com/apache/ozone/blob/003b26a27f4240674d02ce3ebbd7a772426d2c5d/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerYaml.java#L59-L60
   
   
https://github.com/apache/ozone/blob/003b26a27f4240674d02ce3ebbd7a772426d2c5d/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/YamlUtils.java#L55-L56



##########
hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/diskbalancer/TestDiskBalancerService.java:
##########
@@ -379,6 +379,60 @@ public void testConcurrentTasksNotExceedThreadLimit() 
throws Exception {
         100, 5000);
   }
 
+  @Test
+  public void testDiskBalancerInfoAtomicWritePreservesExistingFileOnFailure()
+      throws Exception {
+    File infoFile = tmpDir.resolve("diskBalancer.info").toFile();
+    DiskBalancerInfo oldInfo = new DiskBalancerInfo(
+        DiskBalancerRunningStatus.STOPPED, 10.0d, 100L, 5, true);
+    DiskBalancerInfo newInfo = new DiskBalancerInfo(
+        DiskBalancerRunningStatus.RUNNING, 20.0d, 200L, 10, false);
+    DiskBalancerYaml.createDiskBalancerInfoFile(oldInfo, infoFile);
+
+    IOException exception = assertThrows(IOException.class,
+        () -> DiskBalancerService.writeDiskBalancerInfoAtomically(
+            newInfo, infoFile, (info, path) -> {
+              throw new IOException("simulated write failure");
+            }));
+
+    assertEquals("simulated write failure", exception.getMessage());
+    assertEquals(oldInfo, DiskBalancerYaml.readDiskBalancerInfoFile(infoFile));
+  }
+
+  @Test
+  public void testDiskBalancerInfoAtomicWriteReportsDirectoryCreationFailure()
+      throws Exception {
+    File parent = tmpDir.resolve("diskBalancer-parent").toFile();
+    assertTrue(parent.createNewFile());
+    File infoFile = new File(parent, "diskBalancer.info");
+    DiskBalancerInfo info = new DiskBalancerInfo(
+        DiskBalancerRunningStatus.RUNNING, 10.0d, 100L, 5, true);
+
+    IOException exception = assertThrows(IOException.class,
+        () -> DiskBalancerService.writeDiskBalancerInfoAtomically(
+            info, infoFile, DiskBalancerYaml::createDiskBalancerInfoFile));
+
+    assertTrue(exception.getMessage()
+        .startsWith("Unable to create DiskBalancerInfo directories: "));
+  }
+
+  @Test
+  public void testDiskBalancerInfoAtomicWriteReportsOverwriteFailure()
+      throws Exception {
+    File infoFile = tmpDir.resolve("diskBalancer.info").toFile();
+    assertTrue(infoFile.mkdirs());
+    assertTrue(new File(infoFile, "existing").createNewFile());
+    DiskBalancerInfo info = new DiskBalancerInfo(
+        DiskBalancerRunningStatus.RUNNING, 10.0d, 100L, 5, true);
+
+    IOException exception = assertThrows(IOException.class,
+        () -> DiskBalancerService.writeDiskBalancerInfoAtomically(
+            info, infoFile, DiskBalancerYaml::createDiskBalancerInfoFile));
+
+    assertTrue(exception.getMessage()
+        .startsWith("Unable to overwrite the DiskBalancerInfo file: "));

Review Comment:
   nit: please replace with `assertThat(exception).hasMessageStartingWith(...)` 
(see HDDS-9951)



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

Review Comment:
   nit: please don't add `@VisibleForTesting` annotations (HDDS-12725)



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