Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize asset cleanup #4226

Merged
merged 3 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/OpenQA/Schema/Result/Assets.pm
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ sub _getDirSize {

sub disk_file {
my ($self) = @_;
return locate_asset($self->type, $self->name);
return $self->{_location} //= locate_asset($self->type, $self->name);
}

# actually checking the file - will be updated to fixed in DB by limit_assets
Expand Down
12 changes: 5 additions & 7 deletions lib/OpenQA/Schema/ResultSet/Assets.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,8 @@ sub refresh_assets {
my ($self) = @_;

while (my $asset = $self->next) {
if ($asset->is_fixed) {
$asset->update({fixed => 1});
}
else {
$asset->update({fixed => 0});
}
my $is_fixed = $asset->is_fixed;
$asset->update({fixed => $is_fixed}) if $is_fixed != $asset->fixed;

$asset->refresh_size;
}
Expand All @@ -124,7 +120,8 @@ sub status {
a.id as id, a.name as name, a.t_created as t_created, a.size as size, a.type as type,
a.fixed as fixed,
coalesce(max(j.id), -1) as max_job,
max(case when j.id is not null and j.state!='done' and j.state!='cancelled' then 1 else 0 end) as pending
max(case when j.id is not null and j.state!='done' and j.state!='cancelled' then 1 else 0 end) as pending,
last_use_job_id as last_job
from assets a
left join jobs_assets ja on a.id=ja.asset_id
left join jobs j on j.id=ja.job_id
Expand Down Expand Up @@ -210,6 +207,7 @@ END_SQL
fixed => $fixed,
max_job => ($max_job >= 0 ? $max_job : undef),
pending => $asset_array->[7],
last_job => $asset_array->[8],
groups => {},
parents => {},
);
Expand Down
4 changes: 3 additions & 1 deletion lib/OpenQA/Task/Asset/Limit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ sub _limit {
my $untracked_assets_patterns = $config->{'assets/storage_duration'} // {};
my $now = DateTime->now();
for my $asset (@$assets) {
$update_sth->execute($asset->{max_job} && $asset->{max_job} >= 0 ? $asset->{max_job} : undef, $asset->{id});
my ($max_job, $max_job_before) = ($asset->{max_job}, $asset->{last_job});
$update_sth->execute($max_job && $max_job >= 0 ? $max_job : undef, $asset->{id})
if !$max_job_before || $max_job != $max_job_before;
next if $asset->{fixed} || scalar(keys %{$asset->{groups}}) > 0;

my $asset_name = $asset->{name};
Expand Down
36 changes: 26 additions & 10 deletions t/37-limit_assets.t
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ $test_case->init_data(fixtures_glob => '01-jobs.pl 05-job_modules.pl 06-job_depe
my $t = Test::Mojo->new('OpenQA::WebAPI');
my $app = $t->app;
my $schema = $app->schema;
my $assets = $schema->resultset('Assets');

# assume some assets already have a last_use_job_id
$assets->find($_)->update({last_use_job_id => 99963}) for (2, 3);

note('Asset directory: ' . assetdir());

Expand All @@ -64,7 +68,6 @@ subtest 'configurable concurrency' => sub {
};

subtest 'filesystem removal' => sub {
my $assets = $schema->resultset('Assets');
my $asset_sub_dir = path(assetdir(), 'foo');
$asset_sub_dir->make_path;

Expand Down Expand Up @@ -196,6 +199,7 @@ my %expected_parents = (
my @expected_assets_with_max_job = (
{
max_job => 99981,
last_job => 99963, # assigned in test setup; deviates from max_job on purpose
type => 'iso',
pending => 0,
size => 4,
Expand All @@ -212,13 +216,14 @@ my @expected_assets_with_max_job = (
name => 'iso/openSUSE-13.1-DVD-x86_64-Build0091-Media.iso',
fixed => 0,
groups => {1001 => 99963, 1002 => 99961}
, # specific job groups still visible when a job group is within a parent group
parents => {1 => 1},
type => 'iso',
pending => 1,
id => 2,
size => 4,
max_job => 99963,
, # specific job groups still visible when a job group is within a parent group
parents => {1 => 1},
type => 'iso',
pending => 1,
id => 2,
size => 4,
max_job => 99963,
last_job => 99963, # assigned in test setup
},
{
groups => {1002 => 99961}, # specific job groups still visible when a job group is within a parent group
Expand All @@ -232,6 +237,7 @@ my @expected_assets_with_max_job = (
id => 6,
type => 'repo',
size => 12,
last_job => undef,
},
{
name => 'iso/openSUSE-13.1-DVD-i586-Build0091-Media.iso',
Expand All @@ -244,6 +250,7 @@ my @expected_assets_with_max_job = (
id => 1,
type => 'iso',
size => 4,
last_job => undef,
},
{
type => 'hdd',
Expand All @@ -256,6 +263,7 @@ my @expected_assets_with_max_job = (
name => 'hdd/fixed/openSUSE-13.1-x86_64.hda',
groups => {1001 => 99946},
parents => {},
last_job => undef,
},
{
groups => {1001 => 99938},
Expand All @@ -267,7 +275,8 @@ my @expected_assets_with_max_job = (
pending => 0,
picked_into => '1001',
size => undef, # non-existing asset, pulled in by registering assets from settings of job 99938
type => 'iso'
type => 'iso',
last_job => undef,
},
{
groups => {1001 => 99926},
Expand All @@ -280,6 +289,7 @@ my @expected_assets_with_max_job = (
pending => 0,
id => 4,
size => undef, # non-existing asset, explicitely defined in fixtures (01-jobs.pl)
last_job => undef,
},
);
my %expected_assets_without_max_job = (
Expand All @@ -292,6 +302,7 @@ my %expected_assets_without_max_job = (
type => 'hdd',
size => 0,
max_job => undef,
last_job => undef,
},
'hdd/openSUSE-12.2-x86_64.hda' => {
picked_into => 0,
Expand All @@ -302,9 +313,11 @@ my %expected_assets_without_max_job = (
type => 'hdd',
size => 0,
max_job => undef,
last_job => undef,
},
'hdd/openSUSE-12.3-x86_64.hda' => {
max_job => undef,
last_job => undef,
pending => 0,
type => 'hdd',
size => 0,
Expand All @@ -315,6 +328,7 @@ my %expected_assets_without_max_job = (
},
'hdd/Windows-8.hda' => {
max_job => undef,
last_job => undef,
type => 'hdd',
pending => 0,
size => 0,
Expand All @@ -325,6 +339,7 @@ my %expected_assets_without_max_job = (
},
'hdd/openSUSE-12.1-x86_64.hda' => {
max_job => undef,
last_job => undef,
type => 'hdd',
pending => 0,
size => 0,
Expand Down Expand Up @@ -440,7 +455,8 @@ subtest 'asset status without pending state, max_job and max_job by group' => su

# adjust expected assets
for my $asset (@expected_assets_with_max_job) {
$asset->{pending} = undef;
$asset->{pending} = undef;
$asset->{last_job} = undef;
my $groups = $asset->{groups};
for my $group_id (keys %$groups) {
$groups->{$group_id} = undef;
Expand Down