public class ZooKeeperLeaderElectionService extends Object implements LeaderElectionService, org.apache.curator.framework.recipes.leader.LeaderLatchListener, org.apache.curator.framework.recipes.cache.NodeCacheListener, org.apache.curator.framework.api.UnhandledErrorListener
Constructor and Description |
---|
ZooKeeperLeaderElectionService(org.apache.curator.framework.CuratorFramework client,
String latchPath,
String leaderPath)
Creates a ZooKeeperLeaderElectionService object.
|
Modifier and Type | Method and Description |
---|---|
void |
confirmLeaderSessionID(UUID leaderSessionID)
Confirms that the new leader session ID has been successfully received by the new leader.
|
UUID |
getLeaderSessionID()
Returns the current leader session ID or null, if the contender is not the leader.
|
protected void |
handleStateChange(org.apache.curator.framework.state.ConnectionState newState) |
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 |
isLeader() |
void |
nodeChanged() |
void |
notLeader() |
void |
start(LeaderContender contender)
Starts the leader election service.
|
void |
stop()
Stops the leader election service.
|
String |
toString() |
void |
unhandledError(String message,
Throwable e) |
protected void |
writeLeaderInformation(UUID leaderSessionID)
Writes the current leader's address as well the given leader session ID to ZooKeeper.
|
public ZooKeeperLeaderElectionService(org.apache.curator.framework.CuratorFramework client, String latchPath, String leaderPath)
client
- Client which is connected to the ZooKeeper quorumlatchPath
- ZooKeeper node path for the leader election latchleaderPath
- ZooKeeper node path for the node which stores the current leader informationpublic UUID getLeaderSessionID()
public void start(LeaderContender contender) throws Exception
LeaderElectionService
start
in interface LeaderElectionService
contender
- LeaderContender which applies for the leadershipException
public void stop() throws Exception
LeaderElectionService
stop
in interface LeaderElectionService
Exception
public void confirmLeaderSessionID(UUID leaderSessionID)
LeaderElectionService
LeaderContender
.
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 to the
leader retrieval services.confirmLeaderSessionID
in interface LeaderElectionService
leaderSessionID
- The new leader session IDpublic 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 falsepublic void isLeader()
isLeader
in interface org.apache.curator.framework.recipes.leader.LeaderLatchListener
public void notLeader()
notLeader
in interface org.apache.curator.framework.recipes.leader.LeaderLatchListener
public void nodeChanged() throws Exception
nodeChanged
in interface org.apache.curator.framework.recipes.cache.NodeCacheListener
Exception
protected void writeLeaderInformation(UUID leaderSessionID)
leaderSessionID
- Leader session ID which is written to ZooKeeperprotected void handleStateChange(org.apache.curator.framework.state.ConnectionState newState)
public void unhandledError(String message, Throwable e)
unhandledError
in interface org.apache.curator.framework.api.UnhandledErrorListener
Copyright © 2014–2020 The Apache Software Foundation. All rights reserved.