com.twitter.common.zookeeper
Class SingletonService

java.lang.Object
  extended by com.twitter.common.zookeeper.SingletonService

public class SingletonService
extends Object

A service that uses master election to only allow a single instance of the server to join the ServerSet at a time.


Nested Class Summary
static interface SingletonService.LeadershipListener
           
 
Constructor Summary
SingletonService(ZooKeeperClient zkClient, String servicePath)
          Equivalent to SingletonService(ZooKeeperClient, String, Iterable) with a default wide open acl (ZooDefs.Ids#OPEN_ACL_UNSAFE).
SingletonService(ZooKeeperClient zkClient, String servicePath, Iterable<org.apache.zookeeper.data.ACL> acl)
          Creates a new singleton service, identified by servicePath.
 
Method Summary
 void lead(InetSocketAddress endpoint, Map<String,InetSocketAddress> additionalEndpoints, Status status, SingletonService.LeadershipListener listener)
          Attempts to lead the singleton service.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SingletonService

public SingletonService(ZooKeeperClient zkClient,
                        String servicePath)
Equivalent to SingletonService(ZooKeeperClient, String, Iterable) with a default wide open acl (ZooDefs.Ids#OPEN_ACL_UNSAFE).


SingletonService

public SingletonService(ZooKeeperClient zkClient,
                        String servicePath,
                        Iterable<org.apache.zookeeper.data.ACL> acl)
Creates a new singleton service, identified by servicePath. All nodes related to the service (for both leader election and service registration) will live under the path and each node will be created with the supplied acl. Internally, two ZooKeeper Groups are used to manage a singleton service - one for leader election, and another for the ServerSet where the leader's endpoints are registered. Leadership election should guarantee that at most one instance will ever exist in the ServerSet at once.

Parameters:
zkClient - The ZooKeeper client to use.
servicePath - The path where service nodes live.
acl - The acl to apply to newly created candidate nodes and serverset nodes.
Method Detail

lead

public void lead(InetSocketAddress endpoint,
                 Map<String,InetSocketAddress> additionalEndpoints,
                 Status status,
                 SingletonService.LeadershipListener listener)
          throws Group.WatchException,
                 Group.JoinException,
                 InterruptedException
Attempts to lead the singleton service.

Parameters:
endpoint - The primary endpoint to register as a leader candidate in the service.
additionalEndpoints - Additional endpoints that are available on the host.
status - Current status of the candidate.
listener - Handler to call when the candidate is elected or defeated.
Throws:
Group.WatchException - If there was a problem watching the ZooKeeper group.
Group.JoinException - If there was a problem joining the ZooKeeper group.
InterruptedException - If the thread watching/joining the group was interrupted.