Add Geo::LfsObjectRegistryFinder

parent 295e3bf8
......@@ -2,6 +2,8 @@ module Geo
module Fdw
class LfsObject < ::Geo::BaseFdw
self.table_name = Gitlab::Geo.fdw_table('lfs_objects')
scope :with_files_stored_locally, ->() { where(file_store: [nil, LfsObjectUploader::LOCAL_STORE]) }
end
end
end
......@@ -183,24 +183,6 @@ class GeoNode < ActiveRecord::Base
end
end
def lfs_objects_synced_count
return unless secondary?
relation = Geo::FileRegistry.lfs_objects.synced
if selective_sync?
relation = relation.where(file_id: lfs_objects.pluck(:id))
end
relation.count
end
def lfs_objects_failed_count
return unless secondary?
Geo::FileRegistry.lfs_objects.failed.count
end
def find_or_build_status
status || build_status
end
......
......@@ -74,8 +74,10 @@ class GeoNodeStatus < ActiveRecord::Base
self.cursor_last_event_date = Geo::EventLog.find_by(id: self.cursor_last_event_id)&.created_at
self.repositories_synced_count = geo_node.project_registries.synced.count
self.repositories_failed_count = geo_node.project_registries.failed.count
self.lfs_objects_synced_count = geo_node.lfs_objects_synced_count
self.lfs_objects_failed_count = geo_node.lfs_objects_failed_count
lfs_objects_finder = Geo::LfsObjectRegistryFinder.new(current_node: geo_node)
self.lfs_objects_synced_count = lfs_objects_finder.find_synced_lfs_objects.count
self.lfs_objects_failed_count = lfs_objects_finder.find_failed_lfs_objects.count
attachments_finder = Geo::AttachmentRegistryFinder.new(current_node: geo_node)
self.attachments_synced_count = attachments_finder.find_synced_attachments.count
......
module Geo
class LfsObjectRegistryFinder < RegistryFinder
def find_synced_lfs_objects
relation =
if fdw?
fdw_find_synced_lfs_objects
else
legacy_find_synced_lfs_objects
end
relation
end
def find_failed_lfs_objects
relation =
if fdw?
fdw_find_failed_lfs_objects
else
legacy_find_failed_lfs_objects
end
relation
end
private
def lfs_objects
lfs_object_model = fdw? ? Geo::Fdw::LfsObject : LfsObject
relation =
if selective_sync?
lfs_object_model.joins(:projects).where(projects: { id: current_node.projects })
else
lfs_object_model.all
end
relation.with_files_stored_locally
end
#
# FDW accessors
#
def fdw_table
Geo::Fdw::LfsObject.table_name
end
def fdw_find_synced_lfs_objects
lfs_objects.joins("INNER JOIN file_registry ON file_registry.file_id = #{fdw_table}.id")
.merge(Geo::FileRegistry.lfs_objects)
.merge(Geo::FileRegistry.synced)
end
def fdw_find_failed_lfs_objects
lfs_objects.joins("INNER JOIN file_registry ON file_registry.file_id = #{fdw_table}.id")
.merge(Geo::FileRegistry.lfs_objects)
.merge(Geo::FileRegistry.failed)
end
#
# Legacy accessors (non FDW)
#
def legacy_find_synced_lfs_objects
legacy_find_lfs_objects(Geo::FileRegistry.lfs_objects.synced.pluck(:file_id))
end
def legacy_find_failed_lfs_objects
legacy_find_lfs_objects(Geo::FileRegistry.lfs_objects.failed.pluck(:file_id))
end
def legacy_find_lfs_objects(registry_file_ids)
return LfsObject.none if registry_file_ids.empty?
joined_relation = lfs_objects.joins(<<~SQL)
INNER JOIN
(VALUES #{registry_file_ids.map { |id| "(#{id})" }.join(',')})
file_registry(file_id)
ON #{LfsObject.table_name}.id = file_registry.file_id
SQL
joined_relation
end
end
end
......@@ -15,7 +15,7 @@ FactoryGirl.define do
trait :with_file do
after(:build, :stub) do |registry, _|
file =
if registry.file_type == :lfs
if registry.file_type.to_sym == :lfs
create(:lfs_object)
else
create(:upload)
......
......@@ -298,40 +298,4 @@ describe GeoNode, type: :model do
expect(node.selective_sync?).to be false
end
end
describe '#lfs_objects_synced_count' do
context 'primary node' do
subject { primary_node }
it 'returns nil' do
expect(subject.lfs_objects_synced_count).to be_nil
end
end
context 'secondary node' do
subject { node }
it 'returns a value' do
expect(subject.lfs_objects_synced_count).to eq(0)
end
end
end
describe '#lfs_objects_failed_count' do
context 'primary node' do
subject { primary_node }
it 'returns nil' do
expect(subject.lfs_objects_failed_count).to be_nil
end
end
context 'secondary node' do
subject { node }
it 'returns a value' do
expect(subject.lfs_objects_failed_count).to eq(0)
end
end
end
end
......@@ -162,9 +162,9 @@ describe GeoNodeStatus, :geo, :truncate do
create(:geo_file_registry, success: false)
create(:geo_file_registry, :avatar, success: false)
create(:geo_file_registry, file_type: :attachment, success: false)
create(:geo_file_registry, :lfs)
create(:geo_file_registry, :lfs, :with_file)
create(:geo_file_registry, :lfs, success: false)
create(:geo_file_registry, :lfs, :with_file, success: false)
expect(subject.lfs_objects_failed_count).to eq(1)
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