Class LeaseTiming

java.lang.Object
io.krabka.streams.coordination.LeaseTiming

public final class LeaseTiming extends Object
The lease clock of one lease record under one policy.

Every method takes and returns milliseconds since the Unix epoch, because a deadline is a coordinate and not an extent. remainingAt(long) is the one exception, and it returns the extent that is left.

Read the deadline as a hint and not as a guarantee. A holder past its deadline still owns the newest epoch until a challenger mints a newer one. A challenger that reads a live lease still wins the role at once if it calls InitProducerId anyway. The lease only stops it from trying.

Example

LeaseTiming timing = config.timing(lease);
if (timing.renewDueAt(clock.nowMillis())) {
    leadership.renew();
}
long myTurn = timing.challengeAtMillis(state.rankOf(me).orElseThrow());
  • Constructor Details

    • LeaseTiming

      public LeaseTiming(Lease lease, LeaseConfig config)
      Binds a lease record to a policy.
      Parameters:
      lease - the lease record to read
      config - the policy that supplies the renew interval and the stagger
      Throws:
      NullPointerException - if the lease or the config is null
  • Method Details

    • lease

      public Lease lease()
      Returns the lease record behind this clock.
      Returns:
      the lease this clock reads
    • expiresAtMillis

      public long expiresAtMillis()
      Returns the instant the lease expires.
      Returns:
      the deadline, in milliseconds since the Unix epoch
    • liveAt

      public boolean liveAt(long nowMillis)
      Reports whether the lease is live at one instant.

      The lease is live before its deadline and expired from the deadline on. The rank 0 challenger challenges at exactly the deadline, so the two tests leave no gap and no overlap.

      Parameters:
      nowMillis - the instant to test, in milliseconds since the Unix epoch
      Returns:
      true when the instant falls before the deadline
    • remainingAt

      public Duration remainingAt(long nowMillis)
      Returns the extent that is left before the deadline.
      Parameters:
      nowMillis - the instant to measure from, in milliseconds since the Unix epoch
      Returns:
      the remaining extent, and zero from the deadline on
    • renewAtMillis

      public long renewAtMillis()
      Returns the instant at which the holder writes its next renewal.

      The value is the grant instant plus the renew interval, and it never passes the deadline. A holder that follows it always writes before it loses the lease.

      Returns:
      the renewal instant, in milliseconds since the Unix epoch
    • renewDueAt

      public boolean renewDueAt(long nowMillis)
      Reports whether the holder writes a renewal at one instant.
      Parameters:
      nowMillis - the instant to test, in milliseconds since the Unix epoch
      Returns:
      true when the renewal instant has passed
    • challengeAtMillis

      public long challengeAtMillis(int rank)
      Returns the instant at which a challenger of one rank challenges.

      Rank 0 challenges at the deadline, and each later rank adds one challenge stagger. The stagger saves epoch churn and gives no safety. InitProducerId is atomic at the coordinator, so a simultaneous challenge by every standby still leaves exactly one member with the newest epoch.

      Parameters:
      rank - the challenge rank, where zero is the first standby
      Returns:
      the challenge instant, in milliseconds since the Unix epoch