Class RedisStore

java.lang.Object
me.ahoo.cosid.store.redis.RedisStore
All Implemented Interfaces:
AutoCloseable, me.ahoo.cosid.lease.SlotLeaseStore, me.ahoo.cosid.segment.RangeStore, me.ahoo.cosid.store.Store

public final class RedisStore extends Object implements me.ahoo.cosid.store.Store, me.ahoo.cosid.segment.RangeStore, me.ahoo.cosid.lease.SlotLeaseStore
The Redis store (06-stores.md ยง2.2): both primitives as Lua scripts run through RedisScripts, stamped by the Redis clock (TIME). Redis 7.0 or later (D27).

Keys, each group sharing one hash tag so that a script touches one Redis Cluster slot:

  • cosid:{key}: the counter of a range key, a decimal string; cosid:{key}:sealed: its seal marker. Keys are never deleted (D23).
  • cosid:{ns}:meta: the limit, tolerance and slot offset a namespace was first used with (a hash of limit, tolerance, slotOffset; a meta written by 5.0.0-alpha.1 has no slotOffset, which reads as 0); cosid:{ns}:slot: slot to its row; cosid:{ns}:free: the free slots, scored by slot number; cosid:{ns}:live: the live leases, scored by expiresAt; cosid:{ns}:owner: owner key to the slot it took last; cosid:{ns}:held: owner key to the slots whose row names it.
Every acquire is a few hash and sorted set lookups, O(log n) in the slot count, with a sweep of at most sweepBatch expired leases; there is no scan of the namespace.

Durability precondition. The uniqueness of the ids rests on "an acknowledged write is never lost": either a single node with AOF and appendfsync always, or WAIT covering every replica. Under asynchronous replication a failover can lose range and lease writes and so produce duplicate ids; where uniqueness is strict, prefer the JDBC or ZooKeeper store. The store only hands out a range or a ceiling once its write is acknowledged (RedisScripts).

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    How many expired leases one acquire moves to the free set at most.
  • Constructor Summary

    Constructors
    Constructor
    Description
    A store on the given scripts, with the default sweep batch; closing it closes nothing.
    RedisStore(RedisScripts scripts, int sweepBatch, AutoCloseable resources)
    A store on the given scripts.
  • Method Summary

    Modifier and Type
    Method
    Description
    me.ahoo.cosid.lease.AcquireResult
    acquire(String namespace, me.ahoo.cosid.lease.Owner owner, int limit, Duration ttl, Duration tolerance, int slotOffset, OptionalInt preferredSlot)
     
    void
     
    me.ahoo.cosid.segment.RangeStore
     
    boolean
    release(me.ahoo.cosid.lease.SlotLease lease, long lastIssued)
     
    Optional<me.ahoo.cosid.lease.SlotLease>
    renew(me.ahoo.cosid.lease.SlotLease lease, Duration ttl, Duration tolerance)
     
    me.ahoo.cosid.segment.Range
    reserve(String key, long size, long initialOffset)
     
    boolean
    seal(String key)
    Seals the key; a key that does not exist yet is sealed too, so it is never created (S6).
    me.ahoo.cosid.lease.SlotLeaseStore
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_SWEEP_BATCH

      public static final int DEFAULT_SWEEP_BATCH
      How many expired leases one acquire moves to the free set at most.
      See Also:
  • Constructor Details

    • RedisStore

      public RedisStore(RedisScripts scripts)
      A store on the given scripts, with the default sweep batch; closing it closes nothing.
      Parameters:
      scripts - how the store runs its scripts
    • RedisStore

      public RedisStore(RedisScripts scripts, int sweepBatch, AutoCloseable resources)
      A store on the given scripts.
      Parameters:
      scripts - how the store runs its scripts
      sweepBatch - how many expired leases one acquire moves to the free set at most, positive
      resources - closed with the store: what the store created itself, never an injected client
  • Method Details

    • ranges

      public me.ahoo.cosid.segment.RangeStore ranges()
      Specified by:
      ranges in interface me.ahoo.cosid.store.Store
    • slots

      public me.ahoo.cosid.lease.SlotLeaseStore slots()
      Specified by:
      slots in interface me.ahoo.cosid.store.Store
    • reserve

      public me.ahoo.cosid.segment.Range reserve(String key, long size, long initialOffset)
      Specified by:
      reserve in interface me.ahoo.cosid.segment.RangeStore
    • seal

      public boolean seal(String key)
      Seals the key; a key that does not exist yet is sealed too, so it is never created (S6).
      Specified by:
      seal in interface me.ahoo.cosid.segment.RangeStore
      Parameters:
      key - the range key
      Returns:
      true
    • acquire

      public me.ahoo.cosid.lease.AcquireResult acquire(String namespace, me.ahoo.cosid.lease.Owner owner, int limit, Duration ttl, Duration tolerance, int slotOffset, OptionalInt preferredSlot)
      Specified by:
      acquire in interface me.ahoo.cosid.lease.SlotLeaseStore
    • renew

      public Optional<me.ahoo.cosid.lease.SlotLease> renew(me.ahoo.cosid.lease.SlotLease lease, Duration ttl, Duration tolerance)
      Specified by:
      renew in interface me.ahoo.cosid.lease.SlotLeaseStore
    • release

      public boolean release(me.ahoo.cosid.lease.SlotLease lease, long lastIssued)
      Specified by:
      release in interface me.ahoo.cosid.lease.SlotLeaseStore
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface me.ahoo.cosid.segment.RangeStore
      Specified by:
      close in interface me.ahoo.cosid.lease.SlotLeaseStore
      Specified by:
      close in interface me.ahoo.cosid.store.Store