Class FlakeIdGeneratorConfig
- All Implemented Interfaces:
NamedConfig
,DataSerializable
,IdentifiedDataSerializable
FlakeIdGeneratorConfig
contains the configuration for the member
regarding Flake ID Generator
.- Since:
- 3.10
-
Field Summary
Modifier and TypeFieldDescriptionstatic final long
Default value forgetAllowedFutureMillis()
.static final int
Default value forgetBitsNodeId()
.static final int
Default value forgetBitsSequence()
.static final long
Default value forgetEpochStart()
.static final int
Default value forgetPrefetchCount()
.static final long
Default value forgetPrefetchValidityMillis()
.static final int
Maximum value for prefetch count. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
long
int
int
int
Returns type identifier for this class.long
int
Returns DataSerializableFactory factory ID for this class.getName()
Returns the configuration name.long
int
long
int
hashCode()
boolean
void
Reads fields from the input streamsetAllowedFutureMillis
(long allowedFutureMillis) Sets how far to the future is the generator allowed to go to generate IDs without blocking.setBitsNodeId
(int bitsNodeId) Sets the bit-length of node id component.setBitsSequence
(int bitsSequence) Sets the bit-length of the sequence component.setEpochStart
(long epochStart) Sets the offset of timestamp component in milliseconds.Sets the name or name pattern for this config.setNodeIdOffset
(long nodeIdOffset) Sets the offset that will be added to the node ID assigned to cluster member for this generator.setPrefetchCount
(int prefetchCount) Sets how many IDs are pre-fetched on the background when one call toFlakeIdGenerator.newId()
is made.setPrefetchValidityMillis
(long prefetchValidityMs) Sets for how long the pre-fetched IDs can be used.setStatisticsEnabled
(boolean statisticsEnabled) Enables or disables statistics gathering ofLocalFlakeIdGeneratorStats
.toString()
void
Writes object fields to output stream
-
Field Details
-
DEFAULT_PREFETCH_COUNT
public static final int DEFAULT_PREFETCH_COUNTDefault value forgetPrefetchCount()
.- See Also:
-
DEFAULT_PREFETCH_VALIDITY_MILLIS
public static final long DEFAULT_PREFETCH_VALIDITY_MILLISDefault value forgetPrefetchValidityMillis()
.- See Also:
-
DEFAULT_EPOCH_START
public static final long DEFAULT_EPOCH_STARTDefault value forgetEpochStart()
.1514764800000
is the valueSystem.currentTimeMillis()
would return on 1.1.2018 0:00 UTC.- See Also:
-
DEFAULT_BITS_SEQUENCE
public static final int DEFAULT_BITS_SEQUENCEDefault value forgetBitsSequence()
.- See Also:
-
DEFAULT_BITS_NODE_ID
public static final int DEFAULT_BITS_NODE_IDDefault value forgetBitsNodeId()
.- See Also:
-
DEFAULT_ALLOWED_FUTURE_MILLIS
public static final long DEFAULT_ALLOWED_FUTURE_MILLISDefault value forgetAllowedFutureMillis()
.- See Also:
-
MAXIMUM_PREFETCH_COUNT
public static final int MAXIMUM_PREFETCH_COUNTMaximum value for prefetch count. The limit is ~10% of the default time we allow the IDs to be from the future (seeDEFAULT_ALLOWED_FUTURE_MILLIS
).The reason to limit the prefetch count is that a single call to
FlakeIdGenerator.newId()
might be blocked if the future allowance is exceeded: we want to avoid a single call for a large batch to block another call for a small batch.- See Also:
-
-
Constructor Details
-
FlakeIdGeneratorConfig
public FlakeIdGeneratorConfig() -
FlakeIdGeneratorConfig
-
FlakeIdGeneratorConfig
Copy-constructor
-
-
Method Details
-
getName
Returns the configuration name. This can be actual object name or pattern.- Specified by:
getName
in interfaceNamedConfig
-
setName
Sets the name or name pattern for this config. Must not be modified after this instance is added toConfig
.- Specified by:
setName
in interfaceNamedConfig
-
getPrefetchCount
public int getPrefetchCount()- See Also:
-
setPrefetchCount
Sets how many IDs are pre-fetched on the background when one call toFlakeIdGenerator.newId()
is made. Default is 100.This setting pertains only to
newId()
calls made on the member that configured it.- Parameters:
prefetchCount
- the desired prefetch count, in the range 1..100000.- Returns:
- this instance for fluent API
-
getPrefetchValidityMillis
public long getPrefetchValidityMillis()- See Also:
-
setPrefetchValidityMillis
Sets for how long the pre-fetched IDs can be used. If this time elapses, a new batch of IDs will be fetched. Time unit is milliseconds, default is 600,000 (10 minutes).The IDs contain timestamp component, which ensures rough global ordering of IDs. If an ID is assigned to an object that was created much later, it will be much out of order. If you don't care about ordering, set this value to 0.
This setting pertains only to
newId
calls made on the member that configured it.- Parameters:
prefetchValidityMs
- the desired ID validity or unlimited, if <=0- Returns:
- this instance for fluent API
-
setEpochStart
Sets the offset of timestamp component in milliseconds. By default it's 1514764800000L, that is the beginning of 2018. You can adjust the value to determine the lifespan of the generator. SeeFlakeIdGenerator
's class javadoc for more information.Note: If you set the epoch start to a future instant, negative IDs will be generated until that time occurs.
- Parameters:
epochStart
- the desired epoch start- Returns:
- this instance for fluent API
- Since:
- 4.0
-
getEpochStart
public long getEpochStart()- See Also:
-
getNodeIdOffset
public long getNodeIdOffset()- See Also:
-
setNodeIdOffset
Sets the offset that will be added to the node ID assigned to cluster member for this generator. Might be useful in A/B deployment scenarios where you have cluster A which you want to upgrade. You create cluster B and for some time both will generate IDs and you want to have them unique. In this case, configure node ID offset for generators on cluster B.- Parameters:
nodeIdOffset
- the value added to the node id- Returns:
- this instance for fluent API
- See Also:
-
getBitsSequence
public int getBitsSequence()- See Also:
-
setBitsSequence
Sets the bit-length of the sequence component. This setting determines the maximum rate at which IDs can be generated. SeeFlakeIdGenerator
's class javadoc for more information.- Parameters:
bitsSequence
- sequence component bit-length- Returns:
- this instance for fluent API
- Since:
- 4.0
-
getBitsNodeId
public int getBitsNodeId()- See Also:
-
setBitsNodeId
Sets the bit-length of node id component. SeeFlakeIdGenerator
's class javadoc for more information.- Parameters:
bitsNodeId
- node id component bit-length- Returns:
- this instance for fluent API
- Since:
- 4.0
-
getAllowedFutureMillis
public long getAllowedFutureMillis()- See Also:
-
setAllowedFutureMillis
Sets how far to the future is the generator allowed to go to generate IDs without blocking.The number of bits configured for the sequence number (
setBitsSequence(int)
determines how many IDs can be generated per second. We allow the generator to generate IDs with future timestamps, and this settings limits how much. When more IDs are requested, the call will block. This is important in case of a cluster black-out or cluster restart: we don't store how far the members went and after they restart, they will start from current time. If before the restart the generator went beyond the current time, duplicate IDs could be generated.The default value is 15 seconds (15000). If your cluster is able to restart more quickly, set a lower value.
See
FlakeIdGenerator
's class javadoc for more information.- Parameters:
allowedFutureMillis
- value in milliseconds- Returns:
- this instance for fluent API
- Since:
- 4.0
-
isStatisticsEnabled
public boolean isStatisticsEnabled()- See Also:
-
setStatisticsEnabled
Enables or disables statistics gathering ofLocalFlakeIdGeneratorStats
.- Parameters:
statisticsEnabled
-true
if statistics gathering is enabled (which is also the default),false
otherwise- Returns:
- this instance for fluent API
-
equals
-
hashCode
public int hashCode() -
toString
-
getFactoryId
public int getFactoryId()Description copied from interface:IdentifiedDataSerializable
Returns DataSerializableFactory factory ID for this class.- Specified by:
getFactoryId
in interfaceIdentifiedDataSerializable
- Returns:
- factory ID
-
getClassId
public int getClassId()Description copied from interface:IdentifiedDataSerializable
Returns type identifier for this class. It should be unique per DataSerializableFactory.- Specified by:
getClassId
in interfaceIdentifiedDataSerializable
- Returns:
- type ID
-
writeData
Description copied from interface:DataSerializable
Writes object fields to output stream- Specified by:
writeData
in interfaceDataSerializable
- Parameters:
out
- output- Throws:
IOException
- if an I/O error occurs. In particular, anIOException
may be thrown if the output stream has been closed.
-
readData
Description copied from interface:DataSerializable
Reads fields from the input stream- Specified by:
readData
in interfaceDataSerializable
- Parameters:
in
- input- Throws:
IOException
- if an I/O error occurs. In particular, anIOException
may be thrown if the input stream has been closed.
-