Skip to content
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

Add a new API to reset the whole state machine. #477

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/calculator/calc_state_machine.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public:

ptr<snapshot_ctx> ctx = entry->second;
cur_value_ = ctx->value_;
// Now the last committed_idx is the snapshot's last log index.
last_committed_idx_ = s.get_last_log_idx();
return true;
}

Expand Down Expand Up @@ -213,6 +215,10 @@ public:
}
}

void reset() override {
cur_value_ = 0;
}

int64_t get_current_value() const { return cur_value_; }

private:
Expand Down
6 changes: 6 additions & 0 deletions examples/echo/echo_state_machine.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public:
ptr<buffer> snp_buf = s.serialize();
last_snapshot_ = snapshot::deserialize(*snp_buf);
}
// Now the last committed_idx is the snapshot's last log index.
last_committed_idx_ = s.get_last_log_idx();
return true;
}

Expand Down Expand Up @@ -140,6 +142,10 @@ public:
when_done(ret, except);
}

void reset() override {
// echo state machine has no state to reset.
}

private:
// Last committed Raft log number.
std::atomic<uint64_t> last_committed_idx_;
Expand Down
6 changes: 6 additions & 0 deletions include/libnuraft/state_machine.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ public:
*/
virtual bool allow_leadership_transfer() { return true; }

/**
* Reset the state machine clearing all data and related state,
* should be invoked before follower apply snapshot from leader.
*/
virtual void reset(){};

/**
* Parameters for `adjust_commit_index` API.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/handle_snapshot_sync.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ bool raft_server::handle_snapshot_sync_req(snapshot_sync_req& req, std::unique_l
stop_election_timer();
p_in("successfully compact the log store, will now ask the "
"statemachine to apply the snapshot");
p_in("firstly reset the statemachine");
state_machine_->reset();
if (!state_machine_->apply_snapshot(req.get_snapshot())) {
// LCOV_EXCL_START
p_er("failed to apply the snapshot after log compacted, "
Expand Down