Interface LeaderElectionManager


public interface LeaderElectionManager
Leader Election Manager abstracts cluster election registration and status
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the average amount of time it has taken to poll the leader election service in the past 5 minutes.
    getLeader(String roleName)
    Returns the Participant ID of the node that is elected the leader, if one was provided when the node registered for the role via register(String, LeaderElectionStateChangeListener, String).
    getLeadershipChangeCount(long duration, TimeUnit timeUnit)
    Returns a Map of Role Name to the number of times that the leader has been detected as changing in the given time period.
    long
    Returns the maximum amount of time any poll of the leader election service has taken in the past 5 minutes.
    long
    Returns the minimum amount of time any poll of the leader election service has taken in the past 5 minutes.
    long
    Returns the number of times that the leader election service has been polled in the past 5 minutes
    boolean
    Indicates whether or not this node is an active participant in the election for the given role
    boolean
    isLeader(String roleName)
    Returns a boolean value indicating whether or not this node is the elected leader for the given role
    void
    Adds a new role for which a leader is required, without participating in the leader election.
    void
    register(String roleName, LeaderElectionStateChangeListener listener, String participantId)
    Adds a new role for which a leader is required, providing the given value for this node as the Participant ID.
    void
    Starts managing leader elections for all registered roles
    void
    Stops managing leader elections and relinquishes the role as leader for all registered roles.
    void
    unregister(String roleName)
    Removes the role with the given name from this manager.
  • Method Details

    • start

      void start()
      Starts managing leader elections for all registered roles
    • register

      void register(String roleName, LeaderElectionStateChangeListener listener)
      Adds a new role for which a leader is required, without participating in the leader election. I.e., this node will not be elected leader but will passively observe changes to the leadership. This allows calls to isLeader(String) and getLeader(String) to know which node is currently elected the leader.
      Parameters:
      roleName - the name of the role
      listener - a listener that will be called when the node gains or relinquishes the role of leader
    • register

      void register(String roleName, LeaderElectionStateChangeListener listener, String participantId)
      Adds a new role for which a leader is required, providing the given value for this node as the Participant ID. If the Participant ID is null, this node will never be elected leader but will passively observe changes to the leadership.
      Parameters:
      roleName - the name of the role
      listener - a listener that will be called when the node gains or relinquishes the role of leader
      participantId - the ID to register as this node's Participant ID. All nodes will see this as the identifier when asking to see who the leader is via the getLeader(String) method
    • isActiveParticipant

      boolean isActiveParticipant(String roleName)
      Indicates whether or not this node is an active participant in the election for the given role
      Parameters:
      roleName - the name of the role
      Returns:
      true if this node is an active participant in the election (is allowed to be elected) or false otherwise
    • getLeader

      Optional<String> getLeader(String roleName)
      Returns the Participant ID of the node that is elected the leader, if one was provided when the node registered for the role via register(String, LeaderElectionStateChangeListener, String). If there is currently no leader known or if the role was registered without providing a Participant ID, this will return null.
      Parameters:
      roleName - the name of the role
      Returns:
      the Participant ID of the node that is elected leader, or empty if either no leader is known or the leader did not register with a Participant ID.
    • unregister

      void unregister(String roleName)
      Removes the role with the given name from this manager. If this node is the elected leader for the given role, this node will relinquish the leadership role
      Parameters:
      roleName - the name of the role to unregister
    • isLeader

      boolean isLeader(String roleName)
      Returns a boolean value indicating whether or not this node is the elected leader for the given role
      Parameters:
      roleName - the name of the role
      Returns:
      true if the node is the elected leader, false otherwise.
    • stop

      void stop()
      Stops managing leader elections and relinquishes the role as leader for all registered roles. If the LeaderElectionManager is later started again, all previously registered roles will still be registered.
    • getLeadershipChangeCount

      Map<String,Integer> getLeadershipChangeCount(long duration, TimeUnit timeUnit)
      Returns a Map of Role Name to the number of times that the leader has been detected as changing in the given time period. Note that the amount of time that these counts is stored and the precision is implementation specific.
      Parameters:
      duration - the duration
      timeUnit - the time unit
      Returns:
      a Mapping of role to the number of times that the leader for that role has changed
    • getAveragePollTime

      long getAveragePollTime(TimeUnit timeUnit)
      Returns the average amount of time it has taken to poll the leader election service in the past 5 minutes.
      Parameters:
      timeUnit - the desired time unit
      Returns:
      the average amount of time it has taken to poll the leader election service, or -1 if this is not supported
    • getMinPollTime

      long getMinPollTime(TimeUnit timeUnit)
      Returns the minimum amount of time any poll of the leader election service has taken in the past 5 minutes.
      Parameters:
      timeUnit - the desired time unit
      Returns:
      the minimum amount of time any poll of the leader election service has taken, or -1 if this is not supported
    • getMaxPollTime

      long getMaxPollTime(TimeUnit timeUnit)
      Returns the maximum amount of time any poll of the leader election service has taken in the past 5 minutes.
      Parameters:
      timeUnit - the desired time unit
      Returns:
      the maximum amount of time any poll of the leader election service has taken, or -1 if this is not supported
    • getPollCount

      long getPollCount()
      Returns the number of times that the leader election service has been polled in the past 5 minutes
      Returns:
      the number of times that the leader election service has been polled in the past 5 minutes, or -1 if this is not supported