This is an automated email from the ASF dual-hosted git repository.
kirs pushed a commit to branch 2.0.0-release-prepare
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git
The following commit(s) were added to refs/heads/2.0.0-release-prepare by this
push:
new 5283e54 [cherry-pick][6774] fix snowflake bug (#6752)
5283e54 is described below
commit 5283e54dc8aab3ba2f116b5cbc37cc752689f1c9
Author: OS <[email protected]>
AuthorDate: Tue Nov 9 11:54:12 2021 +0800
[cherry-pick][6774] fix snowflake bug (#6752)
* Use nanoTime replace currentTimemill to avoid clock backwards (#6740)
* [Bug][SnowFlakeUtils] fix snowFlake bug (#6744)
* fix snowFlake bug
* fix hash machine
Co-authored-by: Wenjun Ruan <[email protected]>
Co-authored-by: JinYong Li <[email protected]>
---
.../dolphinscheduler/common/utils/SnowFlakeUtils.java | 15 +++++++++------
.../dolphinscheduler/common/utils/SnowFlakeUtilsTest.java | 3 ++-
2 files changed, 11 insertions(+), 7 deletions(-)
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 1fa14fd..6393e19 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
@@ -24,20 +24,23 @@ import java.util.Objects;
public class SnowFlakeUtils {
// start timestamp
private static final long START_TIMESTAMP = 1609430400000L; //2021-01-01
00:00:00
- // Number of digits
- private static final long SEQUENCE_BIT = 13;
+ // Each machine generates 32 in the same millisecond
+ private static final long SEQUENCE_BIT = 5;
private static final long MACHINE_BIT = 2;
private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BIT);
// The displacement to the left
- private static final long MACHINE_LEFT = SEQUENCE_BIT;
- private static final long TIMESTAMP_LEFT = SEQUENCE_BIT + MACHINE_BIT;
+ private static final long MACHINE_LEFT = SEQUENCE_BIT + MACHINE_BIT;
+ private static final long TIMESTAMP_LEFT = SEQUENCE_BIT + MACHINE_BIT +
MACHINE_LEFT;
private final int machineId;
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;
+ this.machineId =
Math.abs(Objects.hash(InetAddress.getLocalHost().getHostName())) % 4;
} catch (UnknownHostException e) {
throw new SnowFlakeException(e.getMessage());
}
@@ -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 {
diff --git
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/SnowFlakeUtilsTest.java
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/SnowFlakeUtilsTest.java
index 4f4c667..bdf7096 100644
---
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/SnowFlakeUtilsTest.java
+++
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/SnowFlakeUtilsTest.java
@@ -23,7 +23,8 @@ public class SnowFlakeUtilsTest {
@Test
public void testNextId() {
try {
- for (int i = 0; i < 5; i++) {
+ for (int i = 0; i < 100; i++) {
+ Thread.sleep(1);
System.out.println(SnowFlakeUtils.getInstance().nextId());
}
} catch (Exception e) {