-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Easytransaction version 1.4.0 (#133)
* upgrade Fescar0.3.1 to Seata 0.8.1 * support ET run without Zookeeper * implement DB based Master selector * implement DB based StringCodec * implement DB based snowflaker * fix #128
- Loading branch information
Showing
58 changed files
with
2,079 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
easytrans-core/src/main/java/com/yiqiniu/easytrans/idgen/impl/SnowFlake.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.yiqiniu.easytrans.idgen.impl; | ||
|
||
public class SnowFlake { | ||
/** | ||
* modified from https://github.com/beyondfengyu/SnowFlake | ||
*/ | ||
|
||
|
||
/** | ||
* 每一部分占用的位数 | ||
*/ | ||
private final static long TIMESTAMP_SECOND_BIT = 32; | ||
private final static long SEQUENCE_BIT = 16; //序列号占用的位数 4096 | ||
public final static long MACHINE_BIT = 64 - TIMESTAMP_SECOND_BIT - SEQUENCE_BIT; //机器标识占用的位数 32 | ||
/** | ||
* 每一部分的最大值 | ||
*/ | ||
private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT); | ||
private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT); | ||
|
||
/** | ||
* 每一部分向左的位移 | ||
*/ | ||
private final static long TIMESTMP_LEFT = SEQUENCE_BIT; | ||
private final static long MACHINE_LEFT = TIMESTMP_LEFT + MACHINE_BIT; | ||
|
||
private long machineId; //机器标识 | ||
private long sequence = 0L; //序列号 | ||
private long lastStmp = -1L;//上一次时间戳 | ||
|
||
public SnowFlake(long machineId) { | ||
if (machineId > MAX_MACHINE_NUM || machineId < 0) { | ||
throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0"); | ||
} | ||
this.machineId = machineId; | ||
} | ||
|
||
/** | ||
* 产生下一个ID | ||
* | ||
* @return | ||
*/ | ||
public synchronized long nextId() { | ||
long currStmp = getCurrentSecond(); | ||
if (currStmp < lastStmp) { | ||
throw new RuntimeException("Clock moved backwards. Refusing to generate id"); | ||
} | ||
|
||
if (currStmp == lastStmp) { | ||
//相同秒内,序列号自增 | ||
sequence = (sequence + 1) & MAX_SEQUENCE; | ||
//同一秒的序列数已经达到最大 | ||
if (sequence == 0L) { | ||
throw new RuntimeException("max tp/ms reached!"); | ||
} | ||
} else { | ||
//不同毫秒内,序列号置为0 | ||
sequence = 0L; | ||
} | ||
|
||
lastStmp = currStmp; | ||
|
||
return machineId << MACHINE_LEFT // 机器标识部分 | ||
| currStmp << TIMESTMP_LEFT // 时间戳部分 | ||
| sequence; //序列号部分 | ||
} | ||
|
||
|
||
private long getCurrentSecond() { | ||
long mill = System.currentTimeMillis() / 1000; | ||
return mill; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.