Twitter的分布式自增ID算法Snowflake
- - 企业架构 - ITeye博客Twitter-Snowflake算法产生的背景相当简单,为了满足Twitter每秒上万条消息的请求,每条消息都必须分配一条唯一的id,这些id还需要一些大致的顺序(方便客户端排序),并且在分布式系统中不同机器产生的id必须不同. Snowflake算法核心. 把 时间戳, 工作机器id, 序列号组合在一起.
SnowFlake不依赖第三方介质,不像基于ZK,Redis等,每次用完一个区间还得通过网络去获取下一个区间,效率较低,基于SnowFlake的分布式ID生成是目前我见过的最快的
SnowFlake生成的是一个64位的数字,其中42位时间戳,接下来10位是自定义的数,其作用就是区分集群中的所有机器,最后12位是毫秒内序列,集群内每个机器能够在1毫秒内生成2^12 - 1个ID
/** * 基于SnowFlake的序列号生成实现, 64位ID (42(毫秒)+5(机器ID)+5(业务编码)+12(重复累加)) */ static class Generator { private final static long TWEPOCH = 1288834974657L; // 机器标识位数 private final static long WORKER_ID_BITS = 5L; // 数据中心标识位数 private final static long DATA_CENTER_ID_BITS = 5L; // 机器ID最大值 31 private final static long MAX_WORKER_ID = -1L ^ (-1L << WORKER_ID_BITS); // 数据中心ID最大值 31 private final static long MAX_DATA_CENTER_ID = -1L ^ (-1L << DATA_CENTER_ID_BITS); // 毫秒内自增位 private final static long SEQUENCE_BITS = 12L; // 机器ID偏左移12位 private final static long WORKER_ID_SHIFT = SEQUENCE_BITS; private final static long DATA_CENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS; // 时间毫秒左移22位 private final static long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS; private final static long SEQUENCE_MASK = -1L ^ (-1L << SEQUENCE_BITS); private long lastTimestamp = -1L; private long sequence = 0L; private final long workerId; private final long dataCenterId; //private final AtomicBoolean lock = new AtomicBoolean(false); Generator(long workerId, long dataCenterId) { if (workerId > MAX_WORKER_ID || workerId < 0) { throw new IllegalArgumentException(String.format("%s must range from %d to %d", K_WORK_ID, 0, MAX_WORKER_ID)); } if (dataCenterId > MAX_DATA_CENTER_ID || dataCenterId < 0) { throw new IllegalArgumentException(String.format("%s must range from %d to %d", K_DC_ID, 0, MAX_DATA_CENTER_ID)); } this.workerId = workerId; this.dataCenterId = dataCenterId; } synchronized long nextValue() throws SequenceException { long timestamp = time(); if (timestamp < lastTimestamp) { throw new SequenceException("Clock moved backwards, refuse to generate id for " + (lastTimestamp - timestamp) + " milliseconds"); } if (lastTimestamp == timestamp) { // 当前毫秒内,则+1 sequence = (sequence + 1) & SEQUENCE_MASK; if (sequence == 0) { // 当前毫秒内计数满了,则等待下一秒 timestamp = tilNextMillis(lastTimestamp); } } else { sequence = 0; } lastTimestamp = timestamp; // ID偏移组合生成最终的ID,并返回ID long nextId = ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | (dataCenterId << DATA_CENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT) | sequence; return nextId; } private long tilNextMillis(final long lastTimestamp) { long timestamp = this.time(); while (timestamp <= lastTimestamp) { timestamp = this.time(); } return timestamp; } private long time() { return System.currentTimeMillis(); } }