Skip to content

Commit

Permalink
check memory buffer first and add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockkenan committed Jul 2, 2021
1 parent 046b522 commit 17290e1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/kvstore/raftex/RaftPart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ void RaftPart::processAppendLogRequest(
req.get_log_str_list().end());

// may be need to rollback wal_
if (!( req.get_last_log_id_sent() == wal_->lastLogId() && req.get_last_log_term_sent() == wal_->lastLogTerm())) {
if (!(req.get_last_log_id_sent() == lastLogId_ && req.get_last_log_term_sent() == lastLogTerm_)) {
// check the diff index in log
size_t diffIndex = 0;
{
Expand Down
8 changes: 8 additions & 0 deletions src/kvstore/wal/FileBasedWal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,14 @@ size_t FileBasedWal::accessAllBuffers(std::function<bool(BufferPtr buffer)> fn)
}

TermID FileBasedWal::getLogTerm(LogID id) {
// check the memory log buffer
for (auto it = buffers_.rbegin(); it != buffers_.rend(); ++it) {
auto buffer = *it;
if (id >= buffer->firstLogId() && id <= buffer->lastLogId()) {
return buffer->getTerm(id - buffer->firstLogId());
}
}
// check the log file
TermID term = -1;
auto walIter = std::make_unique<FileBasedWalIterator>(shared_from_this(), id, id);
if (walIter->valid()) {
Expand Down
38 changes: 38 additions & 0 deletions src/kvstore/wal/test/FileBasedWalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,44 @@ TEST(FileBasedWal, LinkTest) {
EXPECT_EQ(num + 1, wal->walFiles_.size());
}

TEST(FileBasedWal, getLogTermTest) {
FileBasedWalPolicy policy;
policy.fileSize = 1024L * 1024L;
policy.bufferSize = 1024L * 1024L;
policy.numBuffers = 2;

TempDir walDir("/tmp/testWal.XXXXXX");
auto wal = FileBasedWal::getWal(walDir.path(),
"",
policy,
[](LogID, TermID, ClusterID, const std::string&) {
return true;
});

// Append > 10MB logs in total
for (int i = 1; i <= 10000; i++) {
ASSERT_TRUE(wal->appendLog(i /*id*/, i /*term*/, 0 /*cluster*/,
folly::stringPrintf(kLongMsg, i)));
}

// in the memory buffer
ASSERT_EQ(10000, wal->getLogTerm(10000));
// in the file
ASSERT_EQ(4, wal->getLogTerm(4));

// Close the wal
wal.reset();

// Now let's open it to read
wal = FileBasedWal::getWal(walDir.path(),
"",
policy,
[](LogID, TermID, ClusterID, const std::string&) {
return true;
});
EXPECT_EQ(10, wal->getLogTerm(10));
}

} // namespace wal
} // namespace nebula

Expand Down

0 comments on commit 17290e1

Please sign in to comment.