| 1 |
package Test2::Harness::Collector::JobDir; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use File::Spec(); |
| 8 |
|
| 9 |
use Errno qw/EMFILE ENFILE/; |
| 10 |
|
| 11 |
use Carp qw/croak/; |
| 12 |
use Time::HiRes qw/time/; |
| 13 |
use List::Util qw/first/; |
| 14 |
|
| 15 |
use Test2::Util qw/ipc_separator/; |
| 16 |
|
| 17 |
use Test2::Harness::Util::UUID qw/gen_uuid/; |
| 18 |
use Test2::Harness::Util::JSON qw/decode_json/; |
| 19 |
use Test2::Harness::Util qw/maybe_read_file open_file apply_encoding/; |
| 20 |
|
| 21 |
use Test2::Harness::Event; |
| 22 |
|
| 23 |
use Test2::Harness::Util::File::Stream; |
| 24 |
use Test2::Harness::Util::File::JSONL; |
| 25 |
use Test2::Harness::Util::File::Value; |
| 26 |
|
| 27 |
use Test2::Harness::Collector::TapParser qw{ |
| 28 |
parse_stdout_tap |
| 29 |
parse_stderr_tap |
| 30 |
}; |
| 31 |
|
| 32 |
use Test2::Harness::Util::HashBase qw{ |
| 33 |
<run_id <job_id <job_try <job_root <runner_pid |
| 34 |
<done |
| 35 |
|
| 36 |
-_ready_buffer |
| 37 |
|
| 38 |
-_events_files -_events_buffer -_events_indexes -events_dir -_events_seen |
| 39 |
|
| 40 |
-stderr_file -_stderr_buffer -_stderr_index -_stderr_cg -_stderr_state |
| 41 |
-stdout_file -_stdout_buffer -_stdout_index -_stdout_cg -_stdout_state |
| 42 |
|
| 43 |
-exit_file -_exit_done -_exit_buffer |
| 44 |
|
| 45 |
-et_file -et_buffer -et_done |
| 46 |
-pet_file -pet_buffer -pet_done |
| 47 |
|
| 48 |
-last_stamp |
| 49 |
|
| 50 |
-open_errors -open_error_seen |
| 51 |
}; |
| 52 |
|
| 53 |
sub init { |
| 54 |
my $self = shift; |
| 55 |
|
| 56 |
croak "'run_id' is a required attribute" |
| 57 |
unless $self->{+RUN_ID}; |
| 58 |
|
| 59 |
croak "'job_id' is a required attribute" |
| 60 |
unless $self->{+JOB_ID}; |
| 61 |
|
| 62 |
croak "'job_root' is a required attribute" |
| 63 |
unless $self->{+JOB_ROOT}; |
| 64 |
|
| 65 |
$self->{+_EVENTS_SEEN} = {}; |
| 66 |
|
| 67 |
$self->{+_STDOUT_BUFFER} ||= []; |
| 68 |
$self->{+_STDERR_BUFFER} ||= []; |
| 69 |
$self->{+_EVENTS_BUFFER} ||= {}; |
| 70 |
$self->{+_READY_BUFFER} ||= []; |
| 71 |
|
| 72 |
$self->{+LAST_STAMP} = time(); |
| 73 |
} |
| 74 |
|
| 75 |
sub poll { |
| 76 |
my $self = shift; |
| 77 |
my ($max) = @_; |
| 78 |
|
| 79 |
delete $self->{+OPEN_ERRORS}; |
| 80 |
|
| 81 |
$self->_fill_buffers($max); |
| 82 |
|
| 83 |
return @{delete $self->{+OPEN_ERRORS}} if $self->{+OPEN_ERRORS}; |
| 84 |
|
| 85 |
my (@out, @new); |
| 86 |
|
| 87 |
# If we have a max number of events then we need to pass that along to the |
| 88 |
# inner-pollers, but we need to pass around how many MORE we need, this sub |
| 89 |
# will return the amount we still need. |
| 90 |
# If this finds that we do not need any more it will exit the loop instead |
| 91 |
# of returning a number. |
| 92 |
my $check = defined($max) |
| 93 |
? sub { |
| 94 |
my $want = $max - scalar(@out) - scalar(@new); |
| 95 |
return undef if $want < 1; |
| 96 |
return $want; |
| 97 |
} |
| 98 |
: sub { 1 }; |
| 99 |
|
| 100 |
while (!defined($max) || @out < $max) { |
| 101 |
push @new => $self->_poll_streams($check->() // last); |
| 102 |
|
| 103 |
push @new => $self->_poll_timeouts($check->() // last) if $self->{+ET_BUFFER} || $self->{+PET_BUFFER}; |
| 104 |
|
| 105 |
# 'exit' MUST come last, so do not even think about grabbing |
| 106 |
# them until @new is empty. |
| 107 |
# Micro-optimization, 'exit' only ever has 1 thing, so do |
| 108 |
# not enter the subs if we do not need to. |
| 109 |
push @new => $self->_poll_exit($check->() // last) if !@new && defined $self->{+_EXIT_BUFFER}; |
| 110 |
# We need to check if the runner exited BEFORE trying to check the exit value. |
| 111 |
|
| 112 |
last unless @new; |
| 113 |
|
| 114 |
push @out => @new; |
| 115 |
@new = (); |
| 116 |
} |
| 117 |
|
| 118 |
return map { |
| 119 |
my $stamp = $_->{stamp} ? $self->{+LAST_STAMP} = $_->{stamp} : $self->{+LAST_STAMP}; |
| 120 |
Test2::Harness::Event->new(stamp => $stamp, %{$_}); |
| 121 |
} @out; |
| 122 |
} |
| 123 |
|
| 124 |
sub _poll_streams { |
| 125 |
my $self = shift; |
| 126 |
my ($max) = @_; |
| 127 |
|
| 128 |
my $ready = $self->{+_READY_BUFFER}; |
| 129 |
return splice(@$ready, 0, $max) unless @$ready < $max; |
| 130 |
|
| 131 |
my $stdout = $self->{+_STDOUT_BUFFER}; |
| 132 |
my $stdout_cg = $self->{+_STDOUT_CG} ||= []; |
| 133 |
my $stdout_params = { |
| 134 |
buffer => $stdout, |
| 135 |
comment_group => $stdout_cg, |
| 136 |
tag => 'STDOUT', |
| 137 |
debug => 0, |
| 138 |
parser => \&parse_stdout_tap, |
| 139 |
max => $max, |
| 140 |
}; |
| 141 |
|
| 142 |
my $stderr = $self->{+_STDERR_BUFFER}; |
| 143 |
my $stderr_cg = $self->{+_STDERR_CG} ||= []; |
| 144 |
my $stderr_params = { |
| 145 |
buffer => $stderr, |
| 146 |
comment_group => $stderr_cg, |
| 147 |
tag => 'STDERR', |
| 148 |
debug => 1, |
| 149 |
parser => \&parse_stderr_tap, |
| 150 |
max => $max, |
| 151 |
}; |
| 152 |
|
| 153 |
my $out_event = $self->_poll_stream($stdout_params); |
| 154 |
my $err_event = $self->_poll_stream($stderr_params); |
| 155 |
|
| 156 |
# Once both stderr and stdout are waiting for an event we should go ahead |
| 157 |
# and stick the events into ready. More often than not both streams will be |
| 158 |
# waiting for the same event, the read_buffer_event logic will avoid |
| 159 |
# duplicates. We want to call it on both buffers because some IPC |
| 160 |
# situations can result in both streams waiting for different events. Also |
| 161 |
# we need the sync point removed from both buffers so things can continue. |
| 162 |
# This is an intentional bottle-neck that keeps STDOUT, STDERR, and the |
| 163 |
# Test2 events in sync so that stderr and stdout appear where they should |
| 164 |
# (mostly) relative to the events. This is not perfect, but it is as close |
| 165 |
# as we can get when recombining 3+ output streams. |
| 166 |
if ($out_event && $err_event) { |
| 167 |
$self->_poll_streams_ready_buffer_event($stdout); |
| 168 |
$self->_poll_streams_ready_buffer_event($stderr); |
| 169 |
} |
| 170 |
|
| 171 |
if ($self->{+_EXIT_DONE} && (!$max || @$ready < $max)) { |
| 172 |
# All done, flush the comment groups |
| 173 |
$self->_poll_stream_flush_group($stdout_params) if @$stdout_cg; |
| 174 |
$self->_poll_stream_flush_group($stderr_params) if @$stderr_cg; |
| 175 |
|
| 176 |
$self->_poll_streams_flush_events(); |
| 177 |
} |
| 178 |
|
| 179 |
return splice(@$ready, 0, $max); |
| 180 |
} |
| 181 |
|
| 182 |
sub _poll_streams_flush_events { |
| 183 |
my $self = shift; |
| 184 |
|
| 185 |
my $buffers = $self->{+_EVENTS_BUFFER}; |
| 186 |
for my $pid (keys %$buffers) { |
| 187 |
for my $tid (keys %{$buffers->{$pid}}) { |
| 188 |
my $buffer = $buffers->{$pid}->{$tid} or next; |
| 189 |
while(my $e = shift @$buffer) { |
| 190 |
$e = ref($e) ? $e : decode_json($e); |
| 191 |
push @{$self->{+_READY_BUFFER}} => $self->_process_events_line($e); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
sub _poll_streams_ready_buffer_event { |
| 198 |
my $self = shift; |
| 199 |
my ($buffer) = @_; |
| 200 |
|
| 201 |
my $set = shift @$buffer; |
| 202 |
my ($pid, $tid, $sid) = @$set; |
| 203 |
|
| 204 |
my $seen = $self->{+_EVENTS_SEEN}; |
| 205 |
return if $seen->{$tid}->{$pid}->{$sid}; |
| 206 |
|
| 207 |
my $e = shift @{$self->{+_EVENTS_BUFFER}->{$pid}->{$tid}} or return; |
| 208 |
$seen->{$tid}->{$pid}->{$sid} = 1; |
| 209 |
|
| 210 |
$e = ref($e) ? $e : decode_json($e); |
| 211 |
|
| 212 |
die "Stream error: Events skipped or recieved out of order ($e->{stream_id} != $sid)" |
| 213 |
if $e->{stream_id} != $sid; |
| 214 |
|
| 215 |
push @{$self->{+_READY_BUFFER}} => $self->_process_events_line($e); |
| 216 |
} |
| 217 |
|
| 218 |
sub _poll_stream_add_event { |
| 219 |
my $self = shift; |
| 220 |
my ($line, $params) = @_; |
| 221 |
|
| 222 |
my $parser = $params->{parser}; |
| 223 |
my $tag = $params->{tag}; |
| 224 |
my $debug = $params->{debug}; |
| 225 |
|
| 226 |
my $facet_data = $parser->($line); |
| 227 |
$facet_data ||= {info => [{details => $line, tag => $tag, debug => $debug}]}; |
| 228 |
my $event_id = $facet_data->{about}->{uuid} ||= gen_uuid(); |
| 229 |
|
| 230 |
push @{$self->{+_READY_BUFFER}} => { |
| 231 |
facet_data => $facet_data, |
| 232 |
event_id => $event_id, |
| 233 |
job_id => $self->{+JOB_ID}, |
| 234 |
job_try => $self->{+JOB_TRY}, |
| 235 |
run_id => $self->{+RUN_ID}, |
| 236 |
}; |
| 237 |
} |
| 238 |
|
| 239 |
sub _poll_stream_flush_group { |
| 240 |
my $self = shift; |
| 241 |
my ($params) = @_; |
| 242 |
|
| 243 |
my $comment_group = $params->{comment_group}; |
| 244 |
|
| 245 |
return unless @$comment_group; |
| 246 |
|
| 247 |
shift @$comment_group; # Remove the indentation state |
| 248 |
|
| 249 |
my $line = join "\n" => @$comment_group; |
| 250 |
$self->_poll_stream_add_event($line, $params); |
| 251 |
@$comment_group = (); |
| 252 |
} |
| 253 |
|
| 254 |
sub _poll_stream_buffer_group { |
| 255 |
my $self = shift; |
| 256 |
my ($line, $params) = @_; |
| 257 |
|
| 258 |
return undef unless $line =~ m/^(\s*)#/; |
| 259 |
my $indent = $1; |
| 260 |
|
| 261 |
my $comment_group = $params->{comment_group}; |
| 262 |
|
| 263 |
if (@$comment_group && $comment_group->[0] ne $indent) { |
| 264 |
# If comment indentation has changed we do not want to append to the group |
| 265 |
$self->_poll_stream_flush_group($params); |
| 266 |
return 1; |
| 267 |
} |
| 268 |
else { |
| 269 |
# Starting a new group |
| 270 |
push @$comment_group => $indent; |
| 271 |
} |
| 272 |
|
| 273 |
push @$comment_group => $line; |
| 274 |
shift @{$params->{buffer}}; |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
sub _poll_stream { |
| 279 |
my $self = shift; |
| 280 |
my ($params) = @_; |
| 281 |
|
| 282 |
my $max = $params->{max}; |
| 283 |
my $buff = $params->{buffer}; |
| 284 |
my $comment_group = $params->{comment_group}; |
| 285 |
|
| 286 |
my $added = 0; |
| 287 |
while (@$buff && (!$max || $added < $max)) { |
| 288 |
my $line = $buff->[0]; |
| 289 |
|
| 290 |
# Already have an esync waiting |
| 291 |
return 1 if ref $line; |
| 292 |
|
| 293 |
chomp($line); |
| 294 |
|
| 295 |
my $esync = $self->_poll_stream_process_harness_line($line, $params); |
| 296 |
return 1 if $esync; |
| 297 |
|
| 298 |
# Put 'comment' lines together in a group, IE buffer this until we are done with comments |
| 299 |
# get undef if there was no comment to buffer |
| 300 |
# get 1 if we had to flush the buffer and start a new one |
| 301 |
# get 0 if we did buffer the event, but no flush |
| 302 |
my $stat = $self->_poll_stream_buffer_group($line, $params); |
| 303 |
if (defined($stat)) { |
| 304 |
$added += $stat; |
| 305 |
next; |
| 306 |
} |
| 307 |
|
| 308 |
# non-comment line, flush the comment group |
| 309 |
if (@$comment_group) { |
| 310 |
$self->_poll_stream_flush_group($params); |
| 311 |
$added++; |
| 312 |
next; |
| 313 |
} |
| 314 |
|
| 315 |
shift @$buff; |
| 316 |
$self->_poll_stream_add_event($line, $params); |
| 317 |
$added++; |
| 318 |
} |
| 319 |
|
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
sub _poll_stream_process_harness_line { |
| 324 |
my $self = shift; |
| 325 |
my ($line, $params) = @_; |
| 326 |
|
| 327 |
my $job_id = $self->{+JOB_ID}; |
| 328 |
return undef unless $line =~ s/T2-HARNESS-\Q$job_id\E-(ESYNC|EVENT): (.+)//; |
| 329 |
my ($type, $data) = ($1, $2); |
| 330 |
|
| 331 |
my $esync; |
| 332 |
if ($type eq 'ESYNC') { |
| 333 |
$esync = [split ipc_separator() => $data]; |
| 334 |
} |
| 335 |
elsif ($type eq 'EVENT') { |
| 336 |
my $event_data = decode_json($data); |
| 337 |
my $pid = $event_data->{pid}; |
| 338 |
my $tid = $event_data->{tid}; |
| 339 |
my $sid = $event_data->{stream_id}; |
| 340 |
|
| 341 |
push @{$self->{+_EVENTS_BUFFER}->{$pid}->{$tid}} => $event_data; |
| 342 |
$esync = [$pid, $tid, $sid]; |
| 343 |
} |
| 344 |
else { |
| 345 |
die "Unexpected harness type: $type"; |
| 346 |
} |
| 347 |
|
| 348 |
# This becomes the esync, anything leftover actually belongs to the |
| 349 |
# next line. |
| 350 |
my $buff = $params->{buffer}; |
| 351 |
$buff->[0] = $esync; |
| 352 |
$buff->[1] = defined($buff->[1]) ? $line . $buff->[1] : $line if length $line; |
| 353 |
|
| 354 |
# Flush any comment group already buffered, an event is a sane |
| 355 |
# boundary, not above that partial comments that might be |
| 356 |
# interrupted by the sync point will be part of the next group |
| 357 |
$self->_poll_stream_flush_group($params); |
| 358 |
|
| 359 |
return $esync; |
| 360 |
} |
| 361 |
|
| 362 |
my %FILE_MAP = ( |
| 363 |
'stdout' => [STDOUT_FILE, \&open_file], |
| 364 |
'stderr' => [STDERR_FILE, \&open_file], |
| 365 |
'exit' => [EXIT_FILE, 'Test2::Harness::Util::File::Value'], |
| 366 |
|
| 367 |
'event_timeout' => [ET_FILE, 'Test2::Harness::Util::File::Value'], |
| 368 |
'post_exit_timeout' => [PET_FILE, 'Test2::Harness::Util::File::Value'], |
| 369 |
); |
| 370 |
|
| 371 |
sub _open_file { |
| 372 |
my $self = shift; |
| 373 |
my ($file) = @_; |
| 374 |
|
| 375 |
my $map = $FILE_MAP{$file} or croak "'$file' is not a known job file"; |
| 376 |
my ($key, $type) = @$map; |
| 377 |
|
| 378 |
return $self->{$key} if $self->{$key}; |
| 379 |
|
| 380 |
my $path = File::Spec->catfile($self->{+JOB_ROOT}, $file); |
| 381 |
my $out; |
| 382 |
|
| 383 |
if (ref $type) { |
| 384 |
return undef unless -e $path; |
| 385 |
return $self->{$key} = $self->try_open($path => sub { $type->($path, '<') }); |
| 386 |
} |
| 387 |
|
| 388 |
return $self->{$key} = $self->try_open($path => sub { $type->new(name => $path) }); |
| 389 |
} |
| 390 |
|
| 391 |
sub _fill_stream_buffers { |
| 392 |
my $self = shift; |
| 393 |
my ($max) = @_; |
| 394 |
|
| 395 |
my $stdout_state = $self->{+_STDOUT_STATE} //= {}; |
| 396 |
my $stderr_state = $self->{+_STDERR_STATE} //= {}; |
| 397 |
|
| 398 |
my $stdout_buff = $self->{+_STDOUT_BUFFER} ||= []; |
| 399 |
my $stderr_buff = $self->{+_STDERR_BUFFER} ||= []; |
| 400 |
|
| 401 |
my $stdout_file = $self->{+STDOUT_FILE} || $self->_open_file('stdout'); |
| 402 |
my $stderr_file = $self->{+STDERR_FILE} || $self->_open_file('stderr'); |
| 403 |
|
| 404 |
return unless $stdout_file && $stderr_file; |
| 405 |
|
| 406 |
my @sets = grep { defined $_->[0] } ( |
| 407 |
[$stdout_file, $stdout_buff, 'io', 'STDOUT', $stdout_state], |
| 408 |
[$stderr_file, $stderr_buff, 'io', 'STDERR', $stderr_state], |
| 409 |
); |
| 410 |
|
| 411 |
return unless @sets; |
| 412 |
|
| 413 |
# Cache the result of the exists check on success, files can come into |
| 414 |
# existence at any time though so continue to check if it fails. |
| 415 |
while (1) { |
| 416 |
my $added = 0; |
| 417 |
my @events_files = $self->events_files(); |
| 418 |
for my $set (@events_files, @sets) { |
| 419 |
my ($file, $buff, $type, $name, $state) = @$set; |
| 420 |
next if $max && @$buff > $max; |
| 421 |
|
| 422 |
my $pos = tell($file); |
| 423 |
my $line = <$file>; |
| 424 |
if (defined($line) && ($self->{+_EXIT_DONE} || substr($line, -1) eq "\n")) { |
| 425 |
print "\n" if $state && delete $state->{$pos}; |
| 426 |
|
| 427 |
my $job_id = $self->{+JOB_ID}; |
| 428 |
if ($type eq 'io' && $line =~ s/T2-HARNESS-\Q$job_id\E-ENCODING: (.+)\n$//) { |
| 429 |
apply_encoding($file, $1); |
| 430 |
} |
| 431 |
|
| 432 |
push @$buff => $line if length($line); |
| 433 |
seek($file, 0, 1) if eof($file); # Reset EOF. |
| 434 |
$added++; |
| 435 |
} |
| 436 |
else { |
| 437 |
if ($name && defined($line) && $ENV{YATH_INTERACTIVE}) { |
| 438 |
my ($fh); |
| 439 |
|
| 440 |
if ($name eq 'STDOUT') { |
| 441 |
$fh = \*STDOUT; |
| 442 |
} |
| 443 |
elsif ($name eq 'STDERR') { |
| 444 |
$fh = \*STDERR; |
| 445 |
} |
| 446 |
|
| 447 |
my $len = length($line); |
| 448 |
if (my $check = $state->{$pos}->{len}) { |
| 449 |
if ($len != $check) { |
| 450 |
delete $state->{$pos}->{done}; |
| 451 |
$line = substr($line, $check); |
| 452 |
} |
| 453 |
else { |
| 454 |
$line = "\n[INTERACTIVE] $line"; |
| 455 |
} |
| 456 |
} |
| 457 |
else { |
| 458 |
$line = "\n[INTERACTIVE] $line"; |
| 459 |
} |
| 460 |
|
| 461 |
$state->{$pos}->{len} = $len; |
| 462 |
|
| 463 |
my $stamp = $state->{$pos}->{stamp} //= time; |
| 464 |
my $delta = time - $stamp; |
| 465 |
|
| 466 |
if($delta >= 1 && !$state->{$pos}->{done}) { |
| 467 |
$fh->autoflush(1); |
| 468 |
|
| 469 |
$state->{$pos}->{done} = 1; |
| 470 |
print $fh $line; |
| 471 |
} |
| 472 |
} |
| 473 |
seek($file, $pos, 0); |
| 474 |
} |
| 475 |
} |
| 476 |
last unless $added; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
sub events_files { |
| 481 |
my $self = shift; |
| 482 |
|
| 483 |
my $buff = $self->{+_EVENTS_BUFFER} ||= {}; |
| 484 |
my $files = $self->{+_EVENTS_FILES} ||= {}; |
| 485 |
|
| 486 |
my $dir = File::Spec->catdir($self->{+JOB_ROOT}, 'events'); |
| 487 |
return unless -d $dir; |
| 488 |
|
| 489 |
my $dh; |
| 490 |
if ($self->try_open($dir => sub { opendir($dh, $dir) or die $! })) { |
| 491 |
for my $file (readdir($dh)) { |
| 492 |
next unless '.jsonl' eq substr($file, -6); |
| 493 |
|
| 494 |
next if $files->{$file}; |
| 495 |
|
| 496 |
my $path = File::Spec->catfile($dir, $file); |
| 497 |
|
| 498 |
next if $files->{$file}; |
| 499 |
|
| 500 |
my $fh = $self->try_open( |
| 501 |
$path => sub { [ |
| 502 |
split(ipc_separator() => substr(substr($file, 6 + length(ipc_separator())), 0, -6)), |
| 503 |
open_file($path, '<'), |
| 504 |
] } |
| 505 |
); |
| 506 |
|
| 507 |
$files->{$file} = $fh if $fh; |
| 508 |
} |
| 509 |
closedir($dh); |
| 510 |
} |
| 511 |
|
| 512 |
return map { [$_->[2] => $buff->{$_->[0]}->{$_->[1]} ||= [], 'jsonl'] } values %$files; |
| 513 |
} |
| 514 |
|
| 515 |
sub try_open { |
| 516 |
my $self = shift; |
| 517 |
my ($path, $callback) = @_; |
| 518 |
|
| 519 |
local ($@, $?, $!, $.); |
| 520 |
|
| 521 |
my $out; |
| 522 |
my $ok = eval { |
| 523 |
$out = $callback->(); |
| 524 |
1; |
| 525 |
}; |
| 526 |
my $errno = $!; |
| 527 |
my $err = $@; |
| 528 |
|
| 529 |
return $out if $ok; |
| 530 |
|
| 531 |
die $@ unless $errno == ENFILE || $errno == EMFILE; |
| 532 |
|
| 533 |
my $errors = $self->{+OPEN_ERRORS} //= []; |
| 534 |
|
| 535 |
unless ($self->{+OPEN_ERROR_SEEN}->{$path}++) { |
| 536 |
push @$errors => Test2::Harness::Event->new( |
| 537 |
stamp => time, |
| 538 |
job_id => 0, |
| 539 |
job_try => undef, |
| 540 |
event_id => gen_uuid(), |
| 541 |
run_id => $self->{+RUN_ID}, |
| 542 |
facet_data => { |
| 543 |
info => [{ |
| 544 |
details => "Could not open '$path', this is NOT FATAL as yath will try again. Errno is '$errno', Exception was: $err", |
| 545 |
tag => 'INTERNAL', |
| 546 |
important => 1, |
| 547 |
}], |
| 548 |
} |
| 549 |
); |
| 550 |
} |
| 551 |
|
| 552 |
return undef; |
| 553 |
} |
| 554 |
|
| 555 |
sub _fill_buffers { |
| 556 |
my $self = shift; |
| 557 |
my ($max) = @_; |
| 558 |
# NOTE 1: 'max' will only effect stdout, stderr, and events.jsonl, the |
| 559 |
# other files only have 1 value each so they will not eat too much memory. |
| 560 |
# |
| 561 |
# NOTE 2: 'max' only effects how many items are ADDED to the buffer, not |
| 562 |
# how many are in the buffer, that is good enough, poll() will take care of |
| 563 |
# the actual event limiting. We only use this here to make sure the buffer |
| 564 |
# grows slowly, this is important if max is used to avoid eating memory. We |
| 565 |
# still need to add to the buffers each time though in case we are waiting |
| 566 |
# for a sync event before we flush. |
| 567 |
|
| 568 |
# Wait for the directory |
| 569 |
return unless -d $self->{+JOB_ROOT}; |
| 570 |
|
| 571 |
$self->_fill_stream_buffers($max); |
| 572 |
|
| 573 |
# Do not look for exit until we are done with the other streams |
| 574 |
return if $self->{+_EXIT_DONE} || @{$self->{+_STDOUT_BUFFER}} || @{$self->{+_STDERR_BUFFER}} || first { @$_ } map { values %{$_} } values %{$self->{+_EVENTS_BUFFER}}; |
| 575 |
|
| 576 |
$self->_open_file('event_timeout'); |
| 577 |
$self->_open_file('post_exit_timeout'); |
| 578 |
|
| 579 |
my $found_timeout = 0; |
| 580 |
for my $set ([ET_FILE, ET_BUFFER], [PET_FILE, PET_BUFFER]) { |
| 581 |
my ($key, $buffer_key) = @$set; |
| 582 |
next if $self->{$buffer_key}; |
| 583 |
next unless $self->{$key} && $self->{$key}->exists; |
| 584 |
$self->{$buffer_key} = $self->{$key}->read_line // next; |
| 585 |
$found_timeout++; |
| 586 |
} |
| 587 |
|
| 588 |
return if $found_timeout; |
| 589 |
|
| 590 |
return if $self->{+OPEN_ERRORS}; |
| 591 |
|
| 592 |
my $ended = 0; |
| 593 |
|
| 594 |
# We need to check if the runner exited BEFORE trying to check the exit value. |
| 595 |
my $runner_exited = $self->{+RUNNER_PID} && !kill(0, $self->{+RUNNER_PID}); |
| 596 |
my $exit_file = $self->{+EXIT_FILE} || $self->_open_file('exit') || return; |
| 597 |
return if $self->{+OPEN_ERRORS}; |
| 598 |
|
| 599 |
if ($exit_file->exists) { |
| 600 |
my $line = $exit_file->read_line; |
| 601 |
if (defined($line)) { |
| 602 |
$self->{+_EXIT_BUFFER} = $line; |
| 603 |
$self->{+_EXIT_DONE} = 1; |
| 604 |
$ended++; |
| 605 |
} |
| 606 |
} |
| 607 |
elsif ($runner_exited) { |
| 608 |
$self->{+_EXIT_BUFFER} = '-1'; |
| 609 |
$self->{+_EXIT_DONE} = 1; |
| 610 |
$ended++; |
| 611 |
} |
| 612 |
|
| 613 |
return unless $ended; |
| 614 |
|
| 615 |
# If we found exit we need one last buffer fill on the other sources. |
| 616 |
# If we do not do this we have a race condition. Ignore the max for this. |
| 617 |
$self->_fill_stream_buffers(); |
| 618 |
} |
| 619 |
|
| 620 |
sub _poll_timeouts { |
| 621 |
my $self = shift; |
| 622 |
|
| 623 |
my @out; |
| 624 |
|
| 625 |
if (defined $self->{+ET_BUFFER} && !$self->{+ET_DONE}++) { |
| 626 |
push @out => $self->_process_timeout_line('event' => $self->{+ET_BUFFER}, <<" EOT"); |
| 627 |
Test2::Harness checks for timeouts at a configurable interval, if a test does |
| 628 |
not produce any output to stdout or stderr between intervals it will be |
| 629 |
forcefully killed under the assumption it has hung. See the '--event-timeout' |
| 630 |
option to configure the interval. |
| 631 |
EOT |
| 632 |
} |
| 633 |
|
| 634 |
if (defined $self->{+PET_BUFFER} && !$self->{+PET_DONE}++) { |
| 635 |
push @out => $self->_process_timeout_line('post-exit' => $self->{+ET_BUFFER}, <<" EOT"); |
| 636 |
Sometimes tests will fork and then return. On supported systems Test2::Harness |
| 637 |
will start all tests with their own process group and will wait for the entire |
| 638 |
group to exit before considering the test done. In these cases Test2::Harness |
| 639 |
will poll for output from the process group at a configurable interval, if no |
| 640 |
output is produced between intervals the process group will be forcefully |
| 641 |
killed. See the '--post-exit-timeout' option to configure the interval. |
| 642 |
EOT |
| 643 |
} |
| 644 |
|
| 645 |
return @out; |
| 646 |
} |
| 647 |
|
| 648 |
sub _poll_exit { |
| 649 |
my $self = shift; |
| 650 |
# Intentionally ignoring the max argument, this only ever returns 1 item, |
| 651 |
# and would not be called if max was 0. |
| 652 |
|
| 653 |
return unless defined $self->{+_EXIT_BUFFER}; |
| 654 |
my $value = delete $self->{+_EXIT_BUFFER}; |
| 655 |
|
| 656 |
return $self->_process_exit_line($value); |
| 657 |
} |
| 658 |
|
| 659 |
sub _process_events_line { |
| 660 |
my $self = shift; |
| 661 |
my ($event_data) = @_; |
| 662 |
|
| 663 |
$event_data->{job_id} = $self->{+JOB_ID}; |
| 664 |
$event_data->{job_try} = $self->{+JOB_TRY}; |
| 665 |
$event_data->{run_id} = $self->{+RUN_ID}; |
| 666 |
$event_data->{event_id} ||= $event_data->{facet_data}->{about}->{uuid} ||= gen_uuid(); |
| 667 |
|
| 668 |
return $event_data; |
| 669 |
} |
| 670 |
|
| 671 |
sub _process_exit_line { |
| 672 |
my $self = shift; |
| 673 |
my ($value) = @_; |
| 674 |
|
| 675 |
chomp($value); |
| 676 |
|
| 677 |
my $stdout = maybe_read_file(File::Spec->catfile($self->{+JOB_ROOT}, "stdout")) // ''; |
| 678 |
my $stderr = maybe_read_file(File::Spec->catfile($self->{+JOB_ROOT}, "stderr")) // ''; |
| 679 |
|
| 680 |
$stdout =~ s/T2-HARNESS-\S+-(?:ESYNC|EVENT): .+\n//g; |
| 681 |
$stderr =~ s/T2-HARNESS-\S+-(?:ESYNC|EVENT): .+\n//g; |
| 682 |
|
| 683 |
my $event_id = gen_uuid(); |
| 684 |
|
| 685 |
my ($exit, $err, $sig, $dmp, $stamp, $retry) = (split(/\s+/, $value), '', '', '', '', '', ''); |
| 686 |
|
| 687 |
$self->{+DONE} = {retry => $retry}; |
| 688 |
|
| 689 |
return { |
| 690 |
event_id => $event_id, |
| 691 |
job_id => $self->{+JOB_ID}, |
| 692 |
job_try => $self->{+JOB_TRY}, |
| 693 |
run_id => $self->{+RUN_ID}, |
| 694 |
stamp => $stamp, |
| 695 |
|
| 696 |
facet_data => { |
| 697 |
about => {uuid => $event_id}, |
| 698 |
harness_job_exit => { |
| 699 |
details => "Test script exited $exit ($err\:$sig)", |
| 700 |
exit => $exit, |
| 701 |
code => $err, |
| 702 |
signal => $sig, |
| 703 |
dumped => $dmp, |
| 704 |
retry => $retry, |
| 705 |
job_id => $self->{+JOB_ID}, |
| 706 |
job_try => $self->{+JOB_TRY}, |
| 707 |
stdout => $stdout, |
| 708 |
stderr => $stderr, |
| 709 |
stamp => $stamp, |
| 710 |
line => $value, |
| 711 |
}, |
| 712 |
} |
| 713 |
}; |
| 714 |
} |
| 715 |
|
| 716 |
sub _process_timeout_line { |
| 717 |
my $self = shift; |
| 718 |
my ($type, $buffer, $reason) = @_; |
| 719 |
|
| 720 |
chomp($buffer //= ''); |
| 721 |
my ($stamp, $delta) = split /\s+/, $buffer; |
| 722 |
$stamp //= time(); |
| 723 |
$delta = defined($delta) ? sprintf('%.4f', $delta) : '??'; |
| 724 |
|
| 725 |
my $event_id = gen_uuid(); |
| 726 |
|
| 727 |
return { |
| 728 |
event_id => $event_id, |
| 729 |
job_id => $self->{+JOB_ID}, |
| 730 |
job_try => $self->{+JOB_TRY}, |
| 731 |
run_id => $self->{+RUN_ID}, |
| 732 |
stamp => $stamp, |
| 733 |
|
| 734 |
facet_data => { |
| 735 |
about => {uuid => $event_id, details => "Timeout ($type)"}, |
| 736 |
errors => [ |
| 737 |
{ |
| 738 |
tag => 'TIMEOUT', |
| 739 |
details => "A timeout ($type) has occured (after $delta seconds), job was forcefully killed", |
| 740 |
fail => 1, |
| 741 |
}, |
| 742 |
], |
| 743 |
info => [ |
| 744 |
{ |
| 745 |
tag => 'TIMEOUT', |
| 746 |
debug => 1, |
| 747 |
important => 1, |
| 748 |
details => $reason, |
| 749 |
}, |
| 750 |
], |
| 751 |
} |
| 752 |
}; |
| 753 |
} |
| 754 |
|
| 755 |
1; |
| 756 |
|
| 757 |
__END__ |
| 758 |
|
| 759 |
=pod |
| 760 |
|
| 761 |
=encoding UTF-8 |
| 762 |
|
| 763 |
=head1 NAME |
| 764 |
|
| 765 |
Test2::Harness::Collector::JobDir - Job Directory Parser, read events from an active |
| 766 |
jobs output directory. |
| 767 |
|
| 768 |
=head1 DESCRIPTION |
| 769 |
|
| 770 |
This module is responsible for reading and parsing a running jobs output |
| 771 |
directory. The result is an event stream. |
| 772 |
|
| 773 |
This module is not intended for external use, it is an implementation detail |
| 774 |
and can change at any time. Currently instances of this module are not passed |
| 775 |
to any plugins or callbacks. |
| 776 |
|
| 777 |
=head1 SOURCE |
| 778 |
|
| 779 |
The source code repository for Test2-Harness can be found at |
| 780 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 781 |
|
| 782 |
=head1 MAINTAINERS |
| 783 |
|
| 784 |
=over 4 |
| 785 |
|
| 786 |
=item Chad Granum E<lt>[email protected]<gt> |
| 787 |
|
| 788 |
=back |
| 789 |
|
| 790 |
=head1 AUTHORS |
| 791 |
|
| 792 |
=over 4 |
| 793 |
|
| 794 |
=item Chad Granum E<lt>[email protected]<gt> |
| 795 |
|
| 796 |
=back |
| 797 |
|
| 798 |
=head1 COPYRIGHT |
| 799 |
|
| 800 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 801 |
|
| 802 |
This program is free software; you can redistribute it and/or |
| 803 |
modify it under the same terms as Perl itself. |
| 804 |
|
| 805 |
See F<http://dev.perl.org/licenses/> |
| 806 |
|
| 807 |
=cut |