Refactoring Geo::ProjectRegistryFinder

parent 7017fb41
...@@ -72,8 +72,8 @@ class GeoNodeStatus < ActiveRecord::Base ...@@ -72,8 +72,8 @@ class GeoNodeStatus < ActiveRecord::Base
self.db_replication_lag_seconds = Gitlab::Geo::HealthCheck.db_replication_lag_seconds self.db_replication_lag_seconds = Gitlab::Geo::HealthCheck.db_replication_lag_seconds
self.cursor_last_event_id = Geo::EventLogState.last_processed&.event_id self.cursor_last_event_id = Geo::EventLogState.last_processed&.event_id
self.cursor_last_event_date = Geo::EventLog.find_by(id: self.cursor_last_event_id)&.created_at self.cursor_last_event_date = Geo::EventLog.find_by(id: self.cursor_last_event_id)&.created_at
self.repositories_synced_count = projects_finder.count_synced_projects self.repositories_synced_count = projects_finder.count_synced_project_registries
self.repositories_failed_count = projects_finder.count_failed_projects self.repositories_failed_count = projects_finder.count_failed_project_registries
self.lfs_objects_synced_count = lfs_objects_finder.count_synced_lfs_objects self.lfs_objects_synced_count = lfs_objects_finder.count_synced_lfs_objects
self.lfs_objects_failed_count = lfs_objects_finder.count_failed_lfs_objects self.lfs_objects_failed_count = lfs_objects_finder.count_failed_lfs_objects
self.attachments_synced_count = attachments_finder.find_synced_attachments.count self.attachments_synced_count = attachments_finder.find_synced_attachments.count
......
module Geo module Geo
class ProjectRegistryFinder < RegistryFinder class ProjectRegistryFinder < RegistryFinder
def count_synced_projects def count_synced_project_registries
relation = relation =
if selective_sync? if selective_sync?
legacy_find_synced_projects legacy_find_synced_project_registries
else else
find_synced_projects_registries find_synced_project_registries
end end
relation.count relation.count
end end
def count_failed_projects def count_failed_project_registries
relation = find_failed_project_registries.count
if selective_sync?
legacy_find_failed_projects
else
find_failed_projects_registries
end
relation.count
end end
def find_failed_project_registries(type = nil) def find_failed_project_registries(type = nil)
relation = relation =
if selective_sync? if selective_sync?
legacy_find_failed_project_registries(type) legacy_find_filtered_failed_project_registries(type)
else else
find_failed_projects_registries(type) find_filtered_failed_project_registries(type)
end end
relation relation
...@@ -57,11 +50,11 @@ module Geo ...@@ -57,11 +50,11 @@ module Geo
protected protected
def find_synced_projects_registries def find_synced_project_registries
Geo::ProjectRegistry.synced Geo::ProjectRegistry.synced
end end
def find_failed_projects_registries(type = nil) def find_filtered_failed_project_registries(type = nil)
case type case type
when 'repository' when 'repository'
Geo::ProjectRegistry.failed_repos Geo::ProjectRegistry.failed_repos
...@@ -117,16 +110,18 @@ module Geo ...@@ -117,16 +110,18 @@ module Geo
legacy_find_projects(Geo::ProjectRegistry.dirty.retry_due.pluck(:project_id)) legacy_find_projects(Geo::ProjectRegistry.dirty.retry_due.pluck(:project_id))
end end
# @return [ActiveRecord::Relation<Project>] list of synced projects # @return [ActiveRecord::Relation<Geo::ProjectRegistry>] list of synced projects
def legacy_find_synced_projects def legacy_find_synced_project_registries
legacy_find_projects(Geo::ProjectRegistry.synced.pluck(:project_id)) legacy_find_project_registries(Geo::ProjectRegistry.synced)
end end
# @return [ActiveRecord::Relation<Project>] list of projects that sync has failed # @return [ActiveRecord::Relation<Geo::ProjectRegistry>] list of projects that sync has failed
def legacy_find_failed_projects def legacy_find_filtered_failed_project_registries(type = nil)
legacy_find_projects(Geo::ProjectRegistry.failed.pluck(:project_id)) project_registries = find_filtered_failed_project_registries(type)
legacy_find_project_registries(project_registries)
end end
# @return [ActiveRecord::Relation<Project>]
def legacy_find_projects(registry_project_ids) def legacy_find_projects(registry_project_ids)
return Project.none if registry_project_ids.empty? return Project.none if registry_project_ids.empty?
...@@ -140,15 +135,15 @@ module Geo ...@@ -140,15 +135,15 @@ module Geo
joined_relation joined_relation
end end
def legacy_find_failed_project_registries(type) # @return [ActiveRecord::Relation<Geo::ProjectRegistry>]
project_registries = find_failed_projects_registries(type) def legacy_find_project_registries(project_registries)
return Geo::ProjectRegistry.none if project_registries.empty? return Geo::ProjectRegistry.none if project_registries.empty?
joined_relation = project_registries.joins(<<~SQL) joined_relation = project_registries.joins(<<~SQL)
INNER JOIN INNER JOIN
(VALUES #{current_node.projects.pluck(:id).map { |id| "(#{id})" }.join(',')}) (VALUES #{current_node.projects.pluck(:id).map { |id| "(#{id})" }.join(',')})
projects(project_id) projects(id)
ON #{Geo::ProjectRegistry.table_name}.id = projects.project_id ON #{Geo::ProjectRegistry.table_name}.project_id = projects.id
SQL SQL
joined_relation joined_relation
......
...@@ -18,11 +18,11 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do ...@@ -18,11 +18,11 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do
stub_current_geo_node(secondary) stub_current_geo_node(secondary)
end end
describe '#count_synced_projects' do describe '#count_synced_project_registries' do
it 'delegates to #find_synced_projects_registries' do it 'delegates to #find_synced_project_registries' do
expect(subject).to receive(:find_synced_projects_registries).and_call_original expect(subject).to receive(:find_synced_project_registries).and_call_original
subject.count_synced_projects subject.count_synced_project_registries
end end
it 'counts projects that has been synced' do it 'counts projects that has been synced' do
...@@ -31,7 +31,7 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do ...@@ -31,7 +31,7 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do
create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty) create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty)
create(:geo_project_registry, :synced, :wiki_dirty, project: project_wiki_dirty) create(:geo_project_registry, :synced, :wiki_dirty, project: project_wiki_dirty)
expect(subject.count_synced_projects).to eq 1 expect(subject.count_synced_project_registries).to eq 1
end end
context 'with selective sync' do context 'with selective sync' do
...@@ -39,10 +39,10 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do ...@@ -39,10 +39,10 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do
secondary.update_attribute(:namespaces, [synced_group]) secondary.update_attribute(:namespaces, [synced_group])
end end
it 'delegates to #legacy_find_synced_projects' do it 'delegates to #legacy_find_synced_project_registries' do
expect(subject).to receive(:legacy_find_synced_projects).and_call_original expect(subject).to receive(:legacy_find_synced_project_registries).and_call_original
subject.count_synced_projects subject.count_synced_project_registries
end end
it 'counts projects that has been synced' do it 'counts projects that has been synced' do
...@@ -53,16 +53,16 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do ...@@ -53,16 +53,16 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do
create(:geo_project_registry, :synced, project: project_1_in_synced_group) create(:geo_project_registry, :synced, project: project_1_in_synced_group)
create(:geo_project_registry, :sync_failed, project: project_2_in_synced_group) create(:geo_project_registry, :sync_failed, project: project_2_in_synced_group)
expect(subject.count_synced_projects).to eq 1 expect(subject.count_synced_project_registries).to eq 1
end end
end end
end end
describe '#count_failed_projects' do describe '#count_failed_project_registries' do
it 'delegates to #find_failed_projects_registries' do it 'delegates to #find_failed_project_registries' do
expect(subject).to receive(:find_failed_projects_registries).and_call_original expect(subject).to receive(:find_failed_project_registries).and_call_original
subject.count_failed_projects subject.count_failed_project_registries
end end
it 'counts projects that sync has failed' do it 'counts projects that sync has failed' do
...@@ -71,7 +71,7 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do ...@@ -71,7 +71,7 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do
create(:geo_project_registry, :repository_sync_failed, project: project_repository_dirty) create(:geo_project_registry, :repository_sync_failed, project: project_repository_dirty)
create(:geo_project_registry, :wiki_sync_failed, project: project_wiki_dirty) create(:geo_project_registry, :wiki_sync_failed, project: project_wiki_dirty)
expect(subject.count_failed_projects).to eq 3 expect(subject.count_failed_project_registries).to eq 3
end end
context 'with selective sync' do context 'with selective sync' do
...@@ -79,10 +79,10 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do ...@@ -79,10 +79,10 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do
secondary.update_attribute(:namespaces, [synced_group]) secondary.update_attribute(:namespaces, [synced_group])
end end
it 'delegates to #legacy_find_failed_projects' do it 'delegates to #find_failed_project_registries' do
expect(subject).to receive(:legacy_find_failed_projects).and_call_original expect(subject).to receive(:find_failed_project_registries).and_call_original
subject.count_failed_projects subject.count_failed_project_registries
end end
it 'counts projects that sync has failed' do it 'counts projects that sync has failed' do
...@@ -93,7 +93,63 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do ...@@ -93,7 +93,63 @@ describe Geo::ProjectRegistryFinder, :geo, :truncate do
create(:geo_project_registry, :repository_sync_failed, project: project_1_in_synced_group) create(:geo_project_registry, :repository_sync_failed, project: project_1_in_synced_group)
create(:geo_project_registry, :synced, project: project_2_in_synced_group) create(:geo_project_registry, :synced, project: project_2_in_synced_group)
expect(subject.count_failed_projects).to eq 1 expect(subject.count_failed_project_registries).to eq 1
end
end
end
describe '#find_failed_project_registries' do
let(:project_1_in_synced_group) { create(:project, group: synced_group) }
let(:project_2_in_synced_group) { create(:project, group: synced_group) }
let!(:synced) { create(:geo_project_registry, :synced) }
let!(:sync_failed) { create(:geo_project_registry, :sync_failed, project: project_synced) }
let!(:repository_sync_failed) { create(:geo_project_registry, :repository_sync_failed, project: project_1_in_synced_group) }
let!(:wiki_sync_failed) { create(:geo_project_registry, :wiki_sync_failed, project: project_2_in_synced_group) }
it 'delegates to #find_filtered_failed_project_registries' do
expect(subject).to receive(:find_filtered_failed_project_registries).and_call_original
subject.find_failed_project_registries
end
it 'returns project registries that sync has failed' do
expect(subject.find_failed_project_registries).to match_array([sync_failed, repository_sync_failed, wiki_sync_failed])
end
it 'returns only project registries that repository sync has failed' do
expect(subject.find_failed_project_registries('repository')).to match_array([sync_failed, repository_sync_failed])
end
it 'returns only project registries that wiki sync has failed' do
expect(subject.find_failed_project_registries('wiki')).to match_array([sync_failed, wiki_sync_failed])
end
context 'with selective sync' do
before do
secondary.update_attribute(:namespaces, [synced_group])
end
it 'delegates to #legacy_find_filtered_failed_project_registries' do
expect(subject).to receive(:legacy_find_filtered_failed_project_registries).and_call_original
subject.find_failed_project_registries
end
it 'returns project registries that sync has failed' do
expect(subject.find_failed_project_registries).to match_array([repository_sync_failed, wiki_sync_failed])
end
it 'returns only project registries that repository sync has failed' do
create(:geo_project_registry, :repository_sync_failed)
expect(subject.find_failed_project_registries('repository')).to match_array([repository_sync_failed])
end
it 'returns only project registries that wiki sync has failed' do
create(:geo_project_registry, :wiki_sync_failed)
expect(subject.find_failed_project_registries('wiki')).to match_array([wiki_sync_failed])
end end
end end
end end
......
...@@ -40,6 +40,7 @@ FactoryGirl.define do ...@@ -40,6 +40,7 @@ FactoryGirl.define do
resync_repository true resync_repository true
resync_wiki true resync_wiki true
repository_retry_count 1 repository_retry_count 1
wiki_retry_count 1
end end
trait :repository_sync_failed do trait :repository_sync_failed 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