Skip to content

Commit

Permalink
fix. raft follower will rollback itself when it misses a certain log
Browse files Browse the repository at this point in the history
if a leader fails to update its last_log_id_sent (maybe caused be
follower fail or network issue), it will then try to send some
duplicated logs in the next appendLog request.

```
                      commitId_: 99
                          |
                          v
local wal: |-------------------|
req.append_entries:        |----------------|
```

in this case, follower will do rollback even if the logs are the same.
  • Loading branch information
UndefinedSy committed Jul 5, 2024
1 parent fee249b commit 73a32f7
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/kvstore/raftex/RaftPart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1731,8 +1731,10 @@ void RaftPart::processAppendLogRequest(const cpp2::AppendLogRequest& req,
TermID lastTerm = (numLogs == 0) ? req.get_last_log_term_sent()
: req.get_log_str_list().back().get_log_term();
auto localWalIt = wal_->iterator(firstId, lastId);
bool hasConflict = false;
for (size_t i = 0; i < numLogs && localWalIt->valid(); ++i, ++(*localWalIt), ++diffIndex) {
if (localWalIt->logTerm() != req.get_log_str_list()[i].get_log_term()) {
hasConflict = true;
break;
}
}
Expand All @@ -1749,7 +1751,9 @@ void RaftPart::processAppendLogRequest(const cpp2::AppendLogRequest& req,

// Found a difference at log of (firstId + diffIndex), all logs from (firstId + diffIndex)
// could be truncated
wal_->rollbackToLog(firstId + diffIndex - 1);
if (hasConflict) {
wal_->rollbackToLog(firstId + diffIndex - 1);
}
firstId = firstId + diffIndex;
numLogs = numLogs - diffIndex;
}
Expand Down

0 comments on commit 73a32f7

Please sign in to comment.