Skip to content

Commit

Permalink
v2.8.0 解决低频率下使用带来的偶数问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ALI1416 committed Aug 31, 2023
1 parent ef3a644 commit 172045d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 更新日志

## v2.8.0 `2023.8.31`

- 解决低频率下使用带来的偶数问题

## v2.7.0 `2023.7.27`

- 代码优化
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@

## 简介

本项目根据[Twitter的雪花ID生成器](https://github.com/twitter-archive/snowflake)重构,并加上了手动设置参数、时钟回拨处理等,以及支持[SpringBoot自动配置](https://github.com/ALI1416/id-spring-boot-autoconfigure)
本项目根据[Twitter的雪花ID生成器](https://github.com/twitter-archive/snowflake)重构,并加上了手动设置参数、时钟回拨处理,偶数问题解决等,以及支持[SpringBoot自动配置](https://github.com/ALI1416/id-spring-boot-autoconfigure)

## 依赖导入

```xml
<dependency>
<groupId>cn.404z</groupId>
<artifactId>id</artifactId>
<version>2.7.0</version>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.8</version>
<version>1.4.11</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>id</artifactId>
<version>2.7.0</version>
<version>2.8.0</version>

<parent>
<groupId>cn.404z</groupId>
Expand All @@ -30,7 +30,7 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.8</version>
<version>1.4.11</version>
<scope>provided</scope>
</dependency>
<!-- 仅供测试使用 -->
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/cn/z/id/Id.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.util.concurrent.ThreadLocalRandom;

/**
* <h1>高性能雪花ID生成器</h1>
Expand Down Expand Up @@ -191,15 +192,15 @@ public static synchronized long next() {
currentTimestamp = Clock.now();
}
/* 阻塞结束后 */
// 序列号归零
sequence = 0;
// "序列号"归零
sequence = getRandomInitSequence();
// 更新"上一个时间戳"为"当前时间戳"
lastTimestamp = currentTimestamp;
}
} else if (lastTimestamp < currentTimestamp) {
/* 当前时间戳增加了 */
// "序列号"归零
sequence = 0;
sequence = getRandomInitSequence();
// 更新"上一个时间戳"为"当前时间戳"
lastTimestamp = currentTimestamp;
} else {
Expand All @@ -208,7 +209,7 @@ public static synchronized long next() {
// 修改"开始时间戳"为"开始时间戳"-(上一个时间戳-当前时间戳+1)
startTimestamp -= (lastTimestamp - currentTimestamp + 1);
// "序列号"归零
sequence = 0;
sequence = getRandomInitSequence();
// 更新"上一个时间戳"为"当前时间戳"
lastTimestamp = currentTimestamp;
}
Expand All @@ -232,4 +233,16 @@ public static long reset() {
return difference;
}

/**
* 获取随机初始序列号<br>
* 低频率使用下,序列号一直都是0,会导致id都为偶数<br>
* 当id作为分库分表的分片键时会出现严重的数据倾斜问题
*
* @return 随机初始序列号
* @see 2.8.0
*/
private static long getRandomInitSequence() {
return SEQUENCE_MAX == 0 ? 0 : ThreadLocalRandom.current().nextLong(2);
}

}
2 changes: 1 addition & 1 deletion src/test/java/cn/z/id/IdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.UUID;

/**
* <h1>高性能雪花Id生成器测试</h1>
* <h1>高性能雪花ID生成器测试</h1>
*
* <p>
* createDate 2021/02/25 08:52:28
Expand Down

0 comments on commit 172045d

Please sign in to comment.