How to use kazoo client for leader election?

丶灬走出姿态 提交于 2019-12-03 12:06:44

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!