Debian Codegraph @

t/unit/Test2/Harness/Runner/Resource/SharedJobSlots/State.t

raw
1 use Test2::V0 -target => 'Test2::Harness::Runner::Resource::SharedJobSlots::State';
2 use File::Temp qw/tempfile/;
3
4 use ok $CLASS;
5
6 sub inst {
7 my %params = @_;
8
9 my $state_file = $params{state_file};
10
11 unless ($state_file) {
12 my $fh;
13 ($fh, $state_file) = tempfile(UNLINK => 1);
14 close($fh);
15 }
16
17 return $CLASS->new(
18 state_file => $state_file,
19 max_slots => 10,
20 max_slots_per_job => 3,
21 max_slots_per_run => 9,
22 runner_pid => $$,
23 %params,
24 );
25 }
26
27 subtest init_checks => sub {
28 for my $field (qw/state_file max_slots max_slots_per_job max_slots_per_run/) {
29 my %proto = (
30 state_file => '/dev/null',
31 max_slots => 100,
32 max_slots_per_job => 5,
33 max_slots_per_run => 50,
34 );
35
36 # Remove the field we are testing for.
37 delete $proto{$field};
38
39 like(
40 dies { $CLASS->new(%proto) },
41 qr/'$field' is a required attribute/,
42 "Require '$field' be provided"
43 );
44 }
45
46 my $one = inst();
47 isa_ok($one, [$CLASS], "Created an instance");
48 };
49
50 subtest init_state => sub {
51 my $one = inst(runner_id => 'one');
52 my $state = $one->transaction('w');
53 like(
54 $state,
55 {
56 runners => {
57 one => {runner_id => 'one'}, # The runner added for our transacton
58 },
59 },
60 "Got initial state"
61 );
62
63 # Remove the local data (not stored)
64 my $local = delete $state->{local};
65 like(
66 $local,
67 {
68 lock => FDNE, # The lock should not be present anymore (it is weakened inside the transaction, gone after)
69 write => T, # This was a write transaction
70 },
71 "Local data is as expected",
72 );
73
74 my $stored = Test2::Harness::Util::File::JSON->new(name => $one->state_file)->read;
75 is($state, $stored, "state and stored match");
76 };
77
78 subtest transaction => sub {
79 my $one = inst(runner_id => 'one');
80
81 my $end_state = $one->transaction(
82 w => sub {
83 my ($the_one, $state, @args) = @_;
84
85 ref_is($the_one, $one, "Got the instance first");
86 ref_ok($state, 'HASH', "got a hash");
87 is(\@args, [qw/arg1 arg2/], "Got additional args");
88
89 my $local_check = {
90 lock => T(),
91 write => T(),
92 mode => 'w',
93 stack => [
94 {cb => T(), args => ['arg1', 'arg2']},
95 ]
96 };
97
98 is($state->{local}, $local_check, "Got accurate state");
99
100 subtest nested_transaction => sub {
101 $one->transaction(
102 'r' => sub {
103 my ($also_the_one, $also_state) = @_;
104
105 ref_is($also_the_one, $one, "got the same instance");
106 ref_is($also_state, $state, "Got the same state object");
107
108 is(
109 $state->{local},
110 {
111 lock => T(),
112 write => F(),
113 mode => 'r',
114 stack => [
115 {cb => T(), args => ['arg1', 'arg2']},
116 {cb => T(), args => []},
117 ]
118 },
119 "State temporarily modified"
120 );
121 },
122 );
123 };
124
125 is($one->transaction(), $state, "transaction with no callback returns state");
126
127 is($state->{local}, $local_check, "State restored");
128
129 return $state;
130 },
131 'arg1',
132 'arg2'
133 );
134
135 like(
136 $end_state,
137 {
138 local => {lock => FDNE}, # Lock released
139 runners => {
140 one => { # Added runner
141 user => $ENV{USER},
142 seen => T(),
143 added => T(),
144 runner_id => 'one',
145 },
146 },
147 },
148 "Got correct end state"
149 );
150
151 my $two = inst(runner_id => 'two', state_file => $one->{state_file});
152 my $state = $two->update_registration;
153
154 ok($state->{runners}->{two}, "Got registration");
155
156 $two->transaction(
157 rw => sub {
158 my ($me, $state) = @_;
159 $state->{runners}->{two}->{remove} = 1;
160 }
161 );
162
163 $state = $one->transaction(
164 ro => sub {
165 my ($me, $state) = @_;
166 ok(!$state->{runners}->{two}, "Two is not registered anymore");
167 }
168 );
169
170 like(
171 dies { $two->transaction('rw') },
172 qr/Shared slot registration expired/,
173 "Cannot proceed if our registration expired",
174 );
175
176 my $three = inst(runner_id => 'three', state_file => $one->{state_file});
177 $state = $three->update_registration;
178
179 ok($state->{runners}->{three}, "Got registration");
180
181 $one->transaction(
182 rw => sub {
183 my ($me, $state) = @_;
184 $state->{runners}->{three}->{seen} = 1; # Very long time ago.
185 }
186 );
187
188 # Make sure RO mode is aware, even though it does not write the update
189 $state = $one->transaction(
190 ro => sub {
191 my ($me, $state) = @_;
192 ok(!$state->{runners}->{three}, "Three is not registered anymore (timed out)");
193 ok(!$state->{runners}->{two}, "Two is not registered anymore");
194 }
195 );
196
197 $state = $one->transaction(
198 rw => sub {
199 my ($me, $state) = @_;
200 ok(!$state->{runners}->{three}, "Three is not registered anymore (timed out)");
201 ok(!$state->{runners}->{two}, "Two is not registered anymore");
202 return $state;
203 }
204 );
205
206 delete $state->{local};
207
208 my $stored = Test2::Harness::Util::File::JSON->new(name => $one->state_file)->read;
209 is($state, $stored, "state and stored match");
210 };
211
212 sub consistent_state {
213 my ($insts, $state_check) = @_;
214
215 my $ctx = context();
216
217 my $state;
218 subtest "consistent state" => sub {
219 my $base = $state = shift(@$insts);
220 my $state = $base->state;
221
222 my $idx = 1;
223 while (my $i = shift @$insts) {
224 my $st2 = $i->state;
225 is($st2, $state, "state [" . $idx++ . "] matches state [0]");
226 }
227
228 use Data::Dumper;
229 is($state, $state_check, "State matches expectations", Dumper($state)) if $state_check;
230 };
231
232 $ctx->release;
233
234 return $state;
235 }
236
237 subtest registration => sub {
238 my $one = inst(runner_id => 'one');
239 my $two = inst(runner_id => 'two', state_file => $one->{state_file});
240 my $three = inst(runner_id => 'three', state_file => $one->{state_file});
241
242 $one->update_registration;
243 consistent_state(
244 [$one, $two, $three],
245 hash {
246 field runners => {
247 one => T(),
248 };
249 etc;
250 },
251 );
252
253 $two->update_registration;
254 consistent_state(
255 [$one, $two, $three],
256 hash {
257 field runners => {
258 one => T(),
259 two => T(),
260 };
261 etc;
262 },
263 );
264
265 $three->update_registration;
266 consistent_state(
267 [$one, $two, $three],
268 hash {
269 field runners => {
270 one => T(),
271 two => T(),
272 three => T(),
273 };
274 etc;
275 },
276 );
277
278 $two->remove_registration;
279 consistent_state(
280 [$one, $two, $three],
281 hash {
282 field runners => {
283 one => T(),
284 two => DNE(),
285 three => T(),
286 };
287 etc;
288 },
289 );
290
291 # Emulate 'three' timing out.
292 my $file = Test2::Harness::Util::File::JSON->new(name => $one->{state_file});
293 my $data = $file->read;
294 $data->{runners}->{three}->{seen} -= 100 + $one->TIMEOUT;
295 $file->write($data);
296
297 consistent_state(
298 [$one, $two, $three],
299 hash {
300 field runners => {
301 one => T(),
302 two => DNE(),
303 three => DNE(),
304 };
305 etc;
306 },
307 );
308
309 like(
310 dies { $three->update_registration },
311 qr/Shared slot registration expired/,
312 "Cannot write after timing out"
313 );
314 };
315
316 subtest _entry_expired => sub {
317 my $one = inst(runner_id => 'one');
318
319 ok($one->_entry_expired(undef), "Invalid entry is expired");
320 ok($one->_entry_expired({remove => 1}), "Entry to be removed is expired");
321 ok($one->_entry_expired({}), "no 'seen' field expired");
322
323 ok(!$one->_entry_expired({seen => time}), "Recently seen, not expired");
324
325 ok($one->_entry_expired({seen => (time - (10 + $one->TIMEOUT))}), "Old is expired");
326 };
327
328 subtest runner_todo => sub {
329 my $one = inst(runner_id => 'one');
330
331 my $entry = {};
332 is($one->_runner_todo($entry), undef, "Nothing to do");
333 is($one->_runner_todo($entry, 'j1'), undef, "Nothing to do");
334
335 is($one->_runner_todo($entry, j1 => 2), 2, "Got job count");
336 is($entry->{todo}, 2, "todo is set");
337
338 is($one->_runner_todo($entry, j2 => 3), 3, "Got job count");
339 is($entry->{todo}, 5, "todo is set");
340
341 is($one->_runner_todo($entry, j3 => 1), 1, "Got job count");
342 is($entry->{todo}, 6, "todo is set");
343
344 is($one->_runner_todo($entry, 'j2'), 3, "Got job count");
345 is($entry->{todo}, 6, "todo is set");
346
347 is($one->_runner_todo($entry, j2 => -1), 3, "Got job count");
348 is($entry->{todo}, 3, "todo is set");
349 };
350
351 subtest _runner_calcs => sub {
352 my $one = inst(runner_id => 'one');
353
354 my $r = {
355 _calc_cache => "cache!",
356 max_slots => 100,
357 assigned => {1 => {count => 1}, 2 => {count => 2}},
358 allocated => 3,
359 todo => 101,
360 };
361
362 is($one->_runner_calcs($r), "cache!", "Get cache if it is present");
363
364 delete $r->{_calc_cache};
365
366 is(
367 $one->_runner_calcs($r),
368 {
369 max => 9, # Use the global max as runner max is too high
370 assigned => 3,
371 active => 6, # Assigned + Allocated
372 total => 107, # Active + TODO
373 wants => 9, # We have more tests than slots, so we want the max
374 },
375 "Calculated data",
376 );
377 ok($r->{_calc_cache}, "Have a cache");
378 is($one->_runner_calcs($r), $r->{_calc_cache}, "Result matches cache");
379 $r->{_calc_cache}->{xxx} = 'added';
380 is($one->_runner_calcs($r), $r->{_calc_cache}, "Result matches cache");
381 is($one->_runner_calcs($r)->{xxx}, 'added', "Extra cache key found");
382
383 $r = {
384 max_slots => 5,
385 assigned => {1 => {count => 2}, 2 => {count => 2}},
386 allocated => 0,
387 todo => 101,
388 };
389 is(
390 $one->_runner_calcs($r),
391 {
392 max => 5, # Use our max, less than the global
393 assigned => 4,
394 active => 4, # Assigned + Allocated
395 total => 105, # Active + TODO
396 wants => 5, # We want our max
397 },
398 "Calculated data",
399 );
400
401 $r = {
402 assigned => {1 => {count => 5}, 2 => {count => 5}},
403 allocated => 2,
404 todo => 101,
405 };
406 is(
407 $one->_runner_calcs($r),
408 {
409 max => 9,
410 assigned => 10,
411 active => 12, # Assigned + Allocated
412 total => 113, # Active + TODO
413 wants => 12, # We want what we are already using, even though it is higher than max.
414 },
415 "Calculated data",
416 );
417 };
418
419 subtest allocate_slots => sub {
420 my $one = inst(runner_id => 'one');
421
422 like(dies { $one->allocate_slots(todo => 1) }, qr/'con' is required/, "con must be specified");
423
424 $one->{max_slots_per_job} = 10; $one->{my_max_slots_per_job} = 11; $one->{max_slots} = 11;
425 like(
426 dies { $one->allocate_slots(con => [11, 11], todo => 100) },
427 qr/Slot request exceeds max slots per job \(11 vs \(10 || 11 || 11\)\)/,
428 "Cannot exceed slot limits A"
429 );
430
431 $one->{max_slots_per_job} = 11; $one->{my_max_slots_per_job} = 10; $one->{max_slots} = 11;
432 like(
433 dies { $one->allocate_slots(con => [11, 11], todo => 100) },
434 qr/Slot request exceeds max slots per job \(11 vs \(11 || 10 || 11\)\)/,
435 "Cannot exceed slot limits B"
436 );
437
438 $one->{max_slots_per_job} = 11; $one->{my_max_slots_per_job} = 11; $one->{max_slots} = 10;
439 like(
440 dies { $one->allocate_slots(con => [11, 11], todo => 100) },
441 qr/Slot request exceeds max slots per job \(11 vs \(11 || 11 || 10\)\)/,
442 "Cannot exceed slot limits C"
443 );
444
445 $one->transaction(rw => sub {
446 my ($self, $state) = @_;
447
448 # Make sure we have an allocation so we do not trigger a redistribute.
449 $state->{runners}->{one}->{allocated} = 5;
450 $state->{runners}->{one}->{allotment} = 2;
451
452 # Do calcs and cache them so we can verify they get cleared.
453 my $calcs = $self->_runner_calcs($state->{runners}->{one});
454 $calcs->{CACHED} = 1;
455 });
456
457 ok($one->state->{runners}->{one}->{_calc_cache}->{CACHED}, "runner calc cache is as expected", $one->state->{runners}->{one}->{_calc_cache});
458 is($one->state->{runners}->{one}->{allocated}, 5, "Allocation is 5");
459 is($one->allocate_slots(con => [4, 4], job_id => '123'), 4, "We got 4 slots!");
460 ok(!$one->state->{runners}->{one}->{_calc_cache}->{CACHED}, "Allocating slots reset runner calc cache", $one->state->{runners}->{one}->{_calc_cache});
461 is($one->state->{runners}->{one}->{allocated}, 4, "Allocation updated to 4");
462 };
463
464 done_testing;
465
466 __END__
467
468 TODO do more testing on this
469
470 sub _allocate_slots {
471 my $self = shift;
472 my ($state, %params) = @_;
473
474 my $entry = $state->{runners}->{$self->{+RUNNER_ID}};
475 delete $entry->{_calc_cache};
476
477 my $count = $params{count};
478 my $job_id = $params{job_id};
479 $self->_runner_todo($entry, $job_id => $count);
480
481 my $allocated = $entry->{allocated};
482
483 # We have what we need already allocated
484 return $entry->{allocated} = $count
485 if $count <= $allocated;
486
487 # Our allocation, if any, is not big enough, free it so we do not have a
488 # deadlock with all runner holding an insufficient allocation.
489 $allocated = $entry->{allocated} = 0;
490
491 my $calcs = $self->_runner_calcs($entry);
492
493 for (0 .. 1) {
494 $self->_redistribute($state) if $_; # Only run on second loop
495
496 # Cannot do anything if we have no allotment or no available slots.
497 # This will go to the next loop for a redistribution, or end the loop.
498 my $allotment = $entry->{allotment} or next;
499 my $available = $allotment - $calcs->{assigned} or next;
500
501 # If our allotment is lower than the count we may end up never getting
502 # enough, so we forcefully reduce the count.
503 # We do this for busy systems where the pool is too small to meet the
504 # request. But we do not reduce the count to the available level,
505 # availability can change to match the allotment.
506 my $c = min($allotment, $count);
507
508 next unless $available >= $c;
509 return $entry->{allocated} = $c;
510 }
511
512 return 0;
513 }
514
515 sub assign_slots {
516 my $self = shift;
517 my (%params) = @_;
518
519 my $job = $params{job} or croak "'job' is required";
520
521 return $self->transaction(rw => '_assign_slots', job => $job);
522 }
523
524 sub _assign_slots {
525 my $self = shift;
526 my ($state, %params) = @_;
527
528 my $entry = $state->{runners}->{$self->{+RUNNER_ID}};
529 delete $entry->{_calc_cache};
530
531 my $job = $params{job};
532 my $job_id = $job->{job_id};
533 my $allocated = $entry->{allocated};
534
535 my $count = $self->_runner_todo($entry, $job_id => -1);
536
537 $job->{count} = $count;
538 $job->{started} = time;
539
540 $entry->{allocated} = 0;
541
542 $entry->{assigned}->{$job->{job_id}} = $job;
543
544 return $job;
545 }
546
547 sub release_slots {
548 my $self = shift;
549 my (%params) = @_;
550
551 my $job_id = $params{job_id} or croak "'job_id' is required";
552
553 return $self->transaction(rw => '_release_slots', job_id => $job_id);
554 }
555
556 sub _release_slots {
557 my $self = shift;
558 my ($state, %params) = @_;
559
560 my $entry = $state->{runners}->{$self->{+RUNNER_ID}};
561
562 my $job_id = $params{job_id};
563
564 delete $entry->{assigned}->{$job_id};
565 delete $entry->{_calc_cache};
566
567 $self->_runner_todo($entry, $job_id => -1);
568
569 # Reduce our allotment if it makes sense to do so.
570 my $calcs = $self->_runner_calcs($entry);
571 $entry->{allotment} = $calcs->{total} if $entry->{allotment} > $calcs->{total};
572 }
573
574 sub _redistribute {
575 my $self = shift;
576 my ($state) = @_;
577
578 my $max_run = $self->{+MAX_SLOTS_PER_RUN};
579
580 my $wanted = 0;
581 for my $runner (values %{$state->{+RUNNERS}}) {
582 my $calcs = $self->_runner_calcs($runner);
583 $runner->{allotment} = $calcs->{wants};
584 $wanted += $calcs->{wants};
585 }
586
587 # Everyone gets what they want!
588 my $max = $self->{+MAX_SLOTS};
589 return if $wanted <= $max;
590
591 my $meth = $self->{+ALGORITHM};
592
593 return $self->$meth($state);
594 }
595
596 sub _redistribute_first {
597 my $self = shift;
598 my ($state) = @_;
599
600 my $min = $self->{+MIN_SLOTS_PER_RUN};
601 my $max = $self->{+MAX_SLOTS};
602
603 my $c = 0;
604 for my $runner (sort { $a->{added} <=> $b->{added} } values %{$state->{+RUNNERS}}) {
605 my $calcs = $self->_runner_calcs($runner);
606 my $wants = $calcs->{wants};
607
608 if ($max >= $wants) {
609 $runner->{allotment} = $wants;
610 }
611 else {
612 $runner->{allotment} = max($max, $min, 0);
613 }
614
615 $max -= $runner->{allotment};
616
617 $c++;
618 }
619
620 return;
621 }
622
623 sub _redistribute_fair {
624 my $self = shift;
625 my ($state) = @_;
626
627 my $runs = scalar keys %{$state->{+RUNNERS}};
628
629 # Avoid a divide by 0 below.
630 return unless $runs;
631
632 my $total = $self->{+MAX_SLOTS};
633 my $min = $self->{+MIN_SLOTS_PER_RUN};
634
635 my $used = 0;
636 for my $runner (values %{$state->{+RUNNERS}}) {
637 my $calcs = $self->_runner_calcs($runner);
638
639 # We never want less than the 'active' number
640 my $set = $calcs->{active};
641
642 # If min is greater than the active number and there are todo tests, we
643 # use the min instead.
644 $set = $min if $set < $min && $runner->todo;
645
646 $runner->{allotment} = $set;
647 $used += $set;
648 }
649
650 my $free = $total - $used;
651 return unless $free >= 1;
652
653 # Is there a more efficient way to do this? Yikes!
654 my @runners = values %{$state->{+RUNNERS}};
655 while ($free > 0) {
656 @runners = sort { $a->{allotment} <=> $b->{allotment} || $a->{added} <=> $b->{added} }
657 grep { my $c = $self->_runner_calcs($_); $c->{wants} > $_->{allotment} }
658 @runners;
659
660 $free--;
661 $runners[0]->{allotment}++;
662 }
663
664 return;
665 }
666
667