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 6dd41bede5c HDDS-16278. Reuse DecimalFormat in SCMNodeManager 
statistics (#11113)
6dd41bede5c is described below

commit 6dd41bede5c0fe15517df0873ad4226446d7ee24
Author: KUAN-HAO HUANG <[email protected]>
AuthorDate: Fri Sep 18 22:35:38 2026 +0800

    HDDS-16278. Reuse DecimalFormat in SCMNodeManager statistics (#11113)
---
 .../hadoop/hdds/scm/node/SCMNodeManager.java       | 25 +++++++++++++++-------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/node/SCMNodeManager.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/node/SCMNodeManager.java
index fe886bf0c20..c8b100bd9c1 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/node/SCMNodeManager.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/node/SCMNodeManager.java
@@ -158,6 +158,20 @@ public class SCMNodeManager implements NodeManager, 
ContainerReplicaPendingOpsSu
   private static final String DNUUID = "UUID";
   private static final String VERSION = "VERSION";
 
+  // DecimalFormat is not thread-safe; keep one per thread and reuse across 
node-stats/JMX loops.
+  private static final ThreadLocal<DecimalFormat> ONE_DECIMAL_FORMAT =
+      ThreadLocal.withInitial(() -> {
+        DecimalFormat format = new DecimalFormat("#0.0");
+        format.setRoundingMode(RoundingMode.HALF_UP);
+        return format;
+      });
+  private static final ThreadLocal<DecimalFormat> TWO_DECIMAL_FORMAT =
+      ThreadLocal.withInitial(() -> {
+        DecimalFormat format = new DecimalFormat("#0.00");
+        format.setRoundingMode(RoundingMode.HALF_UP);
+        return format;
+      });
+
   /**
    * Constructs SCM machine Manager.
    */
@@ -1329,9 +1343,7 @@ private static String convertUnit(double value) {
       unit.replace(0, 2, "TB");
     }
 
-    DecimalFormat decimalFormat = new DecimalFormat("#0.0");
-    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
-    String newValue = decimalFormat.format(value);
+    String newValue = ONE_DECIMAL_FORMAT.get().format(value);
     return newValue + unit.toString();
   }
 
@@ -1356,8 +1368,7 @@ public static String[] calculateStoragePercentage(
       }
       long scmNonUsed = capacity - scmUsed - remaining;
 
-      DecimalFormat decimalFormat = new DecimalFormat("#0.00");
-      decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
+      DecimalFormat decimalFormat = TWO_DECIMAL_FORMAT.get();
 
       double usedPerc = ((double) scmUsed / capacity) * 100;
       usedPerc = usedPerc > 100.0 ? 100.0 : usedPerc;
@@ -1420,9 +1431,7 @@ private void nodeUsageStatistics(Map<String, String> 
nodeStatics) {
       dev += (usages[i] - totalOzoneUsed) * (usages[i] - totalOzoneUsed);
     }
     dev = (float) Math.sqrt(dev / usages.length);
-    DecimalFormat decimalFormat = new DecimalFormat("#0.00");
-    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
-    nodeStatics.put(UsageStatics.STDEV.getLabel(), decimalFormat.format(dev));
+    nodeStatics.put(UsageStatics.STDEV.getLabel(), 
TWO_DECIMAL_FORMAT.get().format(dev));
   }
 
   private void nodeStateStatistics(Map<String, String> nodeStatics) {


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

Reply via email to