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

Use separate FDW classes to wrap ActiveRecord models

parent 1b0af3c3
class Geo::BaseFdw < ActiveRecord::Base
self.abstract_class = true
if Gitlab::Geo.geo_database_configured?
establish_connection Rails.configuration.geo_database
end
def self.connection
raise 'Geo secondary database is not configured' unless Gitlab::Geo.geo_database_configured?
super
end
end
module Geo
module Fdw
class LfsObject < ::Geo::BaseFdw
self.table_name = Gitlab::Geo.fdw_table('lfs_objects')
end
end
end
module Geo
module Fdw
class Upload < ::Geo::BaseFdw
self.table_name = Gitlab::Geo.fdw_table('uploads')
end
end
end
class LfsObject < ActiveRecord::Base class LfsObject < ActiveRecord::Base
# EE specific modules
prepend EE::LfsObject
has_many :lfs_objects_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :lfs_objects_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :projects, through: :lfs_objects_projects has_many :projects, through: :lfs_objects_projects
......
class Upload < ActiveRecord::Base class Upload < ActiveRecord::Base
# EE specific modules
prepend EE::Upload
# Upper limit for foreground checksum processing # Upper limit for foreground checksum processing
CHECKSUM_THRESHOLD = 100.megabytes CHECKSUM_THRESHOLD = 100.megabytes
......
...@@ -54,10 +54,10 @@ module Geo ...@@ -54,10 +54,10 @@ module Geo
end end
def fdw_find_lfs_object_ids def fdw_find_lfs_object_ids
fdw_table = "#{Gitlab::Geo.fdw_schema}.lfs_objects" fdw_table = Geo::Fdw::LfsObject.table_name
# Filter out objects in object storage (this is done in GeoNode#lfs_objects) # Filter out objects in object storage (this is done in GeoNode#lfs_objects)
LfsObject.fdw.joins("LEFT OUTER JOIN file_registry ON file_registry.file_id = #{fdw_table}.id AND file_registry.file_type = 'lfs'") Geo::Fdw::LfsObject.joins("LEFT OUTER JOIN file_registry ON file_registry.file_id = #{fdw_table}.id AND file_registry.file_type = 'lfs'")
.where("#{fdw_table}.file_store IS NULL OR #{fdw_table}.file_store = #{LfsObjectUploader::LOCAL_STORE}") .where("#{fdw_table}.file_store IS NULL OR #{fdw_table}.file_store = #{LfsObjectUploader::LOCAL_STORE}")
.where('file_registry.file_id IS NULL') .where('file_registry.file_id IS NULL')
.order(created_at: :desc) .order(created_at: :desc)
...@@ -67,10 +67,10 @@ module Geo ...@@ -67,10 +67,10 @@ module Geo
end end
def fdw_find_upload_object_ids def fdw_find_upload_object_ids
fdw_table = "#{Gitlab::Geo.fdw_schema}.uploads" fdw_table = Geo::Fdw::Upload.table_name
obj_types = Geo::FileService::DEFAULT_OBJECT_TYPES.map { |val| "'#{val}'" }.join(',') obj_types = Geo::FileService::DEFAULT_OBJECT_TYPES.map { |val| "'#{val}'" }.join(',')
Upload.fdw.joins("LEFT OUTER JOIN file_registry ON file_registry.file_id = #{fdw_table}.id AND file_registry.file_type IN (#{obj_types})") Geo::Fdw::Upload.joins("LEFT OUTER JOIN file_registry ON file_registry.file_id = #{fdw_table}.id AND file_registry.file_type IN (#{obj_types})")
.where('file_registry.file_id IS NULL') .where('file_registry.file_id IS NULL')
.order(created_at: :desc) .order(created_at: :desc)
.limit(db_retrieve_batch_size) .limit(db_retrieve_batch_size)
......
module EE
module Geo
module ForeignDataWrapped
extend ActiveSupport::Concern
class_methods do
def fdw
instance = self.clone
instance.table_name = "#{::Gitlab::Geo.fdw_schema}.#{self.table_name}"
def instance.name
table_name
end
instance.establish_connection Rails.configuration.geo_database
instance
end
end
end
end
end
module EE
# LfsObject EE mixin
#
# This module is intended to encapsulate EE-specific model logic
# and be prepended in the `LfsObject` model
module LfsObject
extend ActiveSupport::Concern
prepended do
prepend ::EE::Geo::ForeignDataWrapped
end
end
end
...@@ -10,7 +10,6 @@ module EE ...@@ -10,7 +10,6 @@ module EE
include Elastic::ProjectsSearch include Elastic::ProjectsSearch
prepend GeoAwareAvatar prepend GeoAwareAvatar
prepend ImportStatusStateMachine prepend ImportStatusStateMachine
prepend ::EE::Geo::ForeignDataWrapped
before_validation :mark_remote_mirrors_for_removal before_validation :mark_remote_mirrors_for_removal
......
module EE
# Upload EE mixin
#
# This module is intended to encapsulate EE-specific model logic
# and be prepended in the `Upload` model
module Upload
extend ActiveSupport::Concern
prepended do
prepend ::EE::Geo::ForeignDataWrapped
end
end
end
...@@ -14,6 +14,8 @@ module Gitlab ...@@ -14,6 +14,8 @@ module Gitlab
SECONDARY_JOBS = %i(repository_sync_job file_download_job).freeze SECONDARY_JOBS = %i(repository_sync_job file_download_job).freeze
FDW_SCHEMA = 'gitlab_secondary'.freeze
def self.current_node def self.current_node
self.cache_value(:geo_node_current) do self.cache_value(:geo_node_current) do
GeoNode.find_by(host: Gitlab.config.gitlab.host, GeoNode.find_by(host: Gitlab.config.gitlab.host,
...@@ -77,13 +79,13 @@ module Gitlab ...@@ -77,13 +79,13 @@ module Gitlab
def self.fdw? def self.fdw?
self.cache_value(:geo_fdw?) do self.cache_value(:geo_fdw?) do
::Geo::BaseRegistry.connection.execute( ::Geo::BaseRegistry.connection.execute(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '#{self.fdw_schema}' AND table_type = 'FOREIGN TABLE'" "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '#{FDW_SCHEMA}' AND table_type = 'FOREIGN TABLE'"
).first.fetch('count').to_i.positive? ).first.fetch('count').to_i.positive?
end end
end end
def self.fdw_schema def self.fdw_table(table_name)
'gitlab_secondary'.freeze FDW_SCHEMA + ".#{table_name}"
end end
def self.repository_sync_job def self.repository_sync_job
......
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