debian-mirror-gitlab/lib/gitlab/database/async_indexes/index_creator.rb

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

68 lines
1.6 KiB
Ruby
Raw Normal View History

2021-10-27 15:23:28 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module AsyncIndexes
class IndexCreator
include ExclusiveLeaseGuard
TIMEOUT_PER_ACTION = 1.day
STATEMENT_TIMEOUT = 9.hours
def initialize(async_index)
@async_index = async_index
end
def perform
try_obtain_lease do
if index_exists?
log_index_info('Skipping index creation as the index exists')
else
log_index_info('Creating async index')
set_statement_timeout do
connection.execute(async_index.definition)
end
log_index_info('Finished creating async index')
end
async_index.destroy
end
end
private
attr_reader :async_index
def index_exists?
connection.indexes(async_index.table_name).any? { |index| index.name == async_index.name }
end
def connection
2021-12-11 22:18:48 +05:30
@connection ||= async_index.connection
2021-10-27 15:23:28 +05:30
end
def lease_timeout
TIMEOUT_PER_ACTION
end
2022-01-26 12:08:38 +05:30
def lease_key
[super, async_index.connection_db_config.name].join('/')
end
2021-10-27 15:23:28 +05:30
def set_statement_timeout
connection.execute("SET statement_timeout TO '%ds'" % STATEMENT_TIMEOUT)
yield
ensure
connection.execute('RESET statement_timeout')
end
def log_index_info(message)
Gitlab::AppLogger.info(message: message, table_name: async_index.table_name, index_name: async_index.name)
end
end
end
end
end