雪花算法Id生成【变种】

雪花算法,网上随随便便都能搜到很多,然后无一例外,都说明了算法的一些场景限制,可就是没有一篇文章直接给出一个已经解决或部分解决了这些限制,可以直接使用的算法……

因为我们面向的用户不是互联网企业,所以服务器不需要支持到25,数据中心不需要支持到25,毫秒内最大的序列数量不需要支持到212,反倒是时间戳,默认241只支持69年不太够,如果按1970年开始,那么2039年Id就要到最大值了,算算那时候我还没退休呢(我可是希望能够在当前公司做到退休的,毕竟老东西在外面已经没市场了 😛)!另外目前接触到的客户,基本服务器都是在域控内,所以NTP时间回拨问题肯定避免不了……

算法实现原始参考:snowflake-net
与原始算法的区别:

  1. 构造函数去除:sequence
  2. 构造函数增加:基准时间戳,默认为UTC时间2020/01/01;NTP回拨时间支持,默认2000ms;默认服务器3个Bit;默认数据中心2个Bit;默认序列12Bit,也就是说默认构造函数中,从服务器位和数据中心位扣除了5个Bit填补到时间戳,这样时间戳最大支持241+5(69*32=2208年,差不多22个世纪了 😛)
    public class IdWorker
    {
        /// <summary>
        /// UTC-Time 2020/01/01 0:00:00 +00:00
        /// </summary>
        const long DefaultBenchmarkTimestamp = 1577836800000L;
        const byte IdMaxBits = 64;
        const int TimestampMinBits = 41;
        const int WorkerIdAndDatacenterIdMinBits = 2;
        const int WorkerIdAndDatacenterIdMaxBits = 6;
        const int SequenceMinBits = 10;
        const int SequenceMaxBits = 14;
        const int ExtraClockCallbackMillisToCleanUp = 1000;
        const int AllowedNTPClockCallbackMaxMillis = 60000;
        public long BenchmarkTimestamp { get; private set; }
        public long WorkId { get; private set; }
        public long DatacenterId { get; private set; }
        public int TimestampBits { get; private set; }
        public int WorkerIdBits { get; private set; }
        public int DatacenterIdBits { get; private set; }
        public int SequenceBits { get; private set; }
        public int AllowedNTPClockCallbackMillis { get; private set; } //https://blog.csdn.net/a2279338659/article/details/143637143
        private readonly int _sequenceIdMaxValue;
        private readonly int _workerIdShift;
        private readonly int _datacenterIdShift;
        private readonly int _timestampShift;
        private readonly object _lock = new();
        private readonly int _maxCountMustBeCleanUp;
        protected long _sequence;
        protected long _firstTimestamp = -1L;
        protected long _lastTimestamp = -1L;
        /// <summary>
        /// key: timestamp  value: sequence
        /// </summary>
        protected readonly Dictionary<long, long> _storageTimestamps = new Dictionary<long, long>();

        public IdWorker(int workerId, int datacenterId, int allowedNTPClockCallbackMillis = 2000,
            long benchmarkTimestamp = DefaultBenchmarkTimestamp,
            int workerIdBits = 3, int datacenterIdBits = WorkerIdAndDatacenterIdMinBits, int sequenceBits = 12)
        {
            ValidNumberRange(allowedNTPClockCallbackMillis, AllowedNTPClockCallbackMaxMillis, nameof(allowedNTPClockCallbackMillis));
            var nowTimeStamp = this.TimeGen();
            if (benchmarkTimestamp >= nowTimeStamp)
            {
                throw new ArgumentException($"{nameof(benchmarkTimestamp)} is greater than the current timestamp");
            }
            ValidBitsRange(workerIdBits, WorkerIdAndDatacenterIdMinBits, WorkerIdAndDatacenterIdMaxBits, nameof(workerIdBits));
            ValidBitsRange(datacenterIdBits, WorkerIdAndDatacenterIdMinBits, WorkerIdAndDatacenterIdMaxBits, nameof(datacenterId));
            ValidBitsRange(sequenceBits, SequenceMinBits, SequenceMaxBits, nameof(sequenceBits));
            TimestampBits = IdMaxBits - workerIdBits - datacenterIdBits - sequenceBits - 1;
            if (TimestampBits < TimestampMinBits)
            {
                throw new ArgumentException($"The number of bits used for Timestamp must be greater than or equal to {TimestampMinBits}, and currently it is only {TimestampBits}");
            }

            var workIdMaxValue = GetNumberWithBits(workerIdBits);
            ValidNumberRange(workerId, workIdMaxValue, nameof(workerId));
            var datacenterIdMaxValue = GetNumberWithBits(datacenterIdBits);
            ValidNumberRange(datacenterId, datacenterIdMaxValue, nameof(datacenterId));

            this._sequenceIdMaxValue = GetNumberWithBits(sequenceBits);
            this.AllowedNTPClockCallbackMillis = allowedNTPClockCallbackMillis;
            this.WorkerIdBits = workerIdBits;
            this.DatacenterIdBits = datacenterIdBits;
            this.SequenceBits = sequenceBits;
            this.BenchmarkTimestamp = benchmarkTimestamp;
            this.WorkId = workerId;
            this.DatacenterId = datacenterId;

            this._workerIdShift = this.SequenceBits;
            this._datacenterIdShift = this.SequenceBits + this.WorkerIdBits;
            this._timestampShift = this.SequenceBits + this.WorkerIdBits + this.DatacenterIdBits;

            this._maxCountMustBeCleanUp = this.AllowedNTPClockCallbackMillis + ExtraClockCallbackMillisToCleanUp;
        }

        private static int GetNumberWithBits(int bits)
        {
            return -1 ^ (-1 << bits);
        }

        private static void ValidNumberRange(int inputValue, int maxValue, string numberName)
        {
            if (inputValue < 0 || inputValue > maxValue)
            {
                throw new ArgumentException($"The allowed range for {numberName} is {0}-{maxValue}, and {inputValue} is out of the range");
            }
        }

        private static void ValidBitsRange(int inputBit, int minBits, int maxBits, string bitName)
        {
            if (inputBit < minBits || inputBit > maxBits)
            {
                throw new ArgumentException($"The allowed range for {bitName} is {minBits}-{maxBits}, and {inputBit} is out of the range");
            }
        }

        public virtual DateTimeOffset GetDateTimeOffset(long snowflakeId)
        {
            if (snowflakeId < 0)
            {
                throw new ArgumentException($"{nameof(snowflakeId)} must be greater than or equals to 0");
            }
            var timestamp = (snowflakeId >> this._timestampShift) + this.BenchmarkTimestamp;
            if (timestamp <= 0)
            {
                throw new Exception("Conversion failed");
            }
            return DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
        }

        public virtual long NextId()
        {
            lock (_lock)
            {
                var timestamp = 0L;
                var prevTimestamp = -1L;
                var compareTimestamp = this._lastTimestamp - this.AllowedNTPClockCallbackMillis;
                do
                {
                    timestamp = this.TimeGen();
                    if (prevTimestamp == timestamp)
                    {
                        continue;
                    }
                    if (timestamp < compareTimestamp)
                    {
                        throw new Exception("Serious clock callback, requiring manual handling");
                    }
                    if (!this._storageTimestamps.ContainsKey(timestamp))
                    {
                        this._sequence = 0;
                        this._storageTimestamps[timestamp] = this._sequence;
                        break;
                    }
                    else
                    {
                        this._sequence = this._storageTimestamps[timestamp];
                        this._sequence = (this._sequence + 1) & this._sequenceIdMaxValue;
                        if (this._sequence > 0)
                        {
                            this._storageTimestamps[timestamp] = this._sequence;
                            break;
                        }
                        prevTimestamp = timestamp;
                    }
                }
                while (true);
                UpdateAndCleanUp();
                return ((timestamp - this.BenchmarkTimestamp) << this._timestampShift)
                    | (this.DatacenterId << this._datacenterIdShift)
                    | (this.WorkId << this._workerIdShift)
                    | _sequence;

                void UpdateAndCleanUp()
                {
                    if (this.AllowedNTPClockCallbackMillis > 0)
                    {
                        if (this._lastTimestamp < timestamp)
                        {
                            this._lastTimestamp = timestamp;
                        }
                        if (this._firstTimestamp < 0)
                        {
                            this._firstTimestamp = timestamp;
                        }
                        if (this._storageTimestamps.Count >= this._maxCountMustBeCleanUp)
                        {
                            var cleanTimestamp = timestamp - this.AllowedNTPClockCallbackMillis;
                            while (this._firstTimestamp < cleanTimestamp)
                            {
                                this._storageTimestamps.Remove(this._firstTimestamp);
                                this._firstTimestamp++;
                            }
                        }
                    }
                    else
                    {
                        this._lastTimestamp = timestamp;
                        if (this._storageTimestamps.Count >= this._maxCountMustBeCleanUp)
                        {
                            var allowedDicCount = this.AllowedNTPClockCallbackMillis + 1;
                            var i = -1;
                            do
                            {
                                this._storageTimestamps.Remove(timestamp + i);
                                i--;
                            }
                            while (this._storageTimestamps.Count > allowedDicCount);
                        }
                    }
                }
            }
        }

        protected virtual long TimeGen()
        {
            return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
        }
    }

