Package org.apache.storm.zookeeper
Class LeaderElectorImp
- java.lang.Object
-
- org.apache.storm.zookeeper.LeaderElectorImp
-
- All Implemented Interfaces:
AutoCloseable
,ILeaderElector
public class LeaderElectorImp extends Object implements ILeaderElector
-
-
Constructor Summary
Constructors Constructor Description LeaderElectorImp(org.apache.storm.shade.org.apache.curator.framework.CuratorFramework zk, String id, LeaderListenerCallbackFactory leaderListenerCallbackFactory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addToLeaderLockQueue()
queue up for leadership lock.boolean
awaitLeadership(long timeout, TimeUnit timeUnit)
Wait for the caller to gain leadership.void
close()
Method called to allow for cleanup.List<NimbusInfo>
getAllNimbuses()
Get list of current nimbus addresses.NimbusInfo
getLeader()
Get the current leader's address.boolean
isLeader()
Decide if the caller currently has the leader lock.void
prepare(Map<String,Object> conf)
Method guaranteed to be called as part of initialization of leader elector instance.void
quitElectionFor(int delayMs)
Removes the caller from leadership election, relinquishing leadership if acquired, then requeues for leadership after the specified delay.
-
-
-
Constructor Detail
-
LeaderElectorImp
public LeaderElectorImp(org.apache.storm.shade.org.apache.curator.framework.CuratorFramework zk, String id, LeaderListenerCallbackFactory leaderListenerCallbackFactory)
-
-
Method Detail
-
prepare
public void prepare(Map<String,Object> conf)
Description copied from interface:ILeaderElector
Method guaranteed to be called as part of initialization of leader elector instance.- Specified by:
prepare
in interfaceILeaderElector
- Parameters:
conf
- configuration
-
addToLeaderLockQueue
public void addToLeaderLockQueue() throws Exception
Description copied from interface:ILeaderElector
queue up for leadership lock. The call returns immediately and the caller must check isLeader() to perform any leadership action. This method can be called multiple times so it needs to be idempotent.- Specified by:
addToLeaderLockQueue
in interfaceILeaderElector
- Throws:
Exception
-
quitElectionFor
public void quitElectionFor(int delayMs) throws Exception
Description copied from interface:ILeaderElector
Removes the caller from leadership election, relinquishing leadership if acquired, then requeues for leadership after the specified delay.- Specified by:
quitElectionFor
in interfaceILeaderElector
- Parameters:
delayMs
- The delay to wait before re-entering the election- Throws:
Exception
-
isLeader
public boolean isLeader() throws Exception
Description copied from interface:ILeaderElector
Decide if the caller currently has the leader lock.- Specified by:
isLeader
in interfaceILeaderElector
- Returns:
- true if the caller currently has the leader lock.
- Throws:
Exception
-
awaitLeadership
public boolean awaitLeadership(long timeout, TimeUnit timeUnit) throws InterruptedException
Description copied from interface:ILeaderElector
Wait for the caller to gain leadership. This should only be used in single-Nimbus clusters, and is only useful to allow testing code to wait for a LocalCluster's Nimbus to gain leadership before trying to submit topologies.- Specified by:
awaitLeadership
in interfaceILeaderElector
- Returns:
- true is leadership was acquired, false otherwise
- Throws:
InterruptedException
-
getLeader
public NimbusInfo getLeader()
Description copied from interface:ILeaderElector
Get the current leader's address.- Specified by:
getLeader
in interfaceILeaderElector
- Returns:
- the current leader's address, may return null if no one has the lock.
-
getAllNimbuses
public List<NimbusInfo> getAllNimbuses() throws Exception
Description copied from interface:ILeaderElector
Get list of current nimbus addresses.- Specified by:
getAllNimbuses
in interfaceILeaderElector
- Returns:
- list of current nimbus addresses, includes leader.
- Throws:
Exception
-
close
public void close() throws Exception
Description copied from interface:ILeaderElector
Method called to allow for cleanup. Relinquishes leadership if owned by the caller.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceILeaderElector
- Throws:
Exception
-
-