forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance/fix FileBasedWAL (vesoft-inc#27)
* Enhance/fix FileBasedWAL 1) Support log rollback 2) Separate buffer flusher logic, so that the number of flushers is independent from the number of WALs/Shards 3) Improve the multiple threads support, but still not support multiple threads calling appendLogs()/rollbackToLog() 4) Add UT for rollback 5) Fix the flaky UTs * Rebased and addressed dutor's comments
- Loading branch information
1 parent
4c78a44
commit 965f3c3
Showing
17 changed files
with
1,364 additions
and
577 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved | ||
* | ||
* This source code is licensed under Apache 2.0 License | ||
* (found in the LICENSE.Apache file in the root directory) | ||
*/ | ||
|
||
#include "base/Base.h" | ||
#include "raftex/BufferFlusher.h" | ||
#include "raftex/FileBasedWal.h" | ||
|
||
namespace vesoft { | ||
namespace vgraph { | ||
namespace raftex { | ||
|
||
BufferFlusher::BufferFlusher() | ||
: flushThread_("Buffer flusher", | ||
std::bind(&BufferFlusher::flushLoop, this)) { | ||
} | ||
|
||
|
||
BufferFlusher::~BufferFlusher() { | ||
{ | ||
std::lock_guard<std::mutex> g(buffersLock_); | ||
stopped_ = true; | ||
} | ||
|
||
bufferReadyCV_.notify_one(); | ||
|
||
flushThread_.join(); | ||
CHECK(buffers_.empty()); | ||
} | ||
|
||
|
||
bool BufferFlusher::flushBuffer(std::shared_ptr<FileBasedWal> wal, | ||
BufferPtr buffer) { | ||
{ | ||
std::lock_guard<std::mutex> g(buffersLock_); | ||
|
||
if (stopped_) { | ||
LOG(ERROR) << "Buffer flusher has stopped"; | ||
return false; | ||
} | ||
|
||
buffers_.emplace(std::move(wal), std::move(buffer)); | ||
} | ||
|
||
// Notify the loop thread | ||
bufferReadyCV_.notify_one(); | ||
|
||
return true; | ||
} | ||
|
||
|
||
void BufferFlusher::flushLoop() { | ||
LOG(INFO) << "Buffer flusher loop started"; | ||
|
||
while(true) { | ||
decltype(buffers_)::value_type bufferPair; | ||
{ | ||
std::unique_lock<std::mutex> g(buffersLock_); | ||
if (buffers_.empty()) { | ||
if (stopped_) { | ||
VLOG(1) << "The buffer flusher has stopped," | ||
" so exiting the flush loop"; | ||
break; | ||
} | ||
// Otherwise need to wait | ||
bufferReadyCV_.wait(g, [this] { | ||
return !buffers_.empty() || stopped_; | ||
}); | ||
} else { | ||
bufferPair = std::move(buffers_.front()); | ||
buffers_.pop(); | ||
|
||
bufferPair.first->flushBuffer(bufferPair.second); | ||
} | ||
} | ||
} | ||
|
||
LOG(INFO) << "Buffer flusher loop finished"; | ||
} | ||
|
||
} // namespace raftex | ||
} // namespace vgraph | ||
} // namespace vesoft | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved | ||
* | ||
* This source code is licensed under Apache 2.0 License | ||
* (found in the LICENSE.Apache file in the root directory) | ||
*/ | ||
|
||
#ifndef RAFTEX_BUFFERFLUSHER_H_ | ||
#define RAFTEX_BUFFERFLUSHER_H_ | ||
|
||
#include "base/Base.h" | ||
#include "thread/NamedThread.h" | ||
#include "raftex/InMemoryLogBuffer.h" | ||
|
||
namespace vesoft { | ||
namespace vgraph { | ||
namespace raftex { | ||
|
||
class FileBasedWal; | ||
|
||
class BufferFlusher final { | ||
public: | ||
BufferFlusher(); | ||
~BufferFlusher(); | ||
|
||
bool flushBuffer(std::shared_ptr<FileBasedWal> wal, BufferPtr buffer); | ||
|
||
private: | ||
void flushLoop(); | ||
|
||
private: | ||
bool stopped_{false}; | ||
|
||
std::queue< | ||
std::pair<std::shared_ptr<FileBasedWal>, BufferPtr> | ||
> buffers_; | ||
std::mutex buffersLock_; | ||
std::condition_variable bufferReadyCV_; | ||
|
||
thread::NamedThread flushThread_; | ||
}; | ||
|
||
} // namespace raftex | ||
} // namespace vgraph | ||
} // namespace vesoft | ||
|
||
#endif // RAFTEX_BUFFERFLUSHER_H_ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.