Skip to content

Commit d037153

Browse files
committed
default id gen
1 parent bdde700 commit d037153

6 files changed

Lines changed: 72 additions & 6 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.codingapi.txlcn.common.util.id;
2+
3+
/**
4+
* Description:
5+
* Date: 2/1/19
6+
*
7+
* @author ujued
8+
*/
9+
public class DefaultIdGen implements IdGen {
10+
11+
/**
12+
* 2019-2-1
13+
*/
14+
private static final long START_TIME = 1548992829394L;
15+
16+
private long machineId;
17+
18+
private int machineOffset;
19+
20+
private long seq;
21+
22+
private int seqLen;
23+
24+
private long lastTime;
25+
26+
public DefaultIdGen(int seqLen, long machineId) {
27+
this.seqLen = seqLen;
28+
this.machineOffset = seqLen;
29+
this.machineId = machineId;
30+
}
31+
32+
@Override
33+
public synchronized String nextId() {
34+
long curTime = System.currentTimeMillis();
35+
if (curTime < lastTime) {
36+
throw new IllegalStateException("");
37+
}
38+
39+
if (curTime == lastTime) {
40+
seq = (seq + 1) & (~(-1 << seqLen));
41+
if (seq == 0L) {
42+
curTime = tilNextMillis();
43+
}
44+
} else {
45+
seq = 0L;
46+
}
47+
48+
lastTime = curTime;
49+
50+
long seqWithMachine = (machineId << machineOffset & Long.MAX_VALUE) | seq;
51+
52+
return String.valueOf(curTime - START_TIME) + seqWithMachine;
53+
}
54+
55+
private long tilNextMillis() {
56+
long newTime = System.currentTimeMillis();
57+
while (newTime <= lastTime) {
58+
newTime = System.currentTimeMillis();
59+
}
60+
return newTime;
61+
}
62+
}

txlcn-common/src/main/java/com/codingapi/txlcn/common/util/id/IdGen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
* @author ujued
2323
*/
2424
public interface IdGen {
25-
long nextId();
25+
String nextId();
2626
}

txlcn-common/src/main/java/com/codingapi/txlcn/common/util/id/IdGenInit.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public abstract class IdGenInit {
1414
public static void applySnowFlakeIdGen(int machineLen, int machineId) {
1515
SnowFlakeGenerator.Factory factory = new SnowFlakeGenerator.Factory(machineLen, 0);
1616
SnowFlakeGenerator snowFlakeGenerator = factory.create(0, machineId);
17-
RandomUtils.init(snowFlakeGenerator::nextId);
17+
RandomUtils.init(() -> String.valueOf(snowFlakeGenerator.nextId()));
18+
}
19+
20+
public static void applyDefaultIdGen(int seqLen, int machineId) {
21+
RandomUtils.init(new DefaultIdGen(seqLen, machineId));
1822
}
1923
}

txlcn-common/src/main/java/com/codingapi/txlcn/common/util/id/RandomUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public static String getUUID() {
3535

3636
public static String randomKey() {
3737
if (Objects.isNull(theIdGen)) {
38-
theIdGen = System::nanoTime;
38+
theIdGen = new DefaultIdGen(12, 0);
3939
}
40-
return String.valueOf(theIdGen.nextId());
40+
return theIdGen.nextId();
4141
}
4242

4343
public static String simpleKey() {

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/txmsg/TCSideRpcInitCallBack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void connected(String remoteKey) {
8282
txClientConfig.applyMachineId(resParams.getMachineId());
8383

8484
// 2. IdGen 初始化
85-
IdGenInit.applySnowFlakeIdGen(resParams.getMachineLen(), resParams.getMachineId());
85+
IdGenInit.applyDefaultIdGen(resParams.getMachineLen(), resParams.getMachineId());
8686

8787
// 3. 日志
8888
log.info("Finally, determined dtx time is {}ms, tm rpc timeout is {} ms, machineId is {}",

txlcn-tm/src/main/java/com/codingapi/txlcn/tm/txmsg/EnsureIdGenEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void disconnect(String remoteKey, String appName) {
5555

5656
@Override
5757
public void init() throws Exception {
58-
IdGenInit.applySnowFlakeIdGen(managerConfig.getMachineIdLen(), managerService.machineIdSync());
58+
IdGenInit.applyDefaultIdGen(managerConfig.getMachineIdLen(), managerService.machineIdSync());
5959

6060
Transactions.setApplicationIdWhenRunning(ApplicationInformation.modId(environment, serverProperties));
6161
}

0 commit comments

Comments
 (0)