OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
employ advisory lock for all update services
ulferts0cda1869a808
diff --git a/app/models/journal_manager.rb b/app/models/journal_manager.rbindex 2e8d9f6cac2..c0316a0dfd6 100644--- a/app/models/journal_manager.rb+++ b/app/models/journal_manager.rb@@ -45,14 +45,6 @@ class JournalManager@send_notification = trueend- def with_advisory_lock_transaction(container)- ActiveRecord::Base.transaction do- container.class.with_advisory_lock("create_journal_on_#{container.class.name}_#{container.id}") do- yield- end- end- end-privatedef merge_reference_journals_by_id(new_journals, old_journals, id_key, value)@@ -261,6 +253,7 @@ class JournalManagerjournable.journals.select(&:new_record?)journal.save!+journalenddiff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rbindex 7429727bf31..e7135a956f6 100644--- a/app/services/attachments/create_service.rb+++ b/app/services/attachments/create_service.rb@@ -40,7 +40,7 @@ class Attachments::CreateService## An ActiveRecord::RecordInvalid error is raised if any record can't be saved.def call(uploaded_file:, description:)- if container.respond_to? :add_journal+ if JournalManager.journalized?(container)save_journalized(uploaded_file, description)elsesave_unjournalized(uploaded_file, description)@@ -50,7 +50,7 @@ class Attachments::CreateServiceprivatedef save_journalized(uploaded_file, description)- JournalManager.with_advisory_lock_transaction(container) do+ OpenProject::Mutex.with_advisory_lock_transaction(container) do# Get the latest attachments to ensure having them all for journalization.# A different worker might have added attachments in the meantime, e.g when bulk uploadingcontainer.attachments.reloaddiff --git a/app/services/base_services/base_contracted.rb b/app/services/base_services/base_contracted.rbindex 6674b7c0a34..9f6031d59b0 100644--- a/app/services/base_services/base_contracted.rb+++ b/app/services/base_services/base_contracted.rb@@ -42,7 +42,7 @@ module BaseServicesenddef call(params = nil)- in_context(false) do+ in_context(model, true) doperform(params)endenddiff --git a/app/services/base_services/create.rb b/app/services/base_services/create.rbindex b7dbebbe5d8..809d260007b 100644--- a/app/services/base_services/create.rb+++ b/app/services/base_services/create.rb@@ -30,6 +30,12 @@module BaseServicesclass Create < Write+ def call(params = nil)+ in_user_context(true) do+ perform(params)+ end+ end+privatedef instance(_params)diff --git a/app/services/relations/create_service.rb b/app/services/relations/create_service.rbindex 336e199280a..57b55170d65 100644--- a/app/services/relations/create_service.rb+++ b/app/services/relations/create_service.rb@@ -35,7 +35,7 @@ class Relations::CreateService < Relations::BaseServiceenddef call(relation, send_notifications: true)- in_context(send_notifications) do+ in_user_context(send_notifications) doupdate_relation relation, {}endenddiff --git a/app/services/shared/service_context.rb b/app/services/shared/service_context.rbindex dab712d1a9b..1e8cdb85186 100644--- a/app/services/shared/service_context.rb+++ b/app/services/shared/service_context.rb@@ -30,12 +30,28 @@module Sharedmodule ServiceContext- def in_context(send_notifications)+ private++ def in_context(model, send_notifications, &block)+ if model+ in_mutex_context(model, send_notifications, &block)+ else+ in_user_context(send_notifications, &block)+ end+ end++ def in_mutex_context(model, send_notifications, &block)+ OpenProject::Mutex.with_advisory_lock_transaction(model) do+ in_user_context(send_notifications, &block)+ end+ end++ def in_user_context(send_notifications)result = nilActiveRecord::Base.transaction doUser.execute_as user do- JournalManager.with_send_notifications send_notifications do+ JournalManager.with_send_notifications(send_notifications) doresult = yieldif result.failure?diff --git a/app/services/time_entries/create_service.rb b/app/services/time_entries/create_service.rbindex dbaae11c2f8..efe7b2a74cb 100644--- a/app/services/time_entries/create_service.rb+++ b/app/services/time_entries/create_service.rb@@ -29,7 +29,6 @@#++class TimeEntries::CreateService < ::BaseServices::Create-def after_perform(call)OpenProject::Notifications.send(OpenProject::Events::NEW_TIME_ENTRY_CREATED,diff --git a/app/services/work_packages/create_service.rb b/app/services/work_packages/create_service.rbindex 8a4ec43f7d7..81285f88a54 100644--- a/app/services/work_packages/create_service.rb+++ b/app/services/work_packages/create_service.rb@@ -43,7 +43,7 @@ class WorkPackages::CreateServicedef call(work_package: WorkPackage.new,send_notifications: true,**attributes)- in_context(send_notifications) do+ in_user_context(send_notifications) docreate(attributes, work_package)endenddiff --git a/app/services/work_packages/update_service.rb b/app/services/work_packages/update_service.rbindex e50b052c430..b5b54d58f1f 100644--- a/app/services/work_packages/update_service.rb+++ b/app/services/work_packages/update_service.rb@@ -43,7 +43,7 @@ class WorkPackages::UpdateServiceenddef call(send_notifications: true, **attributes)- in_context(send_notifications) do+ in_context(model, send_notifications) doupdate(attributes)endenddiff --git a/lib/open_project/info.rb b/lib/open_project/info.rbindex 46d2074f158..ca3703ac649 100644--- a/lib/open_project/info.rb+++ b/lib/open_project/info.rb@@ -1,4 +1,5 @@#-- encoding: UTF-8+#-- copyright# OpenProject is an open source project management software.# Copyright (C) 2012-2020 the OpenProject GmbHdiff --git a/lib/open_project/mutex.rb b/lib/open_project/mutex.rbnew file mode 100644index 00000000000..39c86d48bda--- /dev/null+++ b/lib/open_project/mutex.rb@@ -0,0 +1,89 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module OpenProject+ module Mutex+ module_function++ # Adds a mutex on database level via advisory locks:+ # https://www.postgresql.org/docs/12/explicit-locking.html+ # employing the with_advisory_lock gem.+ # This leads to requests received in parallel by two different workers, which might be in different OS processes+ # on different hosts, to be executed sequentially.+ # The locks are, from an abstract perspective, on row level as the entry's id is taken to create the lock.+ # Other entries of the same class can still be created/updated.+ # Relying on explicit row level locking (e.g. ROW EXCLUSIVE FOR UPDATE) would not work, I believe, as it would still+ # allow reading. Allowing reading is desirable in some cases, e.g. when fetching for the work package list, but would not+ # prevent the creation of journals. The journals transaction could not be committed, but the faulty view of the entries+ # current state could be committed anyway afterwards. See below for details.+ #+ # We in particular have to wait until the transaction is complete.+ # Otherwise the following is e.g. possible:+ # Action 1 writes an attachment and releases the lock.+ # Action 2, running in parallel, writes an attachment and releases the lock.+ # Action 3 updates something, some time later.+ # Both have not yet committed their transaction so, given that we have the "READ COMMITTED" transaction level,+ # the two actions cannot yet see the attachment added by the respective other action.+ # Both actions now proceed to write their journals, which will create attachable journals based on the attachments+ # at that time. But given that both do not see the attachment added by the other action yet, they only add+ # attachable journals for the attachment added by themselves. To the user this will look as if one of the actions+ # deleted the other attachment. The next action, Action 3, will then seem to have readded the attachment,+ # seemingly removed before.+ def with_advisory_lock_transaction(entry, &block)+ ActiveRecord::Base.transaction do+ with_advisory_lock(entry, &block)+ end+ end++ def with_advisory_lock(entry)+ lock_name = "mutex_on_#{entry.class.name}_#{entry.id}"++ debug_log("Attempting to fetched advisory lock", lock_name)+ result = entry.class.with_advisory_lock(lock_name, transaction: true) do+ debug_log("Fetched advisory lock", lock_name)+ yield+ end+ debug_log("Released advisory lock", lock_name)++ result+ end++ def debug_log(action, lock_name)+ message = <<~MESSAGE+ #{action}:+ * lockname: #{lock_name}+ * thread: #{Thread.current.object_id}+ * held locks: #{WithAdvisoryLock::Base.lock_stack.map(&:name).join(', ')}+ MESSAGE++ Rails.logger.debug { message }+ end+ end+enddiff --git a/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb b/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rbindex f26d088f6b8..15c404edc19 100644--- a/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb+++ b/lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb@@ -157,6 +157,8 @@ module Redmine(persisted? && allowed_to_on_attachment?(user, self.class.attachable_options[:add_on_persisted_permission]))end+ # TODO: Check if we can remove this.+ # If the attachments where only added via the Attachments::CreateService, it would be thread safe.# Bulk attaches a set of files to an objectdef attach_files(attachments)return unless attachments&.is_a?(Hash)diff --git a/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/save_hooks.rb b/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/save_hooks.rbindex 6d71dd37e23..ebc6120b6a5 100644--- a/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/save_hooks.rb+++ b/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/save_hooks.rb@@ -74,9 +74,9 @@ module Redmine::Acts::Journalizedadd_journal = journals.empty? || JournalManager.changed?(self) || !@journal_notes.empty?- journal = JournalManager.add_journal! self, @journal_user, @journal_notes if add_journal-if add_journal+ journal = JournalManager.add_journal!(self, @journal_user, @journal_notes)+OpenProject::Notifications.send('journal_created',journal: journal,send_notification: JournalManager.send_notification)