2017-03-14 09:05:45 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-26 12:04:22 +00:00
|
|
|
describe Gitlab::Cache::Ci::ProjectPipelineStatus, :redis do
|
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.
Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.
For project authorizations the logic is split up into two classes:
* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups
Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.
This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:
* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups
Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.
Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.
== On The Fly Refreshing
Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).
This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.
== Spec Changes
This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-04-24 15:19:22 +00:00
|
|
|
let!(:project) { create(:project) }
|
2017-03-14 09:05:45 +00:00
|
|
|
let(:pipeline_status) { described_class.new(project) }
|
2017-04-26 12:04:22 +00:00
|
|
|
let(:cache_key) { "projects/#{project.id}/pipeline_status" }
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
describe '.load_for_project' do
|
|
|
|
it "loads the status" do
|
|
|
|
expect_any_instance_of(described_class).to receive(:load_status)
|
|
|
|
|
|
|
|
described_class.load_for_project(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-26 12:04:22 +00:00
|
|
|
describe 'loading in batches' do
|
|
|
|
let(:status) { 'success' }
|
|
|
|
let(:sha) { '424d1b73bc0d3cb726eb7dc4ce17a4d48552f8c6' }
|
|
|
|
let(:ref) { 'master' }
|
|
|
|
let(:pipeline_info) { { sha: sha, status: status, ref: ref } }
|
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.
Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.
For project authorizations the logic is split up into two classes:
* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups
Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.
This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:
* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups
Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.
Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.
== On The Fly Refreshing
Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).
This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.
== Spec Changes
This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-04-24 15:19:22 +00:00
|
|
|
let!(:project_without_status) { create(:project) }
|
2017-04-26 12:04:22 +00:00
|
|
|
|
|
|
|
describe '.load_in_batch_for_projects' do
|
|
|
|
it 'preloads pipeline_status on projects' do
|
|
|
|
described_class.load_in_batch_for_projects([project])
|
|
|
|
|
|
|
|
# Don't call the accessor that would lazy load the variable
|
|
|
|
expect(project.instance_variable_get('@pipeline_status')).to be_a(described_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'without a status in redis' do
|
|
|
|
it 'loads the status from a commit when it was not in redis' do
|
|
|
|
empty_status = { sha: nil, status: nil, ref: nil }
|
|
|
|
fake_pipeline = described_class.new(
|
|
|
|
project_without_status,
|
|
|
|
pipeline_info: empty_status,
|
|
|
|
loaded_from_cache: false
|
|
|
|
)
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class).to receive(:new)
|
|
|
|
.with(project_without_status,
|
2017-04-26 12:04:22 +00:00
|
|
|
pipeline_info: empty_status,
|
2017-06-21 13:48:12 +00:00
|
|
|
loaded_from_cache: false)
|
|
|
|
.and_return(fake_pipeline)
|
2017-04-26 12:04:22 +00:00
|
|
|
expect(fake_pipeline).to receive(:load_from_project)
|
|
|
|
expect(fake_pipeline).to receive(:store_in_cache)
|
|
|
|
|
|
|
|
described_class.load_in_batch_for_projects([project_without_status])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only connects to redis twice' do
|
|
|
|
# Once to load, once to store in the cache
|
|
|
|
expect(Gitlab::Redis).to receive(:with).exactly(2).and_call_original
|
|
|
|
|
|
|
|
described_class.load_in_batch_for_projects([project_without_status])
|
|
|
|
|
|
|
|
expect(project_without_status.pipeline_status).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when a status was cached in redis' do
|
|
|
|
before do
|
|
|
|
Gitlab::Redis.with do |redis|
|
|
|
|
redis.mapped_hmset(cache_key,
|
|
|
|
{ sha: sha, status: status, ref: ref })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'loads the correct status' do
|
|
|
|
described_class.load_in_batch_for_projects([project])
|
|
|
|
|
|
|
|
pipeline_status = project.instance_variable_get('@pipeline_status')
|
|
|
|
|
|
|
|
expect(pipeline_status.sha).to eq(sha)
|
|
|
|
expect(pipeline_status.status).to eq(status)
|
|
|
|
expect(pipeline_status.ref).to eq(ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only connects to redis once' do
|
|
|
|
expect(Gitlab::Redis).to receive(:with).exactly(1).and_call_original
|
|
|
|
|
|
|
|
described_class.load_in_batch_for_projects([project])
|
|
|
|
|
|
|
|
expect(project.pipeline_status).not_to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't load the status separatly" do
|
|
|
|
expect_any_instance_of(described_class).not_to receive(:load_from_project)
|
|
|
|
expect_any_instance_of(described_class).not_to receive(:load_from_cache)
|
|
|
|
|
|
|
|
described_class.load_in_batch_for_projects([project])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.cached_results_for_projects' do
|
|
|
|
it 'loads a status from redis for all projects' do
|
|
|
|
Gitlab::Redis.with do |redis|
|
|
|
|
redis.mapped_hmset(cache_key, { sha: sha, status: status, ref: ref })
|
|
|
|
end
|
|
|
|
|
|
|
|
result = [{ loaded_from_cache: false, pipeline_info: { sha: nil, status: nil, ref: nil } },
|
|
|
|
{ loaded_from_cache: true, pipeline_info: pipeline_info }]
|
|
|
|
|
|
|
|
expect(described_class.cached_results_for_projects([project_without_status, project])).to eq(result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-31 13:47:34 +00:00
|
|
|
describe '.update_for_pipeline' do
|
|
|
|
it 'refreshes the cache if nescessary' do
|
2017-04-26 12:04:22 +00:00
|
|
|
pipeline = build_stubbed(:ci_pipeline,
|
|
|
|
sha: '123456', status: 'success', ref: 'master')
|
2017-03-31 13:47:34 +00:00
|
|
|
fake_status = double
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class).to receive(:new)
|
|
|
|
.with(pipeline.project,
|
2017-04-26 12:04:22 +00:00
|
|
|
pipeline_info: {
|
|
|
|
sha: '123456', status: 'success', ref: 'master'
|
2017-06-21 13:48:12 +00:00
|
|
|
})
|
|
|
|
.and_return(fake_status)
|
2017-03-31 13:47:34 +00:00
|
|
|
|
|
|
|
expect(fake_status).to receive(:store_in_cache_if_needed)
|
|
|
|
|
|
|
|
described_class.update_for_pipeline(pipeline)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-14 09:05:45 +00:00
|
|
|
describe '#has_status?' do
|
|
|
|
it "is false when the status wasn't loaded yet" do
|
|
|
|
expect(pipeline_status.has_status?).to be_falsy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is true when all status information was loaded' do
|
|
|
|
fake_commit = double
|
|
|
|
allow(fake_commit).to receive(:status).and_return('failed')
|
|
|
|
allow(fake_commit).to receive(:sha).and_return('failed424d1b73bc0d3cb726eb7dc4ce17a4d48552f8c6')
|
|
|
|
allow(pipeline_status).to receive(:commit).and_return(fake_commit)
|
|
|
|
allow(pipeline_status).to receive(:has_cache?).and_return(false)
|
|
|
|
|
|
|
|
pipeline_status.load_status
|
|
|
|
|
|
|
|
expect(pipeline_status.has_status?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#load_status' do
|
|
|
|
it 'loads the status from the cache when there is one' do
|
|
|
|
expect(pipeline_status).to receive(:has_cache?).and_return(true)
|
|
|
|
expect(pipeline_status).to receive(:load_from_cache)
|
|
|
|
|
|
|
|
pipeline_status.load_status
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'loads the status from the project commit when there is no cache' do
|
|
|
|
allow(pipeline_status).to receive(:has_cache?).and_return(false)
|
|
|
|
|
2017-04-06 12:20:29 +00:00
|
|
|
expect(pipeline_status).to receive(:load_from_project)
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
pipeline_status.load_status
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores the status in the cache when it loading it from the project' do
|
|
|
|
allow(pipeline_status).to receive(:has_cache?).and_return(false)
|
2017-04-06 12:20:29 +00:00
|
|
|
allow(pipeline_status).to receive(:load_from_project)
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
expect(pipeline_status).to receive(:store_in_cache)
|
|
|
|
|
|
|
|
pipeline_status.load_status
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the state to loaded' do
|
|
|
|
pipeline_status.load_status
|
|
|
|
|
|
|
|
expect(pipeline_status).to be_loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only loads the status once' do
|
|
|
|
expect(pipeline_status).to receive(:has_cache?).and_return(true).exactly(1)
|
|
|
|
expect(pipeline_status).to receive(:load_from_cache).exactly(1)
|
|
|
|
|
|
|
|
pipeline_status.load_status
|
|
|
|
pipeline_status.load_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-06 12:20:29 +00:00
|
|
|
describe "#load_from_project" do
|
2017-03-14 09:05:45 +00:00
|
|
|
let!(:pipeline) { create(:ci_pipeline, :success, project: project, sha: project.commit.sha) }
|
|
|
|
|
|
|
|
it 'reads the status from the pipeline for the commit' do
|
2017-04-06 12:20:29 +00:00
|
|
|
pipeline_status.load_from_project
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
expect(pipeline_status.status).to eq('success')
|
|
|
|
expect(pipeline_status.sha).to eq(project.commit.sha)
|
2017-04-06 12:20:29 +00:00
|
|
|
expect(pipeline_status.ref).to eq(project.default_branch)
|
2017-03-14 09:05:45 +00:00
|
|
|
end
|
2017-03-14 19:46:34 +00:00
|
|
|
|
|
|
|
it "doesn't fail for an empty project" do
|
|
|
|
status_for_empty_commit = described_class.new(create(:empty_project))
|
|
|
|
|
|
|
|
status_for_empty_commit.load_status
|
|
|
|
|
|
|
|
expect(status_for_empty_commit).to be_loaded
|
|
|
|
end
|
2017-03-14 09:05:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#store_in_cache", :redis do
|
|
|
|
it "sets the object in redis" do
|
|
|
|
pipeline_status.sha = '123456'
|
|
|
|
pipeline_status.status = 'failed'
|
|
|
|
|
|
|
|
pipeline_status.store_in_cache
|
2017-04-26 12:04:22 +00:00
|
|
|
read_sha, read_status = Gitlab::Redis.with { |redis| redis.hmget(cache_key, :sha, :status) }
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
expect(read_sha).to eq('123456')
|
|
|
|
expect(read_status).to eq('failed')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#store_in_cache_if_needed', :redis do
|
|
|
|
it 'stores the state in the cache when the sha is the HEAD of the project' do
|
|
|
|
create(:ci_pipeline, :success, project: project, sha: project.commit.sha)
|
2017-04-26 12:04:22 +00:00
|
|
|
pipeline_status = described_class.load_for_project(project)
|
2017-03-14 09:05:45 +00:00
|
|
|
|
2017-04-26 12:04:22 +00:00
|
|
|
pipeline_status.store_in_cache_if_needed
|
|
|
|
sha, status, ref = Gitlab::Redis.with { |redis| redis.hmget(cache_key, :sha, :status, :ref) }
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
expect(sha).not_to be_nil
|
|
|
|
expect(status).not_to be_nil
|
2017-04-06 12:20:29 +00:00
|
|
|
expect(ref).not_to be_nil
|
2017-03-14 09:05:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't store the status in redis when the sha is not the head of the project" do
|
2017-04-26 12:04:22 +00:00
|
|
|
other_status = described_class.new(
|
|
|
|
project,
|
|
|
|
pipeline_info: { sha: "123456", status: "failed" }
|
|
|
|
)
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
other_status.store_in_cache_if_needed
|
2017-04-26 12:04:22 +00:00
|
|
|
sha, status = Gitlab::Redis.with { |redis| redis.hmget(cache_key, :sha, :status) }
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
expect(sha).to be_nil
|
|
|
|
expect(status).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the cache if the repository doesn't have a head commit" do
|
|
|
|
empty_project = create(:empty_project)
|
2017-04-26 12:04:22 +00:00
|
|
|
Gitlab::Redis.with do |redis|
|
|
|
|
redis.mapped_hmset(cache_key,
|
|
|
|
{ sha: 'sha', status: 'pending', ref: 'master' })
|
|
|
|
end
|
|
|
|
|
|
|
|
other_status = described_class.new(empty_project,
|
|
|
|
pipeline_info: {
|
|
|
|
sha: "123456", status: "failed"
|
|
|
|
})
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
other_status.store_in_cache_if_needed
|
2017-04-26 12:04:22 +00:00
|
|
|
sha, status, ref = Gitlab::Redis.with { |redis| redis.hmget("projects/#{empty_project.id}/pipeline_status", :sha, :status, :ref) }
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
expect(sha).to be_nil
|
|
|
|
expect(status).to be_nil
|
2017-04-06 12:20:29 +00:00
|
|
|
expect(ref).to be_nil
|
2017-03-14 09:05:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with a status in redis", :redis do
|
|
|
|
let(:status) { 'success' }
|
|
|
|
let(:sha) { '424d1b73bc0d3cb726eb7dc4ce17a4d48552f8c6' }
|
2017-04-26 12:04:22 +00:00
|
|
|
let(:ref) { 'master' }
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
before do
|
2017-04-26 12:04:22 +00:00
|
|
|
Gitlab::Redis.with do |redis|
|
|
|
|
redis.mapped_hmset(cache_key,
|
|
|
|
{ sha: sha, status: status, ref: ref })
|
|
|
|
end
|
2017-03-14 09:05:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#load_from_cache' do
|
|
|
|
it 'reads the status from redis' do
|
|
|
|
pipeline_status.load_from_cache
|
|
|
|
|
|
|
|
expect(pipeline_status.sha).to eq(sha)
|
|
|
|
expect(pipeline_status.status).to eq(status)
|
2017-04-26 12:04:22 +00:00
|
|
|
expect(pipeline_status.ref).to eq(ref)
|
2017-03-14 09:05:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#has_cache?' do
|
|
|
|
it 'knows the status is cached' do
|
|
|
|
expect(pipeline_status.has_cache?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#delete_from_cache' do
|
|
|
|
it 'deletes values from redis' do
|
|
|
|
pipeline_status.delete_from_cache
|
|
|
|
|
2017-04-26 12:04:22 +00:00
|
|
|
key_exists = Gitlab::Redis.with { |redis| redis.exists(cache_key) }
|
2017-03-14 09:05:45 +00:00
|
|
|
|
|
|
|
expect(key_exists).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|