forked from rails/deadlock_retry
-
Notifications
You must be signed in to change notification settings - Fork 28
/
deadlock_retry.rb
107 lines (89 loc) · 3.04 KB
/
deadlock_retry.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require 'active_support/core_ext/module/attribute_accessors'
module DeadlockRetry
def self.included(base)
base.extend(ClassMethods)
base.class_eval do
class << self
alias_method_chain :transaction, :deadlock_handling
end
end
end
mattr_accessor :innodb_status_cmd
module ClassMethods
DEADLOCK_ERROR_MESSAGES = [
"Deadlock found when trying to get lock",
"Lock wait timeout exceeded",
"deadlock detected"
]
MAXIMUM_RETRIES_ON_DEADLOCK = 3
def transaction_with_deadlock_handling(*objects, &block)
retry_count = 0
check_innodb_status_available
begin
transaction_without_deadlock_handling(*objects, &block)
rescue ActiveRecord::StatementInvalid => error
raise if in_nested_transaction?
if DEADLOCK_ERROR_MESSAGES.any? { |msg| error.message =~ /#{Regexp.escape(msg)}/ }
raise if retry_count >= MAXIMUM_RETRIES_ON_DEADLOCK
retry_count += 1
logger.info "Deadlock detected on retry #{retry_count}, restarting transaction"
log_innodb_status if DeadlockRetry.innodb_status_cmd
exponential_pause(retry_count)
retry
else
raise
end
end
end
private
WAIT_TIMES = [0, 1, 2, 4, 8, 16, 32]
def exponential_pause(count)
sec = WAIT_TIMES[count-1] || 32
# sleep 0, 1, 2, 4, ... seconds up to the MAXIMUM_RETRIES.
# Cap the pause time at 32 seconds.
sleep(sec) if sec != 0
end
def in_nested_transaction?
# open_transactions was added in 2.2's connection pooling changes.
connection.open_transactions != 0
end
def show_innodb_status
self.connection.select_value(DeadlockRetry.innodb_status_cmd)
end
# Should we try to log innodb status -- if we don't have permission to,
# we actually break in-flight transactions, silently (!)
def check_innodb_status_available
return unless DeadlockRetry.innodb_status_cmd == nil
if self.connection.adapter_name == "MySQL"
begin
mysql_version = self.connection.select_rows('show variables like \'version\'')[0][1]
cmd = if mysql_version < '5.5'
'show innodb status'
else
'show engine innodb status'
end
self.connection.select_value(cmd)
DeadlockRetry.innodb_status_cmd = cmd
rescue
logger.info "Cannot log innodb status: #{$!.message}"
DeadlockRetry.innodb_status_cmd = false
end
else
DeadlockRetry.innodb_status_cmd = false
end
end
def log_innodb_status
# show innodb status is the only way to get visiblity into why
# the transaction deadlocked. log it.
lines = show_innodb_status
logger.warn "INNODB Status follows:"
lines.each_line do |line|
logger.warn line
end
rescue => e
# Access denied, ignore
logger.info "Cannot log innodb status: #{e.message}"
end
end
end
ActiveRecord::Base.send(:include, DeadlockRetry) if defined?(ActiveRecord)