Record Class LeaderElectionProperties
java.lang.Object
java.lang.Record
org.springframework.cloud.kubernetes.commons.leader.election.LeaderElectionProperties
@ConfigurationProperties("spring.cloud.kubernetes.leader.election")
public record LeaderElectionProperties(boolean waitForPodReady, boolean publishEvents, Duration leaseDuration, String lockNamespace, String lockName, Duration renewDeadline, Duration retryPeriod, Duration waitAfterRenewalFailure, boolean useConfigMapAsLock, boolean restartOnFailure)
extends Record
waitForPodReady: should we wait for the readiness of the pod,
before we even trigger the leader election process.
publishEvents: should we publish events (ApplicationEvent)
when the state of leaders changes.
leaseDuration: TTL of the lease. No other leader candidate
can acquire the lease unless this one expires.
lockNamespace: where to create the "lock"
(this is either a lease or a config map)
lockName: the name of the lease or configmap
renewDeadline: once the lock is acquired,
and we are the current leader, we try to "extend" the lease.
We must extend it within this timeline.
retryPeriod: how often to retry when trying to get
the lock to become the leader. In our current code,
this is what we use in LeaderInitiator::start,
more exactly in the scheduleAtFixRate
restartOnFailure: what to do when leader election future fails
with an Exception. Do we restart the leader election process
or let it fail and thus this instance never participates
in the leader election process.
First, we try to acquire the lock (lock is either a configmap or a lease)
and by "acquire" I mean write to it (or its annotations for a configmap).
Whoever writes first (all others will get a 409) becomes the leader.
All leader candidates that are not leaders will continue to spin forever
until they get a chance to become one. They retry every 'retryPeriod'.
The current leader, after it establishes itself as one,
will spin forever too, but will try to extend its leadership.
It extends that by updating the entries in the lease,
specifically the one we care about is: renewTime.
This one is updated every 'retryPeriod'. For example,
every 2 seconds (retryPeriod), it will update its 'renewTime' with "now".
All other, non-leaders are spinning and check a few things in each cycle:
"Am I the leader?" If the answer is no, they go below:
"Can I become the leader?" This is answered by looking at:
now().isAfter(leaderElectionRecord.getRenewTime()
.plus(leaderElectionConfig.getLeaseDuration()))
So they can only try to acquire the leadership if 'leaseDuration'
(basically a TTL) + renewTime (when was the last renewal) has expired.
This means that no one will be able to even try to acquire the lock
until that leaseDuration expires. When the pod is killed or dies
unexpectedly (OOM, for example), all non-leaders will wait until
leaseDuration expires.
In case of a graceful shutdown (we call CompletableFuture::cancel on the fabric8 instances),
there is code that fabric8 will trigger to "reset" the lease:
they will set the renewTime to "now" and leaseDuration to 1 second.
- Author:
- wind57
-
Constructor Summary
ConstructorsConstructorDescriptionLeaderElectionProperties(boolean waitForPodReady, boolean publishEvents, Duration leaseDuration, String lockNamespace, String lockName, Duration renewDeadline, Duration retryPeriod, Duration waitAfterRenewalFailure, boolean useConfigMapAsLock, boolean restartOnFailure) Creates an instance of aLeaderElectionPropertiesrecord class. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.Returns the value of theleaseDurationrecord component.lockName()Returns the value of thelockNamerecord component.Returns the value of thelockNamespacerecord component.booleanReturns the value of thepublishEventsrecord component.Returns the value of therenewDeadlinerecord component.booleanReturns the value of therestartOnFailurerecord component.Returns the value of theretryPeriodrecord component.final StringtoString()Returns a string representation of this record class.booleanReturns the value of theuseConfigMapAsLockrecord component.Returns the value of thewaitAfterRenewalFailurerecord component.booleanReturns the value of thewaitForPodReadyrecord component.
-
Constructor Details
-
LeaderElectionProperties
public LeaderElectionProperties(@DefaultValue("true") boolean waitForPodReady, @DefaultValue("true") boolean publishEvents, @DefaultValue("15s") Duration leaseDuration, @DefaultValue("default") String lockNamespace, @DefaultValue("spring-k8s-leader-election-lock") String lockName, @DefaultValue("10s") Duration renewDeadline, @DefaultValue("2s") Duration retryPeriod, @DefaultValue("3s") Duration waitAfterRenewalFailure, @DefaultValue("false") boolean useConfigMapAsLock, @DefaultValue("true") boolean restartOnFailure) Creates an instance of aLeaderElectionPropertiesrecord class.- Parameters:
waitForPodReady- the value for thewaitForPodReadyrecord componentpublishEvents- the value for thepublishEventsrecord componentleaseDuration- the value for theleaseDurationrecord componentlockNamespace- the value for thelockNamespacerecord componentlockName- the value for thelockNamerecord componentrenewDeadline- the value for therenewDeadlinerecord componentretryPeriod- the value for theretryPeriodrecord componentwaitAfterRenewalFailure- the value for thewaitAfterRenewalFailurerecord componentuseConfigMapAsLock- the value for theuseConfigMapAsLockrecord componentrestartOnFailure- the value for therestartOnFailurerecord component
-
-
Method Details
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
waitForPodReady
public boolean waitForPodReady()Returns the value of thewaitForPodReadyrecord component.- Returns:
- the value of the
waitForPodReadyrecord component
-
publishEvents
public boolean publishEvents()Returns the value of thepublishEventsrecord component.- Returns:
- the value of the
publishEventsrecord component
-
leaseDuration
Returns the value of theleaseDurationrecord component.- Returns:
- the value of the
leaseDurationrecord component
-
lockNamespace
Returns the value of thelockNamespacerecord component.- Returns:
- the value of the
lockNamespacerecord component
-
lockName
Returns the value of thelockNamerecord component.- Returns:
- the value of the
lockNamerecord component
-
renewDeadline
Returns the value of therenewDeadlinerecord component.- Returns:
- the value of the
renewDeadlinerecord component
-
retryPeriod
Returns the value of theretryPeriodrecord component.- Returns:
- the value of the
retryPeriodrecord component
-
waitAfterRenewalFailure
Returns the value of thewaitAfterRenewalFailurerecord component.- Returns:
- the value of the
waitAfterRenewalFailurerecord component
-
useConfigMapAsLock
public boolean useConfigMapAsLock()Returns the value of theuseConfigMapAsLockrecord component.- Returns:
- the value of the
useConfigMapAsLockrecord component
-
restartOnFailure
public boolean restartOnFailure()Returns the value of therestartOnFailurerecord component.- Returns:
- the value of the
restartOnFailurerecord component
-