| 1 | |||
| 2 | package Proc::Parallel::Unreliable; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use IO::Event qw(emulate_Event); | ||
| 7 | use IO::Handle; | ||
| 8 | require Exporter; | ||
| 9 | use Hash::Util qw(lock_keys); | ||
| 10 | use POSIX ":sys_wait_h"; | ||
| 11 | use Scalar::Util qw(refaddr); | ||
| 12 | use File::Slurp; | ||
| 13 | |||
| 14 | our @ISA = qw(Exporter); | ||
| 15 | our @EXPORT = qw(run_subprocess); | ||
| 16 | |||
| 17 | sub run_subprocess | ||
| 18 | { | ||
| 19 | my (%params) = @_; | ||
| 20 | |||
| 21 | my $input = $params{input} || die; | ||
| 22 | my $ouptut = $params{output} || die; | ||
| 23 | my $cb = $params{cb} || die; | ||
| 24 | my $count = $params{count} ||= 1; | ||
| 25 | my $timeout = defined($params{timeout}) ? $params{timeout} : 60; | ||
| 26 | my $input_id = $params{input_id} ||= sub { die }; | ||
| 27 | my $output_id = $params{output_id} ||= sub { die }; | ||
| 28 | my $input_hlines = $params{input_hlines} ||= 0; | ||
| 29 | my $output_hlines = $params{output_hlines}||= 0; | ||
| 30 | my $items = $params{items} ||= 1; | ||
| 31 | my $track_items = $params{track_items} ||= 0; | ||
| 32 | my $debug = $params{debug} ||= 0; | ||
| 33 | |||
| 34 | $params{hlines_done} = 0; | ||
| 35 | |||
| 36 | local($SIG{USR1}) = sub { $debug++; }; | ||
| 37 | local($SIG{USR2}) = sub { $debug = 0 }; | ||
| 38 | |||
| 39 | my $self = bless \%params; | ||
| 40 | |||
| 41 | $self->{av0} = $0; | ||
| 42 | local($0) = "$0: control wrapper"; | ||
| 43 | local($SIG{PIPE}) = 'IGNORE'; | ||
| 44 | |||
| 45 | # | ||
| 46 | # Capture the input header | ||
| 47 | # | ||
| 48 | my @hlines; | ||
| 49 | for (1..$input_hlines) { | ||
| 50 | push(@hlines, scalar(<$input>)); | ||
| 51 | print STDERR "CAPTURE HEADER: $hlines[-1]" if $debug > 5; | ||
| 52 | } | ||
| 53 | |||
| 54 | $self->{hlines} = \@hlines; | ||
| 55 | $self->{alldone} = 0; | ||
| 56 | $self->{timeout} = $timeout; | ||
| 57 | $self->{children} = {}; | ||
| 58 | $self->{input_buffer} = []; | ||
| 59 | $self->{attempted} = 0; | ||
| 60 | $self->{completed} = 0; | ||
| 61 | $self->{input_items} = 0; | ||
| 62 | |||
| 63 | $self->{input_handler} = Proc::Parallel::Unreliable::Input->new($self); | ||
| 64 | |||
| 65 | my $last_complete = 0; | ||
| 66 | |||
| 67 | $self->{timer} = IO::Event->timer( | ||
| 68 | interval => $timeout / 5, | ||
| 69 | cb => sub { | ||
| 70 | print STDERR "[cntrl] Timer! alldone=$self->{alldone}\n" if $debug > 3; | ||
| 71 | for my $child (values %{$self->{children}}) { | ||
| 72 | $child->check_health; | ||
| 73 | $child->print_status if $debug > 4 || ($self->{alldone} && $debug); | ||
| 74 | } | ||
| 75 | use POSIX ":sys_wait_h"; | ||
| 76 | my $k; | ||
| 77 | do { $k = waitpid(-1, WNOHANG) } while $k > 0; | ||
| 78 | |||
| 79 | |||
| 80 | my $pending = 0; | ||
| 81 | my $dups_waiting = 0; | ||
| 82 | my $completed = 0; | ||
| 83 | for my $child (values %{$self->{children}}) { | ||
| 84 | $pending += keys %{$child->{pending}}; | ||
| 85 | $dups_waiting += keys %{$child->{duplicate_items}}; | ||
| 86 | $completed += $child->{completed}; | ||
| 87 | } | ||
| 88 | $0 = sprintf("%s: control wrapper: %d input %d completed %d pending %d dups waiting, %d finished recently", | ||
| 89 | $self->{av0}, | ||
| 90 | $self->{input_items}, | ||
| 91 | $completed, | ||
| 92 | $pending, | ||
| 93 | $dups_waiting, | ||
| 94 | $completed - $last_complete); | ||
| 95 | $last_complete = $completed; | ||
| 96 | }, | ||
| 97 | ); | ||
| 98 | |||
| 99 | lock_keys(%$self); | ||
| 100 | |||
| 101 | Proc::Parallel::Unreliable::Child->new($self) for 1..$self->{count}; | ||
| 102 | |||
| 103 | IO::Event::loop; | ||
| 104 | |||
| 105 | if ($self->{attempted}) { | ||
| 106 | printf STDERR "Processed %d items, dropped %d items (%.1f%%)\n", | ||
| 107 | $self->{completed}, | ||
| 108 | $self->{attempted}-$self->{completed}, | ||
| 109 | ($self->{attempted}-$self->{completed})/$self->{attempted}*100; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | sub send_data | ||
| 115 | { | ||
| 116 | my ($self, $one_child) = @_; | ||
| 117 | my $input_buffer = $self->{input_buffer}; | ||
| 118 | |||
| 119 | printf STDERR "[cntrl] send_data %d\n", scalar(@$input_buffer) if $self->{debug} > 4; | ||
| 120 | |||
| 121 | CHILD: | ||
| 122 | for my $child ($one_child ? ($one_child) : values %{$self->{children}}) { | ||
| 123 | while ($child->can_send && @$input_buffer) { | ||
| 124 | $child->send(pop(@$input_buffer)); | ||
| 125 | last CHILD unless @$input_buffer; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | $self->{input_handler}->readevents() if $one_child && ! $self->{alldone}; | ||
| 129 | if ($self->{alldone} && ! @$input_buffer) { | ||
| 130 | for my $child (values %{$self->{children}}) { | ||
| 131 | $child->no_more_input; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | sub dead_child | ||
| 137 | { | ||
| 138 | my ($self, $child, %data) = @_; | ||
| 139 | my $debug = $self->{debug}; | ||
| 140 | |||
| 141 | $self->{attempted} += $data{attempted} || 0; | ||
| 142 | $self->{completed} += $data{completed} || 0; | ||
| 143 | |||
| 144 | my $input_buffer = $self->{input_buffer}; | ||
| 145 | print STDERR "[$child->{pid}] dead_child\n" if $debug > 4; | ||
| 146 | printf STDERR "[cntrl] num children = %d\n", scalar(keys %{$self->{children}}) if $debug > 6; | ||
| 147 | delete $self->{children}{refaddr($child)}; | ||
| 148 | printf STDERR "[cntrl] num children = %d\n", scalar(keys %{$self->{children}}) if $debug > 4; | ||
| 149 | $self->write_pid_file(); | ||
| 150 | if ($self->{alldone} && ! @$input_buffer) { | ||
| 151 | if (keys %{$self->{children}}) { | ||
| 152 | print STDERR "[cntrl] waiting for children to finish\n" if $debug > 4; | ||
| 153 | } else { | ||
| 154 | print STDERR "[cntrl] UNLOOP!\n" if $debug > 1; | ||
| 155 | IO::Event::unloop_all; | ||
| 156 | } | ||
| 157 | } elsif (keys %{$self->{children}} < $self->{count}) { | ||
| 158 | print STDERR "[cntrl] start another kid\n" if $debug > 1; | ||
| 159 | Proc::Parallel::Unreliable::Child->new($self); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | sub write_pid_file | ||
| 164 | { | ||
| 165 | my $self = shift; | ||
| 166 | return unless $self->{pidfile}; | ||
| 167 | overwrite_file($self->{pidfile}, join("\n", $$, map { $_->{pid} } values %{$self->{children}}), "\n"); | ||
| 168 | } | ||
| 169 | |||
| 170 | package Proc::Parallel::Unreliable::Input; | ||
| 171 | |||
| 172 | use strict; | ||
| 173 | use warnings; | ||
| 174 | use Scalar::Util qw(weaken); | ||
| 175 | use Hash::Util qw(lock_keys); | ||
| 176 | |||
| 177 | sub new | ||
| 178 | { | ||
| 179 | my ($pkg, $control) = @_; | ||
| 180 | |||
| 181 | $control->{input}->blocking(0); | ||
| 182 | |||
| 183 | my $self = bless { | ||
| 184 | control => $control, | ||
| 185 | input_buffer => $control->{input_buffer}, | ||
| 186 | input_max => ($control->{items} || 1) * ($control->{count} || 1) * 2, | ||
| 187 | debug => $control->{debug}, | ||
| 188 | }; | ||
| 189 | |||
| 190 | $self->{ioe} = IO::Event->new($control->{input}, $self); | ||
| 191 | |||
| 192 | lock_keys(%$self); | ||
| 193 | |||
| 194 | return $self; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub ie_input | ||
| 198 | { | ||
| 199 | my ($self, $ioe, $ibr) = @_; | ||
| 200 | return unless length($$ibr); | ||
| 201 | my $control = $self->{control}; | ||
| 202 | my $input_buffer = $self->{input_buffer}; | ||
| 203 | |||
| 204 | for(;;) { | ||
| 205 | my $input = <$ioe>; | ||
| 206 | last unless $input; | ||
| 207 | push(@$input_buffer, \$input); | ||
| 208 | $control->{input_items}++; | ||
| 209 | print STDERR "[cntrl] input read\n" if $self->{debug} > 8; | ||
| 210 | } | ||
| 211 | $control->send_data(); | ||
| 212 | $self->readevents; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub readevents | ||
| 216 | { | ||
| 217 | my ($self) = @_; | ||
| 218 | my $input_buffer = $self->{input_buffer}; | ||
| 219 | $self->{ioe}->readevents(@$input_buffer <= $self->{input_max}); | ||
| 220 | printf STDERR "[cntrl] readevents = %s\n", (@$input_buffer <= $self->{input_max}) ? 'On' : 'Off' if $self->{debug} > 7; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub ie_eof | ||
| 224 | { | ||
| 225 | my ($self, $ioe, $ibr) = @_; | ||
| 226 | my $control = $self->{control}; | ||
| 227 | $control->{alldone} = 1; | ||
| 228 | $self->ie_input($ioe, $ibr); | ||
| 229 | my $debug = $self->{debug}; | ||
| 230 | print STDERR "[cntrl] NO MORE INPUT\n" if $debug; | ||
| 231 | # $ioe->close(); | ||
| 232 | $ioe->readevents(0); | ||
| 233 | weaken($self->{control}); | ||
| 234 | } | ||
| 235 | |||
| 236 | package Proc::Parallel::Unreliable::Child; | ||
| 237 | |||
| 238 | use strict; | ||
| 239 | use warnings; | ||
| 240 | use Time::HiRes qw(time); | ||
| 241 | use Hash::Util qw(lock_keys); | ||
| 242 | use Scalar::Util qw(weaken refaddr); | ||
| 243 | use Socket; | ||
| 244 | use List::Util qw(min); | ||
| 245 | |||
| 246 | sub new | ||
| 247 | { | ||
| 248 | my ($pkg, $control) = @_; | ||
| 249 | |||
| 250 | my $childfh = new IO::Handle; | ||
| 251 | my $parentfh = new IO::Handle; | ||
| 252 | |||
| 253 | socketpair($childfh, $parentfh, AF_UNIX, SOCK_STREAM, PF_UNSPEC) | ||
| 254 | or die "socketpair: $!"; | ||
| 255 | |||
| 256 | my $pid = fork(); | ||
| 257 | if (! defined($pid)) { | ||
| 258 | die "cannot fork: $!"; | ||
| 259 | } elsif ($pid == 0) { | ||
| 260 | |||
| 261 | # child | ||
| 262 | |||
| 263 | $0 = "$control->{av0}: worker"; | ||
| 264 | $control->{timer}->cancel(); | ||
| 265 | IO::Event::unloop_all; | ||
| 266 | $childfh->close(); | ||
| 267 | $parentfh->autoflush(1); | ||
| 268 | close(STDIN); | ||
| 269 | close(STDOUT); | ||
| 270 | open STDIN, "<&", \$parentfh or die "dup onto STDIN: $!"; | ||
| 271 | open STDOUT, ">&", \$parentfh or die "dup onto STDOUT: $!"; | ||
| 272 | select($parentfh); | ||
| 273 | print STDERR "[$$] Invoking callback now\n" if $control->{debug}; | ||
| 274 | $control->{cb}->($parentfh, $parentfh); | ||
| 275 | print STDERR "[$$] callback finished\n" if $control->{debug}; | ||
| 276 | POSIX::_exit(0); | ||
| 277 | } | ||
| 278 | |||
| 279 | # parent | ||
| 280 | |||
| 281 | $parentfh->close(); | ||
| 282 | $childfh->blocking(0); | ||
| 283 | $childfh->autoflush(1); | ||
| 284 | |||
| 285 | print STDERR "PRINT TO CHILD: @{$control->{hlines}}\n" if $control->{debug} > 7 && @{$control->{hlines}}; | ||
| 286 | print $childfh @{$control->{hlines}} if @{$control->{hlines}}; | ||
| 287 | |||
| 288 | my $header = $control->{output_hlines} && ! $control->{hlines_done}; | ||
| 289 | |||
| 290 | my $self = bless { | ||
| 291 | pid => $pid, | ||
| 292 | inflight => 0, | ||
| 293 | pending => {}, | ||
| 294 | pending_times => {}, | ||
| 295 | pending_order => {}, | ||
| 296 | control => $control, | ||
| 297 | overflow => 0, | ||
| 298 | last_output => 0, | ||
| 299 | last_input => time, | ||
| 300 | track_items => $control->{track_items}, | ||
| 301 | shutdown => 0, | ||
| 302 | header => $header, | ||
| 303 | output_hlines => $control->{output_hlines}, | ||
| 304 | debug => $control->{debug}, | ||
| 305 | duplicate_items => {}, | ||
| 306 | closing_down => 0, | ||
| 307 | seqno => 0, | ||
| 308 | completed => 0, | ||
| 309 | timebomb => 0, | ||
| 310 | }; | ||
| 311 | |||
| 312 | print STDERR "[$$] Started slave $self->{pid}\n"; | ||
| 313 | |||
| 314 | $self->{ioe} = IO::Event->new($childfh, $self); | ||
| 315 | |||
| 316 | lock_keys(%$self); | ||
| 317 | |||
| 318 | printf STDERR "[$pid] Looking for header: %s\n", $header ? 'Yes' : 'No' if $control->{debug} > 5; | ||
| 319 | |||
| 320 | $control->{children}{refaddr($self)} = $self; | ||
| 321 | |||
| 322 | $control->write_pid_file(); | ||
| 323 | |||
| 324 | $control->send_data($self); | ||
| 325 | |||
| 326 | return $self; | ||
| 327 | }; | ||
| 328 | |||
| 329 | sub can_send | ||
| 330 | { | ||
| 331 | my ($self) = @_; | ||
| 332 | return 0 if $self->{shutdown}; | ||
| 333 | return 0 if $self->{closing_down}; | ||
| 334 | return 0 if $self->{overflow}; | ||
| 335 | return 0 if $self->{track_items} && $self->{inflight} >= $self->{control}{items}; | ||
| 336 | |||
| 337 | # | ||
| 338 | # Since recent output will case check_health to pass, we don't want to send any output | ||
| 339 | # if we're not sure we're healthy | ||
| 340 | # | ||
| 341 | return 0 if (! $self->{track_items}) && $self->{last_output} > $self->{last_input} + $self->{control}{timeout}; | ||
| 342 | |||
| 343 | my $if = $self->{track_items} ? "($self->{inflight} in flight)" : "(not overflowed)"; | ||
| 344 | print STDERR "[$self->{pid}] ready to send $if\n" if $self->{debug} > 5; | ||
| 345 | return 1; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub send | ||
| 349 | { | ||
| 350 | my ($self, $iref) = @_; | ||
| 351 | my $ioe = $self->{ioe}; | ||
| 352 | my $debug = $self->{debug}; | ||
| 353 | if ($self->{track_items}) { | ||
| 354 | my $id = $self->{control}{input_id}->($iref); | ||
| 355 | print STDERR "[$self->{pid}] SEND $id\n" if $debug > 3; | ||
| 356 | if (defined($id)) { | ||
| 357 | if ($self->{pending}{$id}) { | ||
| 358 | print STDERR "[$self->{pid}] Deferring processing of duplicate item $id\n"; | ||
| 359 | $self->{duplicate_items}{$id} = [] | ||
| 360 | unless $self->{duplicate_items}{$id}; | ||
| 361 | push(@{$self->{duplicate_items}{$id}}, $iref); | ||
| 362 | return; | ||
| 363 | } else { | ||
| 364 | $self->{inflight}++; | ||
| 365 | $self->{pending}{$id} = $iref; | ||
| 366 | $self->{pending_times}{$id} = time; | ||
| 367 | $self->{pending_order}{$id} = ++$self->{seqno}; | ||
| 368 | } | ||
| 369 | } else { | ||
| 370 | warn "Unable to determine item id, turning off item tracking"; | ||
| 371 | delete $self->{track_items}; | ||
| 372 | } | ||
| 373 | } elsif ($debug > 5) { | ||
| 374 | print STDERR "[$self->{pid}] sending\n"; | ||
| 375 | } | ||
| 376 | |||
| 377 | $self->{last_output} = time; | ||
| 378 | unless (print $ioe $$iref) { | ||
| 379 | warn "[$self->{pid}] write failed: $!"; | ||
| 380 | $self->ie_werror($ioe); | ||
| 381 | } | ||
| 382 | } | ||
| 383 | |||
| 384 | sub no_more_input | ||
| 385 | { | ||
| 386 | my ($self) = @_; | ||
| 387 | if (keys %{$self->{duplicate_items}} && $self->{track_items}) { | ||
| 388 | print STDERR "[$self->{pid}] ignoring shutdown for the time being\n" if $self->{debug} > 3; | ||
| 389 | } else { | ||
| 390 | return if $self->{shutdown}++; | ||
| 391 | print STDERR "[$self->{pid}] shutting down\n" if $self->{debug} > 3; | ||
| 392 | $self->{ioe}->shutdown(1); | ||
| 393 | $self->{timebomb} = time unless $self->{timebomb}; | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | sub ie_input | ||
| 398 | { | ||
| 399 | my ($self, $ioe, $ibr) = @_; | ||
| 400 | my $control = $self->{control}; | ||
| 401 | my $debug = $self->{debug}; | ||
| 402 | if ($self->{header} && ! $control->{hlines_done}) { | ||
| 403 | # do we have enough header? | ||
| 404 | my $cnt = $$ibr =~ tr/\n/\n/; | ||
| 405 | if ($cnt < $control->{output_hlines}) { | ||
| 406 | print STDERR "[$self->{pid}] Don't have enough header yet (%d vs %d)\n", $cnt, $control->{output_hlines} if $debug > 2; | ||
| 407 | $control->send_data($self) if $self->can_send; | ||
| 408 | return; | ||
| 409 | } | ||
| 410 | print STDERR "[$self->{pid}] we have enough lines for a header\n" if $debug > 5; | ||
| 411 | } | ||
| 412 | while (<$ioe>) { | ||
| 413 | print STDERR "[$self->{pid}] READ $_" if $debug > 8; | ||
| 414 | if ($self->{output_hlines}) { | ||
| 415 | $self->{output_hlines}--; | ||
| 416 | printf STDERR "[$self->{pid}] FOUND HEADER lines remaining: %d, want header: %s\n", $self->{output_hlines}, $control->{hlines_done} ? 'Y' : 'N' if $debug > 4; | ||
| 417 | next if $control->{hlines_done}; | ||
| 418 | # because of the test up above, and because we're single-threaded, we know | ||
| 419 | # that we will be able to output the entire header w/o anyone else running. | ||
| 420 | $control->{output}->($_); | ||
| 421 | next unless $self->{output_hlines} == 0; | ||
| 422 | $control->{hlines_done} = $$; | ||
| 423 | next; | ||
| 424 | } | ||
| 425 | if ($self->{track_items}) { | ||
| 426 | $self->{inflight}--; | ||
| 427 | my $id = $control->{output_id}->(\$_); | ||
| 428 | print STDERR "[$self->{pid}] GOT $id\n" if $debug > 3; | ||
| 429 | delete $self->{pending}{$id} | ||
| 430 | or warn "no pending '$id' to delete"; | ||
| 431 | delete $self->{pending_times}{$id}; | ||
| 432 | my $seq = delete $self->{pending_order}{$id}; | ||
| 433 | for my $i (grep { $self->{pending_order}{$_} < $seq } keys %{$self->{pending_order}}) { | ||
| 434 | print STDERR "[$self->{pid}] No results for $i\n"; | ||
| 435 | delete $self->{pending}{$i}; | ||
| 436 | delete $self->{pending_order}{$i}; | ||
| 437 | delete $self->{pending_times}{$i}; | ||
| 438 | delete $self->{duplicate_items}{$i}; # don't retry dups | ||
| 439 | } | ||
| 440 | if ($self->{duplicate_items}{$id}) { | ||
| 441 | print STDERR "[$self->{pid}] Requeuing duplicate item $id\n"; | ||
| 442 | push(@{$control->{input_buffer}}, @{$self->{duplicate_items}{$id}}); | ||
| 443 | delete $self->{duplicate_items}{$id}; | ||
| 444 | } | ||
| 445 | $self->{completed}++; | ||
| 446 | } | ||
| 447 | $control->{output}->($_); | ||
| 448 | $self->{last_input} = time; | ||
| 449 | } | ||
| 450 | $control->send_data($self) if $self->can_send; | ||
| 451 | } | ||
| 452 | |||
| 453 | sub ie_eof | ||
| 454 | { | ||
| 455 | my ($self, $ioe, $ibr) = @_; | ||
| 456 | $self->ie_input($ioe, $ibr); | ||
| 457 | my $debug = $self->{debug}; | ||
| 458 | print STDERR "[$self->{pid}] EOF\n" if $debug > 2; | ||
| 459 | $self->closedown('EOF'); | ||
| 460 | } | ||
| 461 | |||
| 462 | sub print_status | ||
| 463 | { | ||
| 464 | my ($self) = @_; | ||
| 465 | printf STDERR "[%d] Status. track_items=%s inflight=%d pending=%d overflow=%s shutdown=%d closing_down=%d duplicates=%d last_output=%d oldest_item=%d last_input=%d timebomb=%d\n", | ||
| 466 | $self->{pid}, | ||
| 467 | $self->{track_items}, | ||
| 468 | $self->{inflight}, | ||
| 469 | scalar(keys %{$self->{pending}}), | ||
| 470 | $self->{overflow}, | ||
| 471 | $self->{shutdown}, | ||
| 472 | $self->{closing_down}, | ||
| 473 | scalar(keys %{$self->{duplicate_items}}), | ||
| 474 | time - $self->{last_output}, | ||
| 475 | time - min(time, values %{$self->{pending_times}}), | ||
| 476 | time - $self->{last_input}, | ||
| 477 | ($self->{timebomb} ? time - $self->{timebomb} : 0); | ||
| 478 | } | ||
| 479 | |||
| 480 | # | ||
| 481 | # Look for hung workers | ||
| 482 | # last_output is the time something was sent to the worker | ||
| 483 | # last_input is the time something was received from the worker | ||
| 484 | # | ||
| 485 | sub check_health | ||
| 486 | { | ||
| 487 | my ($self) = @_; | ||
| 488 | my $control = $self->{control}; | ||
| 489 | my $debug = $self->{debug}; | ||
| 490 | |||
| 491 | # | ||
| 492 | # An attempt to work around an odd race-condition hang. | ||
| 493 | # | ||
| 494 | if ($self->{timebomb} and time - $self->{timebomb} > $control->{timeout} * ($self->{control}{items} + 1)) { | ||
| 495 | print STDERR "[$self->{pid}] FAILED HEALTH CHECK - timebomb\n" if $debug; | ||
| 496 | $self->ie_werror; | ||
| 497 | return; | ||
| 498 | } | ||
| 499 | |||
| 500 | # | ||
| 501 | # We've never sent any output. The master can shut us down at any time. | ||
| 502 | # | ||
| 503 | return unless $self->{last_output}; | ||
| 504 | |||
| 505 | # | ||
| 506 | # We're already in the process of closing down. The master already knows and | ||
| 507 | # wont wait for us. | ||
| 508 | # | ||
| 509 | return if $self->{closing_down}; | ||
| 510 | |||
| 511 | # | ||
| 512 | # We've recevied input recently, obviously nothing is wrong. | ||
| 513 | # | ||
| 514 | return if time - $self->{last_input} < $control->{timeout}; | ||
| 515 | |||
| 516 | if ($self->{track_items}) { | ||
| 517 | # | ||
| 518 | # We're tracking individual items. We're in trouble only | ||
| 519 | # if a particular item is overdue. | ||
| 520 | # | ||
| 521 | my $bad; | ||
| 522 | for my $i (values %{$self->{pending_times}}) { | ||
| 523 | next if time - $i < $control->{timeout}; | ||
| 524 | $bad = $i; | ||
| 525 | } | ||
| 526 | return unless $bad; | ||
| 527 | } else { | ||
| 528 | # | ||
| 529 | # We aren't tracking items. Since we had input more recently | ||
| 530 | # than we sent something, we're probably still okay. | ||
| 531 | # | ||
| 532 | return if $self->{last_input} > $self->{last_output}; | ||
| 533 | |||
| 534 | # | ||
| 535 | # We only did some output recently, so maybe we haven't had any | ||
| 536 | # input yet? can_send() prevents us from sending and sending | ||
| 537 | # even though we haven't had any input. | ||
| 538 | # | ||
| 539 | return if time - $self->{last_output} < $control->{timeout}; | ||
| 540 | } | ||
| 541 | print STDERR "[$self->{pid}] FAILED HEALTH CHECK\n"; | ||
| 542 | $self->ie_werror; | ||
| 543 | } | ||
| 544 | |||
| 545 | sub requeue_outstanding | ||
| 546 | { | ||
| 547 | my ($self, $reason) = @_; | ||
| 548 | my $control = $self->{control}; | ||
| 549 | if ($self->{track_items}) { | ||
| 550 | my $pending = $self->{pending}; | ||
| 551 | my $porder = $self->{pending_order}; | ||
| 552 | my @ilist = sort { $porder->{$a} <=> $porder->{$b} } keys %$pending; | ||
| 553 | my $bad = shift(@ilist); | ||
| 554 | warn "[$self->{pid}] Slave process failed at $reason, probably working on '$bad'\n" if $bad; | ||
| 555 | push(@{$control->{input_buffer}}, map { $pending->{$_} } @ilist); | ||
| 556 | } | ||
| 557 | $self->{pending} = {}; | ||
| 558 | $self->{pending_times} = {}; | ||
| 559 | $self->{pending_order} = {}; | ||
| 560 | } | ||
| 561 | |||
| 562 | sub closedown | ||
| 563 | { | ||
| 564 | my ($self, $reason) = @_; | ||
| 565 | my $control = $self->{control}; | ||
| 566 | $self->requeue_outstanding($reason); | ||
| 567 | $control->dead_child($self, attempted => $self->{seqno}, completed => $self->{completed}) if $control; | ||
| 568 | return if $self->{closing_down}++; | ||
| 569 | $self->{ioe}->close; | ||
| 570 | eval { weaken($self->{control}); }; | ||
| 571 | } | ||
| 572 | |||
| 573 | sub ie_werror | ||
| 574 | { | ||
| 575 | my ($self) = @_; | ||
| 576 | $self->closedown('write error'); | ||
| 577 | if (kill(0, $self->{pid})) { | ||
| 578 | print STDERR "Killing child $self->{pid}\n"; | ||
| 579 | if (kill(15, $self->{pid})) { | ||
| 580 | sleep(1); | ||
| 581 | kill(9, $self->{pid}); | ||
| 582 | } | ||
| 583 | } | ||
| 584 | } | ||
| 585 | |||
| 586 | sub ie_outputoverflow | ||
| 587 | { | ||
| 588 | my ($self, $ioe, $overflow) = @_; | ||
| 589 | print STDERR "[$self->{pid}] overflow=$overflow\n" if $self->{debug} > 4; | ||
| 590 | $self->{overflow} = $overflow; | ||
| 591 | } | ||
| 592 | |||
| 593 | 1; | ||
| 594 | |||
| 595 | __END__ |