Commit 3b3d28e0 authored by Sean McGivern's avatar Sean McGivern

Add more accurate way of counting background migrations

Sidekiq::Queue only counts currently enqueued jobs. For background
migrations, most jobs are instead scheduled and therefore in
Sidekiq::ScheduledSet. This is because if we enqueued them all, we'd
process them as fast as Sidekiq could pick them up, which could:

1. Block other jobs from processing.
2. Overwhelm the database.
parent 18d0f8fe
---
title: Add more accurate way of counting remaining background migrations before upgrading
merge_request:
author:
type: fixed
...@@ -116,14 +116,14 @@ following command: ...@@ -116,14 +116,14 @@ following command:
**For Omnibus installations** **For Omnibus installations**
```shell ```shell
sudo gitlab-rails runner -e production 'puts Sidekiq::Queue.new("background_migration").size' sudo gitlab-rails runner -e production 'puts Gitlab::BackgroundMigration.remaining'
``` ```
**For installations from source** **For installations from source**
``` ```
cd /home/git/gitlab cd /home/git/gitlab
sudo -u git -H bundle exec rails runner -e production 'puts Sidekiq::Queue.new("background_migration").size' sudo -u git -H bundle exec rails runner -e production 'puts Gitlab::BackgroundMigration.remaining'
``` ```
## Upgrading to a new major version ## Upgrading to a new major version
......
...@@ -58,6 +58,14 @@ module Gitlab ...@@ -58,6 +58,14 @@ module Gitlab
migration_class_for(class_name).new.perform(*arguments) migration_class_for(class_name).new.perform(*arguments)
end end
def self.remaining
scheduled = Sidekiq::ScheduledSet.new.count do |job|
job.queue == self.queue
end
scheduled + Sidekiq::Queue.new(self.queue).size
end
def self.exists?(migration_class, additional_queues = []) def self.exists?(migration_class, additional_queues = [])
enqueued = Sidekiq::Queue.new(self.queue) enqueued = Sidekiq::Queue.new(self.queue)
scheduled = Sidekiq::ScheduledSet.new scheduled = Sidekiq::ScheduledSet.new
......
...@@ -165,6 +165,32 @@ describe Gitlab::BackgroundMigration do ...@@ -165,6 +165,32 @@ describe Gitlab::BackgroundMigration do
end end
end end
describe '.remaining', :redis do
context 'when there are jobs remaining' do
let(:queue) { Array.new(12) }
before do
allow(Sidekiq::Queue).to receive(:new)
.with(described_class.queue)
.and_return(Array.new(12))
Sidekiq::Testing.disable! do
BackgroundMigrationWorker.perform_in(10.minutes, 'Foo')
end
end
it 'returns the enqueued jobs plus the scheduled jobs' do
expect(described_class.remaining).to eq(13)
end
end
context 'when there are no jobs remaining' do
it 'returns zero' do
expect(described_class.remaining).to be_zero
end
end
end
describe '.exists?' do describe '.exists?' do
context 'when there are enqueued jobs present' do context 'when there are enqueued jobs present' do
let(:queue) do let(:queue) 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