Commit 7e084da1 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'fix/gb/optimize-counting-running-jobs-upon-assignment' into 'master'

Read running builds from denormalized table upon assignment

See merge request gitlab-org/gitlab!77104
parents f52ac300 28a00066
...@@ -69,17 +69,6 @@ module Gitlab ...@@ -69,17 +69,6 @@ module Gitlab
self.class.attempt_counter.increment self.class.attempt_counter.increment
end end
# rubocop: disable CodeReuse/ActiveRecord
def jobs_running_for_project(job)
return '+Inf' unless runner.instance_type?
# excluding currently started job
running_jobs_count = job.project.builds.running.where(runner: ::Ci::Runner.instance_type)
.limit(JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET + 1).count - 1
running_jobs_count < JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET ? running_jobs_count : "#{JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET}+"
end
# rubocop: enable CodeReuse/ActiveRecord
def increment_queue_operation(operation) def increment_queue_operation(operation)
self.class.increment_queue_operation(operation) self.class.increment_queue_operation(operation)
end end
...@@ -242,6 +231,32 @@ module Gitlab ...@@ -242,6 +231,32 @@ module Gitlab
Gitlab::Metrics.histogram(name, comment, labels, buckets) Gitlab::Metrics.histogram(name, comment, labels, buckets)
end end
end end
private
# rubocop: disable CodeReuse/ActiveRecord
def jobs_running_for_project(job)
return '+Inf' unless runner.instance_type?
# excluding currently started job
running_jobs_count = running_jobs_relation(job)
.limit(JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET + 1).count - 1
if running_jobs_count < JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET
running_jobs_count
else
"#{JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET}+"
end
end
def running_jobs_relation(job)
if ::Feature.enabled?(:ci_pending_builds_maintain_denormalized_data, default_enabled: :yaml)
::Ci::RunningBuild.instance_type.where(project_id: job.project_id)
else
job.project.builds.running.where(runner: ::Ci::Runner.instance_type)
end
end
# rubocop: enable CodeReuse/ActiveRecord
end end
end end
end end
......
...@@ -827,11 +827,17 @@ module Ci ...@@ -827,11 +827,17 @@ module Ci
end end
context 'when project already has running jobs' do context 'when project already has running jobs' do
let!(:build2) { create(:ci_build, :running, pipeline: pipeline, runner: shared_runner) } let(:build2) { create(:ci_build, :running, pipeline: pipeline, runner: shared_runner) }
let!(:build3) { create(:ci_build, :running, pipeline: pipeline, runner: shared_runner) } let(:build3) { create(:ci_build, :running, pipeline: pipeline, runner: shared_runner) }
before do
::Ci::RunningBuild.upsert_shared_runner_build!(build2)
::Ci::RunningBuild.upsert_shared_runner_build!(build3)
end
it 'counts job queuing time histogram with expected labels' do it 'counts job queuing time histogram with expected labels' do
allow(attempt_counter).to receive(:increment) allow(attempt_counter).to receive(:increment)
expect(job_queue_duration_seconds).to receive(:observe) expect(job_queue_duration_seconds).to receive(:observe)
.with({ shared_runner: expected_shared_runner, .with({ shared_runner: expected_shared_runner,
jobs_running_for_project: expected_jobs_running_for_project_third_job, jobs_running_for_project: expected_jobs_running_for_project_third_job,
...@@ -845,6 +851,14 @@ module Ci ...@@ -845,6 +851,14 @@ module Ci
shared_examples 'metrics collector' do shared_examples 'metrics collector' do
it_behaves_like 'attempt counter collector' it_behaves_like 'attempt counter collector'
it_behaves_like 'jobs queueing time histogram collector' it_behaves_like 'jobs queueing time histogram collector'
context 'when using denormalized data is disabled' do
before do
stub_feature_flags(ci_pending_builds_maintain_denormalized_data: false)
end
it_behaves_like 'jobs queueing time histogram collector'
end
end end
context 'when shared runner is used' do context 'when shared runner is used' do
...@@ -875,6 +889,16 @@ module Ci ...@@ -875,6 +889,16 @@ module Ci
it_behaves_like 'metrics collector' it_behaves_like 'metrics collector'
end end
context 'when max running jobs bucket size is exceeded' do
before do
stub_const('Gitlab::Ci::Queue::Metrics::JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET', 1)
end
let(:expected_jobs_running_for_project_third_job) { '1+' }
it_behaves_like 'metrics collector'
end
context 'when pending job with queued_at=nil is used' do context 'when pending job with queued_at=nil is used' do
before do before do
pending_job.update!(queued_at: nil) pending_job.update!(queued_at: nil)
......
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