Commit 1bc4a03e authored by Alper Akgun's avatar Alper Akgun

Merge branch '325344-improve-computation-of-namespace-excess-storage' into 'master'

Use a single query to compute excess storage

See merge request gitlab-org/gitlab!57088
parents 10c13f05 22659ceb
......@@ -202,22 +202,15 @@ module EE
def total_repository_size_excess
strong_memoize(:total_repository_size_excess) do
namespace_size_limit = actual_size_limit
namespace_limit_arel = Arel::Nodes::SqlLiteral.new(namespace_size_limit.to_s.presence || 'NULL')
total_excess = (total_repository_size_arel - repository_size_limit_arel).sum
total_excess = total_repository_size_excess_calculation(::Project.arel_table[:repository_size_limit])
total_excess += total_repository_size_excess_calculation(namespace_limit_arel, project_level: false) if namespace_size_limit.to_i > 0
total_excess
projects_for_repository_size_excess.pluck(total_excess).first || 0
end
end
def repository_size_excess_project_count
strong_memoize(:repository_size_excess_project_count) do
namespace_size_limit = actual_size_limit
count = projects_for_repository_size_excess.count
count += projects_for_repository_size_excess(namespace_size_limit).count if namespace_size_limit.to_i > 0
count
projects_for_repository_size_excess.count
end
end
......@@ -428,26 +421,34 @@ module EE
)
end
def total_repository_size_excess_calculation(repository_size_limit, project_level: true)
total_excess = (total_repository_size_arel - repository_size_limit).sum
relation = projects_for_repository_size_excess((repository_size_limit unless project_level))
relation.pluck(total_excess).first || 0 # rubocop:disable Rails/Pick
end
def total_repository_size_arel
arel_table = ::ProjectStatistics.arel_table
arel_table[:repository_size] + arel_table[:lfs_objects_size]
end
def projects_for_repository_size_excess(limit = nil)
if limit
all_projects
.with_total_repository_size_greater_than(limit)
.without_repository_size_limit
def projects_for_repository_size_excess
projects_with_limits = ::Project.without_unlimited_repository_size_limit
if actual_size_limit.to_i > 0
# When the instance or namespace level limit is set, we need to include those without project level limits
projects_with_limits = projects_with_limits.or(::Project.without_repository_size_limit)
end
all_projects
.merge(projects_with_limits)
.with_total_repository_size_greater_than(repository_size_limit_arel)
end
def repository_size_limit_arel
instance_size_limit = actual_size_limit.to_i
if instance_size_limit > 0
self.class.arel_table.coalesce(
::Project.arel_table[:repository_size_limit],
instance_size_limit
)
else
all_projects
.with_total_repository_size_greater_than(::Project.arel_table[:repository_size_limit])
.without_unlimited_repository_size_limit
::Project.arel_table[:repository_size_limit]
end
end
......
---
title: Improve computation of excess repository storage size
merge_request: 57088
author:
type: performance
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