百度雪花算法uid-generator解析

2024-10-03  本文已影响0人  小波同学

概述

UidGenerator是一款基于Snowflake算法的分布式高性能唯一ID生成器,由百度开源。 它使用Java实现,支持自定义workerId位数和初始化策略,适用于虚拟化环境下实例自动重启、漂移等场景。UidGenerator通过借用未来时间来解决sequence的并发限制,采用RingBuffer缓存生成的UID,实现无锁并行生产消费,避免了硬件级“伪共享”问题,单机QPS可达600万。

UidGenerator的主要特点包括:

一、雪花算法的原理

雪花算法是 64 位 的二进制,一共包含了四部分:

1bit-符号位

41bit-时间戳

10bit-机器位

12bit-序列号

二、UID-Generator 的实现

UID-Generator 有两种实现,一种是基于传统雪花算法实现(DefaultUidGenerator),另一种是百度基于雪环算法做的优化版本(CachedUidGenerator)。

2.1 DefaultUidGenerator

DefaultUidGenerator 依照雪花算法实现,它采用抛异常的方式来处理雪花算法的“时间回拨”问题。

protected synchronized long nextId() {
    long currentSecond = getCurrentSecond();

    // 时间回拨 -> 抛异常
    if (currentSecond < lastSecond) {
        long refusedSeconds = lastSecond - currentSecond;
        throw new UidGenerateException("Clock moved backwards. Refusing for %d seconds", refusedSeconds);
    }

    // 在同一秒内,sequence 自增
    if (currentSecond == lastSecond) {
        sequence = (sequence + 1) & bitsAllocator.getMaxSequence();
        // Exceed the max sequence, we wait the next second to generate uid
        if (sequence == 0) {
            currentSecond = getNextSecond(lastSecond);
        }
    // 不在同一秒:重置 sequence
    } else {
        sequence = 0L;
    }

    lastSecond = currentSecond;

    // 分配 Id
    return bitsAllocator.allocate(currentSecond - epochSeconds, workerId, sequence);
}

2.2 CachedUidGenerator

根据上面的实现,我们知道 sequence 决定了 UID-Generator 的并发能力,13 bits 的 sequence 可支持 8192/s 的并发,现实中很有可能不够用,进而诞生了 CachedUidGenerator。
CachedUidGenerator 使用 RingBuffer 缓存生成的id。RingBuffer是个环形数组,默认大小为 8192 个(可以通过boostPower参数设置大小)。

RingBuffer 的运行原理:

RingBuffer 填充触发机制:

为什么 CachedUidGenerator 性能强?

根据上面的研究我们得知:

三、源码分析

3.1 源码结构

com
 └── baidu
     └── fsg
         └── uid
             ├── BitsAllocator.java         - Bit分配器(C)
             ├── UidGenerator.java          - UID生成的接口(I)
             ├── buffer
             │   ├── BufferPaddingExecutor.java     - 填充RingBuffer的执行器(C)
             │   ├── BufferedUidProvider.java       - RingBuffer中UID的提供者(C)
             │   ├── RejectedPutBufferHandler.java  - 拒绝Put到RingBuffer的处理器(C)
             │   ├── RejectedTakeBufferHandler.java - 拒绝从RingBuffer中Take的处理器(C)
             │   └── RingBuffer.java            - 内含两个环形数组(C)
             ├── exception
             │   └── UidGenerateException.java      - 运行时异常
             ├── impl
             │   ├── CachedUidGenerator.java        - RingBuffer存储的UID生成器(C)
             │   └── DefaultUidGenerator.java       - 无RingBuffer的默认UID生成器(C)
             ├── utils
             │   ├── DateUtils.java
             │   ├── DockerUtils.java
             │   ├── EnumUtils.java
             │   ├── NamingThreadFactory.java
             │   ├── NetUtils.java
             │   ├── PaddedAtomicLong.java
             │   └── ValuedEnum.java
             └── worker
                 ├── DisposableWorkerIdAssigner.java    - 用完即弃的WorkerId分配器(C)
                 ├── WorkerIdAssigner.java      - WorkerId分配器(I)
                 ├── WorkerNodeType.java        - 工作节点类型(E)
                 ├── dao
                 │   └── WorkerNodeDAO.java     - MyBatis Mapper
                 └── entity
                     └── WorkerNodeEntity.java      - MyBatis Entity

