mirror of
https://github.com/redmine/redmine.git
synced 2025-11-02 11:25:55 +01:00
Use a global lock provided by @with_advisory_lock@ gem to work around deadlock issues when MySQL >= 5.7 (#39437).
Patch by Jens Krämer. git-svn-id: https://svn.redmine.org/redmine/trunk@22458 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -69,6 +69,7 @@ if File.exist?(database_file)
|
|||||||
case adapter
|
case adapter
|
||||||
when 'mysql2'
|
when 'mysql2'
|
||||||
gem "mysql2", "~> 0.5.0", :platforms => [:mri, :mingw, :x64_mingw]
|
gem "mysql2", "~> 0.5.0", :platforms => [:mri, :mingw, :x64_mingw]
|
||||||
|
gem "with_advisory_lock"
|
||||||
when /postgresql/
|
when /postgresql/
|
||||||
gem 'pg', '~> 1.5.3', :platforms => [:mri, :mingw, :x64_mingw]
|
gem 'pg', '~> 1.5.3', :platforms => [:mri, :mingw, :x64_mingw]
|
||||||
when /sqlite3/
|
when /sqlite3/
|
||||||
|
|||||||
@@ -51,7 +51,14 @@ module Redmine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_to_nested_set(lock=true)
|
def add_to_nested_set(lock=true)
|
||||||
lock_nested_set if lock
|
if lock
|
||||||
|
lock_nested_set { add_to_nested_set_without_lock }
|
||||||
|
else
|
||||||
|
add_to_nested_set_without_lock
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_to_nested_set_without_lock
|
||||||
parent.send :reload_nested_set_values
|
parent.send :reload_nested_set_values
|
||||||
self.root_id = parent.root_id
|
self.root_id = parent.root_id
|
||||||
self.lft = target_lft
|
self.lft = target_lft
|
||||||
@@ -73,15 +80,16 @@ module Redmine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_parent_change
|
def handle_parent_change
|
||||||
lock_nested_set
|
lock_nested_set do
|
||||||
reload_nested_set_values
|
reload_nested_set_values
|
||||||
if parent_id_was
|
if parent_id_was
|
||||||
remove_from_nested_set
|
remove_from_nested_set
|
||||||
|
end
|
||||||
|
if parent
|
||||||
|
move_to_nested_set
|
||||||
|
end
|
||||||
|
reload_nested_set_values
|
||||||
end
|
end
|
||||||
if parent
|
|
||||||
move_to_nested_set
|
|
||||||
end
|
|
||||||
reload_nested_set_values
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_to_nested_set
|
def move_to_nested_set
|
||||||
@@ -124,20 +132,22 @@ module Redmine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy_children
|
def destroy_children
|
||||||
unless @without_nested_set_update
|
if @without_nested_set_update
|
||||||
lock_nested_set
|
children.each {|c| c.send :destroy_without_nested_set_update}
|
||||||
reload_nested_set_values
|
reload
|
||||||
end
|
else
|
||||||
children.each {|c| c.send :destroy_without_nested_set_update}
|
lock_nested_set do
|
||||||
reload
|
reload_nested_set_values
|
||||||
unless @without_nested_set_update
|
children.each {|c| c.send :destroy_without_nested_set_update}
|
||||||
self.class.where(:root_id => root_id).where("lft > ? OR rgt > ?", lft, lft).update_all(
|
reload
|
||||||
[
|
self.class.where(:root_id => root_id).where("lft > ? OR rgt > ?", lft, lft).update_all(
|
||||||
"lft = CASE WHEN lft > :lft THEN lft - :shift ELSE lft END, " +
|
[
|
||||||
"rgt = CASE WHEN rgt > :lft THEN rgt - :shift ELSE rgt END",
|
"lft = CASE WHEN lft > :lft THEN lft - :shift ELSE lft END, " +
|
||||||
{:lft => lft, :shift => rgt - lft + 1}
|
"rgt = CASE WHEN rgt > :lft THEN rgt - :shift ELSE rgt END",
|
||||||
]
|
{:lft => lft, :shift => rgt - lft + 1}
|
||||||
)
|
]
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -166,9 +176,26 @@ module Redmine
|
|||||||
# before locking
|
# before locking
|
||||||
sets_to_lock = [root_id, parent.try(:root_id)].compact.uniq
|
sets_to_lock = [root_id, parent.try(:root_id)].compact.uniq
|
||||||
self.class.reorder(:id).where(:root_id => sets_to_lock).lock(lock).ids
|
self.class.reorder(:id).where(:root_id => sets_to_lock).lock(lock).ids
|
||||||
|
yield
|
||||||
|
elsif Redmine::Database.mysql?
|
||||||
|
# Use a global lock to prevent concurrent modifications - MySQL row locks are broken, this will run into
|
||||||
|
# deadlock errors all the time otherwise.
|
||||||
|
# Trying to lock just the sets in question (by basing the lock name on root_id and parent&.root_id) will run
|
||||||
|
# into the same issues as the sqlserver branch above
|
||||||
|
Issue.with_advisory_lock!("lock_issues", timeout_seconds: 30) do
|
||||||
|
# still lock the issues in question, for good measure
|
||||||
|
sets_to_lock = [id, parent_id].compact
|
||||||
|
inner_join_statement = self.class.select(:root_id).where(id: sets_to_lock).distinct(:root_id).to_sql
|
||||||
|
self.class.reorder(:id).
|
||||||
|
joins("INNER JOIN (#{inner_join_statement}) as i2 ON #{self.class.table_name}.root_id = i2.root_id").
|
||||||
|
lock.ids
|
||||||
|
|
||||||
|
yield
|
||||||
|
end
|
||||||
else
|
else
|
||||||
sets_to_lock = [id, parent_id].compact
|
sets_to_lock = [id, parent_id].compact
|
||||||
self.class.reorder(:id).where("root_id IN (SELECT root_id FROM #{self.class.table_name} WHERE id IN (?))", sets_to_lock).lock.ids
|
self.class.reorder(:id).where("root_id IN (SELECT root_id FROM #{self.class.table_name} WHERE id IN (?))", sets_to_lock).lock.ids
|
||||||
|
yield
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class IssueNestedSetConcurrencyTest < ActiveSupport::TestCase
|
|||||||
self.use_transactional_tests = false
|
self.use_transactional_tests = false
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
skip if sqlite? || mysql?
|
skip if sqlite?
|
||||||
User.current = nil
|
User.current = nil
|
||||||
CustomField.delete_all
|
CustomField.delete_all
|
||||||
end
|
end
|
||||||
@@ -59,21 +59,6 @@ class IssueNestedSetConcurrencyTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_concurrent_subtasks_creation
|
|
||||||
root = Issue.generate!
|
|
||||||
assert_difference 'Issue.count', 30 do
|
|
||||||
threaded(3) do
|
|
||||||
10.times do
|
|
||||||
Issue.generate! :parent_issue_id => root.id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
root.reload
|
|
||||||
assert_equal [1, 62], [root.lft, root.rgt]
|
|
||||||
children_bounds = root.children.sort_by(&:lft).map {|c| [c.lft, c.rgt]}.flatten
|
|
||||||
assert_equal (2..61).to_a, children_bounds
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def threaded(count, &block)
|
def threaded(count, &block)
|
||||||
|
|||||||
Reference in New Issue
Block a user