2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module GitGarbageCollectMethods
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
include ApplicationWorker
|
|
|
|
|
|
|
|
sidekiq_options retry: false
|
|
|
|
feature_category :gitaly
|
|
|
|
loggable_arguments 1, 2, 3
|
|
|
|
end
|
|
|
|
|
|
|
|
# Timeout set to 24h
|
|
|
|
LEASE_TIMEOUT = 86400
|
|
|
|
|
|
|
|
def perform(resource_id, task = :gc, lease_key = nil, lease_uuid = nil)
|
|
|
|
resource = find_resource(resource_id)
|
|
|
|
lease_key ||= default_lease_key(task, resource)
|
|
|
|
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
|
|
|
|
|
|
|
|
task = task.to_sym
|
|
|
|
|
|
|
|
before_gitaly_call(task, resource)
|
|
|
|
gitaly_call(task, resource)
|
|
|
|
|
|
|
|
# Refresh the branch cache in case garbage collection caused a ref lookup to fail
|
|
|
|
flush_ref_caches(resource) if gc?(task)
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
update_repository_statistics(resource, task)
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
# In case pack files are deleted, release libgit2 cache and open file
|
|
|
|
# descriptors ASAP instead of waiting for Ruby garbage collection
|
|
|
|
resource.cleanup
|
|
|
|
ensure
|
|
|
|
cancel_lease(lease_key, lease_uuid) if lease_key.present? && lease_uuid.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def default_lease_key(task, resource)
|
|
|
|
"git_gc:#{task}:#{resource.class.name.underscore.pluralize}:#{resource.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_resource(id)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def gc?(task)
|
2023-04-23 21:23:45 +05:30
|
|
|
%i[gc eager prune].include?(task)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def before_gitaly_call(task, resource)
|
|
|
|
# no-op
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitaly_call(task, resource)
|
|
|
|
repository = resource.repository.raw_repository
|
2023-03-17 16:20:25 +05:30
|
|
|
client = repository.gitaly_repository_client
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
if task == :prune
|
|
|
|
client.prune_unreachable_objects
|
2022-05-07 20:08:51 +05:30
|
|
|
else
|
2023-04-23 21:23:45 +05:30
|
|
|
client.optimize_repository(eager: task == :eager)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
rescue GRPC::NotFound => e
|
|
|
|
Gitlab::GitLogger.error("#{__method__} failed:\nRepository not found")
|
2021-06-08 01:23:25 +05:30
|
|
|
raise Gitlab::Git::Repository::NoRepository, e
|
2021-03-11 19:13:27 +05:30
|
|
|
rescue GRPC::BadStatus => e
|
|
|
|
Gitlab::GitLogger.error("#{__method__} failed:\n#{e}")
|
2021-06-08 01:23:25 +05:30
|
|
|
raise Gitlab::Git::CommandError, e
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def flush_ref_caches(resource)
|
|
|
|
resource.repository.expire_branches_cache
|
|
|
|
resource.repository.branch_names
|
|
|
|
resource.repository.has_visible_content?
|
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def update_repository_statistics(resource, task)
|
2021-03-11 19:13:27 +05:30
|
|
|
resource.repository.expire_statistics_caches
|
|
|
|
|
|
|
|
return if Gitlab::Database.read_only? # GitGarbageCollectWorker may be run on a Geo secondary
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
stats_to_update = stats
|
|
|
|
|
|
|
|
stats_to_update.delete(:repository_size) if task == :incremental_repack
|
|
|
|
|
|
|
|
update_db_repository_statistics(resource, stats_to_update)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def update_db_repository_statistics(resource, stats)
|
2021-03-11 19:13:27 +05:30
|
|
|
# no-op
|
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
def stats
|
|
|
|
[]
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|