From 5d9b7c6ab2eb44c3aeaf34464d1b68ac1f7e62e6 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 7 Jun 2013 19:51:15 +0000 Subject: [PATCH] Speed up findBuildDependencyInQueue This was taking a long time due to the giant SQL query. Issue #99. --- src/script/hydra-queue-runner | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/script/hydra-queue-runner b/src/script/hydra-queue-runner index 43a6ed16..0b9224c1 100755 --- a/src/script/hydra-queue-runner +++ b/src/script/hydra-queue-runner @@ -51,12 +51,15 @@ sub unlockDeadBuilds { # Given a build, return an arbitrary queued build on which this build # depends; or undef if no such build exists. sub findBuildDependencyInQueue { - my ($build) = @_; + my ($buildsByDrv, $build) = @_; my @deps = grep { /\.drv$/ && $_ ne $build->drvpath } computeFSClosure(0, 0, $build->drvpath); return unless scalar @deps > 0; - return $db->resultset('Builds')->search( - { drvpath => [ @deps ], finished => 0, enabled => 1 }, - { join => ['project'], rows => 1 })->single; + foreach my $d (@deps) { + my $b = $buildsByDrv->{$d}; + next unless defined $b; + return $db->resultset('Builds')->find($b); + } + return undef; } @@ -77,6 +80,12 @@ sub checkBuilds { txn_do($db, sub { + # Cache scheduled by derivation path to speed up + # findBuildDependencyInQueue. + my $buildsByDrv = {}; + $buildsByDrv->{$_->drvpath} = $_->id + foreach $db->resultset('Builds')->search({ finished => 0, enabled => 1 }, { join => ['project'] }); + # Get the system types for the runnable builds. my @systemTypes = $db->resultset('Builds')->search( { finished => 0, busy => 0, enabled => 1 }, @@ -109,7 +118,7 @@ sub checkBuilds { # but it ensures that Nix builds are done as part of # their corresponding Hydra builds, rather than as a # dependency of some other Hydra build. - while (my $dep = findBuildDependencyInQueue($build)) { + while (my $dep = findBuildDependencyInQueue($buildsByDrv, $build)) { $build = $dep; } next if $build->busy;