简体   繁体   English

Zookeeper 多领导选举问题

[英]Zookeeper multiple leader election issue

I have a distributed application that uses ZooKeeper for leader election.我有一个使用 ZooKeeper 进行领导选举的分布式应用程序。 Only the elected leader can commit to the database. Only the elected leader can commit to the database. I recently discovered that there is a potential situation which could lead to multiple leaders.我最近发现有一种潜在的情况可能会导致多个领导者。 The situation arises when the elected leader is paused for a long GC and can lose the heartbeat to the ZooKeeper, leading to the election of a new leader. The situation arises when the elected leader is paused for a long GC and can lose the heartbeat to the ZooKeeper, leading to the election of a new leader. At this point, both the nodes think themselves to be the leader and can lead to conflict.此时,两个节点都认为自己是领导者,可能会导致冲突。

Any suggestions on how to avoid such situation ?关于如何避免这种情况的任何建议?

When you use ZooKeeper for leader election you can't guarantee uniqueness of the leader .It's possible to run into this situation even without GC pauses. 当您使用ZooKeeper进行领导者选举时,您无法保证领导者独特性。即使没有GC暂停,也可能遇到这种情况。 For example, when a leader is isolated from the ZooKeeper quorum during a network partitioning or when a leader issues a long running query, dies and a new leader can issue a new query while the current is still active. 例如,当在网络分区期间领导者与ZooKeeper仲裁隔离时,或者当领导者发出长时间运行的查询时,死亡和新领导者可以在当前仍处于活动状态时发出新查询。

The workaround is to use compare-and-set when you update the database. 解决方法是在更新数据库时使用比较和设置。 Once new leader is elected you should get an increasing leader id (eg by updating a node in ZooKeeper and using its version or mzxid) and use it to guard each transaction issued by that leader. 一旦选出新的领导者,您应该获得越来越多的领导者ID(例如,通过更新ZooKeeper中的节点并使用其版本或mzxid)并使用它来保护该领导者发布的每个交易。

For example if you want to change the state of the db then instead of the following transaction: 例如,如果要更改db的状态,则代替以下事务:

BEGIN TRANSACTION;
db.update($change);
END TRANSACTION;

you should use something like 你应该使用类似的东西

BEGIN TRANSACTION;
if (db.leaderID <= $leaderID) {
    db.leaderID = $leaderID;
    db.update($change);
}
END TRANSACTION;

This trick will protect your system from uncertainties caused by concurrent leaders. 这个技巧将保护您的系统免受并发领导者造成的不确定性。 Of course your database should be linearizable and support compare-and-set. 当然, 您的数据库应该是可线性化的,并且支持比较和设置。

To correct one of the answers, Zookeeper does guarantee leader uniqueness on network partitioning with quorum-based consistency.为了更正答案之一,Zookeeper 确实通过基于仲裁的一致性保证了网络分区上的领导者唯一性。 Upon a network partitioning, if a leader is isolated from a quorum, it will lose its leadership due to incapable of connecting to a quorum of nodes.在网络分区时,如果领导者与仲裁隔离,它将由于无法连接到仲裁节点而失去领导权。 In the meanwhile, a new leader is elected in the other partition.同时,在另一个分区中选举了一个新的领导者。 For the same reason, the partition where the old leader is in is unable to elect a new leader.同理,老领导所在的分区无法选举新领导。 The situation is resolved after the network partition is recovered by issuing a new leader election.通过发布新的领导者选举恢复网络分区后,这种情况得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM