Class LeaseKeeper

java.lang.Object
me.ahoo.cosid.lease.LeaseKeeper
All Implemented Interfaces:
AutoCloseable, SlotHandle

public final class LeaseKeeper extends Object implements SlotHandle, AutoCloseable
Holds the slot lease of a runtime (05-machine-lease.md §4.2): acquires it at start(), renews it every ttl/3 with backoff, re-acquires a lost slot and publishes each holding as a SlotBinding.

The renewals run in one long-lived task on the executor, which must run tasks asynchronously (the runtime's default runs each on a virtual thread). Their schedule follows elapsed time, not the wall clock, so a clock step does not delay a renewal.

Its public constructors are the assembly entry CosIdRuntime uses (R2-core-03): they keep their signatures, and a new parameter comes only through a builder or a static factory, never as one more positional overload (api-freeze.md §4.2).

  • Constructor Details

    • LeaseKeeper

      public LeaseKeeper(SlotLeaseStore store, String namespace, Owner owner, int bits, Duration ttl, Duration tolerance, OptionalInt preferredSlot, Duration acquireTimeout, Executor executor, TimeSource time, GeneratorListener listener)
      A lease keeper; start() acquires the slot.
      Parameters:
      store - the lease store
      namespace - the namespace
      owner - the owner, with this runtime's incarnation
      bits - the slots are [0, 2^bits)
      ttl - the lease time to live; default 30s
      tolerance - the ceiling lies this far beyond the expiry (D20); default 1s
      preferredSlot - the slot to try first
      acquireTimeout - how long start() retries an exhausted namespace; default ttl
      executor - runs the renewals; the runtime's own executor by default
      time - the time source
      listener - the event listener
    • LeaseKeeper

      public LeaseKeeper(SlotLeaseStore store, String namespace, Owner owner, int bits, Duration ttl, Duration tolerance, int slotOffset, OptionalInt preferredSlot, Duration acquireTimeout, Executor executor, TimeSource time, GeneratorListener listener)
      A lease keeper of a namespace with a slot offset (api-freeze.md §2.1.1); start() acquires the slot.
      Parameters:
      store - the lease store
      namespace - the namespace
      owner - the owner, with this runtime's incarnation
      bits - the slots are [0, 2^bits)
      ttl - the lease time to live
      tolerance - the ceiling lies this far beyond the expiry (D20)
      slotOffset - recorded in the namespace metadata; the generators add it to the slot
      preferredSlot - the slot to try first
      acquireTimeout - how long start() retries an exhausted namespace
      executor - runs the renewals
      time - the time source
      listener - the event listener
  • Method Details

    • namespace

      public String namespace()
      The namespace the slot is leased in.
      Returns:
      the namespace
    • owner

      public Owner owner()
      The owner the slot is leased for.
      Returns:
      the owner
    • bits

      public int bits()
      The slot bits: the namespace holds 2^bits slots.
      Returns:
      the bits
    • start

      public void start()
      Acquires the slot and starts renewing it.
      Throws:
      CosIdException - if no slot is acquired within the acquire timeout
    • current

      public SlotBinding current()
      Description copied from interface: SlotHandle
      The current binding: one volatile read. A generator reads it once per id.
      Specified by:
      current in interface SlotHandle
      Returns:
      the binding
    • lease

      public Optional<SlotLease> lease()
      The lease as last returned by the store.
      Returns:
      the lease, or empty before the first acquisition and after release
    • close

      public void close(long lastIssued)
      Stops renewing and releases the lease with the exact last issued timestamp (§4.3). Call it once every generator on the slot is closed.
      Parameters:
      lastIssued - the largest timestamp issued on the slot, in milliseconds
    • close

      public void close()
      Stops renewing and releases the lease as if issued up to its ceiling, for a slot no generator used.
      Specified by:
      close in interface AutoCloseable