From 1ba4c76f70f0f3d68e01fc548b0b7de0d43723ca Mon Sep 17 00:00:00 2001 From: Peter Amstutz Date: Thu, 9 Mar 2023 16:36:09 -0500 Subject: [PATCH] 20223: Try to make priority update much lighter weight Uses select and pluck in several places to avoid loading full records. Arvados-DCO-1.1-Signed-off-by: Peter Amstutz --- services/api/app/models/container.rb | 49 ++++++++++---------- services/api/app/models/container_request.rb | 8 ++-- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/services/api/app/models/container.rb b/services/api/app/models/container.rb index 7837e0812b..b59604cb22 100644 --- a/services/api/app/models/container.rb +++ b/services/api/app/models/container.rb @@ -131,12 +131,12 @@ class Container < ArvadosModel def update_priority! return if ![Queued, Locked, Running].include?(state) p = ContainerRequest. - where('container_uuid=? and priority>0', uuid). - includes(:requesting_container). - lock(true). - map do |cr| - if cr.requesting_container - cr.requesting_container.priority + where('container_uuid=? and priority>0', uuid). + select("priority, requesting_container_uuid, created_at"). + lock(true). + map do |cr| + if cr.requesting_container_uuid + Container.where(uuid: cr.requesting_container_uuid).pluck(:priority).first else (cr.priority << 50) - (cr.created_at.to_time.to_f * 1000).to_i end @@ -151,12 +151,11 @@ class Container < ArvadosModel # priority of the parent container (ignoring requests with no # container assigned, because their priority doesn't matter). ContainerRequest. - where(requesting_container_uuid: self.uuid, - state: ContainerRequest::Committed). - where('container_uuid is not null'). - includes(:container). - map(&:container). - map(&:update_priority!) + where('requesting_container_uuid = ? and state = ? and container_uuid is not null', + self.uuid, ContainerRequest::Committed). + pluck(:container_uuid).each do |container_uuid| + Container.find_by_uuid(container_uuid).update_priority! + end end end @@ -830,19 +829,21 @@ class Container < ArvadosModel # Cancel outstanding container requests made by this container. ContainerRequest. - includes(:container). where(requesting_container_uuid: uuid, - state: ContainerRequest::Committed).each do |cr| - leave_modified_by_user_alone do - cr.update_attributes!(priority: 0) - cr.container.reload - if cr.container.state == Container::Queued || cr.container.state == Container::Locked - # If the child container hasn't started yet, finalize the - # child CR now instead of leaving it "on hold", i.e., - # Queued with priority 0. (OTOH, if the child is already - # running, leave it alone so it can get cancelled the - # usual way, get a copy of the log collection, etc.) - cr.update_attributes!(state: ContainerRequest::Final) + state: ContainerRequest::Committed). + find_in_batches(batch_size: 10) do |batch| + batch.each do |cr| + leave_modified_by_user_alone do + cr.set_priority_zero + container_state = Container.where(uuid: cr.container_uuid).pluck(:state).first + if container_state == Container::Queued || container_state == Container::Locked + # If the child container hasn't started yet, finalize the + # child CR now instead of leaving it "on hold", i.e., + # Queued with priority 0. (OTOH, if the child is already + # running, leave it alone so it can get cancelled the + # usual way, get a copy of the log collection, etc.) + cr.update_attributes!(state: ContainerRequest::Final) + end end end end diff --git a/services/api/app/models/container_request.rb b/services/api/app/models/container_request.rb index 0701adb328..ae50f55748 100644 --- a/services/api/app/models/container_request.rb +++ b/services/api/app/models/container_request.rb @@ -300,6 +300,10 @@ class ContainerRequest < ArvadosModel super - ["mounts", "secret_mounts", "secret_mounts_md5", "runtime_token", "output_storage_classes"] end + def set_priority_zero + self.update_attributes!(priority: 0) if self.priority > 0 && self.state != Final + end + protected def fill_field_defaults @@ -564,10 +568,6 @@ class ContainerRequest < ArvadosModel end end - def set_priority_zero - self.update_attributes!(priority: 0) if self.state != Final - end - def set_requesting_container_uuid if (self.requesting_container_uuid = get_requesting_container_uuid()) # Determine the priority of container request for the requesting -- 2.30.2