英文:
In RAFT algorithm, is it possible for a follower to become leader if it got disconnected from the current leader?
问题
在RAF算法中考虑以下情况:
- 集群中当前有一位领导者。
- 所有节点的日志都是最新的。
- 一个跟随者与领导者断开连接,而所有其他跟随者仍然与领导者保持连接并接收心跳。
- 断开连接的跟随者成为候选者,并开始投票选举新的任期,因为它没有收到来自领导者的心跳。
其他跟随者会为新候选者投票并选举它为领导者吗,而之前的领导者仍然健康吗?
英文:
Consider below scenario in RAFT algorithm.
- A leader is currently available in the cluster.
- The log is up to date in all nodes.
- One follower gets disconnected from the leader, while all other followers are still connected to the leader and are receiving heartbeats.
- The follower who got disconnected becomes a candidate and starts a vote for a new term, since it didn't receive heartbeats from leader.
Would the other followers vote for the new candidate and elect it as the leader, while the previous leader is also still healthy?
答案1
得分: 1
以下是翻译的内容:
“如果其他追随者投票支持新候选人并选举他为领袖,而之前的领袖仍然健康,答案是肯定的 - 投票不取决于现任领袖是否健康。
根据Raft白皮书(https://raft.github.io/raft.pdf 第4页),听不到心跳的追随者将变成候选人 - 它将增加任期并请求投票。而其他节点必须投赞成票,因为所有条件都得到满足。事实上,即使领袖也会投赞成票,并停止担任领袖,以防他们收到这样的投票请求。
事件的顺序如下:
- 假设集群稳定,任期号为10;有三个节点A、B、C、D、E,A是领袖
- B不再收到来自A的心跳信号
- B最终会成为候选人
- B将其任期增加到11
- B将启动投票
- 所有节点都会投赞成票,因为a)任期号大于他们以前看到的任期号,b)他们尚未在第11个任期号投票,c)B的日志已经更新 => 投赞成票
- 当领袖看到一个具有比他们自己更大的任期号的消息时,他们会变成追随者,并投赞成票
Raft和许多其他类似的协议都没有强领袖的概念 - 这意味着即使领袖健康,也没有什么可以阻止集群更换领袖。
如果按照白皮书描述实现Raft协议,那么一个断开但仍然存活的追随者会在每次重新连接时发起新的选举,这是一个常见问题。这是因为断开但仍然存活的追随者会不断成为候选人,增加其任期 - 因为他们无法在断开连接时赢得选举。因此,当他们实际重新连接时,突然发现他们的任期号比集群的当前任期号大,因此会发生新的选举(他们不会赢得选举,因为他们没有最新的日志记录)。
实际上,通常会有“预候选人”检查 - 候选人会在提高其任期号之前检查是否与大多数节点连接。这种方法可以防止网络不稳定时不必要的选举。”
英文:
"Would the other followers vote for the new candidate and elect it as the leader, while the previous leader is also still healthy?" the answer is yes - voting does not depend if existing leader is healthy or not.
According to raft white paper (https://raft.github.io/raft.pdf page 4), the follower without hearing a heart beat will become a candidate - it will increase the term and will request votes. And other nodes will have to vote yes as all conditions are met. In fact, even the leader will vote yes and stop being a leader in case they receive such voting request.
This is the sequence of events:
- given the cluster is stable and term number is 10; three nodes A,B,C,D,E and A is the leader
- B stops getting heartbeats from A
- B will become the candidate at some point
- B will increase its term to 11
- B will initiate voting
- all node will vote yes, because a) the term is larger then previous they saw and b) they did not yet vote in term 11 and c) the log of B is as updated as theirs => vote yes
- when the leader sees a message with the term larger than theirs, they turn to be a follower and vote yes as well
Raft, and many other similar protocols, do not have a concept of strong leader - which means nothing stops the cluster to swap the leader even if the leader is healthy.
If raft protocol is implemented as the paper describes, this is a common issue that a disconnected but alive follower keep initiating new elections on every reconnect. This is happens as the disconnected but alive follower keeps becoming the candidate, increasing it terms - as they can't win the election while disconnected. So when they actually reconnect, suddenly their term is larger then the current one of the cluster, hence new election happens (they won't win it as they don't have latest logs).
In practice, it is common to have "pre candidate" check - the candidate checks if it has connectivity to majority of nodes before bumping up its term number. This approach prevents unnecessary election when the network is not stable.
答案2
得分: 0
因为现任领导者仍然身体健康,其他追随者不会选择新的竞争者并选举他为领袖。这是为了让其他追随者能够继续感受到前领袖的脉搏,知道他或她仍然掌握着权力。唯一不知道前领袖仍然掌握权力的是失去连接的追随者。
由于 Raft 算法,一次只有一个领袖。当追随者资格符合候选人时,集群中的每个节点都会接收到 RequestVote
消息。拥有最多任期的候选人会得到每个节点的支持。如果候选人赢得了最多的选票,他们就会被宣布为赢家。
在你描述的情况下,失去连接的追随者将向集群中的所有其他节点发送 RequestVote
消息。然而,其他节点不会投票给失去连接的追随者,因为他们知道之前的领袖仍然是领袖。失去连接的追随者不会获得多数选票,也不会成为新的领袖。
Raft 算法旨在确保集群始终有一个领袖。这很重要,因为领袖负责协调集群的活动。没有领袖,集群将无法正常运作。
参考文献:Raft: A Consensus Algorithm for Replicated State Machines
您可以查看链接文档中的第 3.4、4.2 和 5.1 节,以进一步了解领袖选举。
此外,除了用于领袖心跳的 vote timeout
,追随者还有另一个超时。这个超时被称为 heartbeat timeout
。heartbeat timeout
是追随者在考虑领袖已死亡之前等待来自领袖的心跳的时间量。如果领袖在 heartbeat timeout
内没有发送心跳,追随者将成为候选人并启动新的选举。
还有一个 election timeout
,它是追随者等待其投票请求的响应的时间量,如果追随者在选举超时内未收到对其投票请求的响应,它们将启动新的选举。
心跳超时通常比选举超时短。这是因为心跳超时用于检测暂时不可用的领袖,而选举超时用于检测永久死亡的领袖。
通过具有两个不同的超时,Raft 算法可以确保集群始终有一个领袖,即使领袖暂时不可用。
第 5.2、5.3 和 5.4 节包含有关超时的必要信息。
英文:
No, because the incumbent leader is still in good health, the other followers would not choose the new contender and elect it as the leader. This is so that the other followers can continue to feel the prior leader's heartbeats and know that he or she is still in charge. The only follower who is unaware that the prior leader is still in charge is the one who lost connection.
There is only one leader at a time thanks to the Raft algorithm. Every node in the cluster receives a RequestVote
message when a follower qualifies as a candidate. The contender with the most terms receives the support of each node. If the candidate wins the most votes, they are declared the winner.
In the scenario you described, the disconnected follower will send a RequestVote
message to all other nodes in the cluster. However, the other nodes will not vote for the disconnected follower because they know that the previous leader is still the leader. The disconnected follower will not receive a majority of votes, and it will not become the new leader.
The Raft algorithm is designed to ensure that the cluster always has a leader. This is important because the leader is responsible for coordinating the activities of the cluster. Without a leader, the cluster would be unable to function properly.
Reference: Raft: A Consensus Algorithm for Replicated State Machines
You can check Section 3.4, 4.2 and 5.1 of the linked paper for further understanding of leader election.
Further in addition to the vote timeout
for leader heartbeats, followers have another timeout. This timeout is called the heartbeat timeout
. The heartbeat timeout
is the amount of time a follower will wait for a heartbeat from the leader before considering the leader dead. If the leader does not send a heartbeat within the heartbeat timeout
, the follower becomes a candidate and starts a new election.
There is one election timeout
also, it is the amount of time a follower waits for a response to its vote request before it considers the election failed. If the follower does not receive a response to their vote request within the election timeout, they will start a new election.
The heartbeat timeout is usually shorter than the election timeout. This is because the heartbeat timeout is used to detect a leader who is temporarily unavailable, while the election timeout is used to detect a leader who is permanently dead.
By having two different timeouts, the Raft algorithm can ensure that the cluster always has a leader, even if the leader is temporarily unavailable.
Section 5.2, 5.3, 5.4 have the necessary information about the timeouts.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论