Skip to content

Leader Election

Randgalt edited this page Aug 11, 2011 · 10 revisions

Description

In distributed computing, leader election is the process of designating a single process as the organizer of some task distributed among several computers (nodes). Before the task is begun, all network nodes are unaware which node will serve as the "leader," or coordinator, of the task. After a leader election algorithm has been run, however, each node throughout the network recognizes a particular, unique node as the task leader.

Participating Classes

  • LeaderSelector
  • LeaderSelectorListener

Usage

Creating a LeaderSelector

public LeaderSelector(CuratorFramework client,
String mutexPath,
LeaderSelectorListener listener)
Parameters:
client - the client
mutexPath - the path for this leadership group
listener - listener

public LeaderSelector(CuratorFramework client,
String mutexPath,
ThreadFactory threadFactory,
Executor executor,
LeaderSelectorListener listener)
Parameters:
client - the client
mutexPath - the path for this leadership group
threadFactory - factory to use for making internal threads
executor - the executor to run in
listener - listener

General Usage

LeaderSelectors must be started:

leaderSelector.start();
Once started, the takeLeadership() of your listener will be called when you have leadership. This method should only return when leadership is being relinquished.

When you are through with the LeaderSelector instance, you should call close:

leaderSelector.close();
Clone this wiki locally