negval-deadlock_retry 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -0
- data/README +20 -0
- data/Rakefile +26 -0
- data/lib/deadlock_retry.rb +83 -0
- data/test/deadlock_retry_test.rb +88 -0
- metadata +73 -0
data/CHANGELOG
ADDED
data/README
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
= Deadlock Retry
|
2
|
+
|
3
|
+
Deadlock retry allows the database adapter (currently only tested with the
|
4
|
+
MySQLAdapter) to retry transactions that fall into deadlock. It will retry
|
5
|
+
such transactions three times before finally failing.
|
6
|
+
|
7
|
+
This capability is automatically added to ActiveRecord. No code changes or otherwise are required.
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
Add it to your Rails application by installing the gem:
|
12
|
+
|
13
|
+
sudo gem install negval-deadlock_retry
|
14
|
+
|
15
|
+
and including a reference to it in your application's config/environment.rb:
|
16
|
+
|
17
|
+
config.gem 'negval-deadlock_retry', :lib => 'deadlock_retry', :source => 'http://rubygems.com'
|
18
|
+
|
19
|
+
|
20
|
+
Copyright (c) 2005 Jamis Buck, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
desc "Default task"
|
5
|
+
task :default => [ :test ]
|
6
|
+
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
t.test_files = Dir["test/**/*_test.rb"]
|
9
|
+
t.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
begin
|
13
|
+
require 'jeweler'
|
14
|
+
|
15
|
+
Jeweler::Tasks.new do |s|
|
16
|
+
s.name = "negval-deadlock_retry"
|
17
|
+
s.email = "d.sukhonin@gmail.com"
|
18
|
+
s.homepage = "http://github.com/neglectedvalue/deadlock_retry"
|
19
|
+
s.description = s.summary = "Provides automatical deadlock retry and logging functionality for ActiveRecord and MySQL"
|
20
|
+
s.authors = ["Jamis Buck", "Mike Perham", "Denis Sukhonin"]
|
21
|
+
s.files = FileList['README', 'Rakefile', 'version.yml', "{lib,test}/**/*", 'CHANGELOG']
|
22
|
+
end
|
23
|
+
Jeweler::GemcutterTasks.new
|
24
|
+
rescue LoadError
|
25
|
+
# Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com
|
26
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# Copyright (c) 2005 Jamis Buck
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
# a copy of this software and associated documentation files (the
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
# the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be
|
12
|
+
# included in all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
module DeadlockRetry
|
22
|
+
mattr_accessor :maximum_retries_on_deadlock
|
23
|
+
self.maximum_retries_on_deadlock = MAXIMUM_RETRIES_ON_DEADLOCK || 3
|
24
|
+
|
25
|
+
def self.included(base)
|
26
|
+
base.extend(ClassMethods)
|
27
|
+
base.class_eval do
|
28
|
+
class << self
|
29
|
+
alias_method_chain :transaction, :deadlock_handling
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module ClassMethods
|
35
|
+
DEADLOCK_ERROR_MESSAGES = [
|
36
|
+
"Deadlock found when trying to get lock",
|
37
|
+
"Lock wait timeout exceeded"
|
38
|
+
]
|
39
|
+
|
40
|
+
def transaction_with_deadlock_handling(*objects, &block)
|
41
|
+
retry_count = 0
|
42
|
+
|
43
|
+
begin
|
44
|
+
transaction_without_deadlock_handling(*objects, &block)
|
45
|
+
rescue ActiveRecord::StatementInvalid => error
|
46
|
+
raise if in_nested_transaction?
|
47
|
+
if DEADLOCK_ERROR_MESSAGES.any? { |msg| error.message =~ /#{Regexp.escape(msg)}/ }
|
48
|
+
logger.info "Deadlock detected on retry #{retry_count}, restarting transaction"
|
49
|
+
log_innodb_status
|
50
|
+
raise if retry_count >= DeadlockRetry.maximum_retries_on_deadlock
|
51
|
+
retry_count += 1
|
52
|
+
retry
|
53
|
+
else
|
54
|
+
raise
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def in_nested_transaction?
|
62
|
+
cn = connection
|
63
|
+
# open_transactions was added in 2.2's connection pooling changes.
|
64
|
+
cn.respond_to?(:open_transactions) && cn.open_transactions != 0
|
65
|
+
end
|
66
|
+
|
67
|
+
def log_innodb_status
|
68
|
+
# show innodb status is the only way to get visiblity into why
|
69
|
+
# the transaction deadlocked. log it.
|
70
|
+
lines = connection.select_value("show innodb status")
|
71
|
+
logger.warn "INNODB Status follows:"
|
72
|
+
lines.each_line do |line|
|
73
|
+
logger.warn line
|
74
|
+
end
|
75
|
+
rescue Exception => e
|
76
|
+
# Access denied, ignore
|
77
|
+
logger.warn "Cannot log innodb status: #{e.message}"
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
ActiveRecord::Base.send(:include, DeadlockRetry)
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_record/base'
|
5
|
+
require 'active_record/version'
|
6
|
+
puts "Testing ActiveRecord #{ActiveRecord::VERSION::STRING}"
|
7
|
+
|
8
|
+
require 'test/unit'
|
9
|
+
require "#{File.dirname(__FILE__)}/../lib/deadlock_retry"
|
10
|
+
|
11
|
+
class MockModel
|
12
|
+
@@open_transactions = 0
|
13
|
+
|
14
|
+
def self.transaction(*objects)
|
15
|
+
@@open_transactions += 1
|
16
|
+
yield
|
17
|
+
ensure
|
18
|
+
@@open_transactions -= 1
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.open_transactions
|
22
|
+
@@open_transactions
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.connection
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.logger
|
30
|
+
@logger ||= Logger.new(nil)
|
31
|
+
end
|
32
|
+
|
33
|
+
include DeadlockRetry
|
34
|
+
end
|
35
|
+
|
36
|
+
class DeadlockRetryTest < Test::Unit::TestCase
|
37
|
+
DEADLOCK_ERROR = "MySQL::Error: Deadlock found when trying to get lock"
|
38
|
+
TIMEOUT_ERROR = "MySQL::Error: Lock wait timeout exceeded"
|
39
|
+
|
40
|
+
def test_no_errors
|
41
|
+
assert_equal :success, MockModel.transaction { :success }
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_no_errors_with_deadlock
|
45
|
+
errors = [ DEADLOCK_ERROR ] * 3
|
46
|
+
assert_equal :success, MockModel.transaction { raise ActiveRecord::StatementInvalid, errors.shift unless errors.empty?; :success }
|
47
|
+
assert errors.empty?
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_no_errors_with_lock_timeout
|
51
|
+
errors = [ TIMEOUT_ERROR ] * 3
|
52
|
+
assert_equal :success, MockModel.transaction { raise ActiveRecord::StatementInvalid, errors.shift unless errors.empty?; :success }
|
53
|
+
assert errors.empty?
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_error_if_limit_exceeded
|
57
|
+
assert_raise(ActiveRecord::StatementInvalid) do
|
58
|
+
MockModel.transaction { raise ActiveRecord::StatementInvalid, DEADLOCK_ERROR }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_error_if_unrecognized_error
|
63
|
+
assert_raise(ActiveRecord::StatementInvalid) do
|
64
|
+
MockModel.transaction { raise ActiveRecord::StatementInvalid, "Something else" }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_included_by_default
|
69
|
+
assert ActiveRecord::Base.respond_to?(:transaction_with_deadlock_handling)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_error_in_nested_transaction_should_retry_outermost_transaction
|
73
|
+
tries = 0
|
74
|
+
errors = 0
|
75
|
+
|
76
|
+
MockModel.transaction do
|
77
|
+
tries += 1
|
78
|
+
MockModel.transaction do
|
79
|
+
MockModel.transaction do
|
80
|
+
errors += 1
|
81
|
+
raise ActiveRecord::StatementInvalid, "MySQL::Error: Lock wait timeout exceeded" unless errors > 3
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
assert_equal 4, tries
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: negval-deadlock_retry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jamis Buck
|
14
|
+
- Mike Perham
|
15
|
+
- Denis Sukhonin
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2010-10-27 00:00:00 +04:00
|
21
|
+
default_executable:
|
22
|
+
dependencies: []
|
23
|
+
|
24
|
+
description: Provides automatical deadlock retry and logging functionality for ActiveRecord and MySQL
|
25
|
+
email: d.sukhonin@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
files:
|
33
|
+
- CHANGELOG
|
34
|
+
- README
|
35
|
+
- Rakefile
|
36
|
+
- lib/deadlock_retry.rb
|
37
|
+
- test/deadlock_retry_test.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/neglectedvalue/deadlock_retry
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.7
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Provides automatical deadlock retry and logging functionality for ActiveRecord and MySQL
|
72
|
+
test_files:
|
73
|
+
- test/deadlock_retry_test.rb
|