Fixed an issue where the old leader's log could not be synced with the new one due to the absence of a previous LogEntry #54
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
According to the Raft protocol, before a new
LogEntry
can be added to the follower log, we should check that the previousLogEntry
exists and has the sameterm
number as the leader's entry.If an old leader's log contains a snapshot and only unreplicated entries, the new leader can't verify the previous
LogEntry
on the old leader, since this entry is already snapshotted and not present in aRaftLog
:LogEntry prevEntry = log.getLogEntry(request.getPreviousLogIndex());
return that
prevEntry
isnull
, ifprevLogIndex
<=snapshotIndex
.As a result, an old leader's log can't be synchronised with a new leader's log.
As a fix, we should add a check if
snapshotIndex
is large or equal topreviousLogIndex
, and use the snapshot term in this case.