@@ -140,7 +140,7 @@ namespace Yitter.OrgSystem.TestA | |||||
WorkerId = 1, | WorkerId = 1, | ||||
WorkerIdBitLength = 6, | WorkerIdBitLength = 6, | ||||
SeqBitLength = 6, | |||||
SeqBitLength = 12, | |||||
//TopOverCostCount = 2000, | //TopOverCostCount = 2000, | ||||
//DataCenterIdBitLength = 0, | //DataCenterIdBitLength = 0, | ||||
@@ -153,7 +153,7 @@ namespace Yitter.OrgSystem.TestA | |||||
//IdGen = new DefaultIdGenerator(options); | //IdGen = new DefaultIdGenerator(options); | ||||
YitIdHelper.SetIdGenerator(options); | YitIdHelper.SetIdGenerator(options); | ||||
genIdCount = 50000; | |||||
genIdCount = 5000000; | |||||
while (true) | while (true) | ||||
{ | { | ||||
DateTime start = DateTime.Now; | DateTime start = DateTime.Now; | ||||
@@ -186,21 +186,21 @@ namespace Yitter.OrgSystem.TestA | |||||
if (outputLog) | if (outputLog) | ||||
{ | { | ||||
IdGen.GenIdActionAsync = (arg => | |||||
{ | |||||
if (arg.ActionType == 1) | |||||
{ | |||||
Console.WriteLine($">>>> {arg.WorkerId}:开始:{DateTime.Now.ToString("mm:ss:fff")}, 周期次序:{arg.TermIndex}"); | |||||
} | |||||
else if (arg.ActionType == 2) | |||||
{ | |||||
Console.WriteLine($"<<<< {arg.WorkerId}:结束:{DateTime.Now.ToString("mm:ss:fff")},漂移 {arg.OverCostCountInOneTerm} 次,产生 {arg.GenCountInOneTerm} 个, 周期次序:{arg.TermIndex}"); | |||||
} | |||||
if (arg.ActionType == 8) | |||||
{ | |||||
Console.WriteLine($"---- {arg.WorkerId}:AA结束:{DateTime.Now.ToString("mm:ss:fff")},时间回拨"); | |||||
} | |||||
}); | |||||
//IdGen.GenIdActionAsync = (arg => | |||||
//{ | |||||
// if (arg.ActionType == 1) | |||||
// { | |||||
// Console.WriteLine($">>>> {arg.WorkerId}:开始:{DateTime.Now.ToString("mm:ss:fff")}, 周期次序:{arg.TermIndex}"); | |||||
// } | |||||
// else if (arg.ActionType == 2) | |||||
// { | |||||
// Console.WriteLine($"<<<< {arg.WorkerId}:结束:{DateTime.Now.ToString("mm:ss:fff")},漂移 {arg.OverCostCountInOneTerm} 次,产生 {arg.GenCountInOneTerm} 个, 周期次序:{arg.TermIndex}"); | |||||
// } | |||||
// if (arg.ActionType == 8) | |||||
// { | |||||
// Console.WriteLine($"---- {arg.WorkerId}:AA结束:{DateTime.Now.ToString("mm:ss:fff")},时间回拨"); | |||||
// } | |||||
//}); | |||||
} | } | ||||
for (int i = 1; i < workerCount + 1; i++) | for (int i = 1; i < workerCount + 1; i++) | ||||
@@ -233,10 +233,10 @@ namespace Yitter.OrgSystem.TestA | |||||
if (outputLog) | if (outputLog) | ||||
{ | { | ||||
idGen2.GenIdActionAsync = (arg => | |||||
{ | |||||
Console.WriteLine($"{DateTime.Now.ToString("mm:ss:fff")} {arg.WorkerId} 漂移了 {arg.OverCostCountInOneTerm}, 顺序:{arg.TermIndex}"); | |||||
}); | |||||
//idGen2.GenIdActionAsync = (arg => | |||||
//{ | |||||
// Console.WriteLine($"{DateTime.Now.ToString("mm:ss:fff")} {arg.WorkerId} 漂移了 {arg.OverCostCountInOneTerm}, 顺序:{arg.TermIndex}"); | |||||
//}); | |||||
} | } | ||||
testList.Add(test); | testList.Add(test); | ||||
@@ -16,7 +16,7 @@ namespace Yitter.IdGenerator | |||||
/// <summary> | /// <summary> | ||||
/// 生成过程中产生的事件 | /// 生成过程中产生的事件 | ||||
/// </summary> | /// </summary> | ||||
Action<OverCostActionArg> GenIdActionAsync { get; set; } | |||||
//Action<OverCostActionArg> GenIdActionAsync { get; set; } | |||||
/// <summary> | /// <summary> | ||||
/// 生成新的long型Id | /// 生成新的long型Id | ||||
@@ -15,7 +15,7 @@ namespace Yitter.IdGenerator | |||||
{ | { | ||||
internal interface ISnowWorker | internal interface ISnowWorker | ||||
{ | { | ||||
Action<OverCostActionArg> GenAction { get; set; } | |||||
//Action<OverCostActionArg> GenAction { get; set; } | |||||
long NextId(); | long NextId(); | ||||
} | } | ||||
@@ -327,7 +327,7 @@ namespace Yitter.IdGenerator | |||||
return CalcId(_LastTimeTick); | return CalcId(_LastTimeTick); | ||||
} | } | ||||
protected virtual long CalcId(in long useTimeTick) | |||||
protected virtual long CalcId(long useTimeTick) | |||||
{ | { | ||||
var result = ((useTimeTick << _TimestampShift) + | var result = ((useTimeTick << _TimestampShift) + | ||||
((long)WorkerId << SeqBitLength) + | ((long)WorkerId << SeqBitLength) + | ||||
@@ -337,7 +337,7 @@ namespace Yitter.IdGenerator | |||||
return result; | return result; | ||||
} | } | ||||
protected virtual long CalcTurnBackId(in long useTimeTick) | |||||
protected virtual long CalcTurnBackId(long useTimeTick) | |||||
{ | { | ||||
var result = ((useTimeTick << _TimestampShift) + | var result = ((useTimeTick << _TimestampShift) + | ||||
((long)WorkerId << SeqBitLength) + _TurnBackIndex); | ((long)WorkerId << SeqBitLength) + _TurnBackIndex); | ||||
@@ -50,7 +50,7 @@ namespace Yitter.IdGenerator | |||||
_TimestampShift = (byte)(DataCenterIdBitLength + WorkerIdBitLength + SeqBitLength); | _TimestampShift = (byte)(DataCenterIdBitLength + WorkerIdBitLength + SeqBitLength); | ||||
} | } | ||||
protected override long CalcId(in long useTimeTick) | |||||
protected override long CalcId(long useTimeTick) | |||||
{ | { | ||||
var result = ((useTimeTick << _TimestampShift) + | var result = ((useTimeTick << _TimestampShift) + | ||||
((long)DataCenterId << DataCenterIdBitLength) + | ((long)DataCenterId << DataCenterIdBitLength) + | ||||
@@ -61,7 +61,7 @@ namespace Yitter.IdGenerator | |||||
return result; | return result; | ||||
} | } | ||||
protected override long CalcTurnBackId(in long useTimeTick) | |||||
protected override long CalcTurnBackId(long useTimeTick) | |||||
{ | { | ||||
var result = ((useTimeTick << _TimestampShift) + | var result = ((useTimeTick << _TimestampShift) + | ||||
((long)DataCenterId << DataCenterIdBitLength) + | ((long)DataCenterId << DataCenterIdBitLength) + | ||||
@@ -21,35 +21,35 @@ namespace Yitter.IdGenerator | |||||
{ | { | ||||
private ISnowWorker _SnowWorker { get; set; } | private ISnowWorker _SnowWorker { get; set; } | ||||
public Action<OverCostActionArg> GenIdActionAsync | |||||
{ | |||||
get => _SnowWorker.GenAction; | |||||
set => _SnowWorker.GenAction = value; | |||||
} | |||||
//public Action<OverCostActionArg> GenIdActionAsync | |||||
//{ | |||||
// get => _SnowWorker.GenAction; | |||||
// set => _SnowWorker.GenAction = value; | |||||
//} | |||||
public DefaultIdGenerator(IdGeneratorOptions options) | public DefaultIdGenerator(IdGeneratorOptions options) | ||||
{ | { | ||||
if (options == null) | if (options == null) | ||||
{ | { | ||||
throw new ApplicationException("options error."); | |||||
throw new ArgumentException("options error."); | |||||
} | } | ||||
// 1.BaseTime | // 1.BaseTime | ||||
if (options.BaseTime < DateTime.Now.AddYears(-50) || options.BaseTime > DateTime.Now) | if (options.BaseTime < DateTime.Now.AddYears(-50) || options.BaseTime > DateTime.Now) | ||||
{ | { | ||||
throw new ApplicationException("BaseTime error."); | |||||
throw new ArgumentException("BaseTime error."); | |||||
} | } | ||||
// 2.WorkerIdBitLength | // 2.WorkerIdBitLength | ||||
int maxLength = options.TimestampType == 0 ? 22 : 31; // (秒级时间戳时放大到31位) | int maxLength = options.TimestampType == 0 ? 22 : 31; // (秒级时间戳时放大到31位) | ||||
if (options.WorkerIdBitLength <= 0) | if (options.WorkerIdBitLength <= 0) | ||||
{ | { | ||||
throw new ApplicationException("WorkerIdBitLength error.(range:[1, 21])"); | |||||
throw new ArgumentException("WorkerIdBitLength error.(range:[1, 21])"); | |||||
} | } | ||||
if (options.DataCenterIdBitLength + options.WorkerIdBitLength + options.SeqBitLength > maxLength) | if (options.DataCenterIdBitLength + options.WorkerIdBitLength + options.SeqBitLength > maxLength) | ||||
{ | { | ||||
throw new ApplicationException("error:DataCenterIdBitLength + WorkerIdBitLength + SeqBitLength <= " + maxLength); | |||||
throw new ArgumentException("error:DataCenterIdBitLength + WorkerIdBitLength + SeqBitLength <= " + maxLength); | |||||
} | } | ||||
// 3.WorkerId & DataCenterId | // 3.WorkerId & DataCenterId | ||||
@@ -60,19 +60,19 @@ namespace Yitter.IdGenerator | |||||
} | } | ||||
if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) | if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) | ||||
{ | { | ||||
throw new ApplicationException("WorkerId error. (range:[0, " + maxWorkerIdNumber + "]"); | |||||
throw new ArgumentException("WorkerId error. (range:[0, " + maxWorkerIdNumber + "]"); | |||||
} | } | ||||
var maxDataCenterIdNumber = (1 << options.DataCenterIdBitLength) - 1; | var maxDataCenterIdNumber = (1 << options.DataCenterIdBitLength) - 1; | ||||
if (options.DataCenterId < 0 || options.DataCenterId > maxDataCenterIdNumber) | if (options.DataCenterId < 0 || options.DataCenterId > maxDataCenterIdNumber) | ||||
{ | { | ||||
throw new ApplicationException("DataCenterId error. (range:[0, " + maxDataCenterIdNumber + "]"); | |||||
throw new ArgumentException("DataCenterId error. (range:[0, " + maxDataCenterIdNumber + "]"); | |||||
} | } | ||||
// 4.SeqBitLength | // 4.SeqBitLength | ||||
if (options.SeqBitLength < 2 || options.SeqBitLength > 21) | if (options.SeqBitLength < 2 || options.SeqBitLength > 21) | ||||
{ | { | ||||
throw new ApplicationException("SeqBitLength error. (range:[2, 21])"); | |||||
throw new ArgumentException("SeqBitLength error. (range:[2, 21])"); | |||||
} | } | ||||
// 5.MaxSeqNumber | // 5.MaxSeqNumber | ||||
@@ -83,19 +83,19 @@ namespace Yitter.IdGenerator | |||||
} | } | ||||
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) | if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) | ||||
{ | { | ||||
throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]"); | |||||
throw new ArgumentException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]"); | |||||
} | } | ||||
// 6.MinSeqNumber | // 6.MinSeqNumber | ||||
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber) | if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber) | ||||
{ | { | ||||
throw new ApplicationException("MinSeqNumber error. (range:[5, " + maxSeqNumber + "]"); | |||||
throw new ArgumentException("MinSeqNumber error. (range:[5, " + maxSeqNumber + "]"); | |||||
} | } | ||||
// 7.TopOverCostCount | // 7.TopOverCostCount | ||||
if (options.TopOverCostCount < 0 || options.TopOverCostCount > 10000) | if (options.TopOverCostCount < 0 || options.TopOverCostCount > 10000) | ||||
{ | { | ||||
throw new ApplicationException("TopOverCostCount error. (range:[0, 10000]"); | |||||
throw new ArgumentException("TopOverCostCount error. (range:[0, 10000]"); | |||||
} | } | ||||
switch (options.Method) | switch (options.Method) | ||||
@@ -51,7 +51,7 @@ namespace Yitter.IdGenerator | |||||
// } | // } | ||||
//} | //} | ||||
if (_IdGenInstance == null) throw new ApplicationException("Please initialize Yitter.IdGeneratorOptions first."); | |||||
if (_IdGenInstance == null) throw new ArgumentException("Please initialize Yitter.IdGeneratorOptions first."); | |||||
return _IdGenInstance.NewLong(); | return _IdGenInstance.NewLong(); | ||||
} | } | ||||