Skip to content

Commit add2dfb

Browse files
committed
fix. raft follower will rollback itself when it misses a certain log
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.
1 parent fee249b commit add2dfb

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/kvstore/raftex/RaftPart.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,8 +1731,10 @@ void RaftPart::processAppendLogRequest(const cpp2::AppendLogRequest& req,
17311731
TermID lastTerm = (numLogs == 0) ? req.get_last_log_term_sent()
17321732
: req.get_log_str_list().back().get_log_term();
17331733
auto localWalIt = wal_->iterator(firstId, lastId);
1734+
bool hasConflict = false;
17341735
for (size_t i = 0; i < numLogs && localWalIt->valid(); ++i, ++(*localWalIt), ++diffIndex) {
17351736
if (localWalIt->logTerm() != req.get_log_str_list()[i].get_log_term()) {
1737+
hasConflict = true;
17361738
break;
17371739
}
17381740
}
@@ -1749,7 +1751,9 @@ void RaftPart::processAppendLogRequest(const cpp2::AppendLogRequest& req,
17491751

17501752
// Found a difference at log of (firstId + diffIndex), all logs from (firstId + diffIndex)
17511753
// could be truncated
1752-
wal_->rollbackToLog(firstId + diffIndex - 1);
1754+
if (hasConflict) {
1755+
wal_->rollbackToLog(firstId + diffIndex - 1);
1756+
}
17531757
firstId = firstId + diffIndex;
17541758
numLogs = numLogs - diffIndex;
17551759
}

0 commit comments

Comments
 (0)