温馨提示×

Snowflake算法原理及C#实现

c#
小樊
82
2024-09-02 12:41:56
栏目: 编程语言

Snowflake 是 Twitter 开源的一个分布式 ID 生成算法,用于在分布式系统中生成唯一、有序、不重复的 ID。它的设计目标是在不依赖数据库或其他存储设备的情况下生成全局唯一 ID。

Snowflake 算法的原理如下:

  1. 使用 64 位整数表示 ID,分为以下几部分:

    • 1 位符号位:始终为 0,表示正数。
    • 41 位时间戳:表示当前时间与某个固定时间点(如:2021-01-01 00:00:00)的差值,单位是毫秒。这部分可以表示约 69 年的时间。
    • 10 位工作机器 ID:可以分为 5 位数据中心 ID 和 5 位工作节点 ID,用于标识不同的数据中心和工作节点,最多可以支持 1024 个节点。
    • 12 位序列号:在同一毫秒内,同一个工作节点可以生成的不同 ID 的数量,最多可以生成 4096 个。
  2. 算法流程:

    • 首先,根据当前时间计算出时间戳,并将其左移 22 位,得到高 41 位。
    • 然后,将工作机器 ID 左移 12 位,得到中 10 位。
    • 最后,将序列号加 1,得到低 12 位。
    • 将高 41 位、中 10 位和低 12 位拼接起来,得到一个 64 位的整数,即为生成的全局唯一 ID。

下面是一个简单的 C# 实现:

public class Snowflake
{
    private const long Twepoch = 1609459200000L; // 2021-01-01 00:00:00
    private const int WorkerIdBits = 5;
    private const int DatacenterIdBits = 5;
    private const int SequenceBits = 12;
    private const long MaxWorkerId = -1L ^ (-1L<< WorkerIdBits);
    private const long MaxDatacenterId = -1L ^ (-1L<< DatacenterIdBits);
    private const int WorkerIdShift = SequenceBits;
    private const int DatacenterIdShift = SequenceBits + WorkerIdBits;
    private const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits;
    private const long SequenceMask = -1L ^ (-1L << SequenceBits);

    private readonly object _lock = new object();
    private long _sequence;
    private long _lastTimestamp;

    public Snowflake(long workerId, long datacenterId)
    {
        if (workerId > MaxWorkerId || workerId < 0)
            throw new ArgumentException($"Worker Id must be between 0 and {MaxWorkerId}");
        if (datacenterId > MaxDatacenterId || datacenterId < 0)
            throw new ArgumentException($"Datacenter Id must be between 0 and {MaxDatacenterId}");

        WorkerId = workerId;
        DatacenterId = datacenterId;
    }

    public long WorkerId { get; }
    public long DatacenterId { get; }

    public long NextId()
    {
        lock (_lock)
        {
            var timestamp = GetCurrentTimestamp();
            if (timestamp < _lastTimestamp)
                throw new Exception("Invalid system clock");

            if (_lastTimestamp == timestamp)
            {
                _sequence = (_sequence + 1) & SequenceMask;
                if (_sequence == 0)
                    timestamp = WaitNextMillis(_lastTimestamp);
            }
            else
            {
                _sequence = 0;
            }

            _lastTimestamp = timestamp;
            return ((timestamp - Twepoch)<< TimestampLeftShift) |
                   (DatacenterId<< DatacenterIdShift) |
                   (WorkerId<< WorkerIdShift) |
                   _sequence;
        }
    }

    private long GetCurrentTimestamp()
    {
        return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
    }

    private long WaitNextMillis(long lastTimestamp)
    {
        var timestamp = GetCurrentTimestamp();
        while (timestamp <= lastTimestamp)
        {
            timestamp = GetCurrentTimestamp();
        }
        return timestamp;
    }
}

使用示例:

var snowflake = new Snowflake(1, 1);
var id = snowflake.NextId();
Console.WriteLine(id);

这个实现是线程安全的,但在高并发场景下可能会有性能瓶颈。在实际应用中,可以根据需要对其进行优化。

0