Commit fa6b31f2 authored by Michael Kozono's avatar Michael Kozono

Merge branch '223254-geo-fdw-geo-node' into 'master'

Geo - Remove Geo::Fdw::GeoNode model

See merge request gitlab-org/gitlab!38617
parents 07c8dc0a 6c3a1d23
......@@ -3,7 +3,7 @@
module Geo
class ContainerRepositoryRegistryFinder < RegistryFinder
def count_syncable
current_node_non_fdw.container_repositories.count
container_repositories.count
end
def count_synced
......@@ -19,7 +19,7 @@ module Geo
end
def find_registry_differences(range)
source_ids = current_node_non_fdw.container_repositories.id_in(range).pluck_primary_key
source_ids = container_repositories.id_in(range).pluck_primary_key
tracked_ids = Geo::ContainerRepositoryRegistry.pluck_model_ids_in_range(range)
untracked_ids = source_ids - tracked_ids
......@@ -63,5 +63,11 @@ module Geo
.limit(batch_size)
end
# rubocop:enable CodeReuse/ActiveRecord
private
def container_repositories
current_node.container_repositories
end
end
end
......@@ -66,7 +66,7 @@ module Geo
private
def designs
current_node_non_fdw.designs
current_node.designs
end
def registries
......
......@@ -151,7 +151,7 @@ module Geo
end
def local_storage_only?
!current_node(fdw: false)&.sync_object_storage
!current_node&.sync_object_storage
end
end
end
......@@ -3,13 +3,17 @@
module Geo
class LfsObjectRegistryFinder < FileRegistryFinder
def replicables
lfs_objects = current_node(fdw: false).lfs_objects
local_storage_only? ? lfs_objects.with_files_stored_locally : lfs_objects
end
def syncable
Geo::LfsObjectRegistry
end
private
def lfs_objects
current_node.lfs_objects
end
end
end
......@@ -14,18 +14,8 @@ module Geo
private
def current_node(fdw: true)
fdw ? current_node_fdw : current_node_non_fdw
end
def current_node_fdw
strong_memoize(:current_node_fdw) do
Geo::Fdw::GeoNode.find(current_node_id) if current_node_id
end
end
def current_node_non_fdw
strong_memoize(:current_node_non_fdw) do
def current_node
strong_memoize(:current_node) do
GeoNode.find(current_node_id) if current_node_id
end
end
......
......@@ -45,19 +45,21 @@ module Geo::SelectiveSync
private
def selected_namespaces_and_descendants
relation = selected_namespaces_and_descendants_cte.apply_to(namespaces_model.all)
read_only(relation)
relation = selected_namespaces_and_descendants_cte.apply_to(Namespace.all)
read_only_relation(relation)
end
def selected_namespaces_and_descendants_cte
namespaces_table = Namespace.arel_table
cte = Gitlab::SQL::RecursiveCTE.new(:base_and_descendants)
cte << geo_node_namespace_links
.select(geo_node_namespace_links_table[:namespace_id].as('id'))
.select(geo_node_namespace_links.arel_table[:namespace_id].as('id'))
.except(:order)
# Recursively get all the descendants of the base set.
cte << namespaces_model
cte << Namespace
.select(namespaces_table[:id])
.from([namespaces_table, cte.table])
.where(namespaces_table[:parent_id].eq(cte.table[:id]))
......@@ -67,8 +69,8 @@ module Geo::SelectiveSync
end
def selected_leaf_namespaces_and_ancestors
relation = selected_leaf_namespaces_and_ancestors_cte.apply_to(namespaces_model.all)
read_only(relation)
relation = selected_leaf_namespaces_and_ancestors_cte.apply_to(Namespace.all)
read_only_relation(relation)
end
# Returns a CTE selecting namespace IDs for selected shards
......@@ -78,14 +80,16 @@ module Geo::SelectiveSync
# namespace of every project in those shards. We must also sync every
# ancestor of those namespaces.
def selected_leaf_namespaces_and_ancestors_cte
namespaces_table = Namespace.arel_table
cte = Gitlab::SQL::RecursiveCTE.new(:base_and_ancestors)
cte << namespaces_model
cte << Namespace
.select(namespaces_table[:id], namespaces_table[:parent_id])
.where(id: projects.select(:namespace_id))
# Recursively get all the ancestors of the base set.
cte << namespaces_model
cte << Namespace
.select(namespaces_table[:id], namespaces_table[:parent_id])
.from([namespaces_table, cte.table])
.where(namespaces_table[:id].eq(cte.table[:parent_id]))
......@@ -94,36 +98,10 @@ module Geo::SelectiveSync
cte
end
def read_only(relation)
def read_only_relation(relation)
# relations using a CTE are not safe to use with update_all as it will
# throw away the CTE, hence we mark them as read-only.
relation.extend(Gitlab::Database::ReadOnlyRelation)
relation
end
# This concern doesn't define a geo_node_namespace_links relation. That's
# done in ::GeoNode or ::Geo::Fdw::GeoNode respectively. So when we use the
# same code from the two places, they act differently - the first doesn't
# use FDW, the second does.
def geo_node_namespace_links_table
geo_node_namespace_links.arel_table
end
# This concern doesn't define a namespaces relation. That's done in ::GeoNode
# or ::Geo::Fdw::GeoNode respectively. So when we use the same code from the
# two places, they act differently - the first doesn't use FDW, the second does.
def namespaces_model
namespaces.model
end
# This concern doesn't define a namespaces relation. That's done in ::GeoNode
# or ::Geo::Fdw::GeoNode respectively. So when we use the same code from the
# two places, they act differently - the first doesn't use FDW, the second does.
def namespaces_table
namespaces.arel_table
end
def projects_table
Project.arel_table
end
end
# frozen_string_literal: true
module Geo
module Fdw
class GeoNode < ::Geo::BaseFdw
self.primary_key = :id
self.inheritance_column = nil
self.table_name = Gitlab::Geo::Fdw.foreign_table_name('geo_nodes')
serialize :selective_sync_shards, Array # rubocop:disable Cop/ActiveRecordSerialize
has_many :geo_node_namespace_links, class_name: 'Geo::Fdw::GeoNodeNamespaceLink'
has_many :namespaces, class_name: 'Geo::Fdw::Namespace', through: :geo_node_namespace_links
end
end
end
# frozen_string_literal: true
module Geo
module Fdw
class GeoNodeNamespaceLink < ::Geo::BaseFdw
self.table_name = Gitlab::Geo::Fdw.foreign_table_name('geo_node_namespace_links')
belongs_to :geo_node, class_name: 'Geo::Fdw::GeoNode', inverse_of: :namespaces
belongs_to :namespace, class_name: 'Geo::Fdw::Namespace', inverse_of: :geo_nodes
end
end
end
# frozen_string_literal: true
module Geo
module Fdw
class Namespace < ::Geo::BaseFdw
include Routable
self.primary_key = :id
self.inheritance_column = nil
self.table_name = Gitlab::Geo::Fdw.foreign_table_name('namespaces')
has_many :geo_node_namespace_links, class_name: 'Geo::Fdw::GeoNodeNamespaceLink'
has_many :geo_nodes, class_name: 'Geo::Fdw::GeoNode', through: :geo_node_namespace_links
belongs_to :parent, class_name: "Namespace"
end
end
end
......@@ -3,8 +3,6 @@
require 'spec_helper'
RSpec.describe Geo::FileRegistryFinder, :geo do
include ::EE::GeoHelpers
context 'with abstract methods' do
%w[
replicables
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Geo::Fdw::GeoNodeNamespaceLink, :geo, type: :model do
context 'relationships' do
it { is_expected.to belong_to(:geo_node).class_name('Geo::Fdw::GeoNode').inverse_of(:namespaces) }
it { is_expected.to belong_to(:namespace).class_name('Geo::Fdw::Namespace').inverse_of(:geo_nodes) }
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Geo::Fdw::GeoNode, :geo, type: :model do
context 'relationships' do
it { is_expected.to have_many(:geo_node_namespace_links).class_name('Geo::Fdw::GeoNodeNamespaceLink') }
it { is_expected.to have_many(:namespaces).class_name('Geo::Fdw::Namespace').through(:geo_node_namespace_links) }
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Geo::Fdw::Namespace, :geo, type: :model do
context 'relationships' do
it { is_expected.to have_many(:geo_node_namespace_links).class_name('Geo::Fdw::GeoNodeNamespaceLink') }
it { is_expected.to have_many(:geo_nodes).class_name('Geo::Fdw::GeoNode').through(:geo_node_namespace_links) }
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