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
41e95956
Commit
41e95956
authored
Apr 07, 2021
by
Terri Chu
Committed by
Dmitry Gruzd
Apr 07, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reduce queries on group search API for blobs scope [RUN ALL RSPEC] [RUN AS-IF-FOSS]
parent
0f5d90d2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
14 deletions
+51
-14
ee/changelogs/unreleased/326754-api-endpoint-groups-id-search-blobs-scope-executes-more-than-10.yml
...nt-groups-id-search-blobs-scope-executes-more-than-10.yml
+5
-0
ee/lib/ee/api/search.rb
ee/lib/ee/api/search.rb
+5
-0
ee/spec/requests/api/search_spec.rb
ee/spec/requests/api/search_spec.rb
+31
-6
lib/api/search.rb
lib/api/search.rb
+10
-8
No files found.
ee/changelogs/unreleased/326754-api-endpoint-groups-id-search-blobs-scope-executes-more-than-10.yml
0 → 100644
View file @
41e95956
---
title
:
Reduce queries on group search API for blobs scope
merge_request
:
58495
author
:
type
:
performance
ee/lib/ee/api/search.rb
View file @
41e95956
...
...
@@ -11,6 +11,11 @@ module EE
ELASTICSEARCH_SCOPES
=
%w(wiki_blobs blobs commits notes)
.
freeze
override
:scope_preload_method
def
scope_preload_method
super
.
merge
(
blobs: :with_api_commit_entity_associations
).
freeze
end
override
:verify_search_scope!
def
verify_search_scope!
(
resource
:)
if
ELASTICSEARCH_SCOPES
.
include?
(
params
[
:scope
])
&&
!
use_elasticsearch?
(
resource
)
...
...
ee/spec/requests/api/search_spec.rb
View file @
41e95956
...
...
@@ -134,27 +134,52 @@ RSpec.describe API::Search, factory_default: :keep do
it
'avoids N+1 queries'
do
control
=
ActiveRecord
::
QueryRecorder
.
new
{
get
api
(
endpoint
,
user
),
params:
{
scope:
'commits'
,
search:
'folder'
}
}
project_2
=
create
(
:project
,
:public
,
:repository
,
:wiki_repo
,
name:
'awesome project 2'
)
project_3
=
create
(
:project
,
:public
,
:repository
,
:wiki_repo
,
name:
'awesome project 3'
)
project_2
=
create
(
:project
,
:public
,
:repository
,
:wiki_repo
,
group:
group
,
name:
'awesome project 2'
)
project_2
.
repository
.
index_commits_and_blobs
project_3
.
repository
.
index_commits_and_blobs
3
.
times
do
|
i
|
commit_sha
=
project
.
repository
.
create_file
(
user
,
"
#{
i
}
"
,
"folder
#{
i
}
"
,
message:
"committing folder
#{
i
}
"
,
branch_name:
'master'
)
project
.
repository
.
commit
(
commit_sha
)
end
project
.
repository
.
index_commits_and_blobs
ensure_elasticsearch_index!
# Some N+1 queries still exist
expect
{
get
api
(
endpoint
,
user
),
params:
{
scope:
'commits'
,
search:
'folder'
}
}.
not_to
exceed_query_limit
(
control
).
with_threshold
(
9
)
# N+1 queries still exist (ci_pipelines)
expect
{
get
api
(
endpoint
,
user
),
params:
{
scope:
'commits'
,
search:
'folder'
}
}.
not_to
exceed_query_limit
(
control
).
with_threshold
(
5
)
# support global, group, and project search results expected counts
expected_count
=
level
==
:project
?
5
:
7
expect
(
json_response
.
count
).
to
be
expected_count
end
end
context
'for blobs scope'
do
it_behaves_like
'response is correct'
,
schema:
'public_api/v4/blobs'
do
before
do
get
api
(
endpoint
,
user
),
params:
{
scope:
'blobs'
,
search:
'
monitors
'
}
get
api
(
endpoint
,
user
),
params:
{
scope:
'blobs'
,
search:
'
folder
'
}
end
end
it_behaves_like
'pagination'
,
scope:
'blobs'
it
'avoids N+1 queries'
do
control
=
ActiveRecord
::
QueryRecorder
.
new
{
get
api
(
endpoint
,
user
),
params:
{
scope:
'blobs'
,
search:
'Issue team'
}
}
project_2
=
create
(
:project
,
:public
,
:repository
,
:wiki_repo
,
group:
group
,
name:
'awesome project 2'
)
project_2
.
repository
.
index_commits_and_blobs
3
.
times
do
|
i
|
commit_sha
=
project
.
repository
.
create_file
(
user
,
"
#{
i
}
"
,
"Issue team
#{
i
}
"
,
message:
"
#{
i
}
"
,
branch_name:
'master'
)
project
.
repository
.
commit
(
commit_sha
)
end
project
.
repository
.
index_commits_and_blobs
ensure_elasticsearch_index!
expect
{
get
api
(
endpoint
,
user
),
params:
{
scope:
'blobs'
,
search:
'Issue team'
}
}.
not_to
exceed_query_limit
(
control
)
# support global, group, and project search results expected counts
expected_count
=
level
==
:project
?
6
:
9
expect
(
json_response
.
count
).
to
be
expected_count
end
context
'filters'
do
def
results_filenames
json_response
.
map
{
|
h
|
h
[
'filename'
]
}.
compact
...
...
lib/api/search.rb
View file @
41e95956
...
...
@@ -22,13 +22,15 @@ module API
users:
Entities
::
UserBasic
}.
freeze
SCOPE_PRELOAD_METHOD
=
{
merge_requests: :with_api_entity_associations
,
projects: :with_api_entity_associations
,
issues: :with_api_entity_associations
,
milestones: :with_api_entity_associations
,
commits: :with_api_commit_entity_associations
}.
freeze
def
scope_preload_method
{
merge_requests: :with_api_entity_associations
,
projects: :with_api_entity_associations
,
issues: :with_api_entity_associations
,
milestones: :with_api_entity_associations
,
commits: :with_api_commit_entity_associations
}.
freeze
end
def
search
(
additional_params
=
{})
search_params
=
{
...
...
@@ -60,7 +62,7 @@ module API
end
def
preload_method
SCOPE_PRELOAD_METHOD
[
params
[
:scope
].
to_sym
]
scope_preload_method
[
params
[
:scope
].
to_sym
]
end
def
verify_search_scope!
(
resource
:)
...
...
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