Class LeaseTiming
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 Summary
ConstructorsConstructorDescriptionLeaseTiming(Lease lease, LeaseConfig config) Binds a lease record to a policy. -
Method Summary
Modifier and TypeMethodDescriptionlongchallengeAtMillis(int rank) Returns the instant at which a challenger of one rank challenges.longReturns the instant the lease expires.lease()Returns the lease record behind this clock.booleanliveAt(long nowMillis) Reports whether the lease is live at one instant.remainingAt(long nowMillis) Returns the extent that is left before the deadline.longReturns the instant at which the holder writes its next renewal.booleanrenewDueAt(long nowMillis) Reports whether the holder writes a renewal at one instant.
-
Constructor Details
-
LeaseTiming
Binds a lease record to a policy.- Parameters:
lease- the lease record to readconfig- the policy that supplies the renew interval and the stagger- Throws:
NullPointerException- if the lease or the config is null
-
-
Method Details
-
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
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.
InitProducerIdis 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
-