Remove project restrictions from the repository backfill service

parent 40dac302
......@@ -10,18 +10,6 @@ module Geo
end
def execute
# When Geo customers upgrade to 9.0, the secondaries nodes that are
# enabled will start the backfilling process automatically. We need
# to populate the tracking database correctly for projects synced
# before the process being started or projects created during the
# backfilling. Otherwise, the query to retrieve the projects will
# always return the same projects because they don't have entries
# in the tracking database
if backfilled?
update_registry(DateTime.now, DateTime.now)
return
end
try_obtain_lease do
log('Started repository sync')
started_at, finished_at = fetch_repositories
......@@ -76,7 +64,6 @@ module Geo
def try_obtain_lease
log('Trying to obtain lease to sync repository')
repository_lease = Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT).try_obtain
if repository_lease.nil?
......@@ -93,25 +80,7 @@ module Geo
Gitlab::ExclusiveLease.cancel(lease_key, repository_lease)
end
def backfilled?
return false unless project.repository.exists?
return false if project.repository.exists? && project.repository.empty?
return false if failed_registry_exists?
true
end
def failed_registry_exists?
Geo::ProjectRegistry.failed.where(project_id: project_id).any?
end
def synced_registry_exists?
Geo::ProjectRegistry.synced.where(project_id: project_id).any?
end
def update_registry(started_at, finished_at)
return if synced_registry_exists?
log('Updating registry information')
registry = Geo::ProjectRegistry.find_or_initialize_by(project_id: project_id)
registry.last_repository_synced_at = started_at
......
......@@ -67,35 +67,67 @@ describe Geo::RepositoryBackfillService, services: true do
context 'when repository exists and is not empty' do
let(:project) { create(:project) }
it 'does not fetch the project repositories' do
expect_any_instance_of(Repository).not_to receive(:fetch_geo_mirror)
it 'fetches project repositories' do
fetch_count = 0
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) do
fetch_count += 1
end
subject.execute
expect(fetch_count).to eq 2
end
context 'tracking database' do
it 'tracks missing repository sync' do
it 'tracks repository sync' do
expect { subject.execute }.to change(Geo::ProjectRegistry, :count).by(1)
end
it 'stores last_repository_successful_sync_at when succeed' do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) { true }
subject.execute
registry = Geo::ProjectRegistry.find_by(project_id: project.id)
expect(registry.last_repository_successful_sync_at).not_to be_nil
end
it 'reset last_repository_successful_sync_at when fail' do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) { raise Gitlab::Shell::Error }
subject.execute
registry = Geo::ProjectRegistry.find_by(project_id: project.id)
expect(registry.last_repository_successful_sync_at).to be_nil
end
end
end
context 'when repository was backfilled successfully' do
let(:project) { create(:project) }
let(:last_repository_successful_sync_at) { 5.days.ago }
let(:last_repository_synced_at) { 5.days.ago }
let!(:registry) do
Geo::ProjectRegistry.create(
project: project,
last_repository_synced_at: 5.days.ago,
last_repository_successful_sync_at: last_repository_successful_sync_at
last_repository_synced_at: last_repository_synced_at,
last_repository_successful_sync_at: last_repository_synced_at
)
end
it 'does not fetch the project repositories' do
expect_any_instance_of(Repository).not_to receive(:fetch_geo_mirror)
it 'fetches project repositories' do
fetch_count = 0
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) do
fetch_count += 1
end
subject.execute
expect(fetch_count).to eq 2
end
context 'tracking database' do
......@@ -103,10 +135,26 @@ describe Geo::RepositoryBackfillService, services: true do
expect { subject.execute }.not_to change(Geo::ProjectRegistry, :count)
end
it 'does not update last_repository_successful_sync_at' do
it 'updates registry when succeed' do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) { true }
subject.execute
expect(registry.reload.last_repository_successful_sync_at).to be_within(1.second).of(last_repository_successful_sync_at)
registry.reload
expect(registry.last_repository_synced_at).to be_within(1.minute).of(Time.now)
expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(Time.now)
end
it 'does not update registry last_repository_successful_sync_at when fail' do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) { raise Gitlab::Shell::Error }
subject.execute
registry.reload
expect(registry.last_repository_synced_at).to be_within(1.minute).of(Time.now)
expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(last_repository_synced_at)
end
end
end
......
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