-
Notifications
You must be signed in to change notification settings - Fork 226
Description
Hi, @stateIs0, I think there is an issue when a node updates its votedFor field. Specifically, LuRaftKV does not persist votedFor immediately after updating it.
According to the Raft paper, the votedFor variable should be persisted immediately after it is updated to ensure election safety:
Raft servers must persist enough information to stable storage to survive server restarts safely. In particular, each server persists its current term and vote
However, I noticed that LuRaftKV does not persist votedFor upon updating it. This means that if a node crashes and restarts after voting, it will forget its vote. As a result, it could grant another vote to a different Candidate in the same term, potentially leading to multiple Leaders being elected in that term, which violates Raft's safety guarantees.
To fix this issue, votedFor should be persisted to stable storage immediately after it is updated, before sending out any RPC request or response.
开发者你好,我发现项目中在更新 votedFor 时,似乎并没有立刻对其进行持久化。
根据 Raft 论文的描述,当节点更新 votedFor 时,应当立刻将其持久化,以确保选举的安全性:
Raft servers must persist enough information to stable storage to survive server restarts safely. In particular, each server persists its current term and vote
然而,我注意到 LuRaftKV 在更新 votedFor 后,并未立刻将其持久化。这意味着如果节点在投票后宕机重启,它会忘记自己已经投过票。当其他 Candidate 节点在同一 term 内向其发起投票请求时,它可能会再次投票,最终导致同一 term 中出现多个 Leader,这违背了 Raft 协议的安全性质。
该问题一个常见的可行修复方法是,在更新 votedFor 后,应当立刻将其持久化到稳定存储,并且在发送 RPC请求或响应之前完成该操作。