Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
c799a2cc
Commit
c799a2cc
authored
Mar 31, 2021
by
Dmitry Gruzd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix blobs N+1 project load
parent
f4e9203f
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
43 additions
and
11 deletions
+43
-11
app/presenters/search_service_presenter.rb
app/presenters/search_service_presenter.rb
+3
-1
ee/changelogs/unreleased/326318-fix-blobs-n-plus-1.yml
ee/changelogs/unreleased/326318-fix-blobs-n-plus-1.yml
+5
-0
ee/lib/elastic/latest/git_class_proxy.rb
ee/lib/elastic/latest/git_class_proxy.rb
+2
-2
ee/lib/elastic/latest/git_instance_proxy.rb
ee/lib/elastic/latest/git_instance_proxy.rb
+2
-2
ee/lib/gitlab/elastic/project_search_results.rb
ee/lib/gitlab/elastic/project_search_results.rb
+3
-2
ee/lib/gitlab/elastic/search_results.rb
ee/lib/gitlab/elastic/search_results.rb
+4
-3
ee/spec/features/search/elastic/global_search_spec.rb
ee/spec/features/search/elastic/global_search_spec.rb
+22
-0
ee/spec/lib/elastic/latest/git_instance_proxy_spec.rb
ee/spec/lib/elastic/latest/git_instance_proxy_spec.rb
+2
-1
No files found.
app/presenters/search_service_presenter.rb
View file @
c799a2cc
...
...
@@ -11,7 +11,9 @@ class SearchServicePresenter < Gitlab::View::Presenter::Delegated
merge_requests: :with_web_entity_associations
,
epics: :with_web_entity_associations
,
notes: :with_web_entity_associations
,
milestones: :with_web_entity_associations
milestones: :with_web_entity_associations
,
commits: :with_web_entity_associations
,
blobs: :with_web_entity_associations
}.
freeze
SORT_ENABLED_SCOPES
=
%w(issues merge_requests)
.
freeze
...
...
ee/changelogs/unreleased/326318-fix-blobs-n-plus-1.yml
0 → 100644
View file @
c799a2cc
---
title
:
Fix blobs search N+1
merge_request
:
57826
author
:
type
:
performance
ee/lib/elastic/latest/git_class_proxy.rb
View file @
c799a2cc
...
...
@@ -27,11 +27,11 @@ module Elastic
end
# @return [Kaminari::PaginatableArray]
def
elastic_search_as_found_blob
(
query
,
page:
1
,
per:
20
,
options:
{})
def
elastic_search_as_found_blob
(
query
,
page:
1
,
per:
20
,
options:
{}
,
preload_method:
nil
)
# Highlight is required for parse_search_result to locate relevant line
options
=
options
.
merge
(
highlight:
true
)
elastic_search_and_wrap
(
query
,
type:
es_type
,
page:
page
,
per:
per
,
options:
options
)
do
|
result
,
project
|
elastic_search_and_wrap
(
query
,
type:
es_type
,
page:
page
,
per:
per
,
options:
options
,
preload_method:
preload_method
)
do
|
result
,
project
|
::
Gitlab
::
Elastic
::
SearchResults
.
parse_search_result
(
result
,
project
)
end
end
...
...
ee/lib/elastic/latest/git_instance_proxy.rb
View file @
c799a2cc
...
...
@@ -22,10 +22,10 @@ module Elastic
end
# @return [Kaminari::PaginatableArray]
def
elastic_search_as_found_blob
(
query
,
page:
1
,
per:
20
,
options:
{})
def
elastic_search_as_found_blob
(
query
,
page:
1
,
per:
20
,
options:
{}
,
preload_method:
nil
)
options
=
repository_specific_options
(
options
)
self
.
class
.
elastic_search_as_found_blob
(
query
,
page:
page
,
per:
per
,
options:
options
)
self
.
class
.
elastic_search_as_found_blob
(
query
,
page:
page
,
per:
per
,
options:
options
,
preload_method:
preload_method
)
end
def
delete_index_for_commits_and_blobs
(
wiki:
false
)
...
...
ee/lib/gitlab/elastic/project_search_results.rb
View file @
c799a2cc
...
...
@@ -17,7 +17,7 @@ module Gitlab
private
def
blobs
(
page:
1
,
per_page:
DEFAULT_PER_PAGE
,
count_only:
false
)
def
blobs
(
page:
1
,
per_page:
DEFAULT_PER_PAGE
,
count_only:
false
,
preload_method:
nil
)
return
Kaminari
.
paginate_array
([])
unless
Ability
.
allowed?
(
@current_user
,
:download_code
,
project
)
return
Kaminari
.
paginate_array
([])
if
project
.
empty_repo?
||
query
.
blank?
return
Kaminari
.
paginate_array
([])
unless
root_ref?
...
...
@@ -27,7 +27,8 @@ module Gitlab
query
,
page:
(
page
||
1
).
to_i
,
per:
per_page
,
options:
{
count_only:
count_only
}
options:
{
count_only:
count_only
},
preload_method:
preload_method
)
end
end
...
...
ee/lib/gitlab/elastic/search_results.rb
View file @
c799a2cc
...
...
@@ -38,7 +38,7 @@ module Gitlab
when
'notes'
eager_load
(
notes
,
page
,
per_page
,
preload_method
,
project:
[
:route
,
:namespace
])
when
'blobs'
blobs
(
page:
page
,
per_page:
per_page
)
blobs
(
page:
page
,
per_page:
per_page
,
preload_method:
preload_method
)
when
'wiki_blobs'
wiki_blobs
(
page:
page
,
per_page:
per_page
)
when
'commits'
...
...
@@ -289,7 +289,7 @@ module Gitlab
scope_results
:notes
,
Note
,
count_only:
count_only
end
def
blobs
(
page:
1
,
per_page:
DEFAULT_PER_PAGE
,
count_only:
false
)
def
blobs
(
page:
1
,
per_page:
DEFAULT_PER_PAGE
,
count_only:
false
,
preload_method:
nil
)
return
Kaminari
.
paginate_array
([])
if
query
.
blank?
strong_memoize
(
memoize_key
(
:blobs
,
count_only:
count_only
))
do
...
...
@@ -297,7 +297,8 @@ module Gitlab
query
,
page:
(
page
||
1
).
to_i
,
per:
per_page
,
options:
base_options
.
merge
(
count_only:
count_only
)
options:
base_options
.
merge
(
count_only:
count_only
),
preload_method:
preload_method
)
end
end
...
...
ee/spec/features/search/elastic/global_search_spec.rb
View file @
c799a2cc
...
...
@@ -82,6 +82,28 @@ RSpec.describe 'Global elastic search', :elastic, :sidekiq_inline do
it_behaves_like
'an efficient database result'
end
context
'searching code'
do
let
(
:path
)
{
search_path
(
search:
'*'
,
scope:
'blobs'
)
}
it
'avoids N+1 database queries'
do
project
.
repository
.
index_commits_and_blobs
ensure_elasticsearch_index!
control
=
ActiveRecord
::
QueryRecorder
.
new
{
visit
path
}
expect
(
page
).
to
have_css
(
'.search-results'
)
# Confirm there are search results to prevent false positives
projects
.
each
do
|
project
|
project
.
repository
.
index_commits_and_blobs
end
ensure_elasticsearch_index!
expect
{
visit
path
}.
not_to
exceed_query_limit
(
control
.
count
)
expect
(
page
).
to
have_css
(
'.search-results'
)
# Confirm there are search results to prevent false positives
end
end
end
describe
'I search through the issues and I see pagination'
do
...
...
ee/spec/lib/elastic/latest/git_instance_proxy_spec.rb
View file @
c799a2cc
...
...
@@ -56,7 +56,8 @@ RSpec.describe Elastic::Latest::GitInstanceProxy do
{
page:
2
,
per:
30
,
options:
{
foo: :bar
}
options:
{
foo: :bar
},
preload_method:
nil
}
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment