-
Notifications
You must be signed in to change notification settings - Fork 63
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
feat: overload the PosixFileSystemAdaptor interface to generate real snapshot when follower installs snapshot #245
Changes from 1 commit
bc9356d
e093290
7401b39
5d2528e
2dc674a
8a5aadb
c205ce1
ff0a38d
478fe1f
512e4f2
06a6ef7
629744c
a5a5a9e
ca88bb9
e635f5e
e03e0ac
0259291
4cf2729
3298836
fd7957a
e4046e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
#include "pikiwidb.h" | ||
#include "praft.h" | ||
#include "praft.pb.h" | ||
#include "psnapshot.h" | ||
#include "store.h" | ||
|
||
#define ERROR_LOG_AND_STATUS(msg) \ | ||
|
@@ -103,22 +104,8 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { | |
node_options_.raft_meta_uri = prefix + "/raft_meta"; | ||
node_options_.snapshot_uri = prefix + "/snapshot"; | ||
// node_options_.disable_cli = FLAGS_disable_cli; | ||
|
||
// checkpoint callback | ||
auto checkpoint_callback = [&] (braft::SnapshotWriter* writer) { | ||
TasksVector tasks; | ||
tasks.reserve(g_config.databases); | ||
for (auto i = 0; i < g_config.databases; ++i) { | ||
tasks.push_back({TaskType::kCheckpoint, i, {{TaskArg::kCheckpointPath, writer->get_path()}}}); | ||
} | ||
INFO("start generate snapshot"); | ||
PSTORE.DoSomeThingSpecificDB(tasks); | ||
PSTORE.WaitForCheckpointDone(); | ||
auto writer_path = writer->get_path(); | ||
add_all_files(writer_path, writer, writer_path); | ||
INFO("end generate snapshot"); | ||
}; | ||
node_options_.checkpoint_callback = checkpoint_callback; | ||
snapshot_adaptor_ = new PPosixFileSystemAdaptor(); | ||
node_options_.snapshot_file_system_adaptor = &snapshot_adaptor_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. node_options里有一个定时打快照的参数snapshot_interval_s,可以考虑置零。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已经置0了 |
||
|
||
node_ = std::make_unique<braft::Node>("pikiwidb", braft::PeerId(addr)); // group_id | ||
if (node_->init(node_options_) != 0) { | ||
|
@@ -314,16 +301,21 @@ butil::Status PRaft::RemovePeer(const std::string& peer) { | |
return {0, "OK"}; | ||
} | ||
|
||
butil::Status PRaft::DoSnapshot() { | ||
butil::Status PRaft::DoSnapshot(int64_t self_snapshot_index) { | ||
if (!node_) { | ||
return ERROR_LOG_AND_STATUS("Node is not initialized"); | ||
} | ||
braft::SynchronizedClosure done; | ||
node_->snapshot(&done); | ||
node_->snapshot(&done); // @todo self_snapshot_index | ||
done.wait(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里考虑到我们想通过rocksdb触发event listerner去调用这个函数,不一定需要同步等待。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 所以可以考虑多一个参数用来控制是否进行等待。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好的 |
||
return {0, "OK"}; | ||
} | ||
|
||
void PRaft::GenerateRealSnapshot() { | ||
is_generate_snapshot_.store(true, std::memory_order_release); | ||
DoSnapshot(); // @todo customize the snapshot index | ||
} | ||
|
||
void PRaft::OnJoinCmdConnectionFailed([[maybe_unused]] EventLoop* loop, const char* peer_ip, int port) { | ||
auto cli = join_ctx_.GetClient(); | ||
if (cli) { | ||
|
@@ -445,6 +437,22 @@ void PRaft::on_apply(braft::Iterator& iter) { | |
|
||
void PRaft::on_snapshot_save(braft::SnapshotWriter* writer, braft::Closure* done) { | ||
brpc::ClosureGuard done_guard(done); | ||
if (is_generate_snapshot_.load(std::memory_order_acquire)) { | ||
TasksVector tasks; | ||
tasks.reserve(g_config.databases); | ||
for (auto i = 0; i < g_config.databases; ++i) { | ||
tasks.push_back({TaskType::kCheckpoint, i, {{TaskArg::kCheckpointPath, writer->get_path()}}}); | ||
} | ||
|
||
INFO("start generate snapshot"); | ||
PSTORE.DoSomeThingSpecificDB(tasks); | ||
PSTORE.WaitForCheckpointDone(); | ||
auto writer_path = writer->get_path(); | ||
add_all_files(writer_path, writer, writer_path); | ||
INFO("end generate snapshot"); | ||
|
||
is_generate_snapshot_.store(false, std::memory_order_release); | ||
} | ||
} | ||
|
||
int PRaft::on_snapshot_load(braft::SnapshotReader* reader) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
// | ||
// psnapshot.cc | ||
|
||
#include "psnapshot.h" | ||
#include "config.h" | ||
#include "pikiwidb.h" | ||
#include "praft.h" | ||
#include "store.h" | ||
|
||
namespace pikiwidb { | ||
|
||
struct PConfig; | ||
extern PConfig g_config; | ||
|
||
braft::FileAdaptor* PPosixFileSystemAdaptor::open(const std::string& path, int oflag, | ||
const ::google::protobuf::Message* file_meta, butil::File::Error* e) { | ||
// checkpoint callback | ||
PRAFT.GenerateRealSnapshot(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里会驱动on_snapshot_save去生成快照,如果系统一直向前运行,这时on_snapshot_save会生成在一个新的目录下而不是当前PPosixFileSystemAdaptor::open要找到的目录吧? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 或者说我们通过定制传进去的snapshot index这个接口中还能覆盖到上一次生成的目录里? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
return braft::PosixFileSystemAdaptor::open(path, oflag, file_meta, e); | ||
} | ||
|
||
} // namespace pikiwidb |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "braft/file_system_adaptor.h" | ||
#include "praft.h" | ||
|
||
namespace pikiwidb { | ||
|
||
class PPosixFileSystemAdaptor : public braft::PosixFileSystemAdaptor { | ||
public: | ||
PPosixFileSystemAdaptor() {} | ||
~PPosixFileSystemAdaptor() {} | ||
|
||
braft::FileAdaptor* open(const std::string& path, int oflag, const ::google::protobuf::Message* file_meta, | ||
butil::File::Error* e) override; | ||
}; | ||
|
||
} // namespace pikiwidb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里使用new申请了,虽然只申请了一次,但是最好有对应delete的操作。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scoped_refptrbraft::FileSystemAdaptor snapshot_adaptor_ = nullptr;
这样定义的,应该不需要主动调用delete吧