Class DefaultLeaderElectionService
- java.lang.Object
-
- org.apache.flink.runtime.leaderelection.DefaultLeaderElectionService
-
- All Implemented Interfaces:
AutoCloseable
,LeaderElectionDriver.Listener
,LeaderElectionService
public class DefaultLeaderElectionService extends Object implements LeaderElectionService, LeaderElectionDriver.Listener, AutoCloseable
Default implementation for leader election service. Composed with differentLeaderElectionDriver
, we could perform a leader election for the contender, and then persist the leader information to various storage.DefaultLeaderElectionService
handles a singleLeaderContender
.
-
-
Constructor Summary
Constructors Constructor Description DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory)
DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory, FatalErrorHandler fallbackErrorHandler)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
protected void
confirmLeadership(String componentId, UUID leaderSessionID, String leaderAddress)
LeaderElection
createLeaderElection(String componentId)
Creates a newLeaderElection
instance that is registered to thisLeaderElectionService
instance.UUID
getLeaderSessionID(String componentId)
Returns the current leader session ID for the givencomponentId
ornull
, if the session wasn't confirmed.protected boolean
hasLeadership(String componentId, UUID 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(String componentId, LeaderInformation leaderInformation)
Notifies the listener about a changed leader information for the given component.void
onLeaderInformationChange(LeaderInformationRegister changedLeaderInformation)
Notifies the listener about all currently known leader information.void
onRevokeLeadership()
Callback that is called once the driver loses the leadership.protected void
register(String componentId, LeaderContender contender)
protected void
remove(String componentId)
-
-
-
Constructor Detail
-
DefaultLeaderElectionService
public DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory)
-
DefaultLeaderElectionService
@VisibleForTesting public DefaultLeaderElectionService(LeaderElectionDriverFactory leaderElectionDriverFactory, FatalErrorHandler fallbackErrorHandler)
-
-
Method Detail
-
createLeaderElection
public LeaderElection createLeaderElection(String componentId)
Description copied from interface:LeaderElectionService
Creates a newLeaderElection
instance that is registered to thisLeaderElectionService
instance.- Specified by:
createLeaderElection
in interfaceLeaderElectionService
- Parameters:
componentId
- a unique identifier that refers to the stored leader information that the newly createdLeaderElection
manages.
-
register
protected void register(String componentId, LeaderContender contender) throws Exception
- Throws:
Exception
-
close
public void close() throws Exception
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
confirmLeadership
protected void confirmLeadership(String componentId, UUID leaderSessionID, String leaderAddress)
-
getLeaderSessionID
@VisibleForTesting @Nullable public UUID getLeaderSessionID(String componentId)
Returns the current leader session ID for the givencomponentId
ornull
, if the session wasn't confirmed.
-
onGrantLeadership
public void onGrantLeadership(UUID leaderSessionID)
Description copied from interface:LeaderElectionDriver.Listener
Callback that is called once the driver obtains the leadership.- Specified by:
onGrantLeadership
in interfaceLeaderElectionDriver.Listener
-
onRevokeLeadership
public void onRevokeLeadership()
Description copied from interface:LeaderElectionDriver.Listener
Callback that is called once the driver loses the leadership.- Specified by:
onRevokeLeadership
in interfaceLeaderElectionDriver.Listener
-
onLeaderInformationChange
public void onLeaderInformationChange(String componentId, LeaderInformation leaderInformation)
Description copied from interface:LeaderElectionDriver.Listener
Notifies the listener about a changed leader information for the given component.- Specified by:
onLeaderInformationChange
in interfaceLeaderElectionDriver.Listener
- Parameters:
componentId
- identifying the component whose leader information has changedleaderInformation
- new leader information
-
onLeaderInformationChange
public void onLeaderInformationChange(LeaderInformationRegister changedLeaderInformation)
Description copied from interface:LeaderElectionDriver.Listener
Notifies the listener about all currently known leader information.- Specified by:
onLeaderInformationChange
in interfaceLeaderElectionDriver.Listener
-
onError
public void onError(Throwable t)
Description copied from interface:LeaderElectionDriver.Listener
Notifies the listener if an error occurred.- Specified by:
onError
in interfaceLeaderElectionDriver.Listener
-
-