Merge branch '20984-instance-capacity'
[arvados.git] / services / api / lib / update_priorities.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 def row_lock_for_priority_update container_uuid
6   # Locks all the containers under this container, and also any
7   # immediate parent containers.  This ensures we have locked
8   # everything that gets touched by either a priority update or state
9   # update.
10   ActiveRecord::Base.connection.exec_query %{
11         select 1 from containers where containers.uuid in (
12   select pri_container_uuid from container_tree($1)
13 UNION
14   select container_requests.requesting_container_uuid from container_requests
15     where container_requests.container_uuid = $1
16           and container_requests.state = 'Committed'
17           and container_requests.requesting_container_uuid is not NULL
18 )
19         order by containers.uuid for update
20   }, 'select_for_update_priorities', [container_uuid]
21 end
22
23 def update_priorities starting_container_uuid
24   # Ensure the row locks were taken in order
25   row_lock_for_priority_update starting_container_uuid
26
27   ActiveRecord::Base.connection.exec_query %{
28 update containers set priority=computed.upd_priority from container_tree_priorities($1) as computed
29  where containers.uuid = computed.pri_container_uuid and priority != computed.upd_priority
30 }, 'update_priorities', [starting_container_uuid]
31 end