Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raft fix: fix always loop when no log need to send #3819

Merged
merged 3 commits into from
Jan 27, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/kvstore/raftex/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,29 @@ Host::prepareAppendLogRequest() {
VLOG(2) << idStr_ << "Prepare AppendLogs request from Log " << lastLogIdSent_ + 1 << " to "
<< logIdToSend_;

auto makeReq = [this]() -> std::shared_ptr<cpp2::AppendLogRequest> {
auto req = std::make_shared<cpp2::AppendLogRequest>();
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;
liwenhui-soul marked this conversation as resolved.
Show resolved Hide resolved
};

// 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;
liwenhui-soul marked this conversation as resolved.
Show resolved Hide resolved
}

if (lastLogIdSent_ + 1 > part_->wal()->lastLogId()) {
LOG_IF(INFO, FLAGS_trace_raft)
<< idStr_ << "My lastLogId in wal is " << part_->wal()->lastLogId()
Expand All @@ -285,16 +303,7 @@ Host::prepareAppendLogRequest() {

auto it = part_->wal()->iterator(lastLogIdSent_ + 1, logIdToSend_);
if (it->valid()) {
auto req = std::make_shared<cpp2::AppendLogRequest>();
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<cpp2::RaftLogEntry> logs;
for (size_t cnt = 0; it->valid() && cnt < FLAGS_max_appendlog_batch_size; ++(*it), ++cnt) {
cpp2::RaftLogEntry entry;
Expand Down