debian-mirror-gitlab/lib/gitlab/database/lock_writes_manager.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
4 KiB
Ruby
Raw Normal View History

2022-08-27 11:52:29 +05:30
# frozen_string_literal: true
module Gitlab
module Database
class LockWritesManager
TRIGGER_FUNCTION_NAME = 'gitlab_schema_prevent_write'
2023-01-13 00:05:48 +05:30
# Triggers to block INSERT / UPDATE / DELETE
# Triggers on TRUNCATE are not added to the information_schema.triggers
# See https://www.postgresql.org/message-id/16934.1568989957%40sss.pgh.pa.us
EXPECTED_TRIGGER_RECORD_COUNT = 3
2023-05-27 22:25:52 +05:30
# table_name can include schema name as a prefix. For example: 'gitlab_partitions_static.events_03',
# otherwise, it will default to current used schema, for example 'public'.
2023-03-17 16:20:25 +05:30
def initialize(table_name:, connection:, database_name:, with_retries: true, logger: nil, dry_run: false)
2022-08-27 11:52:29 +05:30
@table_name = table_name
@connection = connection
@database_name = database_name
@logger = logger
2022-10-11 01:57:18 +05:30
@dry_run = dry_run
2023-03-17 16:20:25 +05:30
@with_retries = with_retries
2023-03-04 22:38:38 +05:30
@table_name_without_schema = ActiveRecord::ConnectionAdapters::PostgreSQL::Utils
2023-07-09 08:55:56 +05:30
.extract_schema_qualified_name(table_name.to_s)
2023-03-04 22:38:38 +05:30
.identifier
2022-10-11 01:57:18 +05:30
end
2023-03-17 16:20:25 +05:30
def table_locked_for_writes?
2022-10-11 01:57:18 +05:30
query = <<~SQL
SELECT COUNT(*) from information_schema.triggers
2023-03-04 22:38:38 +05:30
WHERE event_object_table = '#{table_name_without_schema}'
2023-03-17 16:20:25 +05:30
AND trigger_name = '#{write_trigger_name}'
2022-10-11 01:57:18 +05:30
SQL
2023-01-13 00:05:48 +05:30
connection.select_value(query) == EXPECTED_TRIGGER_RECORD_COUNT
2022-08-27 11:52:29 +05:30
end
def lock_writes
2023-03-17 16:20:25 +05:30
if table_locked_for_writes?
2022-10-11 01:57:18 +05:30
logger&.info "Skipping lock_writes, because #{table_name} is already locked for writes"
2023-06-20 00:43:36 +05:30
return result_hash(action: 'skipped')
2022-10-11 01:57:18 +05:30
end
2022-08-27 11:52:29 +05:30
logger&.info "Database: '#{database_name}', Table: '#{table_name}': Lock Writes".color(:yellow)
2022-10-11 01:57:18 +05:30
sql_statement = <<~SQL
2023-03-17 16:20:25 +05:30
CREATE TRIGGER #{write_trigger_name}
2022-08-27 11:52:29 +05:30
BEFORE INSERT OR UPDATE OR DELETE OR TRUNCATE
ON #{table_name}
FOR EACH STATEMENT EXECUTE FUNCTION #{TRIGGER_FUNCTION_NAME}();
SQL
2022-10-11 01:57:18 +05:30
execute_sql_statement(sql_statement)
2023-06-20 00:43:36 +05:30
result_hash(action: 'locked')
2022-08-27 11:52:29 +05:30
end
def unlock_writes
logger&.info "Database: '#{database_name}', Table: '#{table_name}': Allow Writes".color(:green)
2022-10-11 01:57:18 +05:30
sql_statement = <<~SQL
2023-03-17 16:20:25 +05:30
DROP TRIGGER IF EXISTS #{write_trigger_name} ON #{table_name};
2022-08-27 11:52:29 +05:30
SQL
2022-10-11 01:57:18 +05:30
execute_sql_statement(sql_statement)
2023-06-20 00:43:36 +05:30
result_hash(action: 'unlocked')
2022-08-27 11:52:29 +05:30
end
private
2023-03-17 16:20:25 +05:30
attr_reader :table_name, :connection, :database_name, :logger, :dry_run, :table_name_without_schema, :with_retries
2022-10-11 01:57:18 +05:30
def execute_sql_statement(sql)
if dry_run
logger&.info sql
2023-03-17 16:20:25 +05:30
elsif with_retries
raise "Cannot call lock_retries_helper if a transaction is already open" if connection.transaction_open?
run_with_retries(connection) do
2022-10-11 01:57:18 +05:30
connection.execute(sql)
end
2023-03-17 16:20:25 +05:30
else
connection.execute(sql)
2022-10-11 01:57:18 +05:30
end
end
2022-08-27 11:52:29 +05:30
2023-03-17 16:20:25 +05:30
def run_with_retries(connection, &block)
2022-08-27 11:52:29 +05:30
with_statement_timeout_retries do
with_lock_retries(connection) do
yield
end
end
end
def with_statement_timeout_retries(times = 5)
current_iteration = 1
begin
yield
rescue ActiveRecord::QueryCanceled => err # rubocop:disable Database/RescueQueryCanceled
if current_iteration <= times
current_iteration += 1
retry
else
raise err
end
end
end
def with_lock_retries(connection, &block)
Gitlab::Database::WithLockRetries.new(
klass: "gitlab:db:lock_writes",
logger: logger || Gitlab::AppLogger,
2023-03-17 16:20:25 +05:30
connection: connection,
allow_savepoints: false # this causes the WithLockRetries to fail if sub-transaction has been detected.
2022-08-27 11:52:29 +05:30
).run(&block)
end
2023-03-17 16:20:25 +05:30
def write_trigger_name
2023-03-04 22:38:38 +05:30
"gitlab_schema_write_trigger_for_#{table_name_without_schema}"
2022-08-27 11:52:29 +05:30
end
2023-06-20 00:43:36 +05:30
def result_hash(action:)
{ action: action, database: database_name, table: table_name, dry_run: dry_run }
end
2022-08-27 11:52:29 +05:30
end
end
end