Class SegmentChainId

java.lang.Object
me.ahoo.cosid.segment.SegmentChainId
All Implemented Interfaces:
IdGenerator, SegmentId, Statistical, StringIdGenerator

public class SegmentChainId extends Object implements SegmentId
Segment chain algorithm ID generator.

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:

  1. Maintaining a chain of ID segments
  2. Generating IDs from the head of the chain
  3. Prefetching new segments in the background
  4. Dynamically adjusting prefetch distance based on demand

SegmentChainId

  • Field Details

    • DEFAULT_SAFE_DISTANCE

      public static final int DEFAULT_SAFE_DISTANCE
      The 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

      public SegmentChainId(IdSegmentDistributor maxIdDistributor)
      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 segments
      safeDistance - The safe distance for prefetching
      maxIdDistributor - The distributor for allocating new segments
      prefetchWorkerExecutorService - The executor for prefetch jobs
  • Method Details

    • current

      public IdSegment 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.

      Specified by:
      current in interface SegmentId
      Returns:
      The current ID segment (head of the chain)
    • getHead

      public IdSegmentChain 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:
      generate in interface IdGenerator
      Returns:
      A unique distributed ID