Commit d4892973 authored by Igor Drozdov's avatar Igor Drozdov

Use OptimizeRepository and PruneUnreachableObjects for housekeeping

These RPCs have been introduced instead of deprecated RPCs:

- GarbageCollect
- RepackIncremental
- RepackFull
- PackRefs

The functionality is put behind a feature flag: optimized_housekeeping
parent 5cb9071e
...@@ -83,17 +83,27 @@ module GitGarbageCollectMethods ...@@ -83,17 +83,27 @@ module GitGarbageCollectMethods
def gitaly_call(task, resource) def gitaly_call(task, resource)
repository = resource.repository.raw_repository repository = resource.repository.raw_repository
client = get_gitaly_client(task, repository) if Feature.enabled?(:optimized_housekeeping, container(resource), default_enabled: :yaml)
client = repository.gitaly_repository_client
case task
when :prune, :gc if task == :prune
client.garbage_collect(bitmaps_enabled?, prune: task == :prune) client.prune_unreachable_objects
when :full_repack else
client.repack_full(bitmaps_enabled?) client.optimize_repository
when :incremental_repack end
client.repack_incremental else
when :pack_refs client = get_gitaly_client(task, repository)
client.pack_refs
case task
when :prune, :gc
client.garbage_collect(bitmaps_enabled?, prune: task == :prune)
when :full_repack
client.repack_full(bitmaps_enabled?)
when :incremental_repack
client.repack_incremental
when :pack_refs
client.pack_refs
end
end end
rescue GRPC::NotFound => e rescue GRPC::NotFound => e
Gitlab::GitLogger.error("#{__method__} failed:\nRepository not found") Gitlab::GitLogger.error("#{__method__} failed:\nRepository not found")
......
...@@ -7,6 +7,12 @@ module Projects ...@@ -7,6 +7,12 @@ module Projects
private private
# Used for getting a project/group out of the resource in order to scope a feature flag
# Can be removed within https://gitlab.com/gitlab-org/gitlab/-/issues/353607
def container(resource)
resource
end
override :find_resource override :find_resource
def find_resource(id) def find_resource(id)
Project.find(id) Project.find(id)
......
...@@ -7,6 +7,12 @@ module Wikis ...@@ -7,6 +7,12 @@ module Wikis
private private
# Used for getting a project/group out of the resource in order to scope a feature flag
# Can be removed within https://gitlab.com/gitlab-org/gitlab/-/issues/353607
def container(resource)
resource.container
end
override :find_resource override :find_resource
def find_resource(id) def find_resource(id)
Project.find(id).wiki Project.find(id).wiki
......
---
name: optimized_housekeeping
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/81465
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/353607
milestone: '14.9'
type: development
group: group::source code
default_enabled: false
...@@ -7,6 +7,12 @@ module GroupWikis ...@@ -7,6 +7,12 @@ module GroupWikis
private private
# Used for getting a project/group out of the resource in order to scope a feature flag
# Can be removed within https://gitlab.com/gitlab-org/gitlab/-/issues/353607
def container(resource)
resource.container
end
override :find_resource override :find_resource
def find_resource(id) def find_resource(id)
Group.find(id).wiki Group.find(id).wiki
......
...@@ -21,6 +21,16 @@ module Gitlab ...@@ -21,6 +21,16 @@ module Gitlab
response.exists response.exists
end end
def optimize_repository
request = Gitaly::OptimizeRepositoryRequest.new(repository: @gitaly_repo)
GitalyClient.call(@storage, :repository_service, :optimize_repository, request, timeout: GitalyClient.long_timeout)
end
def prune_unreachable_objects
request = Gitaly::PruneUnreachableObjectsRequest.new(repository: @gitaly_repo)
GitalyClient.call(@storage, :repository_service, :prune_unreachable_objects, request, timeout: GitalyClient.long_timeout)
end
def garbage_collect(create_bitmap, prune:) def garbage_collect(create_bitmap, prune:)
request = Gitaly::GarbageCollectRequest.new(repository: @gitaly_repo, create_bitmap: create_bitmap, prune: prune) request = Gitaly::GarbageCollectRequest.new(repository: @gitaly_repo, create_bitmap: create_bitmap, prune: prune)
GitalyClient.call(@storage, :repository_service, :garbage_collect, request, timeout: GitalyClient.long_timeout) GitalyClient.call(@storage, :repository_service, :garbage_collect, request, timeout: GitalyClient.long_timeout)
......
...@@ -54,6 +54,28 @@ RSpec.describe Gitlab::GitalyClient::RepositoryService do ...@@ -54,6 +54,28 @@ RSpec.describe Gitlab::GitalyClient::RepositoryService do
end end
end end
describe '#optimize_repository' do
it 'sends a optimize_repository message' do
expect_any_instance_of(Gitaly::RepositoryService::Stub)
.to receive(:optimize_repository)
.with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash))
.and_return(double(:optimize_repository))
client.optimize_repository
end
end
describe '#prune_unreachable_objects' do
it 'sends a prune_unreachable_objects message' do
expect_any_instance_of(Gitaly::RepositoryService::Stub)
.to receive(:prune_unreachable_objects)
.with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash))
.and_return(double(:prune_unreachable_objects))
client.prune_unreachable_objects
end
end
describe '#repository_size' do describe '#repository_size' do
it 'sends a repository_size message' do it 'sends a repository_size message' do
expect_any_instance_of(Gitaly::RepositoryService::Stub) expect_any_instance_of(Gitaly::RepositoryService::Stub)
......
...@@ -19,14 +19,28 @@ RSpec.shared_examples 'can collect git garbage' do |update_statistics: true| ...@@ -19,14 +19,28 @@ RSpec.shared_examples 'can collect git garbage' do |update_statistics: true|
end end
shared_examples 'it calls Gitaly' do shared_examples 'it calls Gitaly' do
specify do let(:repository_service) { instance_double(Gitlab::GitalyClient::RepositoryService) }
repository_service = instance_double(Gitlab::GitalyClient::RepositoryService)
expect(subject).to receive(:get_gitaly_client).with(task, repository.raw_repository).and_return(repository_service) specify do
expect(repository_service).to receive(gitaly_task) expect_next_instance_of(Gitlab::GitalyClient::RepositoryService, repository.raw_repository) do |instance|
expect(instance).to receive(:optimize_repository).and_call_original
end
subject.perform(*params) subject.perform(*params)
end end
context 'when optimized_housekeeping feature is disabled' do
before do
stub_feature_flags(optimized_housekeeping: false)
end
specify do
expect(subject).to receive(:get_gitaly_client).with(task, repository.raw_repository).and_return(repository_service)
expect(repository_service).to receive(gitaly_task)
subject.perform(*params)
end
end
end end
shared_examples 'it updates the resource statistics' do shared_examples 'it updates the resource statistics' do
...@@ -70,12 +84,31 @@ RSpec.shared_examples 'can collect git garbage' do |update_statistics: true| ...@@ -70,12 +84,31 @@ RSpec.shared_examples 'can collect git garbage' do |update_statistics: true|
end end
it 'handles gRPC errors' do it 'handles gRPC errors' do
allow_next_instance_of(Gitlab::GitalyClient::RepositoryService, repository.raw_repository) do |instance| repository_service = instance_double(Gitlab::GitalyClient::RepositoryService)
allow(instance).to receive(:garbage_collect).and_raise(GRPC::NotFound)
allow_next_instance_of(Projects::GitDeduplicationService) do |instance|
allow(instance).to receive(:execute)
end end
allow(repository.raw_repository).to receive(:gitaly_repository_client).and_return(repository_service)
allow(repository_service).to receive(:optimize_repository).and_raise(GRPC::NotFound)
expect { subject.perform(*params) }.to raise_exception(Gitlab::Git::Repository::NoRepository) expect { subject.perform(*params) }.to raise_exception(Gitlab::Git::Repository::NoRepository)
end end
context 'when optimized_housekeeping feature flag is disabled' do
before do
stub_feature_flags(optimized_housekeeping: false)
end
it 'handles gRPC errors' do
allow_next_instance_of(Gitlab::GitalyClient::RepositoryService, repository.raw_repository) do |instance|
allow(instance).to receive(:garbage_collect).and_raise(GRPC::NotFound)
end
expect { subject.perform(*params) }.to raise_exception(Gitlab::Git::Repository::NoRepository)
end
end
end end
context 'with different lease than the active one' do context 'with different lease than the active one' do
...@@ -152,13 +185,8 @@ RSpec.shared_examples 'can collect git garbage' do |update_statistics: true| ...@@ -152,13 +185,8 @@ RSpec.shared_examples 'can collect git garbage' do |update_statistics: true|
expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid) expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
end end
it 'calls Gitaly' do it_behaves_like 'it calls Gitaly' do
repository_service = instance_double(Gitlab::GitalyClient::RefService) let(:repository_service) { instance_double(Gitlab::GitalyClient::RefService) }
expect(subject).to receive(:get_gitaly_client).with(task, repository.raw_repository).and_return(repository_service)
expect(repository_service).to receive(gitaly_task)
subject.perform(*params)
end end
it 'does not update the resource statistics' do it 'does not update the resource statistics' do
...@@ -180,10 +208,26 @@ RSpec.shared_examples 'can collect git garbage' do |update_statistics: true| ...@@ -180,10 +208,26 @@ RSpec.shared_examples 'can collect git garbage' do |update_statistics: true|
it_behaves_like 'it updates the resource statistics' if update_statistics it_behaves_like 'it updates the resource statistics' if update_statistics
end end
context 'prune' do
before do
expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
end
specify do
expect_next_instance_of(Gitlab::GitalyClient::RepositoryService, repository.raw_repository) do |instance|
expect(instance).to receive(:prune_unreachable_objects).and_call_original
end
subject.perform(resource.id, 'prune', lease_key, lease_uuid)
end
end
shared_examples 'gc tasks' do shared_examples 'gc tasks' do
before do before do
allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid) allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
allow(subject).to receive(:bitmaps_enabled?).and_return(bitmaps_enabled) allow(subject).to receive(:bitmaps_enabled?).and_return(bitmaps_enabled)
stub_feature_flags(optimized_housekeeping: false)
end end
it 'incremental repack adds a new packfile' do it 'incremental repack adds a new packfile' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment