先抄个雪花id介绍,雪花算法:

雪花算法的原始版本是,用于生成分布式id(纯数字,时间顺序),订单编号等。

自增id:对于数据敏感场景不宜使用,且不适合于分布式场景。
guid:采用无意义字符串,数据量增大时造成访问过慢,且不宜排序。

算法描述:

    • 最高位是符号位,始终为0,不可用。
    • 41位的时间序列,精确到毫秒级,41位的长度可以使用69年。时间位还有一个很重要的作用是可以根据时间进行排序。
    • 10位的机器标识,10位的长度最多支持部署1024个节点。
    • 12位的计数序列号,序列号即一系列的自增id,可以支持同一节点同一毫秒生成多个id序号,12位的计数序列号支持每个节点每毫秒产生4096个id序号。

 

好了,回归本人自己介绍:时钟回拨

雪花id严重依赖系统当前时间,当系统时间被人为反后调整时,算法会出问题,可能会出重复id.snowflake原算法是在检测到系统时间被回调后直接抛异常.本代码在时钟回拨后,会将生成的id时间戳停留在最后一次时间戳上,等待系统时间追上后即可以避过时钟回拨问题.

这种处理方式的优点是时钟回拨后不会异常,能一直生成出雪花id,但缺点是雪花id中的时间戳不是系统的当前时间,会是回拨前的最后记录的一次时间戳,但相差也不大.不知道有没有什么生产系统会对这个时间戳要求非常严格,无法使用这种补救方式的?

当然停掉系统后的时钟回拨是无法处理的,这种还是会有可能出现重复id的.

 

介绍完毕,下面直接上源码吧,,本源码还实现雪花id的生成,以及解析雪花id等. 

