summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-07-10 15:42:19 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-07-10 15:42:19 +0200
commit7082530d555ad98fede2823d2123622abaf1c3a3 (patch)
tree9e221fa2fe91746d9daa37a85d6d10ee10d8b22a
parent6120dc261ab3700b7a19f683559494319b6a1b75 (diff)
downloadgitlab-ce-7082530d555ad98fede2823d2123622abaf1c3a3.tar.gz
Schedule stages statuses bg migrations in batches
-rw-r--r--db/post_migrate/20170630111158_migrate_stages_statuses.rb15
-rw-r--r--lib/gitlab/background_migration/migrate_stage_status.rb5
-rw-r--r--spec/migrations/migrate_stages_statuses_spec.rb8
3 files changed, 16 insertions, 12 deletions
diff --git a/db/post_migrate/20170630111158_migrate_stages_statuses.rb b/db/post_migrate/20170630111158_migrate_stages_statuses.rb
index 2bc067e5d90..1641e550480 100644
--- a/db/post_migrate/20170630111158_migrate_stages_statuses.rb
+++ b/db/post_migrate/20170630111158_migrate_stages_statuses.rb
@@ -6,21 +6,22 @@ class MigrateStagesStatuses < ActiveRecord::Migration
disable_ddl_transaction!
BATCH_SIZE = 10000
+ RANGE_SIZE = 1000
MIGRATION = 'MigrateStageStatus'.freeze
class Stage < ActiveRecord::Base
self.table_name = 'ci_stages'
+ include ::EachBatch
end
def up
- index = 1
+ Stage.where(status: nil).each_batch(of: BATCH_SIZE) do |relation, index|
+ relation.each_batch(of: RANGE_SIZE) do |batch|
+ range = relation.pluck('MIN(id)', 'MAX(id)').first
+ schedule = index * 5.minutes
- Stage.where(status: nil).in_batches(of: BATCH_SIZE) do |relation|
- jobs = relation.pluck(:id).map { |id| [MIGRATION, [id]] }
- schedule = index * 5.minutes
- index += 1
-
- BackgroundMigrationWorker.perform_bulk_in(schedule, jobs)
+ BackgroundMigrationWorker.perform_in(schedule, MIGRATION, range)
+ end
end
end
diff --git a/lib/gitlab/background_migration/migrate_stage_status.rb b/lib/gitlab/background_migration/migrate_stage_status.rb
index e4fdc723b13..3c9744d1607 100644
--- a/lib/gitlab/background_migration/migrate_stage_status.rb
+++ b/lib/gitlab/background_migration/migrate_stage_status.rb
@@ -58,7 +58,7 @@ module Gitlab
end
end
- def perform(id)
+ def perform(start_id, stop_id)
status_sql = Build
.where('ci_builds.commit_id = ci_stages.pipeline_id')
.where('ci_builds.stage = ci_stages.name')
@@ -66,7 +66,8 @@ module Gitlab
sql = <<-SQL
UPDATE ci_stages SET status = (#{status_sql})
- WHERE ci_stages.id = #{id.to_i}
+ WHERE ci_stages.status IS NULL
+ AND ci_stages.id BETWEEN #{start_id.to_i} AND #{stop_id.to_i}
SQL
ActiveRecord::Base.connection.execute(sql)
diff --git a/spec/migrations/migrate_stages_statuses_spec.rb b/spec/migrations/migrate_stages_statuses_spec.rb
index 8463583cef3..1769b1e256b 100644
--- a/spec/migrations/migrate_stages_statuses_spec.rb
+++ b/spec/migrations/migrate_stages_statuses_spec.rb
@@ -12,6 +12,7 @@ describe MigrateStagesStatuses, :migration do
before do
stub_const("#{described_class.name}::BATCH_SIZE", 2)
+ stub_const("#{described_class.name}::RANGE_SIZE", 1)
projects.create!(id: 1, name: 'gitlab1', path: 'gitlab1')
projects.create!(id: 2, name: 'gitlab2', path: 'gitlab2')
@@ -49,9 +50,10 @@ describe MigrateStagesStatuses, :migration do
Timecop.freeze do
migrate!
- expect(described_class::MIGRATION).to be_scheduled_migration(5.minutes, 1)
- expect(described_class::MIGRATION).to be_scheduled_migration(5.minutes, 2)
- expect(described_class::MIGRATION).to be_scheduled_migration(10.minutes, 3)
+ puts BackgroundMigrationWorker.jobs.inspect
+ expect(described_class::MIGRATION).to be_scheduled_migration(5.minutes, 1, 1)
+ expect(described_class::MIGRATION).to be_scheduled_migration(5.minutes, 2, 2)
+ expect(described_class::MIGRATION).to be_scheduled_migration(10.minutes, 3, 3)
expect(BackgroundMigrationWorker.jobs.size).to eq 3
end
end