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

Fix checks for worker classes in directly chained clusters #5936

Merged
merged 3 commits into from
Sep 23, 2024
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
25 changes: 8 additions & 17 deletions lib/OpenQA/Schema/Result/ScheduledProducts.pm
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,8 @@ Internal method used by the B<job_create_dependencies()> method
sub _create_dependencies_for_parents ($self, $job, $created_jobs, $deptype, $parents) {
my $schema = $self->result_source->schema;
my $job_dependencies = $schema->resultset('JobDependencies');
my $worker_class;
my $job_settings = $schema->resultset('JobSettings');
my $worker_classes;
for my $parent (@$parents) {
try {
_check_for_cycle($job->id, $parent, $created_jobs);
Expand All @@ -691,25 +692,15 @@ sub _create_dependencies_for_parents ($self, $job, $created_jobs, $deptype, $par
die 'There is a cycle in the dependencies of ' . $job->TEST;
};
if ($deptype eq OpenQA::JobDependencies::Constants::DIRECTLY_CHAINED) {
unless (defined $worker_class) {
$worker_class = $job->settings->find({key => 'WORKER_CLASS'});
$worker_class = $worker_class ? $worker_class->value : '';
}
my $parent_worker_class
= $schema->resultset('JobSettings')->find({job_id => $parent, key => 'WORKER_CLASS'});
$parent_worker_class = $parent_worker_class ? $parent_worker_class->value : '';
if ($worker_class ne $parent_worker_class) {
$worker_classes //= join(',', @{$job_settings->all_values_sorted($job->id, 'WORKER_CLASS')});
my $parent_worker_classes = join(',', @{$job_settings->all_values_sorted($parent, 'WORKER_CLASS')});
if ($worker_classes ne $parent_worker_classes) {
my $test_name = $job->TEST;
die
"Worker class of $test_name ($worker_class) does not match the worker class of its directly chained parent ($parent_worker_class)";
die "Worker class of $test_name ($worker_classes) does not match the worker class of its "
. "directly chained parent ($parent_worker_classes)";
}
}
$job_dependencies->create(
{
child_job_id => $job->id,
parent_job_id => $parent,
dependency => $deptype,
});
$job_dependencies->create({child_job_id => $job->id, parent_job_id => $parent, dependency => $deptype});
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/OpenQA/Schema/ResultSet/JobSettings.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use Mojo::Base 'DBIx::Class::ResultSet', -signatures;

use OpenQA::App;

sub all_values_sorted ($self, $job_id, $key) {
state $options = {distinct => 1, columns => 'value', order_by => 'value'};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using state here?

Copy link
Contributor Author

@Martchus Martchus Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this hash never changes, so no need to re-initialize it on every function call. (That's probably the C++ programmer in me; in C++ that would be static const … and state is the closest Perl has to offer.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit uncommon for a simple, small hashref, but I won't complain

[map { $_->value } $self->search({job_id => $job_id, key => $key}, $options)];
}

sub jobs_for_setting ($self, $options) {
my $limit = OpenQA::App->singleton->config->{misc_limits}{job_settings_max_recent_jobs};

Expand Down
23 changes: 9 additions & 14 deletions lib/OpenQA/Schema/ResultSet/Jobs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ sub create_from_settings {
my $dependency_type = $dependency_definition->{dependency_type};
for my $id (@$ids) {
if ($dependency_type eq OpenQA::JobDependencies::Constants::DIRECTLY_CHAINED) {
my $parent_worker_class = $job_settings->find({job_id => $id, key => 'WORKER_CLASS'});
_handle_directly_chained_dep($parent_worker_class, $id, \%settings);
my $parent_worker_classes = join(',', @{$job_settings->all_values_sorted($id, 'WORKER_CLASS')});
_handle_directly_chained_dep($parent_worker_classes, $id, \%settings);
}
push(@{$new_job_args{parents}}, {parent_job_id => $id, dependency => $dependency_type});
}
Expand Down Expand Up @@ -203,18 +203,13 @@ sub create_from_settings {
return $job;
}

sub _handle_directly_chained_dep ($parent_worker_class, $id, $settings) {
if ($parent_worker_class = $parent_worker_class ? $parent_worker_class->value : '') {
if (!$settings->{WORKER_CLASS}) {
# assume we want to use the worker class from the parent here (and not the default which
# is otherwise assumed)
$settings->{WORKER_CLASS} = $parent_worker_class;
}
elsif ($settings->{WORKER_CLASS} ne $parent_worker_class) {
die "Specified WORKER_CLASS ($settings->{WORKER_CLASS}) does not match the one from"
. " directly chained parent $id ($parent_worker_class)";
}
}
sub _handle_directly_chained_dep ($parent_classes, $id, $settings) {
# assume we want to use the worker class from the parent here (and not the default which is otherwise assumed)
return $settings->{WORKER_CLASS} = $parent_classes unless defined(my $classes = $settings->{WORKER_CLASS});

# raise error if the directly chained child has a different set of worker classes assigned than its parent
die "Specified WORKER_CLASS ($classes) does not match the one from directly chained parent $id ($parent_classes)"
unless $parent_classes eq join(',', sort split(m/,/, $classes));
}

sub _search_modules ($self, $module_re) {
Expand Down
3 changes: 2 additions & 1 deletion t/05-scheduler-dependencies.t
Original file line number Diff line number Diff line change
Expand Up @@ -1083,9 +1083,10 @@ subtest 'WORKER_CLASS validated when creating directly chained dependencies' =>
'bar', 'job B has different class bar (ok for regularly chained dependencies)');
$jobC = _job_create({%default_job_settings, TEST => 'chained-C'}, undef, [], [$jobB->id]);
is($jobC->settings->find({key => 'WORKER_CLASS'})->value, 'bar', 'job C inherits worker class from B');
$job_settings->create({job_id => $jobC->id, key => 'WORKER_CLASS', value => 'baz'});
throws_ok(
sub { _job_create({%default_job_settings, TEST => 'chained-D', WORKER_CLASS => 'foo'}, [], [], [$jobC->id]) },
qr/Specified WORKER_CLASS \(foo\) does not match the one from directly chained parent .* \(bar\)/,
qr/Specified WORKER_CLASS \(foo\) does not match the one from directly chained parent .* \(bar,baz\)/,
'creation of job with mismatching worker class prevented'
);
};
Expand Down
7 changes: 7 additions & 0 deletions t/10-jobs.t
Original file line number Diff line number Diff line change
Expand Up @@ -999,4 +999,11 @@ subtest 'git log diff' => sub {
like $ok, qr{2 files changed}, 'expected git_diff output';
};

subtest 'get all setting values for a job/key in a sorted array' => sub {
my $job_settings = $schema->resultset('JobSettings');
is_deeply $job_settings->all_values_sorted(99926, 'WORKER_CLASS'), [], 'empty array if key does not exist';
$job_settings->create({job_id => 99926, key => 'WORKER_CLASS', value => $_}) for qw(foo bar bar baz);
is_deeply $job_settings->all_values_sorted(99926, 'WORKER_CLASS'), [qw(bar baz foo)], 'all values returned';
};

done_testing();
13 changes: 5 additions & 8 deletions t/api/02-iso.t
Original file line number Diff line number Diff line change
Expand Up @@ -547,17 +547,14 @@ subtest 'Handling different WORKER_CLASS in directly chained dependency chains'
$schema->txn_begin;
add_opensuse_test('chained-a', WORKER_CLASS => 'foo');
add_opensuse_test('chained-b', START_DIRECTLY_AFTER_TEST => 'chained-a', WORKER_CLASS => 'foo');
add_opensuse_test('chained-c', START_DIRECTLY_AFTER_TEST => 'chained-b', WORKER_CLASS => 'bar');
add_opensuse_test('chained-d', START_DIRECTLY_AFTER_TEST => 'chained-c', WORKER_CLASS => 'bar');
add_opensuse_test('chained-e', START_DIRECTLY_AFTER_TEST => 'chained-d', WORKER_CLASS => 'bar');
add_opensuse_test('chained-c', START_DIRECTLY_AFTER_TEST => 'chained-b', WORKER_CLASS => 'bar,baz');
add_opensuse_test('chained-d', START_DIRECTLY_AFTER_TEST => 'chained-c', WORKER_CLASS => 'bar,baz');
add_opensuse_test('chained-e', START_DIRECTLY_AFTER_TEST => 'chained-d', WORKER_CLASS => 'bar,baz');

my $res = schedule_iso($t, {%iso, _GROUP => 'opensuse test'});
is($res->json->{count}, 0, 'none of the jobs has been scheduled');
like(
$_->{error_messages}->[0],
qr/Worker class of chained-(c|d|e) \(bar\) does not match the worker class of its directly chained parent \(foo\)/,
'error reported'
) for @{$res->json->{failed}};
like($_->{error_messages}->[0], qr/chained-(c|d|e) \(bar,baz\) does not match .* \(foo\)/, 'error reported')
for @{$res->json->{failed}};
$schema->txn_rollback;
};
};
Expand Down
Loading