From patchwork Fri Jul 8 18:26:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Jackson X-Patchwork-Id: 9221679 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E15456089D for ; Fri, 8 Jul 2016 18:29:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D580F28375 for ; Fri, 8 Jul 2016 18:29:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CA2352837F; Fri, 8 Jul 2016 18:29:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 55B5628375 for ; Fri, 8 Jul 2016 18:29:00 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bLaUJ-0004B1-Fo; Fri, 08 Jul 2016 18:26:55 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bLaUE-0003uC-Fy for xen-devel@lists.xenproject.org; Fri, 08 Jul 2016 18:26:50 +0000 Received: from [85.158.137.68] by server-12.bemta-3.messagelabs.com id 7A/C9-32245-960FF775; Fri, 08 Jul 2016 18:26:49 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFuplkeJIrShJLcpLzFFi42JxWrrBXjfjQ32 4wex7lhbft0xmcmD0OPzhCksAYxRrZl5SfkUCa8ahWdPZCzaYVPzd8JmxgfGIchcjJ4eEgL/E uhnTWUBsNgFdiaYtf9m6GDk4RARUJG7vNQAxmQVCJXa8sASpEBYwl1g36TsziM0CVHF1ygN2E JtXwFPi/dpLzBAT5STOH/8JZnMCxXctuQlWIyTgIdH8q4kRZKSQgJrE3PXxEK2CEidnPgE7gF lAQuLgixfMICUSAtwSf7vtJzDyzUJSNQtJ1QJGplWMGsWpRWWpRbpGBnpJRZnpGSW5iZk5uoY Gxnq5qcXFiempOYlJxXrJ+bmbGIHBVM/AwLiDsfmE3yFGSQ4mJVHefc/qw4X4kvJTKjMSizPi i0pzUosPMcpwcChJ8Oa8B8oJFqWmp1akZeYAwxomLcHBoyTCWw6S5i0uSMwtzkyHSJ1iVJQS5 00DSQiAJDJK8+DaYLF0iVFWSpiXkYGBQYinILUoN7MEVf4VozgHo5Iwbz3IFJ7MvBK46a+AFj MBLTYIAFtckoiQkmpgNJzJ6Sm85tQfwxv7lQ67KK/v/Cf6pPYCW8LaxWc0M5vFbioXhF3yuZK Qy3Fnzq59c7a9eH36xGfXvrvVwt5L3vl4P1nzRuhnd9ucrjc7lwQtmP1xQ0rHJZF9r4+4dt7n 15iu/LS5TWtRpZ6v33kdr8emb5Jkfn85XJob99H34EZx6V0/snoVFZRYijMSDbWYi4oTAf+cA 4ugAgAA X-Env-Sender: prvs=9904f56b2=Ian.Jackson@citrix.com X-Msg-Ref: server-15.tower-31.messagelabs.com!1468002407!45656047!1 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 36299 invoked from network); 8 Jul 2016 18:26:48 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-15.tower-31.messagelabs.com with RC4-SHA encrypted SMTP; 8 Jul 2016 18:26:48 -0000 X-IronPort-AV: E=Sophos;i="5.28,331,1464652800"; d="scan'208";a="372092140" From: Ian Jackson To: Date: Fri, 8 Jul 2016 19:26:25 +0100 Message-ID: <1468002385-4407-34-git-send-email-ian.jackson@eu.citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1468002385-4407-1-git-send-email-ian.jackson@eu.citrix.com> References: <1468002385-4407-1-git-send-email-ian.jackson@eu.citrix.com> MIME-Version: 1.0 X-DLP: MIA1 Cc: Ian Jackson Subject: [Xen-devel] [OSSTEST PATCH 33/33] tcl daemons: Use with-db X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Fixes a number of latent bugs where the jobdb refcount might get out of step. Signed-off-by: Ian Jackson --- sg-execute-flight | 82 ++++++++++++++++++------------------- tcl/JobDB-Executive.tcl | 106 ++++++++++++++++++++++++------------------------ 2 files changed, 93 insertions(+), 95 deletions(-) diff --git a/sg-execute-flight b/sg-execute-flight index 14c8cda..7932c29 100755 --- a/sg-execute-flight +++ b/sg-execute-flight @@ -28,48 +28,48 @@ proc check {} { if {$stopping} return - jobdb::db-open - - set nqueued [jobdb::db-execute-array dummy " - SELECT job FROM jobs j - WHERE j.flight = $flight - AND j.status = 'queued' - LIMIT 1 - "] - - set nrunning [llength $running] - log "flight $flight nqueued=$nqueued nrunning=$nrunning" - - if {!$nqueued && !$nrunning} { - prequit finished - exec ./cs-flight-bless $flight $blessing running 2>@ stderr - exit 0 - } + jobdb::with-db { + + set nqueued [jobdb::db-execute-array dummy " + SELECT job FROM jobs j + WHERE j.flight = $flight + AND j.status = 'queued' + LIMIT 1 + "] + + set nrunning [llength $running] + log "flight $flight nqueued=$nqueued nrunning=$nrunning" + + if {!$nqueued && !$nrunning} { + prequit finished + exec ./cs-flight-bless $flight $blessing running 2>@ stderr + exit 0 + } + + jobdb::db-execute-array jobinfo " + SELECT * FROM jobs j + WHERE j.flight = $flight + AND j.status = 'queued' + AND 0 = (SELECT count(*) FROM jobs d + WHERE d.flight = $flight + AND ( d.status = 'queued' + OR d.status = 'preparing' + OR d.status = 'running' + OR d.status = 'retriable' ) + AND (d.job IN (SELECT val FROM runvars r + WHERE r.flight = $flight + AND r.job = j.job + AND r.name LIKE '%job') + OR (d.flight || '.' || d.job) IN + (SELECT val FROM runvars r + WHERE r.flight = $flight + AND r.job = j.job + AND r.name LIKE '%job')) + ) + ORDER BY job + " maybe-spawn-job - jobdb::db-execute-array jobinfo " - SELECT * FROM jobs j - WHERE j.flight = $flight - AND j.status = 'queued' - AND 0 = (SELECT count(*) FROM jobs d - WHERE d.flight = $flight - AND ( d.status = 'queued' - OR d.status = 'preparing' - OR d.status = 'running' - OR d.status = 'retriable' ) - AND (d.job IN (SELECT val FROM runvars r - WHERE r.flight = $flight - AND r.job = j.job - AND r.name LIKE '%job') - OR (d.flight || '.' || d.job) IN - (SELECT val FROM runvars r - WHERE r.flight = $flight - AND r.job = j.job - AND r.name LIKE '%job')) - ) - ORDER BY job - " maybe-spawn-job - - jobdb::db-close + } } proc prequit {why} { diff --git a/tcl/JobDB-Executive.tcl b/tcl/JobDB-Executive.tcl index 07a0438..0f450c1 100644 --- a/tcl/JobDB-Executive.tcl +++ b/tcl/JobDB-Executive.tcl @@ -28,36 +28,36 @@ proc logputs {f m} { proc prepare {job} { global flight jobinfo - db-open - set found 0 - db-execute-array jobinfo " - SELECT job, status, recipe FROM jobs - WHERE flight = [pg_quote $flight] - AND job = [pg_quote $job] - " { - switch -exact -- $jobinfo(status) { - queued - preparing - retriable - play { incr found } - default { - error "job $flight.$job status $jobinfo(status)" + with-db { + set found 0 + db-execute-array jobinfo " + SELECT job, status, recipe FROM jobs + WHERE flight = [pg_quote $flight] + AND job = [pg_quote $job] + " { + switch -exact -- $jobinfo(status) { + queued - preparing - retriable - play { incr found } + default { + error "job $flight.$job status $jobinfo(status)" + } } } - } - if {!$found} { - error "job $flight.$job not found" - } + if {!$found} { + error "job $flight.$job not found" + } - setstatus preparing - db-close + setstatus preparing + } } proc job-set-status-unlocked {flight job st} { - db-open - db-execute " - UPDATE jobs SET status='$st' - WHERE flight=$flight AND job='$job' - AND status<>'aborted' AND status<>'broken' - " - db-close + with-db { + db-execute " + UPDATE jobs SET status='$st' + WHERE flight=$flight AND job='$job' + AND status<>'aborted' AND status<>'broken' + " + } } proc job-set-status {flight job st} { @@ -221,38 +221,36 @@ proc lock-tables {tables} { proc spawn-step-begin {flight job ts stepnovar} { upvar 1 $stepnovar stepno - db-open - - db-execute BEGIN - db-execute "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE" - if {[catch { - lock-tables flights - db-execute-array stepinfo " - SELECT max(stepno) AS maxstep FROM steps - WHERE flight=[pg_quote $flight] AND job=[pg_quote $job] - " - set stepno $stepinfo(maxstep) - if {[string length $stepno]} { - incr stepno - } else { - set stepno 1 + with-db { + db-execute BEGIN + db-execute "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE" + if {[catch { + lock-tables flights + db-execute-array stepinfo " + SELECT max(stepno) AS maxstep FROM steps + WHERE flight=[pg_quote $flight] AND job=[pg_quote $job] + " + set stepno $stepinfo(maxstep) + if {[string length $stepno]} { + incr stepno + } else { + set stepno 1 + } + db-execute " + INSERT INTO steps + VALUES ([pg_quote $flight], [pg_quote $job], $stepno, + [pg_quote $ts], 'running', + 'STARTING') + " + db-execute COMMIT + } emsg]} { + global errorInfo errorCode + set ei $errorInfo + set ec $errorCode + catch { db-execute ROLLBACK } + error $emsg $ei $ec } - db-execute " - INSERT INTO steps - VALUES ([pg_quote $flight], [pg_quote $job], $stepno, - [pg_quote $ts], 'running', - 'STARTING') - " - db-execute COMMIT - } emsg]} { - global errorInfo errorCode - set ei $errorInfo - set ec $errorCode - catch { db-execute ROLLBACK } - db-close - error $emsg $ei $ec } - db-close } proc spawn-step-commit {flight job stepno testid} {