Skip to content

Commit 42b980e

Browse files
fix the increase lock timeout on retry
1 parent b0efa6d commit 42b980e

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

lib/safe-pg-migrations/plugins/statement_retrier.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ module StatementRetrier
1313
private
1414

1515
def retry_if_lock_timeout
16-
initial_lock_timeout = SafePgMigrations.config.lock_timeout
16+
@lock_timeout = SafePgMigrations.config.lock_timeout
1717
number_of_retries = 0
1818
begin
1919
number_of_retries += 1
2020
yield
21-
rescue ActiveRecord::LockWaitTimeout
21+
rescue ActiveRecord::LockWaitTimeout => e
2222
# Retrying is useless if we're inside a transaction.
23-
if transaction_open? || number_of_retries >= SafePgMigrations.config.max_tries
24-
SafePgMigrations.config.lock_timeout = initial_lock_timeout
25-
raise
26-
end
23+
raise e if transaction_open? || number_of_retries >= SafePgMigrations.config.max_tries
2724

2825
retry_delay = SafePgMigrations.config.retry_delay
2926
Helpers::Logger.say "Retrying in #{retry_delay} seconds...", sub_item: true
@@ -39,20 +36,23 @@ def retry_if_lock_timeout
3936
end
4037

4138
def increase_lock_timeout
42-
Helpers::Logger.say " Increasing the lock timeout... Currently set to #{SafePgMigrations.config.lock_timeout}",
39+
Helpers::Logger.say " Increasing the lock timeout... Currently set to #{@lock_timeout} seconds",
4340
sub_item: true
44-
SafePgMigrations.config.lock_timeout = (SafePgMigrations.config.lock_timeout + lock_timeout_step)
45-
unless SafePgMigrations.config.lock_timeout < SafePgMigrations.config.max_lock_timeout_for_retry
46-
SafePgMigrations.config.lock_timeout = SafePgMigrations.config.max_lock_timeout_for_retry
41+
@lock_timeout += lock_timeout_step
42+
unless @lock_timeout < SafePgMigrations.config.max_lock_timeout_for_retry
43+
@lock_timeout = SafePgMigrations.config.max_lock_timeout_for_retry
4744
end
48-
Helpers::Logger.say " Lock timeout is now set to #{SafePgMigrations.config.lock_timeout}", sub_item: true
45+
execute("SET lock_timeout TO '#{@lock_timeout}s'")
46+
Helpers::Logger.say " Lock timeout is now set to #{@lock_timeout} seconds", sub_item: true
4947
end
5048

5149
def lock_timeout_step
50+
return @lock_timeout_step if defined?(@lock_timeout_step)
51+
5252
max_lock_timeout_for_retry = SafePgMigrations.config.max_lock_timeout_for_retry
5353
lock_timeout = SafePgMigrations.config.lock_timeout
5454
max_tries = SafePgMigrations.config.max_tries
55-
@lock_timeout_step ||= (max_lock_timeout_for_retry - lock_timeout) / (max_tries - 1)
55+
@lock_timeout_step = (max_lock_timeout_for_retry - lock_timeout) / (max_tries - 1)
5656
end
5757
end
5858
end

test/statement_retrier_test.rb

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@ def test_lock_timeout_increase_on_retry
77
SafePgMigrations.config.lock_timeout = 0.1.seconds
88
SafePgMigrations.config.increase_lock_timeout_on_retry = true
99

10-
calls = calls_for_lock_timeout_migration
10+
2.times do
11+
calls = calls_for_lock_timeout_migration
1112

12-
assert_equal [
13-
' -> Retrying in 60 seconds...',
14-
' -> Increasing the lock timeout... Currently set to 0.1',
15-
' -> Lock timeout is now set to 0.325',
16-
' -> Retrying now.',
17-
' -> Retrying in 60 seconds...',
18-
' -> Increasing the lock timeout... Currently set to 0.325',
19-
' -> Lock timeout is now set to 0.55',
20-
' -> Retrying now.',
21-
' -> Retrying in 60 seconds...',
22-
' -> Increasing the lock timeout... Currently set to 0.55',
23-
' -> Lock timeout is now set to 0.775',
24-
' -> Retrying now.',
25-
' -> Retrying in 60 seconds...',
26-
' -> Increasing the lock timeout... Currently set to 0.775',
27-
' -> Lock timeout is now set to 1',
28-
' -> Retrying now.',
29-
], calls[1..].map(&:first)
13+
assert_equal [
14+
' -> Retrying in 60 seconds...',
15+
' -> Increasing the lock timeout... Currently set to 0.1 seconds',
16+
' -> Lock timeout is now set to 0.325 seconds',
17+
' -> Retrying now.',
18+
' -> Retrying in 60 seconds...',
19+
' -> Increasing the lock timeout... Currently set to 0.325 seconds',
20+
' -> Lock timeout is now set to 0.55 seconds',
21+
' -> Retrying now.',
22+
' -> Retrying in 60 seconds...',
23+
' -> Increasing the lock timeout... Currently set to 0.55 seconds',
24+
' -> Lock timeout is now set to 0.775 seconds',
25+
' -> Retrying now.',
26+
' -> Retrying in 60 seconds...',
27+
' -> Increasing the lock timeout... Currently set to 0.775 seconds',
28+
' -> Lock timeout is now set to 1 seconds',
29+
' -> Retrying now.',
30+
], calls[1..].map(&:first)
31+
end
3032
end
3133

3234
def test_no_lock_timeout_increase_on_retry_if_disabled

0 commit comments

Comments
 (0)