2 # -*- mode: perl; perl-indent-level: 2; -*-
6 crunch-job: Execute job steps, save snapshots as requested, collate output.
10 Obtain job details from Arvados, run tasks on compute nodes (typically
11 invoked by scheduler on controller):
13 crunch-job --job x-y-z
15 Obtain job details from command line, run tasks on local machine
16 (typically invoked by application or developer on VM):
18 crunch-job --job '{"script_version":"/path/to/tree","script":"scriptname",...}'
26 If the job is already locked, steal the lock and run it anyway.
30 Path to .git directory where the specified commit is found.
34 Arvados API authorization token to use during the course of the job.
38 =head1 RUNNING JOBS LOCALLY
40 crunch-job's log messages appear on stderr along with the job tasks'
41 stderr streams. The log is saved in Keep at each checkpoint and when
44 If the job succeeds, the job's output locator is printed on stdout.
46 While the job is running, the following signals are accepted:
50 =item control-C, SIGINT, SIGQUIT
52 Save a checkpoint, terminate any job tasks that are running, and stop.
56 Save a checkpoint and continue.
60 Refresh node allocation (i.e., check whether any nodes have been added
61 or unallocated) and attributes of the Job record that should affect
62 behavior (e.g., cancel job if cancelled_at becomes non-nil).
70 use POSIX ':sys_wait_h';
71 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
77 $ENV{"TMPDIR"} ||= "/tmp";
78 unless (defined $ENV{"CRUNCH_TMP"}) {
79 $ENV{"CRUNCH_TMP"} = $ENV{"TMPDIR"} . "/crunch-job";
80 if ($ENV{"USER"} ne "crunch" && $< != 0) {
81 # use a tmp dir unique for my uid
82 $ENV{"CRUNCH_TMP"} .= "-$<";
85 $ENV{"JOB_WORK"} = $ENV{"CRUNCH_TMP"} . "/work";
86 $ENV{"CRUNCH_INSTALL"} = "$ENV{CRUNCH_TMP}/opt";
87 $ENV{"CRUNCH_WORK"} = $ENV{"JOB_WORK"}; # deprecated
88 mkdir ($ENV{"JOB_WORK"});
95 GetOptions('force-unlock' => \$force_unlock,
96 'git-dir=s' => \$git_dir,
98 'job-api-token=s' => \$job_api_token,
99 'resume-stash=s' => \$resume_stash,
102 if (defined $job_api_token) {
103 $ENV{ARVADOS_API_TOKEN} = $job_api_token;
106 my $have_slurm = exists $ENV{SLURM_JOBID} && exists $ENV{SLURM_NODELIST};
107 my $job_has_uuid = $jobspec =~ /^[-a-z\d]+$/;
108 my $local_job = !$job_has_uuid;
113 $main::ENV{CRUNCH_DEBUG} = 1;
117 $main::ENV{CRUNCH_DEBUG} = 0;
122 my $arv = Arvados->new('apiVersion' => 'v1');
125 my $User = $arv->{'users'}->{'current'}->execute;
133 $Job = $arv->{'jobs'}->{'get'}->execute('uuid' => $jobspec);
134 if (!$force_unlock) {
135 if ($Job->{'is_locked_by_uuid'}) {
136 croak("Job is locked: " . $Job->{'is_locked_by_uuid'});
138 if ($Job->{'success'} ne undef) {
139 croak("Job 'success' flag (" . $Job->{'success'} . ") is not null");
141 if ($Job->{'running'}) {
142 croak("Job 'running' flag is already set");
144 if ($Job->{'started_at'}) {
145 croak("Job 'started_at' time is already set (" . $Job->{'started_at'} . ")");
151 $Job = JSON::decode_json($jobspec);
155 map { croak ("No $_ specified") unless $Job->{$_} }
156 qw(script script_version script_parameters);
159 $Job->{'is_locked_by_uuid'} = $User->{'uuid'};
160 $Job->{'started_at'} = gmtime;
162 $Job = $arv->{'jobs'}->{'create'}->execute('job' => $Job);
166 $job_id = $Job->{'uuid'};
168 # $metastream = Warehouse::Stream->new(whc => new Warehouse);
169 # $metastream->clear;
170 # $metastream->name('.');
171 # $metastream->write_start($job_id . '.log.txt');
174 $Job->{'runtime_constraints'} ||= {};
175 $Job->{'runtime_constraints'}->{'max_tasks_per_node'} ||= 0;
176 my $max_ncpus = $Job->{'runtime_constraints'}->{'max_tasks_per_node'};
179 Log (undef, "check slurm allocation");
182 # Should use $ENV{SLURM_TASKS_PER_NODE} instead of sinfo? (eg. "4(x3),2,4(x2)")
186 my $localcpus = 0 + `grep -cw ^processor /proc/cpuinfo` || 1;
187 push @sinfo, "$localcpus localhost";
189 if (exists $ENV{SLURM_NODELIST})
191 push @sinfo, `sinfo -h --format='%c %N' --nodes='$ENV{SLURM_NODELIST}'`;
195 my ($ncpus, $slurm_nodelist) = split;
196 $ncpus = $max_ncpus if $max_ncpus && $ncpus > $max_ncpus;
199 while ($slurm_nodelist =~ s/^([^\[,]+?(\[.*?\])?)(,|$)//)
202 if ($nodelist =~ /\[((\d+)(-(\d+))?(,(\d+)(-(\d+))?)*)\]/)
205 foreach (split (",", $ranges))
218 push @nodelist, map {
220 $n =~ s/\[[-,\d]+\]/$_/;
227 push @nodelist, $nodelist;
230 foreach my $nodename (@nodelist)
232 Log (undef, "node $nodename - $ncpus slots");
233 my $node = { name => $nodename,
237 foreach my $cpu (1..$ncpus)
239 push @slot, { node => $node,
243 push @node, @nodelist;
248 # Ensure that we get one jobstep running on each allocated node before
249 # we start overloading nodes with concurrent steps
251 @slot = sort { $a->{cpu} <=> $b->{cpu} } @slot;
258 # Claim this job, and make sure nobody else does
259 unless ($Job->update_attributes('is_locked_by_uuid' => $User->{'uuid'}) &&
260 $Job->{'is_locked_by_uuid'} == $User->{'uuid'}) {
261 croak("Error while updating / locking job");
263 $Job->update_attributes('started_at' => scalar gmtime,
266 'tasks_summary' => { 'failed' => 0,
273 Log (undef, "start");
274 $SIG{'INT'} = sub { $main::please_freeze = 1; };
275 $SIG{'QUIT'} = sub { $main::please_freeze = 1; };
276 $SIG{'TERM'} = \&croak;
277 $SIG{'TSTP'} = sub { $main::please_freeze = 1; };
278 $SIG{'ALRM'} = sub { $main::please_info = 1; };
279 $SIG{'CONT'} = sub { $main::please_continue = 1; };
280 $SIG{'HUP'} = sub { $main::please_refresh = 1; };
282 $main::please_freeze = 0;
283 $main::please_info = 0;
284 $main::please_continue = 0;
285 $main::please_refresh = 0;
286 my $jobsteps_must_output_keys = 0; # becomes 1 when any task outputs a key
288 grep { $ENV{$1} = $2 if /^(NOCACHE.*?)=(.*)/ } split ("\n", $$Job{knobs});
289 $ENV{"CRUNCH_JOB_UUID"} = $job_id;
290 $ENV{"JOB_UUID"} = $job_id;
294 my @jobstep_todo = ();
295 my @jobstep_done = ();
296 my @jobstep_tomerge = ();
297 my $jobstep_tomerge_level = 0;
299 my $squeue_kill_checked;
300 my $output_in_keep = 0;
301 my $latest_refresh = scalar time;
305 if (defined $Job->{thawedfromkey})
307 thaw ($Job->{thawedfromkey});
311 my $first_task = $arv->{'job_tasks'}->{'create'}->execute('job_task' => {
312 'job_uuid' => $Job->{'uuid'},
317 push @jobstep, { 'level' => 0,
319 'arvados_task' => $first_task,
321 push @jobstep_todo, 0;
328 $ENV{"CRUNCH_SRC_COMMIT"} = $Job->{script_version};
330 my $skip_install = ($local_job && $Job->{script_version} =~ m{^/});
333 $ENV{"CRUNCH_SRC"} = $Job->{script_version};
334 for my $src_path ("$ENV{CRUNCH_SRC}/arvados/sdk/python") {
336 system("virtualenv", "$ENV{CRUNCH_TMP}/opt") == 0
337 or croak ("virtualenv $ENV{CRUNCH_TMP}/opt failed: exit ".($?>>8));
338 system ("cd $src_path && ./build.sh && \$CRUNCH_TMP/opt/bin/python setup.py install")
340 or croak ("setup.py in $src_path failed: exit ".($?>>8));
348 $build_script = <DATA>;
350 Log (undef, "Install revision ".$Job->{script_version});
351 my $nodelist = join(",", @node);
353 # Clean out crunch_tmp/work, crunch_tmp/opt, crunch_tmp/src*
355 my $cleanpid = fork();
358 srun (["srun", "--nodelist=$nodelist", "-D", $ENV{'TMPDIR'}],
359 ['bash', '-c', 'if mount | grep -q $JOB_WORK/; then sudo /bin/umount $JOB_WORK/* 2>/dev/null; fi; sleep 1; rm -rf $JOB_WORK $CRUNCH_TMP/opt $CRUNCH_TMP/src*']);
364 last if $cleanpid == waitpid (-1, WNOHANG);
365 freeze_if_want_freeze ($cleanpid);
366 select (undef, undef, undef, 0.1);
368 Log (undef, "Clean-work-dir exited $?");
370 # Install requested code version
373 my @srunargs = ("srun",
374 "--nodelist=$nodelist",
375 "-D", $ENV{'TMPDIR'}, "--job-name=$job_id");
377 $ENV{"CRUNCH_SRC_COMMIT"} = $Job->{script_version};
378 $ENV{"CRUNCH_SRC"} = "$ENV{CRUNCH_TMP}/src";
382 my $treeish = $Job->{'script_version'};
383 my $repo = $git_dir || $ENV{'CRUNCH_DEFAULT_GIT_DIR'};
384 # Todo: let script_version specify repository instead of expecting
385 # parent process to figure it out.
386 $ENV{"CRUNCH_SRC_URL"} = $repo;
388 # Create/update our clone of the remote git repo
390 if (!-d $ENV{"CRUNCH_SRC"}) {
391 system(qw(git clone), $repo, $ENV{"CRUNCH_SRC"}) == 0
392 or croak ("git clone $repo failed: exit ".($?>>8));
393 system("cd $ENV{CRUNCH_SRC} && git config clean.requireForce false");
395 `cd $ENV{CRUNCH_SRC} && git remote set-url origin \"\$CRUNCH_SRC_URL\" && git fetch -q origin`;
397 # If this looks like a subversion r#, look for it in git-svn commit messages
399 if ($treeish =~ m{^\d{1,4}$}) {
400 my $gitlog = `cd $ENV{CRUNCH_SRC} && git log --pretty="format:%H" --grep="git-svn-id:.*\@$treeish " origin/master`;
402 if ($gitlog =~ /^[a-f0-9]{40}$/) {
404 Log (undef, "Using commit $commit for script_version $treeish");
408 # If that didn't work, try asking git to look it up as a tree-ish.
410 if (!defined $commit) {
412 my $cooked_treeish = $treeish;
413 if ($treeish !~ m{^[0-9a-f]{5,}$}) {
414 # Looks like a git branch name -- make sure git knows it's
415 # relative to the remote repo
416 $cooked_treeish = "origin/$treeish";
419 my $found = `cd $ENV{CRUNCH_SRC} && git rev-list -1 $cooked_treeish`;
421 if ($found =~ /^[0-9a-f]{40}$/s) {
423 if ($commit ne $treeish) {
424 # Make sure we record the real commit id in the database,
425 # frozentokey, logs, etc. -- instead of an abbreviation or a
426 # branch name which can become ambiguous or point to a
427 # different commit in the future.
428 $ENV{"CRUNCH_SRC_COMMIT"} = $commit;
429 Log (undef, "Using commit $commit for tree-ish $treeish");
430 if ($commit ne $treeish) {
431 $Job->{'script_version'} = $commit;
433 $Job->update_attributes('script_version' => $commit) or
434 croak("Error while updating job");
440 if (defined $commit) {
441 $ENV{"CRUNCH_SRC_COMMIT"} = $commit;
442 @execargs = ("sh", "-c",
443 "mkdir -p $ENV{CRUNCH_INSTALL} && cd $ENV{CRUNCH_TMP} && perl -");
444 $git_archive = `cd $ENV{CRUNCH_SRC} && git archive $commit`;
447 croak ("could not figure out commit id for $treeish");
450 my $installpid = fork();
451 if ($installpid == 0)
453 srun (\@srunargs, \@execargs, {}, $build_script . $git_archive);
458 last if $installpid == waitpid (-1, WNOHANG);
459 freeze_if_want_freeze ($installpid);
460 select (undef, undef, undef, 0.1);
462 Log (undef, "Install exited $?");
467 foreach (qw (script script_version script_parameters runtime_constraints))
471 (ref($Job->{$_}) ? JSON::encode_json($Job->{$_}) : $Job->{$_}));
473 foreach (split (/\n/, $Job->{knobs}))
475 Log (undef, "knob " . $_);
480 $main::success = undef;
486 my $thisround_succeeded = 0;
487 my $thisround_failed = 0;
488 my $thisround_failed_multiple = 0;
490 @jobstep_todo = sort { $jobstep[$a]->{level} <=> $jobstep[$b]->{level}
491 or $a <=> $b } @jobstep_todo;
492 my $level = $jobstep[$jobstep_todo[0]]->{level};
493 Log (undef, "start level $level");
498 my @freeslot = (0..$#slot);
501 my $progress_is_dirty = 1;
502 my $progress_stats_updated = 0;
504 update_progress_stats();
509 for (my $todo_ptr = 0; $todo_ptr <= $#jobstep_todo; $todo_ptr ++)
511 my $id = $jobstep_todo[$todo_ptr];
512 my $Jobstep = $jobstep[$id];
513 if ($Jobstep->{level} != $level)
518 pipe $reader{$id}, "writer" or croak ($!);
519 my $flags = fcntl ($reader{$id}, F_GETFL, 0) or croak ($!);
520 fcntl ($reader{$id}, F_SETFL, $flags | O_NONBLOCK) or croak ($!);
522 my $childslot = $freeslot[0];
523 my $childnode = $slot[$childslot]->{node};
524 my $childslotname = join (".",
525 $slot[$childslot]->{node}->{name},
526 $slot[$childslot]->{cpu});
527 my $childpid = fork();
530 $SIG{'INT'} = 'DEFAULT';
531 $SIG{'QUIT'} = 'DEFAULT';
532 $SIG{'TERM'} = 'DEFAULT';
534 foreach (values (%reader))
538 fcntl ("writer", F_SETFL, 0) or croak ($!); # no close-on-exec
539 open(STDOUT,">&writer");
540 open(STDERR,">&writer");
545 delete $ENV{"GNUPGHOME"};
546 $ENV{"TASK_UUID"} = $Jobstep->{'arvados_task'}->{'uuid'};
547 $ENV{"TASK_QSEQUENCE"} = $id;
548 $ENV{"TASK_SEQUENCE"} = $level;
549 $ENV{"JOB_SCRIPT"} = $Job->{script};
550 while (my ($param, $value) = each %{$Job->{script_parameters}}) {
551 $param =~ tr/a-z/A-Z/;
552 $ENV{"JOB_PARAMETER_$param"} = $value;
554 $ENV{"TASK_SLOT_NODE"} = $slot[$childslot]->{node}->{name};
555 $ENV{"TASK_SLOT_NUMBER"} = $slot[$childslot]->{cpu};
556 $ENV{"TASK_WORK"} = $ENV{"JOB_WORK"}."/".$slot[$childslot]->{cpu};
557 $ENV{"TASK_KEEPMOUNT"} = $ENV{"TASK_WORK"}."/keep";
558 $ENV{"TASK_TMPDIR"} = $ENV{"TASK_WORK"}; # deprecated
559 $ENV{"CRUNCH_NODE_SLOTS"} = $slot[$childslot]->{node}->{ncpus};
560 $ENV{"PATH"} = $ENV{"CRUNCH_INSTALL"} . "/bin:" . $ENV{"PATH"};
566 "--nodelist=".$childnode->{name},
567 qw(-n1 -c1 -N1 -D), $ENV{'TMPDIR'},
568 "--job-name=$job_id.$id.$$",
570 my @execargs = qw(sh);
571 my $build_script_to_send = "";
573 "if [ -e $ENV{TASK_WORK} ]; then rm -rf $ENV{TASK_WORK}; fi; "
574 ."mkdir -p $ENV{JOB_WORK} $ENV{CRUNCH_TMP} $ENV{TASK_WORK} $ENV{TASK_KEEPMOUNT} "
575 ."&& cd $ENV{CRUNCH_TMP} ";
578 $build_script_to_send = $build_script;
583 "&& exec arv-mount $ENV{TASK_KEEPMOUNT} --exec $ENV{CRUNCH_SRC}/crunch_scripts/" . $Job->{"script"};
584 my @execargs = ('bash', '-c', $command);
585 srun (\@srunargs, \@execargs, undef, $build_script_to_send);
589 if (!defined $childpid)
596 $proc{$childpid} = { jobstep => $id,
599 jobstepname => "$job_id.$id.$childpid",
601 croak ("assert failed: \$slot[$childslot]->{'pid'} exists") if exists $slot[$childslot]->{pid};
602 $slot[$childslot]->{pid} = $childpid;
604 Log ($id, "job_task ".$Jobstep->{'arvados_task'}->{'uuid'});
605 Log ($id, "child $childpid started on $childslotname");
606 $Jobstep->{starttime} = time;
607 $Jobstep->{node} = $childnode->{name};
608 $Jobstep->{slotindex} = $childslot;
609 delete $Jobstep->{stderr};
610 delete $Jobstep->{finishtime};
612 splice @jobstep_todo, $todo_ptr, 1;
615 $progress_is_dirty = 1;
619 (@slot > @freeslot && $todo_ptr+1 > $#jobstep_todo))
621 last THISROUND if $main::please_freeze;
622 if ($main::please_info)
624 $main::please_info = 0;
628 update_progress_stats();
635 check_refresh_wanted();
637 update_progress_stats();
638 select (undef, undef, undef, 0.1);
640 elsif (time - $progress_stats_updated >= 30)
642 update_progress_stats();
644 if (($thisround_failed_multiple >= 8 && $thisround_succeeded == 0) ||
645 ($thisround_failed_multiple >= 16 && $thisround_failed_multiple > $thisround_succeeded))
647 my $message = "Repeated failure rate too high ($thisround_failed_multiple/"
648 .($thisround_failed+$thisround_succeeded)
649 .") -- giving up on this round";
650 Log (undef, $message);
654 # move slots from freeslot to holdslot (or back to freeslot) if necessary
655 for (my $i=$#freeslot; $i>=0; $i--) {
656 if ($slot[$freeslot[$i]]->{node}->{hold_until} > scalar time) {
657 push @holdslot, (splice @freeslot, $i, 1);
660 for (my $i=$#holdslot; $i>=0; $i--) {
661 if ($slot[$holdslot[$i]]->{node}->{hold_until} <= scalar time) {
662 push @freeslot, (splice @holdslot, $i, 1);
666 # give up if no nodes are succeeding
667 if (!grep { $_->{node}->{losing_streak} == 0 &&
668 $_->{node}->{hold_count} < 4 } @slot) {
669 my $message = "Every node has failed -- giving up on this round";
670 Log (undef, $message);
677 push @freeslot, splice @holdslot;
678 map { $slot[$freeslot[$_]]->{node}->{losing_streak} = 0 } (0..$#freeslot);
681 Log (undef, "wait for last ".(scalar keys %proc)." children to finish");
684 if ($main::please_continue) {
685 $main::please_continue = 0;
688 $main::please_info = 0, freeze(), collate_output(), save_meta(1) if $main::please_info;
692 check_refresh_wanted();
694 update_progress_stats();
695 select (undef, undef, undef, 0.1);
696 killem (keys %proc) if $main::please_freeze;
700 update_progress_stats();
701 freeze_if_want_freeze();
704 if (!defined $main::success)
707 $thisround_succeeded == 0 &&
708 ($thisround_failed == 0 || $thisround_failed > 4))
710 my $message = "stop because $thisround_failed tasks failed and none succeeded";
711 Log (undef, $message);
720 goto ONELEVEL if !defined $main::success;
723 release_allocation();
726 $Job->update_attributes('output' => &collate_output(),
728 'success' => $Job->{'output'} && $main::success,
729 'finished_at' => scalar gmtime)
732 if ($Job->{'output'})
735 my $manifest_text = `arv keep get $Job->{'output'}`;
736 $arv->{'collections'}->{'create'}->execute('collection' => {
737 'uuid' => $Job->{'output'},
738 'manifest_text' => $manifest_text,
742 Log (undef, "Failed to register output manifest: $@");
746 Log (undef, "finish");
753 sub update_progress_stats
755 $progress_stats_updated = time;
756 return if !$progress_is_dirty;
757 my ($todo, $done, $running) = (scalar @jobstep_todo,
758 scalar @jobstep_done,
759 scalar @slot - scalar @freeslot - scalar @holdslot);
760 $Job->{'tasks_summary'} ||= {};
761 $Job->{'tasks_summary'}->{'todo'} = $todo;
762 $Job->{'tasks_summary'}->{'done'} = $done;
763 $Job->{'tasks_summary'}->{'running'} = $running;
765 $Job->update_attributes('tasks_summary' => $Job->{'tasks_summary'});
767 Log (undef, "status: $done done, $running running, $todo todo");
768 $progress_is_dirty = 0;
775 my $pid = waitpid (-1, WNOHANG);
776 return 0 if $pid <= 0;
778 my $whatslot = ($slot[$proc{$pid}->{slot}]->{node}->{name}
780 . $slot[$proc{$pid}->{slot}]->{cpu});
781 my $jobstepid = $proc{$pid}->{jobstep};
782 my $elapsed = time - $proc{$pid}->{time};
783 my $Jobstep = $jobstep[$jobstepid];
785 my $childstatus = $?;
786 my $exitvalue = $childstatus >> 8;
787 my $exitinfo = sprintf("exit %d signal %d%s",
790 ($childstatus & 128 ? ' core dump' : ''));
791 $Jobstep->{'arvados_task'}->reload;
792 my $task_success = $Jobstep->{'arvados_task'}->{success};
794 Log ($jobstepid, "child $pid on $whatslot $exitinfo success=$task_success");
796 if (!defined $task_success) {
797 # task did not indicate one way or the other --> fail
798 $Jobstep->{'arvados_task'}->{success} = 0;
799 $Jobstep->{'arvados_task'}->save;
806 $temporary_fail ||= $Jobstep->{node_fail};
807 $temporary_fail ||= ($exitvalue == 111);
810 ++$thisround_failed_multiple if $Jobstep->{'failures'} >= 1;
812 # Check for signs of a failed or misconfigured node
813 if (++$slot[$proc{$pid}->{slot}]->{node}->{losing_streak} >=
814 2+$slot[$proc{$pid}->{slot}]->{node}->{ncpus}) {
815 # Don't count this against jobstep failure thresholds if this
816 # node is already suspected faulty and srun exited quickly
817 if ($slot[$proc{$pid}->{slot}]->{node}->{hold_until} &&
819 Log ($jobstepid, "blaming failure on suspect node " .
820 $slot[$proc{$pid}->{slot}]->{node}->{name});
821 $temporary_fail ||= 1;
823 ban_node_by_slot($proc{$pid}->{slot});
826 Log ($jobstepid, sprintf('failure (#%d, %s) after %d seconds',
827 ++$Jobstep->{'failures'},
828 $temporary_fail ? 'temporary ' : 'permanent',
831 if (!$temporary_fail || $Jobstep->{'failures'} >= 3) {
832 # Give up on this task, and the whole job
834 $main::please_freeze = 1;
837 # Put this task back on the todo queue
838 push @jobstep_todo, $jobstepid;
840 $Job->{'tasks_summary'}->{'failed'}++;
844 ++$thisround_succeeded;
845 $slot[$proc{$pid}->{slot}]->{node}->{losing_streak} = 0;
846 $slot[$proc{$pid}->{slot}]->{node}->{hold_until} = 0;
847 push @jobstep_done, $jobstepid;
848 Log ($jobstepid, "success in $elapsed seconds");
850 $Jobstep->{exitcode} = $childstatus;
851 $Jobstep->{finishtime} = time;
852 process_stderr ($jobstepid, $task_success);
853 Log ($jobstepid, "output " . $Jobstep->{'arvados_task'}->{output});
855 close $reader{$jobstepid};
856 delete $reader{$jobstepid};
857 delete $slot[$proc{$pid}->{slot}]->{pid};
858 push @freeslot, $proc{$pid}->{slot};
862 my $newtask_list = $arv->{'job_tasks'}->{'list'}->execute(
864 'created_by_job_task_uuid' => $Jobstep->{'arvados_task'}->{uuid}
866 'order' => 'qsequence'
868 foreach my $arvados_task (@{$newtask_list->{'items'}}) {
870 'level' => $arvados_task->{'sequence'},
872 'arvados_task' => $arvados_task
874 push @jobstep, $jobstep;
875 push @jobstep_todo, $#jobstep;
878 $progress_is_dirty = 1;
882 sub check_refresh_wanted
884 my @stat = stat $ENV{"CRUNCH_REFRESH_TRIGGER"};
885 if (@stat && $stat[9] > $latest_refresh) {
886 $latest_refresh = scalar time;
888 my $Job2 = $arv->{'jobs'}->{'get'}->execute('uuid' => $jobspec);
889 for my $attr ('cancelled_at',
890 'cancelled_by_user_uuid',
891 'cancelled_by_client_uuid') {
892 $Job->{$attr} = $Job2->{$attr};
894 if ($Job->{'cancelled_at'}) {
895 Log (undef, "Job cancelled at " . $Job->{cancelled_at} .
896 " by user " . $Job->{cancelled_by_user_uuid});
898 $main::please_freeze = 1;
906 # return if the kill list was checked <4 seconds ago
907 if (defined $squeue_kill_checked && $squeue_kill_checked > time - 4)
911 $squeue_kill_checked = time;
913 # use killem() on procs whose killtime is reached
916 if (exists $proc{$_}->{killtime}
917 && $proc{$_}->{killtime} <= time)
923 # return if the squeue was checked <60 seconds ago
924 if (defined $squeue_checked && $squeue_checked > time - 60)
928 $squeue_checked = time;
932 # here is an opportunity to check for mysterious problems with local procs
936 # get a list of steps still running
937 my @squeue = `squeue -s -h -o '%i %j' && echo ok`;
939 if ($squeue[-1] ne "ok")
945 # which of my jobsteps are running, according to squeue?
949 if (/^(\d+)\.(\d+) (\S+)/)
951 if ($1 eq $ENV{SLURM_JOBID})
958 # which of my active child procs (>60s old) were not mentioned by squeue?
961 if ($proc{$_}->{time} < time - 60
962 && !exists $ok{$proc{$_}->{jobstepname}}
963 && !exists $proc{$_}->{killtime})
965 # kill this proc if it hasn't exited in 30 seconds
966 $proc{$_}->{killtime} = time + 30;
972 sub release_allocation
976 Log (undef, "release job allocation");
977 system "scancel $ENV{SLURM_JOBID}";
985 foreach my $job (keys %reader)
988 while (0 < sysread ($reader{$job}, $buf, 8192))
990 print STDERR $buf if $ENV{CRUNCH_DEBUG};
991 $jobstep[$job]->{stderr} .= $buf;
992 preprocess_stderr ($job);
993 if (length ($jobstep[$job]->{stderr}) > 16384)
995 substr ($jobstep[$job]->{stderr}, 0, 8192) = "";
1004 sub preprocess_stderr
1008 while ($jobstep[$job]->{stderr} =~ /^(.*?)\n/) {
1010 substr $jobstep[$job]->{stderr}, 0, 1+length($line), "";
1011 Log ($job, "stderr $line");
1012 if ($line =~ /srun: error: (SLURM job $ENV{SLURM_JOB_ID} has expired|Unable to confirm allocation for job $ENV{SLURM_JOB_ID})/) {
1014 $main::please_freeze = 1;
1016 elsif ($line =~ /srun: error: (Node failure on|Unable to create job step) /) {
1017 $jobstep[$job]->{node_fail} = 1;
1018 ban_node_by_slot($jobstep[$job]->{slotindex});
1027 my $task_success = shift;
1028 preprocess_stderr ($job);
1031 Log ($job, "stderr $_");
1032 } split ("\n", $jobstep[$job]->{stderr});
1038 my ($child_out, $child_in, $output_block);
1040 my $pid = open2($child_out, $child_in, 'arv', 'keep', 'get', $hash);
1041 sysread($child_out, $output_block, 64 * 1024 * 1024);
1043 return $output_block;
1048 Log (undef, "collate");
1050 my ($child_out, $child_in);
1051 my $pid = open2($child_out, $child_in, 'arv', 'keep', 'put', '--raw');
1055 next if (!exists $_->{'arvados_task'}->{output} ||
1056 !$_->{'arvados_task'}->{'success'} ||
1057 $_->{'exitcode'} != 0);
1058 my $output = $_->{'arvados_task'}->{output};
1059 if ($output !~ /^[0-9a-f]{32}(\+\S+)*$/)
1061 $output_in_keep ||= $output =~ / [0-9a-f]{32}\S*\+K/;
1062 print $child_in $output;
1064 elsif (@jobstep == 1)
1066 $joboutput = $output;
1069 elsif (defined (my $outblock = fetch_block ($output)))
1071 $output_in_keep ||= $outblock =~ / [0-9a-f]{32}\S*\+K/;
1072 print $child_in $outblock;
1076 print $child_in "XXX fetch_block($output) failed XXX\n";
1080 if (!defined $joboutput) {
1081 my $s = IO::Select->new($child_out);
1082 sysread($child_out, $joboutput, 64 * 1024 * 1024) if $s->can_read(0);
1089 Log (undef, "output $joboutput");
1090 $Job->update_attributes('output' => $joboutput) if $job_has_uuid;
1094 Log (undef, "output undef");
1104 my $sig = 2; # SIGINT first
1105 if (exists $proc{$_}->{"sent_$sig"} &&
1106 time - $proc{$_}->{"sent_$sig"} > 4)
1108 $sig = 15; # SIGTERM if SIGINT doesn't work
1110 if (exists $proc{$_}->{"sent_$sig"} &&
1111 time - $proc{$_}->{"sent_$sig"} > 4)
1113 $sig = 9; # SIGKILL if SIGTERM doesn't work
1115 if (!exists $proc{$_}->{"sent_$sig"})
1117 Log ($proc{$_}->{jobstep}, "sending 2x signal $sig to pid $_");
1119 select (undef, undef, undef, 0.1);
1122 kill $sig, $_; # srun wants two SIGINT to really interrupt
1124 $proc{$_}->{"sent_$sig"} = time;
1125 $proc{$_}->{"killedafter"} = time - $proc{$_}->{"time"};
1135 vec($bits,fileno($_),1) = 1;
1141 sub Log # ($jobstep_id, $logmessage)
1143 if ($_[1] =~ /\n/) {
1144 for my $line (split (/\n/, $_[1])) {
1149 my $fh = select STDERR; $|=1; select $fh;
1150 my $message = sprintf ("%s %d %s %s", $job_id, $$, @_);
1151 $message =~ s{([^ -\176])}{"\\" . sprintf ("%03o", ord($1))}ge;
1154 if ($metastream || -t STDERR) {
1155 my @gmtime = gmtime;
1156 $datetime = sprintf ("%04d-%02d-%02d_%02d:%02d:%02d",
1157 $gmtime[5]+1900, $gmtime[4]+1, @gmtime[3,2,1,0]);
1159 print STDERR ((-t STDERR) ? ($datetime." ".$message) : $message);
1161 # return if !$metastream;
1162 # $metastream->write_data ($datetime . " " . $message);
1168 my ($package, $file, $line) = caller;
1169 my $message = "@_ at $file line $line\n";
1170 Log (undef, $message);
1171 freeze() if @jobstep_todo;
1172 collate_output() if @jobstep_todo;
1174 save_meta() if $metastream;
1181 return if !$job_has_uuid;
1182 $Job->update_attributes('running' => 0,
1184 'finished_at' => scalar gmtime);
1190 # my $justcheckpoint = shift; # false if this will be the last meta saved
1191 # my $m = $metastream;
1192 # $m = $m->copy if $justcheckpoint;
1194 # my $whc = Warehouse->new;
1195 # my $loglocator = $whc->store_block ($m->as_string);
1196 # $arv->{'collections'}->{'create'}->execute('collection' => {
1197 # 'uuid' => $loglocator,
1198 # 'manifest_text' => $m->as_string,
1200 # undef $metastream if !$justcheckpoint; # otherwise Log() will try to use it
1201 # Log (undef, "log manifest is $loglocator");
1202 # $Job->{'log'} = $loglocator;
1203 # $Job->update_attributes('log', $loglocator) if $job_has_uuid;
1207 sub freeze_if_want_freeze
1209 if ($main::please_freeze)
1211 release_allocation();
1214 # kill some srun procs before freeze+stop
1215 map { $proc{$_} = {} } @_;
1218 killem (keys %proc);
1219 select (undef, undef, undef, 0.1);
1221 while (($died = waitpid (-1, WNOHANG)) > 0)
1223 delete $proc{$died};
1238 Log (undef, "Freeze not implemented");
1245 croak ("Thaw not implemented");
1249 # Log (undef, "thaw from $key");
1252 # @jobstep_done = ();
1253 # @jobstep_todo = ();
1254 # @jobstep_tomerge = ();
1255 # $jobstep_tomerge_level = 0;
1256 # my $frozenjob = {};
1258 # my $stream = new Warehouse::Stream ( whc => $whc,
1259 # hash => [split (",", $key)] );
1261 # while (my $dataref = $stream->read_until (undef, "\n\n"))
1263 # if ($$dataref =~ /^job /)
1265 # foreach (split ("\n", $$dataref))
1267 # my ($k, $v) = split ("=", $_, 2);
1268 # $frozenjob->{$k} = freezeunquote ($v);
1273 # if ($$dataref =~ /^merge (\d+) (.*)/)
1275 # $jobstep_tomerge_level = $1;
1277 # = map { freezeunquote ($_) } split ("\n", freezeunquote($2));
1281 # my $Jobstep = { };
1282 # foreach (split ("\n", $$dataref))
1284 # my ($k, $v) = split ("=", $_, 2);
1285 # $Jobstep->{$k} = freezeunquote ($v) if $k;
1287 # $Jobstep->{'failures'} = 0;
1288 # push @jobstep, $Jobstep;
1290 # if ($Jobstep->{exitcode} eq "0")
1292 # push @jobstep_done, $#jobstep;
1296 # push @jobstep_todo, $#jobstep;
1300 # foreach (qw (script script_version script_parameters))
1302 # $Job->{$_} = $frozenjob->{$_};
1304 # $Job->save if $job_has_uuid;
1320 $s =~ s{\\(.)}{$1 eq "n" ? "\n" : $1}ge;
1327 my $srunargs = shift;
1328 my $execargs = shift;
1329 my $opts = shift || {};
1331 my $args = $have_slurm ? [@$srunargs, @$execargs] : $execargs;
1332 print STDERR (join (" ",
1333 map { / / ? "'$_'" : $_ }
1336 if $ENV{CRUNCH_DEBUG};
1338 if (defined $stdin) {
1339 my $child = open STDIN, "-|";
1340 defined $child or die "no fork: $!";
1342 print $stdin or die $!;
1343 close STDOUT or die $!;
1348 return system (@$args) if $opts->{fork};
1351 warn "ENV size is ".length(join(" ",%ENV));
1352 die "exec failed: $!: @$args";
1356 sub ban_node_by_slot {
1357 # Don't start any new jobsteps on this node for 60 seconds
1359 $slot[$slotid]->{node}->{hold_until} = 60 + scalar time;
1360 $slot[$slotid]->{node}->{hold_count}++;
1361 Log (undef, "backing off node " . $slot[$slotid]->{node}->{name} . " for 60 seconds");
1367 # checkout-and-build
1371 my $destdir = $ENV{"CRUNCH_SRC"};
1372 my $commit = $ENV{"CRUNCH_SRC_COMMIT"};
1373 my $repo = $ENV{"CRUNCH_SRC_URL"};
1375 open L, ">", "$destdir.lock" or die "$destdir.lock: $!";
1377 if (readlink ("$destdir.commit") eq $commit && -d $destdir) {
1381 unlink "$destdir.commit";
1382 open STDOUT, ">", "$destdir.log";
1383 open STDERR, ">&STDOUT";
1386 my @git_archive_data = <DATA>;
1387 if (@git_archive_data) {
1388 open TARX, "|-", "tar", "-C", $destdir, "-xf", "-";
1389 print TARX @git_archive_data;
1391 die "'tar -C $destdir -xf -' exited $?: $!";
1396 chomp ($pwd = `pwd`);
1397 my $install_dir = $ENV{"CRUNCH_INSTALL"} || "$pwd/opt";
1400 for my $src_path ("$destdir/arvados/sdk/python") {
1402 shell_or_die ("virtualenv", $install_dir);
1403 shell_or_die ("cd $src_path && ./build.sh && $install_dir/bin/python setup.py install");
1407 if (-e "$destdir/crunch_scripts/install") {
1408 shell_or_die ("$destdir/crunch_scripts/install", $install_dir);
1409 } elsif (!-e "./install.sh" && -e "./tests/autotests.sh") {
1411 shell_or_die ("./tests/autotests.sh", $install_dir);
1412 } elsif (-e "./install.sh") {
1413 shell_or_die ("./install.sh", $install_dir);
1417 unlink "$destdir.commit.new";
1418 symlink ($commit, "$destdir.commit.new") or die "$destdir.commit.new: $!";
1419 rename ("$destdir.commit.new", "$destdir.commit") or die "$destdir.commit: $!";
1428 if ($ENV{"DEBUG"}) {
1429 print STDERR "@_\n";
1432 or die "@_ failed: $! exit 0x".sprintf("%x",$?);