debian-mirror-gitlab/app/workers/git_garbage_collect_worker.rb

127 lines
3.5 KiB
Ruby
Raw Normal View History

2016-08-24 12:49:21 +05:30
class GitGarbageCollectWorker
2018-03-17 18:26:18 +05:30
include ApplicationWorker
2016-08-24 12:49:21 +05:30
2016-11-03 12:29:30 +05:30
sidekiq_options retry: false
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
# Timeout set to 24h
LEASE_TIMEOUT = 86400
2017-09-10 17:25:29 +05:30
GITALY_MIGRATED_TASKS = {
gc: :garbage_collect,
full_repack: :repack_full,
incremental_repack: :repack_incremental
}.freeze
2017-08-17 22:00:37 +05:30
def perform(project_id, task = :gc, lease_key = nil, lease_uuid = nil)
2016-08-24 12:49:21 +05:30
project = Project.find(project_id)
2018-03-17 18:26:18 +05:30
active_uuid = get_lease_uuid(lease_key)
if active_uuid
return unless active_uuid == lease_uuid
renew_lease(lease_key, active_uuid)
else
lease_uuid = try_obtain_lease(lease_key)
return unless lease_uuid
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
task = task.to_sym
2017-08-17 22:00:37 +05:30
cmd = command(task)
2017-09-10 17:25:29 +05:30
gitaly_migrate(GITALY_MIGRATED_TASKS[task]) do |is_enabled|
if is_enabled
gitaly_call(task, project.repository.raw_repository)
else
2018-05-09 12:01:36 +05:30
repo_path = project.repository.path_to_repo
description = "'#{cmd.join(' ')}' in #{repo_path}"
Gitlab::GitLogger.info(description)
2017-09-10 17:25:29 +05:30
output, status = Gitlab::Popen.popen(cmd, repo_path)
2018-05-09 12:01:36 +05:30
2017-09-10 17:25:29 +05:30
Gitlab::GitLogger.error("#{description} failed:\n#{output}") unless status.zero?
end
end
2016-08-24 12:49:21 +05:30
# Refresh the branch cache in case garbage collection caused a ref lookup to fail
2017-08-17 22:00:37 +05:30
flush_ref_caches(project) if task == :gc
2018-03-27 19:54:05 +05:30
# In case pack files are deleted, release libgit2 cache and open file
# descriptors ASAP instead of waiting for Ruby garbage collection
project.cleanup
2017-08-17 22:00:37 +05:30
ensure
2018-03-17 18:26:18 +05:30
cancel_lease(lease_key, lease_uuid) if lease_key.present? && lease_uuid.present?
2017-08-17 22:00:37 +05:30
end
private
2018-03-17 18:26:18 +05:30
def try_obtain_lease(key)
::Gitlab::ExclusiveLease.new(key, timeout: LEASE_TIMEOUT).try_obtain
end
def renew_lease(key, uuid)
::Gitlab::ExclusiveLease.new(key, uuid: uuid, timeout: LEASE_TIMEOUT).renew
end
def cancel_lease(key, uuid)
::Gitlab::ExclusiveLease.cancel(key, uuid)
end
def get_lease_uuid(key)
::Gitlab::ExclusiveLease.get_uuid(key)
end
2017-09-10 17:25:29 +05:30
## `repository` has to be a Gitlab::Git::Repository
def gitaly_call(task, repository)
client = Gitlab::GitalyClient::RepositoryService.new(repository)
case task
when :gc
client.garbage_collect(bitmaps_enabled?)
when :full_repack
client.repack_full(bitmaps_enabled?)
when :incremental_repack
client.repack_incremental
end
end
2017-08-17 22:00:37 +05:30
def command(task)
case task
when :gc
git(write_bitmaps: bitmaps_enabled?) + %w[gc]
when :full_repack
git(write_bitmaps: bitmaps_enabled?) + %w[repack -A -d --pack-kept-objects]
when :incremental_repack
# Normal git repack fails when bitmaps are enabled. It is impossible to
# create a bitmap here anyway.
git(write_bitmaps: false) + %w[repack -d]
else
raise "Invalid gc task: #{task.inspect}"
end
end
def flush_ref_caches(project)
2016-08-24 12:49:21 +05:30
project.repository.after_create_branch
project.repository.branch_names
project.repository.has_visible_content?
end
2017-08-17 22:00:37 +05:30
def bitmaps_enabled?
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.housekeeping_bitmaps_enabled
2017-08-17 22:00:37 +05:30
end
def git(write_bitmaps:)
config_value = write_bitmaps ? 'true' : 'false'
%W[git -c repack.writeBitmaps=#{config_value}]
end
2017-09-10 17:25:29 +05:30
def gitaly_migrate(method, &block)
Gitlab::GitalyClient.migrate(method, &block)
rescue GRPC::NotFound => e
Gitlab::GitLogger.error("#{method} failed:\nRepository not found")
raise Gitlab::Git::Repository::NoRepository.new(e)
rescue GRPC::BadStatus => e
Gitlab::GitLogger.error("#{method} failed:\n#{e}")
raise Gitlab::Git::CommandError.new(e)
end
2016-08-24 12:49:21 +05:30
end