Commit 4d75765a authored by Yannis Roussos's avatar Yannis Roussos

Merge branch 'reduce_expire_pipeline_cache_worker_queries' into 'master'

Reduce query count for extremely frequent ExpirePipelineCacheWorker [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!57304
parents db4cc3c0 c2a52670
......@@ -309,6 +309,7 @@ module Ci
scope :created_after, -> (time) { where('ci_pipelines.created_at > ?', time) }
scope :created_before_id, -> (id) { where('ci_pipelines.id < ?', id) }
scope :before_pipeline, -> (pipeline) { created_before_id(pipeline.id).outside_pipeline_family(pipeline) }
scope :eager_load_project, -> { eager_load(project: [:route, { namespace: :route }]) }
scope :outside_pipeline_family, ->(pipeline) do
where.not(id: pipeline.same_family_pipeline_ids)
......
......@@ -12,7 +12,7 @@ class ExpirePipelineCacheWorker
# rubocop: disable CodeReuse/ActiveRecord
def perform(pipeline_id)
pipeline = Ci::Pipeline.find_by(id: pipeline_id)
pipeline = Ci::Pipeline.eager_load_project.find_by(id: pipeline_id)
return unless pipeline
Ci::ExpirePipelineCacheService.new.execute(pipeline)
......
---
title: Reduce query count for ExpirePipelineCacheWorker
merge_request: 57304
author:
type: performance
......@@ -18,6 +18,23 @@ RSpec.describe ExpirePipelineCacheWorker do
subject.perform(pipeline.id)
end
it 'does not perform extra queries', :aggregate_failures do
recorder = ActiveRecord::QueryRecorder.new { subject.perform(pipeline.id) }
project_queries = recorder.data.values.flat_map {|v| v[:occurrences]}.select {|s| s.include?('FROM "projects"')}
namespace_queries = recorder.data.values.flat_map {|v| v[:occurrences]}.select {|s| s.include?('FROM "namespaces"')}
route_queries = recorder.data.values.flat_map {|v| v[:occurrences]}.select {|s| s.include?('FROM "routes"')}
# This worker is run 1 million times an hour, so we need to save as much
# queries as possible.
expect(recorder.count).to be <= 6
# These arises from #update_etag_cache
expect(project_queries.size).to eq(1)
expect(namespace_queries.size).to eq(1)
expect(route_queries.size).to eq(1)
end
it "doesn't do anything if the pipeline not exist" do
expect_any_instance_of(Ci::ExpirePipelineCacheService).not_to receive(:execute)
expect_any_instance_of(Gitlab::EtagCaching::Store).not_to receive(:touch)
......
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