2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'fileutils'
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe GitGarbageCollectWorker do
|
2018-12-05 23:21:45 +05:30
|
|
|
include GitHelpers
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2016-08-24 12:49:21 +05:30
|
|
|
let(:shell) { Gitlab::Shell.new }
|
2018-03-17 18:26:18 +05:30
|
|
|
let!(:lease_uuid) { SecureRandom.uuid }
|
|
|
|
let!(:lease_key) { "project_housekeeping:#{project.id}" }
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:params) { [project.id, task, lease_key, lease_uuid] }
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
subject { described_class.new }
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
shared_examples 'it calls Gitaly' do
|
|
|
|
specify do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(gitaly_task)
|
|
|
|
.and_return(nil)
|
|
|
|
|
|
|
|
subject.perform(*params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'it updates the project statistics' do
|
|
|
|
specify do
|
|
|
|
expect_any_instance_of(Projects::UpdateStatisticsService).to receive(:execute).and_call_original
|
|
|
|
expect(Projects::UpdateStatisticsService)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(project, nil, statistics: [:repository_size, :lfs_objects_size])
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
subject.perform(*params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
describe "#perform" do
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:gitaly_task) { :garbage_collect }
|
|
|
|
let(:task) { :gc }
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'with active lease_uuid' do
|
|
|
|
before do
|
|
|
|
allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'it calls Gitaly'
|
|
|
|
it_behaves_like 'it updates the project statistics'
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it "flushes ref caches when the task if 'gc'" do
|
|
|
|
expect(subject).to receive(:renew_lease).with(lease_key, lease_uuid).and_call_original
|
|
|
|
expect_any_instance_of(Repository).to receive(:after_create_branch).and_call_original
|
|
|
|
expect_any_instance_of(Repository).to receive(:branch_names).and_call_original
|
|
|
|
expect_any_instance_of(Repository).to receive(:has_visible_content?).and_call_original
|
|
|
|
expect_any_instance_of(Gitlab::Git::Repository).to receive(:has_visible_content?).and_call_original
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
subject.perform(*params)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles gRPC errors' do
|
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(:garbage_collect).and_raise(GRPC::NotFound)
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
expect { subject.perform(*params) }.to raise_exception(Gitlab::Git::Repository::NoRepository)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with different lease than the active one' do
|
|
|
|
before do
|
|
|
|
allow(subject).to receive(:get_lease_uuid).and_return(SecureRandom.uuid)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns silently' do
|
|
|
|
expect_any_instance_of(Repository).not_to receive(:after_create_branch).and_call_original
|
|
|
|
expect_any_instance_of(Repository).not_to receive(:branch_names).and_call_original
|
|
|
|
expect_any_instance_of(Repository).not_to receive(:has_visible_content?).and_call_original
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
subject.perform(*params)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no active lease' do
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:params) { [project.id] }
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
before do
|
|
|
|
allow(subject).to receive(:get_lease_uuid).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when is able to get the lease' do
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
allow(subject).to receive(:try_obtain_lease).and_return(SecureRandom.uuid)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'it calls Gitaly'
|
|
|
|
it_behaves_like 'it updates the project statistics'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it "flushes ref caches when the task if 'gc'" do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(subject).to receive(:get_lease_uuid).with("git_gc:#{task}:#{project.id}").and_return(false)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect_any_instance_of(Repository).to receive(:after_create_branch).and_call_original
|
|
|
|
expect_any_instance_of(Repository).to receive(:branch_names).and_call_original
|
|
|
|
expect_any_instance_of(Repository).to receive(:has_visible_content?).and_call_original
|
|
|
|
expect_any_instance_of(Gitlab::Git::Repository).to receive(:has_visible_content?).and_call_original
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
subject.perform(*params)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
context 'when the repository has joined a pool' do
|
|
|
|
let!(:pool) { create(:pool_repository, :ready) }
|
|
|
|
let(:project) { pool.source_project }
|
|
|
|
|
|
|
|
it 'ensures the repositories are linked' do
|
|
|
|
expect_any_instance_of(PoolRepository).to receive(:link_repository).once
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
subject.perform(*params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'LFS object garbage collection' do
|
|
|
|
before do
|
|
|
|
stub_lfs_setting(enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:lfs_reference) { create(:lfs_objects_project, project: project) }
|
|
|
|
let(:lfs_object) { lfs_reference.lfs_object }
|
|
|
|
|
|
|
|
context 'with cleanup_lfs_during_gc feature flag enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(cleanup_lfs_during_gc: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cleans up unreferenced LFS objects' do
|
|
|
|
expect_next_instance_of(Gitlab::Cleanup::OrphanLfsFileReferences) do |svc|
|
|
|
|
expect(svc.project).to eq(project)
|
|
|
|
expect(svc.dry_run).to be_falsy
|
|
|
|
expect(svc).to receive(:run!).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.perform(*params)
|
|
|
|
|
|
|
|
expect(project.lfs_objects.reload).not_to include(lfs_object)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does nothing if the database is read-only' do
|
|
|
|
expect(Gitlab::Database).to receive(:read_only?) { true }
|
|
|
|
expect_any_instance_of(Gitlab::Cleanup::OrphanLfsFileReferences).not_to receive(:run!)
|
|
|
|
|
|
|
|
subject.perform(*params)
|
|
|
|
|
|
|
|
expect(project.lfs_objects.reload).to include(lfs_object)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with cleanup_lfs_during_gc feature flag disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(cleanup_lfs_during_gc: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not clean up unreferenced LFS objects' do
|
|
|
|
expect_any_instance_of(Gitlab::Cleanup::OrphanLfsFileReferences).not_to receive(:run!)
|
|
|
|
|
|
|
|
subject.perform(*params)
|
|
|
|
|
|
|
|
expect(project.lfs_objects.reload).to include(lfs_object)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when no lease can be obtained' do
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(subject).to receive(:try_obtain_lease).and_return(false)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns silently' do
|
|
|
|
expect(subject).not_to receive(:command)
|
|
|
|
expect_any_instance_of(Repository).not_to receive(:after_create_branch).and_call_original
|
|
|
|
expect_any_instance_of(Repository).not_to receive(:branch_names).and_call_original
|
|
|
|
expect_any_instance_of(Repository).not_to receive(:has_visible_content?).and_call_original
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
subject.perform(*params)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "repack_full" do
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:task) { :full_repack }
|
|
|
|
let(:gitaly_task) { :repack_full }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
|
|
|
expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'it calls Gitaly'
|
|
|
|
it_behaves_like 'it updates the project statistics'
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
context "pack_refs" do
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:task) { :pack_refs }
|
|
|
|
let(:gitaly_task) { :pack_refs }
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
before do
|
|
|
|
expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls Gitaly" do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect_any_instance_of(Gitlab::GitalyClient::RefService).to receive(task)
|
2019-07-31 22:56:46 +05:30
|
|
|
.and_return(nil)
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
subject.perform(*params)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update the project statistics' do
|
|
|
|
expect(Projects::UpdateStatisticsService).not_to receive(:new)
|
|
|
|
|
|
|
|
subject.perform(*params)
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context "repack_incremental" do
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:task) { :incremental_repack }
|
|
|
|
let(:gitaly_task) { :repack_incremental }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
|
|
|
expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it_behaves_like 'it calls Gitaly'
|
|
|
|
it_behaves_like 'it updates the project statistics'
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
shared_examples 'gc tasks' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
2018-03-17 18:26:18 +05:30
|
|
|
allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
|
2017-09-10 17:25:29 +05:30
|
|
|
allow(subject).to receive(:bitmaps_enabled?).and_return(bitmaps_enabled)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'incremental repack adds a new packfile' do
|
|
|
|
create_objects(project)
|
|
|
|
before_packs = packs(project)
|
|
|
|
|
|
|
|
expect(before_packs.count).to be >= 1
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
subject.perform(project.id, 'incremental_repack', lease_key, lease_uuid)
|
2017-08-17 22:00:37 +05:30
|
|
|
after_packs = packs(project)
|
|
|
|
|
|
|
|
# Exactly one new pack should have been created
|
|
|
|
expect(after_packs.count).to eq(before_packs.count + 1)
|
|
|
|
|
|
|
|
# Previously existing packs are still around
|
|
|
|
expect(before_packs & after_packs).to eq(before_packs)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'full repack consolidates into 1 packfile' do
|
|
|
|
create_objects(project)
|
2018-03-17 18:26:18 +05:30
|
|
|
subject.perform(project.id, 'incremental_repack', lease_key, lease_uuid)
|
2017-08-17 22:00:37 +05:30
|
|
|
before_packs = packs(project)
|
|
|
|
|
|
|
|
expect(before_packs.count).to be >= 2
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
subject.perform(project.id, 'full_repack', lease_key, lease_uuid)
|
2017-08-17 22:00:37 +05:30
|
|
|
after_packs = packs(project)
|
|
|
|
|
|
|
|
expect(after_packs.count).to eq(1)
|
|
|
|
|
|
|
|
# Previously existing packs should be gone now
|
|
|
|
expect(after_packs - before_packs).to eq(after_packs)
|
|
|
|
|
|
|
|
expect(File.exist?(bitmap_path(after_packs.first))).to eq(bitmaps_enabled)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'gc consolidates into 1 packfile and updates packed-refs' do
|
|
|
|
create_objects(project)
|
|
|
|
before_packs = packs(project)
|
|
|
|
before_packed_refs = packed_refs(project)
|
|
|
|
|
|
|
|
expect(before_packs.count).to be >= 1
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
subject.perform(project.id, 'gc', lease_key, lease_uuid)
|
2017-08-17 22:00:37 +05:30
|
|
|
after_packed_refs = packed_refs(project)
|
|
|
|
after_packs = packs(project)
|
|
|
|
|
|
|
|
expect(after_packs.count).to eq(1)
|
|
|
|
|
|
|
|
# Previously existing packs should be gone now
|
|
|
|
expect(after_packs - before_packs).to eq(after_packs)
|
|
|
|
|
|
|
|
# The packed-refs file should have been updated during 'git gc'
|
|
|
|
expect(before_packed_refs).not_to eq(after_packed_refs)
|
|
|
|
|
|
|
|
expect(File.exist?(bitmap_path(after_packs.first))).to eq(bitmaps_enabled)
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
it 'cleans up repository after finishing' do
|
|
|
|
expect_any_instance_of(Project).to receive(:cleanup).and_call_original
|
|
|
|
|
|
|
|
subject.perform(project.id, 'gc', lease_key, lease_uuid)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with bitmaps enabled' do
|
|
|
|
let(:bitmaps_enabled) { true }
|
|
|
|
|
|
|
|
include_examples 'gc tasks'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with bitmaps disabled' do
|
|
|
|
let(:bitmaps_enabled) { false }
|
|
|
|
|
|
|
|
include_examples 'gc tasks'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new commit on a random new branch
|
|
|
|
def create_objects(project)
|
2018-12-05 23:21:45 +05:30
|
|
|
rugged = rugged_repo(project.repository)
|
2017-08-17 22:00:37 +05:30
|
|
|
old_commit = rugged.branches.first.target
|
|
|
|
new_commit_sha = Rugged::Commit.create(
|
|
|
|
rugged,
|
|
|
|
message: "hello world #{SecureRandom.hex(6)}",
|
2020-03-13 15:44:24 +05:30
|
|
|
author: { email: 'foo@bar', name: 'baz' },
|
|
|
|
committer: { email: 'foo@bar', name: 'baz' },
|
2017-08-17 22:00:37 +05:30
|
|
|
tree: old_commit.tree,
|
2017-09-10 17:25:29 +05:30
|
|
|
parents: [old_commit]
|
2017-08-17 22:00:37 +05:30
|
|
|
)
|
2018-11-18 11:00:15 +05:30
|
|
|
rugged.references.create("refs/heads/#{SecureRandom.hex(6)}", new_commit_sha)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def packs(project)
|
2018-11-08 19:23:39 +05:30
|
|
|
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
|
|
|
|
Dir["#{project.repository.path_to_repo}/objects/pack/*.pack"]
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def packed_refs(project)
|
|
|
|
path = "#{project.repository.path_to_repo}/packed-refs"
|
|
|
|
FileUtils.touch(path)
|
|
|
|
File.read(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bitmap_path(pack)
|
|
|
|
pack.sub(/\.pack\z/, '.bitmap')
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|