forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdGenerator.java
More file actions
396 lines (345 loc) · 13.3 KB
/
IdGenerator.java
File metadata and controls
396 lines (345 loc) · 13.3 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package act.util;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import org.joda.time.DateTime;
import org.osgl.$;
import org.osgl.util.E;
import org.osgl.util.IO;
import org.osgl.util.S;
import java.io.File;
import java.util.concurrent.atomic.AtomicLong;
/**
* Generate unique ID in a cluster
*/
public class IdGenerator {
/**
* Implementation of {@code StartIdProvider} shall return a
* unique id per each system start
*/
public interface StartIdProvider {
/**
* Returns the system start ID. The start ID shall be different
* between System starts, but it shall remaining the same value
* within one system life time
*/
long startId();
/**
* Generate system start ID based on timestamp
*/
class Timestamp implements StartIdProvider {
private final long id;
public Timestamp() {
long origin = DateTime.parse("2016-05-10").getMillis();
// let's assume the system cannot be restart within 10 seconds
long l = ($.ms() - origin) / 1000 / 10;
id = l;
}
@Override
public long startId() {
return id;
}
}
/**
* Generate system start ID based on incremental sequence. The newly generated ID
* will be write to a File
*/
class FileBasedStartCounter implements StartIdProvider {
private final long id;
public FileBasedStartCounter() {
this(".act.id-global");
}
public FileBasedStartCounter(String path) {
File file = new File(path);
if (file.exists()) {
String s = IO.readContentAsString(file);
long seq = Long.parseLong(s);
seq = seq + 1;
IO.write(S.str(seq), file);
id = (seq);
} else {
id = 0;
IO.write(Long.toString(id), file);
}
}
@Override
public long startId() {
return id;
}
}
/**
* Default start ID provider will try to use the {@link act.util.IdGenerator.StartIdProvider.FileBasedStartCounter}. In case
* File IO is not allowed (e.g. in GAE), then it will use {@link act.util.IdGenerator.StartIdProvider.Timestamp}
*/
class DefaultStartIdProvider implements StartIdProvider {
private StartIdProvider delegate;
public DefaultStartIdProvider() {
this(".act.id-global");
}
public DefaultStartIdProvider(String path) {
try {
delegate = new FileBasedStartCounter(path);
delegate.startId();
} catch (Exception e) {
delegate = new Timestamp();
}
}
@Override
public long startId() {
return delegate.startId();
}
}
}
/**
* {@code SequenceProvider} shall generate unique ID within
* one JVM per each call
*/
public interface SequenceProvider {
long seqId();
class AtomicLongSeq implements SequenceProvider {
private final AtomicLong seq = new AtomicLong(0);
@Override
public long seqId() {
return (seq.incrementAndGet());
}
}
}
public interface NodeIdProvider {
long nodeId();
class IpProvider implements NodeIdProvider {
private enum EffectiveBytes {
ONE(1), TWO(2), THREE(3), FOUR(4);
private int value;
EffectiveBytes(int value) {
this.value = value;
}
public static EffectiveBytes valueOf(int n) {
switch (n) {
case 1: return ONE;
case 2: return TWO;
case 3: return THREE;
case 4: return FOUR;
default :
throw E.unexpected("Invalid EffectiveByte value: %s. Expected: 1 - 4", n);
}
}
}
private final EffectiveBytes effectiveBytes;
private final long id;
public IpProvider() {
this(4);
}
public IpProvider(int effectBytes) {
this.effectiveBytes = EffectiveBytes.valueOf(effectBytes);
String ip = LocalIpAddressUtil.ip();
String[] sa = ip.split("\\.");
int n = effectiveBytes.value;
long l = 0;
for (int i = 0; i < n; ++i) {
String b = sa[3 - i];
long factor = 1;
for (int j = 0; j < i; ++j) {
factor = factor * 256;
}
l += Long.valueOf(b) * factor;
}
id = (l);
}
public long nodeId() {
return id;
}
}
}
public interface LongEncoder {
String longToStr(long l);
abstract class LongEncoderBase implements LongEncoder {
private final char[] digits;
private final int MAX_RADIX;
public LongEncoderBase(char[] digits) {
this.digits = digits;
this.MAX_RADIX = digits.length;
}
/**
* Code copied from JDK Long.toString(long, String)
*/
public String longToStr(long l) {
int radix = MAX_RADIX;
char[] buf = new char[65];
int charPos = 64;
boolean negative = (l < 0);
if (!negative) {
l = -l;
}
while (l <= -radix) {
buf[charPos--] = digits[(int)(-(l % radix))];
l = l / radix;
}
buf[charPos] = digits[(int)(-l)];
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (65 - charPos));
}
}
}
public static class UnsafeLongEncoder extends LongEncoder.LongEncoderBase {
/**
* Extended char table for representing a number as a String
*/
private final static char[] digits = {
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', '!', '$', '%', '&',
'.', ',', ';', ':', '=', '?',
'+', '-', '*', '/', '<', '>',
'_', '~', '#', '^', '@', '|',
'(', ')', '[', ']', '{', '}'
};
public UnsafeLongEncoder() {
super(digits);
}
}
public static class SafeLongEncoder extends LongEncoder.LongEncoderBase {
/**
* Extended char table for representing a number as a String
*/
private final static char[] digits = {
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', '.', '-', '_', '~',
};
public SafeLongEncoder() {
super(digits);
}
}
public static final LongEncoder SAFE_ENCODER = new SafeLongEncoder();
public static final LongEncoder UNSAFE_ENCODER = new UnsafeLongEncoder();
private final NodeIdProvider nodeIdProvider;
private final StartIdProvider startIdProvider;
private final SequenceProvider sequenceProvider;
private LongEncoder longEncoder;
/**
* Create a default IdGenerator with following configuration:
* <ul>
* <li>Node ID provider: four byte IP address</li>
* <li>Start ID provider: stored in <code>.act.id-global</code> file</li>
* <li>Sequence ID provider: Atomic Long sequence</li>
* <li>Long Encoder: {@link SafeLongEncoder}</li>
* </ul>
*/
public IdGenerator() {
this(new NodeIdProvider.IpProvider(), new StartIdProvider.DefaultStartIdProvider(),
new SequenceProvider.AtomicLongSeq(), SAFE_ENCODER);
}
/**
* Create a default IdGenerator with following configuration:
* <ul>
* <li>Node ID provider: four byte IP address</li>
* <li>Start ID provider: stored in <code>.act.id-global</code> file</li>
* <li>Sequence ID provider: Atomic Long sequence</li>
* <li>
* Long Encoder: {@link UnsafeLongEncoder} when `useUnsafeLongEncoder` is set to
* `true` or {@link SafeLongEncoder} otherwise
* </li>
* </ul>
* @param useUnsafeLongEncoder indicate use safe or unsafe long encoder
*/
public IdGenerator(boolean useUnsafeLongEncoder) {
this(new NodeIdProvider.IpProvider(), new StartIdProvider.DefaultStartIdProvider(),
new SequenceProvider.AtomicLongSeq(), useUnsafeLongEncoder ? UNSAFE_ENCODER : SAFE_ENCODER);
}
/**
* Create a default IdGenerator with specified node id provider, start id provider and sequence provider:
*/
public IdGenerator(NodeIdProvider nodeIdProvider, StartIdProvider startIdProvider, SequenceProvider sequenceProvider, LongEncoder longEncoder) {
this.nodeIdProvider = $.requireNotNull(nodeIdProvider);
this.startIdProvider = $.requireNotNull(startIdProvider);
this.sequenceProvider = $.requireNotNull(sequenceProvider);
this.longEncoder = $.requireNotNull(longEncoder);
}
/**
* Create a default IdGenerator with following configuration:
* <ul>
* <li>Node ID provider: N byte IP address, where N is specified by effectiveIpBytes argument</li>
* <li>Start ID provider: stored in <code>.act.id-global</code> file</li>
* <li>Sequnce ID provider: Atomic Long sequence</li>
* </ul>
*/
public IdGenerator(int effectiveIpBytes) {
this.nodeIdProvider = new NodeIdProvider.IpProvider(effectiveIpBytes);
this.startIdProvider = new StartIdProvider.DefaultStartIdProvider();
this.sequenceProvider = new SequenceProvider.AtomicLongSeq();
this.longEncoder = SAFE_ENCODER;
}
/**
* Create a default IdGenerator with following configuration:
* <ul>
* <li>Node ID provider: N byte IP address, where N is specified by effectiveIpBytes argument</li>
* <li>Start ID provider: use start ID file specified by startIdFile argument</li>
* <li>Sequnce ID provider: Atomic Long sequence</li>
* </ul>
*/
public IdGenerator(int effectiveIpBytes, String startIdFile) {
this.nodeIdProvider = new NodeIdProvider.IpProvider(effectiveIpBytes);
this.startIdProvider = new StartIdProvider.DefaultStartIdProvider(startIdFile);
this.sequenceProvider = new SequenceProvider.AtomicLongSeq();
this.longEncoder = SAFE_ENCODER;
}
/**
* Create a default IdGenerator with following configuration:
* <ul>
* <li>Node ID provider: 4 byte IP address</li>
* <li>Start ID provider: use start ID file specified by startIdFile argument</li>
* <li>Sequnce ID provider: Atomic Long sequence</li>
* </ul>
*/
public IdGenerator(String startIdFile) {
this.nodeIdProvider = new NodeIdProvider.IpProvider();
this.startIdProvider = new StartIdProvider.DefaultStartIdProvider(startIdFile);
this.sequenceProvider = new SequenceProvider.AtomicLongSeq();
this.longEncoder = SAFE_ENCODER;
}
/**
* Generate a unique ID across the cluster
* @return generated ID
*/
public String genId() {
S.Buffer sb = S.newBuffer();
sb.a(longEncoder.longToStr(nodeIdProvider.nodeId()))
.a(longEncoder.longToStr(startIdProvider.startId()))
.a(longEncoder.longToStr(sequenceProvider.seqId()));
return sb.toString();
}
}