Add selective sync support for the FDW queries to find failed registries

These changes introduces a new finder to make it easier to
remove the legacy queries in the future.
parent de1a65cc
# frozen_string_literal: true
# Finder for retrieving project registries that verification have
# failed scoped to a type (repository or wiki) using cross-database
# joins for selective sync.
#
# Basic usage:
#
# Geo::LegacyProjectRegistryVerificationFailedFinder.new(current_node: Gitlab::Geo.current_node, :repository).execute
#
# Valid `type` values are:
#
# * `:repository`
# * `:wiki`
#
# Any other value will be ignored.
module Geo
class LegacyProjectRegistryVerificationFailedFinder < RegistryFinder
def initialize(current_node: nil, type:)
super(current_node: current_node)
@type = type.to_s.to_sym
end
def execute
if selective_sync?
failed_registries_for_selective_sync
else
failed_registries
end
end
private
attr_reader :type
def failed_registries
Geo::ProjectRegistry.verification_failed(type)
end
# rubocop: disable CodeReuse/ActiveRecord
def failed_registries_for_selective_sync
legacy_inner_join_registry_ids(
failed_registries,
current_node.projects.pluck(:id),
Geo::ProjectRegistry,
foreign_key: :project_id
)
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
...@@ -34,6 +34,18 @@ module Geo ...@@ -34,6 +34,18 @@ module Geo
registries_for_verified_projects(:wiki).count registries_for_verified_projects(:wiki).count
end end
def count_verification_failed_repositories
registries_for_verification_failed_projects(:repository).count
end
def count_verification_failed_wikis
registries_for_verification_failed_projects(:wiki).count
end
def find_verification_failed_project_registries(type = nil)
registries_for_verification_failed_projects(type)
end
def count_repositories_checksum_mismatch def count_repositories_checksum_mismatch
Geo::ProjectRegistry.repository_checksum_mismatch.count Geo::ProjectRegistry.repository_checksum_mismatch.count
end end
...@@ -50,22 +62,6 @@ module Geo ...@@ -50,22 +62,6 @@ module Geo
Geo::ProjectRegistry.wikis_retrying_verification.count Geo::ProjectRegistry.wikis_retrying_verification.count
end end
def count_verification_failed_repositories
find_verification_failed_project_registries('repository').count
end
def count_verification_failed_wikis
find_verification_failed_project_registries('wiki').count
end
def find_verification_failed_project_registries(type = nil)
if use_legacy_queries?
legacy_find_filtered_verification_failed_projects(type)
else
find_filtered_verification_failed_project_registries(type)
end
end
def find_checksum_mismatch_project_registries(type = nil) def find_checksum_mismatch_project_registries(type = nil)
if use_legacy_queries? if use_legacy_queries?
legacy_find_filtered_checksum_mismatch_projects(type) legacy_find_filtered_checksum_mismatch_projects(type)
...@@ -111,17 +107,6 @@ module Geo ...@@ -111,17 +107,6 @@ module Geo
protected protected
def find_filtered_verification_failed_project_registries(type = nil)
case type
when 'repository'
Geo::ProjectRegistry.verification_failed_repos
when 'wiki'
Geo::ProjectRegistry.verification_failed_wikis
else
Geo::ProjectRegistry.verification_failed
end
end
def find_filtered_checksum_mismatch_project_registries(type = nil) def find_filtered_checksum_mismatch_project_registries(type = nil)
case type case type
when 'repository' when 'repository'
...@@ -229,18 +214,6 @@ module Geo ...@@ -229,18 +214,6 @@ module Geo
::Gitlab::SQL::Glob.q(value) ::Gitlab::SQL::Glob.q(value)
end end
# @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)
legacy_inner_join_registry_ids(
find_filtered_verification_failed_project_registries(type),
current_node.projects.pluck(:id),
Geo::ProjectRegistry,
foreign_key: :project_id
)
end
# rubocop: enable CodeReuse/ActiveRecord
# @return [ActiveRecord::Relation<Geo::ProjectRegistry>] list of projects where there is a checksum_mismatch # @return [ActiveRecord::Relation<Geo::ProjectRegistry>] list of projects where there is a checksum_mismatch
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def legacy_find_filtered_checksum_mismatch_projects(type = nil) def legacy_find_filtered_checksum_mismatch_projects(type = nil)
...@@ -339,7 +312,6 @@ module Geo ...@@ -339,7 +312,6 @@ module Geo
end end
end end
def registries_for_synced_projects(type) def registries_for_synced_projects(type)
finder_klass_for_synced_registries finder_klass_for_synced_registries
.new(current_node: current_node, type: type) .new(current_node: current_node, type: type)
...@@ -373,5 +345,19 @@ module Geo ...@@ -373,5 +345,19 @@ module Geo
.new(current_node: current_node, type: type) .new(current_node: current_node, type: type)
.execute .execute
end end
def finder_klass_for_verification_failed_registries
if Gitlab::Geo::Fdw.enabled_for_selective_sync?
Geo::ProjectRegistryVerificationFailedFinder
else
Geo::LegacyProjectRegistryVerificationFailedFinder
end
end
def registries_for_verification_failed_projects(type)
finder_klass_for_verification_failed_registries
.new(current_node: current_node, type: type)
.execute
end
end end
end end
# frozen_string_literal: true
# Finder for retrieving project registries that have verification failed
# scoped to a type (repository or wiki) using FDW queries.
#
# Basic usage:
#
# Geo::ProjectRegistryVerificationFailedFinder.new(current_node: Gitlab::Geo.current_node, :repository).execute
#
# Valid `type` values are:
#
# * `:repository`
# * `:wiki`
#
# Any other value will be ignored.
module Geo
class ProjectRegistryVerificationFailedFinder
def initialize(current_node:, type:)
@current_node = Geo::Fdw::GeoNode.find(current_node.id)
@type = type.to_s.to_sym
end
def execute
current_node.project_registries.verification_failed(type)
end
private
attr_reader :current_node, :type
end
end
# frozen_string_literal: true # frozen_string_literal: true
# Finder for retrieving project registries that have been verified # Finder for retrieving project registries that have verification succeeded
# scoped to a type (repository or wiki) using FDW queries. # scoped to a type (repository or wiki) using FDW queries.
# #
# Basic usage: # Basic usage:
......
...@@ -46,13 +46,6 @@ class Geo::ProjectRegistry < Geo::BaseRegistry ...@@ -46,13 +46,6 @@ class Geo::ProjectRegistry < Geo::BaseRegistry
where(repository_sync_failed.or(wiki_sync_failed)) where(repository_sync_failed.or(wiki_sync_failed))
end end
def self.verification_failed
repository_verification_failed = arel_table[:last_repository_verification_failure].not_eq(nil)
wiki_verification_failed = arel_table[:last_wiki_verification_failure].not_eq(nil)
where(repository_verification_failed.or(wiki_verification_failed))
end
def self.checksum_mismatch def self.checksum_mismatch
repository_checksum_mismatch = arel_table[:repository_checksum_mismatch].eq(true) repository_checksum_mismatch = arel_table[:repository_checksum_mismatch].eq(true)
wiki_checksum_mismatch = arel_table[:wiki_checksum_mismatch].eq(true) wiki_checksum_mismatch = arel_table[:wiki_checksum_mismatch].eq(true)
...@@ -123,6 +116,17 @@ class Geo::ProjectRegistry < Geo::BaseRegistry ...@@ -123,6 +116,17 @@ class Geo::ProjectRegistry < Geo::BaseRegistry
end end
end end
def self.verification_failed(type)
case type
when :repository
verification_failed_repos
when :wiki
verification_failed_wikis
else
verification_failed_repos.or(verification_failed_wikis)
end
end
def self.flag_repositories_for_resync! def self.flag_repositories_for_resync!
update_all( update_all(
resync_repository: true, resync_repository: true,
......
...@@ -166,28 +166,6 @@ describe Geo::ProjectRegistryFinder, :geo do ...@@ -166,28 +166,6 @@ describe Geo::ProjectRegistryFinder, :geo do
end end
describe '#count_verification_failed_repositories' do describe '#count_verification_failed_repositories' do
it 'delegates to #find_verification_failed_project_registries' do
expect(subject).to receive(:find_verification_failed_project_registries).with('repository').and_call_original
subject.count_verification_failed_repositories
end
it 'delegates to #legacy_find_filtered_verification_failed_projects when use_legacy_queries is true' do
expect(subject).to receive(:use_legacy_queries?).and_return(true)
expect(subject).to receive(:legacy_find_filtered_verification_failed_projects).with('repository').and_call_original
subject.count_verification_failed_repositories
end
it 'delegates to #find_filtered_verification_failed_project_registries when use_legacy_queries is false' do
expect(subject).to receive(:use_legacy_queries?).and_return(false)
expect(subject).to receive(:find_filtered_verification_failed_project_registries).with('repository').and_call_original
subject.count_verification_failed_repositories
end
it 'counts projects that verification has failed' do it 'counts projects that verification has failed' do
create(:geo_project_registry, :repository_verified, project: project_repository_verified) create(:geo_project_registry, :repository_verified, project: project_repository_verified)
create(:geo_project_registry, :repository_verification_failed, project: project_repository_verification_failed) create(:geo_project_registry, :repository_verification_failed, project: project_repository_verification_failed)
...@@ -208,28 +186,6 @@ describe Geo::ProjectRegistryFinder, :geo do ...@@ -208,28 +186,6 @@ describe Geo::ProjectRegistryFinder, :geo do
end end
describe '#count_verification_failed_wikis' do describe '#count_verification_failed_wikis' do
it 'delegates to #find_verification_failed_project_registries' do
expect(subject).to receive(:find_verification_failed_project_registries).with('wiki').and_call_original
subject.count_verification_failed_wikis
end
it 'delegates to #legacy_find_filtered_verification_failed_projects when use_legacy_queries is true' do
expect(subject).to receive(:use_legacy_queries?).and_return(true)
expect(subject).to receive(:legacy_find_filtered_verification_failed_projects).with('wiki').and_call_original
subject.count_verification_failed_wikis
end
it 'delegates to #find_filtered_verification_failed_project_registries when use_legacy_queries is false' do
expect(subject).to receive(:use_legacy_queries?).and_return(false)
expect(subject).to receive(:find_filtered_verification_failed_project_registries).with('wiki').and_call_original
subject.count_verification_failed_wikis
end
it 'counts projects that verification has failed' do it 'counts projects that verification has failed' do
create(:geo_project_registry, :repository_verified, project: project_repository_verified) create(:geo_project_registry, :repository_verified, project: project_repository_verified)
create(:geo_project_registry, :repository_verification_failed, project: project_repository_verification_failed) create(:geo_project_registry, :repository_verification_failed, project: project_repository_verification_failed)
......
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