如何使用kazoo客户进行领导者选举? [英] How to use kazoo client for leader election?

查看:121
本文介绍了如何使用kazoo客户进行领导者选举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是kazoo readthedocs上提到的代码

This is the code mentioned on kazoo readthedocs

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

要使特定节点作为领导者要传递的输入参数是什么? (即/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?)

推荐答案

简而言之:"/electionpath"是您感兴趣的路径,您将在其中创建节点,添加数据并使用dataWatchers监视节点. "my-identifier"是非重入锁的标识符,它将用于验证谁是竞争者中的领导者,并仅允许对领导者进行写操作.

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是集群中可用的服务器(以Zookeeper术语表示的节点).
  2. '/test_zk/path_of_interest/'(您的"/electionpath")是我感兴趣的路径,我将在其中创建节点,添加数据并使用dataWatchers监视节点.
  3. 在此路径下创建临时节点.

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

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

  1. 集群内部的每个节点都存储此临时节点信息.

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

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

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

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. 具有最小创建id(czxid)的节点将被选为领导者职位领导者选举过程.

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

领导者选举内部向选举出的节点提供了一个非重入锁(最小czxid),并为该锁设置了一些标识符,该标识符将用于验证谁是竞争者中的领导者(您的我的标识符" ).

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...!

可调用对象传递给run方法以执行一些选举后的工作.

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

我希望这会有所帮助.

这篇关于如何使用kazoo客户进行领导者选举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