Commit 99aa25b9 authored by pbair's avatar pbair

Change migration table scopes to use id column

Change the scopes that are used to fetch batched_background_migrations
and batched_background_migration_jobs to use the `id` column rather than
`created_at`. This saves an index one of the tables, and ensures we
don't need a tiebreaker for the sort.
parent 4dcd8405
...@@ -47,7 +47,7 @@ class CreateBackgroundMigrationTrackingTables < ActiveRecord::Migration[6.0] ...@@ -47,7 +47,7 @@ class CreateBackgroundMigrationTrackingTables < ActiveRecord::Migration[6.0]
t.integer :status, limit: 2, null: false, default: 0 t.integer :status, limit: 2, null: false, default: 0
t.integer :attempts, limit: 2, null: false, default: 0 t.integer :attempts, limit: 2, null: false, default: 0
t.index [:batched_background_migration_id, :created_at], name: :index_batched_jobs_by_batched_migration_id_created_at t.index [:batched_background_migration_id, :id], name: :index_batched_jobs_by_batched_migration_id_and_id
end end
end end
......
...@@ -21705,7 +21705,7 @@ CREATE INDEX index_badges_on_group_id ON badges USING btree (group_id); ...@@ -21705,7 +21705,7 @@ CREATE INDEX index_badges_on_group_id ON badges USING btree (group_id);
CREATE INDEX index_badges_on_project_id ON badges USING btree (project_id); CREATE INDEX index_badges_on_project_id ON badges USING btree (project_id);
CREATE INDEX index_batched_jobs_by_batched_migration_id_created_at ON batched_background_migration_jobs USING btree (batched_background_migration_id, created_at); CREATE INDEX index_batched_jobs_by_batched_migration_id_and_id ON batched_background_migration_jobs USING btree (batched_background_migration_id, id);
CREATE INDEX index_batched_migrations_on_job_table_and_column_name ON batched_background_migrations USING btree (job_class_name, table_name, column_name); CREATE INDEX index_batched_migrations_on_job_table_and_column_name ON batched_background_migrations USING btree (job_class_name, table_name, column_name);
...@@ -7,11 +7,11 @@ module Gitlab ...@@ -7,11 +7,11 @@ module Gitlab
self.table_name = :batched_background_migrations self.table_name = :batched_background_migrations
has_many :batched_jobs, foreign_key: :batched_background_migration_id has_many :batched_jobs, foreign_key: :batched_background_migration_id
has_one :last_job, -> { order(created_at: :desc) }, has_one :last_job, -> { order(id: :desc) },
class_name: 'Gitlab::Database::BackgroundMigration::BatchedJob', class_name: 'Gitlab::Database::BackgroundMigration::BatchedJob',
foreign_key: :batched_background_migration_id foreign_key: :batched_background_migration_id
scope :queue_order, -> { order(created_at: :asc) } scope :queue_order, -> { order(id: :asc) }
enum status: { enum status: {
paused: 0, paused: 0,
......
...@@ -5,9 +5,6 @@ module Gitlab ...@@ -5,9 +5,6 @@ module Gitlab
module BackgroundMigration module BackgroundMigration
class BatchedMigrationWrapper class BatchedMigrationWrapper
def perform(batch_tracking_record) def perform(batch_tracking_record)
return if batch_tracking_record.migration_aborted?
begin
start_tracking_execution(batch_tracking_record) start_tracking_execution(batch_tracking_record)
execute_batch(batch_tracking_record) execute_batch(batch_tracking_record)
...@@ -20,7 +17,6 @@ module Gitlab ...@@ -20,7 +17,6 @@ module Gitlab
ensure ensure
finish_tracking_execution(batch_tracking_record) finish_tracking_execution(batch_tracking_record)
end end
end
private private
......
...@@ -13,7 +13,7 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedJob, type: :model d ...@@ -13,7 +13,7 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedJob, type: :model d
let(:batched_job) { build(:batched_background_migration_job) } let(:batched_job) { build(:batched_background_migration_job) }
let(:batched_migration) { batched_job.batched_migration } let(:batched_migration) { batched_job.batched_migration }
describe '#migration_job_class' do describe '#migration_aborted?' do
before do before do
batched_migration.status = :aborted batched_migration.status = :aborted
end end
......
...@@ -11,21 +11,21 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigration, type: :m ...@@ -11,21 +11,21 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigration, type: :m
describe '#last_job' do describe '#last_job' do
let!(:batched_migration) { create(:batched_background_migration) } let!(:batched_migration) { create(:batched_background_migration) }
let!(:batched_job1) { create(:batched_background_migration_job, batched_migration: batched_migration) } let!(:batched_job1) { create(:batched_background_migration_job, batched_migration: batched_migration) }
let!(:batched_job2) { create(:batched_background_migration_job, batched_migration: batched_migration, created_at: 1.month.ago) } let!(:batched_job2) { create(:batched_background_migration_job, batched_migration: batched_migration) }
it 'returns the most recently created batch_job' do it 'returns the most recent (in order of id) batched job' do
expect(batched_migration.last_job).to eq(batched_job1) expect(batched_migration.last_job).to eq(batched_job2)
end end
end end
end end
describe '.queue_order' do describe '.queue_order' do
let!(:migration1) { create(:batched_background_migration, created_at: 1.month.ago) } let!(:migration1) { create(:batched_background_migration) }
let!(:migration2) { create(:batched_background_migration, created_at: 2.months.ago) } let!(:migration2) { create(:batched_background_migration) }
let!(:migration3) { create(:batched_background_migration) } let!(:migration3) { create(:batched_background_migration) }
it 'returns batched_migrations ordered by created_at' do it 'returns batched migrations ordered by their id' do
expect(described_class.queue_order.all).to eq([migration2, migration1, migration3]) expect(described_class.queue_order.all).to eq([migration1, migration2, migration3])
end end
end end
......
...@@ -8,28 +8,6 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigrationWrapper, ' ...@@ -8,28 +8,6 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigrationWrapper, '
let_it_be(:active_migration) { create(:batched_background_migration, :active, job_arguments: [:id, :other_id]) } let_it_be(:active_migration) { create(:batched_background_migration, :active, job_arguments: [:id, :other_id]) }
context 'when the migration is aborted' do
let!(:aborted_migration) { create(:batched_background_migration, :aborted) }
let!(:job_record) { create(:batched_background_migration_job, batched_migration: aborted_migration) }
it 'does not run the migration job instance' do
expect(job_record).not_to receive(:migration_job_class)
expect(job_class).not_to receive(:new)
migration_wrapper.perform(job_record)
end
it 'does not update the tracking record in the database' do
migration_wrapper.perform(job_record)
reloaded_job_record = job_record.reload
expect(reloaded_job_record).to be_pending
expect(reloaded_job_record.attempts).to eq(0)
end
end
context 'when the migration is not aborted' do
let!(:job_record) { create(:batched_background_migration_job, batched_migration: active_migration) } let!(:job_record) { create(:batched_background_migration_job, batched_migration: active_migration) }
it 'runs the migration job' do it 'runs the migration job' do
...@@ -89,5 +67,4 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigrationWrapper, ' ...@@ -89,5 +67,4 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigrationWrapper, '
end end
end end
end end
end
end end
...@@ -32,8 +32,8 @@ RSpec.describe Gitlab::Database::BackgroundMigration::Scheduler, '#perform' do ...@@ -32,8 +32,8 @@ RSpec.describe Gitlab::Database::BackgroundMigration::Scheduler, '#perform' do
end end
context 'when there are active migrations' do context 'when there are active migrations' do
let!(:last_migration) { create(:batched_background_migration, :active, created_at: 1.month.ago) } let!(:first_migration) { create(:batched_background_migration, :active, batch_size: 2) }
let!(:first_migration) { create(:batched_background_migration, :active, batch_size: 2, created_at: 2.months.ago) } let!(:last_migration) { create(:batched_background_migration, :active) }
let(:job_relation) do let(:job_relation) do
Gitlab::Database::BackgroundMigration::BatchedJob.where(batched_background_migration_id: first_migration.id) Gitlab::Database::BackgroundMigration::BatchedJob.where(batched_background_migration_id: first_migration.id)
......
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