Speed up findBuildDependencyInQueue

This was taking a long time due to the giant SQL query.

Issue #99.
This commit is contained in:
Eelco Dolstra 2013-06-07 19:51:15 +00:00
parent 8e36343b62
commit 5d9b7c6ab2

View file

@ -51,12 +51,15 @@ sub unlockDeadBuilds {
# Given a build, return an arbitrary queued build on which this build # Given a build, return an arbitrary queued build on which this build
# depends; or undef if no such build exists. # depends; or undef if no such build exists.
sub findBuildDependencyInQueue { sub findBuildDependencyInQueue {
my ($build) = @_; my ($buildsByDrv, $build) = @_;
my @deps = grep { /\.drv$/ && $_ ne $build->drvpath } computeFSClosure(0, 0, $build->drvpath); my @deps = grep { /\.drv$/ && $_ ne $build->drvpath } computeFSClosure(0, 0, $build->drvpath);
return unless scalar @deps > 0; return unless scalar @deps > 0;
return $db->resultset('Builds')->search( foreach my $d (@deps) {
{ drvpath => [ @deps ], finished => 0, enabled => 1 }, my $b = $buildsByDrv->{$d};
{ join => ['project'], rows => 1 })->single; next unless defined $b;
return $db->resultset('Builds')->find($b);
}
return undef;
} }
@ -77,6 +80,12 @@ sub checkBuilds {
txn_do($db, sub { 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. # Get the system types for the runnable builds.
my @systemTypes = $db->resultset('Builds')->search( my @systemTypes = $db->resultset('Builds')->search(
{ finished => 0, busy => 0, enabled => 1 }, { finished => 0, busy => 0, enabled => 1 },
@ -109,7 +118,7 @@ sub checkBuilds {
# but it ensures that Nix builds are done as part of # but it ensures that Nix builds are done as part of
# their corresponding Hydra builds, rather than as a # their corresponding Hydra builds, rather than as a
# dependency of some other Hydra build. # dependency of some other Hydra build.
while (my $dep = findBuildDependencyInQueue($build)) { while (my $dep = findBuildDependencyInQueue($buildsByDrv, $build)) {
$build = $dep; $build = $dep;
} }
next if $build->busy; next if $build->busy;