From patchwork Thu Jan 7 19:38:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Jackson X-Patchwork-Id: 7979451 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B62C6BEEED for ; Thu, 7 Jan 2016 19:41:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E39C02013D for ; Thu, 7 Jan 2016 19:41:52 +0000 (UTC) Received: from lists.xen.org (lists.xenproject.org [50.57.142.19]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1691720138 for ; Thu, 7 Jan 2016 19:41:52 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aHGOF-0003TQ-O3; Thu, 07 Jan 2016 19:38:31 +0000 Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aHGOE-0003SS-Le for xen-devel@lists.xenproject.org; Thu, 07 Jan 2016 19:38:30 +0000 Received: from [85.158.143.35] by server-2.bemta-4.messagelabs.com id 03/E7-08977-5BEBE865; Thu, 07 Jan 2016 19:38:29 +0000 X-Env-Sender: prvs=807bdda5f=Ian.Jackson@citrix.com X-Msg-Ref: server-3.tower-21.messagelabs.com!1452195506!8498330!3 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 7.35.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 22720 invoked from network); 7 Jan 2016 19:38:29 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-3.tower-21.messagelabs.com with RC4-SHA encrypted SMTP; 7 Jan 2016 19:38:29 -0000 X-IronPort-AV: E=Sophos;i="5.20,534,1444694400"; d="scan'208";a="323534840" From: Ian Jackson To: Date: Thu, 7 Jan 2016 19:38:15 +0000 Message-ID: <1452195496-16016-7-git-send-email-ian.jackson@eu.citrix.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1452195496-16016-1-git-send-email-ian.jackson@eu.citrix.com> References: <1452195496-16016-1-git-send-email-ian.jackson@eu.citrix.com> MIME-Version: 1.0 X-DLP: MIA2 Cc: Ian Jackson , Ian Campbell Subject: [Xen-devel] [OSSTEST PATCH 6/7] Database locking: Tcl: Retry only on DEADLOCK DETECTED X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Use the new errorCode coming out of db-execute* to tell when the error is that we got a database deadlock, which is the situation in which we should retry. This involves combining the two catch blocks, so that there is only one error handling strategy. Previously errors on COMMIT would be retried and others would not. Now errors anywhere might be retried but only if the DB indicated deadlock. We now unconditionally execute ROLLBACK. This is more correct, since we always previously executed BEGIN. And, we pass the errorInfo and errorCode from the $body to the caller. I have tested this with a test db instance, using contrived means to generate a database deadlock, and it does actually retry. Signed-off-by: Ian Jackson --- tcl/JobDB-Executive.tcl | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/tcl/JobDB-Executive.tcl b/tcl/JobDB-Executive.tcl index ed9abbb..63db4f0 100644 --- a/tcl/JobDB-Executive.tcl +++ b/tcl/JobDB-Executive.tcl @@ -283,25 +283,27 @@ proc transaction {tables script} { db-execute "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE" lock-tables $tables uplevel 1 $script + db-execute COMMIT } result] - if {!$rc} { - if {[catch { - db-execute COMMIT - } emsg]} { - puts "commit failed: $emsg; retrying ..." - db-execute ROLLBACK - if {[incr retries -1] <= 0} { - error \ - "commit failed, too many retries: $emsg\n$errorInfo\n$errorCode\n" + set ei $errorInfo + set ec $errorCode + if {$rc} { + db-execute ROLLBACK + switch -glob $errorCode { + {OSSTEST-PSQL * 40P01} { + # DEADLOCK DETECTED + puts "transaction deadlock ($result) retrying ..." + if {[incr retries -1] <= 0} { + error \ + "transaction failed, too many retries: $result\n$errorInfo\n$errorCode\n" + } + after 500 + continue } - after 500 - continue } - } else { - db-execute ROLLBACK } db-close - return -code $rc $result + return -code $rc -errorinfo $ei -errorcode $ec $result } }