2021-11-05 17:10:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module BackgroundMigration
|
|
|
|
# Class responsible for executing background migrations based on the given database.
|
|
|
|
#
|
|
|
|
# Chooses the correct worker class when selecting jobs from the queue based on the
|
|
|
|
# convention of how the queues and worker classes are setup for each database.
|
|
|
|
#
|
|
|
|
# Also provides a database connection to the correct tracking database.
|
2021-12-16 22:12:52 -05:00
|
|
|
class JobCoordinator # rubocop:disable Metrics/ClassLength
|
|
|
|
class << self
|
2022-02-03 19:13:53 -05:00
|
|
|
def for_tracking_database(tracking_database)
|
|
|
|
worker_class = worker_for_tracking_database[tracking_database]
|
|
|
|
|
|
|
|
if worker_class.nil?
|
|
|
|
raise ArgumentError, "tracking_database must be one of [#{worker_for_tracking_database.keys.join(', ')}]"
|
|
|
|
end
|
|
|
|
|
|
|
|
new(worker_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-12-16 22:12:52 -05:00
|
|
|
def worker_classes
|
|
|
|
@worker_classes ||= [
|
2022-02-03 19:13:53 -05:00
|
|
|
::BackgroundMigrationWorker,
|
|
|
|
::BackgroundMigration::CiDatabaseWorker
|
2021-12-16 22:12:52 -05:00
|
|
|
].freeze
|
|
|
|
end
|
2021-11-05 17:10:23 -04:00
|
|
|
|
2021-12-16 22:12:52 -05:00
|
|
|
def worker_for_tracking_database
|
|
|
|
@worker_for_tracking_database ||= worker_classes
|
2022-02-03 19:13:53 -05:00
|
|
|
.select { |worker_class| Gitlab::Database.has_config?(worker_class.tracking_database) }
|
2021-12-16 22:12:52 -05:00
|
|
|
.index_by(&:tracking_database)
|
|
|
|
.with_indifferent_access
|
|
|
|
.freeze
|
2021-11-05 17:10:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-16 22:12:52 -05:00
|
|
|
attr_reader :worker_class
|
2021-11-05 17:10:23 -04:00
|
|
|
|
2021-12-23 04:13:45 -05:00
|
|
|
delegate :minimum_interval, :perform_in, to: :worker_class
|
|
|
|
|
2021-11-05 17:10:23 -04:00
|
|
|
def queue
|
|
|
|
@queue ||= worker_class.sidekiq_options['queue']
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_shared_connection(&block)
|
|
|
|
Gitlab::Database::SharedModel.using_connection(connection, &block)
|
|
|
|
end
|
|
|
|
|
2022-03-07 04:13:09 -05:00
|
|
|
def pending_jobs(include_dead_jobs: false)
|
|
|
|
Enumerator.new do |y|
|
2021-11-16 04:13:21 -05:00
|
|
|
queues = [
|
|
|
|
Sidekiq::ScheduledSet.new,
|
|
|
|
Sidekiq::Queue.new(self.queue)
|
|
|
|
]
|
|
|
|
|
2022-03-07 04:13:09 -05:00
|
|
|
if include_dead_jobs
|
2021-11-16 04:13:21 -05:00
|
|
|
queues << Sidekiq::RetrySet.new
|
|
|
|
queues << Sidekiq::DeadSet.new
|
|
|
|
end
|
2021-11-05 17:10:23 -04:00
|
|
|
|
2021-11-16 04:13:21 -05:00
|
|
|
queues.each do |queue|
|
|
|
|
queue.each do |job|
|
2022-03-07 04:13:09 -05:00
|
|
|
y << job if job.klass == worker_class.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def steal(steal_class, retry_dead_jobs: false)
|
|
|
|
with_shared_connection do
|
|
|
|
pending_jobs(include_dead_jobs: retry_dead_jobs).each do |job|
|
|
|
|
migration_class, migration_args = job.args
|
2021-11-05 17:10:23 -04:00
|
|
|
|
2022-03-07 04:13:09 -05:00
|
|
|
next unless migration_class == steal_class
|
|
|
|
next if block_given? && !(yield job)
|
2021-11-05 17:10:23 -04:00
|
|
|
|
2022-03-07 04:13:09 -05:00
|
|
|
begin
|
|
|
|
perform(migration_class, migration_args) if job.delete
|
|
|
|
rescue Exception # rubocop:disable Lint/RescueException
|
|
|
|
worker_class # enqueue this migration again
|
|
|
|
.perform_async(migration_class, migration_args)
|
2021-11-05 17:10:23 -04:00
|
|
|
|
2022-03-07 04:13:09 -05:00
|
|
|
raise
|
2021-11-05 17:10:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(class_name, arguments)
|
2021-11-16 04:13:21 -05:00
|
|
|
with_shared_connection do
|
2022-01-19 22:14:52 -05:00
|
|
|
migration_instance_for(class_name).perform(*arguments)
|
2021-11-16 04:13:21 -05:00
|
|
|
end
|
2021-11-05 17:10:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remaining
|
|
|
|
enqueued = Sidekiq::Queue.new(self.queue)
|
|
|
|
scheduled = Sidekiq::ScheduledSet.new
|
|
|
|
|
|
|
|
[enqueued, scheduled].sum do |set|
|
|
|
|
set.count do |job|
|
|
|
|
job.klass == worker_class.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?(migration_class, additional_queues = [])
|
|
|
|
enqueued = Sidekiq::Queue.new(self.queue)
|
|
|
|
scheduled = Sidekiq::ScheduledSet.new
|
|
|
|
|
|
|
|
enqueued_job?([enqueued, scheduled], migration_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dead_jobs?(migration_class)
|
|
|
|
dead_set = Sidekiq::DeadSet.new
|
|
|
|
|
|
|
|
enqueued_job?([dead_set], migration_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrying_jobs?(migration_class)
|
|
|
|
retry_set = Sidekiq::RetrySet.new
|
|
|
|
|
|
|
|
enqueued_job?([retry_set], migration_class)
|
|
|
|
end
|
|
|
|
|
2022-01-19 22:14:52 -05:00
|
|
|
def migration_instance_for(class_name)
|
|
|
|
migration_class = migration_class_for(class_name)
|
|
|
|
|
|
|
|
if migration_class < Gitlab::BackgroundMigration::BaseJob
|
|
|
|
migration_class.new(connection: connection)
|
|
|
|
else
|
|
|
|
migration_class.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-05 17:10:23 -04:00
|
|
|
def migration_class_for(class_name)
|
|
|
|
Gitlab::BackgroundMigration.const_get(class_name, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueued_job?(queues, migration_class)
|
|
|
|
queues.any? do |queue|
|
|
|
|
queue.any? do |job|
|
|
|
|
job.klass == worker_class.name && job.args.first == migration_class
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-12-16 22:12:52 -05:00
|
|
|
def initialize(worker_class)
|
2021-11-05 17:10:23 -04:00
|
|
|
@worker_class = worker_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def connection
|
2021-11-15 10:10:57 -05:00
|
|
|
@connection ||= Gitlab::Database
|
|
|
|
.database_base_models
|
2022-02-03 19:13:53 -05:00
|
|
|
.fetch(worker_class.tracking_database)
|
2021-11-15 10:10:57 -05:00
|
|
|
.connection
|
2021-11-05 17:10:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|