Class LeaderElector


  • public class LeaderElector
    extends Object
    • Field Detail

      • JITTER_FACTOR

        protected static final Double JITTER_FACTOR
    • Method Detail

      • run

        public void run()
        Starts the leader election loop. May only be run once.

        start() is preferred as it does not hold a thread.

      • start

        public CompletableFuture<?> start()
        Start a leader elector. The future may be cancelled to stop the leader elector. May only be run once.
        Returns:
        the future
      • release

        public boolean release()
        Release the leadership if currently held. If not cancelled, the elector will continue to try and re-acquire the lock.
        Returns:
        true if the lock was successfully released. false if there is no lock, or this is not the leader
      • canBecomeLeader

        protected final boolean canBecomeLeader​(LeaderElectionRecord leaderElectionRecord)
      • jitter

        protected static Duration jitter​(Duration duration,
                                         double maxFactor)
        Returns a Duration between the provided duration and (duration + maxFactor*duration)
        Parameters:
        duration - to apply jitter to
        maxFactor - for jitter
        Returns:
        the jittered duration