Commit bba3204b authored by Harsh Chouraria's avatar Harsh Chouraria

Fix shared runner minutes reset on self-managed

When namespace count is lower than 100,000,
the :ci_parallel_minutes_reset feature fails
with a zero division error due to integer division

This affects most self-managed instances but not
GitLab.com which has well over 100,000 namespaces

Closes: https://gitlab.com/gitlab-org/gitlab/-/issues/330068

Changelog: fixed
EE: true
parent 83ecd3c1
......@@ -20,7 +20,11 @@ class ClearSharedRunnersMinutesWorker # rubocop:disable Scalability/IdempotentWo
start_id = Namespace.minimum(:id)
last_id = Namespace.maximum(:id)
execution_offset = TIME_SPREAD / ((last_id - start_id) / BATCH_SIZE)
# Uses float division to avoid a ZeroDivisionError
# when namespace ID range is lower than BATCH_SIZE
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/330068
batches = (last_id - start_id).fdiv(BATCH_SIZE)
execution_offset = (TIME_SPREAD / batches).to_i
(start_id..last_id).step(BATCH_SIZE).with_index do |batch_start_id, batch_index|
batch_end_id = batch_start_id + BATCH_SIZE - 1
......
......@@ -133,7 +133,7 @@ RSpec.describe ClearSharedRunnersMinutesWorker do
end
end
context 'when ci_parallel_minutes_reset feature flag is enabled' do
context 'when ci_parallel_minutes_reset feature flag is enabled, with batch size lower than namespace count' do
subject { worker.perform }
before do
......@@ -143,6 +143,7 @@ RSpec.describe ClearSharedRunnersMinutesWorker do
create(:namespace, id: id)
end
# Test with a batch size lower than count of namespaces
stub_const("#{described_class}::BATCH_SIZE", 3)
end
......@@ -157,5 +158,25 @@ RSpec.describe ClearSharedRunnersMinutesWorker do
subject
end
end
context 'when ci_parallel_minutes_reset feature flag is enabled, with batch size higher than namespace count' do
subject { worker.perform }
before do
stub_feature_flags(ci_parallel_minutes_reset: true)
[2, 3, 4, 5, 7, 8, 10, 14].each do |id|
create(:namespace, id: id)
end
# Use the default BATCH_SIZE (100_000)
end
it 'runs the worker in a single batch', :aggregate_failures do
# Runs a single batch, immediately
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(0.seconds, 2, 100_001)
subject
end
end
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