From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 7E07744643E for ; Wed, 14 Oct 2020 02:28:36 +0300 (MSK) From: Vladislav Shpilevoy Date: Wed, 14 Oct 2020 01:28:30 +0200 Message-Id: <93da596e063b426b8571cd639ffea176b96eebb2.1602631481.git.v.shpilevoy@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 4/6] raft: introduce on_update trigger List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org Raft state machine now has a trigger invoked each time when any of the visible Raft attributes is changed: state, term, vote. The trigger is needed to commit synchronous transactions of an old leader, when a new leader is elected. This is done via a trigger so as not to depend on box in raft code too much. That would make it harder to extract it into a new module later. The trigger is executed in the Raft worker fiber, so as not to stop the state machine transitions anywhere, which currently don't contain a single yield. And the synchronous transaction queue clearance requires a yield, to write CONFIRM and ROLLBACK records to WAL. Part of #5339 --- src/box/raft.c | 8 ++++++++ src/box/raft.h | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/box/raft.c b/src/box/raft.c index ff28e7f08..8c127f268 100644 --- a/src/box/raft.c +++ b/src/box/raft.c @@ -643,6 +643,7 @@ raft_worker_handle_broadcast(void) } replicaset_foreach(replica) relay_push_raft(replica->relay, &req); + trigger_run(&raft.on_update, NULL); raft.is_broadcast_scheduled = false; } @@ -903,6 +904,12 @@ raft_serialize_for_disk(struct raft_request *req) req->vote = raft.vote; } +void +raft_on_update(struct trigger *trigger) +{ + trigger_add(&raft.on_update, trigger); +} + void raft_cfg_is_enabled(bool is_enabled) { @@ -1025,4 +1032,5 @@ void raft_init(void) { ev_timer_init(&raft.timer, raft_sm_schedule_new_election_cb, 0, 0); + rlist_create(&raft.on_update); } diff --git a/src/box/raft.h b/src/box/raft.h index be77a5473..82d5aa442 100644 --- a/src/box/raft.h +++ b/src/box/raft.h @@ -32,6 +32,7 @@ #include #include #include "tarantool_ev.h" +#include "trigger.h" #if defined(__cplusplus) extern "C" { @@ -150,6 +151,11 @@ struct raft { struct fiber *worker; /** Configured election timeout in seconds. */ double election_timeout; + /** + * Trigger invoked each time any of the Raft node visible attributes are + * changed. + */ + struct rlist on_update; }; extern struct raft raft; @@ -251,6 +257,13 @@ raft_serialize_for_network(struct raft_request *req, struct vclock *vclock); void raft_serialize_for_disk(struct raft_request *req); +/** + * Add a trigger invoked each time any of the Raft node visible attributes are + * changed. + */ +void +raft_on_update(struct trigger *trigger); + /** Initialize Raft global data structures. */ void raft_init(void); -- 2.21.1 (Apple Git-122.3)