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

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


The following commit(s) were added to refs/heads/master by this push:
     new 6e4d69006 ZOOKEEPER-4829: [ADDENDUM] Improve backward compatibility 
for QuorumPeerConfig::getPurgeInterval with 3.9
6e4d69006 is described below

commit 6e4d69006298a21896835dcdce678f37acc191b5
Author: Kezhu Wang <[email protected]>
AuthorDate: Fri May 9 22:45:15 2025 +0800

    ZOOKEEPER-4829: [ADDENDUM] Improve backward compatibility for 
QuorumPeerConfig::getPurgeInterval with 3.9
    
    Reviewers: li4wang, tisonkun
    Author: kezhuw
    Closes #2258 from 
kezhuw/ZOOKEEPER-4829-addendum-improve-backward-compatibility
---
 .../java/org/apache/zookeeper/common/Time.java     | 24 +++++++++++++++++++++-
 .../zookeeper/server/DatadirCleanupManager.java    |  3 ++-
 .../zookeeper/server/quorum/QuorumPeerConfig.java  | 10 +++++++++
 .../java/org/apache/zookeeper/common/TimeTest.java | 23 +++++++++++++++++++++
 4 files changed, 58 insertions(+), 2 deletions(-)

diff --git 
a/zookeeper-server/src/main/java/org/apache/zookeeper/common/Time.java 
b/zookeeper-server/src/main/java/org/apache/zookeeper/common/Time.java
index 9abe4d6a8..5916cd229 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/common/Time.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/Time.java
@@ -64,7 +64,6 @@ public static Date elapsedTimeToDate(long elapsedTime) {
      * @param str the interval string
      * @return interval in milliseconds
      */
-
     public static int parseTimeInterval(String str) {
         try {
             int len = str.length();
@@ -95,4 +94,27 @@ public static int parseTimeInterval(String str) {
         }
     }
 
+    /**
+     * Format milliseconds time interval with the highest time unit suffix.
+     *
+     * <p>See {@link #parseTimeInterval(String)} for supported suffixes.
+     *
+     * @param ms milliseconds
+     * @return string formatted with the highest time unit suffix or "0" if 
{@code ms} is 0
+     */
+    public static String formatTimeIntervalMs(long ms) {
+        if (ms == 0) {
+            return "0";
+        } else if (ms % DAY == 0) {
+            return ms / DAY + "d";
+        } else if (ms % HOUR == 0) {
+            return ms / HOUR + "h";
+        } else if (ms % MINUTE == 0) {
+            return ms / MINUTE + "m";
+        } else if (ms % SECOND == 0) {
+            return ms / SECOND + "s";
+        } else {
+            return ms + "ms";
+        }
+    }
 }
diff --git 
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/DatadirCleanupManager.java
 
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DatadirCleanupManager.java
index ac714ee07..acb6e6ad6 100644
--- 
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/DatadirCleanupManager.java
+++ 
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DatadirCleanupManager.java
@@ -21,6 +21,7 @@
 import java.io.File;
 import java.util.Timer;
 import java.util.TimerTask;
+import org.apache.zookeeper.common.Time;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -75,7 +76,7 @@ public DatadirCleanupManager(File snapDir, File dataLogDir, 
int snapRetainCount,
         this.snapRetainCount = snapRetainCount;
         this.purgeIntervalInMs = purgeIntervalInMs;
         LOG.info("autopurge.snapRetainCount set to {}", snapRetainCount);
-        LOG.info("autopurge.purgeInterval set to {}", purgeIntervalInMs);
+        LOG.info("autopurge.purgeInterval set to {}", 
Time.formatTimeIntervalMs(purgeIntervalInMs));
     }
 
     /**
diff --git 
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
 
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
index 0e9c82d4d..fe548519d 100644
--- 
a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
+++ 
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
@@ -903,6 +903,16 @@ public int getSnapRetainCount() {
         return snapRetainCount;
     }
 
+    /**
+     * Use {@link #getPurgeIntervalInMs()} instead.
+     *
+     * @return purge interval in hour unit or 0 if less than one hour.
+     */
+    @Deprecated
+    public int getPurgeInterval() {
+        return purgeIntervalInMs / Time.HOUR;
+    }
+
     public int getPurgeIntervalInMs() {
         return purgeIntervalInMs;
     }
diff --git 
a/zookeeper-server/src/test/java/org/apache/zookeeper/common/TimeTest.java 
b/zookeeper-server/src/test/java/org/apache/zookeeper/common/TimeTest.java
index cc60055a5..c760e3269 100644
--- a/zookeeper-server/src/test/java/org/apache/zookeeper/common/TimeTest.java
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/TimeTest.java
@@ -129,4 +129,27 @@ public void testParseTimeInterval() throws Exception {
         } catch (NumberFormatException e) {
         }
     }
+
+    @Test
+    public void testFormatTimeInterval() throws Exception {
+        assertEquals("0", Time.formatTimeIntervalMs(0));
+        assertEquals("0", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("0")));
+        assertEquals("0", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("0ms")));
+        assertEquals("0", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("0s")));
+        assertEquals("0", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("0m")));
+        assertEquals("0", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("0h")));
+        assertEquals("0", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("0d")));
+        assertEquals("500ms", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("500ms")));
+        assertEquals("1m", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("60s")));
+        assertEquals("61s", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("61s")));
+        assertEquals("59m", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("59m")));
+        assertEquals("1h", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("60m")));
+        assertEquals("2h", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("120m")));
+        assertEquals("61m", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("61m")));
+        assertEquals("23h", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("23h")));
+        assertEquals("1d", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("24h")));
+        assertEquals("2d", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("48h")));
+        assertEquals("25h", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("25h")));
+        assertEquals("2d", 
Time.formatTimeIntervalMs(Time.parseTimeInterval("2d")));
+    }
 }

Reply via email to