Commit f682052a authored by Dmitry Gruzd's avatar Dmitry Gruzd

Fix Elastic::MigrationWorker current_migration

Changelog: fixed
EE: true
parent 2c1838e7
...@@ -87,9 +87,9 @@ module Elastic ...@@ -87,9 +87,9 @@ module Elastic
end end
def current_migration def current_migration
completed_migrations = Elastic::MigrationRecord.load_versions(completed: true) uncompleted_migrations = Elastic::MigrationRecord.load_versions(completed: false)
Elastic::DataMigrationService.migrations.find { |migration| !completed_migrations.include?(migration.version) } Elastic::DataMigrationService.migrations.find { |migration| uncompleted_migrations.include?(migration.version) }
end end
def pause_indexing!(migration) def pause_indexing!(migration)
......
...@@ -22,168 +22,184 @@ RSpec.describe Elastic::MigrationWorker, :elastic do ...@@ -22,168 +22,184 @@ RSpec.describe Elastic::MigrationWorker, :elastic do
before do before do
stub_ee_application_setting(elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_indexing: true)
allow(subject).to receive(:current_migration).and_return(migration)
end end
it 'creates an index if it does not exist' do context 'an unexecuted migration present' do
Gitlab::Elastic::Helper.default.delete_migrations_index
expect { subject.perform }.to change { Gitlab::Elastic::Helper.default.migrations_index_exists? }.from(false).to(true)
end
context 'no unexecuted migrations' do
before do before do
allow(subject).to receive(:current_migration).and_return(nil) allow(subject).to receive(:current_migration).and_return(migration)
end end
it 'skips execution' do it 'creates an index if it does not exist' do
expect(subject).not_to receive(:execute_migration) Gitlab::Elastic::Helper.default.delete_migrations_index
expect(subject.perform).to be_falsey expect { subject.perform }.to change { Gitlab::Elastic::Helper.default.migrations_index_exists? }.from(false).to(true)
end end
end
context 'migration is halted' do context 'migration is halted' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:pause_indexing, :halted_indexing_unpaused, :unpause) do where(:pause_indexing, :halted_indexing_unpaused, :unpause) do
false | false | false false | false | false
false | true | false false | true | false
true | false | true true | false | true
true | true | false true | true | false
end
with_them do
before do
allow(Gitlab::CurrentSettings).to receive(:elasticsearch_pause_indexing?).and_return(true)
allow(migration).to receive(:pause_indexing?).and_return(true)
migration.save_state!(halted: true, pause_indexing: pause_indexing, halted_indexing_unpaused: halted_indexing_unpaused)
end end
it 'unpauses indexing' do with_them do
if unpause before do
expect(Gitlab::CurrentSettings).to receive(:update!).with(elasticsearch_pause_indexing: false) allow(Gitlab::CurrentSettings).to receive(:elasticsearch_pause_indexing?).and_return(true)
else allow(migration).to receive(:pause_indexing?).and_return(true)
expect(Gitlab::CurrentSettings).not_to receive(:update!) migration.save_state!(halted: true, pause_indexing: pause_indexing, halted_indexing_unpaused: halted_indexing_unpaused)
end end
expect(migration).not_to receive(:migrate) it 'unpauses indexing' do
if unpause
expect(Gitlab::CurrentSettings).to receive(:update!).with(elasticsearch_pause_indexing: false)
else
expect(Gitlab::CurrentSettings).not_to receive(:update!)
end
subject.perform expect(migration).not_to receive(:migrate)
subject.perform
end
end end
end end
end
context 'migration process' do context 'migration process' do
before do before do
allow(migration).to receive(:started?).and_return(started) allow(migration).to receive(:started?).and_return(started)
allow(migration).to receive(:completed?).and_return(completed) allow(migration).to receive(:completed?).and_return(completed)
allow(migration).to receive(:batched?).and_return(batched) allow(migration).to receive(:batched?).and_return(batched)
end end
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
# completed is evaluated after migrate method is executed # completed is evaluated after migrate method is executed
where(:started, :completed, :execute_migration, :batched) do where(:started, :completed, :execute_migration, :batched) do
false | false | true | false false | false | true | false
false | true | true | false false | true | true | false
false | false | true | true false | false | true | true
false | true | true | true false | true | true | true
true | false | false | false true | false | false | false
true | true | false | false true | true | false | false
true | false | true | true true | false | true | true
true | true | true | true true | true | true | true
end end
with_them do with_them do
it 'calls migration only when needed', :aggregate_failures do it 'calls migration only when needed', :aggregate_failures do
if execute_migration if execute_migration
expect(migration).to receive(:migrate).once expect(migration).to receive(:migrate).once
else else
expect(migration).not_to receive(:migrate) expect(migration).not_to receive(:migrate)
end
expect(migration).to receive(:save!).with(completed: completed)
expect(Elastic::DataMigrationService).to receive(:drop_migration_has_finished_cache!).with(migration)
subject.perform
end end
expect(migration).to receive(:save!).with(completed: completed) it 'handles batched migrations' do
expect(Elastic::DataMigrationService).to receive(:drop_migration_has_finished_cache!).with(migration) if batched && !completed
# default throttle_delay is 5.minutes
expect( Elastic::MigrationWorker).to receive(:perform_in)
.with(5.minutes)
else
expect( Elastic::MigrationWorker).not_to receive(:perform_in)
end
subject.perform subject.perform
end
end end
it 'handles batched migrations' do context 'indexing pause' do
if batched && !completed before do
# default throttle_delay is 5.minutes allow(migration).to receive(:pause_indexing?).and_return(true)
expect( Elastic::MigrationWorker).to receive(:perform_in)
.with(5.minutes)
else
expect( Elastic::MigrationWorker).not_to receive(:perform_in)
end end
subject.perform let(:batched) { true }
end
end
context 'indexing pause' do where(:started, :completed, :expected) do
before do false | false | false
allow(migration).to receive(:pause_indexing?).and_return(true) true | false | false
end true | true | true
end
let(:batched) { true } with_them do
it 'pauses and unpauses indexing' do
expect(Gitlab::CurrentSettings).to receive(:update!).with(elasticsearch_pause_indexing: true)
expect(Gitlab::CurrentSettings).to receive(:update!).with(elasticsearch_pause_indexing: false) if expected
where(:started, :completed, :expected) do subject.perform
false | false | false end
true | false | false end
true | true | true
end end
with_them do context 'checks space required' do
it 'pauses and unpauses indexing' do let(:helper) { Gitlab::Elastic::Helper.new }
expect(Gitlab::CurrentSettings).to receive(:update!).with(elasticsearch_pause_indexing: true) let(:started) { false }
expect(Gitlab::CurrentSettings).to receive(:update!).with(elasticsearch_pause_indexing: false) if expected let(:completed) { false }
let(:batched) { false }
before do
allow(Gitlab::Elastic::Helper).to receive(:default).and_return(helper)
allow(migration).to receive(:space_requirements?).and_return(true)
allow(migration).to receive(:space_required_bytes).and_return(10)
end
it 'halts the migration if there is not enough space' do
allow(helper).to receive(:cluster_free_size_bytes).and_return(5)
expect(migration).to receive(:halt!)
expect(migration).not_to receive(:migrate)
subject.perform subject.perform
end end
end
end
context 'checks space required' do it 'runs the migration if there is enough space' do
let(:helper) { Gitlab::Elastic::Helper.new } allow(helper).to receive(:cluster_free_size_bytes).and_return(20)
let(:started) { false } expect(migration).not_to receive(:halt!)
let(:completed) { false } expect(migration).to receive(:migrate).once
let(:batched) { false }
before do subject.perform
allow(Gitlab::Elastic::Helper).to receive(:default).and_return(helper) end
allow(migration).to receive(:space_requirements?).and_return(true)
allow(migration).to receive(:space_required_bytes).and_return(10)
end
it 'halts the migration if there is not enough space' do context 'when migration is already started' do
allow(helper).to receive(:cluster_free_size_bytes).and_return(5) let(:started) { true }
expect(migration).to receive(:halt!)
expect(migration).not_to receive(:migrate)
subject.perform it 'does not check space requirements' do
expect(helper).not_to receive(:cluster_free_size_bytes)
expect(migration).not_to receive(:space_required_bytes)
subject.perform
end
end
end end
end
end
it 'runs the migration if there is enough space' do context 'no unexecuted migrations' do
allow(helper).to receive(:cluster_free_size_bytes).and_return(20) before do
expect(migration).not_to receive(:halt!) allow(subject).to receive(:current_migration).and_return(nil)
expect(migration).to receive(:migrate).once end
subject.perform it 'skips execution' do
end expect(subject).not_to receive(:execute_migration)
context 'when migration is already started' do expect(subject.perform).to be_falsey
let(:started) { true } end
end
it 'does not check space requirements' do context 'load_versions returns empty array' do
expect(helper).not_to receive(:cluster_free_size_bytes) before do
expect(migration).not_to receive(:space_required_bytes) allow(Elastic::MigrationRecord).to receive(:load_versions).and_return([])
end
subject.perform it 'skips execution' do
end expect(subject).not_to receive(:execute_migration)
end
expect(subject.perform).to be_falsey
end end
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