1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# frozen_string_literal: true
RSpec.shared_examples 'batched background migrations execution worker' do
include ExclusiveLeaseHelpers
it 'is a limited capacity worker' do
expect(described_class.new).to be_a(LimitedCapacity::Worker)
end
describe 'defining the job attributes' do
it 'defines the data_consistency as always' do
expect(described_class.get_data_consistency).to eq(:always)
end
it 'defines the feature_category as database' do
expect(described_class.get_feature_category).to eq(:database)
end
it 'defines the idempotency as false' do
expect(described_class).not_to be_idempotent
end
it 'does not retry failed jobs' do
expect(described_class.sidekiq_options['retry']).to eq(0)
end
it 'does not deduplicate jobs' do
expect(described_class.get_deduplicate_strategy).to eq(:none)
end
it 'defines the queue namespace' do
expect(described_class.queue_namespace).to eq('batched_background_migrations')
end
end
describe '.perform_with_capacity' do
it 'enqueues jobs without modifying provided arguments' do
expect_next_instance_of(described_class) do |instance|
expect(instance).to receive(:remove_failed_jobs)
end
args = [['main', 123]]
expect(described_class)
.to receive(:bulk_perform_async)
.with(args)
described_class.perform_with_capacity(args)
end
end
describe '.max_running_jobs' do
it 'returns MAX_RUNNING_MIGRATIONS' do
expect(described_class.max_running_jobs).to eq(described_class::MAX_RUNNING_MIGRATIONS)
end
end
describe '#max_running_jobs' do
it 'returns MAX_RUNNING_MIGRATIONS' do
expect(described_class.new.max_running_jobs).to eq(described_class::MAX_RUNNING_MIGRATIONS)
end
end
describe '#remaining_work_count' do
it 'returns 0' do
expect(described_class.new.remaining_work_count).to eq(0)
end
end
describe '#perform_work' do
let(:database_name) { Gitlab::Database::MAIN_DATABASE_NAME.to_sym }
let(:base_model) { Gitlab::Database.database_base_models[database_name] }
let(:table_name) { :events }
let(:job_interval) { 5.minutes }
let(:lease_timeout) { job_interval * described_class::LEASE_TIMEOUT_MULTIPLIER }
let(:interval_variance) { described_class::INTERVAL_VARIANCE }
subject(:worker) { described_class.new }
context 'when the feature flag is disabled' do
let(:migration) do
create(:batched_background_migration, :active, interval: job_interval, table_name: table_name)
end
before do
stub_feature_flags(execute_batched_migrations_on_schedule: false)
end
it 'does nothing' do
expect(Gitlab::Database::BackgroundMigration::BatchedMigration).not_to receive(:find_executable)
expect(worker).not_to receive(:run_migration_job)
worker.perform_work(database_name, migration.id)
end
end
context 'when the feature flag is enabled' do
before do
stub_feature_flags(execute_batched_migrations_on_schedule: true)
end
context 'when the provided database is sharing config' do
before do
skip_if_multiple_databases_not_setup(:ci)
end
it 'does nothing' do
ci_model = Gitlab::Database.database_base_models['ci']
expect(Gitlab::Database).to receive(:db_config_share_with)
.with(ci_model.connection_db_config).and_return('main')
expect(Gitlab::Database::BackgroundMigration::BatchedMigration).not_to receive(:find_executable)
expect(worker).not_to receive(:run_migration_job)
worker.perform_work(:ci, 123)
end
end
context 'when migration does not exist' do
it 'does nothing' do
expect(worker).not_to receive(:run_migration_job)
worker.perform_work(database_name, non_existing_record_id)
end
end
context 'when migration exist' do
let(:migration) do
create(:batched_background_migration, :active, interval: job_interval, table_name: table_name)
end
before do
allow(Gitlab::Database::BackgroundMigration::BatchedMigration).to receive(:find_executable)
.with(migration.id, connection: base_model.connection)
.and_return(migration)
end
context 'when the migration is no longer active' do
it 'does not run the migration' do
expect(Gitlab::Database::SharedModel).to receive(:using_connection).with(base_model.connection).and_yield
expect(migration).to receive(:active?).and_return(false)
expect(worker).not_to receive(:run_migration_job)
worker.perform_work(database_name, migration.id)
end
end
context 'when the interval has not elapsed' do
it 'does not run the migration' do
expect(Gitlab::Database::SharedModel).to receive(:using_connection).with(base_model.connection).and_yield
expect(migration).to receive(:interval_elapsed?).with(variance: interval_variance).and_return(false)
expect(worker).not_to receive(:run_migration_job)
worker.perform_work(database_name, migration.id)
end
end
context 'when the migration is still active and the interval has elapsed' do
let(:table_name_lease_key) do
"#{described_class.name.underscore}:database_name:#{database_name}:" \
"table_name:#{table_name}"
end
context 'when can not obtain lease on the table name' do
it 'does nothing' do
stub_exclusive_lease_taken(table_name_lease_key, timeout: lease_timeout)
expect(worker).not_to receive(:run_migration_job)
worker.perform_work(database_name, migration.id)
end
end
it 'always cleans up the exclusive lease' do
expect_to_obtain_exclusive_lease(table_name_lease_key, 'uuid-table-name', timeout: lease_timeout)
expect_to_cancel_exclusive_lease(table_name_lease_key, 'uuid-table-name')
expect(worker).to receive(:run_migration_job).and_raise(RuntimeError, 'I broke')
expect { worker.perform_work(database_name, migration.id) }.to raise_error(RuntimeError, 'I broke')
end
it 'runs the migration' do
expect(Gitlab::Database::SharedModel).to receive(:using_connection).with(base_model.connection).and_yield
expect_next_instance_of(Gitlab::Database::BackgroundMigration::BatchedMigrationRunner) do |instance|
expect(instance).to receive(:run_migration_job).with(migration)
end
expect_to_obtain_exclusive_lease(table_name_lease_key, 'uuid-table-name', timeout: lease_timeout)
expect_to_cancel_exclusive_lease(table_name_lease_key, 'uuid-table-name')
expect(worker).to receive(:run_migration_job).and_call_original
worker.perform_work(database_name, migration.id)
end
end
end
end
end
end
|