3.2 DefaultUidGenerator

DefaultUidGenerator的产生id的方法与基本上就是常见的snowflake算法实现,仅有一些不同,如以秒为为单位而不是毫秒

DefaultUidGenerator的产生id的方法如下:

protected synchronized long nextId() {
        long currentSecond = getCurrentSecond();
        // Clock moved backwards, refuse to generate uid
        // 发生了时钟回拨
        if (currentSecond < lastSecond) {
            long refusedSeconds = lastSecond - currentSecond;
            throw new UidGenerateException("Clock moved backwards. Refusing for %d seconds", refusedSeconds);
        }
        // At the same second, increase sequence
        if (currentSecond == lastSecond) {
            sequence = (sequence + 1) & bitsAllocator.getMaxSequence();
            // Exceed the max sequence, we wait the next second to generate uid
            if (sequence == 0) {
                // 当前秒的sequence达到最大值,自旋等到下一秒
                currentSecond = getNextSecond(lastSecond);
            }
        // At the different second, sequence restart from zero
        } else {
            sequence = 0L;
        }
        // 上一次生成ID的秒数
        lastSecond = currentSecond;
        // Allocate bits for UID
        return bitsAllocator.allocate(currentSecond - epochSeconds, workerId, sequence);
    }

3.3 CachedUidGenerator

CachedUidGenerator支持缓存生成的id。

基本实现原理

关于CachedUidGenerator,文档上是这样介绍的。

在实现上, UidGenerator通过借用未来时间来解决sequence天然存在的并发限制; 采用RingBuffer来缓存已生成的UID, 并行化UID的生产和消费, 同时对CacheLine补齐,避免了由RingBuffer带来的硬件级「伪共享」问题. 最终单机QPS可达600万。

【采用RingBuffer来缓存已生成的UID, 并行化UID的生产和消费】
因为delta seconds部分是以秒为单位的,所以1个worker 1秒内最多生成的id书为8192个(2的13次方)。

从上可知,支持的最大qps为8192,所以通过缓存id来提高吞吐量。

为什么叫借助未来时间?

BitsAllocator - Bit分配器

整个UID由64bit组成,以下图为例,1bit是符号位,其余63位由deltaSeconds、workerId和sequence组成,注意sequence被放在最后,可方便直接进行求和或自增操作。

该类主要接收上述3个用于组成UID的元素,并计算出各个元素的最大值和对应的位偏移。其申请UID时的方法如下,由这3个元素进行或操作进行拼接。

public long allocate(long deltaSeconds, long workerId, long sequence) {
    return (deltaSeconds << timestampShift) | (workerId << workerIdShift) | sequence;
}

DisposableWorkerIdAssigner - Worker ID分配器

本类用于为每个工作机器分配一个唯一的ID,目前来说是用完即弃,在初始化Bean的时候会自动向MySQL中插入一条关于该服务的启动信息,待MySQL返回其自增ID之后,使用该ID作为工作机器ID并柔和到UID的生成当中。

@Transactional
public long assignWorkerId() {
    // build worker node entity
    WorkerNodeEntity workerNodeEntity = buildWorkerNode();

    // add worker node for new (ignore the same IP + PORT)
    workerNodeDAO.addWorkerNode(workerNodeEntity);
    LOGGER.info("Add worker node:" + workerNodeEntity);

    return workerNodeEntity.getId();
}

RingBuffer - 用于存储UID的双环形数组结构

先看RingBuffer的field outline,这样能大致了解到他的工作模式:

/** * Constants */
private static final int START_POINT = -1;
private static final long CAN_PUT_FLAG = 0L;
private static final long CAN_TAKE_FLAG = 1L;
// 默认扩容阈值
public static final int DEFAULT_PADDING_PERCENT = 50;

/** * The size of RingBuffer's slots, each slot hold a UID * <p> * buffer的大小为2^n */
private final int bufferSize;
/** * 因为bufferSize为2^n,indexMask为bufferSize-1,作为被余数可快速取模 */
private final long indexMask;
/** * 存储UID的数组 */
private final long[] slots;
/** * 存储flag的数组(是否可读或者可写) */
private final PaddedAtomicLong[] flags;

