Package org.agrona.concurrent
Class SnowflakeIdGenerator
- java.lang.Object
-
- org.agrona.concurrent.AbstractSnowflakeIdGeneratorPaddingLhs
-
- org.agrona.concurrent.AbstractSnowflakeIdGeneratorValue
-
- org.agrona.concurrent.AbstractSnowflakeIdGeneratorPaddingRhs
-
- org.agrona.concurrent.SnowflakeIdGenerator
-
- All Implemented Interfaces:
IdGenerator
public final class SnowflakeIdGenerator extends AbstractSnowflakeIdGeneratorPaddingRhs implements 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
Fields Modifier and Type Field Description private EpochClock
clock
static int
EPOCH_BITS
Number of bits used for the timestamp giving 69 years fromtimestampOffsetMs()
.static int
MAX_NODE_ID_AND_SEQUENCE_BITS
Total number of bits used to represent the distributed node and the sequence within a millisecond.private long
maxNodeId
private long
maxSequence
static int
NODE_ID_BITS_DEFAULT
Default number of bits used to represent the distributed node or application which is10
bits allowing for 1024 nodes (0-1023).private long
nodeBits
private int
nodeIdAndSequenceBits
static int
SEQUENCE_BITS_DEFAULT
Default number of bits used to represent the sequence within a millisecond which is12
bits supporting 4,096,000 ids per-second per-node.private int
sequenceBits
private long
timestampOffsetMs
static int
UNUSED_BITS
High order 2's compliment bit which is unused.-
Fields inherited from class org.agrona.concurrent.AbstractSnowflakeIdGeneratorPaddingRhs
p064, p065, p066, p067, p068, p069, p070, p071, p072, p073, p074, p075, p076, p077, p078, p079, p080, p081, p082, p083, p084, p085, p086, p087, p088, p089, p090, p091, p092, p093, p094, p095, p096, p097, p098, p099, p100, p101, p102, p103, p104, p105, p106, p107, p108, p109, p110, p111, p112, p113, p114, p115, p116, p117, p118, p119, p120, p121, p122, p123, p124, p125, p126, p127
-
Fields inherited from class org.agrona.concurrent.AbstractSnowflakeIdGeneratorValue
TIMESTAMP_SEQUENCE_UPDATER, timestampSequence
-
Fields inherited from class org.agrona.concurrent.AbstractSnowflakeIdGeneratorPaddingLhs
p000, p001, p002, p003, p004, p005, p006, p007, p008, p009, p010, p011, p012, p013, p014, p015, p016, p017, p018, p019, p020, p021, p022, p023, p024, p025, p026, p027, p028, p029, p030, p031, p032, p033, p034, p035, p036, p037, p038, p039, p040, p041, p042, p043, p044, p045, p046, p047, p048, p049, p050, p051, p052, p053, p054, p055, p056, p057, p058, p059, p060, p061, p062, p063
-
-
Constructor Summary
Constructors Constructor 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 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.INSTANCE
withNODE_ID_BITS_DEFAULT
node ID bits andSEQUENCE_BITS_DEFAULT
sequence bits.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) long
extractNodeId(long id)
(package private) long
extractSequence(long id)
(package private) long
extractTimestamp(long id)
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.
-
-
-
Field Detail
-
UNUSED_BITS
public static final int UNUSED_BITS
High order 2's compliment bit which is unused.- See Also:
- Constant Field Values
-
EPOCH_BITS
public static final int EPOCH_BITS
Number of bits used for the timestamp giving 69 years fromtimestampOffsetMs()
.- See Also:
- Constant Field Values
-
MAX_NODE_ID_AND_SEQUENCE_BITS
public static final int MAX_NODE_ID_AND_SEQUENCE_BITS
Total number of bits used to represent the distributed node and the sequence within a millisecond.- See Also:
- Constant Field Values
-
NODE_ID_BITS_DEFAULT
public static final int NODE_ID_BITS_DEFAULT
Default number of bits used to represent the distributed node or application which is10
bits allowing for 1024 nodes (0-1023).- See Also:
- Constant Field Values
-
SEQUENCE_BITS_DEFAULT
public static final int SEQUENCE_BITS_DEFAULT
Default number of bits used to represent the sequence within a millisecond which is12
bits supporting 4,096,000 ids per-second per-node.- See Also:
- Constant Field Values
-
nodeIdAndSequenceBits
private final int nodeIdAndSequenceBits
-
sequenceBits
private final int sequenceBits
-
maxNodeId
private final long maxNodeId
-
maxSequence
private final long maxSequence
-
nodeBits
private final long nodeBits
-
timestampOffsetMs
private final long timestampOffsetMs
-
clock
private final EpochClock clock
-
-
Constructor Detail
-
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.INSTANCE
withNODE_ID_BITS_DEFAULT
node ID bits andSEQUENCE_BITS_DEFAULT
sequence bits.- Parameters:
nodeId
- for the node generating ids.
-
-
Method Detail
-
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:
nextId
in interfaceIdGenerator
- Returns:
- the next unique id for this node.
-
extractTimestamp
long extractTimestamp(long id)
-
extractNodeId
long extractNodeId(long id)
-
extractSequence
long extractSequence(long id)
-
-