Commit dafd7ccc authored by David Fernandez's avatar David Fernandez

Merge branch '357094-fix-capacity-phase2' into 'master'

Update Migration::EnqueuerWorker capacity logic

See merge request gitlab-org/gitlab!83901
parents b3f56e02 559fe188
...@@ -76,7 +76,7 @@ module ContainerRegistry ...@@ -76,7 +76,7 @@ module ContainerRegistry
end end
def below_capacity? def below_capacity?
current_capacity <= maximum_capacity current_capacity < maximum_capacity
end end
def waiting_time_passed? def waiting_time_passed?
...@@ -111,11 +111,9 @@ module ContainerRegistry ...@@ -111,11 +111,9 @@ module ContainerRegistry
end end
def current_capacity def current_capacity
strong_memoize(:current_capacity) do ContainerRepository.with_migration_states(
ContainerRepository.with_migration_states( %w[pre_importing pre_import_done importing]
%w[pre_importing pre_import_done importing] ).count
).count
end
end end
def maximum_capacity def maximum_capacity
...@@ -145,7 +143,7 @@ module ContainerRegistry ...@@ -145,7 +143,7 @@ module ContainerRegistry
end end
def re_enqueue_if_capacity def re_enqueue_if_capacity
return unless current_capacity < maximum_capacity return unless below_capacity?
self.class.perform_async self.class.perform_async
end end
......
...@@ -6,6 +6,8 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures ...@@ -6,6 +6,8 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures
include ExclusiveLeaseHelpers include ExclusiveLeaseHelpers
let_it_be_with_reload(:container_repository) { create(:container_repository, created_at: 2.days.ago) } let_it_be_with_reload(:container_repository) { create(:container_repository, created_at: 2.days.ago) }
let_it_be(:importing_repository) { create(:container_repository, :importing) }
let_it_be(:pre_importing_repository) { create(:container_repository, :pre_importing) }
let(:worker) { described_class.new } let(:worker) { described_class.new }
...@@ -26,10 +28,10 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures ...@@ -26,10 +28,10 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures
end end
end end
shared_examples 're-enqueuing based on capacity' do shared_examples 're-enqueuing based on capacity' do |capacity_limit: 4|
context 'below capacity' do context 'below capacity' do
before do before do
allow(ContainerRegistry::Migration).to receive(:capacity).and_return(9999) allow(ContainerRegistry::Migration).to receive(:capacity).and_return(capacity_limit)
end end
it 're-enqueues the worker' do it 're-enqueues the worker' do
...@@ -53,14 +55,16 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures ...@@ -53,14 +55,16 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures
end end
context 'with qualified repository' do context 'with qualified repository' do
it 'starts the pre-import for the next qualified repository' do before do
method = worker.method(:next_repository) method = worker.method(:next_repository)
allow(worker).to receive(:next_repository) do allow(worker).to receive(:next_repository) do
next_qualified_repository = method.call next_qualified_repository = method.call
allow(next_qualified_repository).to receive(:migration_pre_import).and_return(:ok) allow(next_qualified_repository).to receive(:migration_pre_import).and_return(:ok)
next_qualified_repository next_qualified_repository
end end
end
it 'starts the pre-import for the next qualified repository' do
expect_log_extra_metadata( expect_log_extra_metadata(
import_type: 'next', import_type: 'next',
container_repository_id: container_repository.id, container_repository_id: container_repository.id,
...@@ -73,6 +77,24 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures ...@@ -73,6 +77,24 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures
expect(container_repository.reload).to be_pre_importing expect(container_repository.reload).to be_pre_importing
end end
context 'when the new pre-import maxes out the capacity' do
before do
# set capacity to 10
stub_feature_flags(
container_registry_migration_phase2_capacity_25: false
)
# Plus 2 created above gives 9 importing repositories
create_list(:container_repository, 7, :importing)
end
it 'does not re-enqueue the worker' do
expect(described_class).not_to receive(:perform_async)
subject
end
end
it_behaves_like 're-enqueuing based on capacity' it_behaves_like 're-enqueuing based on capacity'
end end
...@@ -124,29 +146,33 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures ...@@ -124,29 +146,33 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures
context 'when an aborted import is available' do context 'when an aborted import is available' do
let_it_be(:aborted_repository) { create(:container_repository, :import_aborted) } let_it_be(:aborted_repository) { create(:container_repository, :import_aborted) }
it 'retries the import for the aborted repository' do context 'with a successful registry request' do
method = worker.method(:next_aborted_repository) before do
allow(worker).to receive(:next_aborted_repository) do method = worker.method(:next_aborted_repository)
next_aborted_repository = method.call allow(worker).to receive(:next_aborted_repository) do
allow(next_aborted_repository).to receive(:migration_import).and_return(:ok) next_aborted_repository = method.call
allow(next_aborted_repository.gitlab_api_client).to receive(:import_status).and_return('import_failed') allow(next_aborted_repository).to receive(:migration_import).and_return(:ok)
next_aborted_repository allow(next_aborted_repository.gitlab_api_client).to receive(:import_status).and_return('import_failed')
next_aborted_repository
end
end end
expect_log_extra_metadata( it 'retries the import for the aborted repository' do
import_type: 'retry', expect_log_extra_metadata(
container_repository_id: aborted_repository.id, import_type: 'retry',
container_repository_path: aborted_repository.path, container_repository_id: aborted_repository.id,
container_repository_migration_state: 'importing' container_repository_path: aborted_repository.path,
) container_repository_migration_state: 'importing'
)
subject subject
expect(aborted_repository.reload).to be_importing expect(aborted_repository.reload).to be_importing
expect(container_repository.reload).to be_default expect(container_repository.reload).to be_default
end end
it_behaves_like 're-enqueuing based on capacity' it_behaves_like 're-enqueuing based on capacity'
end
context 'when an error occurs' do context 'when an error occurs' do
it 'does not abort that migration' do it 'does not abort that migration' do
...@@ -204,7 +230,7 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures ...@@ -204,7 +230,7 @@ RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures
expect(container_repository.migration_skipped_at).not_to be_nil expect(container_repository.migration_skipped_at).not_to be_nil
end end
it_behaves_like 're-enqueuing based on capacity' it_behaves_like 're-enqueuing based on capacity', capacity_limit: 3
end end
context 'when an error occurs' do context 'when an error occurs' do
......
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