public final class SnowflakeIdGenerator extends Object implements IdGenerator
This implementation is lock-less resulting in greater throughput plus less contention and latency jitter.
Note: ntpd, or alternative clock source, should be setup correctly to ensure the clock does not go backwards.
Modifier and Type | Field and Description |
---|---|
static int |
EPOCH_BITS
Number of bits used for the timestamp giving 69 years from
timestampOffsetMs() . |
static int |
MAX_NODE_ID_AND_SEQUENCE_BITS
Total number of bits used to represent the distributed node and the sequence within a millisecond.
|
static int |
NODE_ID_BITS_DEFAULT
Default number of bits used to represent the distributed node or application which is
10 bits allowing
for 1024 nodes (0-1023). |
static int |
SEQUENCE_BITS_DEFAULT
Default number of bits used to represent the sequence within a millisecond which is
12 bits supporting
4,096,000 ids per-second per-node. |
static int |
UNUSED_BITS
High order 2's compliment bit which is unused.
|
Constructor and Description |
---|
SnowflakeIdGenerator(int nodeIdBits,
int sequenceBits,
long nodeId,
long timestampOffsetMs,
EpochClock clock)
Construct a new Snowflake id generator for a given node with a provided offset and
EpochClock . |
SnowflakeIdGenerator(long nodeId)
Construct a new Snowflake id generator for a given node with a 0 offset from 1 Jan 1970 UTC and use
SystemEpochClock.INSTANCE with NODE_ID_BITS_DEFAULT node ID bits and
SEQUENCE_BITS_DEFAULT sequence bits. |
Modifier and Type | Method and Description |
---|---|
long |
maxNodeId()
The max node identity value possible given the configured number of node ID bits.
|
long |
maxSequence()
The max sequence value possible given the configured number of sequence bits.
|
long |
nextId()
Generate the next id in sequence.
|
long |
nodeId()
Node identity which scopes the id generation.
|
long |
timestampOffsetMs()
Offset in milliseconds from Epoch of 1 Jan 1970 UTC which is subtracted to give 69 years of ids.
|
public static final int UNUSED_BITS
public static final int EPOCH_BITS
timestampOffsetMs()
.public static final int MAX_NODE_ID_AND_SEQUENCE_BITS
public static final int NODE_ID_BITS_DEFAULT
10
bits allowing
for 1024 nodes (0-1023).public static final int SEQUENCE_BITS_DEFAULT
12
bits supporting
4,096,000 ids per-second per-node.public SnowflakeIdGenerator(int nodeIdBits, int sequenceBits, long nodeId, long timestampOffsetMs, EpochClock clock)
EpochClock
.nodeIdBits
- number of bits used to represent the distributed node or application.sequenceBits
- number of bits used to represent the sequence within a millisecond.nodeId
- for the node generating ids.timestampOffsetMs
- to adjust the base offset from 1 Jan 1970 UTC to extend the 69-year range.clock
- to provide timestamps.public SnowflakeIdGenerator(long nodeId)
SystemEpochClock.INSTANCE
with NODE_ID_BITS_DEFAULT
node ID bits and
SEQUENCE_BITS_DEFAULT
sequence bits.nodeId
- for the node generating ids.public long nodeId()
maxNodeId()
.public long timestampOffsetMs()
To offset from 1 Jan 2015 UTC then 1420070400000 can be used.
public long maxNodeId()
public long maxSequence()
public long nextId()
maxSequence()
is reached within the same millisecond then this
implementation will busy spin until the next millisecond using ThreadHints.onSpinWait()
and checking
for Thread.isInterrupted()
.nextId
in interface IdGenerator
Copyright © 2014-2022 Real Logic Limited. All Rights Reserved.