| 1 |
package Test2::Harness::Runner::State; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/croak/; |
| 8 |
|
| 9 |
use File::Spec; |
| 10 |
use Time::HiRes qw/time/; |
| 11 |
use List::Util qw/first/; |
| 12 |
|
| 13 |
use Test2::Harness::Util qw/mod2file/; |
| 14 |
|
| 15 |
use Test2::Harness::Settings; |
| 16 |
use Test2::Harness::Runner::Constants; |
| 17 |
|
| 18 |
use Test2::Harness::Runner::Run; |
| 19 |
use Test2::Harness::Util::Queue; |
| 20 |
|
| 21 |
use Test2::Harness::Util::UUID qw/gen_uuid/; |
| 22 |
|
| 23 |
use Test2::Harness::Util::HashBase( |
| 24 |
# These are construction arguments |
| 25 |
qw{ |
| 26 |
<eager_stages |
| 27 |
<workdir |
| 28 |
<preloader |
| 29 |
<no_poll |
| 30 |
<resources |
| 31 |
job_count |
| 32 |
+settings |
| 33 |
}, |
| 34 |
|
| 35 |
qw{ |
| 36 |
<dispatch_file |
| 37 |
<queue_ended |
| 38 |
|
| 39 |
<pending_tasks <task_lookup |
| 40 |
<pending_runs +run <stopped_runs |
| 41 |
<pending_spawns |
| 42 |
|
| 43 |
<running |
| 44 |
<running_categories |
| 45 |
<running_durations |
| 46 |
<running_conflicts |
| 47 |
<running_shares |
| 48 |
<running_tasks |
| 49 |
|
| 50 |
<stage_readiness |
| 51 |
|
| 52 |
<task_list |
| 53 |
|
| 54 |
<halted_runs |
| 55 |
|
| 56 |
<reload_state |
| 57 |
|
| 58 |
<observe |
| 59 |
|
| 60 |
<last_job_activity |
| 61 |
<total_started |
| 62 |
}, |
| 63 |
); |
| 64 |
|
| 65 |
sub init { |
| 66 |
my $self = shift; |
| 67 |
|
| 68 |
croak "You must specify a workdir" |
| 69 |
unless defined $self->{+WORKDIR}; |
| 70 |
|
| 71 |
$self->{+JOB_COUNT} //= $self->settings->runner->job_count // 1; |
| 72 |
|
| 73 |
if (!$self->{+RESOURCES} || !@{$self->{+RESOURCES}}) { |
| 74 |
my $settings = $self->settings; |
| 75 |
my $resources = $self->{+RESOURCES} //= []; |
| 76 |
for my $res (@{$self->settings->runner->resources}) { |
| 77 |
require(mod2file($res)); |
| 78 |
push @$resources => $res->new(settings => $self->settings, observe => $self->{+OBSERVE}); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
unless (grep { $_->job_limiter } @{$self->{+RESOURCES}}) { |
| 83 |
require Test2::Harness::Runner::Resource::JobCount; |
| 84 |
push @{$self->{+RESOURCES}} => Test2::Harness::Runner::Resource::JobCount->new(job_count => $self->{+JOB_COUNT}, settings => $self->settings); |
| 85 |
} |
| 86 |
|
| 87 |
@{$self->{+RESOURCES}} = sort { $a->sort_weight <=> $b->sort_weight } @{$self->{+RESOURCES}}; |
| 88 |
|
| 89 |
$self->{+DISPATCH_FILE} = Test2::Harness::Util::Queue->new(file => File::Spec->catfile($self->{+WORKDIR}, 'dispatch.jsonl')); |
| 90 |
|
| 91 |
$self->{+RELOAD_STATE} //= {}; |
| 92 |
|
| 93 |
$self->poll; |
| 94 |
} |
| 95 |
|
| 96 |
sub settings { |
| 97 |
my $self = shift; |
| 98 |
return $self->{+SETTINGS} //= Test2::Harness::Settings->new(File::Spec->catfile($self->{+WORKDIR}, 'settings.json')); |
| 99 |
} |
| 100 |
|
| 101 |
sub run { |
| 102 |
my $self = shift; |
| 103 |
return $self->{+RUN} if $self->{+RUN}; |
| 104 |
$self->poll(); |
| 105 |
return $self->{+RUN}; |
| 106 |
} |
| 107 |
|
| 108 |
sub done { |
| 109 |
my $self = shift; |
| 110 |
|
| 111 |
$self->poll(); |
| 112 |
|
| 113 |
return 0 if $self->{+RUNNING}; |
| 114 |
return 0 if keys %{$self->{+PENDING_TASKS} //= {}}; |
| 115 |
|
| 116 |
return 0 if $self->{+RUN}; |
| 117 |
return 0 if @{$self->{+PENDING_RUNS} //= []}; |
| 118 |
|
| 119 |
return 0 unless $self->{+QUEUE_ENDED}; |
| 120 |
|
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
sub resource_timeout { |
| 125 |
my $self = shift; |
| 126 |
my ($timeout) = @_; |
| 127 |
|
| 128 |
return 0 unless $timeout; |
| 129 |
|
| 130 |
# Must have started at least one test already |
| 131 |
return 0 unless $self->{+TOTAL_STARTED}; |
| 132 |
|
| 133 |
# Must have no tests currently running |
| 134 |
return 0 if $self->{+RUNNING}; |
| 135 |
|
| 136 |
# Must still have pending tasks |
| 137 |
return 0 unless keys %{$self->{+PENDING_TASKS} //= {}}; |
| 138 |
|
| 139 |
my $idle = time - ($self->{+LAST_JOB_ACTIVITY} // time); |
| 140 |
|
| 141 |
return 0 unless $idle >= $timeout; |
| 142 |
|
| 143 |
return $idle; |
| 144 |
} |
| 145 |
|
| 146 |
sub next_task { |
| 147 |
my $self = shift; |
| 148 |
my ($stage) = @_; |
| 149 |
|
| 150 |
$self->poll(); |
| 151 |
$self->clear_finished_run(); |
| 152 |
|
| 153 |
while(1) { |
| 154 |
if (@{$self->{+PENDING_SPAWNS} //= []}) { |
| 155 |
my $spawn = shift @{$self->{+PENDING_SPAWNS}}; |
| 156 |
next unless $spawn->{stage} eq $stage; |
| 157 |
$self->start_spawn($spawn); |
| 158 |
return $spawn; |
| 159 |
} |
| 160 |
|
| 161 |
my $task = shift @{$self->{+TASK_LIST}} or return undef; |
| 162 |
|
| 163 |
# If we are replaying a state then the task may have already completed, |
| 164 |
# so skip it if it is not in the running lookup. |
| 165 |
next unless $self->{+RUNNING_TASKS}->{$task->{job_id}}; |
| 166 |
next unless $task->{stage} eq $stage; |
| 167 |
|
| 168 |
return $task; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
sub advance { |
| 173 |
my $self = shift; |
| 174 |
$self->poll(); |
| 175 |
|
| 176 |
$_->tick() for @{$self->{+RESOURCES} //= []}; |
| 177 |
|
| 178 |
$self->advance_run(); |
| 179 |
return 0 unless $self->{+RUN}; |
| 180 |
return 1 if $self->advance_tasks(); |
| 181 |
return $self->clear_finished_run(); |
| 182 |
} |
| 183 |
|
| 184 |
my %ACTIONS = ( |
| 185 |
queue_run => '_queue_run', |
| 186 |
queue_task => '_queue_task', |
| 187 |
queue_spawn => '_queue_spawn', |
| 188 |
start_spawn => '_start_spawn', |
| 189 |
start_run => '_start_run', |
| 190 |
start_task => '_start_task', |
| 191 |
stop_run => '_stop_run', |
| 192 |
stop_task => '_stop_task', |
| 193 |
retry_task => '_retry_task', |
| 194 |
stage_ready => '_stage_ready', |
| 195 |
stage_down => '_stage_down', |
| 196 |
end_queue => '_end_queue', |
| 197 |
halt_run => '_halt_run', |
| 198 |
truncate => '_truncate', |
| 199 |
reload => '_reload', |
| 200 |
); |
| 201 |
|
| 202 |
sub poll { |
| 203 |
my $self = shift; |
| 204 |
|
| 205 |
return if $self->{+NO_POLL}; |
| 206 |
|
| 207 |
my $queue = $self->dispatch_file; |
| 208 |
|
| 209 |
for my $item ($queue->poll) { |
| 210 |
my $data = $item->[-1]; |
| 211 |
my $item = $data->{item}; |
| 212 |
my $action = $data->{action}; |
| 213 |
my $pid = $data->{pid}; |
| 214 |
|
| 215 |
my $sub = $ACTIONS{$action} or die "Invalid action '$action'"; |
| 216 |
|
| 217 |
$self->$sub($item, $pid); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
sub _enqueue { |
| 222 |
my $self = shift; |
| 223 |
my ($action, $item) = @_; |
| 224 |
$self->{+DISPATCH_FILE}->enqueue({action => $action, item => $item, stamp => time, pid => $$}); |
| 225 |
$self->poll; |
| 226 |
} |
| 227 |
|
| 228 |
sub truncate { |
| 229 |
my $self = shift; |
| 230 |
$self->halt_run($_) for keys %{$self->{+PENDING_TASKS} // {}}; |
| 231 |
$self->_enqueue(truncate => $$); |
| 232 |
$self->poll; |
| 233 |
} |
| 234 |
|
| 235 |
sub _truncate { } |
| 236 |
|
| 237 |
sub end_queue { $_[0]->_enqueue('end_queue' => 1) } |
| 238 |
sub _end_queue { $_[0]->{+QUEUE_ENDED} = 1 } |
| 239 |
|
| 240 |
sub halt_run { |
| 241 |
my $self = shift; |
| 242 |
my ($run_id) = @_; |
| 243 |
$self->_enqueue(halt_run => $run_id); |
| 244 |
|
| 245 |
my $dir = File::Spec->catdir($self->{+WORKDIR}, $run_id); |
| 246 |
my $file = File::Spec->catfile($dir, 'jobs.jsonl'); |
| 247 |
|
| 248 |
if (-f $file) { |
| 249 |
my $queue = Test2::Harness::Util::Queue->new(file => File::Spec->catfile($dir, 'jobs.jsonl')); |
| 250 |
$queue->end; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
sub _halt_run { |
| 255 |
my $self = shift; |
| 256 |
my ($run_id) = @_; |
| 257 |
|
| 258 |
delete $self->{+PENDING_TASKS}->{$run_id}; |
| 259 |
|
| 260 |
$self->{+HALTED_RUNS}->{$run_id}++; |
| 261 |
} |
| 262 |
|
| 263 |
sub queue_run { |
| 264 |
my $self = shift; |
| 265 |
my ($run) = @_; |
| 266 |
$self->_enqueue(queue_run => $run); |
| 267 |
} |
| 268 |
|
| 269 |
sub _queue_run { |
| 270 |
my $self = shift; |
| 271 |
my ($run) = @_; |
| 272 |
|
| 273 |
push @{$self->{+PENDING_RUNS}} => Test2::Harness::Runner::Run->new( |
| 274 |
%$run, |
| 275 |
workdir => $self->{+WORKDIR}, |
| 276 |
); |
| 277 |
|
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
sub start_run { |
| 282 |
my $self = shift; |
| 283 |
my ($run_id) = @_; |
| 284 |
$self->_enqueue(start_run => $run_id); |
| 285 |
} |
| 286 |
|
| 287 |
sub _start_run { |
| 288 |
my $self = shift; |
| 289 |
my ($run_id) = @_; |
| 290 |
|
| 291 |
my $run = shift @{$self->{+PENDING_RUNS}}; |
| 292 |
die "$0 - Run stack mismatch, run start requested, but no pending runs to start" unless $run; |
| 293 |
die "$0 - Run stack mismatch, run-id does not match next pending run" unless $run->run_id eq $run_id; |
| 294 |
|
| 295 |
$self->{+RUN} = $run; |
| 296 |
|
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
sub stop_run { |
| 301 |
my $self = shift; |
| 302 |
my ($run_id) = @_; |
| 303 |
$self->_enqueue(stop_run => $run_id); |
| 304 |
} |
| 305 |
|
| 306 |
sub _stop_run { |
| 307 |
my $self = shift; |
| 308 |
my ($run_id) = @_; |
| 309 |
|
| 310 |
$self->{+STOPPED_RUNS}->{$run_id} = 1; |
| 311 |
|
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
sub queue_spawn { |
| 316 |
my $self = shift; |
| 317 |
my ($spawn) = @_; |
| 318 |
$spawn->{spawn} //= 1; |
| 319 |
$spawn->{id} //= gen_uuid(); |
| 320 |
$self->_enqueue(queue_spawn => $spawn); |
| 321 |
} |
| 322 |
|
| 323 |
sub _queue_spawn { |
| 324 |
my $self = shift; |
| 325 |
my ($spawn) = @_; |
| 326 |
|
| 327 |
$spawn->{id} //= gen_uuid(); |
| 328 |
$spawn->{spawn} //= 1; |
| 329 |
$spawn->{use_preload} //= 1; |
| 330 |
|
| 331 |
$spawn->{stage} //= 'default'; |
| 332 |
$spawn->{stage} = $self->task_stage($spawn); |
| 333 |
|
| 334 |
push @{$self->{+PENDING_SPAWNS}} => $spawn; |
| 335 |
|
| 336 |
return; |
| 337 |
} |
| 338 |
|
| 339 |
sub start_spawn { |
| 340 |
my $self = shift; |
| 341 |
my ($spec) = @_; |
| 342 |
$self->_enqueue(start_spawn => $spec); |
| 343 |
} |
| 344 |
|
| 345 |
sub _start_spawn { |
| 346 |
my $self = shift; |
| 347 |
my ($spec) = @_; |
| 348 |
|
| 349 |
my $uuid = $spec->{id} or die "Could not find UUID for spawn"; |
| 350 |
|
| 351 |
@{$self->{+PENDING_SPAWNS}} = grep { $_->{id} ne $uuid } @{$self->{+PENDING_SPAWNS}}; |
| 352 |
|
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
sub queue_task { |
| 357 |
my $self = shift; |
| 358 |
my ($task) = @_; |
| 359 |
$self->_enqueue(queue_task => $task); |
| 360 |
} |
| 361 |
|
| 362 |
sub _queue_task { |
| 363 |
my $self = shift; |
| 364 |
my ($task) = @_; |
| 365 |
|
| 366 |
my $job_id = $task->{job_id} or die "Task missing job_id"; |
| 367 |
my $run_id = $task->{run_id} or die "Task missing run_id"; |
| 368 |
|
| 369 |
die "Task already in queue" if $self->{+TASK_LOOKUP}->{$job_id}; |
| 370 |
|
| 371 |
return if $self->{+HALTED_RUNS}->{$run_id}; |
| 372 |
|
| 373 |
$self->{+TASK_LOOKUP}->{$job_id} = $task; |
| 374 |
|
| 375 |
my $pending = $self->task_pending_lookup($task); |
| 376 |
push @{$pending} => $task; |
| 377 |
|
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
sub start_task { |
| 382 |
my $self = shift; |
| 383 |
my ($spec) = @_; |
| 384 |
$self->_enqueue(start_task => $spec); |
| 385 |
} |
| 386 |
|
| 387 |
sub _start_task { |
| 388 |
my $self = shift; |
| 389 |
my ($spec) = @_; |
| 390 |
|
| 391 |
my $job_id = $spec->{job_id} or die "No job_id provided"; |
| 392 |
my $run_stage = $spec->{stage} or die "No stage provided"; |
| 393 |
my $res = $spec->{res} or die "No res provided"; |
| 394 |
my $res_skip = $spec->{resource_skip}; |
| 395 |
|
| 396 |
my $task = $self->{+TASK_LOOKUP}->{$job_id} or die "Could not find task to start"; |
| 397 |
|
| 398 |
my ($run_id, $smoke, $stage, $cat, $dur) = $self->task_fields($task); |
| 399 |
|
| 400 |
my $set = $self->{+PENDING_TASKS}->{$run_id}->{$smoke}->{$stage}->{$cat}->{$dur}; |
| 401 |
my $count = @$set; |
| 402 |
@$set = grep { $_->{job_id} ne $job_id } @$set; |
| 403 |
die "Task $job_id was not pending ($count -> " . scalar(@$set) . ")" unless $count > @$set; |
| 404 |
|
| 405 |
$self->prune_hash($self->{+PENDING_TASKS}, $run_id, $smoke, $stage, $cat, $dur); |
| 406 |
|
| 407 |
# Set the stage, new task hashref |
| 408 |
$task = {%$task, stage => $run_stage} unless $task->{stage} && $task->{stage} eq $run_stage; |
| 409 |
|
| 410 |
$task->{env_vars}->{$_} = $res->{env_vars}->{$_} for keys %{$res->{env_vars}}; |
| 411 |
push @{$task->{test_args}} => @{$res->{args}}; |
| 412 |
|
| 413 |
for my $resource (@{$self->{+RESOURCES}}) { |
| 414 |
my $class = ref($resource); |
| 415 |
my $val = $res->{record}->{$class} // next; |
| 416 |
$resource->record($task->{job_id}, $val); |
| 417 |
} |
| 418 |
|
| 419 |
die "Already running task $job_id" if $self->{+RUNNING_TASKS}->{$job_id}; |
| 420 |
$self->{+RUNNING_TASKS}->{$job_id} = $task; |
| 421 |
|
| 422 |
$task->{resource_skip} = $res_skip if $res_skip; |
| 423 |
|
| 424 |
push @{$self->{+TASK_LIST}} => $task; |
| 425 |
|
| 426 |
$self->{+LAST_JOB_ACTIVITY} = time; |
| 427 |
$self->{+TOTAL_STARTED}++; |
| 428 |
$self->{+RUNNING}++; |
| 429 |
$self->{+RUNNING_CATEGORIES}->{$cat}++; |
| 430 |
$self->{+RUNNING_DURATIONS}->{$dur}++; |
| 431 |
|
| 432 |
my ($cfls, $shrs) = $self->task_locks($task); |
| 433 |
|
| 434 |
for my $cfl (@$cfls) { |
| 435 |
die "Unexpected shared use of '$cfl' ($self->{+RUNNING_SHARES}->{$cfl}) running at this time!" |
| 436 |
if $self->{+RUNNING_SHARES}->{$cfl}; |
| 437 |
|
| 438 |
die "Unexpected parallel conflict '$cfl' ($self->{+RUNNING_CONFLICTS}->{$cfl}) running at this time!" |
| 439 |
if $self->{+RUNNING_CONFLICTS}->{$cfl}++; |
| 440 |
} |
| 441 |
|
| 442 |
for my $shr (@$shrs) { |
| 443 |
die "Unexpected exclusive conflict '$shr' running at this time!" |
| 444 |
if $self->{+RUNNING_CONFLICTS}->{$shr}; |
| 445 |
|
| 446 |
$self->{+RUNNING_SHARES}->{$shr}++; |
| 447 |
} |
| 448 |
|
| 449 |
return; |
| 450 |
} |
| 451 |
|
| 452 |
sub stop_task { |
| 453 |
my $self = shift; |
| 454 |
my ($job_id) = @_; |
| 455 |
$self->_enqueue(stop_task => $job_id); |
| 456 |
} |
| 457 |
|
| 458 |
sub _stop_task { |
| 459 |
my $self = shift; |
| 460 |
my ($job_id) = @_; |
| 461 |
|
| 462 |
my $task = delete $self->{+TASK_LOOKUP}->{$job_id} or die "Could not find task to stop ($job_id)"; |
| 463 |
|
| 464 |
delete $self->{+RUNNING_TASKS}->{$job_id} or die "Task is not running, cannot stop it ($job_id)"; |
| 465 |
|
| 466 |
$_->release($job_id) for @{$self->{+RESOURCES}}; |
| 467 |
|
| 468 |
my ($run_id, $smoke, $stage, $cat, $dur) = $self->task_fields($task); |
| 469 |
$self->{+LAST_JOB_ACTIVITY} = time; |
| 470 |
$self->{+RUNNING}--; |
| 471 |
$self->{+RUNNING_CATEGORIES}->{$cat}--; |
| 472 |
$self->{+RUNNING_DURATIONS}->{$dur}--; |
| 473 |
|
| 474 |
my ($cfls, $shrs) = $self->task_locks($task); |
| 475 |
$self->{+RUNNING_CONFLICTS}->{$_}-- for @$cfls; |
| 476 |
$self->{+RUNNING_SHARES}->{$_}-- for @$shrs; |
| 477 |
|
| 478 |
return; |
| 479 |
} |
| 480 |
|
| 481 |
sub retry_task { |
| 482 |
my $self = shift; |
| 483 |
my ($job_id) = @_; |
| 484 |
|
| 485 |
$self->_enqueue(retry_task => $job_id); |
| 486 |
} |
| 487 |
|
| 488 |
sub _retry_task { |
| 489 |
my $self = shift; |
| 490 |
my ($job_id) = @_; |
| 491 |
|
| 492 |
my $task = $self->{+TASK_LOOKUP}->{$job_id} or die "Could not find task to retry"; |
| 493 |
|
| 494 |
$self->_stop_task($job_id); |
| 495 |
|
| 496 |
return if $self->{+HALTED_RUNS}->{$task->{run_id}}; |
| 497 |
|
| 498 |
$task = {is_try => 0, %$task}; |
| 499 |
$task->{is_try}++; |
| 500 |
$task->{category} = 'isolation' if $self->{+RUN}->retry_isolated; |
| 501 |
|
| 502 |
$self->_queue_task($task); |
| 503 |
|
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
sub stage_ready { |
| 508 |
my $self = shift; |
| 509 |
my ($stage) = @_; |
| 510 |
$self->_enqueue(stage_ready => $stage); |
| 511 |
} |
| 512 |
|
| 513 |
sub _stage_ready { |
| 514 |
my $self = shift; |
| 515 |
my ($stage, $pid) = @_; |
| 516 |
|
| 517 |
$self->{+STAGE_READINESS}->{$stage} = $pid // 1; |
| 518 |
|
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
sub stage_down { |
| 523 |
my $self = shift; |
| 524 |
my ($stage) = @_; |
| 525 |
$self->_enqueue(stage_down => $stage); |
| 526 |
} |
| 527 |
|
| 528 |
sub _stage_down { |
| 529 |
my $self = shift; |
| 530 |
my ($stage) = @_; |
| 531 |
|
| 532 |
$self->{+STAGE_READINESS}->{$stage} = 0; |
| 533 |
|
| 534 |
return; |
| 535 |
} |
| 536 |
|
| 537 |
sub reload { |
| 538 |
my $self = shift; |
| 539 |
my ($stage, $data) = @_; |
| 540 |
$stage //= 'default'; |
| 541 |
$self->_enqueue(reload => {%$data, stage => $stage}); |
| 542 |
return; |
| 543 |
} |
| 544 |
|
| 545 |
sub _reload { |
| 546 |
my $self = shift; |
| 547 |
my ($data) = @_; |
| 548 |
|
| 549 |
my $stage = $data->{stage}; |
| 550 |
my $file = $data->{file}; |
| 551 |
my $success = $data->{reloaded}; |
| 552 |
my $error = $data->{error}; |
| 553 |
my $warnings = $data->{warnings}; |
| 554 |
|
| 555 |
my $reload_state = $self->{+RELOAD_STATE} //= {}; |
| 556 |
my $stage_state = $reload_state->{$stage} //= {}; |
| 557 |
|
| 558 |
# It either succeeded, or the stage will be reloaded, no need to track brokenness |
| 559 |
if (defined $success) { |
| 560 |
delete $stage_state->{$file}; |
| 561 |
} |
| 562 |
else { |
| 563 |
my $fields = {}; |
| 564 |
$fields->{error} = $error if defined($error) && length($error); |
| 565 |
$fields->{warnings} = $warnings if $warnings && @{$warnings}; |
| 566 |
|
| 567 |
if (keys %$fields) { |
| 568 |
$stage_state->{$file} = $fields; |
| 569 |
} |
| 570 |
else { |
| 571 |
delete $stage_state->{$file}; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
return; |
| 576 |
} |
| 577 |
|
| 578 |
sub task_stage { |
| 579 |
my $self = shift; |
| 580 |
my ($task) = @_; |
| 581 |
|
| 582 |
my $wants = $task->{stage}; |
| 583 |
$wants //= 'NOPRELOAD' unless $task->{use_preload}; |
| 584 |
|
| 585 |
return $wants if $self->{+NO_POLL}; |
| 586 |
|
| 587 |
return $wants // 'DEFAULT' unless $self->preloader; |
| 588 |
return $self->preloader->task_stage($task->{file}, $wants); |
| 589 |
} |
| 590 |
|
| 591 |
sub task_locks { |
| 592 |
my $self = shift; |
| 593 |
my ($task) = @_; |
| 594 |
|
| 595 |
my $conflicts = $task->{conflicts} //= []; |
| 596 |
my $shares = $task->{shares} //= []; |
| 597 |
|
| 598 |
# An exclusive claim on a name gazumps a shared one, so a task holding both |
| 599 |
# must not be counted as waiting on itself. The scanner already drops |
| 600 |
# these, but tasks can also be queued by hand, where the scanner will not |
| 601 |
# have been able to fix it up. |
| 602 |
my %exclusive = map { ($_ => 1) } @$conflicts; |
| 603 |
|
| 604 |
return ($conflicts, [grep { !$exclusive{$_} } @$shares]); |
| 605 |
} |
| 606 |
|
| 607 |
sub task_pending_lookup { |
| 608 |
my $self = shift; |
| 609 |
my ($task) = @_; |
| 610 |
|
| 611 |
my ($run_id, $smoke, $stage, $cat, $dur) = $self->task_fields($task); |
| 612 |
|
| 613 |
return $self->{+PENDING_TASKS}->{$run_id}->{$smoke}->{$stage}->{$cat}->{$dur} //= []; |
| 614 |
} |
| 615 |
|
| 616 |
sub task_fields { |
| 617 |
my $self = shift; |
| 618 |
my ($task) = @_; |
| 619 |
|
| 620 |
my $run_id = $task->{run_id} or die "No run id provided by task"; |
| 621 |
my $smoke = $task->{smoke} ? 'smoke' : 'main'; |
| 622 |
my $stage = $self->task_stage($task); |
| 623 |
|
| 624 |
my $cat = $task->{category}; |
| 625 |
my $dur = $task->{duration}; |
| 626 |
|
| 627 |
die "Invalid category: $cat" unless CATEGORIES->{$cat}; |
| 628 |
die "Invalid duration: $dur" unless DURATIONS->{$dur}; |
| 629 |
|
| 630 |
$cat = 'conflicts' if $cat eq 'general' && $task->{conflicts} && @{$task->{conflicts}}; |
| 631 |
|
| 632 |
# Note that "shares" deliberately does NOT promote a task into the |
| 633 |
# "conflicts" bucket. That bucket is searched ahead of "general", which is |
| 634 |
# what lets an exclusive task claim a name before a shared one takes it, |
| 635 |
# which would makes the exclusive task wait. |
| 636 |
|
| 637 |
return ($run_id, $smoke, $stage, $cat, $dur); |
| 638 |
} |
| 639 |
|
| 640 |
sub prune_hash { |
| 641 |
my $self = shift; |
| 642 |
my ($hash, @path) = @_; |
| 643 |
|
| 644 |
die "No path!" unless @path; |
| 645 |
|
| 646 |
my $key = shift @path; |
| 647 |
|
| 648 |
if (@path) { |
| 649 |
my $empty = $self->prune_hash($hash->{$key}, @path); |
| 650 |
return 0 unless $empty; |
| 651 |
} |
| 652 |
|
| 653 |
return 1 unless exists $hash->{$key}; |
| 654 |
|
| 655 |
my $ref = ref($hash->{$key}); |
| 656 |
if ($ref eq 'HASH') { |
| 657 |
return 0 if keys %{$hash->{$key}}; |
| 658 |
} |
| 659 |
elsif ($ref eq 'ARRAY') { |
| 660 |
return 0 if @{$hash->{$key}}; |
| 661 |
} |
| 662 |
|
| 663 |
delete $hash->{$key}; |
| 664 |
return 1; |
| 665 |
} |
| 666 |
|
| 667 |
sub advance_run { |
| 668 |
my $self = shift; |
| 669 |
|
| 670 |
return 0 if $self->{+RUN}; |
| 671 |
|
| 672 |
return 0 unless @{$self->{+PENDING_RUNS} //= []}; |
| 673 |
$self->start_run($self->{+PENDING_RUNS}->[0]->run_id); |
| 674 |
|
| 675 |
return 1; |
| 676 |
} |
| 677 |
|
| 678 |
sub clear_finished_run { |
| 679 |
my $self = shift; |
| 680 |
|
| 681 |
my $run = $self->{+RUN} or return 0; |
| 682 |
|
| 683 |
return 0 unless $self->{+STOPPED_RUNS}->{$run->run_id}; |
| 684 |
return 0 if $self->{+PENDING_TASKS}->{$run->run_id}; |
| 685 |
return 0 if $self->{+RUNNING}; |
| 686 |
|
| 687 |
delete $self->{+RUN}; |
| 688 |
|
| 689 |
return 1; |
| 690 |
} |
| 691 |
|
| 692 |
sub advance_tasks { |
| 693 |
my $self = shift; |
| 694 |
|
| 695 |
for my $resource (@{$self->{+RESOURCES}}) { |
| 696 |
$resource->refresh(); |
| 697 |
|
| 698 |
next unless $resource->job_limiter; |
| 699 |
return 0 if $resource->job_limiter_at_max(); |
| 700 |
} |
| 701 |
|
| 702 |
my ($run_stage, $task, $res, %params) = $self->_next(); |
| 703 |
|
| 704 |
my $out = 0; |
| 705 |
if ($task) { |
| 706 |
$out = 1; |
| 707 |
$self->start_task({job_id => $task->{job_id}, stage => $run_stage, res => $res, %params}); |
| 708 |
} |
| 709 |
|
| 710 |
$_->discharge() for @{$self->{+RESOURCES}}; |
| 711 |
|
| 712 |
return $out; |
| 713 |
} |
| 714 |
|
| 715 |
sub _cat_order { |
| 716 |
my $self = shift; |
| 717 |
|
| 718 |
my @cat_order = ('conflicts', 'general'); |
| 719 |
|
| 720 |
# Only search immiscible if we have no immiscible running |
| 721 |
# put them first if no others are running so we can churn through them |
| 722 |
# early instead of waiting for them to run 1 at a time at the end. |
| 723 |
unshift @cat_order => 'immiscible' unless $self->{+RUNNING_CATEGORIES}->{immiscible}; |
| 724 |
|
| 725 |
# Only search isolation if nothing is running. |
| 726 |
push @cat_order => 'isolation' unless $self->{+RUNNING}; |
| 727 |
|
| 728 |
return \@cat_order; |
| 729 |
} |
| 730 |
|
| 731 |
sub _dur_order { |
| 732 |
my $self = shift; |
| 733 |
|
| 734 |
my $max = 0; |
| 735 |
for my $resource (@{$self->resources}) { |
| 736 |
next unless $resource->job_limiter; |
| 737 |
my $val = $resource->job_limiter_max; |
| 738 |
$max = $val if !$max || $val < $max; |
| 739 |
} |
| 740 |
$max //= 1; |
| 741 |
|
| 742 |
my $maxm1 = $max - 1; |
| 743 |
|
| 744 |
my $durs = $self->{+RUNNING_DURATIONS}; |
| 745 |
|
| 746 |
# 'short' is always ok. |
| 747 |
my @dur_order = ('short'); |
| 748 |
|
| 749 |
# long and medium should be on the front of the search unless we are |
| 750 |
# already running (max - 1) tests of the duration We want long first if |
| 751 |
# we are not saturation on them, followed by medium, whcih is why they |
| 752 |
# are listed in this order. |
| 753 |
for my $c (qw/medium long/) { |
| 754 |
if ($durs->{$c} && $durs->{$c} >= $maxm1) { |
| 755 |
push @dur_order => $c; # Back of the list |
| 756 |
} |
| 757 |
else { |
| 758 |
unshift @dur_order => $c; # Front of the list |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
return \@dur_order; |
| 763 |
} |
| 764 |
|
| 765 |
# This returns a list of [STAGE => RUN_STAGE] pairs. 'STAGE' is the stage in |
| 766 |
# which we search for tasks, 'RUN_STAGE' is the stage that actually does the |
| 767 |
# work. This is what allows us to find tasks for 'eager' stages that are bored. |
| 768 |
sub _stage_order { |
| 769 |
my $self = shift; |
| 770 |
|
| 771 |
my $stage_check = $self->{+STAGE_READINESS} //= {}; |
| 772 |
|
| 773 |
my @stage_list = sort grep { $stage_check->{$_} } keys %$stage_check; |
| 774 |
|
| 775 |
# Populate list with all ready stages |
| 776 |
my %seen; |
| 777 |
my @stages = map {[$_ => $_]} grep { !$seen{$_}++ } @stage_list; |
| 778 |
|
| 779 |
# Add in any eager stages, but make sure they are last. |
| 780 |
for my $rstage (@stage_list) { |
| 781 |
next unless exists $self->{+EAGER_STAGES}->{$rstage}; |
| 782 |
push @stages => map {[$_ => $rstage]} grep { !$seen{$_}++ } @{$self->{+EAGER_STAGES}->{$rstage}}; |
| 783 |
} |
| 784 |
|
| 785 |
return \@stages; |
| 786 |
} |
| 787 |
|
| 788 |
my %SORTED; |
| 789 |
sub _next { |
| 790 |
my $self = shift; |
| 791 |
|
| 792 |
my $run = $self->{+RUN} or return; |
| 793 |
my $run_id = $run->run_id; |
| 794 |
|
| 795 |
my $pending = $self->{+PENDING_TASKS}->{$run_id} or return; |
| 796 |
|
| 797 |
my $conflicts = $self->{+RUNNING_CONFLICTS}; |
| 798 |
my $shares = $self->{+RUNNING_SHARES}; |
| 799 |
my $cat_order = $self->_cat_order; |
| 800 |
my $dur_order = $self->_dur_order; |
| 801 |
my $stages = $self->_stage_order(); |
| 802 |
my $resources = $self->{+RESOURCES}; |
| 803 |
|
| 804 |
# Ugly.... |
| 805 |
my $search = $pending; |
| 806 |
|
| 807 |
for my $smoke (qw/smoke main/) { |
| 808 |
my $search = $search->{$smoke} or next; |
| 809 |
|
| 810 |
for my $stage_set (@$stages) { |
| 811 |
my ($lstage, $run_by_stage) = @$stage_set; |
| 812 |
my $search = $search->{$lstage} or next; |
| 813 |
|
| 814 |
for my $lcat (@$cat_order) { |
| 815 |
my $search = $search->{$lcat} or next; |
| 816 |
|
| 817 |
for my $ldur (@$dur_order) { |
| 818 |
my $search = $search->{$ldur} or next; |
| 819 |
|
| 820 |
# Make sure anything with conflicts runs early, then |
| 821 |
# anything with shares, which an exclusive task may be |
| 822 |
# waiting on. |
| 823 |
unless ($SORTED{$search}++) { |
| 824 |
@$search = sort { |
| 825 |
scalar(@{$b->{conflicts} // []}) <=> scalar(@{$a->{conflicts} // []}) |
| 826 |
|| scalar(@{$b->{shares} // []}) <=> scalar(@{$a->{shares} // []}) |
| 827 |
} @$search; |
| 828 |
} |
| 829 |
|
| 830 |
for my $task (@$search) { |
| 831 |
# An exclusive claim on a name cannot be made while any |
| 832 |
# other job holds that name, exclusively or shared. |
| 833 |
next if first { $conflicts->{$_} || $shares->{$_} } @{$task->{conflicts} // []}; |
| 834 |
|
| 835 |
# A shared claim only has to wait out an exclusive holder. |
| 836 |
next if first { $conflicts->{$_} } @{$task->{shares} // []}; |
| 837 |
|
| 838 |
my $ok = 1; |
| 839 |
my @resource_skip; |
| 840 |
for my $resource (@$resources) { |
| 841 |
my $out = $resource->available($task) || 0; # normalize false to 0 |
| 842 |
|
| 843 |
push @resource_skip => ref($resource) || $resource if $out < 0; |
| 844 |
|
| 845 |
$ok &&= $out; |
| 846 |
|
| 847 |
# If we have a temporarily unavailable resource we |
| 848 |
# skip, but if any resource is never avilable |
| 849 |
# (skip) we want to finish the loop to add them all |
| 850 |
# for the skip message. |
| 851 |
last if !$ok && !@resource_skip; |
| 852 |
} |
| 853 |
|
| 854 |
# Some resource is temporarily not available |
| 855 |
next unless $ok; |
| 856 |
|
| 857 |
my $outres = {args => [], env_vars => {}, record => {}}; |
| 858 |
|
| 859 |
my @out = ($run_by_stage => $task, $outres); |
| 860 |
|
| 861 |
my @record = @$resources; |
| 862 |
|
| 863 |
if (@resource_skip) { |
| 864 |
push @out => (resource_skip => \@resource_skip); |
| 865 |
|
| 866 |
# Only the job limiter resources need to be recorded. |
| 867 |
@record = grep { $_->job_limiter } @record; |
| 868 |
} |
| 869 |
|
| 870 |
for my $resource (@record) { |
| 871 |
my $res = {args => [], env_vars => {}}; |
| 872 |
$resource->assign($task, $res); |
| 873 |
push @{$outres->{args}} => @{$res->{args}}; |
| 874 |
$outres->{env_vars}->{$_} = $res->{env_vars}->{$_} for keys %{$res->{env_vars}}; |
| 875 |
$outres->{record}->{ref($resource)} = $res->{record}; |
| 876 |
} |
| 877 |
|
| 878 |
return @out; |
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
return; |
| 886 |
} |
| 887 |
|
| 888 |
1; |
| 889 |
|
| 890 |
__END__ |
| 891 |
|
| 892 |
|
| 893 |
=pod |
| 894 |
|
| 895 |
=encoding UTF-8 |
| 896 |
|
| 897 |
=head1 NAME |
| 898 |
|
| 899 |
Test2::Harness::Runner::State - State tracking for the runner. |
| 900 |
|
| 901 |
=head1 DESCRIPTION |
| 902 |
|
| 903 |
This module tracks the state for all running tests. This entire module is |
| 904 |
considered an "Implementation Detail". Please do not rely on it always staying |
| 905 |
the same, or even existing in the future. Do not use this directly. |
| 906 |
|
| 907 |
=head1 SOURCE |
| 908 |
|
| 909 |
The source code repository for Test2-Harness can be found at |
| 910 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 911 |
|
| 912 |
=head1 MAINTAINERS |
| 913 |
|
| 914 |
=over 4 |
| 915 |
|
| 916 |
=item Chad Granum E<lt>[email protected]<gt> |
| 917 |
|
| 918 |
=back |
| 919 |
|
| 920 |
=head1 AUTHORS |
| 921 |
|
| 922 |
=over 4 |
| 923 |
|
| 924 |
=item Chad Granum E<lt>[email protected]<gt> |
| 925 |
|
| 926 |
=back |
| 927 |
|
| 928 |
=head1 COPYRIGHT |
| 929 |
|
| 930 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 931 |
|
| 932 |
This program is free software; you can redistribute it and/or |
| 933 |
modify it under the same terms as Perl itself. |
| 934 |
|
| 935 |
See F<http://dev.perl.org/licenses/> |
| 936 |
|
| 937 |
=cut |