Interface LeaderElectionManager
public interface LeaderElectionManager
Leader Election Manager abstracts cluster election registration and status
-
Method Summary
Modifier and TypeMethodDescriptionlong
getAveragePollTime
(TimeUnit timeUnit) Returns the average amount of time it has taken to poll the leader election service in the past 5 minutes.Returns the Participant ID of the node that is elected the leader, if one was provided when the node registered for the role viaregister(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
getMaxPollTime
(TimeUnit timeUnit) Returns the maximum amount of time any poll of the leader election service has taken in the past 5 minutes.long
getMinPollTime
(TimeUnit timeUnit) 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 minutesboolean
isActiveParticipant
(String roleName) Indicates whether or not this node is an active participant in the election for the given roleboolean
Returns a boolean value indicating whether or not this node is the elected leader for the given rolevoid
register
(String roleName, LeaderElectionStateChangeListener listener) 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
start()
Starts managing leader elections for all registered rolesvoid
stop()
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
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 toisLeader(String)
andgetLeader(String)
to know which node is currently elected the leader.- Parameters:
roleName
- the name of the rolelistener
- a listener that will be called when the node gains or relinquishes the role of leader
-
register
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 isnull
, this node will never be elected leader but will passively observe changes to the leadership.- Parameters:
roleName
- the name of the rolelistener
- a listener that will be called when the node gains or relinquishes the role of leaderparticipantId
- 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 thegetLeader(String)
method
-
isActiveParticipant
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) orfalse
otherwise
-
getLeader
Returns the Participant ID of the node that is elected the leader, if one was provided when the node registered for the role viaregister(String, LeaderElectionStateChangeListener, String)
. If there is currently no leader known or if the role was registered without providing a Participant ID, this will returnnull
.- 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
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
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
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 durationtimeUnit
- the time unit- Returns:
- a Mapping of role to the number of times that the leader for that role has changed
-
getAveragePollTime
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
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
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
-