Skip to content

Commit ad9bd8d

Browse files
committed
throttling: refactor the configuration logic
Load the resulting hash after validated the throtting configuration,in Setup.pm Calculate the resulting priority and update debug_message,in Jobs.pm Ref. note-10 in poo#192952
1 parent a144234 commit ad9bd8d

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

lib/OpenQA/Schema/ResultSet/Jobs.pm

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,6 @@ sub latest_jobs ($self, $until = undef) {
101101
return @latest;
102102
}
103103

104-
sub load_throttling_config ($config_string) {
105-
# convert in hash the prio_throttling_parameters string configured in openqa.ini
106-
return unless ($config_string && $config_string =~ /:/);
107-
$config_string =~ s/\s+//g;
108-
my %hash = map { my ($k, $v) = split /:/, $_, 2 } split /,/, $config_string;
109-
return \%hash;
110-
}
111-
112104
sub create_from_settings ($self, $settings, $scheduled_product_id = undef) {
113105
my %settings = %$settings;
114106
my %new_job_args;
@@ -166,17 +158,21 @@ sub create_from_settings ($self, $settings, $scheduled_product_id = undef) {
166158
$new_job_args{priority} += $malus;
167159
}
168160
}
169-
my $throttling = OpenQA::App->singleton->config->{misc_limits}->{prio_throttling_parameters};
170-
$throttling = load_throttling_config($throttling);
171161
# apply resources throttling control
172-
if ($throttling) {
162+
if (my $throttling = OpenQA::App->singleton->config->{misc_limits}->{prio_throttling_parameters}) {
163+
my $debug_msg_2;
173164
for my $resource (keys %$throttling) {
174165
next if !defined $settings{$resource};
175-
my $malus = int($settings{$resource} * $throttling->{$resource});
176-
$debug_msg = sprintf 'Adding priority malus to newly created job due to %s (old: %d, malus: %s)',
177-
$resource, $new_job_args{priority}, $malus;
178-
$new_job_args{priority} += $malus;
166+
my $scale = $throttling->{$resource}->{scale};
167+
my $reference = $throttling->{$resource}->{reference};
168+
my $prio = int(($settings{$resource} - $reference) * $scale);
169+
$debug_msg_2 .= sprintf " %s %s %s;",
170+
$resource, ", scale:$scale", ($reference ? ", reference:$reference" : '');
171+
$new_job_args{priority} += $prio;
179172
}
173+
$debug_msg .= sprintf("\nAdjusting job priority by %s based on resource requirement(s):\n %s",
174+
$new_job_args{priority}, $debug_msg_2)
175+
if $debug_msg_2;
180176
}
181177

182178
my $job = $self->create(\%new_job_args);

lib/OpenQA/Setup.pm

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,24 @@ sub _load_config ($app, $defaults, $mode_specific_defaults) {
6161
return $config;
6262
}
6363

64-
sub validate_prio_throttling_format ($config) {
64+
sub _load_prio_throttling ($config) {
6565
my $throttling = $config->{misc_limits}->{prio_throttling_parameters};
66-
if ($throttling && length $throttling) {
67-
$config->{misc_limits}->{prio_throttling_parameters} =~ s/\s+//g;
68-
die("Wrong formatting for 'prio_throttling_parameters' in openqa.ini")
69-
unless ($throttling =~ /^[A-Z_]+:\d+(?:,[A-Z_]+:\d+)*$/i);
66+
my %hash;
67+
if ($throttling) {
68+
my $n = qr/[+-]?(?:\d+\.?\d*|\.\d+)/; # number
69+
my $u = qr/([A-Z_]+):($n)(?::($n))?/i; # Key:N:M = $1:$2:$3
70+
71+
# remove spaces and validate comma-separated keys sequence
72+
$throttling =~ s/\s+//g;
73+
$app->log->warn("Wrong format in openqa.ini 'prio_throttling_parameters': $throttling")
74+
unless ($throttling =~ qr/^$u(?:,$u)*$/i);
75+
76+
# Implicit iteration along the sequence
77+
while ($throttling =~ /,?$u/g) {
78+
$hash{$1} = {scale => $2, reference => $3 // 0};
79+
}
7080
}
81+
$config->{misc_limits}->{prio_throttling_parameters} = \%hash;
7182
}
7283

7384
sub read_config ($app) {
@@ -304,7 +315,7 @@ sub read_config ($app) {
304315
$config->{influxdb}->{ignored_failed_minion_jobs} = [split(/\s+/, $minion_fail_job_blocklist)];
305316
}
306317

307-
validate_prio_throttling_format($config);
318+
_load_prio_throttling($config);
308319

309320
my $results = delete $global_config->{parallel_children_collapsable_results};
310321
$global_config->{parallel_children_collapsable_results_sel}

0 commit comments

Comments
 (0)