-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedUidGenerator.cs
More file actions
109 lines (91 loc) · 3.67 KB
/
CachedUidGenerator.cs
File metadata and controls
109 lines (91 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Vincent.UidGenerator.Core.Buffer;
using Vincent.UidGenerator.Exception;
using Vincent.UidGenerator.Worker;
namespace Vincent.UidGenerator.Core;
/// <summary>
/// Represents a cached implementation of <see cref="IUidGenerator"/> extends from <see cref="DefaultUidGenerator"/>, based on a lock free <see cref="RingBuffer"/>
/// </summary>
public class CachedUidGenerator : DefaultUidGenerator, IDisposable
{
private readonly RingBuffer _ringBuffer;
private readonly BufferPaddingExecutor _bufferPaddingExecutor;
private readonly int _paddingThreshold;
public CachedUidGenerator(IOptions<CachedUidGeneratorOptions> options, ILogger<CachedUidGenerator> logger,
IWorkerIdAssigner workerIdAssigner)
: this(options.Value, workerIdAssigner)
{
Logger = logger;
}
public CachedUidGenerator(CachedUidGeneratorOptions options, IWorkerIdAssigner workerIdAssigner) : base(options,
workerIdAssigner)
{
if (options.RejectedPutBufferHandler == null)
{
throw new ArgumentNullException("RejectedPutBufferHandler can not be null. you can use default value");
}
if (options.RejectedTakeBufferHandler == null)
{
throw new ArgumentNullException("RejectedTakeBufferHandler can not be null. you can use default value");
}
if (options.BoostPower <= 0)
{
throw new ArgumentException("Boost power must be positive!");
}
// initialize RingBuffer
int bufferSize = ((int) BitsAllocator.MaxSequence + 1) << options.BoostPower;
_paddingThreshold = bufferSize * options.PaddingFactor / 100;
_ringBuffer = new RingBuffer(bufferSize, options.PaddingFactor, options.RejectedPutBufferHandler,
options.RejectedTakeBufferHandler);
_bufferPaddingExecutor = new BufferPaddingExecutor(_ringBuffer, nextIdsForOneSecond, options.UseScheduler,
options.ScheduleInterval);
// fill in all slots of the RingBuffer
_bufferPaddingExecutor.PaddingBuffer();
// start buffer padding threads
_bufferPaddingExecutor.Start();
}
public override long GetUid()
{
try
{
if (_ringBuffer.Count < _paddingThreshold)
{
#if DEBUG
Logger.LogInformation(
$"Reach the padding threshold:{_paddingThreshold}. ringBuffer count: {_ringBuffer.Count}");
#endif
_bufferPaddingExecutor.PaddingBufferAsync();
}
return _ringBuffer.Take();
}
catch (System.Exception e)
{
throw new UidGenerateException("Generate unique id exception. ", e);
}
}
public void Dispose()
{
_bufferPaddingExecutor.Shutdown();
}
/// <summary>
/// Get the UIDs in the same specified second under the max sequence
/// </summary>
/// <param name="currentSecond"></param>
/// <returns>UID list, size of <see cref="BitsAllocator.MaxSequence"/></returns>
public List<long> nextIdsForOneSecond(long currentSecond)
{
// Initialize result list size of (max sequence + 1)
int listSize = (int) BitsAllocator.MaxSequence + 1;
List<long> uIds = new List<long>(listSize);
// Allocate the first sequence of the second, the others can be calculated with the offset
long firstSeqUid = BitsAllocator.Allocate(currentSecond - StartSeconds, WorkerId, 0L);
for (int offset = 0; offset < listSize; offset++)
{
uIds.Add(firstSeqUid + offset);
}
return uIds;
}
}