源码git地址: https://gitee.com/itsm/learning_example/tree/master/snowflake-雪花id

  1  public class snowflakeid
  2     {
  3 
  4         // 开始时间截((new datetime(2020, 1, 1, 0, 0, 0, datetimekind.utc)-jan1st1970).totalmilliseconds)
  5         private const long twepoch = 1577836800000l;
  6 
  7         // 机器id所占的位数
  8         private const int workeridbits = 5;
  9 
 10         // 数据标识id所占的位数
 11         private const int datacenteridbits = 5;
 12 
 13         // 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) 
 14         private const long maxworkerid = -1l ^ (-1l << workeridbits);
 15 
 16         // 支持的最大数据标识id,结果是31 
 17         private const long maxdatacenterid = -1l ^ (-1l << datacenteridbits);
 18 
 19         // 序列在id中占的位数 
 20         private const int sequencebits = 12;
 21 
 22         // 数据标识id向左移17位(12+5) 
 23         private const int datacenteridshift = sequencebits + workeridbits;
 24 
 25         // 机器id向左移12位 
 26         private const int workeridshift = sequencebits;
 27 
 28 
 29         // 时间截向左移22位(5+5+12) 
 30         private const int timestampleftshift = sequencebits + workeridbits + datacenteridbits;
 31 
 32         // 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) 
 33         private const long sequencemask = -1l ^ (-1l << sequencebits);
 34 
 35         // 数据中心id(0~31) 
 36         public long datacenterid { get; private set; }
 37 
 38         // 工作机器id(0~31) 
 39         public long workerid { get; private set; }
 40 
 41         // 毫秒内序列(0~4095) 
 42         public long sequence { get; private set; }
 43 
 44         // 上次生成id的时间截 
 45         public long lasttimestamp { get; private set; }
 46 
 47 
 48         /// <summary>
 49         /// 雪花id
 50         /// </summary>
 51         /// <param name="datacenterid">数据中心id</param>
 52         /// <param name="workerid">工作机器id</param>
 53         public snowflakeid(long datacenterid,long workerid )
 54         {
 55             if (datacenterid > maxdatacenterid || datacenterid < 0)
 56             {
 57                 throw new exception(string.format("datacenter id can't be greater than {0} or less than 0", maxdatacenterid));
 58             }
 59             if (workerid > maxworkerid || workerid < 0)
 60             {
 61                 throw new exception(string.format("worker id can't be greater than {0} or less than 0", maxworkerid));
 62             }
 63             this.workerid = workerid;
 64             this.datacenterid = datacenterid;
 65             this.sequence = 0l;
 66             this.lasttimestamp = -1l;
 67         }
 68 
 69         /// <summary>
 70         /// 获得下一个id
 71         /// </summary>
 72         /// <returns></returns>
 73         public long nextid()
 74         {
 75             lock (this)
 76             {
 77                 long timestamp = getcurrenttimestamp();
 78                 if (timestamp > lasttimestamp) //时间戳改变,毫秒内序列重置
 79                 {
 80                     sequence = 0l;
 81                 }
 82                 else if (timestamp == lasttimestamp) //如果是同一时间生成的,则进行毫秒内序列
 83                 {
 84                     sequence = (sequence + 1) & sequencemask;
 85                     if (sequence == 0) //毫秒内序列溢出
 86                     {
 87                         timestamp = getnexttimestamp(lasttimestamp); //阻塞到下一个毫秒,获得新的时间戳
 88                     }
 89                 }
 90                 else   //当前时间小于上一次id生成的时间戳,证明系统时钟被回拨,此时需要做回拨处理
 91                 {                   
 92                     sequence = (sequence + 1) & sequencemask;
 93                     if (sequence > 0) 
 94                     {
 95                         timestamp = lasttimestamp;     //停留在最后一次时间戳上,等待系统时间追上后即完全度过了时钟回拨问题。
 96                     }
 97                     else   //毫秒内序列溢出
 98                     {
 99                         timestamp = lasttimestamp + 1;   //直接进位到下一个毫秒                          
100                     }
101                     //throw new exception(string.format("clock moved backwards.  refusing to generate id for {0} milliseconds", lasttimestamp - timestamp));
102                 }
103 
104                 lasttimestamp = timestamp;       //上次生成id的时间截
105 
106                 //移位并通过或运算拼到一起组成64位的id
107                 var id= ((timestamp - twepoch) << timestampleftshift)
108                         | (datacenterid << datacenteridshift)
109                         | (workerid << workeridshift)
110                         | sequence;
111                 return id;
112             }
113         }
114 
115         /// <summary>
116         /// 解析雪花id
117         /// </summary>
118         /// <returns></returns>
119         public static string analyzeid(long id)
120         {
121             stringbuilder sb = new stringbuilder();
122 
123             var timestamp = (id >> timestampleftshift)  ;
124             var time = jan1st1970.addmilliseconds(timestamp + twepoch);
125             sb.append(time.tolocaltime().tostring("yyyy-mm-dd hh:mm:ss:fff"));
126 
127             var datacenterid = (id ^ (timestamp  << timestampleftshift)) >> datacenteridshift;
128             sb.append("_" + datacenterid);
129 
130             var workerid = (id ^ ((timestamp  << timestampleftshift) | (datacenterid << datacenteridshift))) >> workeridshift;
131             sb.append("_" + workerid);
132 
133             var sequence = id & sequencemask;
134             sb.append("_" + sequence);
135 
136             return sb.tostring();
137         }
138 
139         /// <summary>
140         /// 阻塞到下一个毫秒,直到获得新的时间戳
141         /// </summary>
142         /// <param name="lasttimestamp">上次生成id的时间截</param>
143         /// <returns>当前时间戳</returns>
144         private static long getnexttimestamp(long lasttimestamp)
145         {
146             long timestamp = getcurrenttimestamp();
147             while (timestamp <= lasttimestamp)
148             {
149                 timestamp = getcurrenttimestamp();
150             }
151             return timestamp;
152         }
153 
154         /// <summary>
155         /// 获取当前时间戳
156         /// </summary>
157         /// <returns></returns>
158         private static long getcurrenttimestamp()
159         {
160             return (long)(datetime.utcnow - jan1st1970).totalmilliseconds; 
161         }
162 
163         private static readonly datetime jan1st1970 = new datetime(1970, 1, 1, 0, 0, 0, datetimekind.utc);
164     }