zookeeper__leader选举——翻译官方recipes文档

本文介绍了一种基于ZooKeeper的简单领导选举方法,通过使用SEQUENCE|EPHEMERAL标志创建znode来标记客户端候选人。同时,文章还讨论了如何避免在领导变更时产生的“惊群”效应。

zookeeper-3.4.6/docs/recipes.html

Leader Election

A 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:

  1. Create znode z with path "ELECTION/n_" with both SEQUENCE and EPHEMERAL flags;

  2. Let C be the children of "ELECTION", and i be the sequence number of z;

  3. 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:

  1. Let C be the new set of children of ELECTION;

  2. If z is the smallest node in C, then execute leader procedure;

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

 

---------------------------------------------------------------------------------------------------------

上述的实现是有原java版本的。这个实现和我直观的想法不太一样,考虑得比较全面(特别是很多人频繁竞选时的惊群效应)

 

一个基于ZooKeeper的 简单的leader选举办法是使用 SEQUENCE|EPHEMERAL标志创建znode,用它来标记客户端候选人.思路是:

有一个znode如"/election",每一个znode创建带SEQUENCE|EPHEMERAL标志的子znode "/election/n_".因为使用SEQUENCE标志,ZooKeeper自增znode的序号,每次追加在"/election"最大子节点后面,即比前面任何请求的序号都大,哪些创建具有最小序号的znode的进程即为leader。

尽管如此,但不全然这么简单,监控leader节点失败是非常重要的。因此,当当前Leader失效,一个连接的客户端成为Leader。一个简易的解决方案是所有进程都去监控当前最小的znode,并检查它,当前最小znode被删除时,自己是否是新Leader。(当leader失效时,由于EPHEMERAL特性,其相应的znode会被删除)。但这会产生一个"惊群"现象(参见早起Linux的epoll惊群):当当前leader失效,其他所有的进程都被唤醒,并且在"/election"节点上执行getChildren来获得"/elcection"当前子节点列表.如果连接的客户端很多,ZooKeeper Server将较耗时。为了避免"惊群",是监控集群中比自己的小的最大的znode。(举例:

1,2,3,4,5;当前节点znode=4,那么4应该监控的是节点3,而不是最小节点1),这样就被避免了所有进程都监控最小节点。

伪代码如下:

设ELECTION 是应用选定的路径,所有候选人要竞选Leader:

 

  1. 创建zonde z ,其对应路径为 "ELECTION/n_"  创建标志(临时EPHEMERAL 、有序SEQUENCE  ,
  2. 设置C为"ELECTION"的子节点,而i为节点z(第一步创建节点成功时,会返回序号)
  3. 在节点"ELECTION/n_j"上设置监控,且j满足j= MAX({k|k<i,n_k ∈C}),即j是比i小但最接近i的节点,如果不存在这样的节点,那么当前节点i就是leader了
当收到znode被删除时的通知消息,

 

  1. 设C是最新的 "ELECTION"子节点集合
  2. 如果z是最小节点,那么z是leader,执行相应逻辑
  3. 否则,监控 "ELECTION/n_j"变化,同上 j= MAX({k|k<i,n_k ∈C})

 

基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制问题,并提供完整的Matlab代码实现。文章结合数据驱动法与Koopman算子理论,利用递归神经网络(RNN)对非线性系统进行建模与线性化处理,从而提升纳米级定位系统的精度与动态响应性能。该法通过提取系统隐含动态特征,构建近似线性模型,便于后续模型预测控制(MPC)的设计与优化,适用于高精度自动化控制场景。文中还展示了相关实验验证与仿真结果,证明了该法的有效性和先进性。; 适合人群:具备一定控制理论基础和Matlab编程能力,从事精密控制、智能制造、自动化或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能控制设计;②为非线性系统建模与线性化提供一种结合深度学习与现代控制理论的新思路;③帮助读者掌握Koopman算子、RNN建模与模型预测控制的综合应用。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现流程,重点关注数据预处理、RNN结构设计、Koopman观测矩阵构建及MPC控制器集成等关键环节,并可通过更换实际系统数据进行迁移验证,深化对法泛化能力的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值