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 94EF044643A for ; Wed, 14 Oct 2020 02:28:35 +0300 (MSK) From: Vladislav Shpilevoy Date: Wed, 14 Oct 2020 01:28:28 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 2/6] raft: factor out the code to wakeup worker fiber 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 has a worker fiber to perform async tasks such as WAL write, state broadcast. The worker was created and woken up from 2 places, leading at least to code duplication. The patch wraps it into a new function raft_worker_wakeup(), and uses it. The patch is not need for anything functional, but was created while working on #5339 and trying ideas. The patch seems to be good refactoring making the code simpler, and therefore it is submitted. --- src/box/raft.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/box/raft.c b/src/box/raft.c index 0b6c373e8..f51e87fde 100644 --- a/src/box/raft.c +++ b/src/box/raft.c @@ -178,6 +178,13 @@ raft_election_quorum(void) return MIN(replication_synchro_quorum, replicaset.registered_count); } +/** + * Wakeup the Raft worker fiber in order to do some async work. If the fiber + * does not exist yet, it is created. + */ +static void +raft_worker_wakeup(void); + /** Schedule broadcast of the complete Raft state to all the followers. */ static void raft_schedule_broadcast(void); @@ -670,10 +677,8 @@ raft_sm_pause_and_dump(void) if (raft.is_write_in_progress) return; ev_timer_stop(loop(), &raft.timer); + raft_worker_wakeup(); raft.is_write_in_progress = true; - if (raft.worker == NULL) - raft.worker = fiber_new("raft_worker", raft_worker_f); - fiber_wakeup(raft.worker); } static void @@ -988,21 +993,28 @@ raft_new_term(void) } static void -raft_schedule_broadcast(void) +raft_worker_wakeup(void) { - raft.is_broadcast_scheduled = true; + if (raft.worker == NULL) { + raft.worker = fiber_new("raft_worker", raft_worker_f); + fiber_set_joinable(raft.worker, true); + } /* * Don't wake the fiber if it writes something. Otherwise it would be a - * spurious wakeup breaking the WAL write not adapted to this. + * spurious wakeup breaking the WAL write not adapted to this. Also + * don't wakeup the current fiber - it leads to undefined behaviour. */ - if (raft.is_write_in_progress) - return; - if (raft.worker == NULL) - raft.worker = fiber_new("raft_worker", raft_worker_f); - if (raft.worker != fiber()) + if (!raft.is_write_in_progress && fiber() != raft.worker) fiber_wakeup(raft.worker); } +static void +raft_schedule_broadcast(void) +{ + raft.is_broadcast_scheduled = true; + raft_worker_wakeup(); +} + void raft_init(void) { -- 2.21.1 (Apple Git-122.3)