Move duplicated queries to GeoNode model

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