2015-08-17 12:18:07 +00:00
|
|
|
#! /usr/bin/env perl
|
2009-01-13 14:02:07 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use File::Path;
|
2014-07-14 11:18:07 +00:00
|
|
|
use File::stat;
|
2009-01-13 14:02:07 +00:00
|
|
|
use File::Basename;
|
2011-11-30 14:25:28 +00:00
|
|
|
use Nix::Store;
|
2009-01-13 14:02:07 +00:00
|
|
|
use Hydra::Schema;
|
|
|
|
use Hydra::Helper::Nix;
|
2012-03-13 11:10:19 +00:00
|
|
|
use Hydra::Model::DB;
|
2009-02-06 14:17:25 +00:00
|
|
|
use POSIX qw(strftime);
|
2009-01-13 14:02:07 +00:00
|
|
|
|
2012-03-13 11:10:19 +00:00
|
|
|
my $db = Hydra::Model::DB->new();
|
2009-01-13 14:02:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
my %roots;
|
|
|
|
|
2012-03-07 14:02:31 +00:00
|
|
|
sub addRoot {
|
2009-01-13 14:02:07 +00:00
|
|
|
my ($path) = @_;
|
2012-03-07 14:02:31 +00:00
|
|
|
registerRoot($path);
|
2009-01-13 14:02:07 +00:00
|
|
|
$roots{$path} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-11 12:09:50 +00:00
|
|
|
my @columns =
|
|
|
|
( "id", "project", "jobset", "job", "system", "finished", "drvpath", "timestamp", "buildstatus"
|
|
|
|
, { "outpaths" => \ "(select string_agg(path, ' ') from BuildOutputs where build = me.id)" }
|
|
|
|
);
|
2012-03-07 14:02:31 +00:00
|
|
|
|
2015-06-11 11:58:23 +00:00
|
|
|
my %seenBuilds;
|
|
|
|
|
2009-02-06 14:17:25 +00:00
|
|
|
sub keepBuild {
|
2014-04-08 15:54:11 +00:00
|
|
|
my ($build, $keepFailedDrvs) = @_;
|
2015-06-11 11:58:23 +00:00
|
|
|
return if defined $seenBuilds{$build->id};
|
|
|
|
$seenBuilds{$build->id} = 1;
|
2012-03-07 14:02:31 +00:00
|
|
|
print STDERR " keeping ", ($build->finished ? "" : "scheduled "), "build ", $build->id, " (",
|
|
|
|
$build->get_column('project'), ":", $build->get_column('jobset'), ":", $build->get_column('job'), "; ",
|
2013-02-13 16:49:28 +00:00
|
|
|
$build->system, "; ",
|
2010-02-15 10:21:11 +00:00
|
|
|
strftime("%Y-%m-%d %H:%M:%S", localtime($build->timestamp)), ")\n";
|
2014-04-08 15:54:11 +00:00
|
|
|
if ($build->finished && ($build->buildstatus == 0 || $build->buildstatus == 6)) {
|
2015-06-11 12:09:50 +00:00
|
|
|
foreach my $path (split / /, $build->get_column('outpaths')) {
|
|
|
|
if (isValidPath($path)) {
|
|
|
|
addRoot $path;
|
2014-04-08 15:54:11 +00:00
|
|
|
} else {
|
2015-06-11 12:09:50 +00:00
|
|
|
print STDERR " warning: output ", $path, " has disappeared\n" if $build->finished;
|
2014-04-08 15:54:11 +00:00
|
|
|
}
|
2013-02-13 16:49:28 +00:00
|
|
|
}
|
2012-03-07 14:02:31 +00:00
|
|
|
}
|
2014-04-08 15:54:11 +00:00
|
|
|
if (!$build->finished || ($keepFailedDrvs && $build->buildstatus != 0)) {
|
2013-02-13 16:49:28 +00:00
|
|
|
if (isValidPath($build->drvpath)) {
|
|
|
|
addRoot $build->drvpath;
|
|
|
|
} else {
|
|
|
|
print STDERR " warning: derivation ", $build->drvpath, " has disappeared\n";
|
|
|
|
}
|
2009-02-06 14:17:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-13 14:02:07 +00:00
|
|
|
|
2014-07-14 11:18:07 +00:00
|
|
|
# Read the current GC roots.
|
2012-03-07 14:12:47 +00:00
|
|
|
print STDERR "*** reading current roots...\n";
|
|
|
|
my $gcRootsDir = getGCRootsDir;
|
|
|
|
opendir DIR, $gcRootsDir or die;
|
|
|
|
my @roots = readdir DIR;
|
|
|
|
closedir DIR;
|
|
|
|
|
|
|
|
|
2013-09-18 11:10:10 +00:00
|
|
|
# For scheduled builds, we register the derivation as a GC root.
|
|
|
|
print STDERR "*** looking for scheduled builds\n";
|
2014-04-08 15:54:11 +00:00
|
|
|
keepBuild($_, 0) foreach $db->resultset('Builds')->search({ finished => 0 }, { columns => [ @columns ] });
|
2013-09-18 11:10:10 +00:00
|
|
|
|
|
|
|
|
2012-03-07 14:02:31 +00:00
|
|
|
# Keep every build in every release of every project.
|
|
|
|
print STDERR "*** looking for release members\n";
|
2014-04-08 15:54:11 +00:00
|
|
|
keepBuild($_, 0) foreach $db->resultset('Builds')->search_literal(
|
2013-02-13 16:49:28 +00:00
|
|
|
"exists (select 1 from releasemembers where build = me.id)",
|
|
|
|
{ order_by => ["project", "jobset", "job", "id"], columns => [ @columns ] });
|
2012-03-07 14:02:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Keep all builds that have been marked as "keep".
|
|
|
|
print STDERR "*** looking for kept builds\n";
|
|
|
|
my @buildsToKeep = $db->resultset('Builds')->search(
|
|
|
|
{ finished => 1, keep => 1 }, { order_by => ["project", "jobset", "job", "id"], columns => [ @columns ] });
|
2014-04-08 15:54:11 +00:00
|
|
|
keepBuild($_, 0) foreach @buildsToKeep;
|
2012-03-07 14:02:31 +00:00
|
|
|
|
2009-01-13 14:02:07 +00:00
|
|
|
|
2012-03-07 14:02:31 +00:00
|
|
|
# Go over all projects.
|
|
|
|
foreach my $project ($db->resultset('Projects')->search({}, { order_by => ["name"] })) {
|
2009-01-13 14:02:07 +00:00
|
|
|
|
2010-11-19 11:01:31 +00:00
|
|
|
# Go over all jobsets in this project.
|
2012-03-07 14:02:31 +00:00
|
|
|
foreach my $jobset ($project->jobsets->search({}, { order_by => ["name" ]})) {
|
2010-11-19 11:01:31 +00:00
|
|
|
my $keepnr = $jobset->keepnr;
|
|
|
|
|
2013-02-13 16:49:28 +00:00
|
|
|
# If the jobset has been hidden and disabled for more than one
|
|
|
|
# week, then don't keep its builds anymore.
|
2012-05-11 07:11:07 +00:00
|
|
|
if ($jobset->enabled == 0 && ($project->hidden == 1 || $jobset->hidden == 1) && (time() - ($jobset->lastcheckedtime || 0) > (7 * 24 * 3600))) {
|
2010-11-23 09:05:09 +00:00
|
|
|
print STDERR "*** skipping disabled jobset ", $project->name, ":", $jobset->name, "\n";
|
|
|
|
next;
|
|
|
|
}
|
2011-04-01 07:40:06 +00:00
|
|
|
|
2013-09-18 11:10:10 +00:00
|
|
|
print STDERR "*** looking for all builds in the unfinished and $keepnr most recent finished evaluations of jobset ",
|
2013-02-13 16:49:28 +00:00
|
|
|
$project->name, ":", $jobset->name, "\n";
|
2012-03-07 14:02:31 +00:00
|
|
|
|
2013-09-18 11:10:10 +00:00
|
|
|
my @evals;
|
|
|
|
|
|
|
|
# Get the unfinished evals.
|
|
|
|
push @evals, $_->get_column("eval") foreach $jobset->builds->search(
|
|
|
|
{ finished => 0 },
|
|
|
|
{ join => "jobsetevalmembers", select => "jobsetevalmembers.eval", as => "eval", distinct => 1 });
|
|
|
|
|
|
|
|
# Get the N most recent finished evals.
|
|
|
|
if ($keepnr) {
|
|
|
|
push @evals, $_->get_column("id") foreach $jobset->jobsetevals->search(
|
|
|
|
{ hasNewBuilds => 1 },
|
|
|
|
{ where => \ "not exists (select 1 from builds b join jobsetevalmembers m on b.id = m.build where m.eval = me.id and b.finished = 0)"
|
|
|
|
, order_by => "id desc", rows => $keepnr });
|
|
|
|
}
|
|
|
|
|
2015-06-11 11:58:23 +00:00
|
|
|
# Note: we also keep the derivations of failed builds so that
|
|
|
|
# they can be restarted.
|
2014-04-08 15:54:11 +00:00
|
|
|
keepBuild($_, 1) foreach $jobset->builds->search(
|
|
|
|
{ id => { -in => $db->resultset('JobsetEvalMembers')->search({ eval => { -in => [@evals] } }, { select => "build" })->as_query }
|
2015-06-11 11:58:23 +00:00
|
|
|
, finished => 1
|
2013-08-16 14:36:06 +00:00
|
|
|
},
|
2013-08-16 14:21:30 +00:00
|
|
|
{ order_by => ["job", "id"], columns => [ @columns ] });
|
2015-06-11 11:58:23 +00:00
|
|
|
|
|
|
|
print STDERR "*** looking for the most recent successful builds of current jobs in ",
|
|
|
|
$project->name, ":", $jobset->name, "\n";
|
|
|
|
|
|
|
|
# Keep the most recently succeeded build of a current job. Oh
|
|
|
|
# I really need to stop using DBIx::Class.
|
|
|
|
keepBuild($_, 1) foreach $jobset->builds->search(
|
|
|
|
{ id => { -in => $jobset->builds->search(
|
|
|
|
{ finished => 1
|
|
|
|
, buildstatus => [0, 6]
|
|
|
|
, job => { -in => $jobset->builds->search(
|
|
|
|
{ eval => { -in => [@evals] } },
|
|
|
|
{ select => "job", distinct => 1, join => "jobsetevalmembers" }
|
|
|
|
)->as_query }
|
|
|
|
},
|
|
|
|
{ group_by => 'job'
|
|
|
|
, select => [ { max => 'id', -as => 'm' } ]
|
|
|
|
})->as_query }
|
|
|
|
},
|
|
|
|
{ columns => [ @columns ] });
|
2009-02-06 15:02:49 +00:00
|
|
|
}
|
2009-01-13 14:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-07 14:12:47 +00:00
|
|
|
# Remove existing roots that are no longer wanted.
|
2009-03-14 23:56:57 +00:00
|
|
|
print STDERR "*** removing unneeded GC roots\n";
|
2009-02-06 21:01:20 +00:00
|
|
|
|
2012-03-07 14:02:31 +00:00
|
|
|
my $rootsKept = 0;
|
|
|
|
my $rootsDeleted = 0;
|
2014-07-14 11:18:07 +00:00
|
|
|
my $now = time();
|
2012-03-07 14:02:31 +00:00
|
|
|
|
2012-03-07 14:12:47 +00:00
|
|
|
foreach my $link (@roots) {
|
2012-03-07 14:02:31 +00:00
|
|
|
next if $link eq "." || $link eq "..";
|
|
|
|
my $path = "/nix/store/$link";
|
2009-01-13 14:02:07 +00:00
|
|
|
if (!defined $roots{$path}) {
|
2014-07-14 11:18:07 +00:00
|
|
|
# Don't delete roots that are less than a day old, to prevent
|
|
|
|
# a race where hydra-eval-jobs has added a root but
|
|
|
|
# hydra-evaluator hasn't added them to the database yet.
|
2014-08-01 15:24:55 +00:00
|
|
|
my $st = lstat("$gcRootsDir/$link");
|
|
|
|
if (!defined $st) {
|
2014-07-16 21:20:58 +00:00
|
|
|
print STDERR "skipping link $link: $!\n";
|
2014-08-01 15:24:55 +00:00
|
|
|
} elsif ($st->ctime < $now - 24 * 60 * 60) {
|
2014-07-14 11:18:07 +00:00
|
|
|
print STDERR "removing root $path\n";
|
|
|
|
$rootsDeleted++;
|
|
|
|
unlink "$gcRootsDir/$link" or warn "cannot remove $gcRootsDir/$link";
|
|
|
|
} else {
|
|
|
|
print STDERR "NOT removing recent root $path\n";
|
|
|
|
$rootsKept++;
|
|
|
|
}
|
2012-03-07 14:02:31 +00:00
|
|
|
} else {
|
2013-02-13 16:49:28 +00:00
|
|
|
$rootsKept++;
|
2009-01-13 14:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 14:02:31 +00:00
|
|
|
print STDERR "kept $rootsKept roots, deleted $rootsDeleted roots\n";
|