Interface IdSegmentDistributor
- All Superinterfaces:
Grouped
- All Known Subinterfaces:
GroupedIdSegmentDistributor
- All Known Implementing Classes:
DefaultGroupedIdSegmentDistributor,DefaultGroupedIdSegmentDistributor.GroupedBinding,IdSegmentDistributor.Atomic,IdSegmentDistributor.Mock,JdbcIdSegmentDistributor,MongoIdSegmentDistributor,ProxyIdSegmentDistributor,SpringRedisIdSegmentDistributor,ZookeeperIdSegmentDistributor
This interface provides the contract for distributing segments (contiguous blocks) of IDs across distributed instances. It is a key component of the segment-based ID generation algorithm, ensuring that each instance receives unique ranges of IDs.
The distributor is responsible for:
- Allocating unique ID segments within a namespace
- Managing segment size (step) configuration
- Providing segment chain functionality for advanced use cases
- Supporting time-to-live for segments
Common implementations include:
- Redis-based distribution
- ZooKeeper-based distribution
- JDBC-based distribution
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classAtomic implementation of IdSegmentDistributor for testing purposes.static classMock implementation of IdSegmentDistributor for testing purposes. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longThe default offset for ID segments.static final intThe default number of segments to allocate at once.static final longThe default step size for ID segments. -
Method Summary
Modifier and TypeMethodDescriptiondefault booleanCheck if this distributor allows segment resetting.static voidensureStep(long step) Ensure that the specified step size is valid.getName()Get the name for this distributor.Get the namespace for this distributor.default StringGet the namespaced name for this distributor.static StringgetNamespacedName(String namespace, String name) Create a namespaced name from namespace and name components.longgetStep()Get the step size for ID segments.default longgetStep(int segments) Get the total step size for the specified number of segments.default IdSegmentAllocate the next ID segment with infinite time-to-live.default IdSegmentnextIdSegment(int segments, long ttl) Allocate the next ID segment with multiple segments and specified time-to-live.default IdSegmentnextIdSegment(long ttl) Allocate the next ID segment with the specified time-to-live.default IdSegmentChainnextIdSegmentChain(IdSegmentChain previousChain) Allocate the next ID segment chain with default configuration.default IdSegmentChainnextIdSegmentChain(IdSegmentChain previousChain, int segments, long ttl) Allocate the next ID segment chain with custom configuration.default longAllocate the next maximum ID using the default step size.longnextMaxId(long step) Allocate the next maximum ID for the specified step size.
-
Field Details
-
DEFAULT_SEGMENTS
static final int DEFAULT_SEGMENTSThe default number of segments to allocate at once.- See Also:
-
DEFAULT_OFFSET
static final long DEFAULT_OFFSETThe default offset for ID segments.- See Also:
-
DEFAULT_STEP
static final long DEFAULT_STEPThe default step size for ID segments.This determines the size of each allocated segment, representing the number of IDs in each contiguous block.
- See Also:
-
-
Method Details
-
getNamespace
Get the namespace for this distributor.The namespace provides a logical grouping for ID segments, allowing multiple independent segment spaces within the same system.
- Returns:
- The namespace
-
getName
Get the name for this distributor.The name uniquely identifies this distributor within its namespace.
- Returns:
- The name
-
getNamespacedName
Get the namespaced name for this distributor.This is a convenience method that combines the namespace and name into a single string identifier.
- Returns:
- The namespaced name (namespace.name)
-
getNamespacedName
Create a namespaced name from namespace and name components.- Parameters:
namespace- The namespacename- The name- Returns:
- The namespaced name (namespace.name)
-
getStep
long getStep()Get the step size for ID segments.The step size determines how many IDs are allocated in each segment. Larger steps reduce coordination overhead but may lead to ID gaps.
- Returns:
- The step size
-
getStep
default long getStep(int segments) Get the total step size for the specified number of segments.This method calculates the total step size when allocating multiple segments at once, which is useful for prefetching scenarios.
- Parameters:
segments- The number of segments- Returns:
- The total step size
-
allowReset
default boolean allowReset()Check if this distributor allows segment resetting.Some distributors support resetting to earlier segments, while others only move forward to prevent ID conflicts.
- Returns:
trueif resetting is allowed,falseotherwise
-
nextMaxId
long nextMaxId(long step) Allocate the next maximum ID for the specified step size.This method allocates a new range of IDs by returning the maximum ID in the allocated range. The range starts at the previous maximum plus one and ends at the returned value.
- Parameters:
step- The step size for allocation- Returns:
- The maximum ID in the allocated range
-
nextMaxId
default long nextMaxId()Allocate the next maximum ID using the default step size.- Returns:
- The maximum ID in the allocated range
-
nextIdSegment
Allocate the next ID segment with infinite time-to-live.- Returns:
- The allocated ID segment
-
nextIdSegment
Allocate the next ID segment with the specified time-to-live.- Parameters:
ttl- The time-to-live for the segment- Returns:
- The allocated ID segment
-
nextIdSegment
Allocate the next ID segment with multiple segments and specified time-to-live.This method allocates a merged segment that represents multiple individual segments, useful for prefetching scenarios.
- Parameters:
segments- The number of segments to allocatettl- The time-to-live for the segment- Returns:
- The allocated merged ID segment
-
nextIdSegmentChain
Allocate the next ID segment chain with default configuration.- Parameters:
previousChain- The previous segment chain- Returns:
- The allocated ID segment chain
-
nextIdSegmentChain
@Nonnull default IdSegmentChain nextIdSegmentChain(IdSegmentChain previousChain, int segments, long ttl) Allocate the next ID segment chain with custom configuration.This method allocates a new segment chain that extends the previous chain, providing the foundation for the segment chain ID generation algorithm.
- Parameters:
previousChain- The previous segment chainsegments- The number of segments to allocatettl- The time-to-live for the segment- Returns:
- The allocated ID segment chain
-
ensureStep
static void ensureStep(long step) Ensure that the specified step size is valid.- Parameters:
step- The step size to validate
-