Skip to content

The currentTerm is modified in the startVote function but is not persisted, which violates the requirements stated in the Raft paper. #57

@liang636600

Description

@liang636600

Hi, @wenweihu86 @loveheaven @guohao @wangwg1 , I found that the com.github.wenweihu86.raft.RaftNode#startVote function modifies currentTerm but does not persist the updated value. The same issue also occurs with the votedFor variable. I think this violates the Raft paper's specification, which states that currentTerm is a persistent state. In the following sections, I will provide a detailed explanation of my findings.

How to trigger this bug

As shown in Figure 1, triggering this bug requires only a single node, n1. First, n1 experiences a timeout, which corresponds to executing the com.github.wenweihu86.raft.RaftNode#startVote function. During the timeout process, n1 updates currentTerm to 1 but fails to persist this change, violating the Raft paper's requirement that currentTerm must be stored persistently (as illustrated in Figure 2). Then, n1 undergoes a restart, and its currentTerm resets to 0. However, according to the Raft paper, currentTerm should be restored to 1 from persistent storage. This discrepancy indicates that the implementation does not adhere to the Raft specification.

Figure 1. Incorrect CurrentTerm Value After N1 Restart.

Figure 2. CurrentTerm Must Be Persisted as Required by the Raft Paper.

Suggested fix

Fixing this bug is straightforward—simply persist the currentTerm and votedFor variables to disk after modifying them in the com.github.wenweihu86.raft.RaftNode#startVote function. The specific changes are as follows:


Simply add raftLog.updateMetaData(currentTerm, votedFor, null, null); right after the line votedFor = localServer.getServerId();.

Thank you for taking the time to read this. I'm looking forward to your confirmation, and would be happy to help fix the issue if needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions