| 1 | package HPCI::JobGroup; | ||
| 2 | |||
| 3 | ### INCLUDES ################################################################## | ||
| 4 | |||
| 5 | # safe Perl | ||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | use Carp; | ||
| 9 | use File::Path qw(make_path remove_tree); | ||
| 10 | use POSIX ":sys_wait_h"; | ||
| 11 | use HPCI::TimeoutQueue; | ||
| 12 | use MooseX::Types::Path::Class qw(Dir File); | ||
| 13 | |||
| 14 | use MooseX::Role::Parameterized; | ||
| 15 | |||
| 16 | parameter theDriver => ( | ||
| 17 | isa => 'Str', | ||
| 18 | required => 1, | ||
| 19 | ); | ||
| 20 | |||
| 21 | role { | ||
| 22 | my $p = shift; | ||
| 23 | my $theDriver = $p->theDriver; | ||
| 24 | my $RunClass = "${theDriver}::Run"; | ||
| 25 | |||
| 26 | has '_runs' => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => "HashRef[$RunClass]", | ||
| 29 | lazy => 1, | ||
| 30 | init_arg => undef, | ||
| 31 | default => sub { {} }, | ||
| 32 | ); | ||
| 33 | |||
| 34 | has '_t_o_q' => ( | ||
| 35 | is => 'ro', | ||
| 36 | isa => 'HPCI::TimeoutQueue', | ||
| 37 | init_arg => undef, | ||
| 38 | default => sub { HPCI::TimeoutQueue->new }, | ||
| 39 | handles => [ | ||
| 40 | qw(add_timeout | ||
| 41 | delete_timeouts | ||
| 42 | expire_timeouts | ||
| 43 | time_to_next_timeout) | ||
| 44 | ], | ||
| 45 | ); | ||
| 46 | |||
| 47 | has '_caught' => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => 'ArrayRef', | ||
| 50 | init_arg => undef, | ||
| 51 | default => sub { [] }, | ||
| 52 | ); | ||
| 53 | |||
| 54 | my $_next_to = undef; | ||
| 55 | |||
| 56 | has '_reaper' => ( | ||
| 57 | is => 'ro', | ||
| 58 | isa => 'Ref', | ||
| 59 | init_arg => undef, | ||
| 60 | default => sub { | ||
| 61 | my $self = shift; | ||
| 62 | sub { | ||
| 63 | while ( ( my $kid = waitpid( -1, WNOHANG ) ) > 0 ) { | ||
| 64 | my $stat = $?; | ||
| 65 | if ( my $run = $self->_runs->{$kid} ) { | ||
| 66 | # keep a queue of completed children for | ||
| 67 | # next_finished_job to collect (one at a time) | ||
| 68 | $self->_reap_run( $run, $stat ); | ||
| 69 | $_next_to = 1; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | $self->warn( | ||
| 73 | "Reaper: kid: $kid, run: UNKNOWN, status: $stat" | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | $SIG{CHLD} = $self->_reaper; | ||
| 78 | } | ||
| 79 | }, | ||
| 80 | ); | ||
| 81 | |||
| 82 | method _reap_run => sub { | ||
| 83 | my $self = shift; | ||
| 84 | my $run = shift; | ||
| 85 | my $stat = shift; | ||
| 86 | my $caught = $self->_caught; | ||
| 87 | push @$caught, [ $run, $stat ]; | ||
| 88 | }; | ||
| 89 | |||
| 90 | method child_catcher => sub { | ||
| 91 | my $self = shift; | ||
| 92 | $SIG{CHLD} = $self->_reaper; | ||
| 93 | }; | ||
| 94 | |||
| 95 | method next_finished_job => sub { | ||
| 96 | my $self = shift; | ||
| 97 | my $caught = $self->_caught; | ||
| 98 | |||
| 99 | my ($status, $run); | ||
| 100 | |||
| 101 | while (1) { | ||
| 102 | $_next_to = undef; | ||
| 103 | if (@$caught) { | ||
| 104 | ($run, $status) = @{ shift @$caught }; | ||
| 105 | last; | ||
| 106 | } | ||
| 107 | |||
| 108 | $_next_to = $self->time_to_next_timeout; | ||
| 109 | if (defined $_next_to && $_next_to <= 0) { | ||
| 110 | $self->expire_timeouts; | ||
| 111 | next; | ||
| 112 | } | ||
| 113 | |||
| 114 | # the reaper might trigger at any time | ||
| 115 | # so there could now be a child in the caught list | ||
| 116 | # even though we just checked it above | ||
| 117 | # We use two methods to minimize the effects of an | ||
| 118 | # inopportune repear event. | ||
| 119 | # 1. we check caught again at the last moment before sleeping | ||
| 120 | # 2. limit the timeout to 5 minutes, so that we check again | ||
| 121 | # soon even if a termination slips in between the last | ||
| 122 | # moment check and the sleep, we don't sleep for days | ||
| 123 | # before checking again for completed children | ||
| 124 | # 3. while we are making the final check of caught, we store | ||
| 125 | # the timeout in $_next_to. The reaper | ||
| 126 | # resets that variable to 1 whenever it catches a child, | ||
| 127 | # so that reduces the sleep time to 1 second. (Zero | ||
| 128 | # would be nice to use, but POSIX doesn't specify what | ||
| 129 | # sleep does with a parameter of 0, so it is implementation | ||
| 130 | # defined and cannot be trusted.) This actually closes the | ||
| 131 | # critical section down (if a child terminates at the worst | ||
| 132 | # possible moment, we will sleep for 1 second because the | ||
| 133 | # reaper over-writes the $_next_to value), so, in theory, we | ||
| 134 | # could just sleep for the entire duration, but a 5 minute | ||
| 135 | # sleep provides a margin of safety, in case the reaper runs | ||
| 136 | # while we are getting the $_next_to # value but before we | ||
| 137 | # actuially use it for the sleep. | ||
| 138 | $_next_to = 5*60 unless defined $_next_to && $_next_to < 5*60; | ||
| 139 | # if reaper was triggered before this point, @caught will be filled | ||
| 140 | next if @$caught; | ||
| 141 | # if reaper is triggered between these two statements, it will | ||
| 142 | # set $_next_to to 1 | ||
| 143 | $self->can("_sleep") | ||
| 144 | ? $self->_sleep($_next_to) | ||
| 145 | : sleep $_next_to; | ||
| 146 | # if reaper triggers after the sleep has started, the sleep | ||
| 147 | # will be interrupted | ||
| 148 | } | ||
| 149 | |||
| 150 | $self->delete_timeouts( $run ); | ||
| 151 | |||
| 152 | $self->info( | ||
| 153 | qq{id finished: "} | ||
| 154 | . $run->unique_id | ||
| 155 | . qq{", with return value: $status} | ||
| 156 | ); | ||
| 157 | |||
| 158 | my $status_handler = $run->stage->_has_command | ||
| 159 | ? '_collect_job_stats' | ||
| 160 | : '_base_register_status'; | ||
| 161 | $run->$status_handler( $status ); | ||
| 162 | |||
| 163 | return $run; | ||
| 164 | }; | ||
| 165 | |||
| 166 | method submit_stage => sub { | ||
| 167 | my $self = shift; | ||
| 168 | my $stage = shift; | ||
| 169 | my $run = $stage->_run; | ||
| 170 | my $name = $run->unique_id; | ||
| 171 | $self->info("Submitting stage: name: $name"); | ||
| 172 | if ($stage->_has_command) { | ||
| 173 | my $command = $stage->command; | ||
| 174 | my ( $run_command, $soft_timeout, $hard_timeout ) = $run->_get_submit_command; | ||
| 175 | $self->info(" wrapped submit command: $run_command"); | ||
| 176 | $self->info(" actual user command: $command"); | ||
| 177 | $stage->_build_script_file; | ||
| 178 | my $pid = $run->_submit_command( $run_command ); | ||
| 179 | $self->info(" pid: $pid"); | ||
| 180 | $self->_runs->{$pid} = $run; | ||
| 181 | $run->pid( $pid ); | ||
| 182 | if ($soft_timeout) { | ||
| 183 | $self->add_timeout( $soft_timeout, $run, 'soft_timeout' ); | ||
| 184 | $self->info(" soft timeout: $soft_timeout"); | ||
| 185 | } | ||
| 186 | if ($hard_timeout) { | ||
| 187 | $self->add_timeout( $hard_timeout, $run, 'hard_timeout' ); | ||
| 188 | $self->info(" hard timeout: $hard_timeout"); | ||
| 189 | } | ||
| 190 | return $run; | ||
| 191 | } | ||
| 192 | else { # _has_code | ||
| 193 | my $stat = $stage->code->() || 0; | ||
| 194 | $self->_reap_run( $run, $stat ); | ||
| 195 | } | ||
| 196 | }; | ||
| 197 | |||
| 198 | method kill_stages => sub { | ||
| 199 | my $self = shift; | ||
| 200 | $self->info("Starting soft timeout of remaining processes"); | ||
| 201 | $self->info(Carp::longmess( "called soft_timeout" )); | ||
| 202 | $self->_kill_stages( 'soft_timeout', 20, '_kill_stages_2' ); | ||
| 203 | $self->info("Finished soft timeout"); | ||
| 204 | }; | ||
| 205 | |||
| 206 | method _kill_stages_2 => sub { | ||
| 207 | my $self = shift; | ||
| 208 | $self->error("Starting hard timeout of remaining processes - soft timeout didn't complete quickly enough"); | ||
| 209 | $self->_kill_stages( 'hard_timeout', 10, '_kill_stages_timed_out' ); | ||
| 210 | $self->info("Finished kill_stages_2"); | ||
| 211 | }; | ||
| 212 | |||
| 213 | method _kill_stages_timed_out => sub { | ||
| 214 | my $self = shift; | ||
| 215 | $self->fatal("Hard timeout took too long - aborting"); | ||
| 216 | exit(99); | ||
| 217 | }; | ||
| 218 | |||
| 219 | method _kill_stages => sub { | ||
| 220 | my $self = shift; | ||
| 221 | my $to_job_meth = shift; | ||
| 222 | my $time_allot = shift; | ||
| 223 | my $to_kill_meth = shift; | ||
| 224 | my @still_running = | ||
| 225 | grep { $_->stats->{exit_status} ne 'unknown' } | ||
| 226 | values %{ $self->_runs }; | ||
| 227 | my $cnt = scalar(@still_running); | ||
| 228 | $self->info( | ||
| 229 | "sending timeout signal ($to_job_meth) to $cnt remaining stages" | ||
| 230 | ); | ||
| 231 | |||
| 232 | if ($cnt) { | ||
| 233 | $_->$to_job_meth for @still_running; | ||
| 234 | $self->add_timeout( $time_allot, $self, $to_kill_meth ); | ||
| 235 | $self->next_finished_job while $cnt--; | ||
| 236 | } | ||
| 237 | }; | ||
| 238 | |||
| 239 | }; | ||
| 240 | |||
| 241 | 1; |