2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-07 11:40:44 -04:00
|
|
|
module Gitlab
|
|
|
|
module HashedStorage
|
|
|
|
# Hashed Storage Migrator
|
|
|
|
#
|
|
|
|
# This is responsible for scheduling and flagging projects
|
|
|
|
# to be migrated from Legacy to Hashed storage, either one by one or in bulk.
|
|
|
|
class Migrator
|
|
|
|
BATCH_SIZE = 100
|
|
|
|
|
|
|
|
# Schedule a range of projects to be bulk migrated with #bulk_migrate asynchronously
|
|
|
|
#
|
2018-12-22 08:54:33 -05:00
|
|
|
# @param [Integer] start first project id for the range
|
|
|
|
# @param [Integer] finish last project id for the range
|
2019-01-22 21:40:05 -05:00
|
|
|
def bulk_schedule_migration(start:, finish:)
|
2019-01-16 20:53:50 -05:00
|
|
|
::HashedStorage::MigratorWorker.perform_async(start, finish)
|
2018-06-07 11:40:44 -04:00
|
|
|
end
|
|
|
|
|
2019-01-22 21:40:05 -05:00
|
|
|
# Schedule a range of projects to be bulk rolledback with #bulk_rollback asynchronously
|
|
|
|
#
|
|
|
|
# @param [Integer] start first project id for the range
|
|
|
|
# @param [Integer] finish last project id for the range
|
|
|
|
def bulk_schedule_rollback(start:, finish:)
|
|
|
|
::HashedStorage::RollbackerWorker.perform_async(start, finish)
|
|
|
|
end
|
|
|
|
|
2018-06-07 11:40:44 -04:00
|
|
|
# Start migration of projects from specified range
|
|
|
|
#
|
2019-01-16 20:53:50 -05:00
|
|
|
# Flagging a project to be migrated is a synchronous action
|
2018-06-07 11:40:44 -04:00
|
|
|
# but the migration runs through async jobs
|
|
|
|
#
|
2018-12-22 08:54:33 -05:00
|
|
|
# @param [Integer] start first project id for the range
|
|
|
|
# @param [Integer] finish last project id for the range
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-01-16 20:53:50 -05:00
|
|
|
def bulk_migrate(start:, finish:)
|
2018-06-07 11:40:44 -04:00
|
|
|
projects = build_relation(start, finish)
|
|
|
|
|
|
|
|
projects.with_route.find_each(batch_size: BATCH_SIZE) do |project|
|
2019-01-16 20:53:50 -05:00
|
|
|
migrate(project)
|
2018-06-07 11:40:44 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-06-07 11:40:44 -04:00
|
|
|
|
2019-01-22 21:40:05 -05:00
|
|
|
# Start rollback of projects from specified range
|
|
|
|
#
|
|
|
|
# Flagging a project to be rolled back is a synchronous action
|
|
|
|
# but the rollback runs through async jobs
|
|
|
|
#
|
|
|
|
# @param [Integer] start first project id for the range
|
|
|
|
# @param [Integer] finish last project id for the range
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def bulk_rollback(start:, finish:)
|
|
|
|
projects = build_relation(start, finish)
|
|
|
|
|
|
|
|
projects.with_route.find_each(batch_size: BATCH_SIZE) do |project|
|
|
|
|
rollback(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2019-01-16 20:53:50 -05:00
|
|
|
# Flag a project to be migrated to Hashed Storage
|
2018-06-07 11:40:44 -04:00
|
|
|
#
|
2018-12-22 08:54:33 -05:00
|
|
|
# @param [Project] project that will be migrated
|
2018-06-07 11:40:44 -04:00
|
|
|
def migrate(project)
|
2020-09-09 20:08:32 -04:00
|
|
|
Gitlab::AppLogger.info "Starting storage migration of #{project.full_path} (ID=#{project.id})..."
|
2018-06-07 11:40:44 -04:00
|
|
|
|
|
|
|
project.migrate_to_hashed_storage!
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => err
|
2020-09-09 20:08:32 -04:00
|
|
|
Gitlab::AppLogger.error("#{err.message} migrating storage of #{project.full_path} (ID=#{project.id}), trace - #{err.backtrace}")
|
2018-06-07 11:40:44 -04:00
|
|
|
end
|
|
|
|
|
2019-01-16 20:57:35 -05:00
|
|
|
# Flag a project to be rolled-back to Legacy Storage
|
|
|
|
#
|
|
|
|
# @param [Project] project that will be rolled-back
|
2018-12-22 08:54:33 -05:00
|
|
|
def rollback(project)
|
2020-09-09 20:08:32 -04:00
|
|
|
Gitlab::AppLogger.info "Starting storage rollback of #{project.full_path} (ID=#{project.id})..."
|
2019-01-16 20:57:35 -05:00
|
|
|
|
|
|
|
project.rollback_to_legacy_storage!
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => err
|
2020-09-09 20:08:32 -04:00
|
|
|
Gitlab::AppLogger.error("#{err.message} rolling-back storage of #{project.full_path} (ID=#{project.id}), trace - #{err.backtrace}")
|
2018-12-22 08:54:33 -05:00
|
|
|
end
|
|
|
|
|
2019-03-11 13:15:44 -04:00
|
|
|
# Returns whether we have any pending storage migration
|
|
|
|
#
|
|
|
|
def migration_pending?
|
|
|
|
any_non_empty_queue?(::HashedStorage::MigratorWorker, ::HashedStorage::ProjectMigrateWorker)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns whether we have any pending storage rollback
|
|
|
|
#
|
|
|
|
def rollback_pending?
|
|
|
|
any_non_empty_queue?(::HashedStorage::RollbackerWorker, ::HashedStorage::ProjectRollbackWorker)
|
|
|
|
end
|
|
|
|
|
2020-09-17 05:09:32 -04:00
|
|
|
# Remove all remaining scheduled rollback operations
|
|
|
|
#
|
|
|
|
def abort_rollback!
|
|
|
|
[::HashedStorage::RollbackerWorker, ::HashedStorage::ProjectRollbackWorker].each do |worker|
|
|
|
|
Sidekiq::Queue.new(worker.queue).clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-07 11:40:44 -04:00
|
|
|
private
|
|
|
|
|
2019-03-11 13:15:44 -04:00
|
|
|
def any_non_empty_queue?(*workers)
|
|
|
|
workers.any? do |worker|
|
2020-08-05 11:09:59 -04:00
|
|
|
Sidekiq::Queue.new(worker.queue).size != 0 # rubocop:disable Style/ZeroLengthPredicate
|
2019-03-11 13:15:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-06-07 11:40:44 -04:00
|
|
|
def build_relation(start, finish)
|
|
|
|
relation = Project
|
|
|
|
table = Project.arel_table
|
|
|
|
|
|
|
|
relation = relation.where(table[:id].gteq(start)) if start
|
|
|
|
relation = relation.where(table[:id].lteq(finish)) if finish
|
|
|
|
|
|
|
|
relation
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-06-07 11:40:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|