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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::SidekiqMigrateJobs, :clean_gitlab_redis_queues,
:clean_gitlab_redis_queues_metadata do
def clear_queues
Sidekiq::Queue.new('authorized_projects').clear
Sidekiq::Queue.new('post_receive').clear
Sidekiq::RetrySet.new.clear
Sidekiq::ScheduledSet.new.clear
end
around do |example|
clear_queues
Sidekiq::Testing.disable!(&example)
clear_queues
end
describe '#migrate_set', :aggregate_failures do
shared_examples 'processing a set' do
let(:migrator) { described_class.new(mappings) }
let(:set_after) do
Sidekiq.redis { |c| c.zrange(set_name, 0, -1, with_scores: true) }
.map { |item, score| [Gitlab::Json.load(item), score] }
end
context 'when the set is empty' do
let(:mappings) { { 'AuthorizedProjectsWorker' => 'new_queue' } }
it 'returns the number of scanned and migrated jobs' do
expect(migrator.migrate_set(set_name)).to eq(
scanned: 0,
migrated: 0)
end
end
context 'when the set is not empty' do
let(:mappings) { {} }
it 'returns the number of scanned and migrated jobs' do
create_jobs
expect(migrator.migrate_set(set_name)).to eq(scanned: 4, migrated: 0)
end
end
context 'when there are no matching jobs' do
let(:mappings) { { 'PostReceive' => 'new_queue' } }
it 'does not change any queue names' do
create_jobs(include_post_receive: false)
expect(migrator.migrate_set(set_name)).to eq(scanned: 3, migrated: 0)
expect(set_after.length).to eq(3)
expect(set_after.map(&:first)).to all(include('queue' => 'default',
'class' => 'AuthorizedProjectsWorker'))
end
end
context 'when there are matching jobs' do
it 'migrates only the workers matching the given worker from the set' do
migrator = described_class.new({ 'AuthorizedProjectsWorker' => 'new_queue' })
freeze_time do
create_jobs
expect(migrator.migrate_set(set_name)).to eq(
scanned: 4,
migrated: 3)
set_after.each.with_index do |(item, score), i|
if item['class'] == 'AuthorizedProjectsWorker'
expect(item).to include('queue' => 'new_queue', 'args' => [i])
else
expect(item).to include('queue' => 'default', 'args' => [i])
end
expect(score).to be_within(schedule_jitter).of(i.succ.hours.from_now.to_i)
end
end
end
it 'allows migrating multiple workers at once' do
migrator = described_class.new({
'AuthorizedProjectsWorker' => 'new_queue',
'PostReceive' => 'another_queue'
})
freeze_time do
create_jobs
expect(migrator.migrate_set(set_name)).to eq(scanned: 4, migrated: 4)
set_after.each.with_index do |(item, score), i|
if item['class'] == 'AuthorizedProjectsWorker'
expect(item).to include('queue' => 'new_queue', 'args' => [i])
else
expect(item).to include('queue' => 'another_queue', 'args' => [i])
end
expect(score).to be_within(schedule_jitter).of(i.succ.hours.from_now.to_i)
end
end
end
it 'allows migrating multiple workers to the same queue' do
migrator = described_class.new({
'AuthorizedProjectsWorker' => 'new_queue',
'PostReceive' => 'new_queue'
})
freeze_time do
create_jobs
expect(migrator.migrate_set(set_name)).to eq(scanned: 4, migrated: 4)
set_after.each.with_index do |(item, score), i|
expect(item).to include('queue' => 'new_queue', 'args' => [i])
expect(score).to be_within(schedule_jitter).of(i.succ.hours.from_now.to_i)
end
end
end
it 'does not try to migrate jobs that are removed from the set during the migration' do
migrator = described_class.new({ 'PostReceive' => 'new_queue' })
freeze_time do
create_jobs
allow(migrator).to receive(:migrate_job_in_set).and_wrap_original do |meth, *args|
Sidekiq.redis { |c| c.zrem(set_name, args.second) }
meth.call(*args)
end
expect(migrator.migrate_set(set_name)).to eq(scanned: 4, migrated: 0)
expect(set_after.length).to eq(3)
expect(set_after.map(&:first)).to all(include('queue' => 'default'))
end
end
it 'does not try to migrate unmatched jobs that are added to the set during the migration' do
migrator = described_class.new({ 'PostReceive' => 'new_queue' })
create_jobs
calls = 0
allow(migrator).to receive(:migrate_job_in_set).and_wrap_original do |meth, *args|
if calls == 0
travel_to(5.hours.from_now) { create_jobs(include_post_receive: false) }
end
calls += 1
meth.call(*args)
end
expect(migrator.migrate_set(set_name)).to eq(scanned: 4, migrated: 1)
expect(set_after.group_by { |job| job.first['queue'] }.transform_values(&:count))
.to eq('default' => 6, 'new_queue' => 1)
end
it 'iterates through the entire set of jobs' do
migrator = described_class.new({ 'NonExistentWorker' => 'new_queue' })
50.times do |i|
travel_to(i.hours.from_now) { create_jobs }
end
expect(migrator.migrate_set(set_name)).to eq(scanned: 200, migrated: 0)
expect(set_after.length).to eq(200)
end
it 'logs output at the start, finish, and every LOG_FREQUENCY jobs' do
freeze_time do
create_jobs
stub_const("#{described_class}::LOG_FREQUENCY", 2)
logger = Logger.new(StringIO.new)
migrator = described_class.new({
'AuthorizedProjectsWorker' => 'new_queue',
'PostReceive' => 'another_queue'
}, logger: logger)
expect(logger).to receive(:info).with(a_string_matching('Processing')).once.ordered
expect(logger).to receive(:info).with(a_string_matching('In progress')).once.ordered
expect(logger).to receive(:info).with(a_string_matching('Done')).once.ordered
expect(migrator.migrate_set(set_name)).to eq(scanned: 4, migrated: 4)
end
end
end
end
context 'scheduled jobs' do
let(:set_name) { 'schedule' }
let(:schedule_jitter) { 0 }
def create_jobs(include_post_receive: true)
AuthorizedProjectsWorker.perform_in(1.hour, 0)
AuthorizedProjectsWorker.perform_in(2.hours, 1)
PostReceive.perform_in(3.hours, 2) if include_post_receive
AuthorizedProjectsWorker.perform_in(4.hours, 3)
end
it_behaves_like 'processing a set'
end
context 'retried jobs' do
def create_jobs(include_post_receive: true)
retry_in(AuthorizedProjectsWorker, 1.hour, 0)
retry_in(AuthorizedProjectsWorker, 2.hours, 1)
retry_in(PostReceive, 3.hours, 2) if include_post_receive
retry_in(AuthorizedProjectsWorker, 4.hours, 3)
end
include_context 'when handling retried jobs'
it_behaves_like 'processing a set'
end
end
describe '#migrate_queues', :aggregate_failures do
let(:migrator) { described_class.new(mappings, logger: logger) }
let(:logger) { nil }
def list_queues
queues = Sidekiq.redis do |conn|
conn.scan_each(match: "queue:*").to_a
end
queues.uniq.map { |queue| queue.split(':', 2).last }
end
def list_jobs(queue_name)
Sidekiq.redis { |conn| conn.lrange("queue:#{queue_name}", 0, -1) }
.map { |item| Gitlab::Json.load(item) }
end
def pre_migrate_checks; end
before do
queue_name_from_worker_name = Gitlab::SidekiqConfig::WorkerRouter.method(:queue_name_from_worker_name)
EmailReceiverWorker.sidekiq_options(queue: queue_name_from_worker_name.call(EmailReceiverWorker))
EmailReceiverWorker.perform_async('foo')
EmailReceiverWorker.perform_async('bar')
# test worker that has ':' inside the queue name
AuthorizedProjectUpdate::ProjectRecalculateWorker.sidekiq_options(
queue: queue_name_from_worker_name.call(AuthorizedProjectUpdate::ProjectRecalculateWorker)
)
AuthorizedProjectUpdate::ProjectRecalculateWorker.perform_async
end
after do
# resets the queue name to its original
EmailReceiverWorker.set_queue
AuthorizedProjectUpdate::ProjectRecalculateWorker.set_queue
end
shared_examples 'migrating queues' do
it 'migrates the jobs to the correct destination queue' do
queues = list_queues
expect(queues).to include(*queues_included_pre_migrate)
expect(queues).not_to include(*queues_excluded_pre_migrate)
pre_migrate_checks
migrator.migrate_queues
queues = list_queues
expect(queues).not_to include(*queues_excluded_post_migrate)
expect(queues).to include(*queues_included_post_migrate)
post_migrate_checks
end
end
context 'with all workers mapped to default queue' do
let(:mappings) do
{ 'EmailReceiverWorker' => 'default', 'AuthorizedProjectUpdate::ProjectRecalculateWorker' => 'default' }
end
let(:queues_included_pre_migrate) do
['email_receiver',
'authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_excluded_pre_migrate) { ['default'] }
let(:queues_excluded_post_migrate) do
['email_receiver',
'authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_included_post_migrate) { ['default'] }
def post_migrate_checks
jobs = list_jobs('default')
expect(jobs.length).to eq(3)
sorted = jobs.sort_by { |job| [job["class"], job["args"]] }
expect(sorted[0]).to include('class' => 'AuthorizedProjectUpdate::ProjectRecalculateWorker',
'queue' => 'default')
expect(sorted[1]).to include('class' => 'EmailReceiverWorker', 'args' => ['bar'], 'queue' => 'default')
expect(sorted[2]).to include('class' => 'EmailReceiverWorker', 'args' => ['foo'], 'queue' => 'default')
end
it_behaves_like 'migrating queues'
end
context 'with custom mapping to different queues' do
let(:mappings) do
{ 'EmailReceiverWorker' => 'new_email',
'AuthorizedProjectUpdate::ProjectRecalculateWorker' => 'new_authorized' }
end
let(:queues_included_pre_migrate) do
['email_receiver',
'authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_excluded_pre_migrate) { %w[new_email new_authorized] }
let(:queues_excluded_post_migrate) do
['email_receiver',
'authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_included_post_migrate) { %w[new_email new_authorized] }
def post_migrate_checks
email_jobs = list_jobs('new_email')
expect(email_jobs.length).to eq(2)
expect(email_jobs[0]).to include('class' => 'EmailReceiverWorker', 'args' => ['bar'], 'queue' => 'new_email')
expect(email_jobs[1]).to include('class' => 'EmailReceiverWorker', 'args' => ['foo'], 'queue' => 'new_email')
export_jobs = list_jobs('new_authorized')
expect(export_jobs.length).to eq(1)
expect(export_jobs[0]).to include('class' => 'AuthorizedProjectUpdate::ProjectRecalculateWorker',
'queue' => 'new_authorized')
end
it_behaves_like 'migrating queues'
end
context 'with illegal JSON payload' do
let(:job) { '{foo: 1}' }
let(:mappings) do
{ 'EmailReceiverWorker' => 'default', 'AuthorizedProjectUpdate::ProjectRecalculateWorker' => 'default' }
end
let(:queues_included_pre_migrate) do
['email_receiver',
'authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_excluded_pre_migrate) { ['default'] }
let(:queues_excluded_post_migrate) do
['email_receiver',
'authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_included_post_migrate) { ['default'] }
let(:logger) { Logger.new(StringIO.new) }
before do
Sidekiq.redis do |conn|
conn.lpush("queue:email_receiver", job)
end
end
def pre_migrate_checks
expect(logger).to receive(:error)
.with(a_string_matching('Unmarshal JSON payload from SidekiqMigrateJobs failed'))
.once
end
def post_migrate_checks
jobs = list_jobs('default')
expect(jobs.length).to eq(3)
sorted = jobs.sort_by { |job| [job["class"], job["args"]] }
expect(sorted[0]).to include('class' => 'AuthorizedProjectUpdate::ProjectRecalculateWorker',
'queue' => 'default')
expect(sorted[1]).to include('class' => 'EmailReceiverWorker', 'args' => ['bar'], 'queue' => 'default')
expect(sorted[2]).to include('class' => 'EmailReceiverWorker', 'args' => ['foo'], 'queue' => 'default')
end
it_behaves_like 'migrating queues'
end
context 'when multiple workers are in the same queue' do
before do
ExportCsvWorker.sidekiq_options(queue: 'email_receiver') # follows EmailReceiverWorker's queue
ExportCsvWorker.perform_async('fizz')
end
after do
ExportCsvWorker.set_queue
end
context 'when the queue exists in mappings' do
let(:mappings) do
{ 'EmailReceiverWorker' => 'email_receiver', 'AuthorizedProjectUpdate::ProjectRecalculateWorker' => 'default',
'ExportCsvWorker' => 'default' }
end
let(:queues_included_pre_migrate) do
['email_receiver',
'authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_excluded_pre_migrate) { ['default'] }
let(:queues_excluded_post_migrate) do
['authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_included_post_migrate) { %w[default email_receiver] }
it_behaves_like 'migrating queues'
def post_migrate_checks
# jobs from email_receiver are not migrated at all
jobs = list_jobs('email_receiver')
expect(jobs.length).to eq(3)
sorted = jobs.sort_by { |job| [job["class"], job["args"]] }
expect(sorted[0]).to include('class' => 'EmailReceiverWorker', 'args' => ['bar'], 'queue' => 'email_receiver')
expect(sorted[1]).to include('class' => 'EmailReceiverWorker', 'args' => ['foo'], 'queue' => 'email_receiver')
expect(sorted[2]).to include('class' => 'ExportCsvWorker', 'args' => ['fizz'], 'queue' => 'email_receiver')
end
end
context 'when the queue doesnt exist in mappings' do
let(:mappings) do
{ 'EmailReceiverWorker' => 'default', 'AuthorizedProjectUpdate::ProjectRecalculateWorker' => 'default',
'ExportCsvWorker' => 'default' }
end
let(:queues_included_pre_migrate) do
['email_receiver',
'authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_excluded_pre_migrate) { ['default'] }
let(:queues_excluded_post_migrate) do
['email_receiver', 'authorized_project_update:authorized_project_update_project_recalculate']
end
let(:queues_included_post_migrate) { ['default'] }
it_behaves_like 'migrating queues'
def post_migrate_checks
# jobs from email_receiver are all migrated
jobs = list_jobs('email_receiver')
expect(jobs.length).to eq(0)
jobs = list_jobs('default')
expect(jobs.length).to eq(4)
sorted = jobs.sort_by { |job| [job["class"], job["args"]] }
expect(sorted[0]).to include('class' => 'AuthorizedProjectUpdate::ProjectRecalculateWorker',
'queue' => 'default')
expect(sorted[1]).to include('class' => 'EmailReceiverWorker', 'args' => ['bar'], 'queue' => 'default')
expect(sorted[2]).to include('class' => 'EmailReceiverWorker', 'args' => ['foo'], 'queue' => 'default')
expect(sorted[3]).to include('class' => 'ExportCsvWorker', 'args' => ['fizz'], 'queue' => 'default')
end
end
end
end
end
|