From 9eddae8fc775ee3f204dbcf25a9f5ee7bad11c4e Mon Sep 17 00:00:00 2001 From: Jung-Sang Ahn Date: Sun, 8 Dec 2019 17:58:50 +0900 Subject: [PATCH] Allow election timer on restart_election_timer API (#82) * Election timer flag should be updated on election timer restart, as the flag will not be effective anymore if timer is initiated. --- include/libnuraft/raft_server.hxx | 2 ++ src/handle_timeout.cxx | 6 ++++++ src/raft_server.cxx | 4 ---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/libnuraft/raft_server.hxx b/include/libnuraft/raft_server.hxx index f9e55118..193dab3f 100644 --- a/include/libnuraft/raft_server.hxx +++ b/include/libnuraft/raft_server.hxx @@ -194,6 +194,8 @@ public: /** * Start the election timer on this server, if this server is a follower. + * It will allow the election timer permanently, if it was disabled + * by state manager. */ void restart_election_timer(); diff --git a/src/handle_timeout.cxx b/src/handle_timeout.cxx index a0fe9c72..6721cae5 100644 --- a/src/handle_timeout.cxx +++ b/src/handle_timeout.cxx @@ -120,6 +120,12 @@ void raft_server::restart_election_timer() { return; } + // If election timer was not allowed, clear the flag. + if (!state_->is_election_timer_allowed()) { + state_->allow_election_timer(true); + ctx_->state_mgr_->save_state(*state_); + } + if (election_task_) { p_tr("cancel existing timer"); cancel_task(election_task_); diff --git a/src/raft_server.cxx b/src/raft_server.cxx index 036ebf64..be9e8b3a 100644 --- a/src/raft_server.cxx +++ b/src/raft_server.cxx @@ -960,10 +960,6 @@ bool raft_server::update_term(ulong term) { become_follower(); return true; } - if (!state_->is_election_timer_allowed()) { - state_->allow_election_timer(true); - ctx_->state_mgr_->save_state(*state_); - } return false; }