Merge branch 'master' into 3761-pull-list-worker
[arvados.git] / services / api / app / controllers / arvados / v1 / jobs_controller.rb
1 class Arvados::V1::JobsController < ApplicationController
2   accept_attribute_as_json :script_parameters, Hash
3   accept_attribute_as_json :runtime_constraints, Hash
4   accept_attribute_as_json :tasks_summary, Hash
5   skip_before_filter :find_object_by_uuid, :only => [:queue, :queue_size]
6   skip_before_filter :render_404_if_no_object, :only => [:queue, :queue_size]
7
8   def create
9     [:repository, :script, :script_version, :script_parameters].each do |r|
10       if !resource_attrs[r]
11         return send_error("#{r} attribute must be specified",
12                           status: :unprocessable_entity)
13       end
14     end
15
16     # We used to ask for the minimum_, exclude_, and no_reuse params
17     # in the job resource. Now we advertise them as flags that alter
18     # the behavior of the create action.
19     [:minimum_script_version, :exclude_script_versions].each do |attr|
20       if resource_attrs.has_key? attr
21         params[attr] = resource_attrs.delete attr
22       end
23     end
24     if resource_attrs.has_key? :no_reuse
25       params[:find_or_create] = !resource_attrs.delete(:no_reuse)
26     end
27
28     if params[:find_or_create]
29       return if false.equal?(load_filters_param)
30       if @filters.empty?  # Translate older creation parameters into filters.
31         @filters =
32           [["repository", "=", resource_attrs[:repository]],
33            ["script", "=", resource_attrs[:script]],
34            ["script_version", "in git",
35             params[:minimum_script_version] || resource_attrs[:script_version]],
36            ["script_version", "not in git", params[:exclude_script_versions]],
37           ].reject { |filter| filter.last.nil? or filter.last.empty? }
38         if image_search = resource_attrs[:runtime_constraints].andand["docker_image"]
39           if image_tag = resource_attrs[:runtime_constraints]["docker_image_tag"]
40             image_search += ":#{image_tag}"
41           end
42           @filters.append(["docker_image_locator", "in docker", image_search])
43         else
44           @filters.append(["docker_image_locator", "=", nil])
45         end
46         if sdk_version = resource_attrs[:runtime_constraints].andand["arvados_sdk_version"]
47           @filters.append(["arvados_sdk_version", "in git", sdk_version])
48         end
49         begin
50           load_job_specific_filters
51         rescue ArgumentError => error
52           return send_error(error.message)
53         end
54       end
55
56       # Check specified filters for some reasonableness.
57       filter_names = @filters.map { |f| f.first }.uniq
58       ["repository", "script"].each do |req_filter|
59         if not filter_names.include?(req_filter)
60           return send_error("#{req_filter} filter required")
61         end
62       end
63
64       # Search for a reusable Job, and return it if found.
65       @objects = Job.readable_by(current_user)
66       apply_filters
67       @object = nil
68       incomplete_job = nil
69       @objects.each do |j|
70         if j.nondeterministic != true and
71             ["Queued", "Running", "Complete"].include?(j.state) and
72             j.script_parameters == resource_attrs[:script_parameters]
73           if j.state != "Complete" && j.owner_uuid == current_user.uuid
74             # We'll use this if we don't find a job that has completed
75             incomplete_job ||= j
76           else
77             if Collection.readable_by(current_user).find_by_portable_data_hash(j.output)
78               # Record the first job in the list
79               if !@object
80                 @object = j
81               end
82               # Ensure that all candidate jobs actually did produce the same output
83               if @object.output != j.output
84                 @object = nil
85                 break
86               end
87             end
88           end
89         end
90         @object ||= incomplete_job
91         if @object
92           return show
93         end
94       end
95     end
96
97     super
98   end
99
100   def cancel
101     reload_object_before_update
102     @object.update_attributes! state: Job::Cancelled
103     show
104   end
105
106   def lock
107     @object.lock current_user.uuid
108     show
109   end
110
111   class LogStreamer
112     Q_UPDATE_INTERVAL = 12
113     def initialize(job, opts={})
114       @job = job
115       @opts = opts
116     end
117     def each
118       if @job.finished_at
119         yield "#{@job.uuid} finished at #{@job.finished_at}\n"
120         return
121       end
122       while not @job.started_at
123         # send a summary (job queue + available nodes) to the client
124         # every few seconds while waiting for the job to start
125         last_ack_at ||= Time.now - Q_UPDATE_INTERVAL - 1
126         if Time.now - last_ack_at >= Q_UPDATE_INTERVAL
127           nodes_in_state = {idle: 0, alloc: 0}
128           ActiveRecord::Base.uncached do
129             Node.where('hostname is not ?', nil).collect do |n|
130               if n.info[:slurm_state]
131                 nodes_in_state[n.info[:slurm_state]] ||= 0
132                 nodes_in_state[n.info[:slurm_state]] += 1
133               end
134             end
135           end
136           job_queue = Job.queue
137           n_queued_before_me = 0
138           job_queue.each do |j|
139             break if j.uuid == @job.uuid
140             n_queued_before_me += 1
141           end
142           yield "#{Time.now}" \
143             " job #{@job.uuid}" \
144             " queue_position #{n_queued_before_me}" \
145             " queue_size #{job_queue.size}" \
146             " nodes_idle #{nodes_in_state[:idle]}" \
147             " nodes_alloc #{nodes_in_state[:alloc]}\n"
148           last_ack_at = Time.now
149         end
150         sleep 3
151         ActiveRecord::Base.uncached do
152           @job.reload
153         end
154       end
155     end
156   end
157
158   def queue
159     params[:order] ||= ['priority desc', 'created_at']
160     load_limit_offset_order_params
161     load_where_param
162     @where.merge!({state: Job::Queued})
163     return if false.equal?(load_filters_param)
164     find_objects_for_index
165     index
166   end
167
168   def queue_size
169     # Users may not be allowed to see all the jobs in the queue, so provide a
170     # method to get just the queue size in order to get a gist of how busy the
171     # cluster is.
172     render :json => {:queue_size => Job.queue.size}
173   end
174
175   def self._create_requires_parameters
176     (super rescue {}).
177       merge({
178               find_or_create: {
179                 type: 'boolean', required: false, default: false
180               },
181               filters: {
182                 type: 'array', required: false
183               },
184               minimum_script_version: {
185                 type: 'string', required: false
186               },
187               exclude_script_versions: {
188                 type: 'array', required: false
189               },
190             })
191   end
192
193   def self._queue_requires_parameters
194     self._index_requires_parameters
195   end
196
197   protected
198
199   def load_job_specific_filters
200     # Convert Job-specific @filters entries into general SQL filters.
201     script_info = {"repository" => nil, "script" => nil}
202     git_filters = Hash.new do |hash, key|
203       hash[key] = {"max_version" => "HEAD", "exclude_versions" => []}
204     end
205     @filters.select! do |(attr, operator, operand)|
206       if (script_info.has_key? attr) and (operator == "=")
207         if script_info[attr].nil?
208           script_info[attr] = operand
209         elsif script_info[attr] != operand
210           raise ArgumentError.new("incompatible #{attr} filters")
211         end
212       end
213       case operator
214       when "in git"
215         git_filters[attr]["min_version"] = operand
216         false
217       when "not in git"
218         git_filters[attr]["exclude_versions"] += Array.wrap(operand)
219         false
220       when "in docker", "not in docker"
221         image_hashes = Array.wrap(operand).flat_map do |search_term|
222           image_search, image_tag = search_term.split(':', 2)
223           Collection.
224             find_all_for_docker_image(image_search, image_tag, @read_users).
225             map(&:portable_data_hash)
226         end
227         @filters << [attr, operator.sub(/ docker$/, ""), image_hashes]
228         false
229       else
230         true
231       end
232     end
233
234     # Build a real script_version filter from any "not? in git" filters.
235     git_filters.each_pair do |attr, filter|
236       case attr
237       when "script_version"
238         script_info.each_pair do |key, value|
239           if value.nil?
240             raise ArgumentError.new("script_version filter needs #{key} filter")
241           end
242         end
243         filter["repository"] = script_info["repository"]
244         begin
245           filter["max_version"] = resource_attrs[:script_version]
246         rescue
247           # Using HEAD, set earlier by the hash default, is fine.
248         end
249       when "arvados_sdk_version"
250         filter["repository"] = "arvados"
251       else
252         raise ArgumentError.new("unknown attribute for git filter: #{attr}")
253       end
254       version_range = Commit.find_commit_range(current_user,
255                                                filter["repository"],
256                                                filter["min_version"],
257                                                filter["max_version"],
258                                                filter["exclude_versions"])
259       if version_range.nil?
260         raise ArgumentError.
261           new("error searching #{filter['repository']} from " +
262               "'#{filter['min_version']}' to '#{filter['max_version']}', " +
263               "excluding #{filter['exclude_versions']}")
264       end
265       @filters.append([attr, "in", version_range])
266     end
267   end
268
269   def load_filters_param
270     begin
271       super
272       load_job_specific_filters
273     rescue ArgumentError => error
274       send_error(error.message)
275       false
276     end
277   end
278 end