Debian Codegraph @

lib/Test2/Harness/Runner/Resource/SharedJobSlots.pm

raw
1 package Test2::Harness::Runner::Resource::SharedJobSlots;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use YAML::Tiny;
8 use Test2::Harness::Runner::Resource::SharedJobSlots::State;
9 use Test2::Harness::Runner::Resource::SharedJobSlots::Config;
10
11 use Time::HiRes qw/time/;
12 use List::Util qw/min/;
13 use Carp qw/confess/;
14
15 use parent 'Test2::Harness::Runner::Resource';
16 use Test2::Harness::Util::HashBase qw{
17 <settings
18 <state
19 <config
20 <runner_id
21 <runner_pid
22 <job_limiter_max
23 <observe
24 };
25
26 sub job_limiter { 1 }
27
28 sub scope_host { 1 }
29
30 sub new {
31 my $class = shift;
32 my $self = bless {@_}, $class;
33 $self->init();
34 return $self;
35 }
36
37 sub init {
38 my $self = shift;
39 my $settings = $self->{+SETTINGS};
40
41 my $sconf = Test2::Harness::Runner::Resource::SharedJobSlots::Config->find(settings => $settings);
42 die "Could not find shared jobs config.\n"
43 unless $sconf;
44
45 my $runner_id;
46 $runner_id = $self->{+RUNNER_ID} //= $settings->runner->runner_id if $settings->check_prefix('runner');
47 my $runner_pid = $self->{+RUNNER_PID} //= $Test2::Harness::Runner::RUNNER_PID // $App::Yath::Command::runner::RUNNER_PID;
48
49 my $prefix = $settings->debug->procname_prefix // '';
50 my $name = $settings->harness->project // '';
51
52 my $dir;
53 if (my $path = $settings->harness->config_file) {
54 if ($path =~ m{^(.*)/[^/]+$}) {
55 $dir = $1;
56 }
57 }
58
59 $dir //= $settings->harness->cwd;
60
61 unless ($name) {
62 $name = $dir;
63 $name =~ s{^.*/}{};
64 }
65
66 $name = "$prefix-$name" if $prefix;
67
68 $self->{+JOB_LIMITER_MAX} = min(grep { $_ } $sconf->max_slots_per_run, $settings->runner->job_count);
69
70 $self->{+STATE} = Test2::Harness::Runner::Resource::SharedJobSlots::State->new(
71 dir => $dir,
72 name => $name,
73 runner_id => $runner_id,
74 runner_pid => $runner_pid,
75
76 state_umask => $sconf->state_umask,
77 state_file => $sconf->state_file,
78 algorithm => $sconf->algorithm,
79 max_slots => $sconf->max_slots,
80 max_slots_per_job => $sconf->max_slots_per_job,
81 max_slots_per_run => $sconf->max_slots_per_run,
82 min_slots_per_run => $sconf->min_slots_per_run,
83 default_slots_per_run => $sconf->default_slots_per_run,
84 default_slots_per_job => $sconf->default_slots_per_job,
85
86 my_max_slots => min($self->settings->runner->job_count, $sconf->max_slots),
87 my_max_slots_per_job => min($self->settings->runner->slots_per_job, $sconf->max_slots_per_job),
88 );
89
90 $self->{+CONFIG} = $sconf;
91
92 return;
93 }
94
95 # Disable this short-circuit otherwise we may never queue a request!
96 sub job_limiter_at_max { 0 }
97
98 sub refresh { $_[0]->{+STATE}->update_registration }
99
100 sub _job_concurrency {
101 my $self = shift;
102 my ($task) = @_;
103
104 my $rmax = $self->settings->runner->job_count;
105 my $jmax = $self->settings->runner->slots_per_job;
106 my $srmax = $self->{+CONFIG}->max_slots_per_run;
107 my $sjmax = $self->{+CONFIG}->max_slots_per_job;
108
109 my $tmin = $task->{min_slots} // 1;
110 my $tmax = $task->{max_slots} // $tmin;
111
112 my $max = min($tmax, $sjmax, $srmax, $jmax, $rmax);
113
114 # Invalid condition, minimum is more than our maximim
115 return if $tmin > $max;
116 $max = $tmin if $max < $tmin;
117
118 return [$tmin, $max];
119 }
120
121 sub available {
122 my $self = shift;
123 my ($task) = @_;
124
125 my $con = $self->_job_concurrency($task);
126 return -1 unless $con;
127
128 my $granted = $self->{+STATE}->allocate_slots(con => $con, job_id => $task->{job_id});
129
130 return unless $granted;
131
132 return $granted
133 }
134
135 sub assign {
136 my $self = shift;
137 my ($task, $state) = @_;
138
139 return if $self->{+OBSERVE};
140
141 my $info = $self->{+STATE}->assign_slots(
142 job => {
143 job_id => $task->{job_id},
144 file => $task->{rel_file} // $task->{file} // $task->{job_name},
145 },
146 );
147
148 $state->{env_vars}->{T2_HARNESS_MY_JOB_CONCURRENCY} = $info->{count};
149
150 return $info;
151 }
152
153 sub record { } # NOOP
154
155 sub release {
156 my $self = shift;
157 my ($job_id) = @_;
158
159 return if $self->{+OBSERVE};
160
161 $self->{+STATE}->release_slots(job_id => $job_id);
162
163 return;
164 }
165
166 sub status_data {
167 my $self = shift;
168
169 my @groups;
170
171 my $runners = $self->state->state->{runners};
172
173 my $global_status = {
174 todo => 0,
175 allotted => 0,
176 assigned => 0,
177 pending => 0,
178 };
179
180 my $time = time;
181
182 for my $runner (sort { $a->{added} <=> $b->{added} } values %$runners) {
183 my $run_status = {
184 todo => $runner->{todo},
185 allotted => $runner->{allotment},
186 assigned => 0,
187 pending => 0,
188 };
189
190 my $job_table = {
191 header => [qw/Runtime Slots Name/],
192 format => ['duration', undef, undef],
193 rows => [],
194 };
195
196 for my $job (sort { $a->{started} <=> $b->{started} } values %{$runner->{assigned}}) {
197 $run_status->{assigned} += $job->{count};
198 my $stamp = $job->{started};
199 my $slots = $job->{count};
200
201 push @{$job_table->{rows}} => [$time - $stamp, $slots, $job->{file} // $job->{job_id}];
202 }
203
204 $run_status->{pending} = $runner->{allotment} - $run_status->{assigned};
205
206 $global_status->{$_} += $run_status->{$_} for keys %$global_status;
207
208 my $run_table = {
209 header => [qw/Todo Allotted Assigned Pending/],
210 rows => [[$run_status->{todo}, $run_status->{allotted}, $run_status->{assigned}, $run_status->{pending}]],
211 };
212
213 push @groups => {
214 title => "$runner->{user} - $runner->{name} - $runner->{runner_id}",
215 tables => [
216 $run_table,
217 $job_table,
218 ],
219 };
220 }
221
222 $global_status->{total} = $self->state->{max_slots};
223 $global_status->{free} = $global_status->{total} - ($global_status->{assigned} + $global_status->{pending});
224 $global_status->{free} = "$global_status->{free} (Minimum per-run overrides max slot count in some cases)" if $global_status->{free} < 0;
225
226 unshift @groups => {
227 title => 'System Wide Summary',
228 tables => [
229 {
230 header => ['Todo', 'Total Shared Slots', 'Allotted Shared Slots', 'Assigned Shared Slots', 'Pending Shared Slots', 'Free Shared Slots'],
231 rows => [[ @{$global_status}{qw/todo total allotted assigned pending free/} ]],
232 }
233 ],
234 };
235
236 return \@groups;
237 }
238
239 1;
240
241 __END__
242
243 =pod
244
245 =encoding UTF-8
246
247 =head1 NAME
248
249 Test2::Harness::Runner::Resource::SharedJobSlots - limit the job count (-j) per machine
250
251 =head1 SYNOPSIS
252
253 B<This synopsis is not about using this in code, but rather how to use it on the command line.>
254
255 In order to use SharedJobSlots you must ether create the C<.sharedjobslots.yml>
256 file, or provide the C<--shared-jobs-config PATH> argument on the command line.
257 The C<PATH> must be a path to a yaml file with configuration specifications for
258 job sharing.
259
260 =head1 CONFIG FILE
261
262 Config files for shared slots must be yaml file, they must also be parsable by
263 L<YAML::Tiny>, which implements a subset of yaml.
264
265 Here is an example config file:
266
267 ---
268 DEFAULT:
269 state_file: /tmp/yath-slot-state
270 max_slots: 8
271 max_slots_per_job: 2
272 max_slots_per_run: 6
273
274 myhostname:
275 state_file: /tmp/myhostname-slot-state
276 max_slots: 16
277 max_slots_per_job: 4
278 max_slots_per_run: 12
279
280 =head2 TOP LEVEL KEYS (HOSTNAMES)
281
282 All top level keys are hostnames. When the config is read the settings for the
283 current hostname will be used. If the hostname is not defined then the
284 C<DEFAULT> host will be read. If there is no C<DEFAULT> host defined an
285 exception will be thrown.
286
287 =head2 CONFIG OPTIONS
288
289 Each option must be specified under a hostname, none of these are valid on
290 their own.
291
292 =over 4
293
294 =item state_file: /path/to/shared/state/file
295
296 B<REQUIRED>
297
298 This specifies the path to the shared state file. All yath processes by all
299 users who are sharing slots need read+write access to this file.
300
301 =item state_umask: 0007
302
303 Defaults to C<0007>. Used to set the umask of the state file as well as the
304 lock file.
305
306 =item max_slots: 8
307
308 Max slots system-wide for all users to share.
309
310 =item max_slots_per_run: 4
311
312 Max slots a specific test run can use.
313
314 =item min_slots_per_run: 0
315
316 Minimum slots per run.
317
318 Set this if you want to make sure that all runs get at least N slots,
319 B<EVEN IF IT MEANS GOING OVER THE SYSTEM-WIDE MAXIMUM!>.
320
321 This defaults to 0.
322
323 =item max_slots_per_job: 2
324
325 Max slots a specific test job (test file) can use.
326
327 =item default_slots_per_run: 4
328
329 If the user does not specify a number of slots, use this as the default.
330
331 =item default_slots_per_job: 2
332
333 If the user does not specify a number of job slots, use this as the default.
334
335 =item algorithm: fair
336
337 =item algorithm: first
338
339 =item algorithm: Fully::Qualified::Module::function_name
340
341 Algorithm to use when assigning slots. 'fair' is the default.
342
343 =back
344
345 =head3 ALGORITHMS
346
347 These are algorithms that are used to decide which test runs get which slots.
348
349 =over 4
350
351 =item fair
352
353 B<DEFAULT>
354
355 This algorithm tries to balance slots so that all runs share an equal fraction
356 of available slots. If there are not enough slots to go around then priority
357 goes to oldest runs, followed by oldest requests.
358
359 =item first
360
361 Priority goes to the oldest run, followed by the next oldest, etc. If the run
362 age is not sufficient to sort requests this will fall back to 'fair'.
363
364 This is mainly useful for CI systems or batched test boxes. This will give
365 priority to the first test run started, so additional test runs will not
366 consume slots the first run wants to use, but if the first run is winding down
367 and does not need all the slots, the second test run can start using only the
368 spare slots.
369
370 Use this with ordered test runs where you do not want a purely serial run
371 order.
372
373 =item Fully::Qualified::Module::function_name
374
375 You can specify custom algorithms by giving fully qualified subroutine names.
376
377 =back
378
379 Example custom algorithm:
380
381 sub custom_sort {
382 my ($state_object, $state_data, $a, $b) = @_;
383
384 return 1 if a_should_come_first($a, $b);
385 return -1 if b_should_come_first($a, $b);
386 return 0 if both_have_same_priority($a, $b);
387
388 # *shrug*
389 return 0;
390 }
391
392 Ultimately this is used in a C<sort()> call, usual rules apply, return should
393 be 1, 0, or -1. $a and $b are the 2 items being compared. $state_object is an
394 instance of C<Test2::Harness::Runner::Resource::SharedJobSlots::State>.
395 $state_data is a hashref like you get from C<< $state_object->state() >> which
396 is useful if you want to know how many slots each runner is using for a 'fair'
397 style algorithm.
398
399 Take a look at the C<request_sort_XXX> methods on
400 C<Test2::Harness::Runner::Resource::SharedJobSlots::State> which implement the
401 3 original sorting methods.
402
403 =head1 SOURCE
404
405 The source code repository for Test2-Harness can be found at
406 F<http://github.com/Test-More/Test2-Harness/>.
407
408 =head1 MAINTAINERS
409
410 =over 4
411
412 =item Chad Granum E<lt>[email protected]<gt>
413
414 =back
415
416 =head1 AUTHORS
417
418 =over 4
419
420 =item Chad Granum E<lt>[email protected]<gt>
421
422 =back
423
424 =head1 COPYRIGHT
425
426 Copyright 2022 Chad Granum E<lt>[email protected]<gt>.
427
428 This program is free software; you can redistribute it and/or
429 modify it under the same terms as Perl itself.
430
431 See F<http://dev.perl.org/licenses/>
432
433 =cut