Interface LeaderElection<T>

  • All Superinterfaces:
    java.lang.AutoCloseable

    public interface LeaderElection<T>
    extends java.lang.AutoCloseable
    Leader election controller.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.util.concurrent.CompletableFuture<java.lang.Void> asyncClose()
      Close the leader election controller and release the leadership (if it was acquired).
      java.util.concurrent.CompletableFuture<LeaderElectionState> elect​(T proposedValue)
      Try to become the leader.
      java.util.concurrent.CompletableFuture<java.util.Optional<T>> getLeaderValue()
      Get the value set by the elected leader, or empty if there's currently no leader.
      java.util.Optional<T> getLeaderValueIfPresent()
      Get the value set by the elected leader, or empty if there's currently no leader.
      LeaderElectionState getState()
      Get the current leader election state.
      • Methods inherited from interface java.lang.AutoCloseable

        close
    • Method Detail

      • elect

        java.util.concurrent.CompletableFuture<LeaderElectionState> elect​(T proposedValue)
        Try to become the leader.

        Warning: because of the distributed nature of the leader election, having been promoted to "leader" status will never provide a strong guarantee that no one else also thinks it's the leader. The caller will have to deal with these race conditions when using the resource itself (eg. using compareAndSet() or fencing mechanisms).

        Parameters:
        proposedValue - the value to set for the leader, in the case this instance succeeds in becoming leader
        Returns:
        a future that will track the completion of the operation if there's a failure in the leader election
      • getLeaderValue

        java.util.concurrent.CompletableFuture<java.util.Optional<T>> getLeaderValue()
        Get the value set by the elected leader, or empty if there's currently no leader.
        Returns:
        a future that will track the completion of the operation
      • getLeaderValueIfPresent

        java.util.Optional<T> getLeaderValueIfPresent()
        Get the value set by the elected leader, or empty if there's currently no leader.

        The call is non blocking and in certain cases can return Optional.empty() even though a leader is technically elected.

        Returns:
        a future that will track the completion of the operation
      • asyncClose

        java.util.concurrent.CompletableFuture<java.lang.Void> asyncClose()
        Close the leader election controller and release the leadership (if it was acquired).
        Returns:
        a future that will track the completion of the operation