Commit 65e1c4ce authored by dfrazao-gitlab's avatar dfrazao-gitlab

Support multiple databases for batched background migrations

- Add an option to disable the batched background migrations worker
  via feature flag for CI and Main database

Changelog: added

Relates to https://gitlab.com/gitlab-org/gitlab/-/merge_requests/83109
parent 83d7a7a8
...@@ -4,6 +4,10 @@ module Database ...@@ -4,6 +4,10 @@ module Database
class CiDatabaseWorker # rubocop:disable Scalability/IdempotentWorker class CiDatabaseWorker # rubocop:disable Scalability/IdempotentWorker
include SingleDatabaseWorker include SingleDatabaseWorker
def self.enabled?
Feature.enabled?(:execute_batched_migrations_on_schedule_ci_database, type: :ops, default_enabled: :yaml)
end
def self.tracking_database def self.tracking_database
@tracking_database ||= Gitlab::Database::CI_DATABASE_NAME @tracking_database ||= Gitlab::Database::CI_DATABASE_NAME
end end
......
...@@ -23,6 +23,10 @@ module Database ...@@ -23,6 +23,10 @@ module Database
def tracking_database def tracking_database
raise NotImplementedError, "#{self.name} does not implement #{__method__}" raise NotImplementedError, "#{self.name} does not implement #{__method__}"
end end
def enabled?
raise NotImplementedError, "#{self.name} does not implement #{__method__}"
end
# :nocov: # :nocov:
def lease_key def lease_key
...@@ -41,7 +45,7 @@ module Database ...@@ -41,7 +45,7 @@ module Database
end end
Gitlab::Database::SharedModel.using_connection(base_model.connection) do Gitlab::Database::SharedModel.using_connection(base_model.connection) do
break unless Feature.enabled?(:execute_batched_migrations_on_schedule, type: :ops, default_enabled: :yaml) && active_migration break unless self.class.enabled? && active_migration
with_exclusive_lease(active_migration.interval) do with_exclusive_lease(active_migration.interval) do
# Now that we have the exclusive lease, reload migration in case another process has changed it. # Now that we have the exclusive lease, reload migration in case another process has changed it.
......
...@@ -4,6 +4,10 @@ module Database ...@@ -4,6 +4,10 @@ module Database
class BatchedBackgroundMigrationWorker # rubocop:disable Scalability/IdempotentWorker class BatchedBackgroundMigrationWorker # rubocop:disable Scalability/IdempotentWorker
include BatchedBackgroundMigration::SingleDatabaseWorker include BatchedBackgroundMigration::SingleDatabaseWorker
def self.enabled?
Feature.enabled?(:execute_batched_migrations_on_schedule, type: :ops, default_enabled: :yaml)
end
def self.tracking_database def self.tracking_database
@tracking_database ||= Gitlab::Database::MAIN_DATABASE_NAME.to_sym @tracking_database ||= Gitlab::Database::MAIN_DATABASE_NAME.to_sym
end end
......
---
name: execute_batched_migrations_on_schedule_ci_database
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/83109
rollout_issue_url:
milestone: '14.9'
type: ops
group: group::database
default_enabled: false
# frozen_string_literal: true # frozen_string_literal: true
RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_database| RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_database, feature_flag:|
include ExclusiveLeaseHelpers include ExclusiveLeaseHelpers
describe 'defining the job attributes' do describe 'defining the job attributes' do
...@@ -39,6 +39,16 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d ...@@ -39,6 +39,16 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d
end end
end end
describe '.enabled?' do
it 'does not raise an error' do
expect { described_class.enabled? }.not_to raise_error
end
it 'returns true' do
expect(described_class.enabled?).to be_truthy
end
end
describe '#perform' do describe '#perform' do
subject(:worker) { described_class.new } subject(:worker) { described_class.new }
...@@ -76,7 +86,7 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d ...@@ -76,7 +86,7 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d
context 'when the feature flag is disabled' do context 'when the feature flag is disabled' do
before do before do
stub_feature_flags(execute_batched_migrations_on_schedule: false) stub_feature_flags(feature_flag => false)
end end
it 'does nothing' do it 'does nothing' do
...@@ -89,7 +99,7 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d ...@@ -89,7 +99,7 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d
context 'when the feature flag is enabled' do context 'when the feature flag is enabled' do
before do before do
stub_feature_flags(execute_batched_migrations_on_schedule: true) stub_feature_flags(feature_flag => true)
allow(Gitlab::Database::BackgroundMigration::BatchedMigration).to receive(:active_migration).and_return(nil) allow(Gitlab::Database::BackgroundMigration::BatchedMigration).to receive(:active_migration).and_return(nil)
end end
......
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Database::BatchedBackgroundMigration::CiDatabaseWorker, :clean_gitlab_redis_shared_state do RSpec.describe Database::BatchedBackgroundMigration::CiDatabaseWorker, :clean_gitlab_redis_shared_state do
it_behaves_like 'it runs batched background migration jobs', 'ci' it_behaves_like 'it runs batched background migration jobs', 'ci', feature_flag: :execute_batched_migrations_on_schedule_ci_database
end end
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Database::BatchedBackgroundMigrationWorker do RSpec.describe Database::BatchedBackgroundMigrationWorker do
it_behaves_like 'it runs batched background migration jobs', :main it_behaves_like 'it runs batched background migration jobs', :main, feature_flag: :execute_batched_migrations_on_schedule
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