summaryrefslogtreecommitdiff
path: root/spec/workers
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2018-07-19 17:16:47 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2018-08-06 15:20:36 +0200
commit91b752dce63147bc99d7784d3d37865efb5e9352 (patch)
tree447dcd9dc5efcb14af5439f247d87938daf845dc /spec/workers
parent5f742eb95a0080343167469ccabfeccd3630007d (diff)
downloadgitlab-ce-91b752dce63147bc99d7784d3d37865efb5e9352.tar.gz
Respond to DB health in background migrations
This changes the BackgroundMigration worker so it checks for the health of the DB before performing a background migration. This in turn allows us to reduce the minimum interval, without having to worry about blowing things up if we schedule too many migrations. In this setup, the BackgroundMigration worker will reschedule jobs as long as the database is considered to be in an unhealthy state. Once the database has recovered, the migration can be performed. To determine if the database is in a healthy state, we look at the replication lag of any replication slots defined on the primary. If the lag is deemed to great (100 MB by default) for too many slots, the migration is rescheduled for a later point in time. The health checking code is hidden behind a feature flag, allowing us to disable it if necessary.
Diffstat (limited to 'spec/workers')
-rw-r--r--spec/workers/background_migration_worker_spec.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/spec/workers/background_migration_worker_spec.rb b/spec/workers/background_migration_worker_spec.rb
index d67e7698635..3bd072e7125 100644
--- a/spec/workers/background_migration_worker_spec.rb
+++ b/spec/workers/background_migration_worker_spec.rb
@@ -3,6 +3,12 @@ require 'spec_helper'
describe BackgroundMigrationWorker, :sidekiq, :clean_gitlab_redis_shared_state do
let(:worker) { described_class.new }
+ describe '.minimum_interval' do
+ it 'returns 2 minutes' do
+ expect(described_class.minimum_interval).to eq(2.minutes.to_i)
+ end
+ end
+
describe '.perform' do
it 'performs a background migration' do
expect(Gitlab::BackgroundMigration)
@@ -28,5 +34,51 @@ describe BackgroundMigrationWorker, :sidekiq, :clean_gitlab_redis_shared_state d
worker.perform('Foo', [10, 20])
end
+
+ it 'reschedules a migration if the database is not healthy' do
+ allow(worker)
+ .to receive(:always_perform?)
+ .and_return(false)
+
+ allow(worker)
+ .to receive(:healthy_database?)
+ .and_return(false)
+
+ expect(described_class)
+ .to receive(:perform_in)
+ .with(a_kind_of(Numeric), 'Foo', [10, 20])
+
+ worker.perform('Foo', [10, 20])
+ end
+ end
+
+ describe '#healthy_database?' do
+ context 'using MySQL', :mysql do
+ it 'returns true' do
+ expect(worker.healthy_database?).to eq(true)
+ end
+ end
+
+ context 'using PostgreSQL', :postgresql do
+ context 'when replication lag is too great' do
+ it 'returns false' do
+ allow(Postgresql::ReplicationSlot)
+ .to receive(:lag_too_great?)
+ .and_return(true)
+
+ expect(worker.healthy_database?).to eq(false)
+ end
+ 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
end
end