datadev_sh 发表于 2018-4-10 16:29:34

zookeeper 关于“Leader Election”为什么看不到 /election 目录

zookeeper官方文档说选举时,会创建 /electio ,然后选序号最小的最为leader。
这段文档基本没看明白。

https://zookeeper.apache.org/doc/current/recipes.html(Leader Election在这个页面最下面)



Leader ElectionA simple way of doing leader election with ZooKeeper is to use the SEQUENCE|EPHEMERAL flags when creating znodes that represent "proposals" of clients. The idea is to have a znode, say "/election", such that each znode creates a child znode "/election/n_" with both flags SEQUENCE|EPHEMERAL. With the sequence flag, ZooKeeper automatically appends a sequence number that is greater that any one previously appended to a child of "/election". The process that created the znode with the smallest appended sequence number is the leader.That's not all, though. It is important to watch for failures of the leader, so that a new client arises as the new leader in the case the current leader fails. A trivial solution is to have all application processes watching upon the current smallest znode, and checking if they are the new leader when the smallest znode goes away (note that the smallest znode will go away if the leader fails because the node is ephemeral). But this causes a herd effect: upon of failure of the current leader, all other processes receive a notification, and execute getChildren on "/election" to obtain the current list of children of "/election". If the number of clients is large, it causes a spike on the number of operations that ZooKeeper servers have to process. To avoid the herd effect, it is sufficient to watch for the next znode down on the sequence of znodes. If a client receives a notification that the znode it is watching is gone, then it becomes the new leader in the case that there is no smaller znode. Note that this avoids the herd effect by not having all clients watching the same znode.Here's the pseudo code:Let ELECTION be a path of choice of the application. To volunteer to be a leader:
[*]Create znode z with path "ELECTION/n_" with both SEQUENCE and EPHEMERAL flags;
[*]Let C be the children of "ELECTION", and i be the sequence number of z;
[*]Watch for changes on "ELECTION/n_j", where j is the largest sequence number such that j < i and n_j is a znode in C;
Upon receiving a notification of znode deletion:
[*]Let C be the new set of children of ELECTION;
[*]If z is the smallest node in C, then execute leader procedure;
[*]Otherwise, watch for changes on "ELECTION/n_j", where j is the largest sequence number such that j < i and n_j is a znode in C;
Note that the znode having no preceding znode on the list of children does not imply that the creator of this znode is aware that it is the current leader. Applications may consider creating a separate znode to acknowledge that the leader has executed the leader procedure.



datadev_sh 发表于 2018-4-12 19:10:09

我知道了。

这里说的选举,是说我们的程序如果用zk来做选举的方法。这段文字的第一句话说了。“A simple way of doing leader election with ZooKeeper ”。而不是说,zk自己在选举时,会用这个方法。

nextuser 发表于 2018-4-10 21:05:35

这不是什么问题。
查看下子目录,里面还有的目录的。你看到的应该是第一层目录。
推荐参考
http://www.aboutyun.com/home.php?mod=space&uid=1330&do=blog&id=3537
页: [1]
查看完整版本: zookeeper 关于“Leader Election”为什么看不到 /election 目录