public class DefaultLeaderElectionService extends Object implements LeaderElectionService, LeaderElectionEventHandler
LeaderElectionDriver, we could perform a leader election for the contender, and then persist the
leader information to various storage.| Constructor and Description |
|---|
DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory) |
| Modifier and Type | Method and Description |
|---|---|
void |
confirmLeadership(UUID leaderSessionID,
String leaderAddress)
Confirms that the
LeaderContender has accepted the leadership identified by the given
leader session id. |
UUID |
getLeaderSessionID()
Returns the current leader session ID or null, if the contender is not the leader.
|
boolean |
hasLeadership(UUID leaderSessionId)
Returns true if the
LeaderContender with which the service has been started owns
currently the leadership under the given leader session id. |
void |
onGrantLeadership(UUID newLeaderSessionId)
Called by specific
LeaderElectionDriver when the leadership is granted. |
void |
onLeaderInformationChange(LeaderInformation leaderInformation)
Called by specific
LeaderElectionDriver when the leader information is changed. |
void |
onRevokeLeadership()
Called by specific
LeaderElectionDriver when the leadership is revoked. |
void |
start(LeaderContender contender)
Starts the leader election service.
|
void |
stop()
Stops the leader election service.
|
public DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory)
public final void start(LeaderContender contender) throws Exception
LeaderElectionServicestart in interface LeaderElectionServicecontender - LeaderContender which applies for the leadershipExceptionpublic final void stop()
throws Exception
LeaderElectionServicestop in interface LeaderElectionServiceExceptionpublic void confirmLeadership(UUID leaderSessionID, String leaderAddress)
LeaderElectionServiceLeaderContender has accepted the leadership identified by the given
leader session id. It also publishes the leader address under which the leader is reachable.
The rational behind this method is to establish an order between setting the new leader
session ID in the LeaderContender and publishing the new leader session ID as well as
the leader address to the leader retrieval services.
confirmLeadership in interface LeaderElectionServiceleaderSessionID - The new leader session IDleaderAddress - The address of the new leaderpublic boolean hasLeadership(@Nonnull UUID leaderSessionId)
LeaderElectionServiceLeaderContender with which the service has been started owns
currently the leadership under the given leader session id.hasLeadership in interface LeaderElectionServiceleaderSessionId - identifying the current leaderLeaderContender is the leader, otherwise false@VisibleForTesting @Nullable public UUID getLeaderSessionID()
public void onGrantLeadership(UUID newLeaderSessionId)
LeaderElectionEventHandlerLeaderElectionDriver when the leadership is granted.onGrantLeadership in interface LeaderElectionEventHandlernewLeaderSessionId - the valid leader session idpublic void onRevokeLeadership()
LeaderElectionEventHandlerLeaderElectionDriver when the leadership is revoked.onRevokeLeadership in interface LeaderElectionEventHandlerpublic void onLeaderInformationChange(LeaderInformation leaderInformation)
LeaderElectionEventHandlerLeaderElectionDriver when the leader information is changed. Then
the LeaderElectionService could write the leader information again if necessary. This
method should only be called when LeaderElectionDriver.hasLeadership() is true.
Duplicated leader change events could happen, so the implementation should check whether the
passed leader information is really different with internal confirmed leader information.onLeaderInformationChange in interface LeaderElectionEventHandlerleaderInformation - leader information which contains leader session id and leader
address.Copyright © 2014–2022 The Apache Software Foundation. All rights reserved.