Skip to content

Commit 3557cb4

Browse files
committed
refactor: Move throttling into its own function
... and use this in the tests to avoid executing the whole job posting process.
1 parent ab5d3c4 commit 3557cb4

File tree

2 files changed

+61
-51
lines changed

2 files changed

+61
-51
lines changed

lib/OpenQA/Schema/ResultSet/Jobs.pm

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -146,35 +146,7 @@ sub create_from_settings ($self, $settings, $scheduled_product_id = undef) {
146146
# assign scheduled product
147147
$new_job_args{scheduled_product_id} = $scheduled_product_id;
148148

149-
my $debug_msg;
150-
my $max_job_time = looks_like_number $settings{MAX_JOB_TIME} ? $settings{MAX_JOB_TIME} : 0;
151-
my $timeout_scale = looks_like_number $settings{TIMEOUT_SCALE} ? $settings{TIMEOUT_SCALE} : 0;
152-
$max_job_time *= $timeout_scale if $max_job_time and $timeout_scale > 1;
153-
if ($max_job_time and $max_job_time > DEFAULT_MAX_JOB_TIME) {
154-
if (my $scale = OpenQA::App->singleton->config->{misc_limits}->{max_job_time_prio_scale}) {
155-
my $malus = int($max_job_time / $scale);
156-
$debug_msg = sprintf 'Adding priority malus to newly created job (old: %d, malus: %s)',
157-
$new_job_args{priority}, $malus;
158-
$new_job_args{priority} += $malus;
159-
}
160-
}
161-
# apply resources throttling control
162-
if (my $throttling
163-
= OpenQA::App->singleton && OpenQA::App->singleton->config->{misc_limits}->{prio_throttling_data})
164-
{
165-
my $throttling_info;
166-
for my $resource (keys %$throttling) {
167-
next if !defined $settings{$resource};
168-
my $scale = $throttling->{$resource}->{scale};
169-
my $reference = $throttling->{$resource}->{reference};
170-
my $prio = int(($settings{$resource} - $reference) * $scale);
171-
$throttling_info .= "$resource, scale: $scale" . ($reference ? ", reference: $reference;" : ';');
172-
$new_job_args{priority} += $prio;
173-
}
174-
$debug_msg .= sprintf("\nAdjusting job priority by %s based on resource requirement(s): %s",
175-
$new_job_args{priority}, $throttling_info)
176-
if $throttling_info;
177-
}
149+
my $debug_msg = _apply_prio_throttling(\%settings, \%new_job_args);
178150

179151
my $job = $self->create(\%new_job_args);
180152
log_debug(sprintf "(Job %d) $debug_msg", $job->id) if $debug_msg;
@@ -197,6 +169,42 @@ sub create_from_settings ($self, $settings, $scheduled_product_id = undef) {
197169
return $job;
198170
}
199171

172+
sub _apply_prio_throttling ($settings, $new_job_args) {
173+
my $debug_msg;
174+
my $max_job_time = looks_like_number $settings->{MAX_JOB_TIME} ? $settings->{MAX_JOB_TIME} : 0;
175+
my $timeout_scale = looks_like_number $settings->{TIMEOUT_SCALE} ? $settings->{TIMEOUT_SCALE} : 0;
176+
$max_job_time *= $timeout_scale if $max_job_time and $timeout_scale > 1;
177+
if ($max_job_time and $max_job_time > DEFAULT_MAX_JOB_TIME) {
178+
if (my $scale = OpenQA::App->singleton->config->{misc_limits}->{max_job_time_prio_scale}) {
179+
my $malus = int($max_job_time / $scale);
180+
$debug_msg = sprintf 'Adding priority malus to newly created job (old: %d, malus: %s)',
181+
$new_job_args->{priority}, $malus;
182+
$new_job_args->{priority} += $malus;
183+
}
184+
}
185+
186+
if (my $throttling
187+
= OpenQA::App->singleton && OpenQA::App->singleton->config->{misc_limits}->{prio_throttling_data})
188+
{
189+
my $throttling_info;
190+
for my $resource (keys %$throttling) {
191+
next unless defined $settings->{$resource};
192+
my $scale = $throttling->{$resource}->{scale};
193+
my $reference = $throttling->{$resource}->{reference};
194+
my $prio = int(($settings->{$resource} - $reference) * $scale);
195+
$throttling_info .= "$resource, scale: $scale" . ($reference ? ", reference: $reference;" : ';');
196+
$new_job_args->{priority} += $prio;
197+
}
198+
$debug_msg .= sprintf(
199+
'. Adjusting job priority by %s based on resource requirement(s): %s',
200+
$new_job_args->{priority},
201+
$throttling_info
202+
) if $throttling_info;
203+
}
204+
return $debug_msg;
205+
}
206+
207+
200208
sub _handle_dependency_settings ($self, $settings, $new_job_args) {
201209
my $job_settings = $self->result_source->schema->resultset('JobSettings');
202210
# handle dependencies

t/api/04-jobs.t

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,71 +1021,73 @@ subtest 'array settings correctly assigned when posting job' => sub {
10211021
};
10221022

10231023
subtest 'priority correctly assigned when posting job' => sub {
1024+
my $default_prio = 50;
1025+
10241026
# post new job and check default priority
10251027
$t->post_ok('/api/v1/jobs', form => \%jobs_post_params)->status_is(200);
10261028
$t->get_ok('/api/v1/jobs/' . $t->tx->res->json->{id})->status_is(200);
10271029
$t->json_is('/job/group', 'opensuse', 'group assigned (1)');
1028-
$t->json_is('/job/priority', 50, 'global default priority assigned');
1030+
$t->json_is('/job/priority', $default_prio, 'global default priority assigned');
10291031

10301032
subtest 'priority malus due to high MAX_JOB_TIME' => sub {
1031-
my $max = 7300;
1032-
local $jobs_post_params{MAX_JOB_TIME} = $max;
1033+
my %new_job_args = (priority => $default_prio);
1034+
my $max_time = 7300;
1035+
local $jobs_post_params{MAX_JOB_TIME} = $max_time;
10331036
$t->post_ok('/api/v1/jobs', form => \%jobs_post_params)->status_is(200);
10341037
$t->get_ok('/api/v1/jobs/' . $t->tx->res->json->{id})->status_is(200);
1035-
$t->json_is('/job/priority', 50 + $max / 100, 'increased prio value');
1038+
$t->json_is('/job/priority', $default_prio + $max_time / 100, 'increased prio value');
10361039

10371040
local $jobs_post_params{TIMEOUT_SCALE} = 2;
1038-
$t->post_ok('/api/v1/jobs', form => \%jobs_post_params)->status_is(200);
1039-
$t->get_ok('/api/v1/jobs/' . $t->tx->res->json->{id})->status_is(200);
1040-
$t->json_is('/job/priority', 50 + $max * 2 / 100, 'increased prio value with TIMEOUT_SCALE');
1041+
OpenQA::Schema::ResultSet::Jobs::_apply_prio_throttling(\%jobs_post_params, \%new_job_args);
1042+
is $new_job_args{priority}, $default_prio + $max_time * 2 / 100, 'increased prio value with TIMEOUT_SCALE';
10411043
delete $jobs_post_params{TIMEOUT_SCALE};
10421044

10431045
my $limits = OpenQA::App->singleton->config->{misc_limits};
1046+
%new_job_args = (priority => $default_prio);
10441047
$limits->{max_job_time_prio_scale} = 10;
1045-
$t->post_ok('/api/v1/jobs', form => \%jobs_post_params)->status_is(200);
1046-
$t->get_ok('/api/v1/jobs/' . $t->tx->res->json->{id})->status_is(200);
1047-
$t->json_is('/job/priority', 50 + $max / 10, 'custom scale value: increased prio value');
1048+
OpenQA::Schema::ResultSet::Jobs::_apply_prio_throttling(\%jobs_post_params, \%new_job_args);
1049+
is $new_job_args{priority}, $default_prio + $max_time / 10, 'custom scale value: increased prio value';
10481050

1051+
%new_job_args = (priority => $default_prio);
10491052
$limits->{max_job_time_prio_scale} = 0;
1050-
$t->post_ok('/api/v1/jobs', form => \%jobs_post_params)->status_is(200);
1051-
$t->get_ok('/api/v1/jobs/' . $t->tx->res->json->{id})->status_is(200);
1052-
$t->json_is('/job/priority', 50, 'feature disabled: prio value unchanged');
1053+
OpenQA::Schema::ResultSet::Jobs::_apply_prio_throttling(\%jobs_post_params, \%new_job_args);
1054+
is $new_job_args{priority}, $default_prio, 'feature disabled: prio value unchanged';
10531055
};
10541056

10551057
subtest 'priority scaled up due to QEMURAM demand' => sub {
1058+
my %new_job_args = (priority => $default_prio);
10561059
my $add = 20;
10571060
local $jobs_post_params{QEMURAM} = 4096;
10581061

10591062
my $config = OpenQA::Setup::read_config($t->app);
10601063
$config->{misc_limits}->{prio_throttling_parameters} = 'XXX :0.2, QEMURAM:0.01:2048';
10611064
$config->{misc_limits}->{prio_throttling_data} = OpenQA::Setup::_load_prio_throttling($t->app, $config);
1062-
$t->post_ok('/api/v1/jobs', form => \%jobs_post_params)->status_is(200);
1063-
$t->get_ok('/api/v1/jobs/' . $t->tx->res->json->{id})->status_is(200);
1064-
$t->json_is('/job/priority', 50 + $add, 'increased prio value');
1065+
OpenQA::Schema::ResultSet::Jobs::_apply_prio_throttling(\%jobs_post_params, \%new_job_args);
1066+
is $new_job_args{priority}, $default_prio + $add, 'increased prio value';
10651067
};
10661068

10671069
subtest 'priority improved due to QEMURAM low demand' => sub {
1070+
my %new_job_args = (priority => $default_prio);
10681071
my $add = -10;
10691072
local $jobs_post_params{QEMURAM} = 1024;
10701073

10711074
my $config = OpenQA::Setup::read_config($t->app);
10721075
$config->{misc_limits}->{prio_throttling_parameters} = 'XXX :0.2, QEMURAM:0.01:2048';
10731076
$config->{misc_limits}->{prio_throttling_data} = OpenQA::Setup::_load_prio_throttling($t->app, $config);
1074-
$t->post_ok('/api/v1/jobs', form => \%jobs_post_params)->status_is(200);
1075-
$t->get_ok('/api/v1/jobs/' . $t->tx->res->json->{id})->status_is(200);
1076-
$t->json_is('/job/priority', 50 + $add, 'decreased prio value');
1077+
OpenQA::Schema::ResultSet::Jobs::_apply_prio_throttling(\%jobs_post_params, \%new_job_args);
1078+
is $new_job_args{priority}, $default_prio + $add, 'decreased prio value';
10771079
};
10781080

10791081
subtest 'priority scaled up due to HDDSIZEGB demand' => sub {
1082+
my %new_job_args = (priority => $default_prio);
10801083
my $add = 2;
10811084
local $jobs_post_params{HDDSIZEGB} = 40;
10821085
my $config = OpenQA::Setup::read_config($t->app);
10831086
$config->{misc_limits}->{prio_throttling_parameters}
10841087
= 'XXX :0.2, FAKE_HDDSIZEGB:0.01, HDDSIZEGB:0.05, YYY: 0.1';
10851088
$config->{misc_limits}->{prio_throttling_data} = OpenQA::Setup::_load_prio_throttling($t->app, $config);
1086-
$t->post_ok('/api/v1/jobs', form => \%jobs_post_params)->status_is(200);
1087-
$t->get_ok('/api/v1/jobs/' . $t->tx->res->json->{id})->status_is(200);
1088-
$t->json_is('/job/priority', 50 + $add, 'increased prio value');
1089+
OpenQA::Schema::ResultSet::Jobs::_apply_prio_throttling(\%jobs_post_params, \%new_job_args);
1090+
is $new_job_args{priority}, $default_prio + $add, 'increased prio value';
10891091
};
10901092

10911093
# post new job in job group with customized default priority

0 commit comments

Comments
 (0)