C# Snowflake

C# 版 雪花算法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Snowflake
{
    public class SnowflakeSequence
    {
        //基准时间 
        private const long startTime = 1519740777809L;
        //机器标识位数
        private const int workerIdBits = 5;
        //数据中心标识位数
        private const int dataCenterIdBits = 5;
        //序列号识位数
        private const int sequenceBits = 12;
        //机器ID最大值
        private const long maxWorkerId = -1L ^ (-1L << workerIdBits);
        //数据中心标识ID最大值
        private const long maxDataCenterId = -1L ^ (-1L << dataCenterIdBits);
        //机器ID偏左移12位
        private const int workerIdShift = sequenceBits;
        //数据ID偏左移17位
        private const int dataCenterIdShift = sequenceBits + workerIdBits;
        //时间毫秒左移22位
        private const int timestampLeftShift = sequenceBits + workerIdBits + dataCenterIdBits;
        //序列号ID最大值
        private const long sequenceMask = -1L ^ (-1L << sequenceBits);

        /// <summary>
        /// 实例ID
        /// </summary>
        private long workerId { get; set; }

        /// <summary>
        /// 数据中心id
        /// </summary>
        private long dataCenterId { get; set; }

        /// <summary>
        /// 毫秒级序列值
        /// </summary>
        private long sequence { get; set; } = 0L;

        /// <summary>
        /// 上一次计算id时间戳
        /// </summary>
        private long lastTimestamp { get; set; } = -1L;

        /// <summary>
        /// 基于Snowflake创建分布式ID生成器
        /// sequence
        /// </summary>
        /// <param name="workerId">工作机器ID,数据范围为0~31</param>
        /// <param name="dataCenterId">数据中心ID,数据范围为0~31</param>
        /// <exception cref="Exception"></exception>
        public SnowflakeSequence(long workerId, long dataCenterId)
        {
            if (workerId > maxWorkerId || workerId < 0)
            {
                throw new Exception(String.Format("worker Id can't be greater than %d or less than 0", maxWorkerId));
            }
            if (dataCenterId > maxDataCenterId || dataCenterId < 0)
            {
                throw new Exception(String.Format("dataCenter Id can't be greater than %d or less than 0", maxDataCenterId));
            }
            this.workerId = workerId;
            this.dataCenterId = dataCenterId;
        }

        private static readonly object _lock = new Object();
        /// <summary>
        /// 获取ID
        /// </summary>
        /// <returns></returns>
        /// <exception cref="RuntimeException"></exception>
        public long nextId()
        {
            lock (_lock)
            {
                long timestamp = this.timeGen();
                // 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
                if (timestamp < lastTimestamp)
                {
                    long offset = lastTimestamp - timestamp;
                    if (offset <= 5)
                    {
                        try
                        {
                            //this.wait(offset << 1);
                            Thread.Sleep((int)offset << 1);
                            timestamp = this.timeGen();
                            if (timestamp < lastTimestamp)
                            {
                                throw new Exception(String.Format("Clock moved backwards.  Refusing to generate id for %d milliseconds", offset));
                            }
                        }
                        catch (Exception e)
                        {
                            throw e;
                        }
                    }
                    else
                    {
                        throw new Exception(String.Format("Clock moved backwards.  Refusing to generate id for %d milliseconds", offset));
                    }
                }
                // 解决跨毫秒生成ID序列号始终为偶数的缺陷:如果是同一时间生成的,则进行毫秒内序列
                if (lastTimestamp == timestamp)
                {
                    // 通过位与运算保证计算的结果范围始终是 0-4095
                    sequence = (sequence + 1) & sequenceMask;
                    if (sequence == 0)
                    {
                        timestamp = this.tilNextMillis(lastTimestamp);
                    }
                }
                else
                {
                    // 时间戳改变,毫秒内序列重置
                    sequence = 0L;
                }
                lastTimestamp = timestamp;
                /*
                 * 1.左移运算是为了将数值移动到对应的段(41、5、5,12那段因为本来就在最右,因此不用左移)
                 * 2.然后对每个左移后的值(la、lb、lc、sequence)做位或运算,是为了把各个短的数据合并起来,合并成一个二进制数
                 * 3.最后转换成10进制,就是最终生成的id
                 */
                return ((timestamp - startTime) << timestampLeftShift) |
                        (dataCenterId << dataCenterIdShift) |
                        (workerId << workerIdShift) |
                        sequence;
            }
        }

        /**
         * 保证返回的毫秒数在参数之后(阻塞到下一个毫秒,直到获得新的时间戳)
         *
         * @param lastTimestamp
         * @return
         */
        private long tilNextMillis(long lastTimestamp)
        {
            long timestamp = this.timeGen();
            while (timestamp <= lastTimestamp)
            {
                timestamp = this.timeGen();
            }
            return timestamp;
        }

        /// <summary>
        /// 时钟序列
        /// </summary>
        /// <returns></returns>
        private long timeGen()
        {
            // 解决高并发下获取时间戳的性能问题
            //return SystemClock.now();//运行一段时间死锁
            return DateTimeOffset.Now.ToUnixTimeMilliseconds();
        }
    }

    /// <summary>
    /// 获取时钟
    ///(定时更新_period 毫秒),提高性能
    /// </summary>
    public class SystemClock
    {
        /// <summary>
        /// 间隔时间(毫秒)
        /// </summary>
        private readonly long _period;
        /// <summary>
        /// 时间戳-原子性
        /// </summary>
        private readonly AtomicLong _now;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="period"></param>
        private SystemClock(long period)
        {
            _period = period;
            _now = new AtomicLong(DateTimeOffset.Now.ToUnixTimeMilliseconds());
            //定时更新时钟
            scheduleClockUpdating();
        }

        //lazy 单例线程安全
        private static readonly Lazy<SystemClock> _instance = new Lazy<SystemClock>(() => new SystemClock(1), LazyThreadSafetyMode.ExecutionAndPublication);

        public static SystemClock Instance
        {
            get { return _instance.Value; }
        }

        private void scheduleClockUpdating()
        {
            //ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable-> {
            //    Thread thread = new Thread(runnable, "system-clock");
            //    thread.setDaemon(true);
            //    return thread;
            //});
            //scheduler.scheduleAtFixedRate(()->_now.Set(DateTimeOffset.Now.ToUnixTimeMilliseconds()), _period, _period, TimeUnit.MILLISECONDS);
            //每毫秒更新一次时间戳
            var tm = new Timer(objState =>
            {
                _now.Set(DateTimeOffset.Now.ToUnixTimeMilliseconds());
            }, null, _period, _period);
        }

        private long currentTimeMillis()
        {
            return _now.Get();
        }

        public static long now()
        {
            return Instance.currentTimeMillis();
        }

        public static String nowDate()
        {
            return now().ToString();
        }

    }

    /// <summary>
    /// Provides lock-free atomic read/write utility for a <c>long</c> value. The atomic classes found in this package
    /// were are meant to replicate the <c>java.util.concurrent.atomic</c> package in Java by Doug Lea. The two main differences
    /// are implicit casting back to the <c>long</c> data type, and the use of a non-volatile inner variable.
    /// 
    /// <para>The internals of these classes contain wrapped usage of the <c>System.Threading.Interlocked</c> class, which is how
    /// we are able to provide atomic operation without the use of locks. </para>
    /// </summary>
    /// <remarks>
    /// It's also important to note that <c>++</c> and <c>--</c> are never atomic, and one of the main reasons this class is 
    /// needed. I don't believe its possible to overload these operators in a way that is autonomous.
    /// </remarks>
    public class AtomicLong
    {
        private long _value;

        /// <summary>
        /// Creates a new <c>AtomicLong</c> instance with an initial value of <c>0</c>.
        /// </summary>
        public AtomicLong()
            : this(0)
        {

        }

        /// <summary>
        /// Creates a new <c>AtomicLong</c> instance with the initial value provided.
        /// </summary>
        public AtomicLong(long value)
        {
            _value = value;
        }

        /// <summary>
        /// This method returns the current value.
        /// </summary>
        /// <returns>
        /// The <c>long</c> value accessed atomically.
        /// </returns>
        public long Get()
        {
            return Interlocked.Read(ref _value);
        }

        /// <summary>
        /// This method sets the current value atomically.
        /// </summary>
        /// <param name="value">
        /// The new value to set.
        /// </param>
        public void Set(long value)
        {
            Interlocked.Exchange(ref _value, value);
        }

        /// <summary>
        /// This method atomically sets the value and returns the original value.
        /// </summary>
        /// <param name="value">
        /// The new value.
        /// </param>
        /// <returns>
        /// The value before setting to the new value.
        /// </returns>
        public long GetAndSet(long value)
        {
            return Interlocked.Exchange(ref _value, value);
        }

        /// <summary>
        /// Atomically sets the value to the given updated value if the current value <c>==</c> the expected value.
        /// </summary>
        /// <param name="expected">
        /// The value to compare against.
        /// </param>
        /// <param name="result">
        /// The value to set if the value is equal to the <c>expected</c> value.
        /// </param>
        /// <returns>
        /// <c>true</c> if the comparison and set was successful. A <c>false</c> indicates the comparison failed.
        /// </returns>
        public bool CompareAndSet(long expected, long result)
        {
            return Interlocked.CompareExchange(ref _value, result, expected) == expected;
        }

        /// <summary>
        /// Atomically adds the given value to the current value.
        /// </summary>
        /// <param name="delta">
        /// The value to add.
        /// </param>
        /// <returns>
        /// The updated value.
        /// </returns>
        public long AddAndGet(long delta)
        {
            return Interlocked.Add(ref _value, delta);
        }

        /// <summary>
        /// This method atomically adds a <c>delta</c> the value and returns the original value.
        /// </summary>
        /// <param name="delta">
        /// The value to add to the existing value.
        /// </param>
        /// <returns>
        /// The value before adding the delta.
        /// </returns>
        public long GetAndAdd(long delta)
        {
            for (; ; )
            {
                long current = Get();
                long next = current + delta;
                if (CompareAndSet(current, next))
                {
                    return current;
                }
            }
        }

        /// <summary>
        /// This method increments the value by 1 and returns the previous value. This is the atomic 
        /// version of post-increment.
        /// </summary>
        /// <returns>
        /// The value before incrementing.
        /// </returns>
        public long Increment()
        {
            return GetAndAdd(1);
        }

        /// <summary>
        /// This method decrements the value by 1 and returns the previous value. This is the atomic 
        /// version of post-decrement.
        /// </summary>
        /// <returns>
        /// The value before decrementing.
        /// </returns>
        public long Decrement()
        {
            return GetAndAdd(-1);
        }

        /// <summary>
        /// This method increments the value by 1 and returns the new value. This is the atomic version 
        /// of pre-increment.
        /// </summary>
        /// <returns>
        /// The value after incrementing.
        /// </returns>
        public long PreIncrement()
        {
            return Interlocked.Increment(ref _value);
        }

        /// <summary>
        /// This method decrements the value by 1 and returns the new value. This is the atomic version 
        /// of pre-decrement.
        /// </summary>
        /// <returns>
        /// The value after decrementing.
        /// </returns>
        public long PreDecrement()
        {
            return Interlocked.Decrement(ref _value);
        }

        /// <summary>
        /// This operator allows an implicit cast from <c>AtomicLong</c> to <c>long</c>.
        /// </summary>
        public static implicit operator long(AtomicLong value)
        {
            return value.Get();
        }

    }
}

参考:GitHub - wzycoding/snowflake

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值