debian-mirror-gitlab/lib/gitlab/hashed_storage/migrator.rb

68 lines
2.1 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
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
#
2019-03-02 22:35:43 +05:30
# @param [Integer] start first project id for the range
# @param [Integer] finish last project id for the range
2019-05-30 16:15:17 +05:30
def bulk_schedule(start:, finish:)
2019-03-02 22:35:43 +05:30
::HashedStorage::MigratorWorker.perform_async(start, finish)
2018-11-08 19:23:39 +05:30
end
# Start migration of projects from specified range
#
2019-03-02 22:35:43 +05:30
# Flagging a project to be migrated is a synchronous action
2018-11-08 19:23:39 +05:30
# but the migration runs through async jobs
#
2019-03-02 22:35:43 +05:30
# @param [Integer] start first project id for the range
# @param [Integer] finish last project id for the range
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2019-03-02 22:35:43 +05:30
def bulk_migrate(start:, finish:)
2018-11-08 19:23:39 +05:30
projects = build_relation(start, finish)
projects.with_route.find_each(batch_size: BATCH_SIZE) do |project|
migrate(project)
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-08 19:23:39 +05:30
2019-03-02 22:35:43 +05:30
# Flag a project to be migrated to Hashed Storage
2018-11-08 19:23:39 +05:30
#
2019-03-02 22:35:43 +05:30
# @param [Project] project that will be migrated
2018-11-08 19:23:39 +05:30
def migrate(project)
Rails.logger.info "Starting storage migration of #{project.full_path} (ID=#{project.id})..."
project.migrate_to_hashed_storage!
rescue => err
Rails.logger.error("#{err.message} migrating storage of #{project.full_path} (ID=#{project.id}), trace - #{err.backtrace}")
end
2019-03-02 22:35:43 +05:30
def rollback(project)
2019-05-30 16:15:17 +05:30
# TODO: implement rollback strategy
2019-03-02 22:35:43 +05:30
end
2018-11-08 19:23:39 +05:30
private
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-08 19:23:39 +05:30
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-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-08 19:23:39 +05:30
end
end
end