How to use kazoo client for leader election?
Asked Answered
M

1

10

This is the code mentioned on kazoo readthedocs

election=zk.Election("/electionpath", "my-identifier")

what are the input arguments to be passed to make particular node as leader? (i.e what does /electionpath and my-identifier refers here?)

Mont answered 24/8, 2016 at 13:45 Comment(0)
U
5

In Short: "/electionpath" is your path of interest where you will be creating nodes, adding data and watching nodes using dataWatchers. "my-identifier" is identifier to the non re-entrant lock which will be used to verify who is the leader out of the contenders and allow writes only to leader.

In Detail: To simplify it explaining first why there should be leader in zookeeper. It is the leader who does all the write operations and connection related handling. Consider following example to understand concept of leader election.

  1. A, B, C are the available servers(nodes in zookeeper terms) under my cluster.
  2. '/test_zk/path_of_interest/'(your "/electionpath") is my path of interest where I will be creating nodes, adding data and watching nodes using dataWatchers.
  3. Create ephemeral node under this path.

In [1]: zk_client.create('test_zk/path_of_interest/test_ephemeral', ephemeral=True)

  1. Internally each node of cluster stores this ephemeral node information.

In [9]: zk_client.get("test_zk/path_of_interest/test_ephemeral")

Out [9]: ('',ZnodeStat(czxid=678608988239, mzxid=687195015354, ctime=1476960597584, mtime=1477310417594, version=1145, cversion=0, aversion=0, ephemeralOwner=0, dataLength=185, numChildren=0, pzxid=678608988239))

  1. The node with the smallest creation id(czxid) will be elected as leader post leader election process.

  2. Leader election internally gives a non re-entrant lock to elected node(smallest czxid) and sets some identifier to that lock which will be used to verify who is the leader out of the contenders(your "my-identifier").

Now let's see actual code to elect leader.

In [7]: election = zk_client.Election('/test_zk/path_of_interest', 'test-election')

In [8]: def leader_func():
   ...:     print 'Election Completed...!'
   ...:     

In [9]: election.run(leader_func)
Election Completed...!

A callable is passed to run method to do some post election stuff.

I hope this helps.

Unsought answered 24/10, 2016 at 13:21 Comment(2)
How do I get who the current leader is? Is it possible to give an example with two or more nodes?Immobility
@Aditya, for election, sequential node is created for the given path, assuming the path is '/job', suppose we have three node distributed system. Each node will try creating a sequential node(zookeeper node) with the path '/job'. As it is a sequential node(zookeeper node), zookeeper will automatically assign a sequence number, so which ever instance node gets the lowest sequence number will be chosen as leader. To query which node is the current leader, query the command line of zookeeper with the path /job, then get the node information with path created with the lowest sequence number.Sholeen

© 2022 - 2024 — McMap. All rights reserved.