/** * Tail: last position sequence to produce */
private final AtomicLong tail = new PaddedAtomicLong(START_POINT);

/** * Cursor: current position sequence to consume */
private final AtomicLong cursor = new PaddedAtomicLong(START_POINT);

/** * Threshold for trigger padding buffer */
private final int paddingThreshold;

/** * Reject putbuffer handle policy * <p> * 拒绝方式为打印日志 */
private RejectedPutBufferHandler rejectedPutHandler = this::discardPutBuffer;
/** * Reject take buffer handle policy * <p> * 拒绝方式为抛出异常并打印日志 */
private RejectedTakeBufferHandler rejectedTakeHandler = this::exceptionRejectedTakeBuffer;

/** * Executor of padding buffer * <p> * 填充RingBuffer的executor */
private BufferPaddingExecutor bufferPaddingExecutor;

RingBuffer环形数组,数组每个元素成为一个slot。RingBuffer容量,默认为Snowflake算法中sequence最大值,且为2^N。可通过boostPower配置进行扩容,以提高RingBuffer 读写吞吐量。

Tail指针、Cursor指针用于环形数组上读写slot:

CachedUidGenerator采用了双RingBuffer,Uid-RingBuffer用于存储Uid、Flag-RingBuffer用于存储Uid状态(是否可填充、是否可消费)

由于数组元素在内存中是连续分配的,可最大程度利用CPU cache以提升性能。但同时会带来「伪共享」FalseSharing问题,为此在Tail、Cursor指针、Flag-RingBuffer中采用了CacheLine 补齐方式。


那为什么一个使用long而另一个使用PaddedAtomicLong呢?

RingBuffer.put(long uid)

put(long)方法是一个同步方法,换句话说就是串行写,保证了填充slot和移动tail是原子操作

public synchronized boolean put(long uid) {
        // 拿到当前生产者指针
        long currentTail = tail.get();
        // 拿到当前消费者指针
        long currentCursor = cursor.get();

        // tail catches the cursor, means that you can't put any cause of RingBuffer is full
        long distance = currentTail - (currentCursor == START_POINT ? 0 : currentCursor);
        if (distance == bufferSize - 1) {
            rejectedPutHandler.rejectPutBuffer(this, uid);
            return false;
        }

        // 1. pre-check whether the flag is CAN_PUT_FLAG
        // 通过当前生产者指针拿到状态集数组的状态
        int nextTailIndex = calSlotIndex(currentTail + 1);
        if (flags[nextTailIndex].get() != CAN_PUT_FLAG) {
            rejectedPutHandler.rejectPutBuffer(this, uid);
            return false;
        }

        // 2. put UID in the next slot
        // 3. update next slot' flag to CAN_TAKE_FLAG
        // 4. publish tail with sequence increase by one
        // 添加uid到slots数组
        slots[nextTailIndex] = uid;
        // 设置状态集数组状态为可拿状态
        flags[nextTailIndex].set(CAN_TAKE_FLAG);
        // 生产者指针+1
        tail.incrementAndGet();

        // The atomicity of operations above, guarantees by 'synchronized'. In another word,
        // the take operation can't consume the UID we just put, until the tail is published(tail.incrementAndGet())
        return true;
    }

RingBuffer.take()

UID的读取是一个lock free操作,使用CAS成功将tail往后移动之后即视为线程安全。

public long take() {
        // spin get next available cursor
        long currentCursor = cursor.get();
        long nextCursor = cursor.updateAndGet(old -> old == tail.get() ? old : old + 1);

        // check for safety consideration, it never occurs
        Assert.isTrue(nextCursor >= currentCursor, "Curosr can't move back");

        // trigger padding in an async-mode if reach the threshold
        long currentTail = tail.get();
        if (currentTail - nextCursor < paddingThreshold) {
            LOGGER.info("Reach the padding threshold:{}. tail:{}, cursor:{}, rest:{}", paddingThreshold, currentTail,
                    nextCursor, currentTail - nextCursor);
            bufferPaddingExecutor.asyncPadding(); ---(a)
        }

        // cursor catch the tail, means that there is no more available UID to take
        if (nextCursor == currentCursor) {
            rejectedTakeHandler.rejectTakeBuffer(this);
        }

        // 1. check next slot flag is CAN_TAKE_FLAG
        int nextCursorIndex = calSlotIndex(nextCursor);
        Assert.isTrue(flags[nextCursorIndex].get() == CAN_TAKE_FLAG, "Curosr not in can take status");

        // 2. get UID from next slot
        // 3. set next slot flag as CAN_PUT_FLAG.
        long uid = slots[nextCursorIndex];
        flags[nextCursorIndex].set(CAN_PUT_FLAG);

        // Note that: Step 2,3 can not swap. If we set flag before get value of slot, the producer may overwrite the
        // slot with a new UID, and this may cause the consumer take the UID twice after walk a round the ring
        return uid;
    }

