| 1 |
package Test2::Harness::Stall::Detector; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/croak/; |
| 8 |
use List::Util qw/first/; |
| 9 |
use File::Spec(); |
| 10 |
use Time::HiRes qw/time/; |
| 11 |
|
| 12 |
use Test2::Harness::Util::Queue(); |
| 13 |
|
| 14 |
use Test2::Harness::Util::HashBase qw{ |
| 15 |
<workdir <job_count |
| 16 |
<strong <loose |
| 17 |
|
| 18 |
+queue +state |
| 19 |
+last_check +last_start +first_ready |
| 20 |
+scheduler_pid +stage_pids |
| 21 |
+reports +replay_errors |
| 22 |
}; |
| 23 |
|
| 24 |
# Give up after this many consecutive replay failures. A State handler that |
| 25 |
# dislikes a record will dislike it every time, and re-reading the whole |
| 26 |
# dispatch file once a second is worse than not reporting. |
| 27 |
use constant MAX_REPLAY_ERRORS => 3; |
| 28 |
|
| 29 |
# Actions only the scheduler process enqueues. start_run is the useful one: it |
| 30 |
# is written before any task starts, so it is present even in a run where |
| 31 |
# nothing ever ran. |
| 32 |
my %SCHEDULER_ACTIONS = (start_run => 1, start_task => 1); |
| 33 |
|
| 34 |
# How often check() is allowed to touch the filesystem. The render loop calls |
| 35 |
# it thousands of times a second. |
| 36 |
use constant CHECK_INTERVAL => 1; |
| 37 |
|
| 38 |
# Wait this long before saying the same stall again, so a benign tail cannot |
| 39 |
# flood a CI log, and stop entirely after MAX_REPORTS. |
| 40 |
use constant REPEAT_INTERVAL => 300; |
| 41 |
use constant MAX_REPORTS => 5; |
| 42 |
|
| 43 |
sub init { |
| 44 |
my $self = shift; |
| 45 |
|
| 46 |
croak "You must specify a workdir" unless defined $self->{+WORKDIR}; |
| 47 |
|
| 48 |
$self->{+JOB_COUNT} //= 1; |
| 49 |
$self->{+REPORTS} = []; |
| 50 |
|
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
# Parse a --stall-report value: 'STRONG:LOOSE', or one number for both. |
| 55 |
# Returns nothing when reporting is disabled. |
| 56 |
sub parse_spec { |
| 57 |
my $class = shift; |
| 58 |
my ($spec) = @_; |
| 59 |
|
| 60 |
return unless defined $spec && length "$spec"; |
| 61 |
|
| 62 |
my ($strong, $loose) = split /:/, "$spec", 2; |
| 63 |
|
| 64 |
$loose = $strong unless defined $loose && length $loose; |
| 65 |
|
| 66 |
# An opt-in diagnostic that silently disables itself on a typo is a trap: |
| 67 |
# the site turns it on once and only finds out years later. |
| 68 |
for my $val ($strong, $loose) { |
| 69 |
croak "Invalid --stall-report value '$spec', want SECONDS or STRONG:LOOSE" |
| 70 |
unless defined $val && $val =~ m/^\d+(?:\.\d+)?$/; |
| 71 |
} |
| 72 |
|
| 73 |
return if $strong <= 0 && $loose <= 0; |
| 74 |
|
| 75 |
return (strong => $strong, loose => $loose); |
| 76 |
} |
| 77 |
|
| 78 |
# A State that can never reach a user resource class. State::init only builds |
| 79 |
# the classes named in the settings when 'resources' is empty, so handing it an |
| 80 |
# in-tree resource means _stop_task's release() only ever reaches JobCount. |
| 81 |
# Without this the main process would run the very callback most likely to be |
| 82 |
# wedged. Do not remove the resources argument. |
| 83 |
sub build_state { |
| 84 |
my $self = shift; |
| 85 |
|
| 86 |
# Loaded here rather than at compile time: the collector loads this module |
| 87 |
# only to ask whether reporting is on, and should not pay for the runner's |
| 88 |
# state machine to answer that. |
| 89 |
require Test2::Harness::Runner::State; |
| 90 |
require Test2::Harness::Runner::Resource::JobCount; |
| 91 |
|
| 92 |
return Test2::Harness::Runner::State->new( |
| 93 |
workdir => $self->{+WORKDIR}, |
| 94 |
job_count => $self->{+JOB_COUNT}, |
| 95 |
observe => 1, |
| 96 |
resources => [ |
| 97 |
Test2::Harness::Runner::Resource::JobCount->new( |
| 98 |
job_count => $self->{+JOB_COUNT}, |
| 99 |
), |
| 100 |
], |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
# Read only for the record stamps State does not expose. last_job_activity is |
| 105 |
# no use here, it moves when a test stops as well as when one starts. |
| 106 |
sub queue { |
| 107 |
my $self = shift; |
| 108 |
|
| 109 |
return $self->{+QUEUE} //= Test2::Harness::Util::Queue->new( |
| 110 |
file => File::Spec->catfile($self->{+WORKDIR}, 'dispatch.jsonl'), |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
sub poll_stamps { |
| 115 |
my $self = shift; |
| 116 |
|
| 117 |
my $items = eval { [$self->queue->poll] }; |
| 118 |
|
| 119 |
unless ($items) { |
| 120 |
my $count = ++$self->{+REPLAY_ERRORS}; |
| 121 |
warn "Stall detector could not read the dispatch queue: $@" if $count <= MAX_REPLAY_ERRORS; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
for my $item (@$items) { |
| 126 |
my $data = $item->[-1] or next; |
| 127 |
my $action = $data->{action} or next; |
| 128 |
my $stamp = $data->{stamp}; |
| 129 |
|
| 130 |
$self->{+LAST_START} = $stamp if $action eq 'start_task'; |
| 131 |
|
| 132 |
if ($action eq 'stage_ready') { |
| 133 |
$self->{+FIRST_READY} //= $stamp; |
| 134 |
$self->{+STAGE_PIDS}->{$data->{item}} = $data->{pid} if $data->{pid}; |
| 135 |
} |
| 136 |
|
| 137 |
# A stage that has gone down must leave the list. Its pid can be |
| 138 |
# recycled, and SIGUSR1 to a process that never installed the handler |
| 139 |
# kills it. |
| 140 |
delete $self->{+STAGE_PIDS}->{$data->{item}} if $action eq 'stage_down'; |
| 141 |
|
| 142 |
$self->{+SCHEDULER_PID} //= $data->{pid} |
| 143 |
if $data->{pid} && $SCHEDULER_ACTIONS{$action}; |
| 144 |
} |
| 145 |
|
| 146 |
return 1; |
| 147 |
} |
| 148 |
|
| 149 |
sub stage_pids { $_[0]->{+STAGE_PIDS} // {} } |
| 150 |
|
| 151 |
sub replay { |
| 152 |
my $self = shift; |
| 153 |
|
| 154 |
# State's handlers die on anything they consider inconsistent, and they |
| 155 |
# were not written for read-only replay in another process. State::init |
| 156 |
# polls as its last act, so construction can die too and has to be inside |
| 157 |
# the same guard. A detector must never be able to end a healthy run. |
| 158 |
my $state = $self->{+STATE}; |
| 159 |
my $ok = eval { |
| 160 |
$state //= $self->build_state(); |
| 161 |
$state->poll; |
| 162 |
1; |
| 163 |
}; |
| 164 |
my $err = $@; |
| 165 |
|
| 166 |
unless ($ok) { |
| 167 |
$self->{+STATE} = undef; |
| 168 |
|
| 169 |
my $count = ++$self->{+REPLAY_ERRORS}; |
| 170 |
|
| 171 |
warn "Stall detector could not read the run state: $err"; |
| 172 |
warn "Stall reporting disabled after $count failures.\n" if $count == MAX_REPLAY_ERRORS; |
| 173 |
|
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$self->{+STATE} = $state; |
| 178 |
|
| 179 |
return $state; |
| 180 |
} |
| 181 |
|
| 182 |
sub pending_tasks { |
| 183 |
my $self = shift; |
| 184 |
my ($state) = @_; |
| 185 |
|
| 186 |
my @out; |
| 187 |
my $pending = $state->pending_tasks // {}; |
| 188 |
for my $run_id (keys %$pending) { |
| 189 |
for my $smoke (values %{$pending->{$run_id} // {}}) { |
| 190 |
for my $stage (values %{$smoke // {}}) { |
| 191 |
for my $cat (values %{$stage // {}}) { |
| 192 |
for my $dur (values %{$cat // {}}) { |
| 193 |
push @out => @{$dur // []}; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return \@out; |
| 201 |
} |
| 202 |
|
| 203 |
# True when every pending test is one the scheduler is right to be holding |
| 204 |
# back. These waits are legitimate and can last as long as the longest running |
| 205 |
# test, so reporting them would be noise. |
| 206 |
sub all_pending_blocked { |
| 207 |
my $self = shift; |
| 208 |
my ($state, $tasks) = @_; |
| 209 |
|
| 210 |
my $running = $state->running // 0; |
| 211 |
my $categories = $state->running_categories // {}; |
| 212 |
my $conflicts = $state->running_conflicts // {}; |
| 213 |
my $shares = $state->running_shares // {}; |
| 214 |
|
| 215 |
for my $task (@$tasks) { |
| 216 |
my $cat = $task->{category} // 'general'; |
| 217 |
|
| 218 |
next if $cat eq 'isolation' && $running; |
| 219 |
next if $cat eq 'immiscible' && $categories->{immiscible}; |
| 220 |
|
| 221 |
# These mirror the two rejections in State::_next. An exclusive claim |
| 222 |
# waits for anything holding the name, exclusively or shared; a shared |
| 223 |
# claim waits only for an exclusive holder. |
| 224 |
next if first { $conflicts->{$_} || $shares->{$_} } @{$task->{conflicts} // []}; |
| 225 |
next if first { $conflicts->{$_} } @{$task->{shares} // []}; |
| 226 |
|
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
return 1; |
| 231 |
} |
| 232 |
|
| 233 |
sub check { |
| 234 |
my $self = shift; |
| 235 |
|
| 236 |
return unless $self->{+STRONG} || $self->{+LOOSE}; |
| 237 |
return if @{$self->{+REPORTS}} >= MAX_REPORTS; |
| 238 |
return if ($self->{+REPLAY_ERRORS} // 0) >= MAX_REPLAY_ERRORS; |
| 239 |
|
| 240 |
my $now = time; |
| 241 |
return if $self->{+LAST_CHECK} && $now - $self->{+LAST_CHECK} < CHECK_INTERVAL; |
| 242 |
$self->{+LAST_CHECK} = $now; |
| 243 |
|
| 244 |
my $polled = $self->poll_stamps(); |
| 245 |
my $state = $self->replay() or return; |
| 246 |
|
| 247 |
# Both readers got through in this call, so nothing is persistently wrong. |
| 248 |
# Consecutive, as MAX_REPLAY_ERRORS says: a healthy run must clear it, not |
| 249 |
# only a run that is about to report. |
| 250 |
$self->{+REPLAY_ERRORS} = 0 if $polled; |
| 251 |
|
| 252 |
return unless $self->{+FIRST_READY}; |
| 253 |
return unless grep { $_ } values %{$state->stage_readiness // {}}; |
| 254 |
|
| 255 |
my $tasks = $self->pending_tasks($state); |
| 256 |
return unless @$tasks; |
| 257 |
|
| 258 |
my $running = $state->running // 0; |
| 259 |
my $tier = $running ? 'loose' : 'strong'; |
| 260 |
my $threshold = $running ? $self->{+LOOSE} : $self->{+STRONG}; |
| 261 |
return unless $threshold; |
| 262 |
|
| 263 |
my $since = $now - ($self->{+LAST_START} // $self->{+FIRST_READY}); |
| 264 |
return if $since < $threshold; |
| 265 |
|
| 266 |
return if $self->all_pending_blocked($state, $tasks); |
| 267 |
|
| 268 |
my $last = $self->{+REPORTS}->[-1]; |
| 269 |
return if $last && $now - $last < REPEAT_INTERVAL; |
| 270 |
|
| 271 |
push @{$self->{+REPORTS}} => $now; |
| 272 |
|
| 273 |
return { |
| 274 |
tier => $tier, |
| 275 |
idle => $since, |
| 276 |
threshold => $threshold, |
| 277 |
running => $running, |
| 278 |
pending => scalar(@$tasks), |
| 279 |
round => scalar(@{$self->{+REPORTS}}), |
| 280 |
state => $state, |
| 281 |
tasks => $tasks, |
| 282 |
|
| 283 |
scheduler_pid => $self->{+SCHEDULER_PID}, |
| 284 |
stage_pids => $self->stage_pids, |
| 285 |
}; |
| 286 |
} |
| 287 |
|
| 288 |
1; |
| 289 |
|
| 290 |
__END__ |
| 291 |
|
| 292 |
=pod |
| 293 |
|
| 294 |
=encoding UTF-8 |
| 295 |
|
| 296 |
=head1 NAME |
| 297 |
|
| 298 |
Test2::Harness::Stall::Detector - Notice when the scheduler has stopped |
| 299 |
starting tests. |
| 300 |
|
| 301 |
=head1 DESCRIPTION |
| 302 |
|
| 303 |
A stall is new tests not being started even though tests are pending and there |
| 304 |
is capacity to start them. Whether other tests are currently running is |
| 305 |
irrelevant; a stall is about what is not being started. |
| 306 |
|
| 307 |
This class decides when that has happened. It is driven from the main |
| 308 |
C<yath test> process, which is healthy during a stall and is an ancestor of the |
| 309 |
scheduler. It adds no bookkeeping to the runner: everything it needs is already |
| 310 |
in the run's own C<dispatch.jsonl>. |
| 311 |
|
| 312 |
Two readers are involved. A raw L<Test2::Harness::Util::Queue> supplies the |
| 313 |
record stamps, because C<last_job_activity> moves when a test stops as well as |
| 314 |
when one starts. A L<Test2::Harness::Runner::State> replay supplies the counts. |
| 315 |
|
| 316 |
The replay is built with an explicit in-tree C<resources> list. |
| 317 |
C<Test2::Harness::Runner::State> only constructs the resource classes named in |
| 318 |
the settings when none are supplied, so passing one keeps C<release()> away |
| 319 |
from user code -- the callback most likely to be wedged when this fires. B<Do |
| 320 |
not remove that argument.> |
| 321 |
|
| 322 |
Nothing here may end a healthy run, so the replay is wrapped and repeated |
| 323 |
failures disable reporting rather than repeating forever. |
| 324 |
|
| 325 |
=head2 WHAT IS NOT A STALL |
| 326 |
|
| 327 |
Two waits are suppressed, because the scheduler is right to hold back and both |
| 328 |
can last as long as the longest running test: pending tests that are all |
| 329 |
C<isolation> while something is running, and pending tests all blocked by a |
| 330 |
conflict already held. Reporting is also gated on a stage being currently |
| 331 |
ready, so a long preload is not mistaken for a stall. |
| 332 |
|
| 333 |
=head1 SYNOPSIS |
| 334 |
|
| 335 |
use Test2::Harness::Stall::Detector; |
| 336 |
|
| 337 |
my %spec = Test2::Harness::Stall::Detector->parse_spec('600:1200') |
| 338 |
or return; |
| 339 |
|
| 340 |
my $detector = Test2::Harness::Stall::Detector->new( |
| 341 |
%spec, |
| 342 |
workdir => $workdir, |
| 343 |
job_count => $job_count, |
| 344 |
); |
| 345 |
|
| 346 |
# Called often; it rate-limits itself. |
| 347 |
if (my $found = $detector->check) { |
| 348 |
... |
| 349 |
} |
| 350 |
|
| 351 |
=head1 ATTRIBUTES |
| 352 |
|
| 353 |
=over 4 |
| 354 |
|
| 355 |
=item $string = $detector->workdir() |
| 356 |
|
| 357 |
The run's working directory. |
| 358 |
|
| 359 |
=item $int = $detector->job_count() |
| 360 |
|
| 361 |
Used only to build the replay's job limiter. |
| 362 |
|
| 363 |
=item $seconds = $detector->strong() |
| 364 |
|
| 365 |
How long to wait before reporting while tests are pending and none are running. |
| 366 |
|
| 367 |
=item $seconds = $detector->loose() |
| 368 |
|
| 369 |
How long to wait before reporting while tests are pending and others are still |
| 370 |
running. |
| 371 |
|
| 372 |
=back |
| 373 |
|
| 374 |
=head1 PUBLIC METHODS |
| 375 |
|
| 376 |
=over 4 |
| 377 |
|
| 378 |
=item %spec = $class->parse_spec($string) |
| 379 |
|
| 380 |
Parses a C<--stall-report> value, either C<SECONDS> or C<STRONG:LOOSE>. Returns |
| 381 |
an empty list when reporting is disabled, and croaks on an unparseable value. |
| 382 |
|
| 383 |
=item $hashref = $detector->check() |
| 384 |
|
| 385 |
Returns nothing unless a stall is being reported. Rate-limits itself, so it is |
| 386 |
safe to call from a busy loop. The returned hashref describes what was |
| 387 |
observed, and carries the replayed state, the pending tasks, and the scheduler |
| 388 |
and stage pids. |
| 389 |
|
| 390 |
=item $hashref = $detector->stage_pids() |
| 391 |
|
| 392 |
Stage name to pid, for stages that are currently up. These feed the signal |
| 393 |
whitelist, so a stage that has gone down is removed. |
| 394 |
|
| 395 |
=item $queue = $detector->queue() |
| 396 |
|
| 397 |
The raw dispatch queue reader, separate from the replay's own. Never |
| 398 |
C<$state-E<gt>dispatch_file>: that reader is stateful and the main process |
| 399 |
needs it untouched so C<stop()> can replay the whole file on Ctrl-C. |
| 400 |
|
| 401 |
=item $bool = $detector->poll_stamps() |
| 402 |
|
| 403 |
Reads new records for their stamps and pids, returning false if the queue |
| 404 |
could not be read. C<last_job_activity> cannot serve here, because it moves |
| 405 |
when a test stops as well as when one starts. |
| 406 |
|
| 407 |
=item $state = $detector->build_state() |
| 408 |
|
| 409 |
Builds the observer state. See the warning above about the C<resources> |
| 410 |
argument. |
| 411 |
|
| 412 |
=item $state = $detector->replay() |
| 413 |
|
| 414 |
Polls the observer state, returning nothing if it could not be read. Repeated |
| 415 |
failures disable reporting rather than re-reading the whole queue every second |
| 416 |
for the rest of the run. |
| 417 |
|
| 418 |
=item $arrayref = $detector->pending_tasks($state) |
| 419 |
|
| 420 |
Every pending task across all runs, flattened. |
| 421 |
|
| 422 |
=item $bool = $detector->all_pending_blocked($state, $tasks) |
| 423 |
|
| 424 |
Whether every pending test is one the scheduler is right to be holding back: |
| 425 |
an C<isolation> test while anything runs, an C<immiscible> one while another |
| 426 |
immiscible runs, an exclusive claim on a name something else holds either way, |
| 427 |
or a shared claim on a name something holds exclusively. These mirror the |
| 428 |
rejections in C<State::_next> and must follow it. |
| 429 |
|
| 430 |
=back |
| 431 |
|
| 432 |
=head1 SOURCE |
| 433 |
|
| 434 |
The source code repository for Test2-Harness can be found at |
| 435 |
L<https://github.com/Test-More/Test2-Harness/>. |
| 436 |
|
| 437 |
=head1 MAINTAINERS |
| 438 |
|
| 439 |
=over 4 |
| 440 |
|
| 441 |
=item Chad Granum E<lt>[email protected]<gt> |
| 442 |
|
| 443 |
=back |
| 444 |
|
| 445 |
=head1 AUTHORS |
| 446 |
|
| 447 |
=over 4 |
| 448 |
|
| 449 |
=item Chad Granum E<lt>[email protected]<gt> |
| 450 |
|
| 451 |
=back |
| 452 |
|
| 453 |
=head1 COPYRIGHT |
| 454 |
|
| 455 |
Copyright Chad Granum E<lt>[email protected]<gt>. |
| 456 |
|
| 457 |
This program is free software; you can redistribute it and/or |
| 458 |
modify it under the same terms as Perl itself. |
| 459 |
|
| 460 |
See L<https://dev.perl.org/licenses/> |
| 461 |
|
| 462 |
=cut |