2019-03-30 03:15:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-02 11:12:36 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe BackgroundMigrationWorker, :clean_gitlab_redis_shared_state do
|
2018-01-04 10:49:15 -05:00
|
|
|
let(:worker) { described_class.new }
|
|
|
|
|
2018-07-19 11:16:47 -04:00
|
|
|
describe '.minimum_interval' do
|
|
|
|
it 'returns 2 minutes' do
|
|
|
|
expect(described_class.minimum_interval).to eq(2.minutes.to_i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-19 13:09:10 -05:00
|
|
|
describe '#perform' do
|
2020-11-06 16:08:57 -05:00
|
|
|
before do
|
|
|
|
allow(worker).to receive(:jid).and_return(1)
|
|
|
|
expect(worker).to receive(:always_perform?).and_return(false)
|
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
context 'when lease can be obtained' do
|
|
|
|
before do
|
|
|
|
expect(Gitlab::BackgroundMigration)
|
|
|
|
.to receive(:perform)
|
|
|
|
.with('Foo', [10, 20])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'performs a background migration' do
|
|
|
|
worker.perform('Foo', [10, 20])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when lease_attempts is 1' do
|
|
|
|
it 'performs a background migration' do
|
|
|
|
worker.perform('Foo', [10, 20], 1)
|
|
|
|
end
|
|
|
|
end
|
2018-01-04 10:49:15 -05:00
|
|
|
end
|
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
context 'when lease not obtained (migration of same class was performed recently)' do
|
|
|
|
before do
|
|
|
|
expect(Gitlab::BackgroundMigration).not_to receive(:perform)
|
|
|
|
|
|
|
|
worker.lease_for('Foo').try_obtain
|
|
|
|
end
|
2018-01-04 10:49:15 -05:00
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
it 'reschedules the migration and decrements the lease_attempts' do
|
|
|
|
expect(described_class)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.with(a_kind_of(Numeric), 'Foo', [10, 20], 4)
|
2018-01-04 10:49:15 -05:00
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
worker.perform('Foo', [10, 20], 5)
|
|
|
|
end
|
2018-01-04 10:49:15 -05:00
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
context 'when lease_attempts is 1' do
|
|
|
|
it 'reschedules the migration and decrements the lease_attempts' do
|
|
|
|
expect(described_class)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.with(a_kind_of(Numeric), 'Foo', [10, 20], 0)
|
2018-01-04 10:49:15 -05:00
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
worker.perform('Foo', [10, 20], 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when lease_attempts is 0' do
|
|
|
|
it 'gives up performing the migration' do
|
|
|
|
expect(described_class).not_to receive(:perform_in)
|
|
|
|
expect(Sidekiq.logger).to receive(:warn).with(
|
|
|
|
class: 'Foo',
|
|
|
|
message: 'Job could not get an exclusive lease after several tries. Giving up.',
|
|
|
|
job_id: 1)
|
|
|
|
|
|
|
|
worker.perform('Foo', [10, 20], 0)
|
|
|
|
end
|
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
end
|
2018-07-19 11:16:47 -04:00
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
context 'when database is not healthy' do
|
|
|
|
before do
|
|
|
|
allow(worker).to receive(:healthy_database?).and_return(false)
|
|
|
|
end
|
2018-07-19 11:16:47 -04:00
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
it 'reschedules a migration if the database is not healthy' do
|
|
|
|
expect(described_class)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.with(a_kind_of(Numeric), 'Foo', [10, 20], 4)
|
2018-07-19 11:16:47 -04:00
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
worker.perform('Foo', [10, 20])
|
|
|
|
end
|
2018-07-19 11:16:47 -04:00
|
|
|
|
2020-11-06 16:08:57 -05:00
|
|
|
context 'when lease_attempts is 0' do
|
|
|
|
it 'gives up performing the migration' do
|
|
|
|
expect(described_class).not_to receive(:perform_in)
|
|
|
|
expect(Sidekiq.logger).to receive(:warn).with(
|
|
|
|
class: 'Foo',
|
|
|
|
message: 'Database was unhealthy after several tries. Giving up.',
|
|
|
|
job_id: 1)
|
|
|
|
|
|
|
|
worker.perform('Foo', [10, 20], 0)
|
|
|
|
end
|
|
|
|
end
|
2018-07-19 11:16:47 -04:00
|
|
|
end
|
2020-02-19 13:09:10 -05:00
|
|
|
|
|
|
|
it 'sets the class that will be executed as the caller_id' do
|
|
|
|
expect(Gitlab::BackgroundMigration).to receive(:perform) do
|
|
|
|
expect(Labkit::Context.current.to_h).to include('meta.caller_id' => 'Foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
worker.perform('Foo', [10, 20])
|
|
|
|
end
|
2018-07-19 11:16:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#healthy_database?' do
|
2019-06-13 09:12:28 -04:00
|
|
|
context 'when replication lag is too great' do
|
|
|
|
it 'returns false' do
|
|
|
|
allow(Postgresql::ReplicationSlot)
|
|
|
|
.to receive(:lag_too_great?)
|
|
|
|
.and_return(true)
|
2018-07-19 11:16:47 -04:00
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
expect(worker.healthy_database?).to eq(false)
|
2018-07-19 11:16:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when replication lag is small enough' do
|
|
|
|
it 'returns true' do
|
|
|
|
allow(Postgresql::ReplicationSlot)
|
|
|
|
.to receive(:lag_too_great?)
|
|
|
|
.and_return(false)
|
|
|
|
|
|
|
|
expect(worker.healthy_database?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
end
|
|
|
|
end
|