X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/465cb9225cce74600349239a295b1360ce2b0fa6..d7e559136a7ed5ee3f178b26aa2b5924cea1bca7:/sdk/cli/bin/crunch-job diff --git a/sdk/cli/bin/crunch-job b/sdk/cli/bin/crunch-job index 13001e7f92..4e5b0826b6 100755 --- a/sdk/cli/bin/crunch-job +++ b/sdk/cli/bin/crunch-job @@ -127,6 +127,7 @@ my $job_api_token; my $no_clear_tmp; my $resume_stash; my $docker_bin = "docker.io"; +my $docker_run_args = ""; GetOptions('force-unlock' => \$force_unlock, 'git-dir=s' => \$git_dir, 'job=s' => \$jobspec, @@ -134,6 +135,7 @@ GetOptions('force-unlock' => \$force_unlock, 'no-clear-tmp' => \$no_clear_tmp, 'resume-stash=s' => \$resume_stash, 'docker-bin=s' => \$docker_bin, + 'docker-run-args=s' => \$docker_run_args, ); if (defined $job_api_token) { @@ -390,12 +392,12 @@ if (!defined $no_clear_tmp) { my $cleanpid = fork(); if ($cleanpid == 0) { - # Find FUSE mounts that look like Keep mounts (the mount path has the - # word "keep") and unmount them. Then clean up work directories. - # TODO: When #5036 is done and widely deployed, we can get rid of the - # regular expression and just unmount everything with type fuse.keep. + # Find FUSE mounts under $CRUNCH_TMP and unmount them. + # Then clean up work directories. + # TODO: When #5036 is done and widely deployed, we can limit mount's + # -t option to simply fuse.keep. srun (["srun", "--nodelist=$nodelist", "-D", $ENV{'TMPDIR'}], - ['bash', '-ec', '-o', 'pipefail', 'mount -t fuse,fuse.keep | awk \'($3 ~ /\ykeep\y/){print $3}\' | xargs -r -n 1 fusermount -u -z; sleep 1; rm -rf $JOB_WORK $CRUNCH_INSTALL $CRUNCH_TMP/task $CRUNCH_TMP/src* $CRUNCH_TMP/*.cid']); + ['bash', '-ec', '-o', 'pipefail', 'mount -t fuse,fuse.keep | awk "(index(\$3, \"$CRUNCH_TMP\") == 1){print \$3}" | xargs -r -n 1 fusermount -u -z; sleep 1; rm -rf $JOB_WORK $CRUNCH_INSTALL $CRUNCH_TMP/task $CRUNCH_TMP/src* $CRUNCH_TMP/*.cid']); exit (1); } while (1) @@ -411,13 +413,15 @@ if (!defined $no_clear_tmp) { } # If this job requires a Docker image, install that. -my ($docker_locator, $docker_stream, $docker_hash, $docker_limitmem); +my ($docker_locator, $docker_stream, $docker_hash, $docker_limitmem, $dockeruserarg); if ($docker_locator = $Job->{docker_image_locator}) { + Log (undef, "Install docker image $docker_locator"); ($docker_stream, $docker_hash) = find_docker_image($docker_locator); if (!$docker_hash) { croak("No Docker image hash found from locator $docker_locator"); } + Log (undef, "docker image hash is $docker_hash"); $docker_stream =~ s/^\.//; my $docker_install_script = qq{ if ! $docker_bin images -q --no-trunc --all | grep -qxF \Q$docker_hash\E; then @@ -449,6 +453,42 @@ fi {fork => 1}); $docker_limitmem = ($? == 0); + # Find a non-root Docker user to use. + # Tries the default user for the container, then 'crunch', then 'nobody', + # testing for whether the actual user id is non-zero. This defends against + # mistakes but not malice, but we intend to harden the security in the future + # so we don't want anyone getting used to their jobs running as root in their + # Docker containers. + my @tryusers = ("", "crunch", "nobody"); + foreach my $try_user (@tryusers) { + my $try_user_arg; + if ($try_user eq "") { + Log(undef, "Checking if container default user is not UID 0"); + $try_user_arg = ""; + } else { + Log(undef, "Checking if user '$try_user' is not UID 0"); + $try_user_arg = "--user=$try_user"; + } + srun(["srun", "--nodelist=" . $node[0]], + ["/bin/sh", "-ec", + "a=`$docker_bin run $docker_run_args $try_user_arg $docker_hash id --user` && " . + " test \$a -ne 0"], + {fork => 1}); + if ($? == 0) { + $dockeruserarg = $try_user_arg; + if ($try_user eq "") { + Log(undef, "Container will run with default user"); + } else { + Log(undef, "Container will run with $dockeruserarg"); + } + last; + } + } + + if (!defined $dockeruserarg) { + croak("Could not find a user in container that is not UID 0 (tried default user, @tryusers) or there was a problem running 'id' in the container."); + } + if ($Job->{arvados_sdk_version}) { # The job also specifies an Arvados SDK version. Add the SDKs to the # tar file for the build script to install. @@ -784,6 +824,9 @@ update_progress_stats(); THISROUND: for (my $todo_ptr = 0; $todo_ptr <= $#jobstep_todo; $todo_ptr ++) { + # Don't create new tasks if we already know the job's final result. + last if defined($main::success); + my $id = $jobstep_todo[$todo_ptr]; my $Jobstep = $jobstep[$id]; if ($Jobstep->{level} != $level) @@ -831,11 +874,12 @@ for (my $todo_ptr = 0; $todo_ptr <= $#jobstep_todo; $todo_ptr ++) $ENV{"TASK_SLOT_NUMBER"} = $slot[$childslot]->{cpu}; $ENV{"TASK_WORK"} = $ENV{"CRUNCH_TMP"}."/task/$childslotname"; $ENV{"HOME"} = $ENV{"TASK_WORK"}; - $ENV{"TASK_KEEPMOUNT"} = $ENV{"TASK_WORK"}.".keep"; $ENV{"TASK_TMPDIR"} = $ENV{"TASK_WORK"}; # deprecated $ENV{"CRUNCH_NODE_SLOTS"} = $round_max_slots{$ENV{TASK_SLOT_NODE}}; $ENV{"PATH"} = $ENV{"CRUNCH_INSTALL"} . "/bin:" . $ENV{"PATH"}; + my $keep_mnt = $ENV{"TASK_WORK"}.".keep"; + $ENV{"GZIP"} = "-n"; my @srunargs = ( @@ -844,22 +888,35 @@ for (my $todo_ptr = 0; $todo_ptr <= $#jobstep_todo; $todo_ptr ++) qw(-n1 -c1 -N1 -D), $ENV{'TMPDIR'}, "--job-name=$job_id.$id.$$", ); + + my $stdbuf = " stdbuf --output=0 --error=0 "; + + my $arv_file_cache = ""; + if (defined($Job->{'runtime_constraints'}->{'keep_cache_mb_per_task'})) { + $arv_file_cache = "--file-cache=" . ($Job->{'runtime_constraints'}->{'keep_cache_mb_per_task'} * 1024 * 1024); + } + my $command = - "if [ -e $ENV{TASK_WORK} ]; then rm -rf $ENV{TASK_WORK}; fi; " - ."mkdir -p $ENV{CRUNCH_TMP} $ENV{JOB_WORK} $ENV{TASK_WORK} $ENV{TASK_KEEPMOUNT} " - ."&& cd $ENV{CRUNCH_TMP} " + "if [ -e \Q$ENV{TASK_WORK}\E ]; then rm -rf \Q$ENV{TASK_WORK}\E; fi; " + ."mkdir -p \Q$ENV{CRUNCH_TMP}\E \Q$ENV{JOB_WORK}\E \Q$ENV{TASK_WORK}\E \Q$keep_mnt\E " + ."&& cd \Q$ENV{CRUNCH_TMP}\E " # These environment variables get used explicitly later in # $command. No tool is expected to read these values directly. .q{&& MEM=$(awk '($1 == "MemTotal:"){print $2}' {"script"}; + + if ($Job->{arvados_sdk_version}) { + $command .= $stdbuf; + $command .= "perl - \Q$ENV{CRUNCH_SRC}/crunch_scripts/$Job->{script}\E"; + } else { + $command .= "/bin/sh -c \'python -c " . + '"from pkg_resources import get_distribution as get; print \"Using Arvados SDK version\", get(\"arvados-python-client\").version"' . + ">&2 2>/dev/null; " . + "mkdir -p \"$ENV{JOB_WORK}\" \"$ENV{TASK_WORK}\" && " . + "if which stdbuf >/dev/null ; then " . + " exec $stdbuf \Q$ENV{CRUNCH_SRC}/crunch_scripts/$Job->{script}\E ;" . + " else " . + " exec \Q$ENV{CRUNCH_SRC}/crunch_scripts/$Job->{script}\E ;" . + " fi\'"; + } } else { # Non-docker run $command .= "crunchstat -cgroup-root=/sys/fs/cgroup -poll=10000 "; - $command .= "stdbuf --output=0 --error=0 "; + $command .= $stdbuf; $command .= "perl - $ENV{CRUNCH_SRC}/crunch_scripts/" . $Job->{"script"}; } @@ -975,7 +1042,7 @@ for (my $todo_ptr = 0; $todo_ptr <= $#jobstep_todo; $todo_ptr ++) || ($round_num_freeslots > @freeslot && $todo_ptr+1 > $#jobstep_todo)) { - last THISROUND if $main::please_freeze || defined($main::success); + last THISROUND if $main::please_freeze; if ($main::please_info) { $main::please_info = 0; @@ -987,17 +1054,19 @@ for (my $todo_ptr = 0; $todo_ptr <= $#jobstep_todo; $todo_ptr ++) my $gotsome = readfrompipes () + reapchildren (); - if (!$gotsome) + if (!$gotsome || ($latest_refresh + 2 < scalar time)) { check_refresh_wanted(); check_squeue(); update_progress_stats(); - select (undef, undef, undef, 0.1); } - elsif (time - $progress_stats_updated >= 30) + elsif (time - $progress_stats_updated >= 30 || $progress_is_dirty) { update_progress_stats(); } + if (!$gotsome) { + select (undef, undef, undef, 0.1); + } $working_slot_count = scalar(grep { $_->{node}->{fail_count} == 0 && $_->{node}->{hold_count} < 4 } @slot); if (($thisround_failed_multiple >= 8 && $thisround_succeeded == 0) || @@ -1101,8 +1170,8 @@ sub update_progress_stats $progress_stats_updated = time; return if !$progress_is_dirty; my ($todo, $done, $running) = (scalar @jobstep_todo, - scalar @jobstep_done, - scalar @slot - scalar @freeslot - scalar @holdslot); + scalar @jobstep_done, + scalar keys(%proc)); $Job->{'tasks_summary'} ||= {}; $Job->{'tasks_summary'}->{'todo'} = $todo; $Job->{'tasks_summary'}->{'done'} = $done; @@ -1136,6 +1205,9 @@ sub reapchildren if (!defined $task_success) { # task did not indicate one way or the other --> fail + Log($jobstepid, sprintf( + "ERROR: Task process exited %s, but never updated its task record to indicate success and record its output.", + exit_status_s($childstatus))); $Jobstep->{'arvados_task'}->{success} = 0; $Jobstep->{'arvados_task'}->save; $task_success = 0; @@ -1272,8 +1344,9 @@ sub check_squeue # squeue check interval (15s) this should make the squeue check an # infrequent event. my $silent_procs = 0; - for my $jobstep (values %proc) + for my $procinfo (values %proc) { + my $jobstep = $jobstep[$procinfo->{jobstep}]; if ($jobstep->{stderr_at} < $last_squeue_check) { $silent_procs++; @@ -1282,17 +1355,18 @@ sub check_squeue return if $silent_procs == 0; # use killem() on procs whose killtime is reached - while (my ($pid, $jobstep) = each %proc) + while (my ($pid, $procinfo) = each %proc) { - if (exists $jobstep->{killtime} - && $jobstep->{killtime} <= time + my $jobstep = $jobstep[$procinfo->{jobstep}]; + if (exists $procinfo->{killtime} + && $procinfo->{killtime} <= time && $jobstep->{stderr_at} < $last_squeue_check) { my $sincewhen = ""; if ($jobstep->{stderr_at}) { $sincewhen = " in last " . (time - $jobstep->{stderr_at}) . "s"; } - Log($jobstep->{jobstep}, "killing orphaned srun process $pid (task not in slurm queue, no stderr received$sincewhen)"); + Log($procinfo->{jobstep}, "killing orphaned srun process $pid (task not in slurm queue, no stderr received$sincewhen)"); killem ($pid); } } @@ -1327,12 +1401,12 @@ sub check_squeue } # Check for child procs >60s old and not mentioned by squeue. - while (my ($pid, $jobstep) = each %proc) + while (my ($pid, $procinfo) = each %proc) { - if ($jobstep->{time} < time - 60 - && $jobstep->{jobstepname} - && !exists $ok{$jobstep->{jobstepname}} - && !exists $jobstep->{killtime}) + if ($procinfo->{time} < time - 60 + && $procinfo->{jobstepname} + && !exists $ok{$procinfo->{jobstepname}} + && !exists $procinfo->{killtime}) { # According to slurm, this task has ended (successfully or not) # -- but our srun child hasn't exited. First we must wait (30 @@ -1341,8 +1415,8 @@ sub check_squeue # terminated, we'll conclude some slurm communication # error/delay has caused the task to die without notifying srun, # and we'll kill srun ourselves. - $jobstep->{killtime} = time + 30; - Log($jobstep->{jobstep}, "notice: task is not in slurm queue but srun process $pid has not exited"); + $procinfo->{killtime} = time + 30; + Log($procinfo->{jobstep}, "notice: task is not in slurm queue but srun process $pid has not exited"); } } } @@ -1364,15 +1438,21 @@ sub readfrompipes foreach my $job (keys %reader) { my $buf; - while (0 < sysread ($reader{$job}, $buf, 8192)) + if (0 < sysread ($reader{$job}, $buf, 65536)) { print STDERR $buf if $ENV{CRUNCH_DEBUG}; $jobstep[$job]->{stderr_at} = time; $jobstep[$job]->{stderr} .= $buf; + + # Consume everything up to the last \n preprocess_stderr ($job); + if (length ($jobstep[$job]->{stderr}) > 16384) { - substr ($jobstep[$job]->{stderr}, 0, 8192) = ""; + # If we get a lot of stderr without a newline, chop off the + # front to avoid letting our buffer grow indefinitely. + substr ($jobstep[$job]->{stderr}, + 0, length($jobstep[$job]->{stderr}) - 8192) = ""; } $gotsome = 1; } @@ -1393,7 +1473,7 @@ sub preprocess_stderr # whoa. $main::please_freeze = 1; } - elsif ($line =~ /srun: error: Node failure on/) { + elsif ($line =~ /srun: error: (Node failure on|Aborting, .*\bio error\b)/) { my $job_slot_index = $jobstep[$job]->{slotindex}; $slot[$job_slot_index]->{node}->{fail_count}++; $jobstep[$job]->{tempfail} = 1; @@ -1652,20 +1732,24 @@ sub log_writer_finish() close($log_pipe_in); + my $logger_failed = 0; my $read_result = log_writer_read_output(120); if ($read_result == -1) { + $logger_failed = -1; Log (undef, "timed out reading from 'arv-put'"); } elsif ($read_result != 0) { + $logger_failed = -2; Log(undef, "failed to read arv-put log manifest to EOF"); } waitpid($log_pipe_pid, 0); if ($?) { + $logger_failed ||= $?; Log(undef, "log_writer_finish: arv-put exited " . exit_status_s($?)) } close($log_pipe_out); - my $arv_put_output = $log_pipe_out_buf; + my $arv_put_output = $logger_failed ? undef : $log_pipe_out_buf; $log_pipe_pid = $log_pipe_in = $log_pipe_out = $log_pipe_out_buf = $log_pipe_out_select = undef; @@ -1731,13 +1815,13 @@ sub save_meta my $justcheckpoint = shift; # false if this will be the last meta saved return if $justcheckpoint; # checkpointing is not relevant post-Warehouse.pm return unless log_writer_is_active(); + my $log_manifest = log_writer_finish(); + return unless defined($log_manifest); - my $log_manifest = ""; if ($Job->{log}) { my $prev_log_coll = api_call("collections/get", uuid => $Job->{log}); - $log_manifest .= $prev_log_coll->{manifest_text}; + $log_manifest = $prev_log_coll->{manifest_text} . $log_manifest; } - $log_manifest .= log_writer_finish(); my $log_coll = api_call( "collections/create", ensure_unique_name => 1, collection => { @@ -2116,10 +2200,11 @@ if (@ARGV) { $Log->("Built Python SDK virtualenv"); } - my $pip_bin = "pip"; + my @pysdk_version_cmd = ("python", "-c", + "from pkg_resources import get_distribution as get; print get('arvados-python-client').version"); if ($venv_built) { $Log->("Running in Python SDK virtualenv"); - $pip_bin = "$venv_dir/bin/pip"; + @pysdk_version_cmd = (); my $orig_argv = join(" ", map { quotemeta($_); } @ARGV); @ARGV = ("/bin/sh", "-ec", ". \Q$venv_dir/bin/activate\E; exec $orig_argv"); @@ -2128,14 +2213,18 @@ if (@ARGV) { "\$PATH. Can't install Python SDK."); } - my $pkgs = `(\Q$pip_bin\E freeze 2>/dev/null | grep arvados) || dpkg-query --show '*arvados*'`; - if ($pkgs) { - $Log->("Using Arvados SDK:"); - foreach my $line (split /\n/, $pkgs) { - $Log->($line); + if (@pysdk_version_cmd) { + open(my $pysdk_version_pipe, "-|", @pysdk_version_cmd); + my $pysdk_version = <$pysdk_version_pipe>; + close($pysdk_version_pipe); + if ($? == 0) { + chomp($pysdk_version); + $Log->("Using Arvados SDK version $pysdk_version"); + } else { + # A lot could've gone wrong here, but pretty much all of it means that + # Python won't be able to load the Arvados SDK. + $Log->("Warning: Arvados SDK not found"); } - } else { - $Log->("Arvados SDK packages not found"); } while (my ($sdk_dir, $sdk_envkey) = each(%SDK_ENVVARS)) { @@ -2212,11 +2301,12 @@ if (-d $sdk_root) { my $python_dir = "$install_dir/python"; if ((-d $python_dir) and can_run("python2.7")) { open(my $egg_info_pipe, "-|", - "python2.7 \Q$python_dir/setup.py\E --quiet egg_info 2>&1 >/dev/null"); + "python2.7 \Q$python_dir/setup.py\E egg_info 2>&1 >/dev/null"); my @egg_info_errors = <$egg_info_pipe>; close($egg_info_pipe); + if ($?) { - if (@egg_info_errors and ($egg_info_errors[-1] =~ /\bgit\b/)) { + if (@egg_info_errors and (($egg_info_errors[-1] =~ /\bgit\b/) or ($egg_info_errors[-1] =~ /\[Errno 2\]/))) { # egg_info apparently failed because it couldn't ask git for a build tag. # Specify no build tag. open(my $pysdk_cfg, ">>", "$python_dir/setup.cfg"); @@ -2225,7 +2315,7 @@ if ((-d $python_dir) and can_run("python2.7")) { } else { my $egg_info_exit = $? >> 8; foreach my $errline (@egg_info_errors) { - print STDERR_ORIG $errline; + warn $errline; } warn "python setup.py egg_info failed: exit $egg_info_exit"; exit ($egg_info_exit || 1);