Skip to content

Commit

Permalink
s/targeted_table/target_table
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Krage <[email protected]>
  • Loading branch information
rkrage and Ryan Krage committed Sep 27, 2023
1 parent 26cad76 commit 88f08c3
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/pg_ha_migrations/safe_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,25 +520,25 @@ def exec_migration(conn, direction)
end

def safely_acquire_lock_for_table(table, mode: :access_exclusive, &block)
nested_targeted_table = Thread.current[__method__]
nested_target_table = Thread.current[__method__]

_check_postgres_adapter!

targeted_table = PgHaMigrations::Table.from_table_name(table, mode)
target_table = PgHaMigrations::Table.from_table_name(table, mode)

if nested_targeted_table
if nested_targeted_table != targeted_table
raise PgHaMigrations::InvalidMigrationError, "Nested lock detected! Cannot acquire lock on #{targeted_table.fully_qualified_name} while #{nested_targeted_table.fully_qualified_name} is locked."
elsif nested_targeted_table.mode < targeted_table.mode
raise PgHaMigrations::InvalidMigrationError, "Lock escalation detected! Cannot change lock level from #{nested_targeted_table.mode.inspect} to #{targeted_table.mode.inspect} for #{targeted_table.fully_qualified_name}."
if nested_target_table
if nested_target_table != target_table
raise PgHaMigrations::InvalidMigrationError, "Nested lock detected! Cannot acquire lock on #{target_table.fully_qualified_name} while #{nested_target_table.fully_qualified_name} is locked."
elsif nested_target_table.mode < target_table.mode
raise PgHaMigrations::InvalidMigrationError, "Lock escalation detected! Cannot change lock level from #{nested_target_table.mode.inspect} to #{target_table.mode.inspect} for #{target_table.fully_qualified_name}."
end
else
Thread.current[__method__] = targeted_table
Thread.current[__method__] = target_table
end

# Locking a partitioned table will also lock child tables (including sub-partitions),
# so we need to check for blocking queries on those tables as well
targeted_tables = targeted_table.partitions(include_sub_partitions: true, include_self: true)
target_tables = target_table.partitions(include_sub_partitions: true, include_self: true)

successfully_acquired_lock = false

Expand All @@ -547,7 +547,7 @@ def safely_acquire_lock_for_table(table, mode: :access_exclusive, &block)
blocking_transactions = PgHaMigrations::BlockingDatabaseTransactions.find_blocking_transactions("#{PgHaMigrations::LOCK_TIMEOUT_SECONDS} seconds")
blocking_transactions.any? do |query|
query.tables_with_locks.any? do |locked_table|
targeted_tables.any? do |target_table|
target_tables.any? do |target_table|
target_table.conflicts_with?(locked_table)
end
end
Expand All @@ -564,13 +564,13 @@ def safely_acquire_lock_for_table(table, mode: :access_exclusive, &block)
adjust_timeout_method = connection.postgresql_version >= 9_03_00 ? :adjust_lock_timeout : :adjust_statement_timeout
begin
method(adjust_timeout_method).call(PgHaMigrations::LOCK_TIMEOUT_SECONDS) do
connection.execute("LOCK #{targeted_table.fully_qualified_name} IN #{targeted_table.mode.to_sql} MODE;")
connection.execute("LOCK #{target_table.fully_qualified_name} IN #{target_table.mode.to_sql} MODE;")
end
successfully_acquired_lock = true
rescue ActiveRecord::StatementInvalid => e
if e.message =~ /PG::LockNotAvailable.+ lock timeout/ || e.message =~ /PG::QueryCanceled.+ statement timeout/
sleep_seconds = PgHaMigrations::LOCK_FAILURE_RETRY_DELAY_MULTLIPLIER * PgHaMigrations::LOCK_TIMEOUT_SECONDS
say "Timed out trying to acquire #{targeted_table.mode.to_sql} lock on the #{targeted_table.fully_qualified_name} table."
say "Timed out trying to acquire #{target_table.mode.to_sql} lock on the #{target_table.fully_qualified_name} table."
say "Sleeping for #{sleep_seconds}s to allow potentially queued up queries to finish before continuing."
sleep(sleep_seconds)

Expand All @@ -586,7 +586,7 @@ def safely_acquire_lock_for_table(table, mode: :access_exclusive, &block)
end
end
ensure
Thread.current[__method__] = nil unless nested_targeted_table
Thread.current[__method__] = nil unless nested_target_table
end

def adjust_lock_timeout(timeout_seconds = PgHaMigrations::LOCK_TIMEOUT_SECONDS, &block)
Expand Down

0 comments on commit 88f08c3

Please sign in to comment.