Skip to content

Commit

Permalink
Make manually create snapshot result knowable (#377)
Browse files Browse the repository at this point in the history
* Make manually create snapshot result knowable

* little fix
  • Loading branch information
JackyWoo authored Sep 29, 2022
1 parent 0a084b6 commit 4d4d6fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
12 changes: 10 additions & 2 deletions include/libnuraft/raft_server.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,17 @@ public:
* Manually create a snapshot based on the latest committed
* log index of the state machine.
*
* @return `true` on success.
* @return Log index number of the created snapshot or`0` if failed.
*/
ulong create_snapshot();

/**
* Get the log index number of the last snapshot.
*
* @return Log index number of the last snapshot.
* `0` if snapshot does not exist.
*/
bool create_snapshot();
ulong get_last_snapshot_idx() const;

protected:
typedef std::unordered_map<int32, ptr<peer>>::const_iterator peer_itor;
Expand Down
9 changes: 7 additions & 2 deletions src/handle_commit.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,15 @@ bool raft_server::apply_config_log_entry(ptr<log_entry>& le,
return true;
}

bool raft_server::create_snapshot() {
ulong raft_server::create_snapshot() {
uint64_t committed_idx = sm_commit_index_;
p_in("manually create a snapshot on %lu", committed_idx);
return snapshot_and_compact(committed_idx, true);
return snapshot_and_compact(committed_idx, true) ? committed_idx : 0;
}

ulong raft_server::get_last_snapshot_idx() const {
std::lock_guard<std::mutex> l(last_snapshot_lock_);
return last_snapshot_ ? last_snapshot_->get_last_log_idx(): 0;
}

bool raft_server::snapshot_and_compact(ulong committed_idx, bool forced_creation) {
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/raft_server_test.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1988,8 +1988,9 @@ int snapshot_manual_creation_test() {
uint64_t committed_index = s1.raftServer->get_committed_log_idx();

// Create a manual snapshot.
CHK_OK( s1.raftServer->create_snapshot() );
CHK_EQ( committed_index, s1.getTestSm()->last_snapshot()->get_last_log_idx() );
ulong log_idx = s1.raftServer->create_snapshot();
CHK_EQ( committed_index, log_idx );
CHK_EQ( log_idx, s1.raftServer->get_last_snapshot_idx() );

// Make req to S3 failed.
s1.fNet->makeReqFail("S3");
Expand Down

0 comments on commit 4d4d6fd

Please sign in to comment.