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
42b0158b
Commit
42b0158b
authored
Jul 18, 2020
by
Dmitry Gruzd
Committed by
Michael Kozono
Jul 18, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Resolve "Search API merge_requests scope preload project features"
parent
c7b422df
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
12 deletions
+43
-12
app/models/merge_request.rb
app/models/merge_request.rb
+9
-10
ee/app/models/merge_train.rb
ee/app/models/merge_train.rb
+2
-2
ee/changelogs/unreleased/219309-fix-mr-n-plus-1.yml
ee/changelogs/unreleased/219309-fix-mr-n-plus-1.yml
+5
-0
spec/services/search_service_spec.rb
spec/services/search_service_spec.rb
+27
-0
No files found.
app/models/merge_request.rb
View file @
42b0158b
...
@@ -251,17 +251,12 @@ class MergeRequest < ApplicationRecord
...
@@ -251,17 +251,12 @@ class MergeRequest < ApplicationRecord
end
end
scope
:join_project
,
->
{
joins
(
:target_project
)
}
scope
:join_project
,
->
{
joins
(
:target_project
)
}
scope
:references_project
,
->
{
references
(
:target_project
)
}
scope
:references_project
,
->
{
references
(
:target_project
)
}
PROJECT_ROUTE_AND_NAMESPACE_ROUTE
=
[
target_project:
[
:route
,
{
namespace: :route
}],
source_project:
[
:route
,
{
namespace: :route
}]
].
freeze
scope
:with_api_entity_associations
,
->
{
scope
:with_api_entity_associations
,
->
{
preload
(
:assignees
,
:author
,
:unresolved_notes
,
:labels
,
:milestone
,
preload_routables
:timelogs
,
:latest_merge_request_diff
,
.
preload
(
:assignees
,
:author
,
:unresolved_notes
,
:labels
,
:milestone
,
*
PROJECT_ROUTE_AND_NAMESPACE_ROUTE
,
:timelogs
,
:latest_merge_request_diff
,
metrics:
[
:latest_closed_by
,
:merged_by
])
target_project: :project_feature
,
metrics:
[
:latest_closed_by
,
:merged_by
])
}
}
scope
:by_target_branch_wildcard
,
->
(
wildcard_branch_name
)
do
scope
:by_target_branch_wildcard
,
->
(
wildcard_branch_name
)
do
...
@@ -269,6 +264,10 @@ class MergeRequest < ApplicationRecord
...
@@ -269,6 +264,10 @@ class MergeRequest < ApplicationRecord
end
end
scope
:by_target_branch
,
->
(
branch_name
)
{
where
(
target_branch:
branch_name
)
}
scope
:by_target_branch
,
->
(
branch_name
)
{
where
(
target_branch:
branch_name
)
}
scope
:preload_source_project
,
->
{
preload
(
:source_project
)
}
scope
:preload_source_project
,
->
{
preload
(
:source_project
)
}
scope
:preload_routables
,
->
do
preload
(
target_project:
[
:route
,
{
namespace: :route
}],
source_project:
[
:route
,
{
namespace: :route
}])
end
scope
:with_auto_merge_enabled
,
->
do
scope
:with_auto_merge_enabled
,
->
do
with_state
(
:opened
).
where
(
auto_merge_enabled:
true
)
with_state
(
:opened
).
where
(
auto_merge_enabled:
true
)
...
...
ee/app/models/merge_train.rb
View file @
42b0158b
...
@@ -71,8 +71,8 @@ class MergeTrain < ApplicationRecord
...
@@ -71,8 +71,8 @@ class MergeTrain < ApplicationRecord
scope
:by_id
,
->
(
sort
=
:asc
)
{
order
(
id:
sort
)
}
scope
:by_id
,
->
(
sort
=
:asc
)
{
order
(
id:
sort
)
}
scope
:preload_api_entities
,
->
do
scope
:preload_api_entities
,
->
do
preload
(
:user
,
merge_request:
MergeRequest
::
PROJECT_ROUTE_AND_NAMESPACE_ROUTE
,
preload
(
:user
,
:merge_request
,
pipeline:
Ci
::
Pipeline
::
PROJECT_ROUTE_AND_NAMESPACE_ROUTE
)
pipeline:
Ci
::
Pipeline
::
PROJECT_ROUTE_AND_NAMESPACE_ROUTE
)
.
merge
(
MergeRequest
.
preload_routables
)
end
end
class
<<
self
class
<<
self
...
...
ee/changelogs/unreleased/219309-fix-mr-n-plus-1.yml
0 → 100644
View file @
42b0158b
---
title
:
Avoid N+1 of merge requests associations in Search
merge_request
:
36712
author
:
type
:
performance
spec/services/search_service_spec.rb
View file @
42b0158b
...
@@ -374,6 +374,19 @@ RSpec.describe SearchService do
...
@@ -374,6 +374,19 @@ RSpec.describe SearchService do
subject
(
:result
)
{
search_service
.
search_objects
}
subject
(
:result
)
{
search_service
.
search_objects
}
shared_examples
"redaction limits N+1 queries"
do
|
limit
:|
it
'does not exceed the query limit'
do
# issuing the query to remove the data loading call
unredacted_results
.
to_a
# only the calls from the redaction are left
query
=
ActiveRecord
::
QueryRecorder
.
new
{
result
}
# these are the project authorization calls, which are not preloaded
expect
(
query
.
count
).
to
be
<=
limit
end
end
def
found_blob
(
project
)
def
found_blob
(
project
)
Gitlab
::
Search
::
FoundBlob
.
new
(
project:
project
)
Gitlab
::
Search
::
FoundBlob
.
new
(
project:
project
)
end
end
...
@@ -427,6 +440,12 @@ RSpec.describe SearchService do
...
@@ -427,6 +440,12 @@ RSpec.describe SearchService do
it
'redacts the inaccessible merge request'
do
it
'redacts the inaccessible merge request'
do
expect
(
result
).
to
contain_exactly
(
readable
)
expect
(
result
).
to
contain_exactly
(
readable
)
end
end
context
'with :with_api_entity_associations'
do
let
(
:unredacted_results
)
{
ar_relation
(
MergeRequest
.
with_api_entity_associations
,
readable
,
unreadable
)
}
it_behaves_like
"redaction limits N+1 queries"
,
limit:
7
end
end
end
context
'project repository blobs'
do
context
'project repository blobs'
do
...
@@ -460,6 +479,10 @@ RSpec.describe SearchService do
...
@@ -460,6 +479,10 @@ RSpec.describe SearchService do
it
'redacts the inaccessible snippet'
do
it
'redacts the inaccessible snippet'
do
expect
(
result
).
to
contain_exactly
(
readable
)
expect
(
result
).
to
contain_exactly
(
readable
)
end
end
context
'with :with_api_entity_associations'
do
it_behaves_like
"redaction limits N+1 queries"
,
limit:
12
end
end
end
context
'personal snippets'
do
context
'personal snippets'
do
...
@@ -471,6 +494,10 @@ RSpec.describe SearchService do
...
@@ -471,6 +494,10 @@ RSpec.describe SearchService do
it
'redacts the inaccessible snippet'
do
it
'redacts the inaccessible snippet'
do
expect
(
result
).
to
contain_exactly
(
readable
)
expect
(
result
).
to
contain_exactly
(
readable
)
end
end
context
'with :with_api_entity_associations'
do
it_behaves_like
"redaction limits N+1 queries"
,
limit:
3
end
end
end
context
'commits'
do
context
'commits'
do
...
...
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