Introuce linear root method in UpdateAllMirrorsWorker

In this commit we're introducing the new linear root method
in the UpdateAllMirrorsWorker behind a feature flag.
parent 0ecee668
---
name: linear_mirrors_worker_roots
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/76735
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/348415
milestone: '14.7'
type: development
group: group::authentication and authorization
default_enabled: false
......@@ -117,12 +117,6 @@ class UpdateAllMirrorsWorker # rubocop:disable Scalability/IdempotentWorker
relation = relation.where('import_state.next_execution_timestamp > ?', offset_at) if offset_at
if check_mirror_plans_in_query?
root_namespaces_sql = Gitlab::ObjectHierarchy
.new(Namespace.where('id = projects.namespace_id'))
.roots
.select(:id)
.to_sql
root_namespaces_join = "INNER JOIN namespaces AS root_namespaces ON root_namespaces.id = (#{root_namespaces_sql})"
relation = relation
......@@ -147,4 +141,19 @@ class UpdateAllMirrorsWorker # rubocop:disable Scalability/IdempotentWorker
def check_mirror_plans_in_query?
::Gitlab::CurrentSettings.should_check_namespace_plan?
end
# rubocop: disable CodeReuse/ActiveRecord
def root_namespaces_sql
namespace = Namespace.where('id = projects.namespace_id')
if Feature.enabled?(:linear_mirrors_worker_roots, default_enabled: :yaml)
namespace.roots.as_ids
else
Gitlab::ObjectHierarchy
.new(namespace)
.roots
.select(:id)
end.to_sql
end
# rubocop: enable CodeReuse/ActiveRecord
end
......@@ -187,28 +187,40 @@ RSpec.describe UpdateAllMirrorsWorker do
let(:unlicensed_projects) { [unlicensed_project1, unlicensed_project2, unlicensed_project3, unlicensed_project4] }
context 'when using SQL to filter projects' do
before do
allow(subject).to receive(:check_mirror_plans_in_query?).and_return(true)
end
shared_examples 'examples checking namespace plans' do
before do
allow(subject).to receive(:check_mirror_plans_in_query?).and_return(true)
end
context 'when capacity is in excess' do
it 'schedules all available mirrors' do
schedule_mirrors!(capacity: 4)
context 'when capacity is in excess' do
it 'schedules all available mirrors' do
schedule_mirrors!(capacity: 4)
expect_import_scheduled(licensed_project1, licensed_project2, public_project)
expect_import_not_scheduled(*unlicensed_projects)
expect_import_scheduled(licensed_project1, licensed_project2, public_project)
expect_import_not_scheduled(*unlicensed_projects)
end
end
context 'when capacity is exactly sufficient' do
it 'does not include unlicensed non-public projects in batches' do
# We expect that all three eligible projects will be
# included in the first batch because the query will only
# return eligible projects.
expect(subject).to receive(:pull_mirrors_batch).with(hash_including(batch_size: 6)).and_call_original.once
schedule_mirrors!(capacity: 3)
end
end
end
context 'when capacity is exactly sufficient' do
it 'does not include unlicensed non-public projects in batches' do
# We expect that all three eligible projects will be
# included in the first batch because the query will only
# return eligible projects.
expect(subject).to receive(:pull_mirrors_batch).with(hash_including(batch_size: 6)).and_call_original.once
it_behaves_like 'examples checking namespace plans'
schedule_mirrors!(capacity: 3)
context 'when feature flag ' do
before do
stub_feature_flags(linear_mirrors_worker_roots: false)
end
it_behaves_like 'examples checking namespace plans'
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment