Commit 743c3227 authored by Alexis Reigel's avatar Alexis Reigel

select group runners also in register_job_service

parent 7584d03a
...@@ -17,6 +17,8 @@ module Ci ...@@ -17,6 +17,8 @@ module Ci
builds = builds =
if runner.shared? if runner.shared?
builds_for_shared_runner builds_for_shared_runner
elsif runner.group?
builds_for_group_runner
else else
builds_for_specific_runner builds_for_specific_runner
end end
...@@ -69,16 +71,33 @@ module Ci ...@@ -69,16 +71,33 @@ module Ci
private private
def builds_for_shared_runner def builds_for_shared_runner
new_builds. builds_for_scheduled_runner(
# don't run projects which have not enabled shared runners and builds running_builds_for_shared_runners,
joins(:project).where(projects: { shared_runners_enabled: true, pending_delete: false }) projects: { shared_runners_enabled: true }
)
end
def builds_for_group_runner
builds_for_scheduled_runner(
running_builds_for_group_runners,
projects: { group_runners_enabled: true }
)
end
def builds_for_scheduled_runner(build_join, project_where)
project_where = project_where.deep_merge(projects: { pending_delete: false })
# don't run projects which have not enabled group runners and builds
builds = new_builds
.joins(:project).where(project_where)
.joins('LEFT JOIN project_features ON ci_builds.project_id = project_features.project_id') .joins('LEFT JOIN project_features ON ci_builds.project_id = project_features.project_id')
.where('project_features.builds_access_level IS NULL or project_features.builds_access_level > 0'). .where('project_features.builds_access_level IS NULL or project_features.builds_access_level > 0')
# Implement fair scheduling # Implement fair scheduling
# 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 group runners at all
joins("LEFT JOIN (#{running_builds_for_shared_runners.to_sql}) AS project_builds ON ci_builds.project_id=project_builds.project_id") builds
.joins("LEFT JOIN (#{build_join.to_sql}) AS project_builds ON ci_builds.project_id=project_builds.project_id")
.order('COALESCE(project_builds.running_builds, 0) ASC', 'ci_builds.id ASC') .order('COALESCE(project_builds.running_builds, 0) ASC', 'ci_builds.id ASC')
end end
...@@ -87,7 +106,15 @@ module Ci ...@@ -87,7 +106,15 @@ module Ci
end end
def running_builds_for_shared_runners def running_builds_for_shared_runners
Ci::Build.running.where(runner: Ci::Runner.shared) running_builds_for_runners(Ci::Runner.shared)
end
def running_builds_for_group_runners
running_builds_for_runners(Ci::Runner.joins(:runner_groups))
end
def running_builds_for_runners(runners)
Ci::Build.running.where(runner: runners)
.group(:project_id).select(:project_id, 'count(*) AS running_builds') .group(:project_id).select(:project_id, 'count(*) AS running_builds')
end end
......
...@@ -2,11 +2,13 @@ require 'spec_helper' ...@@ -2,11 +2,13 @@ require 'spec_helper'
module Ci module Ci
describe RegisterJobService do describe RegisterJobService do
let!(:project) { FactoryBot.create :project, shared_runners_enabled: false } let!(:project) { create :project, shared_runners_enabled: false }
let!(:pipeline) { FactoryBot.create :ci_pipeline, project: project } let!(:group) { create :group }
let!(:pending_job) { FactoryBot.create :ci_build, pipeline: pipeline } let!(:pipeline) { create :ci_pipeline, project: project }
let!(:shared_runner) { FactoryBot.create(:ci_runner, is_shared: true) } let!(:pending_job) { create :ci_build, pipeline: pipeline }
let!(:specific_runner) { FactoryBot.create(:ci_runner, is_shared: false) } let!(:shared_runner) { create :ci_runner, is_shared: true }
let!(:specific_runner) { create :ci_runner, is_shared: false }
let!(:group_runner) { create :ci_runner, groups: [group] }
before do before do
specific_runner.assign_to(project) specific_runner.assign_to(project)
...@@ -167,6 +169,73 @@ module Ci ...@@ -167,6 +169,73 @@ module Ci
end end
end end
context 'allow group runners' do
before do
project.update!(group_runners_enabled: true, group: group)
end
context 'for multiple builds' do
let!(:project2) { create :project, group_runners_enabled: true, group: group }
let!(:pipeline2) { create :ci_pipeline, project: project2 }
let!(:project3) { create :project, group_runners_enabled: true, group: group }
let!(:pipeline3) { create :ci_pipeline, project: project3 }
let!(:build1_project1) { pending_job }
let!(:build2_project1) { create :ci_build, pipeline: pipeline }
let!(:build3_project1) { create :ci_build, pipeline: pipeline }
let!(:build1_project2) { create :ci_build, pipeline: pipeline2 }
let!(:build2_project2) { create :ci_build, pipeline: pipeline2 }
let!(:build1_project3) { create :ci_build, pipeline: pipeline3 }
it 'prefers projects without builds first' do
# it gets for one build from each of the projects
expect(execute(group_runner)).to eq(build1_project1)
expect(execute(group_runner)).to eq(build1_project2)
expect(execute(group_runner)).to eq(build1_project3)
# then it gets a second build from each of the projects
expect(execute(group_runner)).to eq(build2_project1)
expect(execute(group_runner)).to eq(build2_project2)
# in the end the third build
expect(execute(group_runner)).to eq(build3_project1)
end
it 'equalises number of running builds' do
# after finishing the first build for project 1, get a second build from the same project
expect(execute(group_runner)).to eq(build1_project1)
build1_project1.reload.success
expect(execute(group_runner)).to eq(build2_project1)
expect(execute(group_runner)).to eq(build1_project2)
build1_project2.reload.success
expect(execute(group_runner)).to eq(build2_project2)
expect(execute(group_runner)).to eq(build1_project3)
expect(execute(group_runner)).to eq(build3_project1)
end
end
context 'group runner' do
let(:build) { execute(group_runner) }
it { expect(build).to be_kind_of(Build) }
it { expect(build).to be_valid }
it { expect(build).to be_running }
it { expect(build.runner).to eq(group_runner) }
end
end
context 'disallow group runners' do
before do
project.update(group_runners_enabled: false)
end
context 'group runner' do
let(:build) { execute(group_runner) }
it { expect(build).to be_nil }
end
end
context 'when first build is stalled' do context 'when first build is stalled' do
before do before do
pending_job.update(lock_version: 0) pending_job.update(lock_version: 0)
......
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