C# 實現 Snowflake算法 ID生成

http://blog.csdn.net/w200221626/article/details/52064976算法

C# 實現 Snowflake算法測試

DJK)PSWH8S(5O09B75{OYTU

/// <summary>
    /// 動態生產有規律的ID Snowflake算法是Twitter的工程師爲實現遞增而不重複的ID實現的
    /// http://blog.csdn.net/w200221626/article/details/52064976     
    /// C# 實現 Snowflake算法 
    /// </summary>
    public class Snowflake
    {
        private static long machineId;//機器ID
        private static long datacenterId = 0L;//數據ID
        private static long sequence = 0L;//計數從零開始

        private static long twepoch = 687888001020L; //惟一時間隨機量

        private static long machineIdBits = 5L; //機器碼字節數
        private static long datacenterIdBits = 5L;//數據字節數
        public static long maxMachineId = -1L ^ -1L << (int)machineIdBits; //最大機器ID
        private static long maxDatacenterId = -1L ^ (-1L << (int)datacenterIdBits);//最大數據ID

        private static long sequenceBits = 12L; //計數器字節數,12個字節用來保存計數碼        
        private static long machineIdShift = sequenceBits; //機器碼數據左移位數,就是後面計數器佔用的位數
        private static long datacenterIdShift = sequenceBits + machineIdBits;
        private static long timestampLeftShift = sequenceBits + machineIdBits + datacenterIdBits; //時間戳左移動位數就是機器碼+計數器總字節數+數據字節數
        public static long sequenceMask = -1L ^ -1L << (int)sequenceBits; //一微秒內能夠產生計數,若是達到該值則等到下一微妙在進行生成
        private static long lastTimestamp = -1L;//最後時間戳

        private static object syncRoot = new object();//加鎖對象
        static Snowflake snowflake;

        public static Snowflake Instance()
        {
            if (snowflake == null)
                snowflake = new Snowflake();
            return snowflake;
        }

        public Snowflake()
        {
            Snowflakes(0L, -1);
        }

        public Snowflake(long machineId)
        {
            Snowflakes(machineId, -1);
        }

        public Snowflake(long machineId, long datacenterId)
        {
            Snowflakes(machineId, datacenterId);
        }

        private void Snowflakes(long machineId, long datacenterId)
        {
            if (machineId >= 0)
            {
                if (machineId > maxMachineId)
                {
                    throw new Exception("機器碼ID非法");
                }
                Snowflake.machineId = machineId;
            }
            if (datacenterId >= 0)
            {
                if (datacenterId > maxDatacenterId)
                {
                    throw new Exception("數據中心ID非法");
                }
                Snowflake.datacenterId = datacenterId;
            }
        }

        /// <summary>
        /// 生成當前時間戳
        /// </summary>
        /// <returns>毫秒</returns>
        private static long GetTimestamp()
        {
            //讓他2000年開始
            return (long)(DateTime.UtcNow - new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
        }

        /// <summary>
        /// 獲取下一微秒時間戳
        /// </summary>
        /// <param name="lastTimestamp"></param>
        /// <returns></returns>
        private static long GetNextTimestamp(long lastTimestamp)
        {
            long timestamp = GetTimestamp();
            int count = 0;
            while (timestamp <= lastTimestamp)//這裏獲取新的時間,可能會有錯,這算法與comb同樣對機器時間的要求很嚴格
            {
                count++;
                if (count > 10)
                    throw new Exception("機器的時間可能不對");
                Thread.Sleep(1);
                timestamp = GetTimestamp();
            }
            return timestamp;
        }

        /// <summary>
        /// 獲取長整形的ID
        /// </summary>
        /// <returns></returns>
        public long GetId()
        {
            lock (syncRoot)
            {
                long timestamp = GetTimestamp();
                if (Snowflake.lastTimestamp == timestamp)
                { //同一微妙中生成ID
                    sequence = (sequence + 1) & sequenceMask; //用&運算計算該微秒內產生的計數是否已經到達上限
                    if (sequence == 0)
                    {
                        //一微妙內產生的ID計數已達上限,等待下一微妙
                        timestamp = GetNextTimestamp(Snowflake.lastTimestamp);
                    }
                }
                else
                {
                    //不一樣微秒生成ID
                    sequence = 0L;
                }
                if (timestamp < lastTimestamp)
                {
                    throw new Exception("時間戳比上一次生成ID時時間戳還小,故異常");
                }
                Snowflake.lastTimestamp = timestamp; //把當前時間戳保存爲最後生成ID的時間戳
                long Id = ((timestamp - twepoch) << (int)timestampLeftShift)
                    | (datacenterId << (int)datacenterIdShift)
                    | (machineId << (int)machineIdShift)
                    | sequence;
                return Id;
            }
        }

    }
    [TestClass]
    public class SnowflakeUnitTest1
    {
        /// <summary>
        /// 動態生產有規律的ID Snowflake算法是Twitter的工程師爲實現遞增而不重複的ID實現的
        /// </summary>
        [TestMethod]
        public void SnowflakeTestMethod1()
        {
            var ids = new List<long>();
            for (int i = 0; i < 1000000; i++)//測試同時100W有序ID
            {
                ids.Add(Snowflake.Instance().GetId());
            }
            for (int i = 0; i < ids.Count - 1; i++)
            {
                Assert.IsTrue(ids[i] < ids[i+1]);
            }
        }
    }
namespace ConsoleApplicationTester
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine("開始執行 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff") + "    " + Snowflake.Instance().GetId());

                Console.WriteLine("Snowflake.maxMachineId:" + Snowflake.maxMachineId);
            }
        }
    }
}
相關文章
相關標籤/搜索