From bb71c063e379356aee4f4cbac610488d549847f6 Mon Sep 17 00:00:00 2001 From: Not Found <577528249@qq.com> Date: Mon, 4 May 2020 13:24:52 +0800 Subject: [PATCH] fix timestamp over --- .../com/sankuai/inf/leaf/snowflake/SnowflakeIDGenImpl.java | 7 +++++++ .../snowflake/exception/OverMaxTimeStampException.java | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/exception/OverMaxTimeStampException.java diff --git a/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeIDGenImpl.java b/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeIDGenImpl.java index b4f813b2..854bcba6 100644 --- a/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeIDGenImpl.java +++ b/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeIDGenImpl.java @@ -5,6 +5,7 @@ import com.sankuai.inf.leaf.common.Result; import com.sankuai.inf.leaf.common.Status; import com.sankuai.inf.leaf.common.Utils; +import com.sankuai.inf.leaf.snowflake.exception.OverMaxTimeStampException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,11 +21,13 @@ public boolean init() { private static final Logger LOGGER = LoggerFactory.getLogger(SnowflakeIDGenImpl.class); private final long twepoch; + private final long timeStampBits = 41L; private final long workerIdBits = 10L; private final long maxWorkerId = ~(-1L << workerIdBits);//最大能够分配的workerid =1023 private final long sequenceBits = 12L; private final long workerIdShift = sequenceBits; private final long timestampLeftShift = sequenceBits + workerIdBits; + private final long maxTimeStamp; private final long sequenceMask = ~(-1L << sequenceBits); private long workerId; private long sequence = 0L; @@ -43,6 +46,7 @@ public SnowflakeIDGenImpl(String zkAddress, int port) { */ public SnowflakeIDGenImpl(String zkAddress, int port, long twepoch) { this.twepoch = twepoch; + this.maxTimeStamp = ~(-1L << timeStampBits) + twepoch; Preconditions.checkArgument(timeGen() > twepoch, "Snowflake not support twepoch gt currentTime"); final String ip = Utils.getIp(); SnowflakeZookeeperHolder holder = new SnowflakeZookeeperHolder(ip, String.valueOf(port), zkAddress); @@ -60,6 +64,9 @@ public SnowflakeIDGenImpl(String zkAddress, int port, long twepoch) { @Override public synchronized Result get(String key) { long timestamp = timeGen(); + if (timestamp>maxTimeStamp) { + throw new OverMaxTimeStampException("current timestamp is over maxTimeStamp, the generate id will be negative"); + } if (timestamp < lastTimestamp) { long offset = lastTimestamp - timestamp; if (offset <= 5) { diff --git a/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/exception/OverMaxTimeStampException.java b/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/exception/OverMaxTimeStampException.java new file mode 100644 index 00000000..11bc2077 --- /dev/null +++ b/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/exception/OverMaxTimeStampException.java @@ -0,0 +1,7 @@ +package com.sankuai.inf.leaf.snowflake.exception; + +public class OverMaxTimeStampException extends RuntimeException { + public OverMaxTimeStampException(String msg){ + super(msg); + } +}