Class SegmentChainId
- All Implemented Interfaces:
IdGenerator,SegmentId,Statistical,StringIdGenerator
This implementation extends the basic segment algorithm by organizing segments into a chain structure, providing improved performance and resilience through prefetching and dynamic scaling.
Key features of the segment chain algorithm:
- Chain Structure: Segments are organized in a linked chain
- Prefetching: Segments are prefetched in the background
- Dynamic Scaling: Prefetch distance adapts to demand
- High Availability: Multiple segments provide resilience
The algorithm works by:
- Maintaining a chain of ID segments
- Generating IDs from the head of the chain
- Prefetching new segments in the background
- Dynamically adjusting prefetch distance based on demand

-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassBackground prefetch job for segment chain ID generation. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default safe distance for prefetching segments. -
Constructor Summary
ConstructorsConstructorDescriptionSegmentChainId(long idSegmentTtl, int safeDistance, IdSegmentDistributor maxIdDistributor, PrefetchWorkerExecutorService prefetchWorkerExecutorService) Create a new SegmentChainId with custom configuration.SegmentChainId(IdSegmentDistributor maxIdDistributor) Create a new SegmentChainId with default configuration. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface me.ahoo.cosid.IdGenerator
generateAsString, idConverter
-
Field Details
-
DEFAULT_SAFE_DISTANCE
public static final int DEFAULT_SAFE_DISTANCEThe default safe distance for prefetching segments.This is the minimum number of segments to keep prefetched ahead of the current consumption point to ensure continuous ID generation.
- See Also:
-
-
Constructor Details
-
SegmentChainId
Create a new SegmentChainId with default configuration.This constructor creates a generator with infinite segment TTL, default safe distance, and the default prefetch worker executor.
- Parameters:
maxIdDistributor- The distributor for allocating new segments
-
SegmentChainId
public SegmentChainId(long idSegmentTtl, int safeDistance, IdSegmentDistributor maxIdDistributor, PrefetchWorkerExecutorService prefetchWorkerExecutorService) Create a new SegmentChainId with custom configuration.This constructor allows full control over all parameters of the segment chain ID generator.
- Parameters:
idSegmentTtl- The time-to-live for segmentssafeDistance- The safe distance for prefetchingmaxIdDistributor- The distributor for allocating new segmentsprefetchWorkerExecutorService- The executor for prefetch jobs
-
-
Method Details
-
current
Get the current ID segment (the head of the chain).This method returns the segment from which IDs are currently being allocated. In the chain implementation, this is the head of the segment chain.
-
getHead
Get the head of the segment chain.This method provides direct access to the head of the segment chain, which contains additional chain-specific information beyond the basic segment interface.
- Returns:
- The head of the segment chain
-
generate
public long generate()Generate a new distributed ID.This method generates a unique ID by traversing the segment chain and allocating an ID from the first available segment. If no segments are available, it triggers prefetching of new segments.
- Specified by:
generatein interfaceIdGenerator- Returns:
- A unique distributed ID
-