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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
# frozen_string_literal: true
require 'spec_helper'
require 'gitlab/rspec/next_instance_of'
require_relative '../../support/stub_settings_source'
require_relative '../../../sidekiq_cluster/cli'
RSpec.describe Gitlab::SidekiqCluster::CLI, feature_category: :gitlab_cli, stub_settings_source: true do # rubocop:disable RSpec/FilePath
include NextInstanceOf
let(:cli) { described_class.new('/dev/null') }
let(:timeout) { Gitlab::SidekiqCluster::DEFAULT_SOFT_TIMEOUT_SECONDS }
let(:default_options) do
{ env: 'test', directory: Dir.pwd, max_concurrency: 20, min_concurrency: 0, dryrun: false, timeout: timeout }
end
let(:sidekiq_exporter_enabled) { false }
let(:sidekiq_exporter_port) { '3807' }
let(:config) do
{
'sidekiq_exporter' => {
'address' => 'localhost',
'enabled' => sidekiq_exporter_enabled,
'port' => sidekiq_exporter_port
}
}
end
let(:supervisor) { instance_double(Gitlab::SidekiqCluster::SidekiqProcessSupervisor) }
let(:metrics_cleanup_service) { instance_double(Prometheus::CleanupMultiprocDirService, execute: nil) }
before do
allow(Gitlab::ProcessManagement).to receive(:write_pid)
allow(Gitlab::SidekiqCluster::SidekiqProcessSupervisor).to receive(:instance).and_return(supervisor)
allow(supervisor).to receive(:supervise)
allow(Prometheus::CleanupMultiprocDirService).to receive(:new).and_return(metrics_cleanup_service)
stub_config(sidekiq: { routing_rules: [] })
end
around do |example|
original = Settings['monitoring']
Settings['monitoring'] = config
example.run
Settings['monitoring'] = original
end
describe '#run' do
context 'without any arguments' do
it 'raises CommandError' do
expect { cli.run([]) }.to raise_error(described_class::CommandError)
end
end
context 'with arguments' do
it 'starts the Sidekiq workers' do
expect(Gitlab::SidekiqCluster).to receive(:start)
.with([['foo'] + described_class::DEFAULT_QUEUES], default_options)
.and_return([])
cli.run(%w[foo])
end
it 'allows the special * selector' do
worker_queues = %w[foo bar baz]
expect(Gitlab::SidekiqConfig::CliMethods)
.to receive(:worker_queues).and_return(worker_queues)
expect(Gitlab::SidekiqCluster)
.to receive(:start).with([worker_queues], default_options).and_return([])
cli.run(%w[*])
end
it 'raises an error when the arguments contain newlines' do
invalid_arguments = [
["foo\n"],
["foo\r"],
%W[foo b\nar]
]
invalid_arguments.each do |arguments|
expect { cli.run(arguments) }.to raise_error(described_class::CommandError)
end
end
context 'with --negate flag' do
it 'starts Sidekiq workers for all queues in all_queues.yml except the ones in argv' do
expect(Gitlab::SidekiqConfig::CliMethods).to receive(:worker_queues).and_return(['baz'])
expect(Gitlab::SidekiqCluster).to receive(:start)
.with([['baz'] + described_class::DEFAULT_QUEUES], default_options)
.and_return([])
cli.run(%w[foo -n])
end
end
context 'with --max-concurrency flag' do
it 'starts Sidekiq workers for specified queues with a max concurrency' do
expected_queues = [%w[foo bar baz], %w[solo]].each { |queues| queues.concat(described_class::DEFAULT_QUEUES) }
expect(Gitlab::SidekiqConfig::CliMethods).to receive(:worker_queues).and_return(%w[foo bar baz])
expect(Gitlab::SidekiqCluster).to receive(:start)
.with(expected_queues, default_options.merge(max_concurrency: 2))
.and_return([])
cli.run(%w[foo,bar,baz solo -m 2])
end
end
context 'with --min-concurrency flag' do
it 'starts Sidekiq workers for specified queues with a min concurrency' do
expected_queues = [%w[foo bar baz], %w[solo]].each { |queues| queues.concat(described_class::DEFAULT_QUEUES) }
expect(Gitlab::SidekiqConfig::CliMethods).to receive(:worker_queues).and_return(%w[foo bar baz])
expect(Gitlab::SidekiqCluster).to receive(:start)
.with(expected_queues, default_options.merge(min_concurrency: 2))
.and_return([])
cli.run(%w[foo,bar,baz solo --min-concurrency 2])
end
end
context 'with --timeout flag' do
it 'when given', 'starts Sidekiq workers with given timeout' do
expect(Gitlab::SidekiqCluster).to receive(:start)
.with([['foo'] + described_class::DEFAULT_QUEUES], default_options.merge(timeout: 10))
.and_return([])
cli.run(%w[foo --timeout 10])
end
it 'when not given', 'starts Sidekiq workers with default timeout' do
expect(Gitlab::SidekiqCluster).to receive(:start)
.with([['foo'] + described_class::DEFAULT_QUEUES], default_options.merge(timeout:
Gitlab::SidekiqCluster::DEFAULT_SOFT_TIMEOUT_SECONDS))
.and_return([])
cli.run(%w[foo])
end
end
context 'with --list-queues flag' do
it 'errors when given --list-queues and --dryrun' do
expect { cli.run(%w[foo --list-queues --dryrun]) }.to raise_error(described_class::CommandError)
end
it 'prints out a list of queues in alphabetical order' do
expected_queues = [
'default',
'epics:epics_update_epics_dates',
'epics_new_epic_issue',
'mailers',
'new_epic',
'todos_destroyer:todos_destroyer_confidential_epic'
]
allow(Gitlab::SidekiqConfig::CliMethods).to receive(:query_queues).and_return(expected_queues.shuffle)
expect(cli).to receive(:puts).with([expected_queues])
cli.run(%w[--queue-selector feature_category=epics --list-queues])
end
end
context 'queue namespace expansion' do
it 'starts Sidekiq workers for all queues in all_queues.yml with a namespace in argv' do
expect(Gitlab::SidekiqConfig::CliMethods).to receive(:worker_queues).and_return(['cronjob:foo', 'cronjob:bar'])
expect(Gitlab::SidekiqCluster).to receive(:start)
.with([['cronjob', 'cronjob:foo', 'cronjob:bar'] +
described_class::DEFAULT_QUEUES], default_options)
.and_return([])
cli.run(%w[cronjob])
end
end
context "with --queue-selector" do
where do
{
'memory-bound queues' => {
query: 'resource_boundary=memory',
included_queues: %w[project_export],
excluded_queues: %w[merge]
},
'memory- or CPU-bound queues' => {
query: 'resource_boundary=memory,cpu',
included_queues: %w[auto_merge:auto_merge_process project_export],
excluded_queues: %w[merge]
},
'high urgency CI queues' => {
query: 'feature_category=continuous_integration&urgency=high',
included_queues: %w[pipeline_default:ci_drop_pipeline],
excluded_queues: %w[merge]
},
'CPU-bound high urgency CI queues' => {
query: 'feature_category=continuous_integration&urgency=high&resource_boundary=cpu',
included_queues: %w[pipeline_default:ci_create_downstream_pipeline],
excluded_queues: %w[pipeline_default:ci_drop_pipeline merge]
},
'CPU-bound high urgency non-CI queues' => {
query: 'feature_category!=continuous_integration&urgency=high&resource_boundary=cpu',
included_queues: %w[new_issue],
excluded_queues: %w[pipeline_default:ci_create_downstream_pipeline]
},
'CI and SCM queues' => {
query: 'feature_category=continuous_integration|feature_category=source_code_management',
included_queues: %w[pipeline_default:ci_drop_pipeline merge],
excluded_queues: %w[]
}
}
end
with_them do
it 'expands queues by attributes' do
expect(Gitlab::SidekiqCluster).to receive(:start) do |queues, opts|
expect(opts).to eq(default_options)
expect(queues.first).to include(*included_queues)
expect(queues.first).not_to include(*excluded_queues)
expect(queues.first).to include(*described_class::DEFAULT_QUEUES)
[]
end
cli.run(%W[--queue-selector #{query}])
end
it 'works when negated' do
expect(Gitlab::SidekiqCluster).to receive(:start) do |queues, opts|
expect(opts).to eq(default_options)
expect(queues.first).not_to include(*included_queues)
expect(queues.first).to include(*excluded_queues)
expect(queues.first).to include(*described_class::DEFAULT_QUEUES)
[]
end
cli.run(%W[--negate --queue-selector #{query}])
end
end
it 'expands multiple queue groups correctly' do
expected_workers =
if Gitlab.ee?
[
%w[incident_management_close_incident status_page_publish] + described_class::DEFAULT_QUEUES,
%w[bulk_imports_pipeline bulk_imports_pipeline_batch bulk_imports_relation_batch_export bulk_imports_relation_export project_export projects_import_export_parallel_project_export projects_import_export_relation_export repository_import project_template_export] +
described_class::DEFAULT_QUEUES
]
else
[
%w[incident_management_close_incident] + described_class::DEFAULT_QUEUES,
%w[bulk_imports_pipeline bulk_imports_pipeline_batch bulk_imports_relation_batch_export bulk_imports_relation_export project_export projects_import_export_parallel_project_export projects_import_export_relation_export repository_import] +
described_class::DEFAULT_QUEUES
]
end
expect(Gitlab::SidekiqCluster)
.to receive(:start)
.with(expected_workers, default_options)
.and_return([])
cli.run(%w[--queue-selector feature_category=incident_management&has_external_dependencies=true resource_boundary=memory&feature_category=importers])
end
it 'allows the special * selector' do
worker_queues = %w[foo bar baz]
expect(Gitlab::SidekiqConfig::CliMethods)
.to receive(:worker_queues).and_return(worker_queues)
expect(Gitlab::SidekiqCluster)
.to receive(:start).with([worker_queues], default_options).and_return([])
cli.run(%w[--queue-selector *])
end
it 'errors when the selector matches no queues' do
expect(Gitlab::SidekiqCluster).not_to receive(:start)
expect { cli.run(%w[--queue-selector has_external_dependencies=true&has_external_dependencies=false]) }
.to raise_error(described_class::CommandError)
end
it 'errors on an invalid query multiple queue groups correctly' do
expect(Gitlab::SidekiqCluster).not_to receive(:start)
expect { cli.run(%w[--queue-selector unknown_field=chatops]) }
.to raise_error(Gitlab::SidekiqConfig::WorkerMatcher::QueryError)
end
end
context "with routing rules specified" do
before do
stub_config(sidekiq: { routing_rules: [['resource_boundary=cpu', 'foo']] })
end
it "starts Sidekiq workers only for given queues without any additional DEFAULT_QUEUES" do
expect(Gitlab::SidekiqCluster).to receive(:start)
.with([['foo']], default_options)
.and_return([])
cli.run(%w[foo])
end
end
context "with sidekiq settings not specified" do
before do
stub_config(sidekiq: nil)
end
it "does not throw an error" do
allow(Gitlab::SidekiqCluster).to receive(:start).and_return([])
expect { cli.run(%w[foo]) }.not_to raise_error
end
it "starts Sidekiq workers with given queues, and additional default and mailers queues (DEFAULT_QUEUES)" do
expect(Gitlab::SidekiqCluster).to receive(:start)
.with([['foo'] + described_class::DEFAULT_QUEUES], default_options)
.and_return([])
cli.run(%w[foo])
end
end
end
context 'metrics server' do
let(:trapped_signals) { described_class::TERMINATE_SIGNALS + described_class::FORWARD_SIGNALS }
let(:metrics_dir) { Dir.mktmpdir }
before do
stub_env('prometheus_multiproc_dir', metrics_dir)
end
after do
FileUtils.rm_rf(metrics_dir, secure: true)
end
context 'starting the server' do
before do
allow(Gitlab::SidekiqCluster).to receive(:start).and_return([])
end
context 'without --dryrun' do
it 'wipes the metrics directory before starting workers' do
expect(metrics_cleanup_service).to receive(:execute).ordered
expect(Gitlab::SidekiqCluster).to receive(:start).ordered.and_return([])
cli.run(%w[foo])
end
context 'when sidekiq_exporter is not set up' do
let(:config) do
{ 'sidekiq_exporter' => {} }
end
it 'does not start a sidekiq metrics server' do
expect(MetricsServer).not_to receive(:start_for_sidekiq)
cli.run(%w[foo])
end
end
context 'with missing sidekiq_exporter setting' do
let(:config) do
{ 'sidekiq_exporter' => nil }
end
it 'does not start a sidekiq metrics server' do
expect(MetricsServer).not_to receive(:start_for_sidekiq)
cli.run(%w[foo])
end
it 'does not throw an error' do
expect { cli.run(%w[foo]) }.not_to raise_error
end
end
context 'when sidekiq_exporter is disabled' do
it 'does not start a sidekiq metrics server' do
expect(MetricsServer).not_to receive(:start_for_sidekiq)
cli.run(%w[foo])
end
end
context 'when sidekiq_exporter is enabled' do
let(:sidekiq_exporter_enabled) { true }
it 'starts the metrics server' do
expect(MetricsServer).to receive(:start_for_sidekiq).with(metrics_dir: metrics_dir, reset_signals: trapped_signals)
cli.run(%w[foo])
end
end
context 'when a PID is specified' do
it 'writes the PID to a file' do
expect(Gitlab::ProcessManagement).to receive(:write_pid).with('/dev/null')
cli.option_parser.parse!(%w[-P /dev/null])
cli.run(%w[foo])
end
end
context 'when no PID is specified' do
it 'does not write a PID' do
expect(Gitlab::ProcessManagement).not_to receive(:write_pid)
cli.run(%w[foo])
end
end
end
context 'with --dryrun set' do
let(:sidekiq_exporter_enabled) { true }
it 'does not start the server' do
expect(MetricsServer).not_to receive(:start_for_sidekiq)
cli.run(%w[foo --dryrun])
end
end
end
end
context 'supervising the cluster' do
let(:sidekiq_exporter_enabled) { true }
let(:metrics_server_pid) { 99 }
let(:sidekiq_worker_pids) { [2, 42] }
let(:waiter_threads) { [instance_double('Process::Waiter'), instance_double('Process::Waiter')] }
let(:process_status) { instance_double('Process::Status') }
before do
allow(Gitlab::SidekiqCluster).to receive(:start).and_return(waiter_threads)
allow(process_status).to receive(:success?).and_return(true)
allow(cli).to receive(:exit)
waiter_threads.each.with_index do |thread, i|
allow(thread).to receive(:join)
allow(thread).to receive(:pid).and_return(sidekiq_worker_pids[i])
allow(thread).to receive(:value).and_return(process_status)
end
end
context 'when one of the workers has been terminated gracefully' do
it 'stops the entire process cluster' do
expect(MetricsServer).to receive(:start_for_sidekiq).once.and_return(metrics_server_pid)
expect(supervisor).to receive(:supervise).and_yield([2, 99])
expect(supervisor).to receive(:shutdown)
expect(cli).not_to receive(:exit).with(1)
cli.run(%w[foo])
end
end
context 'when one of the workers has failed' do
it 'stops the entire process cluster and exits with a non-zero code' do
expect(MetricsServer).to receive(:start_for_sidekiq).once.and_return(metrics_server_pid)
expect(supervisor).to receive(:supervise).and_yield([2, 99])
expect(supervisor).to receive(:shutdown)
expect(process_status).to receive(:success?).and_return(false)
expect(cli).to receive(:exit).with(1)
cli.run(%w[foo])
end
end
it 'stops the entire process cluster if one of the workers has been terminated' do
expect(MetricsServer).to receive(:start_for_sidekiq).once.and_return(metrics_server_pid)
expect(supervisor).to receive(:supervise).and_yield([2, 99])
expect(supervisor).to receive(:shutdown)
cli.run(%w[foo])
end
it 'restarts the metrics server when it is down' do
expect(supervisor).to receive(:supervise).and_yield([metrics_server_pid])
expect(MetricsServer).to receive(:start_for_sidekiq).twice.and_return(metrics_server_pid)
cli.run(%w[foo])
end
end
end
end
|