tweak node failure detection
[arvados.git] / services / crunch / crunch-job
index 29ee98d355e1194508ce421e6df30649cd462654..60aadcc7a572365ecf80c6dda5eb8355754bd167 100755 (executable)
@@ -55,6 +55,7 @@ use Arvados;
 use Getopt::Long;
 use Warehouse;
 use Warehouse::Stream;
+use IPC::System::Simple qw(capturex);
 
 $ENV{"TMPDIR"} ||= "/tmp";
 $ENV{"CRUNCH_TMP"} = $ENV{"TMPDIR"} . "/crunch-job";
@@ -275,9 +276,9 @@ else
     'qsequence' => 0,
     'parameters' => {},
                                                           });
-  push @jobstep, { level => 0,
-                  attempts => 0,
-                   arvados_task => $first_task,
+  push @jobstep, { 'level' => 0,
+                  'attempts' => 0,
+                   'arvados_task' => $first_task,
                 };
   push @jobstep_todo, 0;
 }
@@ -627,7 +628,8 @@ for (my $todo_ptr = 0; $todo_ptr <= $#jobstep_todo; $todo_ptr ++)
     }
 
     # give up if no nodes are succeeding
-    if (!grep { $_->{node}->{losing_streak} == 0 } @slot) {
+    if (!grep { $_->{node}->{losing_streak} == 0 &&
+                    $_->{node}->{hold_count} < 4 } @slot) {
       my $message = "Every node has failed -- giving up on this round";
       Log (undef, $message);
       last THISROUND;
@@ -681,23 +683,25 @@ goto ONELEVEL if !defined $success;
 release_allocation();
 freeze();
 $Job->{'output'} = &collate_output();
-$Job->{'success'} = 0 if !$Job->{'output'};
+$Job->{'success'} = $Job->{'output'} && $success;
 $Job->save;
 
 if ($Job->{'output'})
 {
-  $arv->{'collections'}->{'create'}->execute('collection' => {
-    'uuid' => $Job->{'output'},
-    'manifest_text' => system("whget", $Job->{arvados_task}->{output}),
-                                             });;
+  eval {
+    my $manifest_text = capturex("whget", $Job->{'output'});
+    $arv->{'collections'}->{'create'}->execute('collection' => {
+      'uuid' => $Job->{'output'},
+      'manifest_text' => $manifest_text,
+    });
+  };
+  if ($@) {
+    Log (undef, "Failed to register output manifest: $@");
+  }
 }
 
-
 Log (undef, "finish");
 
-$Job->{'success'} = $Job->{'output'} && $success;
-$Job->save;
-
 save_meta();
 exit 0;
 
@@ -735,21 +739,23 @@ sub reapchildren
 
   my $exitcode = $?;
   my $exitinfo = "exit $exitcode";
-  $Jobstep->{arvados_task}->reload;
-  my $success = $Jobstep->{arvados_task}->{success};
+  $Jobstep->{'arvados_task'}->reload;
+  my $success = $Jobstep->{'arvados_task'}->{success};
 
   Log ($jobstepid, "child $pid on $whatslot $exitinfo success=$success");
 
   if (!defined $success) {
     # task did not indicate one way or the other --> fail
-    $Jobstep->{arvados_task}->{success} = 0;
-    $Jobstep->{arvados_task}->save;
+    $Jobstep->{'arvados_task'}->{success} = 0;
+    $Jobstep->{'arvados_task'}->save;
     $success = 0;
   }
 
   if (!$success)
   {
-    --$Jobstep->{attempts} if $Jobstep->{node_fail};
+    my $no_incr_attempts;
+    $no_incr_attempts = 1 if $Jobstep->{node_fail};
+
     ++$thisround_failed;
     ++$thisround_failed_multiple if $Jobstep->{attempts} > 1;
 
@@ -762,6 +768,7 @@ sub reapchildren
          $elapsed < 5 &&
          $Jobstep->{attempts} > 1) {
        Log ($jobstepid, "blaming failure on suspect node " . $slot[$proc{$pid}->{slot}]->{node}->{name} . " instead of incrementing jobstep attempts");
+        $no_incr_attempts = 1;
        --$Jobstep->{attempts};
       }
       ban_node_by_slot($proc{$pid}->{slot});
@@ -769,6 +776,8 @@ sub reapchildren
 
     push @jobstep_todo, $jobstepid;
     Log ($jobstepid, "failure in $elapsed seconds");
+
+    --$Jobstep->{attempts} if $no_incr_attempts;
     $Job->{'tasks_summary'}->{'failed'}++;
   }
   else
@@ -782,7 +791,7 @@ sub reapchildren
   $Jobstep->{exitcode} = $exitcode;
   $Jobstep->{finishtime} = time;
   process_stderr ($jobstepid, $success);
-  Log ($jobstepid, "output " . $Jobstep->{arvados_task}->{output});
+  Log ($jobstepid, "output " . $Jobstep->{'arvados_task'}->{output});
 
   close $reader{$jobstepid};
   delete $reader{$jobstepid};
@@ -793,7 +802,7 @@ sub reapchildren
   # Load new tasks
   my $newtask_list = $arv->{'job_tasks'}->{'list'}->execute(
     'where' => {
-      'created_by_job_task' => $Jobstep->{arvados_task}->{uuid}
+      'created_by_job_task' => $Jobstep->{'arvados_task'}->{uuid}
     },
     'order' => 'qsequence'
   );
@@ -918,12 +927,9 @@ sub preprocess_stderr
 
   while ($jobstep[$job]->{stderr} =~ /^(.*?)\n/) {
     my $line = $1;
-    if ($line =~ /\+\+\+mr/) {
-      last;
-    }
     substr $jobstep[$job]->{stderr}, 0, 1+length($line), "";
     Log ($job, "stderr $line");
-    if ($line =~ /srun: error: (SLURM job $ENV{SLURM_JOBID} has expired) /) {
+    if ($line =~ /srun: error: (SLURM job $ENV{SLURM_JOBID} has expired|Unable to confirm allocation for job) /) {
       # whoa.
       $main::please_freeze = 1;
     }
@@ -955,8 +961,10 @@ sub collate_output
   my $joboutput;
   for (@jobstep)
   {
-    next if !exists $_->{arvados_task}->{output} || $_->{exitcode} != 0;
-    my $output = $_->{arvados_task}->{output};
+    next if (!exists $_->{'arvados_task'}->{output} ||
+             !$_->{'arvados_task'}->{'success'} ||
+             $_->{'exitcode'} != 0);
+    my $output = $_->{'arvados_task'}->{output};
     if ($output !~ /^[0-9a-f]{32}(\+\S+)*$/)
     {
       $output_in_keep ||= $output =~ / [0-9a-f]{32}\S*\+K/;
@@ -1324,6 +1332,7 @@ sub ban_node_by_slot {
   # Don't start any new jobsteps on this node for 60 seconds
   my $slotid = shift;
   $slot[$slotid]->{node}->{hold_until} = 60 + scalar time;
+  $slot[$slotid]->{node}->{hold_count}++;
   Log (undef, "backing off node " . $slot[$slotid]->{node}->{name} . " for 60 seconds");
 }