Class SingletonService

  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<> 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


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


public SingletonService(ZooKeeperClient zkClient,
                        String servicePath,
                        Iterable<> 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.

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


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

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.
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.