blib/lib/HPCI/Sub.pm untested

filter: Statementstmt 0.0% 0 / 188 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 44 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 313 CC 30 SCAR 43.8
CC
30
Cov
0%
_use_files_builder
182.0
_files_ready_to_start_stage
90.0
_can_be_skipped
30.0
CRAP 80.1
1 package HPCI::Sub;
2  
3 ### INCLUDES ##################################################################
4  
5 # safe Perl
6 use warnings;
7 use strict;
8  
9 use Moose::Role;
10 use Moose::Util::TypeConstraints;
11 use MooseX::Types::Path::Class qw(Dir);
12  
13 # with 'HPCI::SuperSub';
14  
15 =head1 NAME
16  
17 HPCI::Sub
18  
19 =head1 SYNOPSIS
20  
21 Role for methods and attributes common to subgroup and stage classes
22 (i.e. those subordinate to a group-like class).
23  
24 =head1 ATTRIBUTES
25  
26 =head2 cluster (internally provided)
27  
28 The type of cluster that will be used to execute the subgroup
29 or stage. This value is passed on by the $group->stage or
30 $group->subgroup method when it creates a new child. Since it
31 also uses that value to select the type of stage object that is
32 created, it is somewhat redundant.
33  
34 =cut
35  
36 has 'cluster' => (
37 is => 'ro',
38 isa => 'Str',
39 required => 1,
40 );
41  
42 =head2 group
43  
44 The group or subgroup object that has this object as a child.
45  
46 The group that this object belongs to is automatically provided to
47 initialize this attribute. You don't need to initialize it explicitly,
48 and since its use is expected to be internal, you won't have much, if
49 any, need to use it either.
50  
51 =cut
52  
53 has 'group' => (
54 is => 'ro',
55 isa => 'Object',
56 weak_ref => 1,
57 required => 1,
58 handles => [
59 qw(
60 debug info warn error fatal
61  
62 _stages _subgroups _deps _pre_reqs _blocked _ready
63  
64 file_system_delay
65 _register_subgroup
66 _register_stage
67 _execution_started
68 )
69 ],
70 );
71  
72 sub _croak {
73 my $self = shift;
74 my $msg = shift;
75 $msg = "Stage(" . $self->name . "): $msg";
76 $self->fatal($msg);
77 confess $msg;
78 }
79  
80 =head2 name
81  
82 The name of this subgroup or stage.
83  
84 All stages and subgroups must have names that are different from
85 each other, from all of their (grand)parent groups and from all
86 of the siblings of their (grand)parent groups. Stages or subgroups
87 may have the same name only if they are at most cousins.
88  
89 =cut
90  
91 has 'name' => (
92 is => 'ro',
93 isa => 'Str',
94 required => 1,
95 );
96  
97 after BUILD => sub {
98 my $self = shift;
99 $self->group->_register_descendent($self);
100 };
101  
102 =head2 base_dir (optional) move stuff from Role to here
103  
104 The directory that will contain all generated output (unless
105 that output is specifically directed to some other location).
106 The default is the current directory.
107  
108 =cut
109  
110 has 'base_dir' => (
111 is => 'ro',
112 isa => 'Path::Class::Dir',
113 coerce => 1,
114 default => sub {
115 my $self = shift;
116 Dir->new( '.' );
117 },
118 );
119  
120 =head2 files
121  
122 A hash that can contain lists of files.
123  
124 Throughout this hash, there are filenames contained within hash elements
125 that describe the processing required for that file. Whenever a filename
126 is needed, it can either be a string containing a pathname, or it can be
127 an HPCI::File object (or subclass), or it can be a HashRef. Often, it
128 will be the string form, which will be converted to an object internally.
129  
130 The top level of the hash has keys 'in' (for input), 'out' (for output),
131 'skip', 'skipstage', 'rename', and 'delete'.
132 (The same file might be listed under multiple keys.)
133  
134 The values for these keys are:
135  
136 =over 4
137  
138 =item 'in'
139  
140 a hashref with possible keys:
141  
142 'req' (for required input files)
143 'opt' (for required output files)
144  
145 The value for either of these can be either a filename or a list of filenames.
146  
147 When a C<subgroup> or C<stage> containing any files/in entries is ready to
148 be executed, HPCI checks whether the listed files exist. If one that is
149 C<req> does not exist, the stage or subgroup is aborted. Any files that
150 do exist can get additional validation as specified in the associated
151 C<HPCI::File> subclass for the file - for example, checksum files can
152 be generated or verified.
153  
154 =item 'out'
155  
156 a hashref with possible keys:
157  
158 'req' (for required output files)
159 'opt' (for required output files)
160  
161 The value for either of these can be either a filename or a list of filenames.
162  
163 When a C<subgroup> or C<stage> containing any files/out entries completes
164 execution, HPCI checks whether the listed files have been created or modified.
165 If one that is C<req> does not exist or has not been modified, the stage or
166 subgroup is aborted. Any files that were created by this stage or subgroup
167 can get additional processing as specified in the associated C<HPCI::File>
168 subclass for the file - for example, checksum files can be generated.
169  
170 =item 'skip' (or, deprecated, 'skipstage')
171  
172 The traditional name 'skipstage' for this element has been deprecated
173 and has been replaced by the new name 'skip'. The old name became
174 confusing when subgroups were added and could also contain a files
175 list that applied to the entire subgroup. The 'skip' list defines
176 the contitions under which the stage or subgroup can be skipped.
177 This will be useful in a restart of an HPCI program that previously
178 completed some stages - the 'skip' elements allow HPCI to determine
179 whether this subgrou or stage completed successfully on the earlier
180 run and hance does not need to be re-executed.
181  
182 It contains either:
183  
184 =over 4
185  
186 =item
187  
188 an arrayref
189  
190 =item
191  
192 a hashref with the keys 'pre' (for pre-requisites) and 'lists'
193  
194 =back
195  
196 The arrayref (either the arrayref value of 'skip' or the arrayref value
197 for the 'lists' hash element) can contain either a list of files, or a hashref
198 with keys 'pre' and 'files'.
199  
200 The 'pre' value (if present) at the top level is a list of files which are
201 pre-requisites for all of the lists. If a list has its own 'pre' list, those
202 files are only pre-requities for the files in that list.
203  
204 =item 'rename'
205  
206 a list of pairs of filenames
207  
208 The file named as the first element in each pair (if it exists) is renamed to
209 the second filename in the pair. It is not considered an error for the first
210 file in a pair to not exist - if you want to ensure that a file exists,
211 include it as an 'out'->'req' file as well.
212  
213 =item 'delete'
214  
215 can be either:
216  
217 a scalar filename
218 a list of filenames
219  
220 These will be removed if the stage completes successfully. It is not
221 considered an error if any of these files does not exist - include them
222 in the 'out'->'req' files list if you wish to ensure that they do.
223  
224 =back
225  
226 The contents are used at various times:
227  
228 =over 4
229  
230 =item the stage/subgroup is ready to be executed
231  
232 =over
233  
234 =item
235  
236 if a 'skip' key is present then checking is done to decide whether the
237 stage or subgroup needs to be executed or can be skipped (treating it
238 as a successful completion)
239  
240 the main content of this key is a list of lists of filenames (the target
241 files) - if any of these lists has all of its files existing, then the stage
242 can be skipped
243  
244 if there is a top level and/or a list level 'pre' list, then all of the files
245 in the pre list(s) must also exist and be older than the target files (the
246 files in the top level 'pre' list are checked against all of the target lists,
247 the files in a target level 'pre' list are only checked against that target).
248  
249 C<skip> checking is always done by the parent process, in hopes of
250 avoiding the need to create the stage.
251  
252 =item
253  
254 all 'in'->'req' files must exist, if any is missing, the stage is aborted.
255 If the files exist, then the child stage will be set up (if needed) to
256 download those files from the long-term storage.
257  
258 =item
259  
260 all 'in'->'opt' are checked by the parent. If any exists, then the child stage
261 will be set up (if needed) to download them from the long-term storage.
262  
263 =item
264  
265 Any additional processing of 'in' files that are present (such as
266 validating or auto-generating checksums) is also done at this time, and may
267 also cause the stage or subgroup to be aborted.
268  
269 =back
270  
271 =item the stage or subgroup has completed execution
272  
273 =over
274  
275 =item
276  
277 all 'out'->'req' files must exist and they must have been updated during the
278 execution of the stage (otherwise the stage is treated as failing)
279  
280 =item
281  
282 any 'out'->'opt' files which exists must have been updated during the execution
283 of the stage (otherwise the stage is treated as failing)
284  
285 =item
286  
287 any additional processing of 'out' files which have been updated (such
288 as generating a checksum) is also done
289  
290 =item
291  
292 clusters that require special treatment of files can take copying actions to
293 collect any 'out' files that have been updated and return them to the
294 original node
295  
296 =item
297  
298 if the stage completed successfully, any files lists as 'rename' are renamed
299 to their new name
300  
301 =item
302  
303 if the stage completed successfully, any files lists as 'delete' are removed
304  
305 =back
306  
307 =back
308  
309 =cut
310  
311 has 'files' => (
312 is => 'ro',
313 isa => 'Maybe[HashRef]',
314 default => undef,
315 );
316  
317 has '_use_files' => (
318 is => 'ro',
319 isa => 'Maybe[HashRef]',
320 lazy => 1,
321 init_arg => undef,
322 builder => '_use_files_builder',
323 );
324  
325 sub _use_files_builder {
326 my $self = shift;
327 my $file = $self->files // return undef;
328 my $use_files = {};
329 SIMPLE_FILES:
330 for my $keys ( [qw(in req)],
331 [qw(in opt)],
332 [qw(out req)],
333 [qw(out opt)],
334 ['delete']
335 ) {
336 my $source = $file;
337 $source = $source->{$_} or next SIMPLE_FILES for @$keys;
338 my $prev_dest;
339 my $dest = $use_files;
340 for my $key (@$keys) {
341 $prev_dest = $dest;
342 $dest = $dest->{$key} //= {};
343 }
344 $prev_dest->{$keys->[-1]} = $self->_get_file_list($source);
345 }
346 my $skipinfo;
347 if ($skipinfo = $file->{skipstage} ) {
348 $self->warn(
349 "'skipstage' element name for a files array is depracated, use 'skip' instead"
350 );
351 }
352 if ($skipinfo //= $file->{skip}) {
353 $use_files->{skip} = {};
354 if (ref($skipinfo) eq 'HASH') {
355 $self->_croak('skip hash element must be have keys "pre" and "lists"')
356 unless exists $skipinfo->{pre} && exists $skipinfo->{lists};
357 $use_files->{skip}{pre} = $self->_get_file_list( $skipinfo->{pre} );
358 $skipinfo = $skipinfo->{lists};
359 $self->_croak('skip->{lists} element must be an array')
360 unless ref($skipinfo) eq 'ARRAY';
361 }
362 else {
363 $self->_croak('skip files element must be either a hash or an array')
364 unless ref($skipinfo) eq 'ARRAY';
365 $use_files->{skip}{pre} = [];
366 }
367 my $dest_list = $use_files->{skip}{lists} = [ ];
368 for my $l (@$skipinfo) {
369 my $dest_hash = { };
370 $self->_croak('skip list element must be either a hash or an array')
371 unless ref($l) eq 'ARRAY' || ref($l) eq 'HASH';
372 if (ref($l) eq 'HASH') {
373 $dest_hash->{pre} = $self->_get_file_list( $l->{pre} );
374 $l = $l->{post};
375 $self->_croak('skip post element must be an array')
376 unless ref($l) eq 'ARRAY';
377 }
378 else {
379 $dest_hash->{pre} = [];
380 }
381 $dest_hash->{post} = $self->_get_file_list( $l );
382 push @$dest_list, $dest_hash;
383 }
384 }
385 if ( my $renamelist = $file->{rename} ) {
386 $self->_croak('rename files element an array')
387 unless ref($renamelist) eq 'ARRAY';
388 my $use_pairs = $use_files->{rename} = [];
389 for my $pair (@$renamelist) {
390 $self->_croak('rename files array element must be an array with two values')
391 unless ref($pair) eq 'ARRAY' && scalar(@$pair) == 2;
392 my ($from, $to) = @$pair;
393 push @$use_pairs, [ $self->_get_file_obj($from), $to ];
394 }
395 }
396 return $use_files;
397 };
398  
399 sub _get_file_list {
400 my $self = shift;
401 my $source = shift;
402 $source = [ $source ] unless ref($source);
403 $source = [ $source ] if blessed $source && $source->isa("HPCI::File");
404 my $ref = ref($source);
405 $self->_croak( "Element in files attribute is not an array, a pathname or HPCI::File class. Type is $ref, value is $source")
406 unless $ref eq 'ARRAY';
407 return [ map { $self->_get_file_obj($_) } @$source ];
408 }
409  
410 sub _get_file_obj {
411 my $self = shift;
412 my $file = shift;
413 return $self->_generate_file( ref($file) eq 'ARRAY' ? @$file : $file );
414 }
415  
416 sub _generate_file {
417 my $self = shift;
418 my $file = shift;
419 if (blessed $file && $file->isa("HPCI::File")) {
420 return $file;
421 }
422 my $path = File::Spec->rel2abs("$file");
423 # TODO: add search through storage_classes list here
424 my $known = $self->_file_info->{$path} //= {};
425 return ($known->{file_class} //= $self->file_class)->new(
426 file => $path,
427 stage => $self,
428 %{ $known->{params} // {} },
429 @_
430 );
431 }
432  
433 sub lof {
434 my $lof = shift;
435 my $res = '[';
436 $res .= " $_" for @$lof;
437 $res .= ' ]';
438 return $res;
439 }
440  
441 sub _can_be_skipped {
442 my $self = shift;
443 my $skipinfo = $self->_use_files // return 0;
444 $skipinfo = $skipinfo->{skip} // return 0;
445 my $pre = $skipinfo->{pre};;
446 my $lol = $skipinfo->{lists};
447 LIST:
448 for my $l (@$lol) {
449 my $thispre = [@$pre, @{$l->{pre}}];
450 $l = $l->{post};
451 $self->debug( "Considering skip, pre: ", lof($thispre), ", list: ", lof( $l ) );
452 unless (@$l) { # an empty list does not qualify for skipping
453 $self->debug( "no skip: empty list element" );
454 next LIST;
455 }
456 my $newest_pre;
457 for my $f (@$thispre) {
458 # if there is a pre list
459 # check that they all exist and
460 # get latest pre timestamp
461 my $ts = $f->timestamp;
462 unless (defined $ts) {
463 $self->debug( "no skip: missing pre element: $f" );
464 next LIST;
465 }
466 $newest_pre ||= $ts;
467 $newest_pre = $ts if $ts < $newest_pre;
468 }
469 if ($self->_file_list_acceptable_for_skip( $newest_pre, $l )) {
470 $self->debug( "skipping: file list acceptable" );
471 return 1;
472 }
473 $self->debug( "no skip: file list not acceptable" );
474 }
475 return 0;
476 }
477  
478 # check whether criteria are satisfied to skip executing the stage
479 sub _files_ready_to_start_stage {
480 my $self = shift;
481 return 1 unless my $files = $self->_use_files;
482 my $retval = 1; # success unless missing file(s) found
483 if (my $in = $files->{in}) {
484 while ( my ($type, $fileval) = each %$in ) {
485 for my $file (@$fileval) {
486 unless ($file->valid_in_file || $type ne 'req') {
487 # need to check exist on opt files in case
488 # the driver needs to take special action to
489 # make it available to the stage when it runs
490 #
491 # go through the entire list of files to give user
492 # a full list of missing files rather than just the
493 # first one we notice - let them fix everything for
494 # the next run instead of needing separate extra
495 # funs to be notified of each successive issue
496 $self->error("Required input file ($file) not present");
497 $self->_set_failure_info(
498 "Failed stage ("
499 . $self->name
500 . ") without execution because one or more required input files were not present"
501 );
502 $retval = 0;
503 }
504 }
505 }
506 }
507 if ($retval and my $unshared = $files->{unshared}) {
508 # our @HPCI::ScriptSource::pre_commands;
509 # our @HPCI::ScriptSource::post_success_commands;
510 if (my $in = $unshared->{in}) {
511 while ( my ($type, $fileval) = each %$in ) {
512 for my $file (@$fileval) {
513 my $exists = $self->_unshared_file_exists_for_in($file);
514 my $get = $self->_unshared_file_download_for_in($file);
515 push @HPCI::ScriptSource::pre_commands, "if $exists\n";
516 push @HPCI::ScriptSource::pre_commands, "then\n";
517 push @HPCI::ScriptSource::pre_commands, " $get\n";
518 if ($type eq 'req') {
519 push @HPCI::ScriptSource::pre_commands, "else\n";
520 push @HPCI::ScriptSource::pre_commands, " echo required in file not present: $file 1>&2\n";
521 push @HPCI::ScriptSource::pre_commands, " exit 126\n";
522 }
523 push @HPCI::ScriptSource::pre_commands, "fi\n";
524 }
525 }
526 }
527 if (my $out = $unshared->{out}) {
528 while ( my ($type, $fileval) = each %$out ) {
529 for my $file (@$fileval) {
530 my $exists = $self->_unshared_file_exists_for_out($file);
531 my $put = $self->_unshared_file_upload_for_out($file);
532 push @HPCI::ScriptSource::post_success_commands, "if $exists\n";
533 push @HPCI::ScriptSource::post_success_commands, "then\n";
534 push @HPCI::ScriptSource::post_success_commands, " $put\n";
535 if ($type eq 'req') {
536 push @HPCI::ScriptSource::post_success_commands, "else\n";
537 push @HPCI::ScriptSource::post_success_commands, " echo required out file not present: $file 1>&2\n";
538 push @HPCI::ScriptSource::post_success_commands, " exit 126\n";
539 }
540 push @HPCI::ScriptSource::post_success_commands, "fi\n";
541 }
542 }
543 }
544 }
545 return $retval;
546 }
547  
548 sub _default_file_info {
549 my $self = shift;
550 return $self->group->_file_info;
551 }
552  
553  
554 =head2 connect (optional)
555  
556 This can contain an URL to be used by the driver for types of cluster
557 where it is necessary to connect to the cluster in some way. It can
558 be omitted for local clusters that are directly accessible.
559  
560 =cut
561  
562 has 'connect' => (
563 is => 'ro',
564 isa => 'Str',
565 );
566  
567 =head2 login, password (optional)
568  
569 This can contain an identifier to be used by the driver for types of
570 cluster which require authorization.
571  
572 =cut
573  
574 has [ qw(login password) ] => (
575 is => 'ro',
576 isa => 'Str',
577 );
578  
579 =head2 max_concurrent (optional)
580  
581 The maximum number of stages to be running concurrently. If 0
582 (which is the default), then there is no limit applied directly by
583 HPCI (although the underlying cluster-specific driver might apply
584 limits of its own).
585  
586 =cut
587  
588 has 'max_concurrent' => (
589 is => 'ro',
590 isa => 'Int',
591 default => 0,
592 );
593  
594 =head2 file_class (internal)
595  
596 The default storage class attribute for files that do not
597 have an explicit class given. This is the name of a class.
598 The default is to use the value from the parent (sub-)group.
599  
600 =cut
601  
602 has 'file_class' => (
603 is => 'ro',
604 isa => 'Str',
605 lazy => 1,
606 default => sub {
607 $_[0]->group->file_class
608 },
609 );
610  
611 # has 'storage_classes' => (
612 # is => 'ro',
613 # isa => 'HPCIListList',
614 # lazy => 1,
615 # default => sub { $_[0]->group->storage_classes },
616 # init_arg => undef,
617 # );
618  
619 #### Internal attributes
620  
621 =head1 METHODS
622  
623 =head2 $group->add_file_params
624  
625 Augment the file_params list with additional files.
626 Provide either a hashref or a list of value pairs,
627 in either case, the pairs are filename as the key,
628 and params as the value.
629  
630 =cut
631  
632 # sub add_file_params {
633 # my $self = shift;
634 # my $fi = $self->_file_info;
635 # my $args = $_[0];
636 # $args = { @_ } unless ref $args eq 'HASH';
637 # while ( my ($k,$v) = each %$args ) {
638 # $self->_croak( "file params value for $k must be a hash ref" )
639 # unless (ref($v) eq 'HASH');
640 # $k = File::Spec->rel2abs($k);
641 # $fi->{$k}{params} = $v;
642 # }
643 # }
644  
645  
646 =head1 AUTHOR
647  
648 Christopher Lalansingh - Boutros Lab
649  
650 John Macdonald - Boutros Lab
651  
652 =head1 ACKNOWLEDGEMENTS
653  
654 Paul Boutros, Phd, PI - Boutros http://www.omgubuntu.co.uk/2016/03/vineyard-wine-configuration-tool-linuxLab
655  
656 The Ontario Institute for Cancer Research
657  
658 =cut
659  
660 1;
661