From 178d40f003ee2b59e6384c24959b8acfc09b6e01 Mon Sep 17 00:00:00 2001 From: liwenhui-soul <38217397+liwenhui-soul@users.noreply.github.com> Date: Thu, 27 Jan 2022 10:34:49 +0800 Subject: [PATCH] raft: fix always loop when no log need to send --- src/kvstore/raftex/Host.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/kvstore/raftex/Host.cpp b/src/kvstore/raftex/Host.cpp index 33d2d3f7708..f61c15585fc 100644 --- a/src/kvstore/raftex/Host.cpp +++ b/src/kvstore/raftex/Host.cpp @@ -270,11 +270,29 @@ Host::prepareAppendLogRequest() { VLOG(2) << idStr_ << "Prepare AppendLogs request from Log " << lastLogIdSent_ + 1 << " to " << logIdToSend_; + auto makeReq = [this]() -> std::shared_ptr { + 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_; + req->last_log_id_sent_ref() = lastLogIdSent_; + return req; + }; + // We need to use lastLogIdSent_ + 1 to check whether need to send snapshot if (UNLIKELY(lastLogIdSent_ + 1 < part_->wal()->firstLogId())) { return startSendSnapshot(); } + if (lastLogIdSent_ == logIdToSend_) { + auto req = makeReq(); + return req; + } + if (lastLogIdSent_ + 1 > part_->wal()->lastLogId()) { LOG_IF(INFO, FLAGS_trace_raft) << idStr_ << "My lastLogId in wal is " << part_->wal()->lastLogId() @@ -285,16 +303,7 @@ Host::prepareAppendLogRequest() { auto it = part_->wal()->iterator(lastLogIdSent_ + 1, logIdToSend_); if (it->valid()) { - 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_; - req->last_log_id_sent_ref() = lastLogIdSent_; - + auto req = makeReq(); std::vector logs; for (size_t cnt = 0; it->valid() && cnt < FLAGS_max_appendlog_batch_size; ++(*it), ++cnt) { cpp2::RaftLogEntry entry;