com.twitter.common.zookeeper
Interface Candidate

All Known Implementing Classes:
CandidateImpl

public interface Candidate

Interface definition for becoming or querying for a ZooKeeper-based group leader.


Nested Class Summary
static interface Candidate.Leader
          Encapsulates a leader that can be elected and subsequently defeated.
 
Method Summary
 String getLeaderId()
          Returns the current group leader by querying ZooKeeper synchronously.
 Supplier<Boolean> offerLeadership(Candidate.Leader leader)
          Offers this candidate in leadership elections for as long as the current jvm process is alive.
 

Method Detail

getLeaderId

String getLeaderId()
                   throws ZooKeeperClient.ZooKeeperConnectionException,
                          org.apache.zookeeper.KeeperException,
                          InterruptedException
Returns the current group leader by querying ZooKeeper synchronously.

Returns:
the id of the current group leader or null if there is none
Throws:
ZooKeeperClient.ZooKeeperConnectionException - if there was a problem connecting to ZooKeeper
org.apache.zookeeper.KeeperException - if there was a problem reading the leader information
InterruptedException - if this thread is interrupted getting the leader

offerLeadership

Supplier<Boolean> offerLeadership(Candidate.Leader leader)
                                  throws Group.JoinException,
                                         Group.WatchException,
                                         InterruptedException
Offers this candidate in leadership elections for as long as the current jvm process is alive. Upon election, the onElected callback will be executed and a command that can be used to abdicate leadership will be passed in. If the elected leader jvm process dies or the elected leader successfully abdicates then a new leader will be elected. Leaders that successfully abdicate are removed from the group and will not be eligible for leadership election unless offerLeadership(Leader) is called again.

Parameters:
leader - the leader to notify of election and defeat events
Returns:
a supplier that can be queried to find out if this leader is currently elected
Throws:
Group.JoinException - if there was a problem joining the group
Group.WatchException - if there is a problem generating the 1st group membership list
InterruptedException - if interrupted waiting to join the group and determine initial election results