2018-06-27 07:23:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-02-19 18:09:10 +00:00
|
|
|
class GitGarbageCollectWorker # rubocop:disable Scalability/IdempotentWorker
|
2017-11-28 16:08:30 +00:00
|
|
|
include ApplicationWorker
|
2016-07-08 19:32:58 +00:00
|
|
|
|
2016-10-21 16:13:41 +00:00
|
|
|
sidekiq_options retry: false
|
2019-10-18 11:11:44 +00:00
|
|
|
feature_category :gitaly
|
2020-06-12 12:08:56 +00:00
|
|
|
loggable_arguments 1, 2, 3
|
2016-07-08 19:32:58 +00:00
|
|
|
|
2017-09-04 17:55:04 +00:00
|
|
|
# Timeout set to 24h
|
|
|
|
LEASE_TIMEOUT = 86400
|
|
|
|
|
2016-10-27 12:59:52 +00:00
|
|
|
def perform(project_id, task = :gc, lease_key = nil, lease_uuid = nil)
|
2016-07-08 19:32:58 +00:00
|
|
|
project = Project.find(project_id)
|
2017-09-04 17:55:04 +00:00
|
|
|
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)
|
2016-10-27 12:59:52 +00:00
|
|
|
|
2017-09-04 17:55:04 +00:00
|
|
|
return unless lease_uuid
|
|
|
|
end
|
|
|
|
|
|
|
|
task = task.to_sym
|
2019-04-23 17:32:06 +00:00
|
|
|
|
2019-08-22 03:39:03 +00:00
|
|
|
::Projects::GitDeduplicationService.new(project).execute if task == :gc
|
2019-04-23 17:32:06 +00:00
|
|
|
|
2018-06-13 14:36:43 +00:00
|
|
|
gitaly_call(task, project.repository.raw_repository)
|
2016-07-08 19:32:58 +00:00
|
|
|
|
2016-07-13 13:41:06 +00:00
|
|
|
# Refresh the branch cache in case garbage collection caused a ref lookup to fail
|
2016-10-27 12:59:52 +00:00
|
|
|
flush_ref_caches(project) if task == :gc
|
2018-03-04 06:18:32 +00:00
|
|
|
|
2019-05-02 21:41:05 +00:00
|
|
|
project.repository.expire_statistics_caches if task != :pack_refs
|
Allow public forks to be deduplicated
When a project is forked, the new repository used to be a deep copy of everything
stored on disk by leveraging `git clone`. This works well, and makes isolation
between repository easy. However, the clone is at the start 100% the same as the
origin repository. And in the case of the objects in the object directory, this
is almost always going to be a lot of duplication.
Object Pools are a way to create a third repository that essentially only exists
for its 'objects' subdirectory. This third repository's object directory will be
set as alternate location for objects. This means that in the case an object is
missing in the local repository, git will look in another location. This other
location is the object pool repository.
When Git performs garbage collection, it's smart enough to check the
alternate location. When objects are duplicated, it will allow git to
throw one copy away. This copy is on the local repository, where to pool
remains as is.
These pools have an origin location, which for now will always be a
repository that itself is not a fork. When the root of a fork network is
forked by a user, the fork still clones the full repository. Async, the
pool repository will be created.
Either one of these processes can be done earlier than the other. To
handle this race condition, the Join ObjectPool operation is
idempotent. Given its idempotent, we can schedule it twice, with the
same effect.
To accommodate the holding of state two migrations have been added.
1. Added a state column to the pool_repositories column. This column is
managed by the state machine, allowing for hooks on transitions.
2. pool_repositories now has a source_project_id. This column in
convenient to have for multiple reasons: it has a unique index allowing
the database to handle race conditions when creating a new record. Also,
it's nice to know who the host is. As that's a short link to the fork
networks root.
Object pools are only available for public project, which use hashed
storage and when forking from the root of the fork network. (That is,
the project being forked from itself isn't a fork)
In this commit message I use both ObjectPool and Pool repositories,
which are alike, but different from each other. ObjectPool refers to
whatever is on the disk stored and managed by Gitaly. PoolRepository is
the record in the database.
2018-12-03 13:49:58 +00:00
|
|
|
|
2018-03-04 06:18:32 +00:00
|
|
|
# In case pack files are deleted, release libgit2 cache and open file
|
|
|
|
# descriptors ASAP instead of waiting for Ruby garbage collection
|
|
|
|
project.cleanup
|
2016-10-27 12:59:52 +00:00
|
|
|
ensure
|
2017-09-04 17:55:04 +00:00
|
|
|
cancel_lease(lease_key, lease_uuid) if lease_key.present? && lease_uuid.present?
|
2016-10-27 12:59:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-09-04 17:55:04 +00:00
|
|
|
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-07-19 15:34:14 +00:00
|
|
|
## `repository` has to be a Gitlab::Git::Repository
|
|
|
|
def gitaly_call(task, repository)
|
2019-05-02 21:41:05 +00:00
|
|
|
client = if task == :pack_refs
|
|
|
|
Gitlab::GitalyClient::RefService.new(repository)
|
|
|
|
else
|
|
|
|
Gitlab::GitalyClient::RepositoryService.new(repository)
|
|
|
|
end
|
|
|
|
|
2017-07-19 15:34:14 +00:00
|
|
|
case task
|
|
|
|
when :gc
|
|
|
|
client.garbage_collect(bitmaps_enabled?)
|
|
|
|
when :full_repack
|
|
|
|
client.repack_full(bitmaps_enabled?)
|
|
|
|
when :incremental_repack
|
|
|
|
client.repack_incremental
|
2019-05-02 21:41:05 +00:00
|
|
|
when :pack_refs
|
|
|
|
client.pack_refs
|
2017-07-19 15:34:14 +00:00
|
|
|
end
|
2018-06-13 14:36:43 +00:00
|
|
|
rescue GRPC::NotFound => e
|
2018-07-10 22:08:54 +00:00
|
|
|
Gitlab::GitLogger.error("#{__method__} failed:\nRepository not found")
|
2018-06-13 14:36:43 +00:00
|
|
|
raise Gitlab::Git::Repository::NoRepository.new(e)
|
|
|
|
rescue GRPC::BadStatus => e
|
2018-07-10 22:08:54 +00:00
|
|
|
Gitlab::GitLogger.error("#{__method__} failed:\n#{e}")
|
2018-06-13 14:36:43 +00:00
|
|
|
raise Gitlab::Git::CommandError.new(e)
|
2016-10-27 12:59:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def flush_ref_caches(project)
|
2016-07-08 19:32:58 +00:00
|
|
|
project.repository.after_create_branch
|
2016-07-13 13:41:06 +00:00
|
|
|
project.repository.branch_names
|
|
|
|
project.repository.has_visible_content?
|
2016-07-08 19:32:58 +00:00
|
|
|
end
|
2016-10-27 12:59:52 +00:00
|
|
|
|
|
|
|
def bitmaps_enabled?
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.housekeeping_bitmaps_enabled
|
2016-10-27 12:59:52 +00:00
|
|
|
end
|
2016-07-08 19:32:58 +00:00
|
|
|
end
|