Class AbstractIdSegmentDistributor

java.lang.Object
me.ahoo.cosid.segment.AbstractIdSegmentDistributor
All Implemented Interfaces:
Grouped, IdSegmentDistributor
Direct Known Subclasses:
JdbcIdSegmentDistributor, MongoIdSegmentDistributor, ProxyIdSegmentDistributor, SpringRedisIdSegmentDistributor, ZookeeperIdSegmentDistributor

public abstract class AbstractIdSegmentDistributor extends Object implements IdSegmentDistributor
Base of the IdSegmentDistributors: common properties and one rollback rule for every backend.

The max id this distributor got from the store must strictly move forward. If the store hands out a max id below last max id + step (restored from a backup, a lost Redis key, a manual edit), the range overlaps ids already issued, and nextMaxId(long) throws IdSegmentRollbackException (an IllegalStateException) instead of issuing duplicates. Subclasses only implement doNextMaxId(long).

Since:
3.4.0
  • Constructor Details

    • AbstractIdSegmentDistributor

      protected AbstractIdSegmentDistributor(String namespace, String name, long step)
  • Method Details

    • getNamespace

      public @NonNull String getNamespace()
      Description copied from interface: IdSegmentDistributor
      Get the namespace for this distributor.

      The namespace provides a logical grouping for ID segments, allowing multiple independent segment spaces within the same system.

      Specified by:
      getNamespace in interface IdSegmentDistributor
      Returns:
      The namespace
    • getName

      public @NonNull String getName()
      Description copied from interface: IdSegmentDistributor
      Get the name for this distributor.

      The name uniquely identifies this distributor within its namespace.

      Specified by:
      getName in interface IdSegmentDistributor
      Returns:
      The name
    • getStep

      public long getStep()
      Description copied from interface: IdSegmentDistributor
      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.

      Specified by:
      getStep in interface IdSegmentDistributor
      Returns:
      The step size
    • getLastMaxId

      public long getLastMaxId()
      The largest max id this distributor got so far, 0 before the first one.
    • nextMaxId

      public long nextMaxId(long step)
      Description copied from interface: IdSegmentDistributor
      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.

      Specified by:
      nextMaxId in interface IdSegmentDistributor
      Parameters:
      step - The step size for allocation
      Returns:
      The maximum ID in the allocated range
    • doNextMaxId

      protected abstract long doNextMaxId(long step)
      Ask the store for the next max id, step after the previous one.
      Parameters:
      step - the step, already validated
      Returns:
      the new max id