Commit 52215e89 authored by Stan Hu's avatar Stan Hu

Allow caching of negative FindCommit matches

When FindCommit ref caching is enabled, negative matches would
previously not be cached. However, if a source branch is deleted,
there's no need to keep looking up the same commit. This change caches
the result of a nil commit.
parent 8db4a54d
---
title: Allow caching of negative FindCommit matches
merge_request: 29952
author:
type: performance
...@@ -271,26 +271,30 @@ module Gitlab ...@@ -271,26 +271,30 @@ module Gitlab
end end
def find_commit(revision) def find_commit(revision)
if Gitlab::SafeRequestStore.active? return call_find_commit(revision) unless Gitlab::SafeRequestStore.active?
# We don't use Gitlab::SafeRequestStore.fetch(key) { ... } directly
# because `revision` can be a branch name, so we can't use it as a key # We don't use Gitlab::SafeRequestStore.fetch(key) { ... } directly
# as it could point to another commit later on (happens a lot in # because `revision` can be a branch name, so we can't use it as a key
# tests). # as it could point to another commit later on (happens a lot in
key = { # tests).
storage: @gitaly_repo.storage_name, key = {
relative_path: @gitaly_repo.relative_path, storage: @gitaly_repo.storage_name,
commit_id: revision relative_path: @gitaly_repo.relative_path,
} commit_id: revision
return Gitlab::SafeRequestStore[key] if Gitlab::SafeRequestStore.exist?(key) }
return Gitlab::SafeRequestStore[key] if Gitlab::SafeRequestStore.exist?(key)
commit = call_find_commit(revision)
return unless commit commit = call_find_commit(revision)
key[:commit_id] = commit.id unless GitalyClient.ref_name_caching_allowed? if GitalyClient.ref_name_caching_allowed?
Gitlab::SafeRequestStore[key] = commit Gitlab::SafeRequestStore[key] = commit
else return commit
call_find_commit(revision)
end end
return unless commit
key[:commit_id] = commit.id
Gitlab::SafeRequestStore[key] = commit
end end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
......
...@@ -223,6 +223,19 @@ describe Gitlab::GitalyClient::CommitService do ...@@ -223,6 +223,19 @@ describe Gitlab::GitalyClient::CommitService do
end end
context 'when caching of the ref name is enabled' do context 'when caching of the ref name is enabled' do
it 'caches negative entries' do
expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commit).once.and_return(double(commit: nil))
commit = nil
2.times do
::Gitlab::GitalyClient.allow_ref_name_caching do
commit = described_class.new(repository).find_commit('master')
end
end
expect(commit).to eq(nil)
end
it 'returns a cached commit' do it 'returns a cached commit' do
expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commit).once.and_return(double(commit: commit_dbl)) expect_any_instance_of(Gitaly::CommitService::Stub).to receive(:find_commit).once.and_return(double(commit: commit_dbl))
......
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