public class DefaultLeaderElectionService extends Object implements LeaderElectionService, LeaderElectionDriver.Listener, AutoCloseable
LeaderElectionDriver
, we could perform a leader election for the contender, and then persist the
leader information to various storage.
DefaultLeaderElectionService
handles a single LeaderContender
.
Constructor and Description |
---|
DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory) |
DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory,
FatalErrorHandler fallbackErrorHandler) |
Modifier and Type | Method and Description |
---|---|
void |
close() |
protected void |
confirmLeadership(String componentId,
UUID leaderSessionID,
String leaderAddress)
Confirms the leadership with the
leaderSessionID and leaderAddress for
the LeaderContender that is associated with the componentId . |
LeaderElection |
createLeaderElection(String componentId)
Creates a new
LeaderElection instance that is registered to this LeaderElectionService instance. |
UUID |
getLeaderSessionID(String componentId)
Returns the current leader session ID for the given
componentId or null , if
the session wasn't confirmed. |
protected boolean |
hasLeadership(String componentId,
UUID leaderSessionId)
Checks whether the
ParentService has the leadership acquired for the componentId and leaderSessionID . |
void |
onError(Throwable t)
Notifies the listener if an error occurred.
|
void |
onGrantLeadership(UUID leaderSessionID)
Callback that is called once the driver obtains the leadership.
|
void |
onLeaderInformationChange(LeaderInformationRegister changedLeaderInformation)
Notifies the listener about all currently known leader information.
|
void |
onLeaderInformationChange(String componentId,
LeaderInformation leaderInformation)
Notifies the listener about a changed leader information for the given component.
|
void |
onRevokeLeadership()
Callback that is called once the driver loses the leadership.
|
protected void |
register(String componentId,
LeaderContender contender)
|
protected void |
remove(String componentId)
Removes the
LeaderContender from the ParentService that is associated
with the componentId . |
public DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory)
@VisibleForTesting public DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory, FatalErrorHandler fallbackErrorHandler)
public LeaderElection createLeaderElection(String componentId)
LeaderElectionService
LeaderElection
instance that is registered to this LeaderElectionService
instance.createLeaderElection
in interface LeaderElectionService
componentId
- a unique identifier that refers to the stored leader information that the
newly created LeaderElection
manages.protected void register(String componentId, LeaderContender contender) throws Exception
LeaderContender
under the componentId
with the underlying
ParentService
. Leadership changes are starting to be reported to the LeaderContender
.Exception
protected final void remove(String componentId) throws Exception
LeaderContender
from the ParentService
that is associated
with the componentId
.Exception
public void close() throws Exception
close
in interface AutoCloseable
Exception
protected void confirmLeadership(String componentId, UUID leaderSessionID, String leaderAddress)
leaderSessionID
and leaderAddress
for
the LeaderContender
that is associated with the componentId
.protected boolean hasLeadership(String componentId, UUID leaderSessionId)
ParentService
has the leadership acquired for the componentId
and leaderSessionID
.true
if the service has leadership with the passed leaderSessionID
acquired; false
otherwise.@VisibleForTesting @Nullable public UUID getLeaderSessionID(String componentId)
componentId
or null
, if
the session wasn't confirmed.public void onGrantLeadership(UUID leaderSessionID)
LeaderElectionDriver.Listener
onGrantLeadership
in interface LeaderElectionDriver.Listener
public void onRevokeLeadership()
LeaderElectionDriver.Listener
onRevokeLeadership
in interface LeaderElectionDriver.Listener
public void onLeaderInformationChange(String componentId, LeaderInformation leaderInformation)
LeaderElectionDriver.Listener
onLeaderInformationChange
in interface LeaderElectionDriver.Listener
componentId
- identifying the component whose leader information has changedleaderInformation
- new leader informationpublic void onLeaderInformationChange(LeaderInformationRegister changedLeaderInformation)
LeaderElectionDriver.Listener
onLeaderInformationChange
in interface LeaderElectionDriver.Listener
public void onError(Throwable t)
LeaderElectionDriver.Listener
onError
in interface LeaderElectionDriver.Listener
Copyright © 2014–2023 The Apache Software Foundation. All rights reserved.