mperham-deadlock_retry 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ deadlock_retry changes
2
+
3
+ == v1.0 - (2009-02-07)
4
+
5
+ * Add INNODB status logging for debugging deadlock issues.
6
+ * Clean up so the code will run as a gem plugin.
7
+ * Small fix for ActiveRecord 2.1.x compatibility.
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 mperham-deadlock_retry
14
+
15
+ and including a reference to it in your application's config/environment.rb:
16
+
17
+ config.gem 'mperham-deadlock_retry', :lib => 'deadlock_retry', :source => 'http://gems.github.com'
18
+
19
+
20
+ Copyright (c) 2005 Jamis Buck, released under the MIT license
@@ -0,0 +1,25 @@
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 = "deadlock_retry"
17
+ s.email = "mperham@gmail.com"
18
+ s.homepage = "http://github.com/mperham/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"]
21
+ s.files = FileList['README', 'Rakefile', 'version.yml', "{lib,test}/**/*", 'CHANGELOG']
22
+ end
23
+ rescue LoadError
24
+ # Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com
25
+ 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
+
23
+ def self.included(base)
24
+ base.extend(ClassMethods)
25
+ base.class_eval do
26
+ class << self
27
+ alias_method_chain :transaction, :deadlock_handling
28
+ end
29
+ end
30
+ end
31
+
32
+ module ClassMethods
33
+ DEADLOCK_ERROR_MESSAGES = [
34
+ "Deadlock found when trying to get lock",
35
+ "Lock wait timeout exceeded"
36
+ ]
37
+
38
+ MAXIMUM_RETRIES_ON_DEADLOCK = 3
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
+ raise if retry_count >= MAXIMUM_RETRIES_ON_DEADLOCK
49
+ retry_count += 1
50
+ logger.info "Deadlock detected on retry #{retry_count}, restarting transaction"
51
+ log_innodb_status
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,89 @@
1
+ require 'rubygems'
2
+
3
+ # Change the version if you want to test a different version of ActiveRecord
4
+ gem 'activerecord', '2.2.2'
5
+ require 'active_record'
6
+ require 'active_record/version'
7
+ puts "Testing ActiveRecord #{ActiveRecord::VERSION::STRING}"
8
+
9
+ require 'test/unit'
10
+ require "#{File.dirname(__FILE__)}/../lib/deadlock_retry"
11
+
12
+ class MockModel
13
+ @@open_transactions = 0
14
+
15
+ def self.transaction(*objects)
16
+ @@open_transactions += 1
17
+ yield
18
+ ensure
19
+ @@open_transactions -= 1
20
+ end
21
+
22
+ def self.open_transactions
23
+ @@open_transactions
24
+ end
25
+
26
+ def self.connection
27
+ self
28
+ end
29
+
30
+ def self.logger
31
+ @logger ||= Logger.new(nil)
32
+ end
33
+
34
+ include DeadlockRetry
35
+ end
36
+
37
+ class DeadlockRetryTest < Test::Unit::TestCase
38
+ DEADLOCK_ERROR = "MySQL::Error: Deadlock found when trying to get lock"
39
+ TIMEOUT_ERROR = "MySQL::Error: Lock wait timeout exceeded"
40
+
41
+ def test_no_errors
42
+ assert_equal :success, MockModel.transaction { :success }
43
+ end
44
+
45
+ def test_no_errors_with_deadlock
46
+ errors = [ DEADLOCK_ERROR ] * 3
47
+ assert_equal :success, MockModel.transaction { raise ActiveRecord::StatementInvalid, errors.shift unless errors.empty?; :success }
48
+ assert errors.empty?
49
+ end
50
+
51
+ def test_no_errors_with_lock_timeout
52
+ errors = [ TIMEOUT_ERROR ] * 3
53
+ assert_equal :success, MockModel.transaction { raise ActiveRecord::StatementInvalid, errors.shift unless errors.empty?; :success }
54
+ assert errors.empty?
55
+ end
56
+
57
+ def test_error_if_limit_exceeded
58
+ assert_raise(ActiveRecord::StatementInvalid) do
59
+ MockModel.transaction { raise ActiveRecord::StatementInvalid, DEADLOCK_ERROR }
60
+ end
61
+ end
62
+
63
+ def test_error_if_unrecognized_error
64
+ assert_raise(ActiveRecord::StatementInvalid) do
65
+ MockModel.transaction { raise ActiveRecord::StatementInvalid, "Something else" }
66
+ end
67
+ end
68
+
69
+ def test_included_by_default
70
+ assert ActiveRecord::Base.respond_to?(:transaction_with_deadlock_handling)
71
+ end
72
+
73
+ def test_error_in_nested_transaction_should_retry_outermost_transaction
74
+ tries = 0
75
+ errors = 0
76
+
77
+ MockModel.transaction do
78
+ tries += 1
79
+ MockModel.transaction do
80
+ MockModel.transaction do
81
+ errors += 1
82
+ raise ActiveRecord::StatementInvalid, "MySQL::Error: Lock wait timeout exceeded" unless errors > 3
83
+ end
84
+ end
85
+ end
86
+
87
+ assert_equal 4, tries
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mperham-deadlock_retry
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jamis Buck
8
+ - Mike Perham
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-02-07 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Provides automatical deadlock retry and logging functionality for ActiveRecord and MySQL
18
+ email: mperham@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - README
27
+ - Rakefile
28
+ - version.yml
29
+ - lib/deadlock_retry.rb
30
+ - test/deadlock_retry_test.rb
31
+ - CHANGELOG
32
+ has_rdoc: true
33
+ homepage: http://github.com/mperham/deadlock_retry
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --inline-source
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Provides automatical deadlock retry and logging functionality for ActiveRecord and MySQL
59
+ test_files: []
60
+