Refactor legacy finder to find registries that have been synced

These changes use the new finder when FDW is enabled without selective
sync to avoid code duplication.
parent a365ab9d
# frozen_string_literal: true # frozen_string_literal: true
# Finder for retrieving project registries that have been synced # Finder for retrieving project registries that have been synced
# scoped to a type (repository or wiki) using cross-database joins # scoped to a type (repository or wiki) using cross-database joins.
# for selective sync.
# #
# Basic usage: # Basic usage:
# #
...@@ -22,30 +21,16 @@ module Geo ...@@ -22,30 +21,16 @@ module Geo
end end
def execute def execute
if selective_sync?
synced_registries_for_selective_sync
else
synced_registries
end
end
private
attr_reader :type
def synced_registries
Geo::ProjectRegistry.synced(type)
end
# rubocop: disable CodeReuse/ActiveRecord
def synced_registries_for_selective_sync
legacy_inner_join_registry_ids( legacy_inner_join_registry_ids(
synced_registries, Geo::ProjectRegistry.synced(type),
current_node.projects.pluck(:id), current_node.projects.pluck_primary_key,
Geo::ProjectRegistry, Geo::ProjectRegistry,
foreign_key: :project_id foreign_key: :project_id
) )
end end
# rubocop: enable CodeReuse/ActiveRecord
private
attr_reader :type
end end
end end
...@@ -168,10 +168,10 @@ module Geo ...@@ -168,10 +168,10 @@ module Geo
end end
def finder_klass_for_synced_registries def finder_klass_for_synced_registries
if Gitlab::Geo::Fdw.enabled_for_selective_sync? if !Gitlab::Geo::Fdw.enabled? || use_legacy_queries_for_selective_sync?
Geo::ProjectRegistrySyncedFinder
else
Geo::LegacyProjectRegistrySyncedFinder Geo::LegacyProjectRegistrySyncedFinder
else
Geo::ProjectRegistrySyncedFinder
end end
end end
......
...@@ -22,84 +22,68 @@ describe Geo::LegacyProjectRegistrySyncedFinder, :geo do ...@@ -22,84 +22,68 @@ describe Geo::LegacyProjectRegistrySyncedFinder, :geo do
let!(:registry_repository_dirty_broken_shard) { create(:geo_project_registry, :synced, :repository_dirty, project: project_5) } let!(:registry_repository_dirty_broken_shard) { create(:geo_project_registry, :synced, :repository_dirty, project: project_5) }
let!(:registry_sync_failed) { create(:geo_project_registry, :sync_failed) } let!(:registry_sync_failed) { create(:geo_project_registry, :sync_failed) }
shared_examples 'finds synced registries' do before do
context 'with repository type' do stub_fdw_disabled
subject { described_class.new(current_node: node, type: :repository) } end
context 'without selective sync' do
it 'returns all synced registries' do
expect(subject.execute).to match_array([registry_synced, registry_wiki_dirty, registry_wiki_dirty_broken_shard])
end
end
context 'with selective sync by namespace' do context 'with repository type' do
it 'returns synced registries where projects belongs to the namespaces' do subject { described_class.new(current_node: node, type: :repository) }
node.update!(selective_sync_type: 'namespaces', namespaces: [group_1, nested_group_1])
expect(subject.execute).to match_array([registry_synced, registry_wiki_dirty]) context 'without selective sync' do
end it 'returns all synced registries' do
expect(subject.execute).to match_array([registry_synced, registry_wiki_dirty, registry_wiki_dirty_broken_shard])
end end
end
context 'with selective sync by shard' do context 'with selective sync by namespace' do
it 'returns synced registries where projects belongs to the shards' do it 'returns synced registries where projects belongs to the namespaces' do
node.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken']) node.update!(selective_sync_type: 'namespaces', namespaces: [group_1, nested_group_1])
expect(subject.execute).to match_array([registry_wiki_dirty_broken_shard]) expect(subject.execute).to match_array([registry_synced, registry_wiki_dirty])
end
end end
end end
context 'with wiki type' do context 'with selective sync by shard' do
subject { described_class.new(current_node: node, type: :wiki) } it 'returns synced registries where projects belongs to the shards' do
node.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken'])
context 'without selective sync' do expect(subject.execute).to match_array([registry_wiki_dirty_broken_shard])
it 'returns all synced registries' do
expect(subject.execute).to match_array([registry_synced, registry_repository_dirty, registry_repository_dirty_broken_shard])
end
end end
end
end
context 'with selective sync by namespace' do context 'with wiki type' do
it 'returns synced registries where projects belongs to the namespaces' do subject { described_class.new(current_node: node, type: :wiki) }
node.update!(selective_sync_type: 'namespaces', namespaces: [group_1, nested_group_1])
expect(subject.execute).to match_array([registry_synced, registry_repository_dirty]) context 'without selective sync' do
end it 'returns all synced registries' do
expect(subject.execute).to match_array([registry_synced, registry_repository_dirty, registry_repository_dirty_broken_shard])
end end
end
context 'with selective sync by shard' do context 'with selective sync by namespace' do
it 'returns synced registries where projects belongs to the shards' do it 'returns synced registries where projects belongs to the namespaces' do
node.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken']) node.update!(selective_sync_type: 'namespaces', namespaces: [group_1, nested_group_1])
expect(subject.execute).to match_array([registry_repository_dirty_broken_shard]) expect(subject.execute).to match_array([registry_synced, registry_repository_dirty])
end
end end
end end
context 'with invalid type' do context 'with selective sync by shard' do
subject { described_class.new(current_node: node, type: :invalid) } it 'returns synced registries where projects belongs to the shards' do
node.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken'])
it 'returns nothing' do expect(subject.execute).to match_array([registry_repository_dirty_broken_shard])
expect(subject.execute).to be_empty
end end
end end
end end
# Disable transactions via :delete method because a foreign table context 'with invalid type' do
# can't see changes inside a transaction of a different connection. subject { described_class.new(current_node: node, type: :invalid) }
context 'FDW', :delete do
before do
skip('FDW is not configured') unless Gitlab::Geo::Fdw.enabled?
end
include_examples 'finds synced registries' it 'returns nothing' do
end expect(subject.execute).to be_empty
context 'Legacy' do
before do
stub_fdw_disabled
end end
include_examples 'finds synced registries'
end end
end end
end end
...@@ -28,27 +28,34 @@ describe Geo::ProjectRegistryFinder, :geo do ...@@ -28,27 +28,34 @@ describe Geo::ProjectRegistryFinder, :geo do
shared_examples 'counts all the things' do |method_prefix| shared_examples 'counts all the things' do |method_prefix|
describe '#count_synced_repositories' do describe '#count_synced_repositories' do
it 'counts repositories that have been synced' do before do
create(:geo_project_registry, :sync_failed) project_1_in_synced_group = create(:project, group: synced_group)
project_2_in_synced_group = create(:project, group: synced_group)
project_3_in_synced_group = create(:project, group: synced_group)
project_4_broken_storage = create(:project, :broken_storage)
create(:geo_project_registry, :synced, project: project_synced) create(:geo_project_registry, :synced, project: project_synced)
create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty) create(:geo_project_registry, :synced, :repository_dirty, project: project_1_in_synced_group)
create(:geo_project_registry, :synced, :wiki_dirty, project: project_wiki_dirty) create(:geo_project_registry, :synced, :wiki_dirty, project: project_2_in_synced_group)
create(:geo_project_registry, :sync_failed, project: project_3_in_synced_group)
create(:geo_project_registry, :synced, :wiki_dirty, project: project_4_broken_storage)
end
expect(subject.count_synced_repositories).to eq 2 it 'counts registries that repository have been synced' do
expect(subject.count_synced_repositories).to eq 3
end end
context 'with selective sync' do context 'with selective sync by namespace' do
before do it 'counts registries that repository have been synced where projects belongs to the namespaces' do
secondary.update!(selective_sync_type: 'namespaces', namespaces: [synced_group]) secondary.update!(selective_sync_type: 'namespaces', namespaces: [synced_group])
end
it 'counts projects that has been synced' do expect(subject.count_synced_repositories).to eq 1
project_1_in_synced_group = create(:project, group: synced_group) end
project_2_in_synced_group = create(:project, group: synced_group) end
create(:geo_project_registry, :synced, project: project_synced) context 'with selective sync by shard' do
create(:geo_project_registry, :synced, project: project_1_in_synced_group) it 'counts registries that repository have been synced where projects belongs to the shards' do
create(:geo_project_registry, :sync_failed, project: project_2_in_synced_group) secondary.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken'])
expect(subject.count_synced_repositories).to eq 1 expect(subject.count_synced_repositories).to eq 1
end end
...@@ -56,35 +63,34 @@ describe Geo::ProjectRegistryFinder, :geo do ...@@ -56,35 +63,34 @@ describe Geo::ProjectRegistryFinder, :geo do
end end
describe '#count_synced_wikis' do describe '#count_synced_wikis' do
it 'counts wiki that have been synced' do before do
create(:geo_project_registry, :sync_failed) project_1_in_synced_group = create(:project, group: synced_group)
create(:geo_project_registry, :synced, project: project_synced) project_2_in_synced_group = create(:project, group: synced_group)
create(:geo_project_registry, :synced, :repository_dirty, project: project_repository_dirty) project_3_in_synced_group = create(:project, group: synced_group)
create(:geo_project_registry, :synced, :wiki_dirty, project: project_wiki_dirty) project_4_broken_storage = create(:project, :broken_storage)
expect(subject.count_synced_wikis).to eq 2
end
it 'counts synced wikis with nil wiki_access_level (which means enabled wiki)' do
project_synced.project_feature.update!(wiki_access_level: nil)
create(:geo_project_registry, :synced, project: project_synced) create(:geo_project_registry, :synced, project: project_synced)
create(:geo_project_registry, :synced, :wiki_dirty, project: project_1_in_synced_group)
create(:geo_project_registry, :synced, :repository_dirty, project: project_2_in_synced_group)
create(:geo_project_registry, :sync_failed, project: project_3_in_synced_group)
create(:geo_project_registry, :synced, :repository_dirty, project: project_4_broken_storage)
end
expect(subject.count_synced_wikis).to eq 1 it 'counts registries that wiki have been synced' do
expect(subject.count_synced_wikis).to eq 3
end end
context 'with selective sync' do context 'with selective sync by namespace' do
before do it 'counts registries that wiki have been synced where projects belongs to the namespaces' do
secondary.update!(selective_sync_type: 'namespaces', namespaces: [synced_group]) secondary.update!(selective_sync_type: 'namespaces', namespaces: [synced_group])
end
it 'counts projects that has been synced' do expect(subject.count_synced_wikis).to eq 1
project_1_in_synced_group = create(:project, group: synced_group) end
project_2_in_synced_group = create(:project, group: synced_group) end
create(:geo_project_registry, :synced, project: project_synced) context 'with selective sync by shard' do
create(:geo_project_registry, :synced, project: project_1_in_synced_group) it 'counts registries that wiki have been synced where projects belongs to the shards' do
create(:geo_project_registry, :sync_failed, project: project_2_in_synced_group) secondary.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken'])
expect(subject.count_synced_wikis).to eq 1 expect(subject.count_synced_wikis).to eq 1
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