在(a)处可以看到当达到默认填充阈值50%时,即slots被消费大于50%的时候进行异步填充,这个填充由BufferPaddingExecutor所执行的

BufferPaddingExecutor - RingBuffer元素填充器

该用于填充RingBuffer的执行者最主要的执行方法如下

public void paddingBuffer() {
        LOGGER.info("Ready to padding buffer lastSecond:{}. {}", lastSecond.get(), ringBuffer);

        // is still running
        if (!running.compareAndSet(false, true)) {
            LOGGER.info("Padding buffer is still running. {}", ringBuffer);
            return;
        }

        // fill the rest slots until to catch the cursor
        boolean isFullRingBuffer = false;
        while (!isFullRingBuffer) {
            List<Long> uidList = uidProvider.provide(lastSecond.incrementAndGet());
            for (Long uid : uidList) {
                isFullRingBuffer = !ringBuffer.put(uid);
                if (isFullRingBuffer) {
                    break;
                }
            }
        }

        // not running now 填满收工
        running.compareAndSet(true, false);
        LOGGER.info("End to padding buffer lastSecond:{}. {}", lastSecond.get(), ringBuffer);
    }

当线程池分发多条线程来执行填充任务的时候,成功抢夺运行状态的线程会真正执行对RingBuffer填充,直至全部填满,其他抢夺失败的线程将会直接返回。

/** * Start executors such as schedule */
    public void start() {
        if (bufferPadSchedule != null) {
            bufferPadSchedule.scheduleWithFixedDelay(this::paddingBuffer, scheduleInterval, scheduleInterval, TimeUnit.SECONDS);
        }
    }
 /** * Padding buffer in the thread pool */
    public void asyncPadding() {
        bufferPadExecutors.submit(this::paddingBuffer);
    }

其他函数式接口

CachedUidGenerator - 使用RingBuffer的UID生成器

该类在应用中作为Spring Bean注入到各个组件中,主要作用是初始化RingBuffer和BufferPaddingExecutor。获取ID是通过委托RingBuffer的take()方法达成的,而最重要的方法为BufferedUidProvider的提供者,即lambda表达式中的nextIdsForOneSecond(long)方法,用于生成指定秒currentSecond内的全部UID,提供给BufferPaddingExecutor进行填充

/** 生产一秒内的所有ID */
    protected List<Long> nextIdsForOneSecond(long currentSecond) {
        // Initialize result list size of (max sequence + 1)
        int listSize = (int) bitsAllocator.getMaxSequence() + 1;
        List<Long> uidList = new ArrayList<>(listSize);

        // Allocate the first sequence of the second, the others can be calculated with the offset
        long firstSeqUid = bitsAllocator.allocate(currentSecond - epochSeconds, workerId, 0L);
        for (int offset = 0; offset < listSize; offset++) {
            uidList.add(firstSeqUid + offset);
        }

        return uidList;
    }

总结

百度 UID-Generator 性能强劲,尤其是 CachedUidGenerator 实现了 Id 的缓冲区。
此外,对于分布式集群部署,UID-Generator 依赖 Mysql 的自增 Id 作为 WorkerId。
最后针对雪花算法的时间回拨问题,DefaultUidGenerator 采取了最简单的抛异常来解决,而 CachedUidGenerator 则是利用其“借用未来时间”特性巧妙解决了时间回拨问题。
总体来说,如果想用雪花算法作为分布式唯一 Id 生成器,那么百度 UID-Generator 是一个不错的选择。

参考:
https://segmentfault.com/a/1190000042792224

https://blog.csdn.net/m0_37626564/article/details/121263753

上一篇 下一篇

猜你喜欢

热点阅读