diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-18 11:17:02 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-18 11:17:02 +0300 |
commit | b39512ed755239198a9c294b6a45e65c05900235 (patch) | |
tree | d234a3efade1de67c46b9e5a38ce813627726aa7 /lib/gitlab/database/async_indexes/index_destructor.rb | |
parent | d31474cf3b17ece37939d20082b07f6657cc79a9 (diff) |
Add latest changes from gitlab-org/gitlab@15-3-stable-eev15.3.0-rc42
Diffstat (limited to 'lib/gitlab/database/async_indexes/index_destructor.rb')
-rw-r--r-- | lib/gitlab/database/async_indexes/index_destructor.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/gitlab/database/async_indexes/index_destructor.rb b/lib/gitlab/database/async_indexes/index_destructor.rb new file mode 100644 index 00000000000..fe05872b87a --- /dev/null +++ b/lib/gitlab/database/async_indexes/index_destructor.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +module Gitlab + module Database + module AsyncIndexes + class IndexDestructor + include ExclusiveLeaseGuard + + TIMEOUT_PER_ACTION = 1.day + + def initialize(async_index) + @async_index = async_index + end + + def perform + try_obtain_lease do + if !index_exists? + log_index_info('Skipping dropping as the index does not exist') + else + log_index_info('Dropping async index') + + retries = Gitlab::Database::WithLockRetriesOutsideTransaction.new( + connection: connection, + timing_configuration: Gitlab::Database::Reindexing::REMOVE_INDEX_RETRY_CONFIG, + klass: self.class, + logger: Gitlab::AppLogger + ) + + retries.run(raise_on_exhaustion: false) do + connection.execute(async_index.definition) + end + + log_index_info('Finished dropping 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 + @connection ||= async_index.connection + end + + def lease_timeout + TIMEOUT_PER_ACTION + end + + def lease_key + [super, async_index.connection_db_config.name].join('/') + 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 |