2014-10-08 09:44:25 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe SnippetsFinder do
|
2019-04-09 11:38:58 -04:00
|
|
|
include ExternalAuthorizationServiceHelpers
|
2018-01-18 11:07:06 -05:00
|
|
|
include Gitlab::Allowable
|
2014-10-08 09:44:25 -04:00
|
|
|
|
Rewrite SnippetsFinder to improve performance
This completely rewrites the SnippetsFinder class from the ground up in
order to improve its performance. The old code was beyond salvaging. It
was complex, included various Rails 5 workarounds, comments that
shouldn't be necessary, and most important of all: it produced a really
poorly performing database query.
As a result, I opted for rewriting the finder from scratch, instead of
trying to patch the existing code. Instead of trying to reuse as many
existing methods as possible, I opted for defining new methods
specifically meant for the SnippetsFinder. This requires some extra code
here and there, but allows us to have much more control over the
resulting SQL queries. It is these changes that then allow us to produce
a _much_ more efficient query.
To illustrate how bad the old query was, we will use my own snippets as
an example. Currently I have 52 snippets, most of which are global ones.
To retrieve these, you would run the following Ruby code:
user = User.find_by(username: 'yorickpeterse')
SnippetsFinder.new(user, author: user).execute
On GitLab.com the resulting query will take between 10 and 15 seconds to
run, producing the query plan found at
https://explain.depesz.com/s/Y5IX. Apart from the long execution time,
the total number of buffers (the sum of all shared hits) is around 185
GB, though the real number is probably (hopefully) much lower as I doubt
simply summing these numbers produces the true total number of buffers
used.
The new query's plan can be found at https://explain.depesz.com/s/wHdN,
and this query takes between 10 and 100-ish milliseconds to run. The
total number of buffers used is only about 30 MB.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/52639
2018-10-25 11:35:31 -04:00
|
|
|
describe '#initialize' do
|
|
|
|
it 'raises ArgumentError when a project and author are given' do
|
|
|
|
user = build(:user)
|
|
|
|
project = build(:project)
|
2016-11-26 10:37:26 -05:00
|
|
|
|
Rewrite SnippetsFinder to improve performance
This completely rewrites the SnippetsFinder class from the ground up in
order to improve its performance. The old code was beyond salvaging. It
was complex, included various Rails 5 workarounds, comments that
shouldn't be necessary, and most important of all: it produced a really
poorly performing database query.
As a result, I opted for rewriting the finder from scratch, instead of
trying to patch the existing code. Instead of trying to reuse as many
existing methods as possible, I opted for defining new methods
specifically meant for the SnippetsFinder. This requires some extra code
here and there, but allows us to have much more control over the
resulting SQL queries. It is these changes that then allow us to produce
a _much_ more efficient query.
To illustrate how bad the old query was, we will use my own snippets as
an example. Currently I have 52 snippets, most of which are global ones.
To retrieve these, you would run the following Ruby code:
user = User.find_by(username: 'yorickpeterse')
SnippetsFinder.new(user, author: user).execute
On GitLab.com the resulting query will take between 10 and 15 seconds to
run, producing the query plan found at
https://explain.depesz.com/s/Y5IX. Apart from the long execution time,
the total number of buffers (the sum of all shared hits) is around 185
GB, though the real number is probably (hopefully) much lower as I doubt
simply summing these numbers produces the true total number of buffers
used.
The new query's plan can be found at https://explain.depesz.com/s/wHdN,
and this query takes between 10 and 100-ish milliseconds to run. The
total number of buffers used is only about 30 MB.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/52639
2018-10-25 11:35:31 -04:00
|
|
|
expect { described_class.new(user, author: user, project: project) }
|
|
|
|
.to raise_error(ArgumentError)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
2016-11-26 10:37:26 -05:00
|
|
|
end
|
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
describe '#execute' do
|
|
|
|
set(:user) { create(:user) }
|
|
|
|
set(:private_personal_snippet) { create(:personal_snippet, :private, author: user) }
|
|
|
|
set(:internal_personal_snippet) { create(:personal_snippet, :internal, author: user) }
|
|
|
|
set(:public_personal_snippet) { create(:personal_snippet, :public, author: user) }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
context 'filter by scope' do
|
|
|
|
it "returns all snippets for 'all' scope" do
|
|
|
|
snippets = described_class.new(user, scope: :all).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it "returns all snippets for 'are_private' scope" do
|
|
|
|
snippets = described_class.new(user, scope: :are_private).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it "returns all snippets for 'are_internal' scope" do
|
|
|
|
snippets = described_class.new(user, scope: :are_internal).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(internal_personal_snippet)
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it "returns all snippets for 'are_private' scope" do
|
|
|
|
snippets = described_class.new(user, scope: :are_public).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(public_personal_snippet)
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
context 'filter by author' do
|
|
|
|
it 'returns all public and internal snippets' do
|
|
|
|
snippets = described_class.new(create(:user), author: user).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(internal_personal_snippet, public_personal_snippet)
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns internal snippets' do
|
|
|
|
snippets = described_class.new(user, author: user, scope: :are_internal).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(internal_personal_snippet)
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns private snippets' do
|
|
|
|
snippets = described_class.new(user, author: user, scope: :are_private).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet)
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns public snippets' do
|
|
|
|
snippets = described_class.new(user, author: user, scope: :are_public).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(public_personal_snippet)
|
|
|
|
end
|
2014-10-24 12:24:49 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns all snippets' do
|
|
|
|
snippets = described_class.new(user, author: user).execute
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
|
|
|
|
end
|
Rewrite SnippetsFinder to improve performance
This completely rewrites the SnippetsFinder class from the ground up in
order to improve its performance. The old code was beyond salvaging. It
was complex, included various Rails 5 workarounds, comments that
shouldn't be necessary, and most important of all: it produced a really
poorly performing database query.
As a result, I opted for rewriting the finder from scratch, instead of
trying to patch the existing code. Instead of trying to reuse as many
existing methods as possible, I opted for defining new methods
specifically meant for the SnippetsFinder. This requires some extra code
here and there, but allows us to have much more control over the
resulting SQL queries. It is these changes that then allow us to produce
a _much_ more efficient query.
To illustrate how bad the old query was, we will use my own snippets as
an example. Currently I have 52 snippets, most of which are global ones.
To retrieve these, you would run the following Ruby code:
user = User.find_by(username: 'yorickpeterse')
SnippetsFinder.new(user, author: user).execute
On GitLab.com the resulting query will take between 10 and 15 seconds to
run, producing the query plan found at
https://explain.depesz.com/s/Y5IX. Apart from the long execution time,
the total number of buffers (the sum of all shared hits) is around 185
GB, though the real number is probably (hopefully) much lower as I doubt
simply summing these numbers produces the true total number of buffers
used.
The new query's plan can be found at https://explain.depesz.com/s/wHdN,
and this query takes between 10 and 100-ish milliseconds to run. The
total number of buffers used is only about 30 MB.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/52639
2018-10-25 11:35:31 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns only public snippets if unauthenticated user' do
|
|
|
|
snippets = described_class.new(nil, author: user).execute
|
Rewrite SnippetsFinder to improve performance
This completely rewrites the SnippetsFinder class from the ground up in
order to improve its performance. The old code was beyond salvaging. It
was complex, included various Rails 5 workarounds, comments that
shouldn't be necessary, and most important of all: it produced a really
poorly performing database query.
As a result, I opted for rewriting the finder from scratch, instead of
trying to patch the existing code. Instead of trying to reuse as many
existing methods as possible, I opted for defining new methods
specifically meant for the SnippetsFinder. This requires some extra code
here and there, but allows us to have much more control over the
resulting SQL queries. It is these changes that then allow us to produce
a _much_ more efficient query.
To illustrate how bad the old query was, we will use my own snippets as
an example. Currently I have 52 snippets, most of which are global ones.
To retrieve these, you would run the following Ruby code:
user = User.find_by(username: 'yorickpeterse')
SnippetsFinder.new(user, author: user).execute
On GitLab.com the resulting query will take between 10 and 15 seconds to
run, producing the query plan found at
https://explain.depesz.com/s/Y5IX. Apart from the long execution time,
the total number of buffers (the sum of all shared hits) is around 185
GB, though the real number is probably (hopefully) much lower as I doubt
simply summing these numbers produces the true total number of buffers
used.
The new query's plan can be found at https://explain.depesz.com/s/wHdN,
and this query takes between 10 and 100-ish milliseconds to run. The
total number of buffers used is only about 30 MB.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/52639
2018-10-25 11:35:31 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(public_personal_snippet)
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns all snippets for an admin' do
|
|
|
|
admin = create(:user, :admin)
|
|
|
|
snippets = described_class.new(admin, author: user).execute
|
2018-01-18 11:07:06 -05:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
context 'project snippets' do
|
|
|
|
let(:group) { create(:group, :public) }
|
|
|
|
let(:project) { create(:project, :public, group: group) }
|
|
|
|
let!(:private_project_snippet) { create(:project_snippet, :private, project: project) }
|
|
|
|
let!(:internal_project_snippet) { create(:project_snippet, :internal, project: project) }
|
|
|
|
let!(:public_project_snippet) { create(:project_snippet, :public, project: project) }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns public personal and project snippets for unauthorized user' do
|
|
|
|
snippets = described_class.new(nil, project: project).execute
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(public_project_snippet)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns public and internal snippets for non project members' do
|
|
|
|
snippets = described_class.new(user, project: project).execute
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(internal_project_snippet, public_project_snippet)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns public snippets for non project members' do
|
|
|
|
snippets = described_class.new(user, project: project, scope: :are_public).execute
|
2016-12-09 17:01:08 -05:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(public_project_snippet)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns internal snippets for non project members' do
|
|
|
|
snippets = described_class.new(user, project: project, scope: :are_internal).execute
|
2016-12-09 17:01:08 -05:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(internal_project_snippet)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'does not return private snippets for non project members' do
|
|
|
|
snippets = described_class.new(user, project: project, scope: :are_private).execute
|
2016-12-09 17:01:08 -05:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to be_empty
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns all snippets for project members' do
|
|
|
|
project.add_developer(user)
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
snippets = described_class.new(user, project: project).execute
|
2016-12-09 17:01:08 -05:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(private_project_snippet, internal_project_snippet, public_project_snippet)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns private snippets for project members' do
|
|
|
|
project.add_developer(user)
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
snippets = described_class.new(user, project: project, scope: :are_private).execute
|
Rewrite SnippetsFinder to improve performance
This completely rewrites the SnippetsFinder class from the ground up in
order to improve its performance. The old code was beyond salvaging. It
was complex, included various Rails 5 workarounds, comments that
shouldn't be necessary, and most important of all: it produced a really
poorly performing database query.
As a result, I opted for rewriting the finder from scratch, instead of
trying to patch the existing code. Instead of trying to reuse as many
existing methods as possible, I opted for defining new methods
specifically meant for the SnippetsFinder. This requires some extra code
here and there, but allows us to have much more control over the
resulting SQL queries. It is these changes that then allow us to produce
a _much_ more efficient query.
To illustrate how bad the old query was, we will use my own snippets as
an example. Currently I have 52 snippets, most of which are global ones.
To retrieve these, you would run the following Ruby code:
user = User.find_by(username: 'yorickpeterse')
SnippetsFinder.new(user, author: user).execute
On GitLab.com the resulting query will take between 10 and 15 seconds to
run, producing the query plan found at
https://explain.depesz.com/s/Y5IX. Apart from the long execution time,
the total number of buffers (the sum of all shared hits) is around 185
GB, though the real number is probably (hopefully) much lower as I doubt
simply summing these numbers produces the true total number of buffers
used.
The new query's plan can be found at https://explain.depesz.com/s/wHdN,
and this query takes between 10 and 100-ish milliseconds to run. The
total number of buffers used is only about 30 MB.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/52639
2018-10-25 11:35:31 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(private_project_snippet)
|
|
|
|
end
|
Rewrite SnippetsFinder to improve performance
This completely rewrites the SnippetsFinder class from the ground up in
order to improve its performance. The old code was beyond salvaging. It
was complex, included various Rails 5 workarounds, comments that
shouldn't be necessary, and most important of all: it produced a really
poorly performing database query.
As a result, I opted for rewriting the finder from scratch, instead of
trying to patch the existing code. Instead of trying to reuse as many
existing methods as possible, I opted for defining new methods
specifically meant for the SnippetsFinder. This requires some extra code
here and there, but allows us to have much more control over the
resulting SQL queries. It is these changes that then allow us to produce
a _much_ more efficient query.
To illustrate how bad the old query was, we will use my own snippets as
an example. Currently I have 52 snippets, most of which are global ones.
To retrieve these, you would run the following Ruby code:
user = User.find_by(username: 'yorickpeterse')
SnippetsFinder.new(user, author: user).execute
On GitLab.com the resulting query will take between 10 and 15 seconds to
run, producing the query plan found at
https://explain.depesz.com/s/Y5IX. Apart from the long execution time,
the total number of buffers (the sum of all shared hits) is around 185
GB, though the real number is probably (hopefully) much lower as I doubt
simply summing these numbers produces the true total number of buffers
used.
The new query's plan can be found at https://explain.depesz.com/s/wHdN,
and this query takes between 10 and 100-ish milliseconds to run. The
total number of buffers used is only about 30 MB.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/52639
2018-10-25 11:35:31 -04:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
it 'returns all snippets for an admin' do
|
|
|
|
admin = create(:user, :admin)
|
|
|
|
snippets = described_class.new(admin, project: project).execute
|
2018-01-18 11:07:06 -05:00
|
|
|
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(snippets).to contain_exactly(private_project_snippet, internal_project_snippet, public_project_snippet)
|
|
|
|
end
|
2017-12-11 09:21:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user cannot read cross project' do
|
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).and_call_original
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :read_cross_project) { false }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only personal snippets when the user cannot read cross project' do
|
2019-03-20 05:30:06 -04:00
|
|
|
expect(described_class.new(user).execute).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
|
2017-12-11 09:21:06 -05:00
|
|
|
end
|
|
|
|
end
|
2018-01-18 11:07:06 -05:00
|
|
|
end
|
Rewrite SnippetsFinder to improve performance
This completely rewrites the SnippetsFinder class from the ground up in
order to improve its performance. The old code was beyond salvaging. It
was complex, included various Rails 5 workarounds, comments that
shouldn't be necessary, and most important of all: it produced a really
poorly performing database query.
As a result, I opted for rewriting the finder from scratch, instead of
trying to patch the existing code. Instead of trying to reuse as many
existing methods as possible, I opted for defining new methods
specifically meant for the SnippetsFinder. This requires some extra code
here and there, but allows us to have much more control over the
resulting SQL queries. It is these changes that then allow us to produce
a _much_ more efficient query.
To illustrate how bad the old query was, we will use my own snippets as
an example. Currently I have 52 snippets, most of which are global ones.
To retrieve these, you would run the following Ruby code:
user = User.find_by(username: 'yorickpeterse')
SnippetsFinder.new(user, author: user).execute
On GitLab.com the resulting query will take between 10 and 15 seconds to
run, producing the query plan found at
https://explain.depesz.com/s/Y5IX. Apart from the long execution time,
the total number of buffers (the sum of all shared hits) is around 185
GB, though the real number is probably (hopefully) much lower as I doubt
simply summing these numbers produces the true total number of buffers
used.
The new query's plan can be found at https://explain.depesz.com/s/wHdN,
and this query takes between 10 and 100-ish milliseconds to run. The
total number of buffers used is only about 30 MB.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/52639
2018-10-25 11:35:31 -04:00
|
|
|
|
|
|
|
it_behaves_like 'snippet visibility'
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
context 'external authorization' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let!(:snippet) { create(:project_snippet, :public, project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a finder with external authorization service' do
|
|
|
|
let!(:subject) { create(:project_snippet, project: project) }
|
|
|
|
let(:project_params) { { project: project } }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the result if the external service allows access' do
|
|
|
|
external_service_allow_access(user, project)
|
|
|
|
|
|
|
|
results = described_class.new(user, project: project).execute
|
|
|
|
|
|
|
|
expect(results).to contain_exactly(snippet)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not include any results if the external service denies access' do
|
|
|
|
external_service_deny_access(user, project)
|
|
|
|
|
|
|
|
results = described_class.new(user, project: project).execute
|
|
|
|
|
|
|
|
expect(results).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|