You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@zookeeper.apache.org by Arindam Mukherjee <ar...@gmail.com> on 2013/11/07 06:38:31 UTC

Electing a leader among clients

Using ZooKeeper I want to create a cluster of hosts where a client process
on one of the hosts can play the role of a leader or controller and
coordinate tasks with processes across the rest of the hosts. As the nodes
are hosts similar, the choice of a leader can be arbitrary. I was thinking
of using ZooKeeper for this purpose.

I am very new to ZooKeeper and don't know enough of the available
primitives. What would be a good way to achieve this?

Here is one thought - please let me know if this makes sense or something
else which would work better:
1. Each ZoooKeeper client gets a monotonically increasing ID when it
establishes a session with ZooKeeper server. One way could be to create an
ephemeral sequential znode and use the assigned numeric suffix as the id -
store other data in this znode.
2. Whichever client gets the lowest id becomes the leader. However, for
this to work the entire range of ids assigned to live clients at any point
in time should be available to all the clients so that each client can
independently arrive at the same decision. How to do this?

Thanks.
Arindam

RE: Electing a leader among clients

Posted by Rakesh R <ra...@huawei.com>.
Another approach would be, 

Step-1) All the clients will try to create a ephemeral znode, say 'leader' under one common path /myapp/leaderelection/ 
Step-2) Whoever succeeds will become leader and all the others will be getting KeeperException.NodeExistsException. On this exception, clients can add a watcher to know the 'leader' znode deletion.
Step-3) On znode delete watch notification, again, this cycle continues and perform step-1.

-Rakesh

-----Original Message-----
From: Arindam Mukherjee [mailto:arindam.mukerjee@gmail.com] 
Sent: 07 November 2013 11:09
To: user@zookeeper.apache.org
Subject: Electing a leader among clients

Using ZooKeeper I want to create a cluster of hosts where a client process on one of the hosts can play the role of a leader or controller and coordinate tasks with processes across the rest of the hosts. As the nodes are hosts similar, the choice of a leader can be arbitrary. I was thinking of using ZooKeeper for this purpose.

I am very new to ZooKeeper and don't know enough of the available primitives. What would be a good way to achieve this?

Here is one thought - please let me know if this makes sense or something else which would work better:
1. Each ZoooKeeper client gets a monotonically increasing ID when it establishes a session with ZooKeeper server. One way could be to create an ephemeral sequential znode and use the assigned numeric suffix as the id - store other data in this znode.
2. Whichever client gets the lowest id becomes the leader. However, for this to work the entire range of ids assigned to live clients at any point in time should be available to all the clients so that each client can independently arrive at the same decision. How to do this?

Thanks.
Arindam

Re: Electing a leader among clients

Posted by Cameron McKenzie <mc...@gmail.com>.
The Curator framework provides an implementation of leader election. I
think that it will do what you need it to do without having to write any
code yourself.

http://curator.apache.org/curator-recipes/leader-latch.html


On Thu, Nov 7, 2013 at 4:38 PM, Arindam Mukherjee <
arindam.mukerjee@gmail.com> wrote:

> Using ZooKeeper I want to create a cluster of hosts where a client process
> on one of the hosts can play the role of a leader or controller and
> coordinate tasks with processes across the rest of the hosts. As the nodes
> are hosts similar, the choice of a leader can be arbitrary. I was thinking
> of using ZooKeeper for this purpose.
>
> I am very new to ZooKeeper and don't know enough of the available
> primitives. What would be a good way to achieve this?
>
> Here is one thought - please let me know if this makes sense or something
> else which would work better:
> 1. Each ZoooKeeper client gets a monotonically increasing ID when it
> establishes a session with ZooKeeper server. One way could be to create an
> ephemeral sequential znode and use the assigned numeric suffix as the id -
> store other data in this znode.
> 2. Whichever client gets the lowest id becomes the leader. However, for
> this to work the entire range of ids assigned to live clients at any point
> in time should be available to all the clients so that each client can
> independently arrive at the same decision. How to do this?
>
> Thanks.
> Arindam
>