Add selective sync support for FDW query to count verified registries

These changes introduces a new finder to make it easier to
remove the legacy queries in the future.
parent b282fe8d
# frozen_string_literal: true
# Finder for retrieving project registries that have been verified
# scoped to a type (repository or wiki) using cross-database joins
# for selective sync.
#
# Basic usage:
#
# Geo::LegacyProjectRegistryVerifiedFinder.new(current_node: Gitlab::Geo.current_node, :repository).execute
#
# Valid `type` values are:
#
# * `:repository`
# * `:wiki`
#
# Any other value will be ignored.
module Geo
class LegacyProjectRegistryVerifiedFinder < RegistryFinder
def initialize(current_node:, type:)
super(current_node: current_node)
@type = type.to_s.to_sym
end
def execute
if selective_sync?
verified_registries_for_selective_sync
else
verified_registries
end
end
private
attr_reader :type
def verified_registries
Geo::ProjectRegistry.verified(type)
end
# rubocop: disable CodeReuse/ActiveRecord
def verified_registries_for_selective_sync
legacy_inner_join_registry_ids(
verified_registries,
current_node.projects.pluck(:id),
Geo::ProjectRegistry,
foreign_key: :project_id
)
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
......@@ -27,25 +27,11 @@ module Geo
end
def count_verified_repositories
relation =
if use_legacy_queries?
legacy_find_verified_repositories
else
find_verified_repositories
end
relation.count
registries_for_verified_projects(:repository).count
end
def count_verified_wikis
relation =
if use_legacy_queries?
legacy_find_verified_wikis
else
fdw_find_verified_wikis
end
relation.count
registries_for_verified_projects(:wiki).count
end
def count_repositories_checksum_mismatch
......@@ -153,8 +139,18 @@ module Geo
.execute
end
def find_verified_repositories
Geo::ProjectRegistry.verified_repos
def finder_klass_for_verified_registries
if Gitlab::Geo::Fdw.enabled_for_selective_sync?
Geo::ProjectRegistryVerifiedFinder
else
Geo::LegacyProjectRegistryVerifiedFinder
end
end
def registries_for_verified_projects(type)
finder_klass_for_verified_registries
.new(current_node: current_node, type: type)
.execute
end
def find_filtered_verification_failed_project_registries(type = nil)
......@@ -221,11 +217,6 @@ module Geo
end
# rubocop: enable CodeReuse/ActiveRecord
# @return [ActiveRecord::Relation<Geo::ProjectRegistry>]
def fdw_find_verified_wikis
Geo::ProjectRegistry.verified_wikis
end
def fdw_inner_join_projects
local_registry_table
.join(fdw_project_table, Arel::Nodes::InnerJoin)
......@@ -280,27 +271,6 @@ module Geo
::Gitlab::SQL::Glob.q(value)
end
# @return [ActiveRecord::Relation<Geo::ProjectRegistry>] list of verified projects
def legacy_find_verified_repositories
legacy_find_project_registries(Geo::ProjectRegistry.verified_repos)
end
# @return [ActiveRecord::Relation<Geo::ProjectRegistry>] list of verified wikis
def legacy_find_verified_wikis
legacy_find_project_registries(Geo::ProjectRegistry.verified_wikis)
end
# @return [ActiveRecord::Relation<Project>] list of synced projects
# rubocop: disable CodeReuse/ActiveRecord
def legacy_find_project_registries(project_registries)
legacy_inner_join_registry_ids(
current_node.projects,
project_registries.pluck(:project_id),
Project
)
end
# rubocop: enable CodeReuse/ActiveRecord
# @return [ActiveRecord::Relation<Geo::ProjectRegistry>] list of projects that verification has failed
# rubocop: disable CodeReuse/ActiveRecord
def legacy_find_filtered_verification_failed_projects(type = nil)
......
# frozen_string_literal: true
# Finder for retrieving project registries that have been verified
# scoped to a type (repository or wiki) using FDW queries.
#
# Basic usage:
#
# Geo::ProjectRegistryVerifiedFinder.new(current_node: Gitlab::Geo.current_node, :repository).execute
#
# Valid `type` values are:
#
# * `:repository`
# * `:wiki`
#
# Any other value will be ignored.
module Geo
class ProjectRegistryVerifiedFinder
def initialize(current_node:, type:)
@current_node = Geo::Fdw::GeoNode.find(current_node.id)
@type = type.to_sym
end
def execute
current_node.project_registries.verified(type)
end
private
attr_reader :current_node, :type
end
end
......@@ -112,6 +112,17 @@ class Geo::ProjectRegistry < Geo::BaseRegistry
end
end
def self.verified(type)
case type
when :repository
verified_repos
when :wiki
verified_wikis
else
none
end
end
def self.flag_repositories_for_resync!
update_all(
resync_repository: true,
......
......@@ -146,22 +146,6 @@ describe Geo::ProjectRegistryFinder, :geo do
end
describe '#count_verified_repositories' do
it 'delegates to #find_verified_repositories when use_legacy_queries is false' do
expect(subject).to receive(:use_legacy_queries?).and_return(false)
expect(subject).to receive(:find_verified_repositories).and_call_original
subject.count_verified_repositories
end
it 'delegates to #legacy_find_verified_repositories when use_legacy_queries is true' do
expect(subject).to receive(:use_legacy_queries?).and_return(true)
expect(subject).to receive(:legacy_find_verified_repositories).and_call_original
subject.count_verified_repositories
end
it 'counts projects that verified' do
create(:geo_project_registry, :repository_verified, project: project_repository_verified)
create(:geo_project_registry, :repository_verified, project: build(:project))
......@@ -172,12 +156,6 @@ describe Geo::ProjectRegistryFinder, :geo do
end
describe '#count_verified_wikis' do
it 'delegates to the correct method' do
expect(subject).to receive("#{method_prefix}_find_verified_wikis".to_sym).and_call_original
subject.count_verified_wikis
end
it 'counts wikis that verified' do
create(:geo_project_registry, :wiki_verified, project: project_wiki_verified)
create(:geo_project_registry, :wiki_verified, project: build(:project))
......
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