20447: Don't lock containers table for CR name/description updates.
[arvados.git] / services / api / app / controllers / arvados / v1 / container_requests_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::ContainerRequestsController < ApplicationController
6   accept_attribute_as_json :environment, Hash
7   accept_attribute_as_json :mounts, Hash
8   accept_attribute_as_json :runtime_constraints, Hash
9   accept_attribute_as_json :command, Array
10   accept_attribute_as_json :filters, Array
11   accept_attribute_as_json :scheduling_parameters, Hash
12   accept_attribute_as_json :secret_mounts, Hash
13
14   def self._index_requires_parameters
15     (super rescue {}).
16       merge({
17         include_trash: {
18           type: 'boolean', required: false, default: false, description: "Include container requests whose owner project is trashed.",
19         },
20       })
21   end
22
23   def self._show_requires_parameters
24     (super rescue {}).
25       merge({
26         include_trash: {
27           type: 'boolean', required: false, default: false, description: "Show container request even if its owner project is trashed.",
28         },
29       })
30   end
31
32   def create
33     # Lock containers table to avoid deadlock in cascading priority update (see #20240)
34     Container.transaction do
35       ActiveRecord::Base.connection.execute "LOCK TABLE containers IN EXCLUSIVE MODE"
36       super
37     end
38   end
39
40   def update
41     if (resource_attrs.keys - [:owner_uuid, :name, :description, :properties]).empty?
42       # If no attributes are being updated besides these, there are no
43       # cascading changes to other rows/tables, so we should just use
44       # row locking.
45       @object.reload(lock: true)
46       super
47     else
48       # Lock containers table to avoid deadlock in cascading priority update (see #20240)
49       Container.transaction do
50         ActiveRecord::Base.connection.execute "LOCK TABLE containers IN EXCLUSIVE MODE"
51         super
52       end
53     end
54   end
55 end