ZooKeeper选主的过程

ZooKeeper - Leader Election
Zookeeper—选主

Let us analyze how a leader node can be elected in a ZooKeeper ensemble. Consider there are N number of nodes in a cluster. The process of leader election is as follows −
假设我们现在有一个N主机节点的集群,我们来分析ZooKeeper集群是如何选取一个leader节点。步骤如下:

All the nodes create a sequential, ephemeral znode with the same path, /app/leader_election/guid_.
所有主机节点在同一个目录下创建一个顺序临时z节点。

ZooKeeper ensemble will append the 10-digit sequence number to the path and the znode created will be /app/leader_election/guid_0000000001, /app/leader_election/guid_0000000002, etc.
Zookeeper会在路径后添加10位顺序号。

For a given instance, the node which creates the smallest number in the znode becomes the leader and all the other nodes are followers.
这个情况下,z节点中顺序号最小的主机会被成为leader,其他的节点都是follower。

Each follower node watches the znode having the next smallest number. For example, the node which creates znode /app/leader_election/guid_0000000008 will watch the znode /app/leader_election/guid_0000000007 and the node which creates the znode /app/leader_election/guid_0000000007 will watch the znode /app/leader_election/guid_0000000006.
每一台follower主机节点监控下一个具有最小顺序号的z节点。比如,创建/app/leader_election/guid_0000000008的主机节点会对/app/leader_election/guid_0000000007 z节点进行监控,创建/app/leader_election/guid_0000000007的主机节点会对/app/leader_election/guid_0000000006 z节点进行监控。

If the leader goes down, then its corresponding znode /app/leader_electionN gets deleted.
如果leader宕机了,对应的z节点/app/leader_electionN会被删除。

The next in line follower node will get the notification through watcher about the leader removal.
按顺序下一个follower主机节点会通过对leader注册的监控得到leader已经移除的消息。

The next in line follower node will check if there are other znodes with the smallest number. If none, then it will assume the role of the leader. Otherwise, it finds the node which created the znode with the smallest number as leader.
按顺序下一个follower主机节点会检查是否还有别的具有最小顺序号的z节点。如果没有,他就会成为新的leader。否则,就会查找创建最小z节点的主机节点作为新的leader。

Similarly, all other follower nodes elect the node which created the znode with the smallest number as leader.
同样的,所有其他follower节点也会选择创建最小顺序号z节点的主机节点作为leader。

Leader election is a complex process when it is done from scratch. But ZooKeeper service makes it very simple. Let us move on to the installation of ZooKeeper for development purpose in the next chapter.
当从宕机中恢复时,选择leader是一个很复杂的过程。但是ZooKeeper服务使之变得十分简单。下一章我们将安装ZooKeeper并进行开发。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值