This is an automated email from the ASF dual-hosted git repository.

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 7cb2d94b562 HDDS-16965. Initialize disk balancer state from config if 
diskBalancer.info is absent (#11503)
7cb2d94b562 is described below

commit 7cb2d94b5626afe607c6bb0b084b6742a77cf155
Author: Mark Tsai <[email protected]>
AuthorDate: Mon Oct 19 00:26:49 2026 +0800

    HDDS-16965. Initialize disk balancer state from config if diskBalancer.info 
is absent (#11503)
---
 .../diskbalancer/DiskBalancerConfiguration.java    | 52 +++++++++++++++++++---
 .../diskbalancer/DiskBalancerService.java          |  7 +--
 .../diskbalancer/TestDiskBalancerService.java      | 44 ++++++++++++++++++
 3 files changed, 91 insertions(+), 12 deletions(-)

diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerConfiguration.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerConfiguration.java
index 2902d6962ec..5b35f623495 100644
--- 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerConfiguration.java
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerConfiguration.java
@@ -32,6 +32,7 @@
 import org.apache.hadoop.hdds.conf.ConfigGroup;
 import org.apache.hadoop.hdds.conf.ConfigTag;
 import org.apache.hadoop.hdds.conf.ConfigType;
+import org.apache.hadoop.hdds.conf.PostConstruct;
 import 
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerDataProto.State;
 import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
 import org.slf4j.Logger;
@@ -51,6 +52,17 @@ public final class DiskBalancerConfiguration {
    */
   public static final String DEFAULT_CONTAINER_STATES = "CLOSED,QUASI_CLOSED";
 
+  public static final String HDDS_DATANODE_DISK_BALANCER_THRESHOLD =
+      "hdds.datanode.disk.balancer.volume.density.threshold.percent";
+  public static final String HDDS_DATANODE_DISK_BALANCER_BANDWIDTH =
+      "hdds.datanode.disk.balancer.max.disk.throughputInMBPerSec";
+  public static final String HDDS_DATANODE_DISK_BALANCER_PARALLEL_THREAD =
+      "hdds.datanode.disk.balancer.parallel.thread";
+
+  public static final double DEFAULT_THRESHOLD = 10d;
+  public static final long DEFAULT_DISK_BANDWIDTH_IN_MB = 10L;
+  public static final int DEFAULT_PARALLEL_THREAD = 5;
+
   @Config(key = "hdds.datanode.disk.balancer.info.dir", type = 
ConfigType.STRING,
       defaultValue = "", tags = {ConfigTag.DISKBALANCER},
       description = "The path where datanode diskBalancer's conf is to be " +
@@ -58,24 +70,24 @@ public final class DiskBalancerConfiguration {
           "back to use metadata directory instead.")
   private String infoDir;
 
-  @Config(key = 
"hdds.datanode.disk.balancer.volume.density.threshold.percent", type = 
ConfigType.DOUBLE,
+  @Config(key = HDDS_DATANODE_DISK_BALANCER_THRESHOLD, type = 
ConfigType.DOUBLE,
       defaultValue = "10", tags = {ConfigTag.DISKBALANCER},
       description = "Threshold is a percentage in the range of 0 to 100. A " +
           "datanode is considered balanced if for each volume, the " +
           "utilization of the volume(used space to capacity ratio) differs" +
           " from the utilization of the datanode(used space to capacity ratio" 
+
           " of the entire datanode) no more than the threshold.")
-  private double threshold = 10d;
+  private double threshold = DEFAULT_THRESHOLD;
 
-  @Config(key = "hdds.datanode.disk.balancer.max.disk.throughputInMBPerSec", 
type = ConfigType.LONG,
+  @Config(key = HDDS_DATANODE_DISK_BALANCER_BANDWIDTH, type = ConfigType.LONG,
       defaultValue = "10", tags = {ConfigTag.DISKBALANCER},
       description = "The max balance speed.")
-  private long diskBandwidthInMB = 10;
+  private long diskBandwidthInMB = DEFAULT_DISK_BANDWIDTH_IN_MB;
 
-  @Config(key = "hdds.datanode.disk.balancer.parallel.thread", type = 
ConfigType.INT,
+  @Config(key = HDDS_DATANODE_DISK_BALANCER_PARALLEL_THREAD, type = 
ConfigType.INT,
       defaultValue = "5", tags = {ConfigTag.DISKBALANCER},
       description = "The max parallel balance thread count.")
-  private int parallelThread = 5;
+  private int parallelThread = DEFAULT_PARALLEL_THREAD;
 
   @Config(key = "hdds.datanode.disk.balancer.should.run.default",
       defaultValue = "false",
@@ -169,6 +181,32 @@ public DiskBalancerConfiguration(Double threshold,
   public DiskBalancerConfiguration() {
   }
 
+  private static boolean isValidThreshold(double threshold) {
+    return Double.isFinite(threshold) && threshold > 0d && threshold < 100d;
+  }
+
+  @PostConstruct
+  public void validate() {
+    if (!isValidThreshold(threshold)) {
+      LOG.warn(HDDS_DATANODE_DISK_BALANCER_THRESHOLD +
+              " must be in the range 0 to 100 both exclusive and was set to 
{}. Defaulting to {}",
+          threshold, DEFAULT_THRESHOLD);
+      threshold = DEFAULT_THRESHOLD;
+    }
+
+    if (diskBandwidthInMB <= 0L) {
+      LOG.warn(HDDS_DATANODE_DISK_BALANCER_BANDWIDTH + " must be > 0 and was 
set to {}. Defaulting to {}",
+          diskBandwidthInMB, DEFAULT_DISK_BANDWIDTH_IN_MB);
+      diskBandwidthInMB = DEFAULT_DISK_BANDWIDTH_IN_MB;
+    }
+
+    if (parallelThread <= 0) {
+      LOG.warn(HDDS_DATANODE_DISK_BALANCER_PARALLEL_THREAD + " must be > 0 and 
was set to {}. Defaulting to {}",
+          parallelThread, DEFAULT_PARALLEL_THREAD);
+      parallelThread = DEFAULT_PARALLEL_THREAD;
+    }
+  }
+
   public String getDiskBalancerInfoDir() {
     return infoDir;
   }
@@ -237,7 +275,7 @@ public double getThresholdAsRatio() {
    * @param threshold a percentage value in the range (0 to 100) both exclusive
    */
   public void setThreshold(double threshold) {
-    if (threshold <= 0d || threshold >= 100d) {
+    if (!isValidThreshold(threshold)) {
       throw new IllegalArgumentException(
           "Threshold must be a percentage(double) in the range 0 to 100 both 
exclusive.");
     }
diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerService.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerService.java
index 7ab0243db06..e05afaa2752 100644
--- 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerService.java
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerService.java
@@ -240,11 +240,8 @@ private void loadDiskBalancerInfo() throws IOException {
       if (diskBalancerInfoFile.exists()) {
         diskBalancerInfo = readDiskBalancerInfoFile(diskBalancerInfoFile);
       } else {
-        boolean shouldRunDefault =
-            conf.getObject(DiskBalancerConfiguration.class)
-                .getDiskBalancerShouldRun();
-        diskBalancerInfo = new DiskBalancerInfo(shouldRunDefault,
-            new DiskBalancerConfiguration());
+        DiskBalancerConfiguration diskBalancerConf = 
conf.getObject(DiskBalancerConfiguration.class);
+        diskBalancerInfo = new 
DiskBalancerInfo(diskBalancerConf.getDiskBalancerShouldRun(), diskBalancerConf);
       }
     } catch (IOException e) {
       LOG.warn("Can not load diskBalancerInfo from diskBalancer.info file. " +
diff --git 
a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/diskbalancer/TestDiskBalancerService.java
 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/diskbalancer/TestDiskBalancerService.java
index ad336f12dc9..5e79fb70c26 100644
--- 
a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/diskbalancer/TestDiskBalancerService.java
+++ 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/diskbalancer/TestDiskBalancerService.java
@@ -79,6 +79,7 @@
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
 
 /**
  * This is a test class for DiskBalancerService.
@@ -423,6 +424,46 @@ public void 
testDiskBalancerInfoWriteCreatesParentDirectory(
     svc.shutdown();
   }
 
+  @Test
+  public void testInitialDiskBalancerInfoUsesConfiguredValues() throws 
Exception {
+    File infoDir = tmpDir.resolve("diskBalancer-initial-info").toFile();
+    OzoneConfiguration testConf = confWithDiskBalancerInfoDir(infoDir);
+    DiskBalancerConfiguration diskBalancerConf = 
testConf.getObject(DiskBalancerConfiguration.class);
+    diskBalancerConf.setThreshold(25.0d);
+    diskBalancerConf.setDiskBandwidthInMB(50L);
+    diskBalancerConf.setParallelThread(2);
+    diskBalancerConf.setStopAfterDiskEven(false);
+    diskBalancerConf.setContainerStates("CLOSED");
+    testConf.setFromObject(diskBalancerConf);
+    DiskBalancerInfo expected = new 
DiskBalancerInfo(DiskBalancerRunningStatus.STOPPED, 25.0d, 50L, 2, false,
+        "CLOSED", DiskBalancerVersion.DEFAULT_VERSION);
+
+    DiskBalancerServiceTestImpl svc = getDiskBalancerService(testConf);
+
+    assertEquals(expected, svc.getDiskBalancerInfo());
+    assertEquals(expected, 
DiskBalancerYaml.readDiskBalancerInfoFile(getDiskBalancerInfoFile(infoDir)));
+    svc.shutdown();
+  }
+
+  @ParameterizedTest
+  @ValueSource(strings = {"0", "NaN"})
+  public void 
testInitialDiskBalancerInfoFallsBackToDefaultsForInvalidConfig(String 
rawThreshold) throws Exception {
+    File infoDir = 
tmpDir.resolve("diskBalancer-invalid-initial-info").toFile();
+    OzoneConfiguration testConf = confWithDiskBalancerInfoDir(infoDir);
+    
testConf.set(DiskBalancerConfiguration.HDDS_DATANODE_DISK_BALANCER_THRESHOLD, 
rawThreshold);
+    
testConf.setLong(DiskBalancerConfiguration.HDDS_DATANODE_DISK_BALANCER_BANDWIDTH,
 0L);
+    
testConf.setInt(DiskBalancerConfiguration.HDDS_DATANODE_DISK_BALANCER_PARALLEL_THREAD,
 0);
+    DiskBalancerInfo expected = new 
DiskBalancerInfo(DiskBalancerRunningStatus.STOPPED,
+        DiskBalancerConfiguration.DEFAULT_THRESHOLD, 
DiskBalancerConfiguration.DEFAULT_DISK_BANDWIDTH_IN_MB,
+        DiskBalancerConfiguration.DEFAULT_PARALLEL_THREAD, true);
+
+    DiskBalancerServiceTestImpl svc = getDiskBalancerService(testConf);
+
+    assertEquals(expected, svc.getDiskBalancerInfo());
+    assertEquals(expected, 
DiskBalancerYaml.readDiskBalancerInfoFile(getDiskBalancerInfoFile(infoDir)));
+    svc.shutdown();
+  }
+
   @ContainerTestVersionInfo.ContainerTest
   public void testDiskBalancerInfoWriteReportsDirectoryCreationFailure(
       ContainerTestVersionInfo versionInfo) throws Exception {
@@ -527,6 +568,9 @@ public static Stream<Arguments> 
thresholdValidationTestCases() {
         Arguments.arguments(-1.0, true, null),
         Arguments.arguments(-0.001, true, null),
         Arguments.arguments(100.001, true, null),
+        Arguments.arguments(Double.NaN, true, null),
+        Arguments.arguments(Double.POSITIVE_INFINITY, true, null),
+        Arguments.arguments(Double.NEGATIVE_INFINITY, true, null),
         // Valid boundary values that should be accepted
         Arguments.arguments(0.001, false, 0.001),
         Arguments.arguments(99.999, false, 99.999),


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

Reply via email to