1
0
mirror of synced 2026-02-18 19:07:56 +08:00

重构StartTime

This commit is contained in:
zhouzj
2021-03-14 02:03:37 +08:00
parent 4853a1e528
commit 8baa83777f
13 changed files with 38 additions and 140 deletions

View File

@@ -20,7 +20,7 @@ public class IdGeneratorOptions {
* 开始时间
* 不能超过当前系统时间
*/
public long StartTime = 0;
public long BaseTime = 1582136402000L;
/**
* 机器码,必须由外部系统设置

View File

@@ -14,7 +14,7 @@ public class SnowWorkerM1 implements ISnowWorker {
/**
* 基础时间
*/
protected final long StartTimeUtc;
protected final long BaseTime;
/**
* 机器码
@@ -67,7 +67,7 @@ public class SnowWorkerM1 implements ISnowWorker {
MaxSeqNumber = options.MaxSeqNumber > 0 ? options.MaxSeqNumber : (int) Math.pow(2, SeqBitLength);
MinSeqNumber = options.MinSeqNumber;
TopOverCostCount = options.TopOverCostCount;
StartTimeUtc = options.StartTime != 0 ? options.StartTime : 1582136402000L;
BaseTime = options.BaseTime != 0 ? options.BaseTime : 1582136402000L;
_TimestampShift = (byte) (WorkerIdBitLength + SeqBitLength);
_CurrentSeqNumber = options.MinSeqNumber;
}
@@ -224,7 +224,7 @@ public class SnowWorkerM1 implements ISnowWorker {
protected long GetCurrentTimeTick() {
long millis = System.currentTimeMillis();
return millis - StartTimeUtc;
return millis - BaseTime;
}
protected long GetNextTimeTick() {

View File

@@ -17,46 +17,38 @@ public class DefaultIdGenerator implements IIdGenerator {
private final ISnowWorker _SnowWorker;
public DefaultIdGenerator(IdGeneratorOptions options) throws IdGeneratorException {
if (options == null)
{
if (options == null) {
throw new IdGeneratorException("options error.");
}
if (options.StartTime > System.currentTimeMillis())
{
throw new IdGeneratorException("StartTime error.");
if (options.BaseTime < 315504000000L || options.BaseTime > System.currentTimeMillis()) {
throw new IdGeneratorException("BaseTime error.");
}
if (options.SeqBitLength + options.WorkerIdBitLength > 22)
{
if (options.SeqBitLength + options.WorkerIdBitLength > 22) {
throw new IdGeneratorException("errorWorkerIdBitLength + SeqBitLength <= 22");
}
double maxWorkerIdNumber = Math.pow(2, options.WorkerIdBitLength) - 1;
if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber)
{
if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber) {
throw new IdGeneratorException("WorkerId error. (range:[1, " + maxWorkerIdNumber + "]");
}
if (options.SeqBitLength < 2 || options.SeqBitLength > 21)
{
if (options.SeqBitLength < 2 || options.SeqBitLength > 21) {
throw new IdGeneratorException("SeqBitLength error. (range:[2, 21])");
}
double maxSeqNumber = Math.pow(2, options.SeqBitLength) - 1;
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber)
{
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) {
throw new IdGeneratorException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
}
double maxValue = maxSeqNumber - 2;
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue)
{
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue) {
throw new IdGeneratorException("MinSeqNumber error. (range:[5, " + maxValue + "]");
}
switch (options.Method)
{
switch (options.Method) {
case 1:
_SnowWorker = new SnowWorkerM1(options);
break;
@@ -68,8 +60,7 @@ public class DefaultIdGenerator implements IIdGenerator {
break;
}
if (options.Method == 1)
{
if (options.Method == 1) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
@@ -79,7 +70,7 @@ public class DefaultIdGenerator implements IIdGenerator {
}
@Override
public long newLong() {
public long newLong() {
return _SnowWorker.nextId();
}
}

View File

@@ -1,12 +1,15 @@
package com.yitter.test;
import com.yitter.contract.IIdGenerator;
import java.util.HashSet;
import java.util.Set;
public class GenTest {
private IIdGenerator IdGen;
private int GenIdCount;
private int WorkerId;
private Set IdSet = new HashSet();
public GenTest(IIdGenerator idGen, int genIdCount, int workerId) {
GenIdCount = genIdCount;
@@ -19,6 +22,7 @@ public class GenTest {
for (int i = 0; i < GenIdCount; i++) {
long id = IdGen.newLong();
// IdSet.add(id);
}
long end = System.currentTimeMillis();

View File

@@ -31,7 +31,7 @@ public class StartUp {
// options.MaxSeqNumber = 200;
options.Method = method;
options.StartTime = 1582206693000L; // (2020-2-20)
options.BaseTime = 1582206693000L;
options.WorkerId = 1;
IIdGenerator IdGen = new DefaultIdGenerator(options);