简单有效地解决SnowFlake的时钟回拨问题
程序员文章站
2022-05-03 16:00:28
...
SnowFlake是Twitter开源的可以在分布式系统中生成唯一的ID的算法 , 具体介绍本文不再赘述, 可以参考这篇文章: https://segmentfault.com/a/1190000011282426?utm_source=tag-newest
SnowFlake算法生成id的结果是一个64bit大小的整数, 其中的41位时间戳部分依赖服务器的时间, 当服务器发生时钟回拨时, 在开源的实现中不可避免的会出现报错. 关于解决时钟回拨的问题, 网上已有各种方案, 比如适当等待直到时间被追回, 在内存中保存一段时间内使用过的最大*** …
本文描述一种简单粗暴又可行的方案, 并给出单测加以验证
首先, SnowFlake的末尾12位是***, 用来记录同一毫秒内产生的不同id, 同一毫秒总共可以产生4096个id, 每一毫秒的***都是从0这个基础***开始递增
假设我们的业务系统在单机上的QPS为3w/s, 那么其实平均每毫秒只需要产生30个id即可, 远没有达到设计的4096, 也就是说通常情况下***的使用都是处在一个低水位, 当发生时钟回拨的时候, 这些尚未被使用的序号就可以派上用场了.
因此, 可以对给定的基础***稍加修改, 后面每发生一次时钟回拨就将基础***加上指定的步长, 例如开始时是从0递增, 发生一次时钟回拨后从1024开始递增, 再发生一次时钟回拨则从2048递增, 这样还能够满足3次的时钟回拨到同一时间点(发生这种操作就有点扯了).
处理时钟回拨的逻辑:
/** 步长, 1024 */
private static long stepSize = 2 << 9;
/** 基础***, 每发生一次时钟回拨, basicSequence += stepSize */
private long basicSequence = 0L;
private long handleMovedBackwards(long currStmp) {
basicSequence += stepSize;
if (basicSequence == MAX_SEQUENCE + 1) {
basicSequence = 0;
currStmp = getNextMill();
}
sequence = basicSequence;
lastStmp = currStmp;
return (currStmp - START_STMP) << TIMESTMP_LEFT
| workId << WORK_LEFT
| sequence;
}
改写原先的末尾sequence生成方法:
if (currStmp == lastStmp) {
// 相同毫秒内,***自增
// ...
} else {
// 不同毫秒内,***置为0
sequence = 0L;
}
修改为
if (currStmp == lastStmp) {
// ...
} else {
// 不同毫秒内,***置为 basicSequence
sequence = basicSequence;
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(value = {System.class, SnowFlakeWorker.class})
public class SnowFlakeWorkerTest {
@Test
public void testClockBackwards() throws InterruptedException {
int size = 100;
long beginTime = System.currentTimeMillis();
Set<Long> resultSet = new HashSet<>(128);
for (int i = 0; i < size; i++) {
resultSet.add(snowFlakeWorker.nextId());
}
TimeUnit.SECONDS.sleep(1);
PowerMockito.mockStatic(System.class);
// 第一次回拨
PowerMockito.when(System.currentTimeMillis()).thenReturn(beginTime);
Assert.assertEquals(beginTime, System.currentTimeMillis());
for (int i = 0; i < 5; i++) {
// i > 0, currStmp == lastStmp
long temp = (beginTime - START_STMP) << TIMESTMP_LEFT
| workId << WORK_LEFT
| (STEP_SIZE + i);
long tmpR;
resultSet.add(tmpR = snowFlakeWorker.nextId());
Assert.assertEquals(temp, tmpR);
}
Assert.assertEquals(size + 5, resultSet.size());
// 下一毫秒
PowerMockito.when(System.currentTimeMillis()).thenReturn(beginTime += 1);
long temp = (beginTime - START_STMP) << TIMESTMP_LEFT | workId << WORK_LEFT | STEP_SIZE;
Assert.assertEquals(temp, snowFlakeWorker.nextId());
// 同一毫秒内
temp = (beginTime - START_STMP) << TIMESTMP_LEFT | workId << WORK_LEFT | (STEP_SIZE + 1);
Assert.assertEquals(temp, snowFlakeWorker.nextId());
temp = (beginTime - START_STMP) << TIMESTMP_LEFT | workId << WORK_LEFT | (STEP_SIZE + 2);
Assert.assertEquals(temp, snowFlakeWorker.nextId());
// 第二次回拨
PowerMockito.when(System.currentTimeMillis()).thenReturn(beginTime -= 5);
temp = (beginTime - START_STMP) << TIMESTMP_LEFT | workId << WORK_LEFT | (STEP_SIZE * 2);
Assert.assertEquals(temp, snowFlakeWorker.nextId());
// 不同毫秒内
PowerMockito.when(System.currentTimeMillis()).thenReturn(beginTime += 1);
temp = (beginTime - START_STMP) << TIMESTMP_LEFT | workId << WORK_LEFT | (STEP_SIZE * 2);
Assert.assertEquals(temp, snowFlakeWorker.nextId());
// 第三次回拨
PowerMockito.when(System.currentTimeMillis()).thenReturn(beginTime -= 10);
temp = (beginTime - START_STMP) << TIMESTMP_LEFT | workId << WORK_LEFT | (STEP_SIZE * 3);
Assert.assertEquals(temp, snowFlakeWorker.nextId());
// 同一毫秒内
temp = (beginTime - START_STMP) << TIMESTMP_LEFT | workId << WORK_LEFT | (STEP_SIZE * 3 + 1);
Assert.assertEquals(temp, snowFlakeWorker.nextId());
temp = (beginTime - START_STMP) << TIMESTMP_LEFT | workId << WORK_LEFT | (STEP_SIZE * 3 + 2);
Assert.assertEquals(temp, snowFlakeWorker.nextId());
}
}