Commit 8fbbcb87 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Use materialied CTE to improve builds fair scheduling

This significantly improves performance of the fair scheduling
algorithm, that is using the SQL query to retrieve a queue of builds
ordered by the number of builds running for a given project and a build
id.

Changelog: performance
parent 3886bfc0
...@@ -26,7 +26,8 @@ module Ci ...@@ -26,7 +26,8 @@ module Ci
# this returns builds that are ordered by number of running builds # this returns builds that are ordered by number of running builds
# we prefer projects that don't use shared runners at all # we prefer projects that don't use shared runners at all
relation relation
.joins("LEFT JOIN (#{running_builds_for_shared_runners.to_sql}) AS project_builds ON ci_pending_builds.project_id=project_builds.project_id") .with(running_builds_for_shared_runners_cte.to_arel)
.joins("LEFT JOIN project_builds ON ci_pending_builds.project_id = project_builds.project_id")
.order(Arel.sql('COALESCE(project_builds.running_builds, 0) ASC'), 'ci_pending_builds.build_id ASC') .order(Arel.sql('COALESCE(project_builds.running_builds, 0) ASC'), 'ci_pending_builds.build_id ASC')
end end
end end
...@@ -53,11 +54,14 @@ module Ci ...@@ -53,11 +54,14 @@ module Ci
private private
def running_builds_for_shared_runners def running_builds_for_shared_runners_cte
::Ci::RunningBuild running_builds = ::Ci::RunningBuild
.instance_type .instance_type
.group(:project_id) .group(:project_id)
.select(:project_id, 'COUNT(*) AS running_builds') .select(:project_id, 'COUNT(*) AS running_builds')
::Gitlab::SQL::CTE
.new(:project_builds, running_builds, materialized: true)
end end
# rubocop:enable CodeReuse/ActiveRecord # rubocop:enable CodeReuse/ActiveRecord
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