概述
snowflake并不是Twitter独有的,mongodb也采用类似的方法生产自增ID。
- 分布式ID生成器
- 全局唯一ID生成
- 分布式纯数字ID
snowflake算法的好处有两点:
一是纯数字。
二是整体上来说是按时间顺序的。
业务上可以作为MySQL Innodb引擎的物理主键,可用在高并发情况下订单号生成等。
结构
SnowFlake算法生成id的结果是一个64bit大小的整数,它的结构如下图:
1位
,不用。二进制中最高位为1的都是负数,但是我们生成的id一般都使用整数,所以这个最高位固定是0-
41位
,用来记录时间戳(毫秒)。- 41位可以表示2的41次方−1个数字,
- 如果只用来表示正整数(计算机中正数包含0),可以表示的数值范围是:0 至 2的41次方−1,减1是因为可表示的数值范围是从0开始算的,而不是1。
- 也就是说41位可以表示2的41次方−1个毫秒的值,转化成单位年则是(2的41次方−1)/(1000∗60∗60∗24∗365)=69年
-
10位
,用来记录工作机器id。- 可以部署在2的10次方=1024个节点,包括
5位datacenterId
和5位workerId
5位(bit)
可以表示的最大正整数是2的5次方−1=31,即可以用0、1、2、3、....31这32个数字,来表示不同的datecenterId或workerId
- 可以部署在2的10次方=1024个节点,包括
-
12位
,序列号,用来记录同毫秒内产生的不同id。12位(bit)
可以表示的最大正整数是2的12次方−1=4096,即可以用0、1、2、3、....4095这4096个数字,来表示同一机器同一时间截(毫秒)内产生的4096个ID序号
由于在Java中64bit的整数是long类型,所以在Java中SnowFlake算法生成的id就是long来存储的。
SnowFlake可以保证:
- 所有生成的id按时间趋势递增
- 整个分布式系统内不会产生重复id(因为有datacenterId和workerId来做区分)
代码示例
大家都知道twitter是Scala技术推动者,它开源很多东西都是Scala编写的比如著名的RPC系统Finagle,著名分布式跟踪系统zipkin。这里主要是基于Scala翻译过来的Java版本如下:
public class IdWorker {
private long workerId;
private long datacenterId;
private long sequence;
public IdWorker(long workerId, long datacenterId, long sequence) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
System.out.printf("worker starting. timestamp left shift %d, datacenter id bits %d, worker id bits %d, sequence bits %d, workerid %d",
timestampLeftShift, datacenterIdBits, workerIdBits, sequenceBits, workerId);
this.workerId = workerId;
this.datacenterId = datacenterId;
this.sequence = sequence;
}
private long twepoch = 1523930374567L;
private long workerIdBits = 5L;
private long datacenterIdBits = 5L;
private long maxWorkerId = -1L ^ (-1L << workerIdBits);
private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
private long sequenceBits = 12L;
private long workerIdShift = sequenceBits;
private long datacenterIdShift = sequenceBits + workerIdBits;
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private long sequenceMask = -1L ^ (-1L << sequenceBits);
private long lastTimestamp = -1L;
public long getWorkerId() {
return workerId;
}
public long getDatacenterId() {
return datacenterId;
}
public long getTimestamp() {
return System.currentTimeMillis();
}
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
System.err.printf("clock is moving backwards. Rejecting requests until %d.", lastTimestamp);
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0;
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << timestampLeftShift) |
(datacenterId << datacenterIdShift) |
(workerId << workerIdShift) |
sequence;
}
private long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
private long timeGen() {
return System.currentTimeMillis();
}
public static void main(String[] args) {
IdWorker worker = new IdWorker(1, 1, 1);
long start = System.currentTimeMillis();
int loopNum = 30000000;
for (int i = 0; i < loopNum; i++) {
worker.nextId();
}
long end = System.currentTimeMillis();
long sec = (end - start) / 1000;
System.out.println(sec);
System.out.println(loopNum / sec);
}
}
性能
在我4C8G物理机能跑到接近430W的TPS