Commit e17350be authored by Douwe Maan's avatar Douwe Maan

Merge branch '60449-reduce-gitaly-calls-when-rendering-commits-in-md' into 'master'

Lessen Gitaly N+1 errors in markdown processing

Closes #60449

See merge request gitlab-org/gitlab-ce!31037
parents 35e49d3b 336d3ccc
---
title: Batch processing of commit refs in markdown processing
merge_request: 31037
author:
type: performance
......@@ -337,6 +337,24 @@ module Banzai
@current_project_namespace_path ||= project&.namespace&.full_path
end
def records_per_parent
@_records_per_project ||= {}
@_records_per_project[object_class.to_s.underscore] ||= begin
hash = Hash.new { |h, k| h[k] = {} }
parent_per_reference.each do |path, parent|
record_ids = references_per_parent[path]
parent_records(parent, record_ids).each do |record|
hash[parent][record_identifier(record)] = record
end
end
hash
end
end
private
def full_project_path(namespace, project_ref)
......
......@@ -19,12 +19,11 @@ module Banzai
end
def find_object(project, id)
return unless project.is_a?(Project)
return unless project.is_a?(Project) && project.valid_repo?
if project && project.valid_repo?
# n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/43894
Gitlab::GitalyClient.allow_n_plus_1_calls { project.commit(id) }
end
_, record = records_per_parent[project].detect { |k, _v| Gitlab::Git.shas_eql?(k, id) }
record
end
def referenced_merge_request_commit_shas
......@@ -66,6 +65,14 @@ module Banzai
private
def record_identifier(record)
record.id
end
def parent_records(parent, ids)
parent.commits_by(oids: ids.to_a)
end
def noteable
context[:noteable]
end
......
......@@ -3,22 +3,8 @@
module Banzai
module Filter
class IssuableReferenceFilter < AbstractReferenceFilter
def records_per_parent
@records_per_project ||= {}
@records_per_project[object_class.to_s.underscore] ||= begin
hash = Hash.new { |h, k| h[k] = {} }
parent_per_reference.each do |path, parent|
record_ids = references_per_parent[path]
parent_records(parent, record_ids).each do |record|
hash[parent][record.iid.to_i] = record
end
end
hash
end
def record_identifier(record)
record.iid.to_i
end
def find_object(parent, iid)
......
......@@ -105,6 +105,17 @@ describe Banzai::Filter::CommitReferenceFilter do
expect(doc.css('a').first[:href]).to eq(url)
end
context "a doc with many (29) strings that could be SHAs" do
let!(:oids) { noteable.commits.collect(&:id) }
it 'makes only a single request to Gitaly' do
expect(Gitlab::GitalyClient).to receive(:allow_n_plus_1_calls).exactly(0).times
expect(Gitlab::Git::Commit).to receive(:batch_by_oid).once.and_call_original
reference_filter("A big list of SHAs #{oids.join(", ")}", noteable: noteable)
end
end
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