Commit 9f876395 authored by Stan Hu's avatar Stan Hu

Merge branch...

Merge branch '8798-geo-implement-selective-sync-support-for-fdw-queries-to-find-verified-registries' into 'master'

Geo - Add selective sync support for FDW queries to find verified registries

See merge request gitlab-org/gitlab-ee!10255
parents dd1d0ca9 80a2af18
# 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
......@@ -125,38 +111,6 @@ module Geo
protected
def finder_klass_for_synced_registries
if Gitlab::Geo::Fdw.enabled_for_selective_sync?
Geo::ProjectRegistrySyncedFinder
else
Geo::LegacyProjectRegistrySyncedFinder
end
end
def registries_for_synced_projects(type)
finder_klass_for_synced_registries
.new(current_node: current_node, type: type)
.execute
end
def finder_klass_for_failed_registries
if Gitlab::Geo::Fdw.enabled_for_selective_sync?
Geo::ProjectRegistrySyncFailedFinder
else
Geo::LegacyProjectRegistrySyncFailedFinder
end
end
def registries_for_failed_projects(type)
finder_klass_for_failed_registries
.new(current_node: current_node, type: type)
.execute
end
def find_verified_repositories
Geo::ProjectRegistry.verified_repos
end
def find_filtered_verification_failed_project_registries(type = nil)
case type
when 'repository'
......@@ -221,11 +175,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 +229,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)
......@@ -400,5 +328,49 @@ module Geo
def wiki_missing_on_primary_is_not_true
Arel::Nodes::SqlLiteral.new("project_registry.wiki_missing_on_primary IS NOT TRUE")
end
private
def finder_klass_for_synced_registries
if Gitlab::Geo::Fdw.enabled_for_selective_sync?
Geo::ProjectRegistrySyncedFinder
else
Geo::LegacyProjectRegistrySyncedFinder
end
end
def registries_for_synced_projects(type)
finder_klass_for_synced_registries
.new(current_node: current_node, type: type)
.execute
end
def finder_klass_for_failed_registries
if Gitlab::Geo::Fdw.enabled_for_selective_sync?
Geo::ProjectRegistrySyncFailedFinder
else
Geo::LegacyProjectRegistrySyncFailedFinder
end
end
def registries_for_failed_projects(type)
finder_klass_for_failed_registries
.new(current_node: current_node, type: type)
.execute
end
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
end
end
# 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,
......
---
title: Geo - Add selective sync support for FDW queries to find verified registries
merge_request: 10255
author:
type: changed
# frozen_string_literal: true
require 'spec_helper'
describe Geo::LegacyProjectRegistryVerifiedFinder, :geo do
include EE::GeoHelpers
describe '#execute' do
let(:node) { create(:geo_node) }
let(:group_1) { create(:group) }
let(:group_2) { create(:group) }
let(:nested_group_1) { create(:group, parent: group_1) }
let(:project_1) { create(:project, group: group_1) }
let(:project_2) { create(:project, group: nested_group_1) }
let(:project_3) { create(:project, group: nested_group_1) }
let(:project_4) { create(:project, :broken_storage, group: group_2) }
let(:project_5) { create(:project, :broken_storage, group: group_2) }
let!(:registry_verified) { create(:geo_project_registry, :repository_verified, :wiki_verified, project: project_1) }
let!(:registry_repository_verification_failed) { create(:geo_project_registry, :repository_verification_failed, :wiki_verified, project: project_2) }
let!(:registry_repository_verification_failed_broken_shard) { create(:geo_project_registry, :repository_verification_failed, :wiki_verified, project: project_5) }
let!(:registry_wiki_verification_failed) { create(:geo_project_registry, :repository_verified, :wiki_verification_failed, project: project_3) }
let!(:registry_wiki_verification_failed_broken_shard) { create(:geo_project_registry, :repository_verified, :wiki_verification_failed, project: project_4) }
let!(:registry_verification_failed) { create(:geo_project_registry, :repository_verification_failed, :wiki_verification_failed) }
shared_examples 'finds verified registries' do
context 'with repository type' do
subject { described_class.new(current_node: node, type: :repository) }
context 'without selective sync' do
it 'returns all verified registries' do
expect(subject.execute).to contain_exactly(registry_verified, registry_wiki_verification_failed, registry_wiki_verification_failed_broken_shard)
end
end
context 'with selective sync by namespace' do
it 'returns verified registries where projects belongs to the namespaces' do
node.update!(selective_sync_type: 'namespaces', namespaces: [group_1, nested_group_1])
expect(subject.execute).to contain_exactly(registry_verified, registry_wiki_verification_failed)
end
end
context 'with selective sync by shard' do
it 'returns verified registries where projects belongs to the shards' do
node.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken'])
expect(subject.execute).to contain_exactly(registry_wiki_verification_failed_broken_shard)
end
end
end
context 'with wiki type' do
subject { described_class.new(current_node: node, type: :wiki) }
context 'without selective sync' do
it 'returns all verified registries' do
expect(subject.execute).to contain_exactly(registry_verified, registry_repository_verification_failed, registry_repository_verification_failed_broken_shard)
end
end
context 'with selective sync by namespace' do
it 'returns verified registries where projects belongs to the namespaces' do
node.update!(selective_sync_type: 'namespaces', namespaces: [group_1, nested_group_1])
expect(subject.execute).to contain_exactly(registry_verified, registry_repository_verification_failed)
end
end
context 'with selective sync by shard' do
it 'returns verified registries where projects belongs to the shards' do
node.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken'])
expect(subject.execute).to contain_exactly(registry_repository_verification_failed_broken_shard)
end
end
end
context 'with invalid type' do
subject { described_class.new(current_node: node, type: :invalid) }
it 'returns nothing' do
expect(subject.execute).to be_empty
end
end
end
# Disable transactions via :delete method because a foreign table
# can't see changes inside a transaction of a different connection.
context 'FDW', :delete do
before do
skip('FDW is not configured') unless Gitlab::Geo::Fdw.enabled?
end
include_examples 'finds verified registries'
end
context 'Legacy' do
before do
stub_fdw_disabled
end
include_examples 'finds verified registries'
end
end
end
......@@ -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))
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::ProjectRegistryVerifiedFinder, :geo do
# Disable transactions via :delete method because a foreign table
# can't see changes inside a transaction of a different connection.
describe '#execute', :delete do
let(:node) { create(:geo_node) }
let(:group_1) { create(:group) }
let(:group_2) { create(:group) }
let(:nested_group_1) { create(:group, parent: group_1) }
let(:project_1) { create(:project, group: group_1) }
let(:project_2) { create(:project, group: nested_group_1) }
let(:project_3) { create(:project, group: nested_group_1) }
let(:project_4) { create(:project, :broken_storage, group: group_2) }
let(:project_5) { create(:project, :broken_storage, group: group_2) }
let!(:registry_verified) { create(:geo_project_registry, :repository_verified, :wiki_verified, project: project_1) }
let!(:registry_repository_verification_failed) { create(:geo_project_registry, :repository_verification_failed, :wiki_verified, project: project_2) }
let!(:registry_repository_verification_failed_broken_shard) { create(:geo_project_registry, :repository_verification_failed, :wiki_verified, project: project_5) }
let!(:registry_wiki_verification_failed) { create(:geo_project_registry, :repository_verified, :wiki_verification_failed, project: project_3) }
let!(:registry_wiki_verification_failed_broken_shard) { create(:geo_project_registry, :repository_verified, :wiki_verification_failed, project: project_4) }
let!(:registry_verification_failed) { create(:geo_project_registry, :repository_verification_failed, :wiki_verification_failed) }
before do
skip('FDW is not configured') unless Gitlab::Geo::Fdw.enabled?
end
context 'with repository type' do
subject { described_class.new(current_node: node, type: :repository) }
context 'without selective sync' do
it 'returns all verified registries' do
expect(subject.execute).to contain_exactly(registry_verified, registry_wiki_verification_failed, registry_wiki_verification_failed_broken_shard)
end
end
context 'with selective sync by namespace' do
it 'returns verified registries where projects belongs to the namespaces' do
node.update!(selective_sync_type: 'namespaces', namespaces: [group_1, nested_group_1])
expect(subject.execute).to contain_exactly(registry_verified, registry_wiki_verification_failed)
end
end
context 'with selective sync by shard' do
it 'returns verified registries where projects belongs to the shards' do
node.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken'])
expect(subject.execute).to contain_exactly(registry_wiki_verification_failed_broken_shard)
end
end
end
context 'with wiki type' do
subject { described_class.new(current_node: node, type: :wiki) }
context 'without selective sync' do
it 'returns all verified registries' do
expect(subject.execute).to contain_exactly(registry_verified, registry_repository_verification_failed, registry_repository_verification_failed_broken_shard)
end
end
context 'with selective sync by namespace' do
it 'returns verified registries where projects belongs to the namespaces' do
node.update!(selective_sync_type: 'namespaces', namespaces: [group_1, nested_group_1])
expect(subject.execute).to contain_exactly(registry_verified, registry_repository_verification_failed)
end
end
context 'with selective sync by shard' do
it 'returns verified registries where projects belongs to the shards' do
node.update!(selective_sync_type: 'shards', selective_sync_shards: ['broken'])
expect(subject.execute).to contain_exactly(registry_repository_verification_failed_broken_shard)
end
end
end
context 'with invalid type' do
subject { described_class.new(current_node: node, type: :invalid) }
it 'returns nothing' do
expect(subject.execute).to be_empty
end
end
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