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

问题描述

这是kazoo readthedocs上提到的代码

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

要传递哪些输入参数以使特定节点成为领导者?(即/electionpath 和 my-identifier 在这里指的是什么?)

解决方案

简而言之:"/electionpath"是您感兴趣的路径,您将在其中使用 dataWatchers 创建节点、添加数据和观察节点.我的标识符"是不可重入锁的标识符,用于验证谁是竞争者中的领导者,并只允许写入领导者.

详情:为了简化它首先解释为什么zookeeper中应该有领导者.领导者负责所有的写操作和连接相关的处理.考虑以下示例以了解领导者选举的概念.

  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',短暂=真)

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

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

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

  1. 创建id(czxid)最小的节点将被选举为leader post leader选举过程.

  2. Leader 选举在内部为所选节点(最小的 czxid)提供了一个不可重入锁,并为该锁设置了一些标识符,用于验证谁是竞争者中的领导者(你的我的-标识符").

现在让我们看看选举领导者的实际代码.

In [7]:election = zk_client.Election('/test_zk/path_of_interest', 'test-election')在 [8]: def leader_func():...:打印选举完成......!"...:在 [9] 中:选举.run(leader_func)选举完成……!

一个可调用对象被传递给 run 方法来做一些选举后的事情.

我希望这会有所帮助.

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

解决方案

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.
  1. Internally each node of cluster stores this ephemeral node information.
  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.

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

10-11 01:10