sofastack / sofa-jraft

A production-grade java implementation of RAFT consensus algorithm.
https://www.sofastack.tech/projects/sofa-jraft/
Apache License 2.0
3.52k stars 1.12k forks source link

Commit index may be smaller than snapshot index #1092

Closed Brokenice0415 closed 2 weeks ago

Brokenice0415 commented 3 months ago

Describe the bug

There will be a period of time when commit index may be smaller than snapshot index.

Steps to reproduce

Here is a possible trace.

0 become leader
0 commit = 56
0 send AE {57, 58, 59} to 2
2 reply 0, append {57, 58, 59}, update commit 56
0 add {60}
0 send AE {60} to 2
0 receive 2's reply, update commit 59
0 set snapshot_59
0 crash
2 receive 0, append {60}
2 become leader, add {61}
0 restart, commit reset to 0
2 send AE {61} to 0
0 update commit 56

# now 0's commit index 56 is smaller than snapshot index 59.

Possible solution

Since logs compacted by snapshot must have been committed, we can initialize the lastCommitIndex in BallotBox with the snapshot index (if exists) instead of 0.

Environment

killme2008 commented 3 weeks ago

@Brokenice0415 Looks possible, but it doesn't seem to have any substantial impact, as it will eventually catch up.