使用起来的代码还是与参考算法实现没区别,我可是保留了所有的命名 😛

var idWorker = new IdWorker(0, 0);
var id = idWorker.NextId();

除默认的NextId方法外,我还增加了方法GetDateTimeOffset,根据生成的Id来解析出该Id生成时的时间戳,方便反向测试时间戳是否正确,测试代码如下

            Console.WriteLine($"Id: {long.MaxValue} Long Max");
            var idWorker = new IdWorker(0, 0, sequenceBits: 10, allowedNTPClockCallbackMillis: 0);
            Console.WriteLine("Test in single thread, press any key to start...");
            Console.ReadKey();
            var idList = GenerateId();
            PrintIdList(idList);
            Console.WriteLine("Test in multi-thread, press any key to start...");
            Console.ReadKey();
            var threadCount = 5;
            var listArr = new List<long>[threadCount];
            Parallel.For(0, threadCount, i =>
            {
                listArr[i] = GenerateId();
            });

            for (var i = 0; i < listArr.Length; i++)
            {
                Console.WriteLine($"List index: {i}");
                PrintIdList(listArr[i]);
            }

            Console.WriteLine($"All Count: {listArr.SelectMany(_ => _).Distinct().Count()}");

            List<long> GenerateId(int count=1000)
            {
                var tmpList = new List<long>();
                for (var j = 0; j < count; j++)
                {
                    var id = idWorker.NextId();
                    tmpList.Add(id);
                }
                return tmpList;
            }
            void PrintIdList(IList<long> idList)
            {
                Console.WriteLine($"Id Total Count:{idList.Count}");
                foreach (var id in idList)
                {
                    var time = idWorker.GetDateTimeOffset(id);
                    Console.WriteLine($"Id: {id} Time: {time}");
                }
            }

算法参考资料:分布式唯一ID生成算法——雪花算法(Snowflake)

<think>嗯,用户让我介绍一下雪花算法生成唯一的UID。首先,我需要回忆一下雪花算法的基本结构。Snowflake是Twitter开源的分布式ID生成算法,对吧?它生成的是64位的唯一ID,通常用于分布式系统中确保ID的唯一性。 用户可能是在准备面试,或者是想了解分布式ID生成机制。之前他们问过Spring Bean的生命周期,现在转向雪花算法,可能是想扩展对系统设计的理解。需要确保回答清晰,结构合理,覆盖关键点。 首先,我应该解释雪花算法的组成部分。64位的ID通常分为几个部分:时间戳、工作机器ID、序列号。具体每个部分占多少位呢?比如,时间戳一般是41位,工作机器ID包括数据中心ID和机器ID,各占5位,剩下的12位是序列号。不过不同的实现可能有调整,比如百度的UidGenerator可能调整了位数分配。 接下来,需要说明每个部分的作用。时间戳保证有序性,工作机器ID确保不同节点生成不同的ID,序列号处理同一节点同一毫秒的并发。还要提到时间回拨的问题,如何处理这种情况,比如等待或抛出异常。 然后,要讨论雪花算法的优点,比如高性能、低延迟、趋势递增,这对数据库索引友好。缺点的话,时钟回拨可能是个问题,而且工作机器ID需要手动配置,可能不适合动态扩缩容的场景。 用户可能需要知道实际应用中的例子,比如在分布式系统、订单ID生成中的应用。还可以对比其他分布式ID生成方案,比如UUID、数据库自增、Redis生成等,说明雪花算法的优势。 需要确保回答结构清晰,分点说明,可能用列表或分段来组织内容。还要注意使用中文,避免技术术语过多,但也要准确。如果有必要,可以提到改进方案,比如美团Leaf、百度UidGenerator,这样用户知道有优化的变种存在。 最后,检查是否有遗漏的关键点,比如生成ID长度、是否可能重复、高并发下的表现,以及如何配置工作机器ID。确保回答全面,覆盖用户可能关心的各个方面。</think>--- ### **雪花算法(Snowflake)核心原理** 雪花算法是Twitter开源的分布式唯一ID生成方案,生成ID为**64位整数**,结构清晰且满足分布式系统的高并发、唯一性需求。 --- ### **ID结构分解(64位)** | 组成部分 | 位数 | 作用说明 | |--------------|-------|---------------------------------| | **符号位** | 1位 | 固定为0(保证ID为正整数) | | **时间戳** | 41位 | 记录当前时间与起始时间的差值(毫秒级)| | **工作机器ID**| 10位 | 支持最多$2^{10}=1024$个节点 | | **序列号** | 12位 | 单节点同一毫秒内的自增序号(支持$2^{12}=4096$/ms)| --- ### **工作流程** 1. **时间戳生成** - 使用当前时间减去预设的起始时间(如:2020-01-01),转化为毫秒值,占据高41位 - ✅ 优势:ID随时间递增,利于数据库索引优化 2. **工作机器ID分配** - 通过配置文件或服务注册中心分配唯一机器ID(如:5位数据中心ID + 5位机器ID) - ⚠️ 注意:需避免手动配置冲突(动态扩缩容场景需结合ZooKeeper等协调工具) 3. **序列号管理** - 同一机器同一毫秒内,通过自增序列号生成不同ID - 若当前毫秒序列号耗尽,则阻塞至下一毫秒 --- ### **核心优势** - **高性能**:单机每秒可生成约$409.6$万ID(理论值) - **趋势递增**:利于数据库B+树索引插入 - **去中心化**:无需依赖第三方服务(如数据库、Redis) --- ### **关键问题与解决方案** | 问题 | 解决方案 | |---------------------|---------------------------------------------| | **时钟回拨** | 1. 短暂回拨:等待时钟同步<br>2. 严重回拨:抛出异常 | | **机器ID耗尽** | 拆分位数(如:数据中心ID扩展至7位,机器ID缩至3位) | | **跨毫秒序列重置** | 毫秒变化时序列号归零 | --- ### **改进方案(工业级应用)** 1. **美团Leaf**:优化时钟回拨容忍度 2. **百度UidGenerator**:采用环形缓冲区预生成ID 3. **自定义调整位数**:根据业务需求调整时间戳/机器ID/序列号的占比 --- ### **代码逻辑示例(伪代码)** ```java public class Snowflake { private long lastTimestamp = -1L; private long sequence = 0L; public synchronized long nextId() { long currentTimestamp = getCurrentTimestamp(); if (currentTimestamp < lastTimestamp) { throw new ClockMovedBackException(); } if (currentTimestamp == lastTimestamp) { sequence = (sequence + 1) & MAX_SEQUENCE; if (sequence == 0) { // 当前毫秒序列号耗尽 currentTimestamp = waitNextMillis(lastTimestamp); } } else { sequence = 0L; } lastTimestamp = currentTimestamp; return (currentTimestamp << TIMESTAMP_SHIFT) | (machineId << MACHINE_SHIFT) | sequence; } } ``` --- ### **适用场景** - 分布式系统主键生成(如订单ID、用户ID) - 日志追踪唯一标识 - 高并发写入的数据库分库分表 --- ### **面试回答要点** "雪花算法通过时间戳、机器ID和序列号生成全局唯一ID,兼具有序性和高性能。核心需注意时钟回拨的容错处理,以及机器ID的分配策略。实际应用中需结合业务调整位数分配,或采用改进方案(如Leaf)提升稳定性。"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值