From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id AC77D6EC57; Fri, 30 Jul 2021 14:37:15 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org AC77D6EC57 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627645035; bh=hDfXZiotYq93ZjRyAF1W82e4fMDZqM9lCEiSQDbP06Y=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=fju3vdCm7zqgtDMIAEpJFCzg4rdGNwO3OpjBa27Uc/4qgIH8bYHTl6yREpQ68MIYs DdWp5AMe+1hJ9KndTyHUw3hgC4EOgyIlnqETr/MYopdMNl1Srb1vzQSrCe1F1JPPyx SKsUhFWyN1iidtTxSliTGgZwVt0g0yhTVbddyJKo= Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 126026EC5E for ; Fri, 30 Jul 2021 14:36:19 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 126026EC5E Received: by mail-lf1-f48.google.com with SMTP id x8so3944142lfe.3 for ; Fri, 30 Jul 2021 04:36:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+fE9GgLdiixPtAqSMtoceRpLMoxy0+ZG5CN/aP5+4LE=; b=XQSaW+cysy3OAsODh0ZdqGjSF5S9HnEeNtIeO5USQtCgBGKcriode9OMT04doANVza nNP+GdR2o8Ej9X0nNwMebZ5ZPte/9oa+BB7WR4JS22c86urHrlvuSta1PK++P0YU/1xC KRe1hSm2l+jjOisub6NMtDn+Ka6r55n3LFulsXwYUQmskGLQ2XJ51THc+gusrtIUJhsR Ywv8Dzrxij7NU747SDELS6+Vsd8wcozqBKiG1hiqbKxK3xK43weBRgUnvVIoA2jy513o X0ZOykaAyWI4etvncuVj5LPEVTn+ggZaFoDnQBS2kXfL2Y7aNi9xDiKLLN7PLIpfUJ1L Yrkg== X-Gm-Message-State: AOAM533foMuA2AkwX8EUdOu/3FOlThLRz72RuLfa0ZnEW8SNinh6msMV Fol1V/MgZpJwCG4EpN5zT7WOJwWyXEUJMQ== X-Google-Smtp-Source: ABdhPJxdYcoNOFJmlHBKiG5A2fsIv7XIq2XWW4fTWN1CnSTChhCVtYV9QIX9t/++y78A80bGiY3FQQ== X-Received: by 2002:a05:6512:b09:: with SMTP id w9mr1446377lfu.273.1627644977909; Fri, 30 Jul 2021 04:36:17 -0700 (PDT) Received: from grain.localdomain ([5.18.255.97]) by smtp.gmail.com with ESMTPSA id a1sm128700lff.232.2021.07.30.04.36.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jul 2021 04:36:17 -0700 (PDT) Received: by grain.localdomain (Postfix, from userid 1000) id 396AC5A0021; Fri, 30 Jul 2021 14:35:41 +0300 (MSK) To: tml Date: Fri, 30 Jul 2021 14:35:37 +0300 Message-Id: <20210730113539.563318-4-gorcunov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210730113539.563318-1-gorcunov@gmail.com> References: <20210730113539.563318-1-gorcunov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v9 3/5] limbo: order access to the limbo terms X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Cyrill Gorcunov via Tarantool-patches Reply-To: Cyrill Gorcunov Cc: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Limbo terms tracking is shared between appliers and when one of appliers is waiting for write to complete inside journal_write() routine, an other may need to access read term value to figure out if promote request is valid to apply. Due to cooperative multitasking access to the terms is not consistent so we need to be sure that other fibers either read up to date terms (ie written to the WAL). For this sake we use latching mechanism, when one fiber took terms-lock for updating other readers are waiting until the operation is complete. For example here is a call graph of two appliers applier 1 --------- applier_apply_tx (promote term = 3 current max term = 2) applier_synchro_filter_tx apply_synchro_row journal_write (sleeping) at this moment another applier comes in with obsolete data and term 2 applier 2 --------- applier_apply_tx (term 2) applier_synchro_filter_tx txn_limbo_is_replica_outdated -> false journal_write (sleep) applier 1 --------- journal wakes up apply_synchro_row_cb set max term to 3 So the applier 2 didn't notice that term 3 is already seen and wrote obsolete data. With locking the applier 2 will wait until applier 1 has finished its write. Part-of #6036 Signed-off-by: Cyrill Gorcunov --- src/box/applier.cc | 10 +++++-- src/box/box.cc | 12 ++++---- src/box/txn_limbo.c | 27 ++++++++++++----- src/box/txn_limbo.h | 72 +++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 98 insertions(+), 23 deletions(-) diff --git a/src/box/applier.cc b/src/box/applier.cc index f621fa657..a7f472714 100644 --- a/src/box/applier.cc +++ b/src/box/applier.cc @@ -856,7 +856,7 @@ apply_synchro_row_cb(struct journal_entry *entry) applier_rollback_by_wal_io(entry->res); } else { replica_txn_wal_write_cb(synchro_entry->rcb); - txn_limbo_process(&txn_limbo, synchro_entry->req); + txn_limbo_process_locked(&txn_limbo, synchro_entry->req); trigger_run(&replicaset.applier.on_wal_write, NULL); } fiber_wakeup(synchro_entry->owner); @@ -872,6 +872,7 @@ apply_synchro_row(uint32_t replica_id, struct xrow_header *row) if (xrow_decode_synchro(row, &req) != 0) goto err; + txn_limbo_term_lock(&txn_limbo); struct replica_cb_data rcb_data; struct synchro_entry entry; /* @@ -909,12 +910,15 @@ apply_synchro_row(uint32_t replica_id, struct xrow_header *row) * transactions side, including the async ones. */ if (journal_write(&entry.base) != 0) - goto err; + goto err_unlock; if (entry.base.res < 0) { diag_set_journal_res(entry.base.res); - goto err; + goto err_unlock; } + txn_limbo_term_unlock(&txn_limbo); return 0; +err_unlock: + txn_limbo_term_unlock(&txn_limbo); err: diag_log(); return -1; diff --git a/src/box/box.cc b/src/box/box.cc index 535f30292..5ca617e32 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -1573,7 +1573,7 @@ box_run_elections(void) static int box_check_promote_term_intact(uint64_t promote_term) { - if (txn_limbo.promote_greatest_term != promote_term) { + if (txn_limbo_term_max_raw(&txn_limbo) != promote_term) { diag_set(ClientError, ER_INTERFERING_PROMOTE, txn_limbo.owner_id); return -1; @@ -1585,7 +1585,7 @@ box_check_promote_term_intact(uint64_t promote_term) static int box_trigger_elections(void) { - uint64_t promote_term = txn_limbo.promote_greatest_term; + uint64_t promote_term = txn_limbo_term_max_raw(&txn_limbo); raft_new_term(box_raft()); if (box_raft_wait_term_persisted() < 0) return -1; @@ -1596,7 +1596,7 @@ box_trigger_elections(void) static int box_try_wait_confirm(double timeout) { - uint64_t promote_term = txn_limbo.promote_greatest_term; + uint64_t promote_term = txn_limbo_term_max_raw(&txn_limbo); txn_limbo_wait_empty(&txn_limbo, timeout); return box_check_promote_term_intact(promote_term); } @@ -1612,7 +1612,7 @@ box_wait_limbo_acked(void) if (txn_limbo_is_empty(&txn_limbo)) return txn_limbo.confirmed_lsn; - uint64_t promote_term = txn_limbo.promote_greatest_term; + uint64_t promote_term = txn_limbo_term_max_raw(&txn_limbo); int quorum = replication_synchro_quorum; struct txn_limbo_entry *last_entry; last_entry = txn_limbo_last_synchro_entry(&txn_limbo); @@ -1728,7 +1728,7 @@ box_promote(void) * Currently active leader (the instance that is seen as leader by both * raft and txn_limbo) can't issue another PROMOTE. */ - bool is_leader = txn_limbo_replica_term(&txn_limbo, instance_id) == + bool is_leader = txn_limbo_term(&txn_limbo, instance_id) == raft->term && txn_limbo.owner_id == instance_id; if (box_election_mode != ELECTION_MODE_OFF) is_leader = is_leader && raft->state == RAFT_STATE_LEADER; @@ -1784,7 +1784,7 @@ box_demote(void) return 0; /* Currently active leader is the only one who can issue a DEMOTE. */ - bool is_leader = txn_limbo_replica_term(&txn_limbo, instance_id) == + bool is_leader = txn_limbo_term(&txn_limbo, instance_id) == box_raft()->term && txn_limbo.owner_id == instance_id; if (box_election_mode != ELECTION_MODE_OFF) is_leader = is_leader && box_raft()->state == RAFT_STATE_LEADER; diff --git a/src/box/txn_limbo.c b/src/box/txn_limbo.c index 570f77c46..be5e0adf5 100644 --- a/src/box/txn_limbo.c +++ b/src/box/txn_limbo.c @@ -46,7 +46,8 @@ txn_limbo_create(struct txn_limbo *limbo) fiber_cond_create(&limbo->wait_cond); vclock_create(&limbo->vclock); vclock_create(&limbo->promote_term_map); - limbo->promote_greatest_term = 0; + limbo->promote_term_max = 0; + latch_create(&limbo->promote_latch); limbo->confirmed_lsn = 0; limbo->rollback_count = 0; limbo->is_in_rollback = false; @@ -308,7 +309,7 @@ txn_limbo_checkpoint(const struct txn_limbo *limbo, req->type = IPROTO_PROMOTE; req->replica_id = limbo->owner_id; req->lsn = limbo->confirmed_lsn; - req->term = limbo->promote_greatest_term; + req->term = limbo->promote_term_max; } static void @@ -724,22 +725,23 @@ txn_limbo_wait_empty(struct txn_limbo *limbo, double timeout) } void -txn_limbo_process(struct txn_limbo *limbo, const struct synchro_request *req) +txn_limbo_process_locked(struct txn_limbo *limbo, + const struct synchro_request *req) { uint64_t term = req->term; uint32_t origin = req->origin_id; - if (txn_limbo_replica_term(limbo, origin) < term) { + if (txn_limbo_term_locked(limbo, origin) < term) { vclock_follow(&limbo->promote_term_map, origin, term); - if (term > limbo->promote_greatest_term) - limbo->promote_greatest_term = term; + if (term > limbo->promote_term_max) + limbo->promote_term_max = term; } else if (iproto_type_is_promote_request(req->type) && - limbo->promote_greatest_term > 1) { + limbo->promote_term_max > 1) { /* PROMOTE for outdated term. Ignore. */ say_info("RAFT: ignoring %s request from instance " "id %u for term %llu. Greatest term seen " "before (%llu) is bigger.", iproto_type_name(req->type), origin, (long long)term, - (long long)limbo->promote_greatest_term); + (long long)limbo->promote_term_max); return; } @@ -786,6 +788,15 @@ txn_limbo_process(struct txn_limbo *limbo, const struct synchro_request *req) return; } +void +txn_limbo_process(struct txn_limbo *limbo, + const struct synchro_request *req) +{ + txn_limbo_term_lock(limbo); + txn_limbo_process_locked(limbo, req); + txn_limbo_term_unlock(limbo); +} + void txn_limbo_on_parameters_change(struct txn_limbo *limbo) { diff --git a/src/box/txn_limbo.h b/src/box/txn_limbo.h index 53e52f676..25faffd2b 100644 --- a/src/box/txn_limbo.h +++ b/src/box/txn_limbo.h @@ -31,6 +31,7 @@ */ #include "small/rlist.h" #include "vclock/vclock.h" +#include "latch.h" #include @@ -146,7 +147,11 @@ struct txn_limbo { * instance hasn't read its PROMOTE request yet. During other times the * limbo and raft are in sync and the terms are the same. */ - uint64_t promote_greatest_term; + uint64_t promote_term_max; + /** + * To order access to the promote data. + */ + struct latch promote_latch; /** * Maximal LSN gathered quorum and either already confirmed in WAL, or * whose confirmation is in progress right now. Any attempt to confirm @@ -211,14 +216,61 @@ txn_limbo_last_entry(struct txn_limbo *limbo) in_queue); } +/** Lock promote data. */ +static inline void +txn_limbo_term_lock(struct txn_limbo *limbo) +{ + latch_lock(&limbo->promote_latch); +} + +/** Unlock promote data. */ +static void +txn_limbo_term_unlock(struct txn_limbo *limbo) +{ + latch_unlock(&limbo->promote_latch); +} + +/** Test if promote data is locked. */ +static inline bool +txn_limbo_term_is_locked(const struct txn_limbo *limbo) +{ + return latch_is_locked(&limbo->promote_latch); +} + +/** Fetch replica's term with lock taken. */ +static inline uint64_t +txn_limbo_term_locked(struct txn_limbo *limbo, uint32_t replica_id) +{ + panic_on(!txn_limbo_term_is_locked(limbo), + "limbo: unlocked term read for replica_id %u", + replica_id); + return vclock_get(&limbo->promote_term_map, replica_id); +} + /** * Return the latest term as seen in PROMOTE requests from instance with id * @a replica_id. */ static inline uint64_t -txn_limbo_replica_term(const struct txn_limbo *limbo, uint32_t replica_id) +txn_limbo_term(struct txn_limbo *limbo, uint32_t replica_id) { - return vclock_get(&limbo->promote_term_map, replica_id); + txn_limbo_term_lock(limbo); + uint64_t v = txn_limbo_term_locked(limbo, replica_id); + txn_limbo_term_unlock(limbo); + return v; +} + +/** + * Fiber's preempt not safe read of @a terms_max. + * + * Use it if you're interested in current value + * only and ready that the value is getting updated + * if after the read yield happens. + */ +static inline uint64_t +txn_limbo_term_max_raw(struct txn_limbo *limbo) +{ + return limbo->promote_term_max; } /** @@ -226,11 +278,14 @@ txn_limbo_replica_term(const struct txn_limbo *limbo, uint32_t replica_id) * data from it. The check is only valid when elections are enabled. */ static inline bool -txn_limbo_is_replica_outdated(const struct txn_limbo *limbo, +txn_limbo_is_replica_outdated(struct txn_limbo *limbo, uint32_t replica_id) { - return txn_limbo_replica_term(limbo, replica_id) < - limbo->promote_greatest_term; + txn_limbo_term_lock(limbo); + bool res = txn_limbo_term_locked(limbo, replica_id) < + limbo->promote_term_max; + txn_limbo_term_unlock(limbo); + return res; } /** @@ -302,6 +357,11 @@ txn_limbo_wait_complete(struct txn_limbo *limbo, struct txn_limbo_entry *entry); /** Execute a synchronous replication request. */ void +txn_limbo_process_locked(struct txn_limbo *limbo, + const struct synchro_request *req); + +/** Lock limbo terms and execute a synchronous replication request. */ +void txn_limbo_process(struct txn_limbo *limbo, const struct synchro_request *req); /** -- 2.31.1