Refactor repositories clean up worker

Does not rely on FDW queries to find
projects to clean up.
parent 85aeffd2
...@@ -14,13 +14,14 @@ module Geo ...@@ -14,13 +14,14 @@ module Geo
# rubocop:disable CodeReuse/ActiveRecord # rubocop:disable CodeReuse/ActiveRecord
def perform(geo_node_id) def perform(geo_node_id)
try_obtain_lease do try_obtain_lease do
node = Geo::Fdw::GeoNode.find(geo_node_id) node = GeoNode.find(geo_node_id)
break unless node.selective_sync? break unless node.selective_sync?
node.projects_outside_selective_sync.find_in_batches(batch_size: BATCH_SIZE) do |batch| Geo::ProjectRegistry.select(:id, :project_id).find_in_batches(batch_size: BATCH_SIZE) do |registries|
batch.each do |project| tracked_project_ids = registries.map(&:project_id)
clean_up_repositories(project) replicable_project_ids = node.projects.id_in(tracked_project_ids).pluck_primary_key
end unused_tracked_project_ids = tracked_project_ids - replicable_project_ids
clean_up_repositories(unused_tracked_project_ids)
end end
end end
rescue ActiveRecord::RecordNotFound => error rescue ActiveRecord::RecordNotFound => error
...@@ -30,7 +31,15 @@ module Geo ...@@ -30,7 +31,15 @@ module Geo
private private
def clean_up_repositories(project) def clean_up_repositories(unused_tracked_project_ids)
unused_projects = Project.id_in(unused_tracked_project_ids)
unused_projects.each do |project|
clean_up_repository(project)
end
end
def clean_up_repository(project)
job_id = ::Geo::RepositoryCleanupWorker.perform_async(project.id, project.name, project.disk_path, project.repository.storage) job_id = ::Geo::RepositoryCleanupWorker.perform_async(project.id, project.name, project.disk_path, project.repository.storage)
if job_id if job_id
......
...@@ -2,30 +2,25 @@ ...@@ -2,30 +2,25 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Geo::RepositoriesCleanUpWorker, :geo, :geo_fdw do RSpec.describe Geo::RepositoriesCleanUpWorker, :geo do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers include ExclusiveLeaseHelpers
describe '#perform' do describe '#perform' do
let(:secondary) { create(:geo_node) } let_it_be(:secondary) { create(:geo_node) }
let_it_be(:synced_group) { create(:group) }
let(:synced_group) { create(:group) } let_it_be(:synced_subgroup) { create(:group, parent: synced_group) }
let(:synced_subgroup) { create(:group, parent: synced_group) } let_it_be(:unsynced_group) { create(:group) }
let(:unsynced_group) { create(:group) } let_it_be(:project_1) { create(:project, group: synced_group) }
let_it_be(:project_2) { create(:project, group: synced_group) }
let(:project_1) { create(:project, group: synced_group) } let_it_be(:project_3) { create(:project, :repository, group: unsynced_group) }
let(:project_2) { create(:project, group: synced_group) } let_it_be(:project_4) { create(:project, :repository, group: unsynced_group) }
let!(:project_3) { create(:project, :repository, group: unsynced_group) } let_it_be(:project_5) { create(:project, group: synced_subgroup) }
let(:project_4) { create(:project, :repository, group: unsynced_group) } let_it_be(:project_6) { create(:project, group: synced_subgroup) }
let(:project_5) { create(:project, group: synced_subgroup) } let_it_be(:project_7) { create(:project) }
let(:project_6) { create(:project, group: synced_subgroup) } let_it_be(:project_8) { create(:project) }
let(:project_7) { create(:project) }
let(:project_8) { create(:project) } before_all do
before do
stub_current_geo_node(secondary)
stub_exclusive_lease
create(:geo_project_registry, project: project_1) create(:geo_project_registry, project: project_1)
create(:geo_project_registry, project: project_2) create(:geo_project_registry, project: project_2)
create(:geo_project_registry, project: project_4) create(:geo_project_registry, project: project_4)
...@@ -35,6 +30,11 @@ RSpec.describe Geo::RepositoriesCleanUpWorker, :geo, :geo_fdw do ...@@ -35,6 +30,11 @@ RSpec.describe Geo::RepositoriesCleanUpWorker, :geo, :geo_fdw do
create(:geo_project_registry, project: project_8) create(:geo_project_registry, project: project_8)
end end
before do
stub_current_geo_node(secondary)
stub_exclusive_lease
end
it 'does not perform Geo::RepositoryCleanupWorker when cannnot obtain a lease' do it 'does not perform Geo::RepositoryCleanupWorker when cannnot obtain a lease' do
stub_exclusive_lease_taken stub_exclusive_lease_taken
...@@ -76,10 +76,8 @@ RSpec.describe Geo::RepositoriesCleanUpWorker, :geo, :geo_fdw do ...@@ -76,10 +76,8 @@ RSpec.describe Geo::RepositoriesCleanUpWorker, :geo, :geo_fdw do
subject.perform(secondary.id) subject.perform(secondary.id)
end end
it 'does not leave orphaned entries in the project_registry table' do it 'does not leave orphaned entries in the project_registry table', :sidekiq_inline do
Sidekiq::Testing.inline! do
subject.perform(secondary.id) subject.perform(secondary.id)
end
expect(Geo::ProjectRegistry.where(project_id: [project_3, project_4, project_7, project_8])).to be_empty expect(Geo::ProjectRegistry.where(project_id: [project_3, project_4, project_7, project_8])).to be_empty
end end
...@@ -109,10 +107,8 @@ RSpec.describe Geo::RepositoriesCleanUpWorker, :geo, :geo_fdw do ...@@ -109,10 +107,8 @@ RSpec.describe Geo::RepositoriesCleanUpWorker, :geo, :geo_fdw do
subject.perform(secondary.id) subject.perform(secondary.id)
end end
it 'does not leave orphaned entries in the project_registry table' do it 'does not leave orphaned entries in the project_registry table', :sidekiq_inline do
Sidekiq::Testing.inline! do
subject.perform(secondary.id) subject.perform(secondary.id)
end
expect(Geo::ProjectRegistry.where(project_id: [project_1, project_2, project_3, project_4, project_5, project_6])).to be_empty expect(Geo::ProjectRegistry.where(project_id: [project_1, project_2, project_3, project_4, project_5, project_6])).to be_empty
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