X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/ef56ac56a0bac0710a486c0e5e9cdd3199eddc61..32926d6543e52daa4db8e47c2870bef4d6bdf71d:/services/api/script/crunch-dispatch.rb diff --git a/services/api/script/crunch-dispatch.rb b/services/api/script/crunch-dispatch.rb index d3147225d6..6faf931b28 100755 --- a/services/api/script/crunch-dispatch.rb +++ b/services/api/script/crunch-dispatch.rb @@ -1,5 +1,6 @@ #!/usr/bin/env ruby +require 'shellwords' include Process $options = {} @@ -43,19 +44,48 @@ require File.dirname(__FILE__) + '/../config/boot' require File.dirname(__FILE__) + '/../config/environment' require 'open3' +class LogTime < Time + def to_s + self.utc.strftime "%Y-%m-%d_%H:%M:%S" + end +end + class Dispatcher include ApplicationHelper + include DbCurrentTime + + def initialize + @crunch_job_bin = (ENV['CRUNCH_JOB_BIN'] || `which arv-crunch-job`.strip) + if @crunch_job_bin.empty? + raise "No CRUNCH_JOB_BIN env var, and crunch-job not in path." + end + + @arvados_internal = Rails.configuration.git_internal_dir + if not File.exists? @arvados_internal + $stderr.puts `mkdir -p #{@arvados_internal.shellescape} && git init --bare #{@arvados_internal.shellescape}` + raise "No internal git repository available" unless ($? == 0) + end + + @repo_root = Rails.configuration.git_repositories_dir + @authorizations = {} + @did_recently = {} + @fetched_commits = {} + @git_tags = {} + @node_state = {} + @pipe_auth_tokens = {} + @running = {} + @todo = [] + @todo_pipelines = [] + end def sysuser return act_as_system_user end def refresh_todo - @todo = [] if $options[:jobs] @todo = Job.queue.select(&:repository) end - @todo_pipelines = [] if $options[:pipelines] @todo_pipelines = PipelineInstance.queue end @@ -92,6 +122,9 @@ class Dispatcher def slurm_status slurm_nodes = {} each_slurm_line("sinfo", "%t") do |hostname, state| + # Treat nodes in idle* state as down, because the * means that slurm + # hasn't been able to communicate with it recently. + state.sub!(/^idle\*/, "down") state.sub!(/\W+$/, "") state = "down" unless %w(idle alloc down).include?(state) slurm_nodes[hostname] = {state: state, job: nil} @@ -104,7 +137,6 @@ class Dispatcher def update_node_status return unless Server::Application.config.crunch_job_wrapper.to_s.match /^slurm/ - @node_state ||= {} slurm_status.each_pair do |hostname, slurmdata| next if @node_state[hostname] == slurmdata begin @@ -185,11 +217,131 @@ class Dispatcher nodelist = nodes_available_for_job_now(job) if nodelist.nil? and not did_recently(:wait_for_available_nodes, 3600) $stderr.puts "dispatch: waiting for nodes for #{job.uuid}" - @node_wait_deadline = Time.now + 5.minutes + @node_wait_deadline = db_current_time + 5.minutes end nodelist end + def fail_job job, message + $stderr.puts "dispatch: #{job.uuid}: #{message}" + begin + Log.new(object_uuid: job.uuid, + event_type: 'dispatch', + owner_uuid: job.owner_uuid, + summary: message, + properties: {"text" => message}).save! + rescue + $stderr.puts "dispatch: log.create failed" + end + + begin + job.lock @authorizations[job.uuid].user.uuid + job.state = "Failed" + if not job.save + $stderr.puts "dispatch: save failed setting job #{job.uuid} to failed" + end + rescue ArvadosModel::AlreadyLockedError + $stderr.puts "dispatch: tried to mark job #{job.uuid} as failed but it was already locked by someone else" + end + end + + def stdout_s(cmd_a, opts={}) + IO.popen(cmd_a, "r", opts) do |pipe| + return pipe.read.chomp + end + end + + def git_cmd(*cmd_a) + ["git", "--git-dir=#{@arvados_internal}"] + cmd_a + end + + def get_authorization(job) + if @authorizations[job.uuid] and + @authorizations[job.uuid].user.uuid != job.modified_by_user_uuid + # We already made a token for this job, but we need a new one + # because modified_by_user_uuid has changed (the job will run + # as a different user). + @authorizations[job.uuid].update_attributes expires_at: db_current_time + @authorizations[job.uuid] = nil + end + if not @authorizations[job.uuid] + auth = ApiClientAuthorization. + new(user: User.where('uuid=?', job.modified_by_user_uuid).first, + api_client_id: 0) + if not auth.save + $stderr.puts "dispatch: auth.save failed for #{job.uuid}" + else + @authorizations[job.uuid] = auth + end + end + @authorizations[job.uuid] + end + + def get_commit(repo_name, commit_hash) + # @fetched_commits[V]==true if we know commit V exists in the + # arvados_internal git repository. + if !@fetched_commits[commit_hash] + src_repo = File.join(@repo_root, "#{repo_name}.git") + if not File.exists? src_repo + src_repo = File.join(@repo_root, repo_name, '.git') + if not File.exists? src_repo + fail_job job, "No #{repo_name}.git or #{repo_name}/.git at #{@repo_root}" + return nil + end + end + + # check if the commit needs to be fetched or not + commit_rev = stdout_s(git_cmd("rev-list", "-n1", commit_hash), + err: "/dev/null") + unless $? == 0 and commit_rev == commit_hash + # commit does not exist in internal repository, so import the source repository using git fetch-pack + cmd = git_cmd("fetch-pack", "--no-progress", "--all", src_repo) + $stderr.puts "dispatch: #{cmd}" + $stderr.puts(stdout_s(cmd)) + unless $? == 0 + fail_job job, "git fetch-pack failed" + return nil + end + end + @fetched_commits[commit_hash] = true + end + @fetched_commits[commit_hash] + end + + def tag_commit(commit_hash, tag_name) + # @git_tags[T]==V if we know commit V has been tagged T in the + # arvados_internal repository. + if not @git_tags[tag_name] + cmd = git_cmd("tag", tag_name, commit_hash) + $stderr.puts "dispatch: #{cmd}" + $stderr.puts(stdout_s(cmd, err: "/dev/null")) + unless $? == 0 + # git tag failed. This may be because the tag already exists, so check for that. + tag_rev = stdout_s(git_cmd("rev-list", "-n1", tag_name)) + if $? == 0 + # We got a revision back + if tag_rev != commit_hash + # Uh oh, the tag doesn't point to the revision we were expecting. + # Someone has been monkeying with the job record and/or git. + fail_job job, "Existing tag #{tag_name} points to commit #{tag_rev} but expected commit #{commit_hash}" + return nil + end + # we're okay (fall through to setting @git_tags below) + else + # git rev-list failed for some reason. + fail_job job, "'git tag' for #{tag_name} failed but did not find any existing tag using 'git rev-list'" + return nil + end + end + # 'git tag' was successful, or there is an existing tag that points to the same revision. + @git_tags[tag_name] = commit_hash + elsif @git_tags[tag_name] != commit_hash + fail_job job, "Existing tag #{tag_name} points to commit #{@git_tags[tag_name]} but this job uses commit #{commit_hash}" + return nil + end + @git_tags[tag_name] + end + def start_jobs @todo.each do |job| next if @running[job.uuid] @@ -205,7 +357,7 @@ class Dispatcher when :slurm_immediate nodelist = nodes_available_for_job(job) if nodelist.nil? - if Time.now < @node_wait_deadline + if db_current_time < @node_wait_deadline break else next @@ -232,87 +384,19 @@ class Dispatcher "GEM_PATH=#{ENV['GEM_PATH']}") end - job_auth = ApiClientAuthorization. - new(user: User.where('uuid=?', job.modified_by_user_uuid).first, - api_client_id: 0) - if not job_auth.save - $stderr.puts "dispatch: job_auth.save failed" - next - end - - crunch_job_bin = (ENV['CRUNCH_JOB_BIN'] || `which arv-crunch-job`.strip) - if crunch_job_bin == '' - raise "No CRUNCH_JOB_BIN env var, and crunch-job not in path." - end - - require 'shellwords' - - arvados_internal = Rails.configuration.git_internal_dir - if not File.exists? arvados_internal - $stderr.puts `mkdir -p #{arvados_internal.shellescape} && cd #{arvados_internal.shellescape} && git init --bare` - end - - repo_root = Rails.configuration.git_repositories_dir - src_repo = File.join(repo_root, job.repository + '.git') - if not File.exists? src_repo - src_repo = File.join(repo_root, job.repository, '.git') - if not File.exists? src_repo - $stderr.puts "dispatch: No #{job.repository}.git or #{job.repository}/.git at #{repo_root}" - sleep 1 - next - end - end - - git = "git --git-dir=#{arvados_internal.shellescape}" - - # check if the commit needs to be fetched or not - commit_rev = `#{git} rev-list -n1 #{job.script_version.shellescape} 2>/dev/null`.chomp - unless $? == 0 and commit_rev == job.script_version - # commit does not exist in internal repository, so import the source repository using git fetch-pack - cmd = "#{git} fetch-pack --no-progress --all #{src_repo.shellescape}" - $stderr.puts cmd - $stderr.puts `#{cmd}` - unless $? == 0 - $stderr.puts "dispatch: git fetch-pack failed" - sleep 1 - next - end + ready = (get_authorization(job) and + get_commit(job.repository, job.script_version) and + tag_commit(job.script_version, job.uuid)) + if ready and job.arvados_sdk_version + ready = (get_commit("arvados", job.arvados_sdk_version) and + tag_commit(job.arvados_sdk_version, "#{job.uuid}-arvados-sdk")) end + next unless ready - # check if the commit needs to be tagged with this job uuid - tag_rev = `#{git} rev-list -n1 #{job.uuid.shellescape} 2>/dev/null`.chomp - if $? != 0 - # no job tag found, so create one - cmd = "#{git} tag #{job.uuid.shellescape} #{job.script_version.shellescape}" - $stderr.puts cmd - $stderr.puts `#{cmd}` - unless $? == 0 - $stderr.puts "dispatch: git tag failed" - sleep 1 - next - end - else - # job tag found, check that it has the expected revision - unless tag_rev == job.script_version - # Uh oh, the tag doesn't point to the revision we were expecting. - # Someone has been monkeying with the job record and/or git. - $stderr.puts "dispatch: Already a tag #{job.script_version} pointing to commit #{tag_rev} but expected commit #{job.script_version}" - job.state = "Failed" - if not job.save - $stderr.puts "dispatch: job.save failed" - next - end - next - end - end - - cmd_args << crunch_job_bin - cmd_args << '--job-api-token' - cmd_args << job_auth.api_token - cmd_args << '--job' - cmd_args << job.uuid - cmd_args << '--git-dir' - cmd_args << arvados_internal + cmd_args += [@crunch_job_bin, + '--job-api-token', @authorizations[job.uuid].api_token, + '--job', job.uuid, + '--git-dir', @arvados_internal] $stderr.puts "dispatch: #{cmd_args.join ' '}" @@ -325,7 +409,7 @@ class Dispatcher end $stderr.puts "dispatch: job #{job.uuid}" - start_banner = "dispatch: child #{t.pid} start #{Time.now.ctime.to_s}" + start_banner = "dispatch: child #{t.pid} start #{LogTime.now}" $stderr.puts start_banner @running[job.uuid] = { @@ -337,13 +421,13 @@ class Dispatcher buf: {stderr: '', stdout: ''}, started: false, sent_int: 0, - job_auth: job_auth, + job_auth: @authorizations[job.uuid], stderr_buf_to_flush: '', stderr_flushed_at: Time.new(0), bytes_logged: 0, events_logged: 0, log_throttle_is_open: true, - log_throttle_reset_time: Time.now + Rails.configuration.crunch_log_throttle_period, + log_throttle_reset_time: db_current_time + Rails.configuration.crunch_log_throttle_period, log_throttle_bytes_so_far: 0, log_throttle_lines_so_far: 0, log_throttle_bytes_skipped: 0, @@ -367,18 +451,18 @@ class Dispatcher if (running_job[:bytes_logged] > Rails.configuration.crunch_limit_log_bytes_per_job) message = "Exceeded log limit #{Rails.configuration.crunch_limit_log_bytes_per_job} bytes (crunch_limit_log_bytes_per_job). Log will be truncated." - running_job[:log_throttle_reset_time] = Time.now + 100.years + running_job[:log_throttle_reset_time] = db_current_time + 100.years running_job[:log_throttle_is_open] = false elsif (running_job[:log_throttle_bytes_so_far] > Rails.configuration.crunch_log_throttle_bytes) - remaining_time = running_job[:log_throttle_reset_time] - Time.now + remaining_time = running_job[:log_throttle_reset_time] - db_current_time message = "Exceeded rate #{Rails.configuration.crunch_log_throttle_bytes} bytes per #{Rails.configuration.crunch_log_throttle_period} seconds (crunch_log_throttle_bytes). Logging will be silenced for the next #{remaining_time.round} seconds.\n" running_job[:log_throttle_is_open] = false elsif (running_job[:log_throttle_lines_so_far] > Rails.configuration.crunch_log_throttle_lines) - remaining_time = running_job[:log_throttle_reset_time] - Time.now + remaining_time = running_job[:log_throttle_reset_time] - db_current_time message = "Exceeded rate #{Rails.configuration.crunch_log_throttle_lines} lines per #{Rails.configuration.crunch_log_throttle_period} seconds (crunch_log_throttle_lines), logging will be silenced for the next #{remaining_time.round} seconds.\n" running_job[:log_throttle_is_open] = false end @@ -403,14 +487,14 @@ class Dispatcher @running.each do |job_uuid, j| job = j[:job] - now = Time.now + now = db_current_time if now > j[:log_throttle_reset_time] # It has been more than throttle_period seconds since the last # checkpoint so reset the throttle if j[:log_throttle_bytes_skipped] > 0 message = "#{job_uuid} ! Skipped #{j[:log_throttle_bytes_skipped]} bytes of log" $stderr.puts message - j[:stderr_buf_to_flush] << "#{Time.now.ctime.to_s} #{message}\n" + j[:stderr_buf_to_flush] << "#{LogTime.now} #{message}\n" end j[:log_throttle_reset_time] = now + Rails.configuration.crunch_log_throttle_period @@ -478,7 +562,7 @@ class Dispatcher if rate_limit j, line $stderr.print "#{job_uuid} ! " unless line.index(job_uuid) $stderr.puts line - pub_msg = "#{Time.now.ctime.to_s} #{line.strip}\n" + pub_msg = "#{LogTime.now} #{line.strip}\n" j[:stderr_buf_to_flush] << pub_msg end end @@ -577,8 +661,11 @@ class Dispatcher # fine. end - # Invalidate the per-job auth token - j_done[:job_auth].update_attributes expires_at: Time.now + # Invalidate the per-job auth token, unless the job is still queued and we + # might want to try it again. + if jobrecord.state != "Queued" + j_done[:job_auth].update_attributes expires_at: db_current_time + end @running.delete job_done.uuid end @@ -594,15 +681,13 @@ class Dispatcher end expire_tokens.each do |k, v| - v.update_attributes expires_at: Time.now + v.update_attributes expires_at: db_current_time @pipe_auth_tokens.delete k end end def run act_as_system_user - @running ||= {} - @pipe_auth_tokens ||= { } $stderr.puts "dispatch: ready" while !$signal[:term] or @running.size > 0 read_pipes @@ -620,7 +705,7 @@ class Dispatcher end else refresh_todo unless did_recently(:refresh_todo, 1.0) - update_node_status + update_node_status unless did_recently(:update_node_status, 1.0) unless @todo.empty? or did_recently(:start_jobs, 1.0) or $signal[:term] start_jobs end @@ -637,9 +722,9 @@ class Dispatcher protected def did_recently(thing, min_interval) - @did_recently ||= {} - if !@did_recently[thing] or @did_recently[thing] < Time.now - min_interval - @did_recently[thing] = Time.now + current_time = db_current_time + if !@did_recently[thing] or @did_recently[thing] < current_time - min_interval + @did_recently[thing] = current_time false else true @@ -654,7 +739,7 @@ class Dispatcher # it has been at least crunch_log_seconds_between_events seconds since # the last flush. if running_job[:stderr_buf_to_flush].size > Rails.configuration.crunch_log_bytes_per_event or - (Time.now - running_job[:stderr_flushed_at]) >= Rails.configuration.crunch_log_seconds_between_events + (db_current_time - running_job[:stderr_flushed_at]) >= Rails.configuration.crunch_log_seconds_between_events begin log = Log.new(object_uuid: running_job[:job].uuid, event_type: 'stderr', @@ -667,7 +752,7 @@ class Dispatcher $stderr.puts exception.backtrace end running_job[:stderr_buf_to_flush] = '' - running_job[:stderr_flushed_at] = Time.now + running_job[:stderr_flushed_at] = db_current_time end end end