Skip to content

Commit

Permalink
Add rollback_config API to state machine (#468)
Browse files Browse the repository at this point in the history
* State machine API should be invoked for not only app logs, but also
config logs, not to miss any index number.
  • Loading branch information
greensky00 authored Aug 31, 2023
1 parent 98cbb3f commit 6aecebd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 12 additions & 0 deletions include/libnuraft/state_machine.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ public:
virtual void rollback(const ulong log_idx,
buffer& data) {}

/**
* (Optional)
* Handler on the rollback of a configuration change.
* The configuration can be either committed or uncommitted one,
* and that can be checked by the given `log_idx`, comparing it with
* the current `cluster_config`'s log index.
*
* @param log_idx Raft log number of the configuration change.
* @param new_conf The cluster configuration to be rolled back.
*/
virtual void rollback_config(const ulong log_idx, ptr<cluster_config>& new_conf) { }

/**
* (Optional)
* Extended version of `rollback`, for users want to keep
Expand Down
5 changes: 4 additions & 1 deletion src/handle_append_entries.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -765,15 +765,18 @@ ptr<resp_msg> raft_server::handle_append_entries(req_msg& req)
for ( uint64_t ii = 0; ii < my_last_log_idx - log_idx + 1; ++ii ) {
uint64_t idx = my_last_log_idx - ii;
ptr<log_entry> old_entry = log_store_->entry_at(idx);
ptr<buffer> buf = old_entry->get_buf_ptr();
if (old_entry->get_val_type() == log_val_type::app_log) {
ptr<buffer> buf = old_entry->get_buf_ptr();
buf->pos(0);
state_machine_->rollback_ext
( state_machine::ext_op_params( idx, buf ) );
p_in( "rollback log %" PRIu64 ", term %" PRIu64,
idx, old_entry->get_term() );

} else if (old_entry->get_val_type() == log_val_type::conf) {
ptr<cluster_config> conf_to_rollback =
cluster_config::deserialize(*buf);
state_machine_->rollback_config(idx, conf_to_rollback);
p_in( "revert from a prev config change to config at %" PRIu64,
get_config()->get_log_idx() );
config_changing_ = false;
Expand Down

0 comments on commit 6aecebd

Please sign in to comment.