Welcome to mirror list, hosted at ThFree Co, Russian Federation.

enqueuer_worker_spec.rb « migration « container_registry « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a57a9e3b2e8e7a59297718edda5b985b0372933c (plain)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ContainerRegistry::Migration::EnqueuerWorker, :aggregate_failures, :clean_gitlab_redis_shared_state do
  using RSpec::Parameterized::TableSyntax
  include ExclusiveLeaseHelpers

  let_it_be_with_reload(:container_repository) { create(:container_repository, created_at: 2.days.ago) }
  let_it_be(:importing_repository) { create(:container_repository, :importing) }
  let_it_be(:pre_importing_repository) { create(:container_repository, :pre_importing) }

  let(:worker) { described_class.new }

  before do
    stub_container_registry_config(enabled: true)
    stub_application_setting(container_registry_import_created_before: 1.day.ago)
    stub_container_registry_tags(repository: container_repository.path, tags: %w(tag1 tag2 tag3), with_manifest: true)
  end

  describe '#perform' do
    subject { worker.perform }

    shared_examples 'no action' do
      it 'does not queue or change any repositories' do
        expect(worker).not_to receive(:handle_next_migration)
        expect(worker).not_to receive(:handle_aborted_migration)

        subject

        expect(container_repository.reload).to be_default
      end
    end

    context 'with container_registry_migration_phase2_enqueuer_loop disabled' do
      before do
        stub_feature_flags(container_registry_migration_phase2_enqueuer_loop: false)
      end

      shared_examples 're-enqueuing based on capacity' do |capacity_limit: 4|
        context 'below capacity' do
          before do
            allow(ContainerRegistry::Migration).to receive(:capacity).and_return(capacity_limit)
          end

          it 're-enqueues the worker' do
            expect(described_class).to receive(:perform_async)
            expect(described_class).to receive(:perform_in).with(7.seconds)

            subject
          end

          context 'enqueue_twice feature flag disabled' do
            before do
              stub_feature_flags(container_registry_migration_phase2_enqueue_twice: false)
            end

            it 'only enqueues the worker once' do
              expect(described_class).to receive(:perform_async)
              expect(described_class).not_to receive(:perform_in)

              subject
            end
          end
        end

        context 'above capacity' do
          before do
            allow(ContainerRegistry::Migration).to receive(:capacity).and_return(-1)
          end

          it 'does not re-enqueue the worker' do
            expect(described_class).not_to receive(:perform_async)
            expect(described_class).not_to receive(:perform_in).with(7.seconds)

            subject
          end
        end
      end

      context 'with qualified repository' do
        before do
          allow_worker(on: :next_repository) do |repository|
            allow(repository).to receive(:migration_pre_import).and_return(:ok)
          end
        end

        shared_examples 'starting the next import' do
          it 'starts the pre-import for the next qualified repository' do
            expect_log_extra_metadata(
              import_type: 'next',
              container_repository_id: container_repository.id,
              container_repository_path: container_repository.path,
              container_repository_migration_state: 'pre_importing'
            )

            expect { subject }.to make_queries_matching(/LIMIT 2/)

            expect(container_repository.reload).to be_pre_importing
          end
        end

        it_behaves_like 'starting the next import'

        context 'when the new pre-import maxes out the capacity' do
          before do
            # set capacity to 10
            stub_feature_flags(
              container_registry_migration_phase2_capacity_25: false
            )

            # Plus 2 created above gives 9 importing repositories
            create_list(:container_repository, 7, :importing)
          end

          it 'does not re-enqueue the worker' do
            expect(described_class).not_to receive(:perform_async)
            expect(described_class).not_to receive(:perform_in)

            subject
          end
        end

        it_behaves_like 're-enqueuing based on capacity'

        context 'max tag count is 0' do
          before do
            stub_application_setting(container_registry_import_max_tags_count: 0)
            # Add 8 tags to the next repository
            stub_container_registry_tags(
              repository: container_repository.path, tags: %w(a b c d e f g h), with_manifest: true
            )
          end

          it_behaves_like 'starting the next import'
        end
      end

      context 'migrations are disabled' do
        before do
          allow(ContainerRegistry::Migration).to receive(:enabled?).and_return(false)
        end

        it_behaves_like 'no action' do
          before do
            expect_log_extra_metadata(migration_enabled: false)
          end
        end
      end

      context 'above capacity' do
        before do
          create(:container_repository, :importing)
          create(:container_repository, :importing)
          allow(ContainerRegistry::Migration).to receive(:capacity).and_return(1)
        end

        it_behaves_like 'no action' do
          before do
            expect_log_extra_metadata(below_capacity: false, max_capacity_setting: 1)
          end
        end

        it 'does not re-enqueue the worker' do
          expect(ContainerRegistry::Migration::EnqueuerWorker).not_to receive(:perform_async)
          expect(ContainerRegistry::Migration::EnqueuerWorker).not_to receive(:perform_in)

          subject
        end
      end

      context 'too soon before previous completed import step' do
        where(:state, :timestamp) do
          :import_done     | :migration_import_done_at
          :pre_import_done | :migration_pre_import_done_at
          :import_aborted  | :migration_aborted_at
          :import_skipped  | :migration_skipped_at
        end

        with_them do
          before do
            allow(ContainerRegistry::Migration).to receive(:enqueue_waiting_time).and_return(45.minutes)
            create(:container_repository, state, timestamp => 1.minute.ago)
          end

          it_behaves_like 'no action' do
            before do
              expect_log_extra_metadata(waiting_time_passed: false, current_waiting_time_setting: 45.minutes)
            end
          end
        end

        context 'when last completed repository has nil timestamps' do
          before do
            allow(ContainerRegistry::Migration).to receive(:enqueue_waiting_time).and_return(45.minutes)
            create(:container_repository, migration_state: 'import_done')
          end

          it 'continues to try the next import' do
            expect { subject }.to change { container_repository.reload.migration_state }
          end
        end
      end

      context 'when an aborted import is available' do
        let_it_be(:aborted_repository) { create(:container_repository, :import_aborted) }

        context 'with a successful registry request' do
          before do
            allow_worker(on: :next_aborted_repository) do |repository|
              allow(repository).to receive(:migration_import).and_return(:ok)
              allow(repository.gitlab_api_client).to receive(:import_status).and_return('import_failed')
            end
          end

          it 'retries the import for the aborted repository' do
            expect_log_extra_metadata(
              import_type: 'retry',
              container_repository_id: aborted_repository.id,
              container_repository_path: aborted_repository.path,
              container_repository_migration_state: 'importing'
            )

            subject

            expect(aborted_repository.reload).to be_importing
            expect(container_repository.reload).to be_default
          end

          it_behaves_like 're-enqueuing based on capacity'
        end

        context 'when an error occurs' do
          it 'does not abort that migration' do
            allow_worker(on: :next_aborted_repository) do |repository|
              allow(repository).to receive(:retry_aborted_migration).and_raise(StandardError)
            end

            expect_log_extra_metadata(
              import_type: 'retry',
              container_repository_id: aborted_repository.id,
              container_repository_path: aborted_repository.path,
              container_repository_migration_state: 'import_aborted'
            )

            subject

            expect(aborted_repository.reload).to be_import_aborted
            expect(container_repository.reload).to be_default
          end
        end
      end

      context 'when no repository qualifies' do
        include_examples 'an idempotent worker' do
          before do
            allow(ContainerRepository).to receive(:ready_for_import).and_return(ContainerRepository.none)
          end

          it_behaves_like 'no action'
        end
      end

      context 'over max tag count' do
        before do
          stub_application_setting(container_registry_import_max_tags_count: 2)
        end

        it 'skips the repository' do
          expect_log_extra_metadata(
            import_type: 'next',
            container_repository_id: container_repository.id,
            container_repository_path: container_repository.path,
            container_repository_migration_state: 'import_skipped',
            tags_count_too_high: true,
            max_tags_count_setting: 2
          )

          subject

          expect(container_repository.reload).to be_import_skipped
          expect(container_repository.migration_skipped_reason).to eq('too_many_tags')
          expect(container_repository.migration_skipped_at).not_to be_nil
        end

        context 're-enqueuing' do
          before do
            # skipping will also re-enqueue, so we isolate the capacity behavior here
            allow_worker(on: :next_repository) do |repository|
              allow(repository).to receive(:skip_import).and_return(true)
            end
          end

          it_behaves_like 're-enqueuing based on capacity', capacity_limit: 3
        end
      end

      context 'when an error occurs' do
        before do
          allow(ContainerRegistry::Migration).to receive(:max_tags_count).and_raise(StandardError)
        end

        it 'aborts the import' do
          expect_log_extra_metadata(
            import_type: 'next',
            container_repository_id: container_repository.id,
            container_repository_path: container_repository.path,
            container_repository_migration_state: 'import_aborted'
          )

          expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
            instance_of(StandardError),
            next_repository_id: container_repository.id
          )

          subject

          expect(container_repository.reload).to be_import_aborted
        end
      end

      context 'with the exclusive lease taken' do
        let(:lease_key) { worker.send(:lease_key) }

        before do
          stub_exclusive_lease_taken(lease_key, timeout: 30.minutes)
        end

        it 'does not perform' do
          expect(worker).not_to receive(:runnable?)
          expect(worker).not_to receive(:re_enqueue_if_capacity)

          subject
        end
      end
    end

    context 'with container_registry_migration_phase2_enqueuer_loop enabled' do
      context 'migrations are disabled' do
        before do
          allow(ContainerRegistry::Migration).to receive(:enabled?).and_return(false)
        end

        it_behaves_like 'no action' do
          before do
            expect_log_extra_metadata(migration_enabled: false)
          end
        end
      end

      context 'with no repository qualifies' do
        include_examples 'an idempotent worker' do
          before do
            allow(ContainerRepository).to receive(:ready_for_import).and_return(ContainerRepository.none)
          end

          it_behaves_like 'no action'
        end
      end

      context 'when multiple aborted imports are available' do
        let_it_be(:aborted_repository1) { create(:container_repository, :import_aborted) }
        let_it_be(:aborted_repository2) { create(:container_repository, :import_aborted) }

        before do
          container_repository.update!(created_at: 30.seconds.ago)
        end

        context 'with successful registry requests' do
          before do
            allow_worker(on: :next_aborted_repository) do |repository|
              allow(repository).to receive(:migration_import).and_return(:ok)
              allow(repository.gitlab_api_client).to receive(:import_status).and_return('import_failed')
            end
          end

          it 'retries the import for the aborted repository' do
            expect_log_info(
              [
                {
                  import_type: 'retry',
                  container_repository_id: aborted_repository1.id,
                  container_repository_path: aborted_repository1.path,
                  container_repository_migration_state: 'importing'
                },
                {
                  import_type: 'retry',
                  container_repository_id: aborted_repository2.id,
                  container_repository_path: aborted_repository2.path,
                  container_repository_migration_state: 'importing'
                }
              ]
            )

            expect(worker).to receive(:handle_next_migration).and_call_original

            subject

            expect(aborted_repository1.reload).to be_importing
            expect(aborted_repository2.reload).to be_importing
          end
        end

        context 'when an error occurs' do
          it 'does abort that migration' do
            allow_worker(on: :next_aborted_repository) do |repository|
              allow(repository).to receive(:retry_aborted_migration).and_raise(StandardError)
            end

            expect_log_info(
              [
                {
                  import_type: 'retry',
                  container_repository_id: aborted_repository1.id,
                  container_repository_path: aborted_repository1.path,
                  container_repository_migration_state: 'import_aborted'
                }
              ]
            )

            subject

            expect(aborted_repository1.reload).to be_import_aborted
            expect(aborted_repository2.reload).to be_import_aborted
          end
        end
      end

      context 'when multiple qualified repositories are available' do
        let_it_be(:container_repository2) { create(:container_repository, created_at: 2.days.ago) }

        before do
          allow_worker(on: :next_repository) do |repository|
            allow(repository).to receive(:migration_pre_import).and_return(:ok)
          end

          stub_container_registry_tags(
            repository: container_repository2.path,
            tags: %w(tag4 tag5 tag6),
            with_manifest: true
          )
        end

        shared_examples 'starting all the next imports' do
          it 'starts the pre-import for the next qualified repositories' do
            expect_log_info(
              [
                {
                  import_type: 'next',
                  container_repository_id: container_repository.id,
                  container_repository_path: container_repository.path,
                  container_repository_migration_state: 'pre_importing'
                },
                {
                  import_type: 'next',
                  container_repository_id: container_repository2.id,
                  container_repository_path: container_repository2.path,
                  container_repository_migration_state: 'pre_importing'
                }
              ]
            )

            expect(worker).to receive(:handle_next_migration).exactly(3).times.and_call_original

            expect { subject }.to make_queries_matching(/LIMIT 2/)

            expect(container_repository.reload).to be_pre_importing
            expect(container_repository2.reload).to be_pre_importing
          end
        end

        it_behaves_like 'starting all the next imports'

        context 'when the new pre-import maxes out the capacity' do
          before do
            # set capacity to 10
            stub_feature_flags(
              container_registry_migration_phase2_capacity_25: false
            )

            # Plus 2 created above gives 9 importing repositories
            create_list(:container_repository, 7, :importing)
          end

          it 'starts the pre-import only for one qualified repository' do
            expect_log_info(
              [
                {
                  import_type: 'next',
                  container_repository_id: container_repository.id,
                  container_repository_path: container_repository.path,
                  container_repository_migration_state: 'pre_importing'
                }
              ]
            )

            subject

            expect(container_repository.reload).to be_pre_importing
            expect(container_repository2.reload).to be_default
          end
        end

        context 'max tag count is 0' do
          before do
            stub_application_setting(container_registry_import_max_tags_count: 0)
            # Add 8 tags to the next repository
            stub_container_registry_tags(
              repository: container_repository.path, tags: %w(a b c d e f g h), with_manifest: true
            )
          end

          it_behaves_like 'starting all the next imports'
        end

        context 'when the deadline is hit' do
          it 'does not handle the second qualified repository' do
            expect(worker).to receive(:loop_deadline).and_return(5.seconds.from_now, 2.seconds.ago)
            expect(worker).to receive(:handle_next_migration).once.and_call_original

            subject

            expect(container_repository.reload).to be_pre_importing
            expect(container_repository2.reload).to be_default
          end
        end
      end

      context 'when a mix of aborted imports and qualified repositories are available' do
        let_it_be(:aborted_repository) { create(:container_repository, :import_aborted) }

        before do
          allow_worker(on: :next_aborted_repository) do |repository|
            allow(repository).to receive(:migration_import).and_return(:ok)
            allow(repository.gitlab_api_client).to receive(:import_status).and_return('import_failed')
          end

          allow_worker(on: :next_repository) do |repository|
            allow(repository).to receive(:migration_pre_import).and_return(:ok)
          end
        end

        it 'retries the aborted repository and start the migration on the qualified repository' do
          expect_log_info(
            [
              {
                import_type: 'retry',
                container_repository_id: aborted_repository.id,
                container_repository_path: aborted_repository.path,
                container_repository_migration_state: 'importing'
              },
              {
                import_type: 'next',
                container_repository_id: container_repository.id,
                container_repository_path: container_repository.path,
                container_repository_migration_state: 'pre_importing'
              }
            ]
          )

          subject

          expect(aborted_repository.reload).to be_importing
          expect(container_repository.reload).to be_pre_importing
        end
      end

      context 'above capacity' do
        before do
          create(:container_repository, :importing)
          create(:container_repository, :importing)
          allow(ContainerRegistry::Migration).to receive(:capacity).and_return(1)
        end

        it_behaves_like 'no action' do
          before do
            expect_log_extra_metadata(below_capacity: false, max_capacity_setting: 1)
          end
        end
      end

      context 'too soon before previous completed import step' do
        where(:state, :timestamp) do
          :import_done     | :migration_import_done_at
          :pre_import_done | :migration_pre_import_done_at
          :import_aborted  | :migration_aborted_at
          :import_skipped  | :migration_skipped_at
        end

        with_them do
          before do
            allow(ContainerRegistry::Migration).to receive(:enqueue_waiting_time).and_return(45.minutes)
            create(:container_repository, state, timestamp => 1.minute.ago)
          end

          it_behaves_like 'no action' do
            before do
              expect_log_extra_metadata(waiting_time_passed: false, current_waiting_time_setting: 45.minutes)
            end
          end
        end

        context 'when last completed repository has nil timestamps' do
          before do
            allow(ContainerRegistry::Migration).to receive(:enqueue_waiting_time).and_return(45.minutes)
            create(:container_repository, migration_state: 'import_done')
          end

          it 'continues to try the next import' do
            expect { subject }.to change { container_repository.reload.migration_state }
          end
        end
      end

      context 'over max tag count' do
        before do
          stub_application_setting(container_registry_import_max_tags_count: 2)
        end

        it 'skips the repository' do
          expect_log_info(
            [
              {
                import_type: 'next',
                container_repository_id: container_repository.id,
                container_repository_path: container_repository.path,
                container_repository_migration_state: 'import_skipped',
                container_repository_migration_skipped_reason: 'too_many_tags'
              }
            ]
          )

          expect(worker).to receive(:handle_next_migration).twice.and_call_original
          # skipping the migration will re_enqueue the job
          expect(described_class).to receive(:enqueue_a_job)

          subject

          expect(container_repository.reload).to be_import_skipped
          expect(container_repository.migration_skipped_reason).to eq('too_many_tags')
          expect(container_repository.migration_skipped_at).not_to be_nil
        end
      end

      context 'when an error occurs' do
        before do
          allow(ContainerRegistry::Migration).to receive(:max_tags_count).and_raise(StandardError)
        end

        it 'aborts the import' do
          expect_log_info(
            [
              {
                import_type: 'next',
                container_repository_id: container_repository.id,
                container_repository_path: container_repository.path,
                container_repository_migration_state: 'import_aborted'
              }
            ]
          )

          expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
            instance_of(StandardError),
            next_repository_id: container_repository.id
          )

          # aborting the migration will re_enqueue the job
          expect(described_class).to receive(:enqueue_a_job)

          subject

          expect(container_repository.reload).to be_import_aborted
        end
      end

      context 'with the exclusive lease taken' do
        let(:lease_key) { worker.send(:lease_key) }

        before do
          stub_exclusive_lease_taken(lease_key, timeout: 30.minutes)
        end

        it 'does not perform' do
          expect(worker).not_to receive(:handle_aborted_migration)
          expect(worker).not_to receive(:handle_next_migration)

          subject
        end
      end
    end

    def expect_log_extra_metadata(metadata)
      metadata.each do |key, value|
        expect(worker).to receive(:log_extra_metadata_on_done).with(key, value)
      end
    end

    def expect_log_info(expected_multiple_arguments)
      expected_multiple_arguments.each do |extras|
        expect(worker.logger).to receive(:info).with(worker.structured_payload(extras))
      end
    end

    def allow_worker(on:)
      method_repository = worker.method(on)
      allow(worker).to receive(on) do
        repository = method_repository.call

        yield repository if repository

        repository
      end
    end
  end

  describe 'worker attributes' do
    it 'has deduplication set' do
      expect(described_class.get_deduplicate_strategy).to eq(:until_executing)
      expect(described_class.get_deduplication_options).to include(ttl: 30.minutes)
    end
  end
end