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 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 a LeaderElectionProperties record class.
      Parameters:
      waitForPodReady - the value for the waitForPodReady record component
      publishEvents - the value for the publishEvents record component
      leaseDuration - the value for the leaseDuration record component
      lockNamespace - the value for the lockNamespace record component
      lockName - the value for the lockName record component
      renewDeadline - the value for the renewDeadline record component
      retryPeriod - the value for the retryPeriod record component
      waitAfterRenewalFailure - the value for the waitAfterRenewalFailure record component
      useConfigMapAsLock - the value for the useConfigMapAsLock record component
      restartOnFailure - the value for the restartOnFailure record component
  • Method Details

    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • waitForPodReady

      public boolean waitForPodReady()
      Returns the value of the waitForPodReady record component.
      Returns:
      the value of the waitForPodReady record component
    • publishEvents

      public boolean publishEvents()
      Returns the value of the publishEvents record component.
      Returns:
      the value of the publishEvents record component
    • leaseDuration

      public Duration leaseDuration()
      Returns the value of the leaseDuration record component.
      Returns:
      the value of the leaseDuration record component
    • lockNamespace

      public String lockNamespace()
      Returns the value of the lockNamespace record component.
      Returns:
      the value of the lockNamespace record component
    • lockName

      public String lockName()
      Returns the value of the lockName record component.
      Returns:
      the value of the lockName record component
    • renewDeadline

      public Duration renewDeadline()
      Returns the value of the renewDeadline record component.
      Returns:
      the value of the renewDeadline record component
    • retryPeriod

      public Duration retryPeriod()
      Returns the value of the retryPeriod record component.
      Returns:
      the value of the retryPeriod record component
    • waitAfterRenewalFailure

      public Duration waitAfterRenewalFailure()
      Returns the value of the waitAfterRenewalFailure record component.
      Returns:
      the value of the waitAfterRenewalFailure record component
    • useConfigMapAsLock

      public boolean useConfigMapAsLock()
      Returns the value of the useConfigMapAsLock record component.
      Returns:
      the value of the useConfigMapAsLock record component
    • restartOnFailure

      public boolean restartOnFailure()
      Returns the value of the restartOnFailure record component.
      Returns:
      the value of the restartOnFailure record component