blib/lib/HPCI/Group.pm untested

filter: Statementstmt 0.0% 0 / 346 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 44 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 475 CC 1 SCAR 6.9
CC
1
Cov
0%
_default_file_info
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package HPCI::Group;
2  
3 ### INCLUDES ##################################################################
4  
5 # safe Perl
6 use warnings;
7 use strict;
8 use Carp;
9 use Data::Dumper;
10 use File::ShareDir;
11 use File::Path qw(make_path);
12 use File::Spec;
13 use Time::HiRes qw(usleep gettimeofday);
14 use Try::Tiny;
15 use DateTime;
16 use Module::Load;
17 use HPCI::File;
18  
19 use MooseX::Role::Parameterized;
20 use MooseX::Types::Path::Class qw(Dir File);
21 use Moose::Util::TypeConstraints;
22  
23 ## no critic BoutrosLab::HangingComma
24 ## no critic BoutrosLab::IndentationCheck
25  
26 parameter theDriver => (
27 isa => 'Str',
28 required => 1,
29 );
30  
31 role {
32 my $p = shift;
33 my $theDriver = $p->theDriver;
34 my $StageClass = "${theDriver}::Stage";
35  
36 # do nothing, but let roles write before/after/around wrappers
37 method BUILD => sub {
38 };
39  
40 with
41 'HPCI::Super',
42 'HPCI::SuperSub',
43 'HPCI::Logger',
44 'HPCI::Env',
45 ;
46 # Future considerations for adding:
47 #
48 # HPCI::HTML - generate a report about an execution
49 # HPCI::Template - templates for reports
50 # - was formerly used for script
51 # - now generated dynamically
52 # - allowing for easier control by plugins
53  
54 method wait_for_completed_stage => sub {
55 my $self = shift;
56 my $finished_job = $self->next_finished_job;
57 my $name = $finished_job->stage->_full_name;
58 my $stage = $self->_stages->{$name};
59 if ($stage->_has_command) {
60 $stage->_analyse_completion_state($finished_job);
61 }
62 else {
63 $stage->_set_state( $finished_job->status ? 'fail' : 'pass' );
64 }
65 # if ($stage->is_pass && ! $stage->_files_acceptable_for_completed_stage($finished_job)) {
66 # $self->fail_stage( $stage )
67 # }
68 if ($stage->is_fail
69 && $stage->_chosen_retries < $stage->choose_retries
70 && ($finished_job->_analysis_chose_retry
71 || ( $stage->_has_should_choose_retry
72 && $stage->should_choose_retry->(
73 $finished_job->stats,
74 $finished_job->_stderr
75 )
76 )
77 )
78 ) {
79 $stage->_set_state('retry');
80 $stage->_chosen_retries($stage->_chosen_retries+1);
81 $self->info( "Using user chosen retry: ",
82 $stage->_chosen_retries,
83 " of ",
84 $stage->choose_retries,
85 );
86 }
87 if ($stage->is_fail && $stage->_forced_retries < $stage->force_retries) {
88 $stage->_set_state('retry');
89 $stage->_forced_retries($stage->_forced_retries+1);
90 $self->info( "Using forced retry: ",
91 $stage->_forced_retries,
92 " of ",
93 $stage->force_retries,
94 );
95 }
96 $stage->_files_actions_after_success if $stage->is_pass;
97  
98 my $state = $stage->state;
99 $finished_job->stats->{final_job_state} = $state;
100 my $index = $finished_job->index;
101 $self->info("stage($name) run($index) completion state: $state");
102 return $stage;
103 };
104  
105 =head1 NAME
106  
107 HPCI::Group
108  
109 =head1 SYNOPSIS
110  
111 Role for building a cluster-specific driver for a group
112 of stages. This should only be used internally to the
113 C<HPCI> module - code that uses this driver will
114 not load this module (or the driver module) explicitly.
115  
116 It describes the user interface for a generic group, hiding
117 (as much as possible) the specifics of the actual cluster that
118 is being used. The driver module that consumes this role will
119 arrange to translate the generic interface into the particular
120 interface conventions of the specific cluster that it accesses.
121  
122 An (internally defined) cluster-specific group object is
123 defined with:
124  
125 package HPCD::$cluster::Group;
126 use Moose;
127  
128 ### required method definitions
129  
130 with 'HPCI::Group' => { StageClass => 'HPCD::$cluster::Stage' },
131 # any other roles required ...
132 ;
133  
134 ### cluster-specific method definition if any ...
135  
136 =head1 DESCRIPTION
137  
138 This role provides the generic interface for a group object
139 which can configure and run a collection of stages (jobs)
140 on machines in a cluster. It is written to be independent
141 of the specifics of any particular cluster interface. The
142 cluster-specific module that consumes this role is not accessed
143 directly by the user program - they are provided with a group
144 driver object of the appropriate cluster-specific type using
145 the "class method" HPCI->group (with an appropriate
146 C<cluster> argument) to request an appropriate to build it.
147  
148 =head1 ATTRIBUTES
149  
150 =head2 name (optional)
151  
152 The name of this group of stages. Defaults to 'default_group_name'.
153 Not actually required for a group.
154  
155 =cut
156  
157 has 'name' => (
158 is => 'ro',
159 isa => 'Str',
160 default => 'default_group_name',
161 );
162  
163 =head2 cluster
164  
165 The type of cluster that will be used to execute the group of stages.
166 This value is passed on internally by the HPCI->stage method when it
167 creates a new stage, or by the group->subgroup method when it creates
168 a new subgroup. Since it also uses that value to select the type
169 of group object that is created, it is somewhat redundant.
170  
171 =cut
172  
173 has 'cluster' => (
174 is => 'ro',
175 isa => 'Str',
176 required => 1,
177 );
178  
179 =head2 base_dir (optional)
180  
181 The directory that will contain all generated output (unless
182 that output is specifically directed to some other location).
183 The default is the current directory.
184  
185 =cut
186  
187 has 'base_dir' => (
188 is => 'ro',
189 isa => 'Path::Class::Dir',
190 coerce => 1,
191 default => sub {
192 my $self = shift;
193 Dir->new( '.' );
194 },
195 );
196 =head2 storage_classes (optional) (Not currently used, needs work)
197  
198 HPCI has two conceptual storage types that it expects to be available.
199  
200 Long-term storage is storage that is reliably preserved over time.
201 The data in long-term storage is accessible to all nodes on the
202 cluster, but possibly only accessible through special commands.
203  
204 Working storage is storage that is directly-accessible to a node
205 through normal file system access methods. It can be a private disk
206 that is not accessible to other nodes, or it can be a shared file
207 system that is available to other nodes.
208  
209 It is fairly common, and most convenient, if the working storage also
210 qualifies as long-term storage. That is the default expectation if
211 HPCI is not told otherwise.
212  
213 However, some types of cluster can have their nodes rebuilt at intervals,
214 losing the data on their local disks. Some types of cluster have a
215 shared file system that is not strongly persistent, but which can be
216 rebuilt at intervals. Some types of cluster have shared file systems
217 that have size limitations that mean that some or all of the data sets
218 for stage processes cannot be stored there.
219  
220 In such cases, some or all of the files must have a long-term storage
221 location that is different from the more convenient working storage
222 location that will be used when stages are running. Depending upon the
223 environment, the long-term storage will use something like:
224  
225 =over 4
226  
227 =item network accessed storage
228  
229 A storage facility that allows file to be uploeded and downloaded through
230 a netowrk connection (from any node in the cluster).
231  
232 =item parent managed storage
233  
234 The job controlling program may have long-term storage of it own that
235 is not accessible to other nodes in the cluster. If there is a large
236 enough shared file system (that for some reason cannot be used as long-term
237 storage) the parent HPCI program can copy files between that storage and
238 the shared storage as needed to make the files available and to preserve
239 the results.
240  
241 =item bundled storage
242  
243 In a B<cloud> layout there is often no file system shared amongst all of
244 the nodes and the parent process. In this type of cluster, a submitted
245 stage will include some sort of bundle containing a collection of data
246 and control files (possibly even the entire operating system) to be used
247 by the stage, and a similar sort of bundle is recovered to provide the
248 results of running a stage. (This could be, for example, a C<docker>
249 image.)
250  
251 =back
252  
253 The attribute C<storage_classes> defines the available storage classes
254 that can be used for stage files.
255  
256 In most cases, all files for all stages will be of the same storage
257 class, but some cluster configurations will have multiple storage
258 choices and can have, for some stages, the need to use more than one of
259 the storage classes for different files within the same job.
260  
261 To cater to both of these, the C<storage_classes> attribute can either be
262 a single arrayref which will be used for all files, or it can be a hash of
263 named arrayrefs, with the name being used to select the class for each
264 individual file. A file (described in the C<files> attribute of the C<stage>
265 class) can either be a scalar string specifying the pathname of the working
266 storage location that will be used, or it can be a one element hashref, with
267 the key selecting the storage class and the value providing the working
268 storage pathname. If the hash of named arrayrefs is used, one of the
269 elements of the hash should have the key C<default> - that will be used for
270 files which do not provide an explicit storage class key.
271  
272 The default value for this attribute is:
273  
274 [ 'HPCI::File ]
275  
276 The HPCI::File class defines the usage for the common case in which there is
277 no need for a long-term storage area that is different from the working
278 storage area.
279  
280 Classes that provide to separate long-term storage area will usually require
281 additional arguments, for specifying access control information (such as url,
282 username, password) and how to map the working storage pathname into the
283 corresponding location in the long-term storage.
284  
285 See the documentation for HPCI::File for details on writing new classes.
286  
287 =head2 file_class
288  
289 The default storage class attribute for files that do not
290 have an explicit class given. This is the name of a class.
291 The default is HPCI::File, but a sub-class of HPCI::File can
292 be provided instead.
293  
294 =cut
295  
296 has 'file_class' => (
297 is => 'ro',
298 isa => 'Str',
299 lazy => 1,
300 default => 'HPCI::File',
301 );
302  
303 =head2 _default_file_info (internal)
304  
305 This over-rides the _default_file_info method from HPCI::Super
306 which sets the default contents of the C<file_params> attribute.
307 HPCI::Super normally copies from the parent group, but since this
308 top-level group has no parent it is set to an empty hash.
309  
310 =cut
311  
312 sub _default_file_info { return { } };
313  
314 =head2 _unique_name (internal)
315  
316 The name of the top level group with a timestamp added to make it
317 unique so that the directory created to hold to files from the
318 execution of the group will not conflict with other runs.
319  
320 =cut
321  
322 has '_unique_name' => (
323 is => 'ro',
324 isa => 'Str',
325 lazy => 1,
326 init_arg => undef,
327 default => sub {
328 my $self = shift;
329 my $name = $self->name;
330 my $tstamp = DateTime->now->strftime("%Y%m%d-%H%M%S");
331 return "$name-$tstamp";
332 },
333 );
334  
335 has '_stages' => (
336 is => 'ro',
337 isa => "HashRef[$StageClass]",
338 lazy => 1,
339 init_arg => undef,
340 default => sub { {} },
341 );
342  
343 method _register_stage => sub {
344 my $self = shift;
345 my $stage = shift;
346 my $name = $stage->name;
347 my $fullname = $stage->_full_name;
348 $self->info( "Created stage($fullname)" );
349 $self->_stages->{$fullname} = $stage;
350 $self->_stage_cnt( $self->_stage_cnt + 1 );
351 $self->_ready->{$fullname} = 1;
352 };
353  
354 has '_subgroups' => (
355 is => 'ro',
356 isa => "HashRef[HPCI::Subgroup]",
357 lazy => 1,
358 init_arg => undef,
359 default => sub { {} },
360 );
361  
362 method _register_subgroup => sub {
363 my $self = shift;
364 my $subgroup = shift;
365 my $name = $subgroup->name;
366 my $fullname = $subgroup->_full_name;
367 $self->info( "Created subgroup($fullname)" );
368 $self->_subgroups->{$fullname} = $subgroup;
369 };
370  
371 =head2 connect (optional)
372  
373 This can contain a value to be used by the driver for types of cluster
374 where it is necessary to connect to the cluster in some way. It can
375 be omitted for local clusters that are directly accessible.
376  
377 =cut
378  
379 has 'connect' => (
380 is => 'ro',
381 isa => 'Str',
382 );
383  
384 =head2 login, password (optional)
385  
386 This can contain a value to be used by drivers for
387 clusters which carry out a connection process which
388 require a login and password or some other similar
389 authorization data.
390  
391 =cut
392  
393 has [ qw(login password) ] => (
394 is => 'ro',
395 isa => 'Str',
396 );
397  
398 =head2 max_concurrent (optional)
399  
400 The maximum number of stages to be running concurrently. The value 0
401 (which is the default) means that there is no limit applied directly by
402 HPCI (although the underlying cluster-specific driver might apply
403 limits of its own). This limit is for the group and there is no
404 mechanism provided at present to manage the number of stages for a
405 user across separately invoked programs. So, if your cluster requires
406 the user to limit the number of separate jobs a user is running
407 simultaneously then this can only be a partial solution for you.
408  
409 =cut
410  
411 has 'max_concurrent' => (
412 is => 'ro',
413 isa => 'Int',
414 default => 0,
415 );
416  
417 =head2 status (provided internally)
418  
419 After the execute method has been called, this attribute contains the
420 return result from the execution. This is a hash (indexed by stage
421 name). The value for each stage is an array of the return status.
422 (Usually, this array has only one element, but there will be more
423 if the stage was retried. The final element of the array is almost
424 always the one that you wish to look at.) The return status is a
425 hash - it will always contain an element key 'exit_status' giving
426 the exit status of the stage. Additional entries will be found in
427 the hash for cluster-specific return reults. Thus, to check the
428 exit status of a particular stage you would code either:
429  
430 $result = $group->execute;
431 if ($result->{SOMESTAGENAME}[-1]{exit_status}) {
432 die "SOMESTAGENAME failed!";
433 }
434  
435 or:
436  
437 $group->execute;
438 # ...
439 if ($group->status->{SOMESTAGENAME}[-1]{exit_status}) {
440 die "SOMESTAGENAME failed!";
441 }
442  
443 =cut
444  
445 has 'status' => (
446 is => 'ro',
447 isa => 'HashRef',
448 lazy => 1,
449 init_arg => undef,
450 default => sub { {} },
451 writer => '_set_status',
452 );
453  
454 =head2 file_system_delay
455  
456 Shared files systems can have a delay period during which an action
457 on the file system is not yet visible to another node sharing that
458 file system. This is common for NFS shared file systems, for
459 example.
460  
461 The file_system_delay attribute can be given a non-zero number of
462 seconds to indicate the amount of time to wait to ensure that
463 actions taken on another node are visible.
464  
465 This is used for internal actions such as validating that required
466 stage output files have been created.
467  
468 =cut
469  
470 has 'file_system_delay' => (
471 is => 'ro',
472 isa => 'Int',
473 lazy => 1,
474 default => 0,
475 );
476  
477 #### Internal attributes
478  
479 has [qw(_deps _pre_reqs)] => (
480 is => 'ro',
481 isa => 'HashRef[Str]',
482 lazy => 1,
483 init_arg => undef,
484 default => sub { {} },
485 );
486  
487 has [qw(_stage_cnt _running_cnt _finished_cnt)] => (
488 is => 'rw',
489 isa => 'Int',
490 default => 0,
491 );
492  
493 has [qw(_ready _blocked _submitted _completed _failed)] => (
494 is => 'ro',
495 isa => 'HashRef',
496 init_arg => undef,
497 default => sub { {} },
498 );
499  
500 has [qw(_sub_secs _sub_microsecs)] => (
501 is => 'rw',
502 isa => 'Int',
503 init_arg => undef,
504 default => 0,
505 );
506  
507 has '_execution_started' => (
508 is => 'rw',
509 isa => 'Int',
510 init_arg => undef,
511 default => 0,
512 );
513  
514 =head1 METHODS
515 =cut
516  
517 =head2 $group->execute
518  
519 Execute the stages in the group. Does not return until all stages
520 are complete (or have been skipped because of a failure of some
521 other stage or the attempt is aborted).
522  
523 =cut
524  
525 method execute => sub {
526 my $self = shift;
527 my $catcher = sub {
528 my $sig = shift;
529 $self->warn( "Killed with signal SIG$sig" );
530 $self->kill_stages;
531 };
532 local $SIG{HUP} = $catcher;
533 local $SIG{USR1} = $catcher;
534 local $SIG{USR2} = $catcher;
535 $self->_execution_started(1);
536 local $SIG{CHLD} = $SIG{CHLD};
537 $self->child_catcher;
538 my $cnt = $self->_stage_cnt;
539 my $s = $cnt == 1 ? '' : 's';
540 $self->info( "Starting execution with $cnt stage$s defined.");
541 while ($self->_finished_cnt < $self->_stage_cnt) {
542 $self->_submit_ready || next;
543 unless (keys %{ $self->_submitted }) {
544 my $blocked = join( ', ', keys %{ $self->_blocked } );
545 $self->_discard_list( "Deadlock" );
546 $self->_croak("Deadlock - cannot submit any more jobs, remaining jobs ($blocked) are blocked, there must be a dependency loop.")
547 }
548 $self->_await_one_job;
549 }
550 my $ret_stat = {};
551 while (my ($name, $stage) = each %{ $self->_stages }) {
552 my $base_name = $stage->name;
553 next if $base_name =~ m/^__(START|END)__$/;
554 my @subgroups;
555 my $check = $stage;
556 while (my $parent = $check->group) {
557 last unless $parent->does('HPCI::Sub');
558 unshift @subgroups, $parent->name;
559 $check = $parent;
560 }
561 my $ret_hash = $ret_stat;
562 while (my $subgroup = shift @subgroups) {
563 $ret_hash = $ret_hash->{$subgroup} //= {};
564 }
565 $ret_hash->{$base_name} = $stage->get_run_stats;
566 }
567 $self->info( "All done! Stage status:" );
568 $self->_disp_status( " Group", $self->name, $ret_stat );
569 $self->_set_status($ret_stat);
570 $self->systemlogger->($self,$ret_stat)
571 if $self->can('systemlogger') && $self->_has_systemlogger;
572 return $ret_stat;
573 };
574  
575 method _disp_status => sub {
576 my $self = shift;
577 my $type = shift;
578 my $name = shift;
579 my $hash = shift;
580 my $fullname = shift // $name;
581 my $indent = shift // '';
582 my @stages;
583 my @subgroups;
584 return unless %$hash;
585 $self->info( "$indent$type $name" );
586 $indent .= ' | ';
587 map {
588 my $val = $hash->{$_};
589 push @{ ref($val) eq 'HASH' ? \@subgroups : \@stages }, [ $_, $val ];
590 } sort keys %$hash;
591 if (@stages) {
592 for my $stageinfo (@stages) {
593 my ($sname,$runs) = @$stageinfo;
594 $self->info( "${indent}Stage $sname ($fullname/$sname)" );
595 for my $runnum (0..$#$runs) {
596 my $run = $runs->[$runnum];
597 my $runnumdesc =
598 $runnum != $#$runs ? "Retried run $runnum"
599 : $runnum ? "Final run after $runnum retries"
600 : "Only run - no retries";
601 $self->info( "$indent $runnumdesc" );
602 $self->info( sprintf "$indent %-20s => %s", $_, $run->{$_} )
603 for sort keys %$run;
604 }
605 }
606 }
607 if (@subgroups) {
608 for my $sginfo (@subgroups) {
609 my ($name, $childinfo) = @$sginfo;
610 $self->_disp_status( "Subgroup", $name, $childinfo, "$fullname/$name", $indent );
611 }
612 }
613 };
614  
615 # sub wait_for_all_jobs {
616 # # TODO: Never called, so the html report is never written!
617 # # Merge this code into execute when it is ready to work
618 # my $self = shift;
619 #
620 # # Already done differently in execute
621 # my $runs = [];
622 # while ((scalar (keys %{$self->_runs})) > 0) {
623 # push($runs, $self->next_finished_job);
624 # }
625 #
626 # # modify for the different layout of run status info in execute
627 # if ($self->generate_html_report) {
628 # $self->info("Building html report\n");
629 # $self->render_template_to_file(
630 # template_name => 'report.html.template',
631 # output_file_path => $self->html_report_path,
632 # rendering_variables => {
633 # jobs => $runs,
634 # report_root_url => $self->html_report_url,
635 # }
636 # );
637 # }
638 # else {
639 # $self->info("No html report to build\n");
640 # }
641 #
642 # # execute already returns its different structure
643 # return $runs;
644 # }
645  
646  
647 ### Internal methods
648  
649  
650 # _croak
651 # Kill off any executing stages, issue an error message, and quit.
652 # Make sure that the quit happens even if the rest of the cleanup fails
653  
654 method _croak => sub {
655 my $self = shift;
656 my $msg = shift;
657 my $fatalfailed;
658 try {
659 $self->fatal($msg);
660 }
661 catch {
662 ++$fatalfailed;
663 };
664 try {
665 $self->kill_stages if $self->_finished_cnt < $self->_stage_cnt;
666 }
667 catch {
668 try {
669 $self->fatal("Additional failure during cleanup: $_")
670 unless $fatalfailed;
671 }
672 };
673 confess $msg;
674 };
675  
676 # _submit_ready
677 # - submit all stages that are ready to be run
678 # - that will be stages which have no pre_reqs, or for which all pre_reqs
679 # have completed successfully
680  
681 method _submit_ready => sub {
682 my $self = shift;
683 my $min_submit_window = 500_000; # 0.5 second
684 my $min_delay = 1_000; # 1 millisecond is not worth a delay
685 $self->info("Searching for unblocked stages to submit.\n");
686 while ( keys %{ $self->_ready } ) {
687 # tricky loop through all of the keys of the _ready list
688 # cannot just use: for my $name ( keys %{ $self->_ready } )
689 # as the loop because the loop will be not just deleting $name
690 # (which the for loop is guaranteed to survive and still return
691 # all of the keys), but it can also add new keys when a stage
692 # is elegible to be skipped. That could cause the for loop to
693 # terminate while there are still keys in the hash, and might
694 # give the appearance of a dependency loop where there is not
695 # really one present.
696 (my $name) = keys %{ $self->_ready };
697 return 1 if $self->max_concurrent
698 && $self->max_concurrent <= $self->_running_cnt;
699 delete $self->_ready->{$name};
700 my $stage = $self->_stages->{$name};
701 my ($check, $type) =
702 $stage->name eq '__START__'
703 ? ( $stage->group, 'subgroup' )
704 : ( $stage, 'stage' );
705 my $checkname = $check->_full_name;
706 $stage->assert_command_filled;
707 if ($check->_can_be_skipped) {
708 $self->info("Determined that $type ($checkname) can be skipped.\n");
709 if ($type eq 'stage') {
710 $self->_completed->{$name} = 1;
711 $self->_finished_cnt( $self->_finished_cnt + 1 );
712 $self->_unblock_deps($name);
713 $stage->_set_state('pass');
714 return 0;
715 }
716 else {
717 $self->_skip_subgroup($checkname);
718 }
719 }
720 if ( ! $stage->_files_ready_to_start_stage) {
721 # required in file not present, abort running the stage
722 $self->fail_stage( $name, $stage );
723 $self->_finished_cnt( $self->_finished_cnt + 1 );
724 $self->_discard_deps($stage);
725 return 0;
726 }
727 $self->_running_cnt( $self->_running_cnt + 1 );
728 $self->_submitted->{$name} = 1;
729 my ( $now_secs, $now_microsecs ) = gettimeofday;
730 if ($stage->_has_command && $now_secs < $self->_sub_secs + 2) {
731 my $delta_microsecs =
732 ( $now_secs - $self->_sub_secs ) * 1_000_000 +
733 ( $now_microsecs - $self->_sub_microsecs );
734 $delta_microsecs = $min_submit_window - $delta_microsecs;
735 if ($delta_microsecs > $min_delay) {
736 $self->info(
737 "Safety delay ($delta_microsecs) microseconds before submit.\n"
738 );
739 usleep($delta_microsecs) if $delta_microsecs;
740 $now_microsecs += $delta_microsecs;
741 # 'while' instead of 'if'
742 # to allow delay bounds to be adjusted in the future
743 while ($now_microsecs > 1_000_000) {
744 $now_microsecs -= 1_000_000;
745 $now_secs++;
746 }
747 }
748 }
749 $self->_sub_secs($now_secs);
750 $self->_sub_microsecs($now_microsecs);
751 $self->info("Submitting stage ($name).\n");
752 $self->submit_stage( $stage );
753 }
754 return 1;
755 };
756  
757 # _unblock_deps
758 # - after a stage has finished successfully, any stages that are
759 # dependent upon it can be (possibly partially) unblocked
760 # - if this is the final block for a dependent, it is marked ready to run
761  
762 method _unblock_deps => sub {
763 my $self = shift;
764 my $pre_req = shift;
765 for my $dep ( keys %{ $self->_deps->{$pre_req} } ) {
766 delete $self->_pre_reqs->{$dep}{$pre_req};
767 $self->debug( "removing dep: $pre_req -> $dep, remaining deps are( "
768 . join( ' ', sort keys %{ $self->_pre_reqs->{$dep} } )
769 . " )" );
770 unless (keys %{ $self->_pre_reqs->{$dep} }) {
771 $self->_ready->{$dep} = 1
772 if delete $self->_blocked->{$dep};
773 }
774 }
775 };
776  
777 # _blocked_deps_list
778 # - get all deps recursively
779  
780 method _blocked_deps_list => sub {
781 my $self = shift;
782 my $target = shift;
783 my $seen = shift // {};
784  
785 for my $dep ( grep { !$seen->{$_} && $self->_blocked->{$_} }
786 keys %{ $self->_deps->{$target} } )
787 {
788 $seen->{$dep} = 1;
789 $self->_blocked_deps_list( $dep, $seen );
790 }
791 return $seen;
792 };
793  
794 # _discard_deps
795 # - after a stage has failed. the action to be taken is determined by the
796 # setting of the failure_action attribute
797 #
798 # - abort_group
799 # - discard all unstarted stages
800 # - abort_deps
801 # - discard all stages that are deps of the failed stage
802 # (and their deps, recursively)
803 # - ignore
804 # - continue with all other stages
805  
806 method _discard_deps => sub {
807 my $self = shift;
808 my $stage = shift;
809  
810 my $action = $stage->failure_action;
811  
812 return if $action eq 'ignore';
813  
814 my $name = $stage->name;
815 $self->_discard_list(
816 "failure of stage $name",
817 $action eq 'abort_deps'
818 ? $self->_blocked_deps_list( $name )
819 : () # $action eq 'abort_group'
820 );
821 };
822  
823 method _discard_list => sub {
824 my $self = shift;
825 my $reason = shift;
826 my $list = shift // $self->_blocked;
827 unless (defined $list) {
828 # discard all blocked stages, if no explicit list was provided
829 $list = $self->_blocked;
830 }
831 my @discards = sort keys %$list;
832 for my $stage (@discards) {
833 $self->error(
834 "Skipping stage ($stage) because of $reason");
835 $self->_finished_cnt( $self->_finished_cnt + 1 );
836 delete $self->_blocked->{$stage};
837 $self->_failed->{$stage} = 1;
838 $self->_stages->{$stage}->_set_failure_info(
839 "Skipped because of $reason");
840 }
841 };
842  
843 # _await_one_job
844 # - wait until a running stage completes
845 # - if it failed: remove any stages that depend upon it
846 # (or all remaining jobs if abort_group_on_failure was set)
847  
848 method _await_one_job => sub {
849 my $self = shift;
850 my $stage = $self->wait_for_fully_completed_stage;
851 my $fullname = $stage->_full_name;
852  
853 delete $self->_submitted->{$fullname};
854 $self->_finished_cnt( $self->_finished_cnt + 1 );
855 $self->_running_cnt( $self->_running_cnt - 1 );
856 if ($stage->is_pass) {
857 $self->info("Successful completion of stage ($fullname).\n");
858 $self->_completed->{$fullname} = 1;
859 }
860 else {
861 # job failed and cannot be resubmitted
862 $self->fail_stage( $fullname, $stage );
863 }
864 $self->_unblock_deps($fullname);
865 };
866  
867 method _skip_subgroup => sub {
868 my $self = shift;
869 my $subgroup = shift;
870 my @consider = ( $subgroup->_full_name . '/__END__' );
871 my %skipping = ();
872 my $comp = $self->_completed;
873 while (my $next = shift @consider) {
874 # next if $comp->{$next};
875 # unshift @members, $next;
876 for my $pred ( @{ $self->_pre_reqs->{$next} } ) {
877 next if $comp->{$next} || $skipping{$pred}++;
878 unshift @consider, $pred;
879 }
880 }
881 $self->_discard_list( "part of subgroup ($subgroup) being skipped", \%skipping );
882 };
883  
884 method fail_stage => sub {
885 my $self = shift;
886 my $fullname = shift;
887 my $stage = shift;
888 $self->error( "Failed stage ($fullname).\n" );
889 $self->_failed->{$fullname} = 1;
890 $self->_discard_deps( $stage );
891 $stage->_set_state( 'fail' );
892 };
893  
894 # wait for a stage to complete
895 # - if it failed, determine whether it can be retried
896 #
897 # if it can be restarted, it will be marked as such, and any recomputing
898 # of its internal info required for the retry will have already been done.
899 # All that is required is to actually resubnit it for execution
900 #
901 # return the first completed stage that does not need to be restarted
902 method wait_for_fully_completed_stage => sub {
903 my $self = shift;
904 while (1) {
905 my $stage = $self->wait_for_completed_stage;
906 return $stage unless $stage->is_retry;
907 $self->retry($stage);
908 }
909 };
910  
911 # this method can be over-ridden if cluster-specific code needs to take
912 # any action different from the submit done initially that can't be
913 # handled already in the wait_for_completed_stage method
914 method retry => sub {
915 my $self = shift;
916 my $stage = shift;
917 $self->warn( "Retrying stage(".$stage->name.")");
918 $self->submit_stage($stage);
919 };
920  
921 };
922  
923 =head1 AUTHOR
924  
925 Christopher Lalansingh - Boutros Lab
926  
927 John Macdonald - Boutros Lab
928  
929 =head1 ACKNOWLEDGEMENTS
930  
931 Paul Boutros, Phd, PI - Boutros http://www.omgubuntu.co.uk/2016/03/vineyard-wine-configuration-tool-linuxLab
932  
933 The Ontario Institute for Cancer Research
934  
935 =cut
936  
937 1;
938