From patchwork Tue Sep 17 13:43:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13806170 Received: from fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6465E2905 for ; Tue, 17 Sep 2024 13:43:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726580605; cv=none; b=i4nKHPRhqdna4dAWc2TIZANNIWTnasFEwI6Wm3NX+vU6y6DR/YPSvWBg2ARJ3mBz3/oADqtDWTihSDTXPpYzKYMD3Nj5mgAvfkgKV0zqwan2hxd97t7WvDjAUcUNdxDlXOnCPpEI82yAVELEFI/dmtBPjewxxcItP2/kpm/o4qs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726580605; c=relaxed/simple; bh=aXAoMR9aqamK99/Xf5QbTcExYc8LFuINUEIWwMndDc8=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=aYYsIxsYSBPg+XvKC6wa4FPxR3ceSiECKfBOUP7AAaTnmmOqcVbPqSzJPrm6XDHp3amgPWCCKvVnhbK1QDAwlO2y1iTy9dhMr2gze5pnXu2TuDBysw9kbjqiKLqybeeH+TxawFyxeD087KSvwsuaEm4FL54S/xVhz6fAxaGf/Tg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=l158/PYL; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=dy9RWRun; arc=none smtp.client-ip=103.168.172.153 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="l158/PYL"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="dy9RWRun" Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfhigh.phl.internal (Postfix) with ESMTP id 5DE0B114013D for ; Tue, 17 Sep 2024 09:43:22 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Tue, 17 Sep 2024 09:43:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1726580602; x=1726667002; bh=kpOBAWBEdn SGkjuWLb3Y5A/quuJLV6EvpD3+BoCBZPU=; b=l158/PYLqD1M4sNUI9N3qhom3m tR3qct56Jhrjv0YwzcCBnjuYdU4jjUwIKsAPUwLHZx783nqbLQ08jTLnBvoxRuCO Y66jte3mv8qx8n+jTRJbL0aykzoinFUkwkW6i6sP6Y0WwqgFfkVLzpAkxD9AOXVi mLNoFpZD1NMDKV82qZ8oqS077OQkKhUXMTC9dk9YT5KvRMkh8g2rcetQI2nIWDUf QzQTO5NRhTKJR+WBhzIPg4gFBwEFgaiSGLBC5BadmEk4FQroC1WUxkPRoWhunaoq vRPlCjJPFlH2hYUT7kLBjcMQB23GtxpTi+U6RfXQmjwdeFeCKiO3asVdoCtg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1726580602; x=1726667002; bh=kpOBAWBEdnSGkjuWLb3Y5A/quuJL V6EvpD3+BoCBZPU=; b=dy9RWRunfTpQou8F2gH0ExoFaGOKMWOeTA1vEYkAOksY wctMb/AAv/COJQKNr6cC2wTOZaAsxwSlehJBbo4MU7e3bv7sl/PlcVW5AKiBAIMY rFKxZT1fPgwhM2iGapntpp5BYfRudxrl+4UgOs4gojTlZwNWicTI7R0lNjykUjkK QyAOwmtJuIrTNNgmazh8WoWlFojFo1vxi6v2nnD2go5Vi2SDXoYyjdANnUwKqM2Q 8F+qxkeB6/ILr5jo8QvB0ECeJMzZwcJw6jqyg4S6/1MDZUuqFmvwMLvItFZ8U04C 0ajED/BMQiT6hy07cv/DzK4CX5y1Xt0EZD+s0vVvLg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudekjedgieelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuf fkfhggtggujgesthdtredttddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhh rghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvghrnhepheekfeefgeegvd egvdeffeehtedttdffjeeuffelgffgheefleffleejvdefheeinecuvehluhhsthgvrhfu ihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspg hrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepghhithesvhhg vghrrdhkvghrnhgvlhdrohhrgh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Tue, 17 Sep 2024 09:43:21 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 9829723c (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Tue, 17 Sep 2024 13:43:01 +0000 (UTC) Date: Tue, 17 Sep 2024 15:43:20 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 1/3] refs/reftable: introduce "reftable.lockTimeout" Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: When multiple concurrent processes try to update references in a repository they may try to lock the same lockfiles. This can happen even when the updates are non-conflicting and can both be applied, so it doesn't always make sense to abort the transaction immediately. Both the "loose" and "packed" backends thus have a grace period that they wait for the lock to be released that can be controlled via the config values "core.filesRefLockTimeout" and "core.packedRefsTimeout", respectively. The reftable backend doesn't have such a setting yet and instead fails immediately when it sees such a lock. But the exact same concepts apply here as they do apply to the other backends. Introduce a new "reftable.lockTimeout" config that controls how long we may wait for a "tables.list" lock to be released. The default value of this config is 100ms, which is the same default as we have it for the "loose" backend. Note that even though we also lock individual tables, this config really only applies to the "tables.list" file. This is because individual tables are only ever locked when we already hold the "tables.list" lock during compaction. When we observe such a lock we in fact do not want to compact the table at all because it is already in the process of being compacted by a concurrent process. So applying the same timeout here would not make any sense and only delay progress. Signed-off-by: Patrick Steinhardt --- Documentation/config/reftable.txt | 7 +++++++ refs/reftable-backend.c | 4 ++++ reftable/reftable-writer.h | 8 ++++++++ reftable/stack.c | 18 ++++++++++++------ t/t0610-reftable-basics.sh | 27 +++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 6 deletions(-) diff --git a/Documentation/config/reftable.txt b/Documentation/config/reftable.txt index 05157279778..6c9449d0232 100644 --- a/Documentation/config/reftable.txt +++ b/Documentation/config/reftable.txt @@ -46,3 +46,10 @@ reftable.geometricFactor:: By default, the geometric sequence uses a factor of 2, meaning that for any table, the next-biggest table must at least be twice as big. A maximum factor of 256 is supported. + +reftable.lockTimeout:: + Whenever the reftable backend appends a new table to the stack, it has + to lock the central "tables.list" file before updating it. This config + controls how long the process will wait to acquire the lock in case + another process has already acquired it. Default is 1000 (i.e., retry + for 1 second). diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 1c4b19e737f..e90ddfb98dd 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -256,6 +256,9 @@ static int reftable_be_config(const char *var, const char *value, if (factor > UINT8_MAX) die("reftable geometric factor cannot exceed %u", (unsigned)UINT8_MAX); opts->auto_compaction_factor = factor; + } else if (!strcmp(var, "reftable.locktimeout")) { + unsigned long lock_timeout = git_config_ulong(var, value, ctx->kvi); + opts->lock_timeout_ms = lock_timeout; } return 0; @@ -281,6 +284,7 @@ static struct ref_store *reftable_be_init(struct repository *repo, refs->write_options.default_permissions = calc_shared_perm(0666 & ~mask); refs->write_options.disable_auto_compact = !git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1); + refs->write_options.lock_timeout_ms = 100; git_config(reftable_be_config, &refs->write_options); diff --git a/reftable/reftable-writer.h b/reftable/reftable-writer.h index 189b1f4144f..d5f2446220b 100644 --- a/reftable/reftable-writer.h +++ b/reftable/reftable-writer.h @@ -51,6 +51,14 @@ struct reftable_write_options { * tables to compact. Defaults to 2 if unset. */ uint8_t auto_compaction_factor; + + /* + * The number of milliseconds to wait when trying to lock "tables.list". + * Note that this does not apply to locking individual tables, as these + * should only ever be locked when already holding the "tables.list" + * lock. + */ + unsigned lock_timeout_ms; }; /* reftable_block_stats holds statistics for a single block type */ diff --git a/reftable/stack.c b/reftable/stack.c index ce0a35216ba..5ccad2cff31 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -603,8 +603,10 @@ static int reftable_stack_init_addition(struct reftable_addition *add, add->stack = st; - err = hold_lock_file_for_update(&add->tables_list_lock, st->list_file, - LOCK_NO_DEREF); + err = hold_lock_file_for_update_timeout(&add->tables_list_lock, + st->list_file, + LOCK_NO_DEREF, + st->opts.lock_timeout_ms); if (err < 0) { if (errno == EEXIST) { err = REFTABLE_LOCK_ERROR; @@ -1056,8 +1058,10 @@ static int stack_compact_range(struct reftable_stack *st, * Hold the lock so that we can read "tables.list" and lock all tables * which are part of the user-specified range. */ - err = hold_lock_file_for_update(&tables_list_lock, st->list_file, - LOCK_NO_DEREF); + err = hold_lock_file_for_update_timeout(&tables_list_lock, + st->list_file, + LOCK_NO_DEREF, + st->opts.lock_timeout_ms); if (err < 0) { if (errno == EEXIST) err = REFTABLE_LOCK_ERROR; @@ -1156,8 +1160,10 @@ static int stack_compact_range(struct reftable_stack *st, * "tables.list". We'll then replace the compacted range of tables with * the new table. */ - err = hold_lock_file_for_update(&tables_list_lock, st->list_file, - LOCK_NO_DEREF); + err = hold_lock_file_for_update_timeout(&tables_list_lock, + st->list_file, + LOCK_NO_DEREF, + st->opts.lock_timeout_ms); if (err < 0) { if (errno == EEXIST) err = REFTABLE_LOCK_ERROR; diff --git a/t/t0610-reftable-basics.sh b/t/t0610-reftable-basics.sh index 37510c2b2a5..62da3e37823 100755 --- a/t/t0610-reftable-basics.sh +++ b/t/t0610-reftable-basics.sh @@ -423,6 +423,33 @@ test_expect_success 'ref transaction: fails gracefully when auto compaction fail ) ' +test_expect_success 'ref transaction: timeout acquiring tables.list lock' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + >.git/reftable/tables.list.lock && + test_must_fail git update-ref refs/heads/branch HEAD 2>err && + test_grep "cannot lock references" err + ) +' + +test_expect_success 'ref transaction: retry acquiring tables.list lock' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + LOCK=.git/reftable/tables.list.lock && + >$LOCK && + { + ( sleep 1 && rm -f $LOCK ) & + } && + git -c reftable.lockTimeout=5000 update-ref refs/heads/branch HEAD + ) +' + test_expect_success 'pack-refs: compacts tables' ' test_when_finished "rm -rf repo" && git init repo && From patchwork Tue Sep 17 13:43:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13806172 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82CCD17ADE3 for ; Tue, 17 Sep 2024 13:43:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726580614; cv=none; b=DF1/uIPGev8z9hbb9Es0ukT7rwHJL5ToLwkDiRGiogJ222e3QKYmqVC8NTtheLJUgJFMcrvk3jIaALVnmydCh9Eyy+1XPC1T+mRj50wPI2UIScOK3cQTj0bn3ibHM8ZZQrfb1DOB++xz+gleca2XCTNlC1FOSxRASc1WT7SiEt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726580614; c=relaxed/simple; bh=Toe0Nncee/deQWgdEDeUbcuu4qjG4oAdhay48kS4OFY=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=dIiGA1CHoq5onYgXmC7zlKfjSNhJllXJ7466wu5hg9dYsBGR+7lFy8a8LDj4bm8S2kyuPGp78SVVuWzFHwEi0n7/x6/zsiZbQjRlduiD7tOPrq+hdL861XGlqO8JTwa82H6zsUkw3C+oIrsAtwmPbTi3QH3fFCzedsxFdEnbc8Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=NR6ls8ie; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=bG94JPWH; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="NR6ls8ie"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="bG94JPWH" Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfout.phl.internal (Postfix) with ESMTP id C20EC138022D for ; Tue, 17 Sep 2024 09:43:30 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Tue, 17 Sep 2024 09:43:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1726580610; x=1726667010; bh=Cyqe4MnGVt UyenYM4cS2UED3kFM+V7hy4ans973Eps4=; b=NR6ls8ie9aHFthAWa3O98E10eN /x0+UdT7zxhChH5gY4WG56/qLq8H9X4MIFmEVRuF7vJQmV4UYOS23LoCVbegWd4H Se5JAqrRbS72FwYy5FmboPPNM2zn6x4C4Jho3sAv5VBFil+4Cz/fQsIksheyxpwD DwZcHp6dOzi1GzPVpre8fiIdSG3KTgZh34Yehn6IJ16A7diH017koHTPRtCIOwN0 Yr7unWaqx8OKkZBMM241B3GCqscYbswj+24SIyFGHki2Zg4aRSsS8gx52C3CROVm sjk63gYGqVkOGHKUbiVfgagPUFTIs1/L+uGc6N8aVc4wKn+v0efFjp53JMdA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1726580610; x=1726667010; bh=Cyqe4MnGVtUyenYM4cS2UED3kFM+ V7hy4ans973Eps4=; b=bG94JPWHz5ZjSf4v4Awj4NwcAm3T2YhJIkVQ/GJ+3L/K 773kxjWsdlVb0D5VXYesjtRjBVgbqTtzVJQhLc/xbu3uyMJcVmWxrWwc5zy2Owfe C0Z4i0YUbaU4qAedaoJZ0iJIzAvCN3Uhd6acR1TCLEiKe5kNXaQyQJIzyTEMuk+D 6eo7qp7bTJaGBT+Unx72P77xwUIlgxhwLBBOgqWUqWhS3oI7+mtxHiepzgwy1HXa IL3IqGKGdP6z5B0cN4zIrCXsTSRko9bd6DntXPpTayzvEGAzVRPZaWSKcDCddTLN bBNnC6kWax8teGs/qBbFa9NSkz5ViiuaC+GQzE9s1A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudekjedgieelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuf fkfhggtggujgesthdtredttddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhh rghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvghrnhepheekfeefgeegvd egvdeffeehtedttdffjeeuffelgffgheefleffleejvdefheeinecuvehluhhsthgvrhfu ihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspg hrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepghhithesvhhg vghrrdhkvghrnhgvlhdrohhrgh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Tue, 17 Sep 2024 09:43:30 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 3a28cff7 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Tue, 17 Sep 2024 13:43:04 +0000 (UTC) Date: Tue, 17 Sep 2024 15:43:22 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 2/3] reftable/stack: allow locking of outdated stacks Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: In `reftable_stack_new_addition()` we first lock the stack and then check whether it is still up-to-date. If it is not we return an error to the caller indicating that the stack is outdated. This is overly restrictive in our ref transaction interface though: we lock the stack right before we start to verify the transaction, so we do not really care whether it is outdated or not. What we really want is that the stack is up-to-date after it has been locked so that we can verify queued updates against its current state while we know that it is locked for concurrent modification. Introduce a new flag `REFTABLE_STACK_NEW_ADDITION_RELOAD` that alters the behaviour of `reftable_stack_init_addition()` in this case: when we notice that it is out-of-date we reload it instead of returning an error to the caller. This logic will be wired up in the reftable backend in the next commit. Signed-off-by: Patrick Steinhardt --- refs/reftable-backend.c | 4 +- reftable/reftable-stack.h | 13 ++++++- reftable/stack.c | 20 ++++++---- t/unit-tests/t-reftable-stack.c | 67 ++++++++++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 13 deletions(-) diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index e90ddfb98dd..c500fb820a7 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -766,7 +766,7 @@ static int prepare_transaction_update(struct write_transaction_table_arg **out, if (ret) return ret; - ret = reftable_stack_new_addition(&addition, stack); + ret = reftable_stack_new_addition(&addition, stack, 0); if (ret) { if (ret == REFTABLE_LOCK_ERROR) strbuf_addstr(err, "cannot lock references"); @@ -2203,7 +2203,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store, if (ret < 0) goto done; - ret = reftable_stack_new_addition(&add, stack); + ret = reftable_stack_new_addition(&add, stack, 0); if (ret < 0) goto done; diff --git a/reftable/reftable-stack.h b/reftable/reftable-stack.h index f4f8cabc7fb..67316b215ed 100644 --- a/reftable/reftable-stack.h +++ b/reftable/reftable-stack.h @@ -37,12 +37,21 @@ uint64_t reftable_stack_next_update_index(struct reftable_stack *st); /* holds a transaction to add tables at the top of a stack. */ struct reftable_addition; +enum { + /* + * Reload the stack when the stack is out-of-date after locking it. + */ + REFTABLE_STACK_NEW_ADDITION_RELOAD = (1 << 0), +}; + /* * returns a new transaction to add reftables to the given stack. As a side - * effect, the ref database is locked. + * effect, the ref database is locked. Accepts REFTABLE_STACK_NEW_ADDITION_* + * flags. */ int reftable_stack_new_addition(struct reftable_addition **dest, - struct reftable_stack *st); + struct reftable_stack *st, + int flags); /* Adds a reftable to transaction. */ int reftable_addition_add(struct reftable_addition *add, diff --git a/reftable/stack.c b/reftable/stack.c index 5ccad2cff31..f9c95d5fa62 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -596,7 +596,8 @@ struct reftable_addition { #define REFTABLE_ADDITION_INIT {0} static int reftable_stack_init_addition(struct reftable_addition *add, - struct reftable_stack *st) + struct reftable_stack *st, + int flags) { struct strbuf lock_file_name = STRBUF_INIT; int err; @@ -626,6 +627,11 @@ static int reftable_stack_init_addition(struct reftable_addition *add, err = stack_uptodate(st); if (err < 0) goto done; + if (err > 0 && flags & REFTABLE_STACK_NEW_ADDITION_RELOAD) { + err = reftable_stack_reload_maybe_reuse(add->stack, 1); + if (err) + goto done; + } if (err > 0) { err = REFTABLE_OUTDATED_ERROR; goto done; @@ -633,9 +639,8 @@ static int reftable_stack_init_addition(struct reftable_addition *add, add->next_update_index = reftable_stack_next_update_index(st); done: - if (err) { + if (err) reftable_addition_close(add); - } strbuf_release(&lock_file_name); return err; } @@ -739,13 +744,14 @@ int reftable_addition_commit(struct reftable_addition *add) } int reftable_stack_new_addition(struct reftable_addition **dest, - struct reftable_stack *st) + struct reftable_stack *st, + int flags) { int err = 0; struct reftable_addition empty = REFTABLE_ADDITION_INIT; REFTABLE_CALLOC_ARRAY(*dest, 1); **dest = empty; - err = reftable_stack_init_addition(*dest, st); + err = reftable_stack_init_addition(*dest, st, flags); if (err) { reftable_free(*dest); *dest = NULL; @@ -759,7 +765,7 @@ static int stack_try_add(struct reftable_stack *st, void *arg) { struct reftable_addition add = REFTABLE_ADDITION_INIT; - int err = reftable_stack_init_addition(&add, st); + int err = reftable_stack_init_addition(&add, st, 0); if (err < 0) goto done; @@ -1608,7 +1614,7 @@ static int reftable_stack_clean_locked(struct reftable_stack *st) int reftable_stack_clean(struct reftable_stack *st) { struct reftable_addition *add = NULL; - int err = reftable_stack_new_addition(&add, st); + int err = reftable_stack_new_addition(&add, st, 0); if (err < 0) { goto done; } diff --git a/t/unit-tests/t-reftable-stack.c b/t/unit-tests/t-reftable-stack.c index d62a9c1bed5..a37cc698d87 100644 --- a/t/unit-tests/t-reftable-stack.c +++ b/t/unit-tests/t-reftable-stack.c @@ -271,7 +271,7 @@ static void t_reftable_stack_transaction_api(void) reftable_addition_destroy(add); - err = reftable_stack_new_addition(&add, st); + err = reftable_stack_new_addition(&add, st, 0); check(!err); err = reftable_addition_add(add, write_test_ref, &ref); @@ -292,6 +292,68 @@ static void t_reftable_stack_transaction_api(void) clear_dir(dir); } +static void t_reftable_stack_transaction_with_reload(void) +{ + char *dir = get_tmp_dir(__LINE__); + struct reftable_stack *st1 = NULL, *st2 = NULL; + int err; + struct reftable_addition *add = NULL; + struct reftable_ref_record refs[2] = { + { + .refname = (char *) "refs/heads/a", + .update_index = 1, + .value_type = REFTABLE_REF_VAL1, + .value.val1 = { '1' }, + }, + { + .refname = (char *) "refs/heads/b", + .update_index = 2, + .value_type = REFTABLE_REF_VAL1, + .value.val1 = { '1' }, + }, + }; + struct reftable_ref_record ref = { 0 }; + + err = reftable_new_stack(&st1, dir, NULL); + check(!err); + err = reftable_new_stack(&st2, dir, NULL); + check(!err); + + err = reftable_stack_new_addition(&add, st1, 0); + check(!err); + err = reftable_addition_add(add, write_test_ref, &refs[0]); + check(!err); + err = reftable_addition_commit(add); + check(!err); + reftable_addition_destroy(add); + + /* + * The second stack is now outdated, which we should notice. We do not + * create the addition and lock the stack by default, but allow the + * reload to happen when REFTABLE_STACK_NEW_ADDITION_RELOAD is set. + */ + err = reftable_stack_new_addition(&add, st2, 0); + check_int(err, ==, REFTABLE_OUTDATED_ERROR); + err = reftable_stack_new_addition(&add, st2, REFTABLE_STACK_NEW_ADDITION_RELOAD); + check(!err); + err = reftable_addition_add(add, write_test_ref, &refs[1]); + check(!err); + err = reftable_addition_commit(add); + check(!err); + reftable_addition_destroy(add); + + for (size_t i = 0; i < ARRAY_SIZE(refs); i++) { + err = reftable_stack_read_ref(st2, refs[i].refname, &ref); + check(!err); + check(reftable_ref_record_equal(&refs[i], &ref, GIT_SHA1_RAWSZ)); + } + + reftable_ref_record_release(&ref); + reftable_stack_destroy(st1); + reftable_stack_destroy(st2); + clear_dir(dir); +} + static void t_reftable_stack_transaction_api_performs_auto_compaction(void) { char *dir = get_tmp_dir(__LINE__); @@ -322,7 +384,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) */ st->opts.disable_auto_compact = i != n; - err = reftable_stack_new_addition(&add, st); + err = reftable_stack_new_addition(&add, st, 0); check(!err); err = reftable_addition_add(add, write_test_ref, &ref); @@ -1314,6 +1376,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED) TEST(t_reftable_stack_reload_with_missing_table(), "stack iteration with garbage tables"); TEST(t_reftable_stack_tombstone(), "'tombstone' refs in stack"); TEST(t_reftable_stack_transaction_api(), "update transaction to stack"); + TEST(t_reftable_stack_transaction_with_reload(), "transaction with reload"); TEST(t_reftable_stack_transaction_api_performs_auto_compaction(), "update transaction triggers auto-compaction"); TEST(t_reftable_stack_update_index_check(), "update transactions with equal update indices"); TEST(t_reftable_stack_uptodate(), "stack must be reloaded before ref update"); From patchwork Tue Sep 17 13:43:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13806171 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8295717107F for ; Tue, 17 Sep 2024 13:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726580614; cv=none; b=s8/1iuzU7mKfO4nwgTHCzOQlhLB754VCer4DZhFVrBnFxStgRd7GtW7lpT90M7hN+za8cPUpgUk493T2EsWxwvXHVr7ZiKTiSrnXfTCelNoAQbFZ20phGDV4ubFP8zZecU6PTX1e8vG3qzMCoRVn2lo/LT2bOjlqSUMFR1MKxhM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726580614; c=relaxed/simple; bh=RHitIEb/5n75CChFBXAqCBWPZV24bdF03RupSs+mmsI=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=KaG+4nKDM8eFY7KffIIxsA6w4Sa/BrUAoo4vd8yBN9eU2oMzv/p3V/bDKeK5cp7nXn3sECaVlrmmF0j2PkOtWgZk3Fpo2oJMxLg6pwMgHF/Lw6HduU9J+8GaRXWamDWjutYcs903LJJ0RhHq8YVFmPcRh1hDDZDLsOwtPW5TA64= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=Rq/3QMvq; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Wm108hdd; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="Rq/3QMvq"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Wm108hdd" Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfout.phl.internal (Postfix) with ESMTP id BB3111380073 for ; Tue, 17 Sep 2024 09:43:31 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-11.internal (MEProxy); Tue, 17 Sep 2024 09:43:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1726580611; x=1726667011; bh=zOCNGARfiC rCzliOjcu2ZOL8/DoZFs+d0RdtY6maxf8=; b=Rq/3QMvqGZwUkcTlZsFISpfUCJ i/4Q3/kL3E+k6NjXRKN4PbKI9TLtCgNjtxw2cuqLoB69SFtIEpii1rq+uyPTFbnt khuxRXpokTeJqg2y42sWOAbrEtSsRGU2AZkfkGyPBBXq5SRI6kdB+J62Wz2XKpA9 xHLvJNSl82Hg4wv/v9NYJvNz9APvSg7tR2wg9AQAYwyCJVQuACDBkHSrsnQUoLvW 8+ouXuTRDK2MLomZQASf7MpofgB4xR31WFeMNpcFQeyGu+aj5OkHZjlpkA6FKwM4 sU+tFq7XChpr+WD0gI+T8GxQh5kxvFFAPvB9vwCf9FCnMmQ/RzMvezBB+HQw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1726580611; x=1726667011; bh=zOCNGARfiCrCzliOjcu2ZOL8/DoZ Fs+d0RdtY6maxf8=; b=Wm108hddkQPiVSC2/p40uwj1QuxwUF9lZ9JAL28UtU8q GUzsXNLH0rrdAY5nhlFeh/+8Tl36fS8JEsmVlHQf3hLkjy6oB5jq++1jUkBgC00U 65jAhvM4ToVGi+ZEMkHqyKXK31tjBJdeV0V2xjkOzE2UMe/sgQ3IgUqoJSZ3bcGa +542q4ZNwElxDhy4gfpBV3BDGUSTC6z5NNOA5B6xGQA2SyWI7AMcCJSBeblGitgT 4JYqeum1HFbvBcarPky7FAjQFL3JM0xsyDA9ioOqup+hNtSuiWqv30P+TdftkCwT 7XudXjtwcYEVi8uylABOCph+gtT86vJroyrAIjfwpA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudekjedgieelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuf fkfhggtggujgesthdtredttddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhh rghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvghrnhepheekfeefgeegvd egvdeffeehtedttdffjeeuffelgffgheefleffleejvdefheeinecuvehluhhsthgvrhfu ihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspg hrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepghhithesvhhg vghrrdhkvghrnhgvlhdrohhrgh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Tue, 17 Sep 2024 09:43:30 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 697a0d86 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Tue, 17 Sep 2024 13:43:06 +0000 (UTC) Date: Tue, 17 Sep 2024 15:43:25 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 3/3] refs/reftable: reload locked stack when preparing transaction Message-ID: <37ec3acbff00564ecf3b63d1635aad5da0c54b19.1726578382.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: When starting a reftable transaction we lock all stacks we are about to modify. While it may happen that the stack is out-of-date at this point in time we don't really care: transactional updates encode the expected state of a certain reference, so all that we really want to verify is that the _current_ value matches that expected state. Pass `REFTABLE_STACK_NEW_ADDITION_RELOAD` when locking the stack such that an out-of-date stack will be reloaded after having been locked. This change is safe because all verifications of the expected state happen after this step anyway. Add a testcase that verifies that many writers are now able to write to the stack concurrently without failures and with a deterministic end result. Signed-off-by: Patrick Steinhardt --- refs/reftable-backend.c | 3 ++- t/t0610-reftable-basics.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index c500fb820a7..d4b383ca179 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -766,7 +766,8 @@ static int prepare_transaction_update(struct write_transaction_table_arg **out, if (ret) return ret; - ret = reftable_stack_new_addition(&addition, stack, 0); + ret = reftable_stack_new_addition(&addition, stack, + REFTABLE_STACK_NEW_ADDITION_RELOAD); if (ret) { if (ret == REFTABLE_LOCK_ERROR) strbuf_addstr(err, "cannot lock references"); diff --git a/t/t0610-reftable-basics.sh b/t/t0610-reftable-basics.sh index 62da3e37823..2d951c8ceb6 100755 --- a/t/t0610-reftable-basics.sh +++ b/t/t0610-reftable-basics.sh @@ -450,6 +450,37 @@ test_expect_success 'ref transaction: retry acquiring tables.list lock' ' ) ' +test_expect_success 'ref transaction: many concurrent writers' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + # Set a high timeout such that a busy CI machine will not abort + # early. 10 seconds should hopefully be ample of time to make + # this non-flaky. + git config set reftable.lockTimeout 10000 && + test_commit --no-tag initial && + + head=$(git rev-parse HEAD) && + for i in $(test_seq 100) + do + printf "%s commit\trefs/heads/branch-%s\n" "$head" "$i" || + return 1 + done >expect && + printf "%s commit\trefs/heads/main\n" "$head" >>expect && + + for i in $(test_seq 100) + do + { git update-ref refs/heads/branch-$i HEAD& } || + return 1 + done && + + wait && + git for-each-ref --sort=v:refname >actual && + test_cmp expect actual + ) +' + test_expect_success 'pack-refs: compacts tables' ' test_when_finished "rm -rf repo" && git init repo &&