Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use kazoo client for leader election?

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

like image 689
shan Avatar asked Aug 24 '16 13:08

shan


1 Answers

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.

like image 146
Akshay Avatar answered Sep 19 '22 09:09

Akshay