8018: Add container_count field.
[arvados.git] / services / api / app / models / container.rb
index 2d3402f6bab2a04572f25444c7609080bef25f22..c1c3eae94b7769f35c90e874b0805e537847da71 100644 (file)
@@ -85,51 +85,64 @@ class Container < ArvadosModel
       where('output_path = ?', attrs[:output_path]).
       where('container_image = ?', attrs[:container_image]).
       where('mounts = ?', self.deep_sort_hash(attrs[:mounts]).to_yaml).
-      where('runtime_constraints = ?', self.deep_sort_hash(attrs[:runtime_constraints]).to_yaml).
-      where('state in (?)', [Container::Queued, Container::Locked,
-                             Container::Running, Container::Complete]).
-      reject {|c| c.state == Container::Complete and (c.exit_code != 0 or
-                                                      c.output.nil? or
-                                                      c.log.nil?)}
-    if candidates.empty?
-      nil
-    elsif candidates.count == 1
-      candidates.first
-    else
-      # Multiple candidates found, search for the best one:
-      # The most recent completed container
-      winner = candidates.select {|c| c.state == Container::Complete}.
-        sort_by {|c| c.finished_at}.last
-      return winner if not winner.nil?
-      # The running container that's most likely to finish sooner.
-      winner = candidates.select {|c| c.state == Container::Running}.
-        sort {|a, b| [b.progress, a.started_at] <=> [a.progress, b.started_at]}.first
-      return winner if not winner.nil?
-      # The locked container that's most likely to start sooner.
-      winner = candidates.select {|c| c.state == Container::Locked}.
-        sort {|a, b| [b.priority, a.created_at] <=> [a.priority, b.created_at]}.first
-      return winner if not winner.nil?
-      # The queued container that's most likely to start sooner.
-      winner = candidates.select {|c| c.state == Container::Queued}.
-        sort {|a, b| [b.priority, a.created_at] <=> [a.priority, b.created_at]}.first
-      return winner if not winner.nil?
+      where('runtime_constraints = ?', self.deep_sort_hash(attrs[:runtime_constraints]).to_yaml)
+
+    # Check for Completed candidates that only had consistent outputs.
+    completed = candidates.where(state: Complete).where(exit_code: 0)
+    if completed.select("output").group('output').limit(2).length == 1
+      return completed.order('finished_at asc').limit(1).first
     end
+
+    # Check for Running candidates and return the most likely to finish sooner.
+    running = candidates.where(state: Running).
+      order('progress desc, started_at asc').limit(1).first
+    return running if not running.nil?
+
+    # Check for Locked or Queued ones and return the most likely to start first.
+    locked_or_queued = candidates.where("state IN (?)", [Locked, Queued]).
+      order('state asc, priority desc, created_at asc').limit(1).first
+    return locked_or_queued if not locked_or_queued.nil?
+
+    # No suitable candidate found.
+    nil
   end
 
-  protected
+  def lock
+    with_lock do
+      if self.state == Locked
+        raise AlreadyLockedError
+      end
+      self.state = Locked
+      self.save!
+    end
+  end
 
-  def self.deep_sort_hash(x)
-    if x.is_a? Hash
-      x.sort.collect do |k, v|
-        [k, deep_sort_hash(v)]
-      end.to_h
-    elsif x.is_a? Array
-      x.collect { |v| deep_sort_hash(v) }
-    else
-      x
+  def unlock
+    with_lock do
+      if self.state == Queued
+        raise InvalidStateTransitionError
+      end
+      self.state = Queued
+      self.save!
+    end
+  end
+
+  def self.readable_by(*users_list)
+    if users_list.select { |u| u.is_admin }.any?
+      return self
     end
+    user_uuids = users_list.map { |u| u.uuid }
+    uuid_list = user_uuids + users_list.flat_map { |u| u.groups_i_can(:read) }
+    uuid_list.uniq!
+    permitted = "(SELECT head_uuid FROM links WHERE link_class='permission' AND tail_uuid IN (:uuids))"
+    joins(:container_requests).
+      where("container_requests.uuid IN #{permitted} OR "+
+            "container_requests.owner_uuid IN (:uuids)",
+            uuids: uuid_list)
   end
 
+  protected
+
   def fill_field_defaults
     self.state ||= Queued
     self.environment ||= {}
@@ -253,9 +266,15 @@ class Container < ArvadosModel
   end
 
   def sort_serialized_attrs
-    self.environment = self.class.deep_sort_hash(self.environment)
-    self.mounts = self.class.deep_sort_hash(self.mounts)
-    self.runtime_constraints = self.class.deep_sort_hash(self.runtime_constraints)
+    if self.environment_changed?
+      self.environment = self.class.deep_sort_hash(self.environment)
+    end
+    if self.mounts_changed?
+      self.mounts = self.class.deep_sort_hash(self.mounts)
+    end
+    if self.runtime_constraints_changed?
+      self.runtime_constraints = self.class.deep_sort_hash(self.runtime_constraints)
+    end
   end
 
   def handle_completed