Refactor legacy finder to find registries pending verification

With this change we can dry up finders avoiding  code duplication.
parent d505b39e
......@@ -29,78 +29,22 @@ module Geo
attr_reader :batch_size, :shard_name
def local_registry_table
Geo::ProjectRegistry.arel_table
end
def fdw_project_table
Geo::Fdw::Project.arel_table
end
def fdw_repository_state_table
Geo::Fdw::ProjectRepositoryState.arel_table
end
def fdw_inner_join_projects
local_registry_table
.join(fdw_project_table, Arel::Nodes::InnerJoin)
.on(local_registry_table[:project_id].eq(fdw_project_table[:id]))
.join_sources
end
def fdw_inner_join_repository_state
local_registry_table
.join(fdw_repository_state_table, Arel::Nodes::InnerJoin)
.on(local_registry_table[:project_id].eq(fdw_repository_state_table[:project_id]))
.join_sources
end
def local_repo_condition
local_registry_table[:repository_verification_checksum_sha].eq(nil)
.and(local_registry_table[:last_repository_verification_failure].eq(nil))
.and(local_registry_table[:resync_repository].eq(false))
.and(repository_missing_on_primary_is_not_true)
end
def repository_missing_on_primary_is_not_true
Arel::Nodes::SqlLiteral.new("project_registry.repository_missing_on_primary IS NOT TRUE")
end
def local_wiki_condition
local_registry_table[:wiki_verification_checksum_sha].eq(nil)
.and(local_registry_table[:last_wiki_verification_failure].eq(nil))
.and(local_registry_table[:resync_wiki].eq(false))
.and(wiki_missing_on_primary_is_not_true)
end
def wiki_missing_on_primary_is_not_true
Arel::Nodes::SqlLiteral.new("project_registry.wiki_missing_on_primary IS NOT TRUE")
end
# rubocop:disable CodeReuse/ActiveRecord
def registries_pending_verification
repo_condition =
local_repo_condition
.and(fdw_repository_state_table[:repository_verification_checksum].not_eq(nil))
wiki_condition =
local_wiki_condition
.and(fdw_repository_state_table[:wiki_verification_checksum].not_eq(nil))
Geo::ProjectRegistry
.joins(fdw_inner_join_projects)
.joins(fdw_inner_join_repository_state)
.where(repo_condition.or(wiki_condition))
.where(fdw_project_table[:repository_storage].eq(shard_name))
.joins(Geo::Fdw::GeoNode.fdw_inner_join_projects)
.joins(Geo::Fdw::GeoNode.fdw_inner_join_repository_state)
.where(Geo::Fdw::GeoNode.fdw_registries_pending_verification)
.where(Geo::Fdw::Project.within_shard(shard_name))
.limit(batch_size)
end
# rubocop:enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
# rubocop:disable CodeReuse/ActiveRecord
def registries_pending_verification_for_selective_sync
registries = Geo::ProjectRegistry
.where(local_repo_condition.or(local_wiki_condition))
.pluck(:project_id, local_repo_condition.to_sql, local_wiki_condition.to_sql)
.where(Geo::ProjectRegistry.registries_pending_verification)
.pluck(:project_id, Geo::ProjectRegistry.repositories_pending_verification.to_sql, Geo::ProjectRegistry.wikis_pending_verification.to_sql)
return Geo::ProjectRegistry.none if registries.empty?
......@@ -108,6 +52,7 @@ module Geo
"(#{project_id}, #{quote_value(want_to_sync_repo)}, #{quote_value(want_to_sync_wiki)})"
end
legacy_repository_state_table = ::ProjectRepositoryState.arel_table
project_registry_sync_table = Arel::Table.new(:project_registry_sync_table)
joined_relation =
......@@ -136,10 +81,6 @@ module Geo
foreign_key: :project_id
)
end
# rubocop: enable CodeReuse/ActiveRecord
def legacy_repository_state_table
::ProjectRepositoryState.arel_table
end
# rubocop:enable CodeReuse/ActiveRecord
end
end
......@@ -14,6 +14,44 @@ module Geo
has_many :geo_node_namespace_links, class_name: 'Geo::Fdw::GeoNodeNamespaceLink'
has_many :namespaces, class_name: 'Geo::Fdw::Namespace', through: :geo_node_namespace_links
def self.fdw_inner_join_projects
project_registries_table = Geo::ProjectRegistry.arel_table
fdw_projects_table = Geo::Fdw::Project.arel_table
project_registries_table
.join(fdw_projects_table, Arel::Nodes::InnerJoin)
.on(project_registries_table[:project_id].eq(fdw_projects_table[:id]))
.join_sources
end
def self.fdw_inner_join_repository_state
project_registries_table = Geo::ProjectRegistry.arel_table
fdw_repository_state_table = Geo::Fdw::ProjectRepositoryState.arel_table
project_registries_table
.join(fdw_repository_state_table, Arel::Nodes::InnerJoin)
.on(project_registries_table[:project_id].eq(fdw_repository_state_table[:project_id]))
.join_sources
end
def self.fdw_registries_pending_verification
fdw_repositories_pending_verification.or(fdw_wikis_pending_verification)
end
def self.fdw_repositories_pending_verification
fdw_repository_state_table = Geo::Fdw::ProjectRepositoryState.arel_table
Geo::ProjectRegistry.repositories_pending_verification
.and(fdw_repository_state_table[:repository_verification_checksum].not_eq(nil))
end
def self.fdw_wikis_pending_verification
fdw_repository_state_table = Geo::Fdw::ProjectRepositoryState.arel_table
Geo::ProjectRegistry.wikis_pending_verification
.and(fdw_repository_state_table[:wiki_verification_checksum].not_eq(nil))
end
def project_registries
return Geo::ProjectRegistry.all unless selective_sync?
......@@ -32,7 +70,7 @@ module Geo
query = selected_namespaces_and_descendants
Geo::ProjectRegistry
.joins(fdw_inner_join_projects)
.joins(self.class.fdw_inner_join_projects)
.where(fdw_projects_table.name => { namespace_id: query.select(:id) })
end
......@@ -61,14 +99,10 @@ module Geo
def registries_for_selected_shards
Geo::ProjectRegistry
.joins(fdw_inner_join_projects)
.joins(self.class.fdw_inner_join_projects)
.where(fdw_projects_table.name => { repository_storage: selective_sync_shards })
end
def project_registries_table
Geo::ProjectRegistry.arel_table
end
def fdw_projects_table
Geo::Fdw::Project.arel_table
end
......@@ -80,13 +114,6 @@ module Geo
def fdw_geo_node_namespace_links_table
Geo::Fdw::GeoNodeNamespaceLink.arel_table
end
def fdw_inner_join_projects
project_registries_table
.join(fdw_projects_table, Arel::Nodes::InnerJoin)
.on(project_registries_table[:project_id].eq(fdw_projects_table[:id]))
.join_sources
end
end
end
end
......@@ -7,6 +7,8 @@ module Geo
self.table_name = Gitlab::Geo::Fdw.foreign_table_name('projects')
scope :within_shard, -> (shard_name) { arel_table[:repository_storage].eq(shard_name) }
class << self
# Searches for a list of projects based on the query given in `query`.
#
......
......@@ -141,6 +141,30 @@ class Geo::ProjectRegistry < Geo::BaseRegistry
end
end
def self.registries_pending_verification
repositories_pending_verification.or(wikis_pending_verification)
end
def self.repositories_pending_verification
repository_exists_on_primary =
Arel::Nodes::SqlLiteral.new("project_registry.repository_missing_on_primary IS NOT TRUE")
arel_table[:repository_verification_checksum_sha].eq(nil)
.and(arel_table[:last_repository_verification_failure].eq(nil))
.and(arel_table[:resync_repository].eq(false))
.and(repository_exists_on_primary)
end
def self.wikis_pending_verification
wiki_exists_on_primary =
Arel::Nodes::SqlLiteral.new("project_registry.wiki_missing_on_primary IS NOT TRUE")
arel_table[:wiki_verification_checksum_sha].eq(nil)
.and(arel_table[:last_wiki_verification_failure].eq(nil))
.and(arel_table[:resync_wiki].eq(false))
.and(wiki_exists_on_primary)
end
def self.flag_repositories_for_resync!
update_all(
resync_repository: true,
......
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