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,
org.apache.flink.runtime.rpc.FatalErrorHandler fallbackErrorHandler) |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
protected CompletableFuture<Void> |
confirmLeadershipAsync(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 CompletableFuture<Boolean> |
hasLeadershipAsync(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, org.apache.flink.runtime.rpc.FatalErrorHandler fallbackErrorHandler)
public LeaderElection createLeaderElection(String componentId)
LeaderElectionServiceLeaderElection instance that is registered to this LeaderElectionService instance.createLeaderElection in interface LeaderElectionServicecomponentId - 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.Exceptionprotected final void remove(String componentId) throws Exception
LeaderContender from the ParentService that is associated
with the componentId.Exceptionpublic void close()
throws Exception
close in interface AutoCloseableExceptionprotected CompletableFuture<Void> confirmLeadershipAsync(String componentId, UUID leaderSessionID, String leaderAddress)
leaderSessionID and leaderAddress for
the LeaderContender that is associated with the componentId. The
information is only propagated to the HA backend if the leadership is still acquired.protected CompletableFuture<Boolean> hasLeadershipAsync(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.ListeneronGrantLeadership in interface LeaderElectionDriver.Listenerpublic void onRevokeLeadership()
LeaderElectionDriver.ListeneronRevokeLeadership in interface LeaderElectionDriver.Listenerpublic void onLeaderInformationChange(String componentId, LeaderInformation leaderInformation)
LeaderElectionDriver.ListeneronLeaderInformationChange in interface LeaderElectionDriver.ListenercomponentId - identifying the component whose leader information has changedleaderInformation - new leader informationpublic void onLeaderInformationChange(LeaderInformationRegister changedLeaderInformation)
LeaderElectionDriver.ListeneronLeaderInformationChange in interface LeaderElectionDriver.Listenerpublic void onError(Throwable t)
LeaderElectionDriver.ListeneronError in interface LeaderElectionDriver.ListenerCopyright © 2014–2025 The Apache Software Foundation. All rights reserved.