Skip to content

Commit 37e3bef

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 37e3bef

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

lib/OpenQA/Schema/ResultSet/Jobs.pm

Lines changed: 9 additions & 14 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;
@@ -167,16 +159,19 @@ sub create_from_settings ($self, $settings, $scheduled_product_id = undef) {
167159
}
168160
}
169161
my $throttling = OpenQA::App->singleton->config->{misc_limits}->{prio_throttling_parameters};
170-
$throttling = load_throttling_config($throttling);
171162
# apply resources throttling control
172-
if ($throttling) {
163+
if ($throttling ref eq 'HASH') {
164+
my $debug_msg_2;
173165
for my $resource (keys %$throttling) {
174166
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;
167+
my $prio
168+
= int(($settings{$resource} - $throttling->{$resource}->{reference}) * $throttling->{$resource}->{scale});
169+
$debug_msg_2 .= sprintf ' %s, scale: %s;', $resource, $prio;
170+
$new_job_args{priority} += $prio;
179171
}
172+
$debug_msg .= sprintf('\nAdding priority scale to base prio %s of the job due to:\n %s',
173+
$new_job_args{priority}, $debug_msg_2)
174+
if $debug_msg_2;
180175
}
181176

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

lib/OpenQA/Setup.pm

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,29 @@ 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 $ret;
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+
die("Wrong format in openqa.ini 'prio_throttling_parameters': $throttling")
74+
unless ($throttling =~ qr/^$u(?:,$u)*$/i);
75+
#
76+
my %hash;
77+
# Implicit iteration along the sequence
78+
while ($throttling =~ /,?$u/g) {
79+
$hash{$1} = {scale => $2, reference => $3 // 0};
80+
}
81+
$ret = \%hash;
82+
}
83+
else {
84+
$ret = undef;
7085
}
86+
$config->{misc_limits}->{prio_throttling_parameters} = $ret;
7187
}
7288

7389
sub read_config ($app) {
@@ -304,7 +320,7 @@ sub read_config ($app) {
304320
$config->{influxdb}->{ignored_failed_minion_jobs} = [split(/\s+/, $minion_fail_job_blocklist)];
305321
}
306322

307-
validate_prio_throttling_format($config);
323+
_load_prio_throttling($config);
308324

309325
my $results = delete $global_config->{parallel_children_collapsable_results};
310326
$global_config->{parallel_children_collapsable_results_sel}

0 commit comments

Comments
 (0)