From 0f94577d2613634b268b56b6dbd445bd5528542c Mon Sep 17 00:00:00 2001 From: Wenjun Ruan Date: Mon, 8 Nov 2021 22:26:17 +0800 Subject: [PATCH] Use nanoTime replace currentTimemill to avoid clock backwards (#6740) --- .../apache/dolphinscheduler/common/utils/SnowFlakeUtils.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/SnowFlakeUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/SnowFlakeUtils.java index 1fa14fd6cf..9bb360e02a 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/SnowFlakeUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/SnowFlakeUtils.java @@ -35,6 +35,9 @@ public class SnowFlakeUtils { private long sequence = 0L; private long lastTimestamp = -1L; + private static final long SYSTEM_TIMESTAMP = System.currentTimeMillis(); + private static final long SYSTEM_NANOTIME = System.nanoTime(); + private SnowFlakeUtils() throws SnowFlakeException { try { this.machineId = Math.abs(Objects.hash(InetAddress.getLocalHost().getHostName())) % 32; @@ -80,7 +83,7 @@ public class SnowFlakeUtils { } private long nowTimestamp() { - return System.currentTimeMillis(); + return SYSTEM_TIMESTAMP + (System.nanoTime() - SYSTEM_NANOTIME) / 1000000; } public static class SnowFlakeException extends Exception {