From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id +HXXI3Xy1V5PCAAA0tVLHw (envelope-from ) for ; Tue, 02 Jun 2020 06:32:21 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id uHDBH3Xy1V7FdQAAB5/wlQ (envelope-from ) for ; Tue, 02 Jun 2020 06:32:21 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 0C8C594030A for ; Tue, 2 Jun 2020 06:32:20 +0000 (UTC) Received: from localhost ([::1]:55706 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jg0TC-0005RC-VI for larch@yhetil.org; Tue, 02 Jun 2020 02:32:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44190) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jg0Sw-0005R1-2c for guix-patches@gnu.org; Tue, 02 Jun 2020 02:32:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:54397) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jg0Sv-0002vh-Oe for guix-patches@gnu.org; Tue, 02 Jun 2020 02:32:01 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jg0Sv-0006o5-Mc for guix-patches@gnu.org; Tue, 02 Jun 2020 02:32:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#41658] [PATCH] fixes / improvements for (guix store database) Resent-From: Caleb Ristvedt Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 02 Jun 2020 06:32:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 41658 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 41658@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.159107951526151 (code B ref -1); Tue, 02 Jun 2020 06:32:01 +0000 Received: (at submit) by debbugs.gnu.org; 2 Jun 2020 06:31:55 +0000 Received: from localhost ([127.0.0.1]:37710 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jg0So-0006ni-Qg for submit@debbugs.gnu.org; Tue, 02 Jun 2020 02:31:55 -0400 Received: from lists.gnu.org ([209.51.188.17]:58026) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jg0Sm-0006na-Gc for submit@debbugs.gnu.org; Tue, 02 Jun 2020 02:31:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44188) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jg0Sm-0005QZ-AI for guix-patches@gnu.org; Tue, 02 Jun 2020 02:31:52 -0400 Received: from mail-il1-x12d.google.com ([2607:f8b0:4864:20::12d]:41927) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jg0Sj-0002vG-KU for guix-patches@gnu.org; Tue, 02 Jun 2020 02:31:51 -0400 Received: by mail-il1-x12d.google.com with SMTP id d1so11792522ila.8 for ; Mon, 01 Jun 2020 23:31:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cune-org.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:user-agent:mime-version; bh=0Hv+5EliMxBI2r7Kdfa5detqIH1SOtXpJjvG2m1ohUY=; b=miGmYGS+Dcxez+wwuHvzo394g1Z/mWtAHfHZ748IzWrVOzYvfXIloejVqqFmA9VkFp 3wcY/JX2vQmMDhgTyO9zgP49XsFN5dnVbbEnrokJHEUVRnLLFb2XhgCxIpERyyHfVh89 Zr6/JPEgRgBZPNeFrs1riRQetFO5EBTMdxVXA/OMEhcJvMuEGGWyqE2ybDWWOxUP4qaS jxSgNASNHvtk29HRfXxA7XF37VsgbzZlkXivXyS65NgF5LTXVcH8aOxAE8zUDowrpmYc sa+TvvIAY00xNqVam9KFAo1OHhK8zK0YhmSvkPaWRHhK31lvCOOdAuZ/9iTgujRo9F+R YW1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:user-agent :mime-version; bh=0Hv+5EliMxBI2r7Kdfa5detqIH1SOtXpJjvG2m1ohUY=; b=ZGzP0F8wXgwPtmzytbbPjP4gFxd66Ygaye06CQ0HdSYBUasRqk+fJ0bQLwJumN5BAD 8GVaay23Rqg5GNyH2VZQjjejEfJdmkquoYDcgzhFUmwCJrBf63XppNiPggK3eqlFNwI2 iBYl4bS7EacKWOF2/bfZzf8V7bm52cg+tvn50O1mntAntnlxpMuS1aMbb6Rt8a3BHyh2 XIVw0uqrrpPe0rSzRvIbi9PvnkUFIp4ZjoLJ5ayiGh3mpCSD7FnhE7A9Jnx2xgtpPiMM d8SinW8YDCXFYSlfzM2u7rc0Qn/uUW4jP7Cy5sKdkhjoiPcA/xpAWJzSVQV6IBqGaPdY rWrw== X-Gm-Message-State: AOAM532c3kHeYEuZU4opBpVk6wG8IHbOLf4rhZN80SPNtvk7egAlCKMT rhbfDWZmYzAZUHxynwv7G3RoTCdm6yg= X-Google-Smtp-Source: ABdhPJy1b/qpnItrtxbrYPRYHjnTVYFxZ/++41AyK1+OpHiK/M3lDh0VxNocXOsv5tqHg2KZLPzB1w== X-Received: by 2002:a92:5c18:: with SMTP id q24mr3666503ilb.181.1591079503689; Mon, 01 Jun 2020 23:31:43 -0700 (PDT) Received: from GuixPotato ([208.89.170.24]) by smtp.gmail.com with ESMTPSA id y13sm701102iob.51.2020.06.01.23.31.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jun 2020 23:31:42 -0700 (PDT) From: Caleb Ristvedt Date: Tue, 02 Jun 2020 01:31:38 -0500 Message-ID: <87ftbernat.fsf@cune.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Received-SPF: pass client-ip=2607:f8b0:4864:20::12d; envelope-from=caleb.ristvedt@cune.org; helo=mail-il1-x12d.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-Spam-Score: -1.4 (-) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -2.4 (--) X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=fail (rsa verify failed) header.d=cune-org.20150623.gappssmtp.com header.s=20150623 header.b=miGmYGS+; dmarc=none; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Spam-Score: -2.11 X-TUID: TTPLrhMA+6D2 --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain After some pondering about why the database might be locked so frequently, this is what I've managed to come up with. The first patch is the most likely to actually help with that, and the others mostly involve improving robustness. Ideally we'd come up with a test to quantify how much these kinds of changes affect contention over the database. For now, though, all that I can think of is seeing how this affects the systems that have had issues with that. - reepca --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0001-database-work-around-guile-sqlite3-bug-preventing-st.patch Content-Transfer-Encoding: quoted-printable From=20cce653c590be1506e15044e445aa9805370ac759 Mon Sep 17 00:00:00 2001 From: Caleb Ristvedt Date: Mon, 1 Jun 2020 18:50:07 -0500 Subject: [PATCH 1/4] database: work around guile-sqlite3 bug preventing statement reset MIME-Version: 1.0 Content-Type: text/plain; charset=3DUTF-8 Content-Transfer-Encoding: 8bit guile-sqlite3 provides statement caching, making it unnecessary for sqlite = to keep re-preparing statements that are frequently used. Unfortunately it doesn't quite emulate the semantics of sqlite_finalize properly, because it doesn't cause a commit if the statement being finalized is the last "active" statement. We work around this by wrapping sqlite-finalize with our own version that ensures sqlite-reset is called, which does The Right Thing=E2= =84=A2. * guix/store/database.scm (sqlite-finalize): new procedure that shadows the sqlite-finalize from (sqlite3). =2D-- guix/store/database.scm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/guix/store/database.scm b/guix/store/database.scm index ef52036ede..d4251e580e 100644 =2D-- a/guix/store/database.scm +++ b/guix/store/database.scm @@ -130,6 +130,36 @@ transaction after it finishes." If FILE doesn't exist, create it and initialize it as a new database." (call-with-database file (lambda (db) exp ...))) =20 +(define (sqlite-finalize stmt) + ;; Cached statements aren't reset when sqlite-finalize is invoked on + ;; them. This can cause problems with automatically-started transactions: + ;; + ;; "An implicit transaction (a transaction that is started automatically, + ;; not a transaction started by BEGIN) is committed automatically when t= he + ;; last active statement finishes. A statement finishes when its last cu= rsor + ;; closes, which is guaranteed to happen when the prepared statement is + ;; reset or finalized. Some statements might "finish" for the purpose of + ;; transaction control prior to being reset or finalized, but there is no + ;; guarantee of this." + ;; + ;; Thus, it's possible for an implicitly-started transaction to hang aro= und + ;; until sqlite-reset is called when the cached statement is next + ;; used. Because the transaction is committed automatically only when the + ;; *last active statement* finishes, the implicitly-started transaction = may + ;; later be upgraded to a write transaction (!) and this non-reset state= ment + ;; will still be keeping the transaction from committing until it is next + ;; used or the database connection is closed. This has the potential to = make + ;; (exclusive) write access to the database necessary for much longer th= an + ;; it should be. + ;; + ;; (see https://www.sqlite.org/lang_transaction.html) + ;; To work around this, we wrap sqlite-finalize so that sqlite-reset is + ;; always called. This will continue working even when the behavior is f= ixed + ;; in guile-sqlite3, since resetting twice doesn't cause any problems. We + ;; can remove this once the fixed guile-sqlite3 is widespread. + (sqlite-reset stmt) + ((@ (sqlite3) sqlite-finalize) stmt)) + (define (last-insert-row-id db) ;; XXX: (sqlite3) currently lacks bindings for 'sqlite3_last_insert_rowi= d'. ;; Work around that. =2D-=20 2.26.2 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-database-rewrite-query-procedures-in-terms-of-with-s.patch Content-Transfer-Encoding: quoted-printable From=20ee24ab21122b1c75a7d67d7062550e15e54ab62f Mon Sep 17 00:00:00 2001 From: Caleb Ristvedt Date: Mon, 1 Jun 2020 19:21:43 -0500 Subject: [PATCH 2/4] database: rewrite query procedures in terms of with-statement. Most of our queries would fail to finalize their statements properly if sql= ite returned an error during their execution. This resolves that, and also mak= es them somewhat more concise as a side-effect. This also makes some small changes to improve certain queries where behavior was strange or overly verbose. * guix/store/database.scm (call-with-statement): new procedure. (with-statement): new macro. (last-insert-row-id, path-id, update-or-insert, add-references): rewrite = to use with-statement. (update-or-insert): factor last-insert-row-id out of the end of both branches. (add-references): remove pointless last-insert-row-id call. * .dir-locals.el (with-statement): add indenting information. =2D-- .dir-locals.el | 1 + guix/store/database.scm | 53 ++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/.dir-locals.el b/.dir-locals.el index fcde914e60..a085269e85 100644 =2D-- a/.dir-locals.el +++ b/.dir-locals.el @@ -89,6 +89,7 @@ =20 (eval . (put 'with-database 'scheme-indent-function 2)) (eval . (put 'call-with-transaction 'scheme-indent-function 2)) + (eval . (put 'with-statement 'scheme-indent-function 3)) =20 (eval . (put 'call-with-container 'scheme-indent-function 1)) (eval . (put 'container-excursion 'scheme-indent-function 1)) diff --git a/guix/store/database.scm b/guix/store/database.scm index d4251e580e..2209da3df1 100644 =2D-- a/guix/store/database.scm +++ b/guix/store/database.scm @@ -160,14 +160,26 @@ If FILE doesn't exist, create it and initialize it as= a new database." (sqlite-reset stmt) ((@ (sqlite3) sqlite-finalize) stmt)) =20 +(define (call-with-statement db sql proc) + (let ((stmt (sqlite-prepare db sql #:cache? #t))) + (dynamic-wind + (const #t) + (lambda () + (proc stmt)) + (lambda () + (sqlite-finalize stmt))))) + +(define-syntax-rule (with-statement db sql stmt exp ...) + "Run EXP... with STMT bound to a prepared statement corresponding to the= sql +string SQL for DB." + (call-with-statement db sql + (lambda (stmt) exp ...))) + (define (last-insert-row-id db) ;; XXX: (sqlite3) currently lacks bindings for 'sqlite3_last_insert_rowi= d'. ;; Work around that. =2D (let* ((stmt (sqlite-prepare db "SELECT last_insert_rowid();" =2D #:cache? #t)) =2D (result (sqlite-fold cons '() stmt))) =2D (sqlite-finalize stmt) =2D (match result + (with-statement db "SELECT last_insert_rowid();" stmt + (match (sqlite-fold cons '() stmt) ((#(id)) id) (_ #f)))) =20 @@ -177,13 +189,11 @@ If FILE doesn't exist, create it and initialize it as= a new database." (define* (path-id db path) "If PATH exists in the 'ValidPaths' table, return its numerical identifier. Otherwise, return #f." =2D (let ((stmt (sqlite-prepare db path-id-sql #:cache? #t))) + (with-statement db path-id-sql stmt (sqlite-bind-arguments stmt #:path path) =2D (let ((result (sqlite-fold cons '() stmt))) =2D (sqlite-finalize stmt) =2D (match result =2D ((#(id) . _) id) =2D (_ #f))))) + (match (sqlite-fold cons '() stmt) + ((#(id) . _) id) + (_ #f)))) =20 (define update-sql "UPDATE ValidPaths SET hash =3D :hash, registrationTime =3D :time, deriv= er =3D @@ -200,20 +210,17 @@ and re-inserting instead of updating, which causes pr= oblems with foreign keys, of course. Returns the row id of the row that was modified or inserted." (let ((id (path-id db path))) (if id =2D (let ((stmt (sqlite-prepare db update-sql #:cache? #t))) + (with-statement db update-sql stmt (sqlite-bind-arguments stmt #:id id #:deriver deriver #:hash hash #:size nar-size #:time time) =2D (sqlite-fold cons '() stmt) =2D (sqlite-finalize stmt) =2D (last-insert-row-id db)) =2D (let ((stmt (sqlite-prepare db insert-sql #:cache? #t))) + (sqlite-fold cons '() stmt)) + (with-statement db insert-sql stmt (sqlite-bind-arguments stmt #:path path #:deriver deriver #:hash hash #:size nar-size #:time time) =2D (sqlite-fold cons '() stmt) ;execute it =2D (sqlite-finalize stmt) =2D (last-insert-row-id db))))) + (sqlite-fold cons '() stmt))) + (last-insert-row-id db))) =20 (define add-reference-sql "INSERT OR REPLACE INTO Refs (referrer, reference) VALUES (:referrer, :r= eference);") @@ -221,15 +228,13 @@ of course. Returns the row id of the row that was mod= ified or inserted." (define (add-references db referrer references) "REFERRER is the id of the referring store item, REFERENCES is a list ids of items referred to." =2D (let ((stmt (sqlite-prepare db add-reference-sql #:cache? #t))) + (with-statement db add-reference-sql stmt (for-each (lambda (reference) (sqlite-reset stmt) (sqlite-bind-arguments stmt #:referrer referrer #:reference reference) =2D (sqlite-fold cons '() stmt) ;execute it =2D (last-insert-row-id db)) =2D references) =2D (sqlite-finalize stmt))) + (sqlite-fold cons '() stmt)) + references))) =20 (define* (sqlite-register db #:key path (references '()) deriver hash nar-size time) =2D-=20 2.26.2 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0003-database-ensure-update-or-insert-is-run-within-a-tra.patch Content-Transfer-Encoding: quoted-printable From=207d34c27c33aed3e8a49b9796a62a8c19d352e653 Mon Sep 17 00:00:00 2001 From: Caleb Ristvedt Date: Mon, 1 Jun 2020 21:43:14 -0500 Subject: [PATCH 3/4] database: ensure update-or-insert is run within a transaction update-or-insert can break if an insert occurs between when it decides whet= her to update or insert and when it actually performs that operation. Putting = the check and the update/insert operation in the same transaction ensures that = the update/insert will only succeed if no other write has occurred in the middl= e. * guix/store/database.scm (call-with-savepoint): new procedure. (update-or-insert): use call-with-savepoint to ensure the read and the insert/update occur within the same transaction. =2D-- .dir-locals.el | 1 + guix/store/database.scm | 68 +++++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/.dir-locals.el b/.dir-locals.el index a085269e85..ef25cb100a 100644 =2D-- a/.dir-locals.el +++ b/.dir-locals.el @@ -90,6 +90,7 @@ (eval . (put 'with-database 'scheme-indent-function 2)) (eval . (put 'call-with-transaction 'scheme-indent-function 2)) (eval . (put 'with-statement 'scheme-indent-function 3)) + (eval . (put 'call-with-savepoint 'scheme-indent-function 1)) =20 (eval . (put 'call-with-container 'scheme-indent-function 1)) (eval . (put 'container-excursion 'scheme-indent-function 1)) diff --git a/guix/store/database.scm b/guix/store/database.scm index 2209da3df1..3955c48b1f 100644 =2D-- a/guix/store/database.scm +++ b/guix/store/database.scm @@ -120,6 +120,26 @@ transaction after it finishes." (begin (sqlite-exec db "rollback;") (throw 'sqlite-error who error description)))))) +(define* (call-with-savepoint db proc + #:optional (savepoint-name "SomeSavepoint")) + "Call PROC after creating a savepoint named SAVEPOINT-NAME. If PROC exi= ts +abnormally, rollback to that savepoint. In all cases, remove the savepoint +prior to returning." + (define (exec sql) + (with-statement db sql stmt + (sqlite-fold cons '() stmt))) + + (dynamic-wind + (lambda () + (exec (string-append "SAVEPOINT " savepoint-name ";"))) + (lambda () + (catch #t + proc + (lambda args + (exec (string-append "ROLLBACK TO " savepoint-name ";")) + (apply throw args)))) + (lambda () + (exec (string-append "RELEASE " savepoint-name ";"))))) =20 (define %default-database-file ;; Default location of the store database. @@ -208,19 +228,41 @@ VALUES (:path, :hash, :time, :deriver, :size)") doesn't exactly have... they've got something close, but it involves delet= ing and re-inserting instead of updating, which causes problems with foreign k= eys, of course. Returns the row id of the row that was modified or inserted." =2D (let ((id (path-id db path))) =2D (if id =2D (with-statement db update-sql stmt =2D (sqlite-bind-arguments stmt #:id id =2D #:deriver deriver =2D #:hash hash #:size nar-size #:time time) =2D (sqlite-fold cons '() stmt)) =2D (with-statement db insert-sql stmt =2D (sqlite-bind-arguments stmt =2D #:path path #:deriver deriver =2D #:hash hash #:size nar-size #:time time) =2D (sqlite-fold cons '() stmt))) =2D (last-insert-row-id db))) + + ;; It's important that querying the path-id and the insert/update operat= ion + ;; take place in the same transaction, as otherwise some other + ;; process/thread/fiber could register the same path between when we che= ck + ;; whether it's already registered and when we register it, resulting in + ;; duplicate paths (which, due to a 'unique' constraint, would cause an + ;; exception to be thrown). With the default journaling mode this will + ;; prevent writes from occurring during that sensitive time, but with WAL + ;; mode it will instead arrange to return SQLITE_BUSY when a write occurs + ;; between the start of a read transaction and its upgrading to a write + ;; transaction (see https://sqlite.org/rescode.html#busy_snapshot). + ;; Experimentally, it seems this SQLITE_BUSY will ignore a busy_timeout = and + ;; immediately return (makes sense, since waiting won't change anything). + + ;; Note that when that kind of SQLITE_BUSY error is returned, it will ke= ep + ;; being returned every time we try to upgrade the same outermost + ;; transaction to a write transaction. So when retrying, we have to res= tart + ;; the *outermost* write transaction. We can't inherently tell whether + ;; we're the outermost write transaction, so we leave the retry-handling= to + ;; the caller. + (call-with-savepoint db + (lambda () + (let ((id (path-id db path))) + (if id + (with-statement db update-sql stmt + (sqlite-bind-arguments stmt #:id id + #:deriver deriver + #:hash hash #:size nar-size #:time ti= me) + (sqlite-fold cons '() stmt)) + (with-statement db insert-sql stmt + (sqlite-bind-arguments stmt + #:path path #:deriver deriver + #:hash hash #:size nar-size #:time ti= me) + (sqlite-fold cons '() stmt))) + (last-insert-row-id db))))) =20 (define add-reference-sql "INSERT OR REPLACE INTO Refs (referrer, reference) VALUES (:referrer, :r= eference);") =2D-=20 2.26.2 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0004-database-separate-transaction-handling-and-retry-han.patch Content-Transfer-Encoding: quoted-printable From=20e30271728dfb23324c981d226c752b17689c9eef Mon Sep 17 00:00:00 2001 From: Caleb Ristvedt Date: Mon, 1 Jun 2020 22:15:21 -0500 Subject: [PATCH 4/4] database: separate transaction-handling and retry-handling. Previously call-with-transaction would both retry when SQLITE_BUSY errors w= ere thrown and do what its name suggested (start and rollback/commit a transaction). This changes it to do only what its name implies, which simplifies its implementation. Retrying is provided by the new call-with-SQLITE_BUSY-retrying procedure. * guix/store/database.scm (call-with-transaction): no longer restarts, new #:restartable? argument controls whether "begin" or "begin immediate" is used. (call-with-SQLITE_BUSY-retrying, call-with-retrying-transaction, call-with-retrying-savepoint): new procedures. (register-items): use call-with-retrying-transaction to preserve old behavior. * .dir-locals.el (call-with-retrying-transaction, call-with-retrying-savepoint): add indentation information. =2D-- .dir-locals.el | 2 ++ guix/store/database.scm | 69 +++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/.dir-locals.el b/.dir-locals.el index ef25cb100a..e9dccd0511 100644 =2D-- a/.dir-locals.el +++ b/.dir-locals.el @@ -90,7 +90,9 @@ (eval . (put 'with-database 'scheme-indent-function 2)) (eval . (put 'call-with-transaction 'scheme-indent-function 2)) (eval . (put 'with-statement 'scheme-indent-function 3)) + (eval . (put 'call-with-retrying-transaction 'scheme-indent-function 2)) (eval . (put 'call-with-savepoint 'scheme-indent-function 1)) + (eval . (put 'call-with-retrying-savepoint 'scheme-indent-function 1)) =20 (eval . (put 'call-with-container 'scheme-indent-function 1)) (eval . (put 'container-excursion 'scheme-indent-function 1)) diff --git a/guix/store/database.scm b/guix/store/database.scm index 3955c48b1f..2a78379dac 100644 =2D-- a/guix/store/database.scm +++ b/guix/store/database.scm @@ -99,27 +99,44 @@ create it and initialize it as a new database." ;; XXX: missing in guile-sqlite3@0.1.0 (define SQLITE_BUSY 5) =20 =2D(define (call-with-transaction db proc) =2D "Start a transaction with DB (make as many attempts as necessary) and = run =2DPROC. If PROC exits abnormally, abort the transaction, otherwise commit= the =2Dtransaction after it finishes." +(define (call-with-SQLITE_BUSY-retrying thunk) + "Call THUNK, retrying as long as it exits abnormally due to SQLITE_BUSY +errors." (catch 'sqlite-error + thunk + (lambda (key who code errmsg) + (if (=3D code SQLITE_BUSY) + (call-with-SQLITE_BUSY-retrying thunk) + (throw key who code errmsg))))) + + + +(define* (call-with-transaction db proc #:key restartable?) + "Start a transaction with DB and run PROC. If PROC exits abnormally, ab= ort +the transaction, otherwise commit the transaction after it finishes. +RESTARTABLE? may be set to a non-#f value when it is safe to run PROC mult= iple +times. This may reduce contention for the database somewhat." + (define (exec sql) + (with-statement db sql stmt + (sqlite-fold cons '() stmt))) + ;; We might use begin immediate here so that if we need to retry, we fig= ure + ;; that out immediately rather than because some SQLITE_BUSY exception g= ets + ;; thrown partway through PROC - in which case the part already executed + ;; (which may contain side-effects!) might have to be executed again for + ;; every retry. + (exec (if restartable? "begin;" "begin immediate;")) + (catch #t (lambda () =2D ;; We use begin immediate here so that if we need to retry, we =2D ;; figure that out immediately rather than because some SQLITE_BUSY =2D ;; exception gets thrown partway through PROC - in which case the =2D ;; part already executed (which may contain side-effects!) would be =2D ;; executed again for every retry. =2D (sqlite-exec db "begin immediate;") =2D (let ((result (proc))) =2D (sqlite-exec db "commit;") =2D result)) =2D (lambda (key who error description) =2D (if (=3D error SQLITE_BUSY) =2D (call-with-transaction db proc) =2D (begin =2D (sqlite-exec db "rollback;") =2D (throw 'sqlite-error who error description)))))) + (let-values ((result (proc))) + (exec "commit;") + (apply values result))) + (lambda args + ;; The roll back may or may not have occurred automatically when the + ;; error was generated. If it has occurred, this does nothing but si= gnal + ;; an error. If it hasn't occurred, this needs to be done. + (false-if-exception (exec "rollback;")) + (apply throw args)))) + (define* (call-with-savepoint db proc #:optional (savepoint-name "SomeSavepoint")) "Call PROC after creating a savepoint named SAVEPOINT-NAME. If PROC exi= ts @@ -141,6 +158,18 @@ prior to returning." (lambda () (exec (string-append "RELEASE " savepoint-name ";"))))) =20 +(define* (call-with-retrying-transaction db proc #:key restartable?) + (call-with-SQLITE_BUSY-retrying + (lambda () + (call-with-transaction db proc #:restartable? restartable?)))) + +(define* (call-with-retrying-savepoint db proc + #:optional (savepoint-name + "SomeSavepoint")) + (call-with-SQLITE_BUSY-retrying + (lambda () + (call-with-savepoint db proc savepoint-name)))) + (define %default-database-file ;; Default location of the store database. (string-append %store-database-directory "/db.sqlite")) @@ -431,7 +460,7 @@ Write a progress report to LOG-PORT." (mkdir-p db-dir) (parameterize ((sql-schema schema)) (with-database (string-append db-dir "/db.sqlite") db =2D (call-with-transaction db + (call-with-retrying-transaction db (lambda () (let* ((prefix (format #f "registering ~a items" (length ite= ms))) (progress (progress-reporter/bar (length items) =2D-=20 2.26.2 --=-=-=-- --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEdNapMPRLm4SepVYGwWaqSV9/GJwFAl7V8koACgkQwWaqSV9/ GJwHXgf/R/mpXxFexv9B/V22BwF8+bNmDr6L0KVdFmaKz15nmPbMnnLtqmJTkrik vbEtwQ9MT4we6yNRD76VaVEe+iE1Yk9WvCyBNUcoPL07dlzY/hN+0frSjQI4MqSc GIktVbdt6Bf3bloCl5SHg2KrGEYY+ptWliEuY3AfQnBL/7YoSKfd4me5TLa1PB/4 RhyVqVFJHq/jeSQvirDYQoMCiRaDCxu8g+xV5/7ITE3Ue+gkM2aYKSrWz8cFhiJ4 /S3zFZYuI31d2J8jdkbcz5tuVG/ALpTTCIJa8cwIpRB2Yon6yZN4zoDX8ue3P8we qEnsPdkxSPu7lckVk7OqQNQTvxpknQ== =2ARa -----END PGP SIGNATURE----- --==-=-=--