Class MachineLeaseManager

java.lang.Object
me.ahoo.cosid.machine.lease.MachineLeaseManager

public class MachineLeaseManager extends Object
The single machine id allocation algorithm, on top of any MachineLeaseStore.

Order: take back this instance's own lease, then a released machine id, then a never-used one, and only then an expired one. A stable instance never takes over another instance's expired lease, and with MachineIdDistributor.FOREVER_SAFE_GUARD_DURATION no lease ever expires.

Every time comes from MachineLeaseStore.serverTime(), except the watermark written on release, which is the generator's last timestamp from this host's clock. Hosts' clocks must therefore be kept in sync: a host running ahead only delays the next owner of a released machine id (it waits until its clock passes the watermark), which is safe but slow.

Since:
3.4.0
  • Constructor Details

  • Method Details

    • getStore

      public MachineLeaseStore getStore()
    • acquire

      public Lease acquire(String namespace, InstanceId instanceId, int machineBit, Duration safeGuardDuration) throws MachineIdOverflowException
      Take a machine id for the instance.
      Throws:
      MachineIdOverflowException - if every machine id is held by a live (or stable) owner
    • reclaim

      public Optional<Lease> reclaim(String namespace, InstanceId instanceId)
      Take back the lease this instance holds, for instance after a restart without a lease in memory.
      Returns:
      empty if the instance holds none: it was released or taken over
    • renew

      public Optional<Lease> renew(Lease lease)
      Renew a lease.
      Returns:
      the renewed lease, or empty if it was taken over in the meantime
    • release

      public boolean release(Lease lease, long watermark)
      Release a lease on shutdown.
      Parameters:
      watermark - the generator's last timestamp (host clock), the next owner's clock-backwards watermark
      Returns:
      false if the lease was taken over in the meantime