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 437176EC41; Wed, 22 Sep 2021 16:06:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 437176EC41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1632316011; bh=UZ9ZN1xx4Xd7I6gg2h8hh/2ft/PUKJ6lgqxrVypMAjo=; 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=V7djXPd2bGxyEyqiyg0rnGCkPdSKnjpxFC4TXjU3ipiYOBo4iOYHChIFpy11A+/zG vm09AJ6ebr503NFp8wneI12NoxpxIGecYhj+hEdu6q4HPfRFfVWEYZYvXH3V9S2bqD yhDdLQA4DqeDvbkZ/8r4Ll73kDA5kMuTJdOSqcZE= Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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 622246EC41 for ; Wed, 22 Sep 2021 16:06:05 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 622246EC41 Received: by mail-lf1-f43.google.com with SMTP id z24so11494535lfu.13 for ; Wed, 22 Sep 2021 06:06:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kIosz7Qziexk7ku4uXALYiitvna0WltzTxmxF6/F1Vk=; b=iJlh97FgMD+2JtQXlk5mxTAjD8sHZZAGAhSPSsFeEFW0YxP9IY5rFNwk4iEQc0g568 f/JqIwjBDZn2nKdSZ1vuMhnLFXvRwHj1XqpKPQzaXChQNqGm/d63mSn/dP+KTbF2oPV1 SXphXY1rtomI/3oEwwyOzbJQx0kBf3dx0CL3rZ8qqM0TMXM5oBrYrDORYpMAeXrI7FaI F7b4/ODRVpDjn2LighROqFT33ywCvdQQiMaqpjO0Aeq1SE+uSyrLI2paTW3FUbU+ICMk WyE1WLplz0Fc39avDtmc58OLMWBXHYX5SSKk3AxtORMEAM/z1vaZZL/wxyic50iVAb6b 0BIA== X-Gm-Message-State: AOAM531Gt53MH8DJ8Ih1UVSd9xlglksq5wiqXvj9Jvp9z00fkJmgP65N rGN1zHU/4sa5/OLoRot9LPZxgWylP2CnLA== X-Google-Smtp-Source: ABdhPJwT4OZwMtUWIQKn+wlLaNn90YI9HZP5mcz1ZAEyL9xKPdUFLtUqfA+uCXa25tRKwFOy6TMV2w== X-Received: by 2002:ac2:4d22:: with SMTP id h2mr27054026lfk.429.1632315961412; Wed, 22 Sep 2021 06:06:01 -0700 (PDT) Received: from grain.localdomain ([5.18.253.97]) by smtp.gmail.com with ESMTPSA id h4sm174134lft.184.2021.09.22.06.06.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Sep 2021 06:06:00 -0700 (PDT) Received: by grain.localdomain (Postfix, from userid 1000) id 4B4A15A0020; Wed, 22 Sep 2021 16:05:36 +0300 (MSK) To: tml Date: Wed, 22 Sep 2021 16:05:32 +0300 Message-Id: <20210922130535.79479-3-gorcunov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210922130535.79479-1-gorcunov@gmail.com> References: <20210922130535.79479-1-gorcunov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v17 2/5] qsync: 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 read up to date terms (ie written to the WAL). For this sake we use a latching mechanism, when one fiber takes a 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. We introduce the following helpers: 1) txn_limbo_process_begin: which takes a lock (and will be validating request in future in sake of split brain detection) 2) txn_limbo_process_commit and txn_limbo_process_rollback which simply release the lock but have different names for better semantics 3) txn_limbo_process is a general function which uses x_begin and x_commit helper internally Since txn_limbo_process_begin() can fail we had to change all callers to return error. In particular box.ctrl.promote() and box.ctrl.demote() commands now may fail. Internally they write promote or demote packet to the WAL and process synchro queue. So to eliminate code duplication we use new box_issue_synchro() helper which respects locking mechanism. Part-of #6036 Signed-off-by: Cyrill Gorcunov --- src/box/applier.cc | 16 +++++++--- src/box/box.cc | 71 +++++++++++++++++++---------------------- src/box/memtx_engine.cc | 3 +- src/box/txn_limbo.c | 34 ++++++++++++++++++-- src/box/txn_limbo.h | 49 +++++++++++++++++++++++++--- 5 files changed, 121 insertions(+), 52 deletions(-) diff --git a/src/box/applier.cc b/src/box/applier.cc index b981bd436..f0751b68a 100644 --- a/src/box/applier.cc +++ b/src/box/applier.cc @@ -458,7 +458,8 @@ applier_wait_snapshot(struct applier *applier) struct synchro_request req; if (xrow_decode_synchro(&row, &req) != 0) diag_raise(); - txn_limbo_process(&txn_limbo, &req); + if (txn_limbo_process(&txn_limbo, &req) != 0) + diag_raise(); } else if (iproto_type_is_raft_request(row.type)) { struct raft_request req; if (xrow_decode_raft(&row, &req, NULL) != 0) @@ -857,7 +858,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_run(&txn_limbo, synchro_entry->req); trigger_run(&replicaset.applier.on_wal_write, NULL); } fiber_wakeup(synchro_entry->owner); @@ -873,6 +874,9 @@ apply_synchro_row(uint32_t replica_id, struct xrow_header *row) if (xrow_decode_synchro(row, &req) != 0) goto err; + if (txn_limbo_process_begin(&txn_limbo, &req) != 0) + goto err; + struct replica_cb_data rcb_data; struct synchro_entry entry; /* @@ -910,12 +914,16 @@ 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_rollback; if (entry.base.res < 0) { diag_set_journal_res(entry.base.res); - goto err; + goto err_rollback; } + txn_limbo_process_commit(&txn_limbo); return 0; + +err_rollback: + txn_limbo_process_rollback(&txn_limbo); err: diag_log(); return -1; diff --git a/src/box/box.cc b/src/box/box.cc index 7b11d56d6..19e67b186 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -424,8 +424,7 @@ wal_stream_apply_synchro_row(struct wal_stream *stream, struct xrow_header *row) say_error("couldn't decode a synchro request"); return -1; } - txn_limbo_process(&txn_limbo, &syn_req); - return 0; + return txn_limbo_process(&txn_limbo, &syn_req); } static int @@ -1670,48 +1669,43 @@ box_wait_limbo_acked(double timeout) return wait_lsn; } -/** Write and process a PROMOTE request. */ -static void -box_issue_promote(uint32_t prev_leader_id, int64_t promote_lsn) +/** Write and process PROMOTE or DEMOTE request. */ +static int +box_issue_synchro(uint16_t type, uint32_t prev_leader_id, int64_t promote_lsn) { struct raft *raft = box_raft(); + + assert(type == IPROTO_RAFT_PROMOTE || + type == IPROTO_RAFT_DEMOTE); assert(raft->volatile_term == raft->term); assert(promote_lsn >= 0); - txn_limbo_write_promote(&txn_limbo, promote_lsn, - raft->term); + struct synchro_request req = { - .type = IPROTO_RAFT_PROMOTE, - .replica_id = prev_leader_id, - .origin_id = instance_id, - .lsn = promote_lsn, - .term = raft->term, + .type = type, + .replica_id = prev_leader_id, + .origin_id = instance_id, + .lsn = promote_lsn, + .term = raft->term, }; - txn_limbo_process(&txn_limbo, &req); + + if (txn_limbo_process_begin(&txn_limbo, &req) != 0) + return -1; + + if (type == IPROTO_RAFT_PROMOTE) + txn_limbo_write_promote(&txn_limbo, req.lsn, req.term); + else + txn_limbo_write_demote(&txn_limbo, req.lsn, req.term); + + txn_limbo_process_run(&txn_limbo, &req); assert(txn_limbo_is_empty(&txn_limbo)); + + txn_limbo_process_commit(&txn_limbo); + return 0; } /** A guard to block multiple simultaneous promote()/demote() invocations. */ static bool is_in_box_promote = false; -/** Write and process a DEMOTE request. */ -static void -box_issue_demote(uint32_t prev_leader_id, int64_t promote_lsn) -{ - assert(box_raft()->volatile_term == box_raft()->term); - assert(promote_lsn >= 0); - txn_limbo_write_demote(&txn_limbo, promote_lsn, - box_raft()->term); - struct synchro_request req = { - .type = IPROTO_RAFT_DEMOTE, - .replica_id = prev_leader_id, - .origin_id = instance_id, - .lsn = promote_lsn, - .term = box_raft()->term, - }; - txn_limbo_process(&txn_limbo, &req); - assert(txn_limbo_is_empty(&txn_limbo)); -} - int box_promote_qsync(void) { @@ -1732,8 +1726,8 @@ box_promote_qsync(void) diag_set(ClientError, ER_NOT_LEADER, raft->leader); return -1; } - box_issue_promote(txn_limbo.owner_id, wait_lsn); - return 0; + return box_issue_synchro(IPROTO_RAFT_PROMOTE, + txn_limbo.owner_id, wait_lsn); } int @@ -1789,9 +1783,8 @@ box_promote(void) if (wait_lsn < 0) return -1; - box_issue_promote(txn_limbo.owner_id, wait_lsn); - - return 0; + return box_issue_synchro(IPROTO_RAFT_PROMOTE, + txn_limbo.owner_id, wait_lsn); } int @@ -1826,8 +1819,8 @@ box_demote(void) int64_t wait_lsn = box_wait_limbo_acked(replication_synchro_timeout); if (wait_lsn < 0) return -1; - box_issue_demote(txn_limbo.owner_id, wait_lsn); - return 0; + return box_issue_synchro(IPROTO_RAFT_DEMOTE, + txn_limbo.owner_id, wait_lsn); } int diff --git a/src/box/memtx_engine.cc b/src/box/memtx_engine.cc index de918c335..09f1d671c 100644 --- a/src/box/memtx_engine.cc +++ b/src/box/memtx_engine.cc @@ -238,8 +238,7 @@ memtx_engine_recover_synchro(const struct xrow_header *row) * because all its rows have a zero replica_id. */ req.origin_id = req.replica_id; - txn_limbo_process(&txn_limbo, &req); - return 0; + return txn_limbo_process(&txn_limbo, &req); } static int diff --git a/src/box/txn_limbo.c b/src/box/txn_limbo.c index 70447caaf..eb9aa7780 100644 --- a/src/box/txn_limbo.c +++ b/src/box/txn_limbo.c @@ -47,6 +47,7 @@ txn_limbo_create(struct txn_limbo *limbo) vclock_create(&limbo->vclock); vclock_create(&limbo->promote_term_map); limbo->promote_greatest_term = 0; + latch_create(&limbo->promote_latch); limbo->confirmed_lsn = 0; limbo->rollback_count = 0; limbo->is_in_rollback = false; @@ -724,11 +725,14 @@ 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_run(struct txn_limbo *limbo, + const struct synchro_request *req) { + assert(latch_is_locked(&limbo->promote_latch)); + uint64_t term = req->term; uint32_t origin = req->origin_id; - if (txn_limbo_replica_term(limbo, origin) < term) { + if (vclock_get(&limbo->promote_term_map, origin) < (int64_t)term) { vclock_follow(&limbo->promote_term_map, origin, term); if (term > limbo->promote_greatest_term) limbo->promote_greatest_term = term; @@ -786,6 +790,32 @@ txn_limbo_process(struct txn_limbo *limbo, const struct synchro_request *req) return; } +int +txn_limbo_process_begin(struct txn_limbo *limbo, + const struct synchro_request *req) +{ + latch_lock(&limbo->promote_latch); + /* + * FIXME: For now we take a lock only but idea + * is to verify incoming requests to detect split + * brain situation. Thus we need to change the code + * semantics in advance. + */ + (void)req; + return 0; +} + +int +txn_limbo_process(struct txn_limbo *limbo, + const struct synchro_request *req) +{ + if (txn_limbo_process_begin(limbo, req) != 0) + return -1; + txn_limbo_process_run(limbo, req); + txn_limbo_process_commit(limbo); + return 0; +} + 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..afe9b429f 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 @@ -147,6 +148,10 @@ struct txn_limbo { * limbo and raft are in sync and the terms are the same. */ uint64_t promote_greatest_term; + /** + * 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 @@ -216,9 +221,12 @@ txn_limbo_last_entry(struct txn_limbo *limbo) * @a replica_id. */ static inline uint64_t -txn_limbo_replica_term(const struct txn_limbo *limbo, uint32_t replica_id) +txn_limbo_replica_term(struct txn_limbo *limbo, uint32_t replica_id) { - return vclock_get(&limbo->promote_term_map, replica_id); + latch_lock(&limbo->promote_latch); + uint64_t v = vclock_get(&limbo->promote_term_map, replica_id); + latch_unlock(&limbo->promote_latch); + return v; } /** @@ -226,11 +234,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; + latch_lock(&limbo->promote_latch); + uint64_t v = vclock_get(&limbo->promote_term_map, replica_id); + bool res = v < limbo->promote_greatest_term; + latch_unlock(&limbo->promote_latch); + return res; } /** @@ -300,8 +311,36 @@ txn_limbo_ack(struct txn_limbo *limbo, uint32_t replica_id, int64_t lsn); int txn_limbo_wait_complete(struct txn_limbo *limbo, struct txn_limbo_entry *entry); +/** + * Initiate execution of a synchronous replication request. + */ +int +txn_limbo_process_begin(struct txn_limbo *limbo, + const struct synchro_request *req); + +/** Commit a synchronous replication request. */ +static inline void +txn_limbo_process_commit(struct txn_limbo *limbo) +{ + assert(latch_is_locked(&limbo->promote_latch)); + latch_unlock(&limbo->promote_latch); +} + +/** Rollback a synchronous replication request. */ +static inline void +txn_limbo_process_rollback(struct txn_limbo *limbo) +{ + assert(latch_is_locked(&limbo->promote_latch)); + latch_unlock(&limbo->promote_latch); +} + /** Execute a synchronous replication request. */ void +txn_limbo_process_run(struct txn_limbo *limbo, + const struct synchro_request *req); + +/** Process a synchronous replication request. */ +int txn_limbo_process(struct txn_limbo *limbo, const struct synchro_request *req); /** -- 2.31.1