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
LeaderElectionService
start
in interface LeaderElectionService
contender
- LeaderContender which applies for the leadershipException
public final void stop() throws Exception
LeaderElectionService
stop
in interface LeaderElectionService
Exception
public void confirmLeadership(UUID leaderSessionID, String leaderAddress)
LeaderElectionService
LeaderContender
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 LeaderElectionService
leaderSessionID
- The new leader session IDleaderAddress
- The address of the new leaderpublic boolean hasLeadership(@Nonnull UUID leaderSessionId)
LeaderElectionService
LeaderContender
with which the service has been started owns
currently the leadership under the given leader session id.hasLeadership
in interface LeaderElectionService
leaderSessionId
- identifying the current leaderLeaderContender
is the leader, otherwise false@VisibleForTesting @Nullable public UUID getLeaderSessionID()
public void onGrantLeadership(UUID newLeaderSessionId)
LeaderElectionEventHandler
LeaderElectionDriver
when the leadership is granted.onGrantLeadership
in interface LeaderElectionEventHandler
newLeaderSessionId
- the valid leader session idpublic void onRevokeLeadership()
LeaderElectionEventHandler
LeaderElectionDriver
when the leadership is revoked.onRevokeLeadership
in interface LeaderElectionEventHandler
public void onLeaderInformationChange(LeaderInformation leaderInformation)
LeaderElectionEventHandler
LeaderElectionDriver
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 LeaderElectionEventHandler
leaderInformation
- leader information which contains leader session id and leader
address.Copyright © 2014–2023 The Apache Software Foundation. All rights reserved.