You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by ji...@apache.org on 2026/09/17 17:07:37 UTC

(hugegraph) branch master updated: fix(core): format the graph space storage percentage with Locale.ROOT (#3216)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4dc311b32 fix(core): format the graph space storage percentage with Locale.ROOT (#3216)
4dc311b32 is described below

commit 4dc311b32ab0ef20fcf3670fd8860cdf90626f04
Author: Sebastian Gruza <se...@gruzalab.pl>
AuthorDate: Thu Sep 17 19:07:32 2026 +0200

    fix(core): format the graph space storage percentage with Locale.ROOT (#3216)
    
    Co-authored-by: imbajin <ji...@apache.org>
---
 .../org/apache/hugegraph/space/GraphSpace.java     |  7 ++-
 .../org/apache/hugegraph/unit/UnitTestSuite.java   |  2 +
 .../unit/core/GraphSpaceInfoLocaleTest.java        | 66 ++++++++++++++++++++++
 3 files changed, 73 insertions(+), 2 deletions(-)

diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/space/GraphSpace.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/space/GraphSpace.java
index 5d91aa9f2..f821809b6 100644
--- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/space/GraphSpace.java
+++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/space/GraphSpace.java
@@ -20,6 +20,7 @@ package org.apache.hugegraph.space;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
+import java.util.Locale;
 import java.util.Map;
 
 import org.apache.commons.lang.StringUtils;
@@ -389,9 +390,11 @@ public class GraphSpace {
         infos.put("cpu_used", this.cpuUsed);
         infos.put("memory_used", this.memoryUsed);
         infos.put("storage_used", this.storageUsed);
+        // Locale.ROOT: the default locale may format with a decimal comma
+        // ("0,33" on pl_PL, de_DE, fr_FR, ...), which parseFloat() rejects
         float storageUserPercent = Float.parseFloat(
-                String.format("%.2f", (float) this.storageUsed /
-                                      ((float) this.storageLimit * 1.0)));
+                String.format(Locale.ROOT, "%.2f", (float) this.storageUsed /
+                                                   ((float) this.storageLimit * 1.0)));
         infos.put("storage_percent", storageUserPercent);
         infos.put("graph_number_used", this.graphNumberUsed);
         infos.put("role_number_used", this.roleNumberUsed);
diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java
index 4bde4f01c..0a9c621f9 100644
--- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java
+++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java
@@ -50,6 +50,7 @@ import org.apache.hugegraph.unit.core.BackendStoreInfoTest;
 import org.apache.hugegraph.unit.core.ConditionQueryFlattenTest;
 import org.apache.hugegraph.unit.core.ConditionTest;
 import org.apache.hugegraph.unit.core.DataTypeTest;
+import org.apache.hugegraph.unit.core.GraphSpaceInfoLocaleTest;
 import org.apache.hugegraph.unit.core.GraphManagerStoresWaitTest;
 import org.apache.hugegraph.unit.core.DirectionsTest;
 import org.apache.hugegraph.unit.core.ExceptionTest;
@@ -138,6 +139,7 @@ import org.junit.runners.Suite;
 
         /* types */
         DataTypeTest.class,
+        GraphSpaceInfoLocaleTest.class,
         GraphManagerStoresWaitTest.class,
         DirectionsTest.class,
         SerialEnumTest.class,
diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphSpaceInfoLocaleTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphSpaceInfoLocaleTest.java
new file mode 100644
index 000000000..973890df7
--- /dev/null
+++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphSpaceInfoLocaleTest.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hugegraph.unit.core;
+
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.hugegraph.space.GraphSpace;
+import org.apache.hugegraph.testutil.Assert;
+import org.apache.hugegraph.unit.BaseUnitTest;
+import org.junit.Test;
+
+/**
+ * GraphSpace.info() formats the storage percentage and parses it back;
+ * that must not depend on the JVM's default locale (a decimal comma on
+ * pl_PL, de_DE, fr_FR, ... used to fail with NumberFormatException).
+ */
+public class GraphSpaceInfoLocaleTest extends BaseUnitTest {
+
+    private static float storagePercent(Locale locale) {
+        Locale savedFormat = Locale.getDefault(Locale.Category.FORMAT);
+        Locale savedDisplay = Locale.getDefault(Locale.Category.DISPLAY);
+        Locale.setDefault(locale);
+        try {
+            GraphSpace space = new GraphSpace("gs_locale");
+            space.storageLimit(100);
+            space.setStorageUsed(33);
+            Map<String, Object> info = space.info();
+            return (Float) info.get("storage_percent");
+        } finally {
+            Locale.setDefault(Locale.Category.FORMAT, savedFormat);
+            Locale.setDefault(Locale.Category.DISPLAY, savedDisplay);
+        }
+    }
+
+    @Test
+    public void testInfoWithDecimalCommaLocales() {
+        for (Locale locale : new Locale[]{new Locale("pl", "PL"),
+                                          Locale.GERMANY, Locale.FRANCE,
+                                          new Locale("ru", "RU")}) {
+            Assert.assertEquals(locale.toString(), 0.33f,
+                                storagePercent(locale), 0.0001f);
+        }
+    }
+
+    @Test
+    public void testInfoWithDecimalPointLocales() {
+        Assert.assertEquals(0.33f, storagePercent(Locale.ROOT), 0.0001f);
+        Assert.assertEquals(0.33f, storagePercent(Locale.US), 0.0001f);
+    }
+}