Class MachineIdLease

java.lang.Object
me.ahoo.cosid.machine.MachineIdLease

public final class MachineIdLease extends Object
The local view of how long this instance may keep using its machine id.

Other instances may take a machine id over once its owner has not renewed it for safeGuardDuration. The lease is therefore valid until the start of the last successful renewal plus safeGuardDuration, minus a margin for clock skew between hosts. Generators check it before every id, so an instance that cannot renew its lease stops generating ids before another instance can reuse the machine id.

Thread-safe; isValid(long) is a pair of volatile reads.

  • Field Details

    • MAX_CLOCK_SKEW_MARGIN

      public static final Duration MAX_CLOCK_SKEW_MARGIN
      Upper bound of the clock skew margin.
    • FOREVER

      public static final MachineIdLease FOREVER
      A lease that never expires, used when machine ids are never recycled.
  • Constructor Details

    • MachineIdLease

      public MachineIdLease(@Nullable String namespace, @Nullable InstanceId instanceId, Duration safeGuardDuration)
  • Method Details

    • renew

      public void renew(long renewedAt)
      Extend the lease after a successful distribute or guard.
      Parameters:
      renewedAt - the time, in milliseconds, at which the renewal request was started
    • markLost

      public void markLost()
      The remote store reported that the machine id now belongs to another instance. The lease never becomes valid again.
    • isLost

      public boolean isLost()
    • markReleased

      public void markReleased()
      The machine id is about to be reverted on shutdown. The lease never becomes valid again, so no id is generated with a machine id that another instance may already have taken.
    • isReleased

      public boolean isReleased()
    • getValidUntil

      public long getValidUntil()
    • isValid

      public boolean isValid(long now)
    • ensureValid

      public void ensureValid() throws MachineIdLostException
      Throw MachineIdLostException if the lease is lost or expired.
      Throws:
      MachineIdLostException
    • toString

      public String toString()
      Overrides:
      toString in class Object