com.twitter.common.zookeeper
Class CandidateImpl

java.lang.Object
  extended by com.twitter.common.zookeeper.CandidateImpl
All Implemented Interfaces:
Candidate

public class CandidateImpl
extends Object
implements Candidate

Implements leader election for small groups of candidates. This implementation is subject to the herd effect for a given group and should only be used for small (~10 member) candidate pools.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.twitter.common.zookeeper.Candidate
Candidate.Leader
 
Constructor Summary
CandidateImpl(Group group)
          Equivalent to CandidateImpl(Group, com.google.common.base.Function) using a judge that always picks the lowest numbered candidate ephemeral node - by proxy the oldest or 1st candidate.
CandidateImpl(Group group, Function<Iterable<String>,String> judge)
          Creates a candidate that can be used to offer leadership for the given group.
 
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CandidateImpl

public CandidateImpl(Group group)
Equivalent to CandidateImpl(Group, com.google.common.base.Function) using a judge that always picks the lowest numbered candidate ephemeral node - by proxy the oldest or 1st candidate.


CandidateImpl

public CandidateImpl(Group group,
                     Function<Iterable<String>,String> judge)
Creates a candidate that can be used to offer leadership for the given group. The judge is used to pick the current leader from all group members whenever the group membership changes. To form a well-behaved election group with one leader, all candidates should use the same judge.

Method Detail

getLeaderId

public String getLeaderId()
                   throws ZooKeeperClient.ZooKeeperConnectionException,
                          org.apache.zookeeper.KeeperException,
                          InterruptedException
Description copied from interface: Candidate
Returns the current group leader by querying ZooKeeper synchronously.

Specified by:
getLeaderId in interface Candidate
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

public Supplier<Boolean> offerLeadership(Candidate.Leader leader)
                                  throws Group.JoinException,
                                         Group.WatchException,
                                         InterruptedException
Description copied from interface: Candidate
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 Candidate.offerLeadership(Leader) is called again.

Specified by:
offerLeadership in interface Candidate
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