2020-10-13 14:08:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module Reindexing
|
|
|
|
class Coordinator
|
|
|
|
include ExclusiveLeaseGuard
|
|
|
|
|
|
|
|
# Maximum lease time for the global Redis lease
|
|
|
|
# This should be higher than the maximum time for any
|
|
|
|
# long running step in the reindexing process (compare with
|
|
|
|
# statement timeouts).
|
|
|
|
TIMEOUT_PER_ACTION = 1.day
|
|
|
|
|
2020-12-31 04:10:34 -05:00
|
|
|
attr_reader :index, :notifier
|
2020-10-13 14:08:58 -04:00
|
|
|
|
2020-12-31 04:10:34 -05:00
|
|
|
def initialize(index, notifier = GrafanaNotifier.new)
|
|
|
|
@index = index
|
|
|
|
@notifier = notifier
|
2020-10-13 14:08:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2020-12-31 04:10:34 -05:00
|
|
|
# This obtains a global lease such that there's
|
|
|
|
# only one live reindexing process at a time.
|
|
|
|
try_obtain_lease do
|
|
|
|
action = ReindexAction.create_for(index)
|
|
|
|
|
|
|
|
with_notifications(action) do
|
|
|
|
perform_for(index, action)
|
2020-10-13 14:08:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-21 22:15:20 -05:00
|
|
|
def drop
|
|
|
|
try_obtain_lease do
|
|
|
|
Gitlab::AppLogger.info("Removing index #{index.identifier} which is a leftover, temporary index from previous reindexing activity")
|
|
|
|
|
|
|
|
retries = Gitlab::Database::WithLockRetriesOutsideTransaction.new(
|
|
|
|
connection: index.connection,
|
|
|
|
timing_configuration: REMOVE_INDEX_RETRY_CONFIG,
|
|
|
|
klass: self.class,
|
|
|
|
logger: Gitlab::AppLogger
|
|
|
|
)
|
|
|
|
|
|
|
|
retries.run(raise_on_exhaustion: false) do
|
|
|
|
index.connection.tap do |conn|
|
|
|
|
conn.execute("DROP INDEX CONCURRENTLY IF EXISTS #{conn.quote_table_name(index.schema)}.#{conn.quote_table_name(index.name)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-13 14:08:58 -04:00
|
|
|
private
|
|
|
|
|
2020-12-31 04:10:34 -05:00
|
|
|
def with_notifications(action)
|
|
|
|
notifier.notify_start(action)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
notifier.notify_end(action)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_for(index, action)
|
2021-07-19 08:10:08 -04:00
|
|
|
ReindexConcurrently.new(index).perform
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2020-12-31 04:10:34 -05:00
|
|
|
action.state = :failed
|
|
|
|
|
|
|
|
raise
|
|
|
|
ensure
|
|
|
|
action.finish
|
|
|
|
end
|
|
|
|
|
2020-10-13 14:08:58 -04:00
|
|
|
def lease_timeout
|
|
|
|
TIMEOUT_PER_ACTION
|
|
|
|
end
|
2021-12-13 22:13:32 -05:00
|
|
|
|
|
|
|
def lease_key
|
|
|
|
[super, index.connection_db_config.name].join('/')
|
|
|
|
end
|
2020-10-13 14:08:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|