Package org.agrona.concurrent
Class SnowflakeIdGenerator
java.lang.Object
org.agrona.concurrent.SnowflakeIdGenerator
- All Implemented Interfaces:
IdGenerator
Generate unique identifiers based on the Twitter
Snowflake algorithm.
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intNumber of bits used for the timestamp giving 69 years fromtimestampOffsetMs().static final intTotal number of bits used to represent the distributed node and the sequence within a millisecond.static final intDefault number of bits used to represent the distributed node or application which is10bits allowing for 1024 nodes (0-1023).static final intDefault number of bits used to represent the sequence within a millisecond which is12bits supporting 4,096,000 ids per-second per-node.static final intHigh order 2's compliment bit which is unused. -
Constructor Summary
ConstructorsConstructorDescriptionSnowflakeIdGenerator(int nodeIdBits, int sequenceBits, long nodeId, long timestampOffsetMs, EpochClock clock) Construct a new Snowflake id generator for a given node with a provided offset andEpochClock.SnowflakeIdGenerator(long nodeId) Construct a new Snowflake id generator for a given node with a 0 offset from 1 Jan 1970 UTC and useSystemEpochClock.INSTANCEwithNODE_ID_BITS_DEFAULTnode ID bits andSEQUENCE_BITS_DEFAULTsequence bits. -
Method Summary
Modifier and TypeMethodDescriptionlongThe max node identity value possible given the configured number of node ID bits.longThe max sequence value possible given the configured number of sequence bits.longnextId()Generate the next id in sequence.longnodeId()Node identity which scopes the id generation.longOffset in milliseconds from Epoch of 1 Jan 1970 UTC which is subtracted to give 69 years of ids.
-
Field Details
-
UNUSED_BITS
public static final int UNUSED_BITSHigh order 2's compliment bit which is unused.- See Also:
-
EPOCH_BITS
public static final int EPOCH_BITSNumber of bits used for the timestamp giving 69 years fromtimestampOffsetMs().- See Also:
-
MAX_NODE_ID_AND_SEQUENCE_BITS
public static final int MAX_NODE_ID_AND_SEQUENCE_BITSTotal number of bits used to represent the distributed node and the sequence within a millisecond.- See Also:
-
NODE_ID_BITS_DEFAULT
public static final int NODE_ID_BITS_DEFAULTDefault number of bits used to represent the distributed node or application which is10bits allowing for 1024 nodes (0-1023).- See Also:
-
SEQUENCE_BITS_DEFAULT
public static final int SEQUENCE_BITS_DEFAULTDefault number of bits used to represent the sequence within a millisecond which is12bits supporting 4,096,000 ids per-second per-node.- See Also:
-
-
Constructor Details
-
SnowflakeIdGenerator
public 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 andEpochClock.- Parameters:
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.
-
SnowflakeIdGenerator
public SnowflakeIdGenerator(long nodeId) Construct a new Snowflake id generator for a given node with a 0 offset from 1 Jan 1970 UTC and useSystemEpochClock.INSTANCEwithNODE_ID_BITS_DEFAULTnode ID bits andSEQUENCE_BITS_DEFAULTsequence bits.- Parameters:
nodeId- for the node generating ids.
-
-
Method Details
-
nodeId
public long nodeId()Node identity which scopes the id generation. This is limited tomaxNodeId().- Returns:
- the node identity which scopes the id generation.
-
timestampOffsetMs
public long timestampOffsetMs()Offset in milliseconds from Epoch of 1 Jan 1970 UTC which is subtracted to give 69 years of ids.To offset from 1 Jan 2015 UTC then 1420070400000 can be used.
- Returns:
- offset in milliseconds from Epoch of 1 Jan 1970 UTC which is subtracted to give 69 years of ids.
-
maxNodeId
public long maxNodeId()The max node identity value possible given the configured number of node ID bits.- Returns:
- max node identity value.
-
maxSequence
public long maxSequence()The max sequence value possible given the configured number of sequence bits.- Returns:
- max sequence value.
-
nextId
public long nextId()Generate the next id in sequence. IfmaxSequence()is reached within the same millisecond then this implementation will busy spin until the next millisecond usingThread.onSpinWait()and checking forThread.isInterrupted().- Specified by:
nextIdin interfaceIdGenerator- Returns:
- the next unique id for this node.
-