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

fix a node can't accept more logs after receiving snapshot #3909

Merged
merged 4 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/kvstore/raftex/RaftLogIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace raftex {
class RaftLogIterator final : public LogIterator {
public:
/**
* @brief Construct a new raf log iterator
* @brief Construct a new raft log iterator
*
* @param firstLogId First log id in iterator
* @param logEntries Log entries from rpc request
Expand Down
36 changes: 33 additions & 3 deletions src/kvstore/raftex/RaftPart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1561,12 +1561,42 @@ void RaftPart::processAppendLogRequest(const cpp2::AppendLogRequest& req,
// previously in fact. There are two choise: ask leader to send logs after committedLogId_ or
// just do nothing.
if (req.get_last_log_id_sent() < committedLogId_ ||
wal_->lastLogId() < req.get_last_log_id_sent() ||
wal_->getLogTerm(req.get_last_log_id_sent()) != req.get_last_log_term_sent()) {
wal_->lastLogId() < req.get_last_log_id_sent()) {
// case 1 and case 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so case 1 is last_log_id < committedLogId_ and case 2 is wal_->lastLogId() < get_last_log_id_sent ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, see the comment above in line 1539

resp.last_matched_log_id_ref() = committedLogId_;
resp.last_matched_log_term() = committedLogTerm_;
resp.error_code() = nebula::cpp2::ErrorCode::E_RAFT_LOG_GAP;
return;
}
auto prevLogTerm = wal_->getLogTerm(req.get_last_log_id_sent());
if (UNLIKELY(prevLogTerm == FileBasedWal::INVALID_TERM)) {
/*
At this point, the condition below established:
committedLogId <= req.get_last_log_id_sent() <= wal_->lastLogId()

When INVALID_TERM is returned, we failed to find the log of req.get_last_log_id_sent()
in wal. This usually happens the node has received a snapshot recently.
*/
if (req.get_last_log_id_sent() == committedLogId_ &&
req.get_last_log_term_sent() == committedLogTerm_) {
// Logs are matched of at log index of committedLogId_, and we could check remaing wal if
// there are any.
// The first log of wal must be committedLogId_ + 1, it can't be 0 (no wal) as well
// because it has been checked by case 2
DCHECK(wal_->firstLogId() == committedLogId_ + 1);
} else {
// case 3: checked by committedLogId_ and committedLogTerm_
// When log is not matched, we just return committedLogId_ and committedLogTerm_ instead
resp.last_matched_log_id_ref() = committedLogId_;
resp.last_matched_log_term() = committedLogTerm_;
resp.error_code() = nebula::cpp2::ErrorCode::E_RAFT_LOG_GAP;
return;
}
} else if (prevLogTerm != req.get_last_log_term_sent()) {
// case 3
resp.last_matched_log_id_ref() = committedLogId_;
resp.last_matched_log_term() = committedLogTerm_;
resp.error_code() = nebula::cpp2::ErrorCode::E_RAFT_LOG_GAP;
// lastMatchedLogId is committedLogId_
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/kvstore/wal/FileBasedWal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ size_t FileBasedWal::accessAllWalInfo(std::function<bool(WalFileInfoPtr info)> f
}

TermID FileBasedWal::getLogTerm(LogID id) {
TermID term = -1;
TermID term = INVALID_TERM;
auto iter = iterator(id, id);
if (iter->valid()) {
term = iter->logTerm();
Expand Down
6 changes: 5 additions & 1 deletion src/kvstore/wal/FileBasedWal.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class FileBasedWal final : public Wal, public std::enable_shared_from_this<FileB
friend class WalFileIterator;

public:
/**
* @brief Invalid term when wal not found, used in getLogTerm
*/
static constexpr TermID INVALID_TERM{-1};

// A factory method to create a new WAL
/**
* @brief Build the file based wal
Expand Down Expand Up @@ -110,7 +115,6 @@ class FileBasedWal final : public Wal, public std::enable_shared_from_this<FileB
*/
bool appendLog(LogID id, TermID term, ClusterID cluster, std::string msg) override;

//
/**
* @brief Append a list of log messages to the WAL. This method **IS NOT** thread-safe. We **DO
* NOT** expect multiple threads will append logs simultaneously
Expand Down