2021-01-03 14:25:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module Reindexing
|
|
|
|
class Coordinator
|
2023-04-23 21:23:45 +05:30
|
|
|
include AsyncDdlExclusiveLeaseGuard
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
attr_reader :index, :notifier
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def initialize(index, notifier = GrafanaNotifier.new)
|
|
|
|
@index = index
|
|
|
|
@notifier = notifier
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2023-03-17 16:20:25 +05:30
|
|
|
return if too_late_for_reindexing?
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
# 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)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
def drop
|
2023-03-17 16:20:25 +05:30
|
|
|
return if too_late_for_reindexing?
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
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(
|
2023-03-17 16:20:25 +05:30
|
|
|
connection: connection,
|
2022-03-02 08:16:31 +05:30
|
|
|
timing_configuration: REMOVE_INDEX_RETRY_CONFIG,
|
|
|
|
klass: self.class,
|
|
|
|
logger: Gitlab::AppLogger
|
|
|
|
)
|
|
|
|
|
|
|
|
retries.run(raise_on_exhaustion: false) do
|
2023-03-17 16:20:25 +05:30
|
|
|
connection.execute("DROP INDEX CONCURRENTLY IF EXISTS #{full_index_name}")
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
private
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
delegate :connection, :connection_db_config, to: :index
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def with_notifications(action)
|
|
|
|
notifier.notify_start(action)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
notifier.notify_end(action)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_for(index, action)
|
2021-09-30 23:02:18 +05:30
|
|
|
ReindexConcurrently.new(index).perform
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError
|
2021-03-08 18:12:59 +05:30
|
|
|
action.state = :failed
|
|
|
|
|
|
|
|
raise
|
|
|
|
ensure
|
|
|
|
action.finish
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def lease_timeout
|
|
|
|
TIMEOUT_PER_ACTION
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def full_index_name
|
|
|
|
[
|
|
|
|
connection.quote_table_name(index.schema),
|
|
|
|
connection.quote_table_name(index.name)
|
|
|
|
].join('.')
|
|
|
|
end
|
|
|
|
|
|
|
|
# We need to check the time explicitly because we execute 4 reindexing
|
|
|
|
# action per rake invocation and one action can take up to 24 hours.
|
|
|
|
# This means that it can span for more than the weekend.
|
|
|
|
def too_late_for_reindexing?
|
|
|
|
!Time.current.on_weekend?
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|