Twitter的分佈式自增ID算法snowflake(雪花算法) - C#版

概述
分佈式系統中,有一些須要使用全局惟一ID的場景,這種時候爲了防止ID衝突可使用36位的UUID,可是UUID有一些缺點,首先他相對比較長,另外UUID通常是無序的。有些時候咱們但願能使用一種簡單一些的ID,而且但願ID可以按照時間有序生成。而twitter的snowflake解決了這種需求,最初Twitter把存儲系統從MySQL遷移到Cassandra,由於Cassandra沒有順序ID生成機制,因此開發了這樣一套全局惟一ID生成服務。
該項目地址爲:https://github.com/twitter/snowflake是用Scala實現的。
python版詳見開源項目https://github.com/erans/pysnowflake。python

結構
snowflake的結構以下(每部分用-分開):git

0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000github

第一位爲未使用,接下來的41位爲毫秒級時間(41位的長度可使用69年),而後是5位datacenterId和5位workerId(10位的長度最多支持部署1024個節點) ,最後12位是毫秒內的計數(12位的計數順序號支持每一個節點每毫秒產生4096個ID序號)less

一共加起來恰好64位,爲一個Long型。(轉換成字符串長度爲18)分佈式

snowflake生成的ID總體上按照時間自增排序,而且整個分佈式系統內不會產生ID碰撞(由datacenter和workerId做區分),而且效率較高。聽說:snowflake每秒可以產生26萬個ID。this

源碼(C#版本源碼)spa

public class IdWorker
    {
        //機器ID
        private static long workerId;
        private static long twepoch = 687888001020L; //惟一時間,這是一個避免重複的隨機量,自行設定不要大於當前時間戳
        private static long sequence = 0L;
        private static int workerIdBits = 4; //機器碼字節數。4個字節用來保存機器碼(定義爲Long類型會出現,最大偏移64位,因此左移64位沒有意義)
        public static long maxWorkerId = -1L ^ -1L << workerIdBits; //最大機器ID
        private static int sequenceBits = 10; //計數器字節數,10個字節用來保存計數碼
        private static int workerIdShift = sequenceBits; //機器碼數據左移位數,就是後面計數器佔用的位數
        private static int timestampLeftShift = sequenceBits + workerIdBits; //時間戳左移動位數就是機器碼和計數器總字節數
        public static long sequenceMask = -1L ^ -1L << sequenceBits; //一微秒內能夠產生計數,若是達到該值則等到下一微妙在進行生成
        private long lastTimestamp = -1L;

        /// <summary>
        /// 機器碼
        /// </summary>
        /// <param name="workerId"></param>
        public IdWorker(long workerId)
        {
            if (workerId > maxWorkerId || workerId < 0)
                throw new Exception(string.Format("worker Id can't be greater than {0} or less than 0 ", workerId));
            IdWorker.workerId = workerId;
        }

        public long nextId()
        {
            lock (this)
            {
                long timestamp = timeGen();
                if (this.lastTimestamp == timestamp)
                { //同一微妙中生成ID
                    IdWorker.sequence = (IdWorker.sequence + 1) & IdWorker.sequenceMask; //用&運算計算該微秒內產生的計數是否已經到達上限
                    if (IdWorker.sequence == 0)
                    {
                        //一微妙內產生的ID計數已達上限,等待下一微妙
                        timestamp = tillNextMillis(this.lastTimestamp);
                    }
                }
                else
                { //不一樣微秒生成ID
                    IdWorker.sequence = 0; //計數清0
                }
                if (timestamp < lastTimestamp)
                { //若是當前時間戳比上一次生成ID時時間戳還小,拋出異常,由於不能保證如今生成的ID以前沒有生成過
                    throw new Exception(string.Format("Clock moved backwards.  Refusing to generate id for {0} milliseconds",
                        this.lastTimestamp - timestamp));
                }
                this.lastTimestamp = timestamp; //把當前時間戳保存爲最後生成ID的時間戳
                long nextId = (timestamp - twepoch << timestampLeftShift) | IdWorker.workerId << IdWorker.workerIdShift | IdWorker.sequence;
                return nextId;
            }
        }

        /// <summary>
        /// 獲取下一微秒時間戳
        /// </summary>
        /// <param name="lastTimestamp"></param>
        /// <returns></returns>
        private long tillNextMillis(long lastTimestamp)
        {
            long timestamp = timeGen();
            while (timestamp <= lastTimestamp)
            {
                timestamp = timeGen();
            }
            return timestamp;
        }

        /// <summary>
        /// 生成當前時間戳
        /// </summary>
        /// <returns></returns>
        private long timeGen()
        {
            return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
        }

    }

調用方法:.net

IdWorker idworker = new IdWorker(1);
            for (int i = 0; i < 1000; i++)
            {
                Response.Write(idworker.nextId() + "<br/>");
            }

轉自:https://blog.csdn.net/u011872945/article/details/54562213code

相關文章
相關標籤/搜索