Move duplicated queries to GeoNode model

parent 2bffea1e
......@@ -113,6 +113,42 @@ class GeoNode < ActiveRecord::Base
namespaces.flat_map { |namespace| namespace.all_projects.select(:id).pluck(:id) }.uniq
end
def lfs_objects
if project_ids
LfsObject.joins(:projects).where(projects: { id: project_ids })
else
LfsObject.all
end
end
def projects
if project_ids
Project.where(id: project_ids)
else
Project.all
end
end
def project_registries
if project_ids
Geo::ProjectRegistry.where(project_id: project_ids)
else
Geo::ProjectRegistry.all
end
end
def uploads
if project_ids
uploads_table = Upload.arel_table
group_uploads = uploads_table[:model_type].eq('Namespace').and(uploads_table[:model_id].in(Gitlab::Geo.current_node.namespace_ids))
project_uploads = uploads_table[:model_type].eq('Project').and(uploads_table[:model_id].in(project_ids))
other_uploads = uploads_table[:model_type].not_in(%w[Namespace Project])
Upload.where(group_uploads.or(project_uploads).or(other_uploads))
else
Upload.all
end
end
private
......
......@@ -54,7 +54,7 @@ class GeoNodeStatus
@lfs_objects_synced_count ||= begin
relation = Geo::FileRegistry.where(file_type: :lfs)
if restricted_project_ids
if Gitlab::Geo.current_node.project_ids
relation = relation.where(file_id: lfs_objects.pluck(:id))
end
......@@ -104,49 +104,18 @@ class GeoNodeStatus
end
def attachments
@attachments ||=
if restricted_project_ids
uploads_table = Upload.arel_table
group_uploads = uploads_table[:model_type].eq('Namespace').and(uploads_table[:model_id].in(Gitlab::Geo.current_node.namespace_ids))
project_uploads = uploads_table[:model_type].eq('Project').and(uploads_table[:model_id].in(restricted_project_ids))
other_uploads = uploads_table[:model_type].not_in(%w[Namespace Project])
Upload.where(group_uploads.or(project_uploads).or(other_uploads))
else
Upload.all
end
@attachments ||= Gitlab::Geo.current_node.uploads
end
def lfs_objects
@lfs_objects ||=
if restricted_project_ids
LfsObject.joins(:projects).where(projects: { id: restricted_project_ids })
else
LfsObject.all
end
@lfs_objects ||= Gitlab::Geo.current_node.lfs_objects
end
def project_registries
@project_registries ||=
if restricted_project_ids
Geo::ProjectRegistry.where(project_id: restricted_project_ids)
else
Geo::ProjectRegistry.all
end
@project_registries ||= Gitlab::Geo.current_node.project_registries
end
def repositories
@repositories ||=
if restricted_project_ids
Project.where(id: restricted_project_ids)
else
Project.all
end
end
def restricted_project_ids
return @restricted_project_ids if defined?(@restricted_project_ids)
@restricted_project_ids = Gitlab::Geo.current_node.project_ids
@repositories ||= Gitlab::Geo.current_node.projects
end
end
......@@ -151,6 +151,10 @@ module Geo
Gitlab::ExclusiveLease.cancel(lease_key, uuid)
end
def current_node
Gitlab::Geo.current_node
end
def node_enabled?
# Only check every minute to avoid polling the DB excessively
unless @last_enabled_check.present? && @last_enabled_check > 1.minute.ago
......
......@@ -19,40 +19,23 @@ module Geo
def find_object_ids(restricted_project_ids)
downloaded_ids = find_downloaded_ids([:attachment, :avatar, :file])
relation =
if restricted_project_ids
uploads_table = Upload.arel_table
group_uploads = uploads_table[:model_type].eq('Namespace').and(uploads_table[:model_id].in(Gitlab::Geo.current_node.namespace_ids))
project_uploads = uploads_table[:model_type].eq('Project').and(uploads_table[:model_id].in(restricted_project_ids))
other_uploads = uploads_table[:model_type].not_in(%w[Namespace Project])
Upload.where(group_uploads.or(project_uploads).or(other_uploads))
else
Upload.all
end
relation.where.not(id: downloaded_ids)
.order(created_at: :desc)
.limit(db_retrieve_batch_size)
.pluck(:id, :uploader)
.map { |id, uploader| [id, uploader.sub(/Uploader\z/, '').downcase] }
current_node.uploads
.where.not(id: downloaded_ids)
.order(created_at: :desc)
.limit(db_retrieve_batch_size)
.pluck(:id, :uploader)
.map { |id, uploader| [id, uploader.sub(/Uploader\z/, '').downcase] }
end
def find_lfs_object_ids(restricted_project_ids)
downloaded_ids = find_downloaded_ids([:lfs])
relation =
if restricted_project_ids
LfsObject.joins(:projects).where(projects: { id: restricted_project_ids })
else
LfsObject.all
end
relation.where.not(id: downloaded_ids)
.order(created_at: :desc)
.limit(db_retrieve_batch_size)
.pluck(:id)
.map { |id| [id, :lfs] }
current_node.lfs_objects
.where.not(id: downloaded_ids)
.order(created_at: :desc)
.limit(db_retrieve_batch_size)
.pluck(:id)
.map { |id| [id, :lfs] }
end
def find_downloaded_ids(file_types)
......
......@@ -16,39 +16,26 @@ module Geo
end
def load_pending_resources
restricted_project_ids = Gitlab::Geo.current_node.project_ids
project_ids_not_synced = find_project_ids_not_synced(restricted_project_ids)
project_ids_updated_recently = find_project_ids_updated_recently(restricted_project_ids)
project_ids_not_synced = find_project_ids_not_synced
project_ids_updated_recently = find_project_ids_updated_recently
interleave(project_ids_not_synced, project_ids_updated_recently)
end
def find_project_ids_not_synced(restricted_project_ids)
relation =
if restricted_project_ids
Project.where(id: restricted_project_ids)
else
Project.all
end
relation.where.not(id: Geo::ProjectRegistry.synced.pluck(:project_id))
.order(last_repository_updated_at: :desc)
.limit(db_retrieve_batch_size)
.pluck(:id)
def find_project_ids_not_synced
current_node.projects
.where.not(id: Geo::ProjectRegistry.synced.pluck(:project_id))
.order(last_repository_updated_at: :desc)
.limit(db_retrieve_batch_size)
.pluck(:id)
end
def find_project_ids_updated_recently(restricted_project_ids)
relation =
if restricted_project_ids
Geo::ProjectRegistry.where(project_id: restricted_project_ids)
else
Geo::ProjectRegistry.all
end
relation.dirty
.order(Gitlab::Database.nulls_first_order(:last_repository_synced_at, :desc))
.limit(db_retrieve_batch_size)
.pluck(:project_id)
def find_project_ids_updated_recently
current_node.project_registries
.dirty
.order(Gitlab::Database.nulls_first_order(:last_repository_synced_at, :desc))
.limit(db_retrieve_batch_size)
.pluck(:project_id)
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