Skip to content

Commit

Permalink
Add more UT for FileBasedWAL (vesoft-inc#15)
Browse files Browse the repository at this point in the history
* Fixes vesoft-inc#14 Add more UT for FileBasedWAL
  • Loading branch information
sherman-the-tank authored and dutor committed Sep 13, 2018
1 parent d383bb8 commit 7b2658f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set(CMAKE_CXX_STANDARD 14)
set(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++")

# Possible values are Debug, Release, RelWithDebInfo, MinSizeRel
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
set(CMAKE_BUILD_TYPE "Debug")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "_build")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "_build")
Expand Down
1 change: 1 addition & 0 deletions common/base/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include <folly/ThreadLocal.h>
#include <folly/Varint.h>

#include "thread/NamedThread.h"
//#include "base/StringUnorderedMap.h"

#define VE_MUST_USE_RESULT __attribute__((warn_unused_result))
Expand Down
10 changes: 5 additions & 5 deletions consensus/FileBasedWal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace consensus {

using WalFileInfoPair = FileBasedWal::WalFiles::value_type;
using namespace vesoft::fs;

using namespace vesoft::thread;

namespace internal {

Expand Down Expand Up @@ -115,6 +115,7 @@ class FileBasedWalIterator final : public LogIterator {
idRanges_.push_back(
std::make_pair(it->second.firstLogId,
it->second.lastLogId));
++it;
}
} else {
LOG(ERROR) << "LogID " << currId_
Expand Down Expand Up @@ -302,7 +303,8 @@ FileBasedWal::FileBasedWal(const folly::StringPiece dir,
}

bufferFlushThread_.reset(
new std::thread(std::bind(&FileBasedWal::bufferFlushLoop, this)));
new NamedThread("wal_buffer_flush_t",
std::bind(&FileBasedWal::bufferFlushLoop, this)));
}


Expand Down Expand Up @@ -597,12 +599,11 @@ void FileBasedWal::flushBuffer(std::shared_ptr<Buffer> buffer) {

std::lock_guard<std::mutex> guard(walFilesLock_);

auto& info = walFiles_.back();
Cord cord;
int64_t logId = buffer->firstLogId();
auto it = buffer->logs().begin();
while (it != buffer->logs().end()) {
if (info.second.size + cord.size()
if (walFiles_.back().second.size + cord.size()
+ it->size()
+ sizeof(LogID)
+ sizeof(int32_t) * 2
Expand All @@ -614,7 +615,6 @@ void FileBasedWal::flushBuffer(std::shared_ptr<Buffer> buffer) {
// Need to close the current file and create a new file
closeCurrFile();
prepareNewFile(logId);
info = walFiles_.back();
}

cord << logId << int32_t(it->size());
Expand Down
2 changes: 1 addition & 1 deletion consensus/FileBasedWal.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class FileBasedWal final : public Wal,
std::condition_variable bufferReadyCV_;
std::condition_variable slotReadyCV_;

std::unique_ptr<std::thread> bufferFlushThread_;
std::unique_ptr<thread::NamedThread> bufferFlushThread_;

private: // Methods
// Private constructor to prevent instantiating the class directly
Expand Down
2 changes: 2 additions & 0 deletions consensus/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_executable(
FileBasedWalTest.cpp
$<TARGET_OBJECTS:consensus_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:thread_obj>
$<TARGET_OBJECTS:fs_obj>
)
target_link_libraries(
Expand All @@ -15,6 +16,7 @@ target_link_libraries(
event
double-conversion
dl
ev
-pthread
)
add_test(NAME file_based_wal_test COMMAND file_based_wal_test)
Expand Down
61 changes: 61 additions & 0 deletions consensus/test/FileBasedWalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,67 @@ TEST(FileBasedWal, AppendLogs) {
++(*it);
++id;
}
EXPECT_EQ(10, id);
}


TEST(FileBasedWal, CacheOverflow) {
// Make a message which length > 1KB
static const char* kMsg =
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789"
"abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz-%06d";

// Force to make each file 1MB, each buffer is 1MB, and there are two
// buffers at most
FileBasedWalPolicy policy;
policy.fileSize = 1;
policy.bufferSize = 1;
policy.numBuffers = 2;

TempDir walDir("/tmp/testWal.XXXXXX");

// Create a new WAL, add one log and close it
auto wal = FileBasedWal::getWal(walDir.path(), policy);
EXPECT_EQ(-1, wal->lastLogId());

// Append > 10MB logs in total
for (int i = 0; i < 10000; i++) {
ASSERT_TRUE(wal->appendLog(i, folly::stringPrintf(kMsg, i)));
}
ASSERT_EQ(9999, wal->lastLogId());
// Close the wal
wal.reset();

// Check the number of files
auto files = FileUtils::listAllFilesInDir(walDir.path());
ASSERT_EQ(11, files.size());

// Now let's open it to read
wal = FileBasedWal::getWal(walDir.path(), policy);
EXPECT_EQ(9999, wal->lastLogId());

auto it = wal->iterator(0);
LogID id = 0;
while (it->valid()) {
ASSERT_EQ(id, it->logId());
ASSERT_EQ(folly::stringPrintf(kMsg, id), it->logMsg());
++(*it);
++id;
}
EXPECT_EQ(10000, id);
}

} // namespace consensus
Expand Down

0 comments on commit 7b2658f

Please sign in to comment.