From cba793511ec4f4881f4f7b3ab473f4e570d2507a Mon Sep 17 00:00:00 2001 From: liwenhui-soul <38217397+liwenhui-soul@users.noreply.github.com> Date: Fri, 21 Jan 2022 11:25:29 +0800 Subject: [PATCH] fix always loop when no log need to send --- src/kvstore/raftex/Host.cpp | 13 ++++++++++++- src/kvstore/raftex/RaftPart.cpp | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/kvstore/raftex/Host.cpp b/src/kvstore/raftex/Host.cpp index 33d2d3f7708..3e10f2970a9 100644 --- a/src/kvstore/raftex/Host.cpp +++ b/src/kvstore/raftex/Host.cpp @@ -280,7 +280,18 @@ Host::prepareAppendLogRequest() { << idStr_ << "My lastLogId in wal is " << part_->wal()->lastLogId() << ", but you are seeking " << lastLogIdSent_ + 1 << ", so i have nothing to send, logIdToSend_ = " << logIdToSend_; - return nebula::cpp2::ErrorCode::E_RAFT_NO_WAL_FOUND; + auto req = std::make_shared(); + req->space_ref() = part_->spaceId(); + req->part_ref() = part_->partitionId(); + req->current_term_ref() = logTermToSend_; + req->committed_log_id_ref() = committedLogId_; + req->leader_addr_ref() = part_->address().host; + req->leader_port_ref() = part_->address().port; + req->last_log_term_sent_ref() = lastLogTermSent_; + // the purpose of setting last_log_id to -1 is to make remote host return committedLogId which + // could be used as lastLogIdSent in next loop + req->last_log_id_sent_ref() = -1; + return req; } auto it = part_->wal()->iterator(lastLogIdSent_ + 1, logIdToSend_); diff --git a/src/kvstore/raftex/RaftPart.cpp b/src/kvstore/raftex/RaftPart.cpp index 4d49bf60972..f8c76b0087a 100644 --- a/src/kvstore/raftex/RaftPart.cpp +++ b/src/kvstore/raftex/RaftPart.cpp @@ -1001,6 +1001,12 @@ void RaftPart::processAppendLogResponses(const AppendLogResponses& resps, LOG_EVERY_N(WARNING, 100) << idStr_ << "Only " << numSucceeded << " hosts succeeded, Need to try again"; usleep(1000); + { + std::lock_guard g(raftLock_); + // wal could be rollback between two cycles, if we still use the old lastLogId, we may always + // get E_RAFT_NO_WAL_FOUND + lastLogId = std::min(wal_->lastLogId(), lastLogId); + } replicateLogs(eb, std::move(iter), currTerm, lastLogId, committedId, prevLogTerm, prevLogId); } }