summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/background_migration/batched_migration_wrapper_spec.rb
blob: d6c984c7adb72d103ba04fad541183521d995a2f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigrationWrapper, '#perform' do
  subject { described_class.new.perform(job_record) }

  let(:job_class) { Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJob }

  let_it_be(:pause_ms) { 250 }
  let_it_be(:active_migration) { create(:batched_background_migration, :active, job_arguments: [:id, :other_id]) }

  let!(:job_record) do
    create(:batched_background_migration_job,
           batched_migration: active_migration,
           pause_ms: pause_ms
          )
  end

  let(:job_instance) { double('job instance', batch_metrics: {}) }

  before do
    allow(job_class).to receive(:new).and_return(job_instance)
  end

  it 'runs the migration job' do
    expect(job_instance).to receive(:perform).with(1, 10, 'events', 'id', 1, pause_ms, 'id', 'other_id')

    subject
  end

  it 'updates the tracking record in the database' do
    test_metrics = { 'my_metris' => 'some value' }

    expect(job_instance).to receive(:perform)
    expect(job_instance).to receive(:batch_metrics).and_return(test_metrics)

    freeze_time do
      subject

      reloaded_job_record = job_record.reload

      expect(reloaded_job_record).not_to be_pending
      expect(reloaded_job_record.attempts).to eq(1)
      expect(reloaded_job_record.started_at).to eq(Time.current)
      expect(reloaded_job_record.metrics).to eq(test_metrics)
    end
  end

  context 'when running a job that failed previously' do
    let!(:job_record) do
      create(:batched_background_migration_job, :failed,
        batched_migration: active_migration,
        pause_ms: pause_ms,
        attempts: 1,
        finished_at: 1.hour.ago,
        metrics: { 'my_metrics' => 'some_value' }
      )
    end

    it 'increments attempts and updates other fields' do
      updated_metrics = { 'updated_metrics' => 'some_value' }

      expect(job_instance).to receive(:perform)
      expect(job_instance).to receive(:batch_metrics).and_return(updated_metrics)

      freeze_time do
        subject

        job_record.reload

        expect(job_record).not_to be_failed
        expect(job_record.attempts).to eq(2)
        expect(job_record.started_at).to eq(Time.current)
        expect(job_record.finished_at).to eq(Time.current)
        expect(job_record.metrics).to eq(updated_metrics)
      end
    end
  end

  context 'reporting prometheus metrics' do
    let(:labels) { job_record.batched_migration.prometheus_labels }

    before do
      allow(job_instance).to receive(:perform)
    end

    it 'reports batch_size' do
      expect(described_class.metrics[:gauge_batch_size]).to receive(:set).with(labels, job_record.batch_size)

      subject
    end

    it 'reports sub_batch_size' do
      expect(described_class.metrics[:gauge_sub_batch_size]).to receive(:set).with(labels, job_record.sub_batch_size)

      subject
    end

    it 'reports interval' do
      expect(described_class.metrics[:gauge_interval]).to receive(:set).with(labels, job_record.batched_migration.interval)

      subject
    end

    it 'reports updated tuples (currently based on batch_size)' do
      expect(described_class.metrics[:counter_updated_tuples]).to receive(:increment).with(labels, job_record.batch_size)

      subject
    end

    it 'reports migrated tuples' do
      count = double
      expect(job_record.batched_migration).to receive(:migrated_tuple_count).and_return(count)
      expect(described_class.metrics[:gauge_migrated_tuples]).to receive(:set).with(labels, count)

      subject
    end

    it 'reports summary of query timings' do
      metrics = { 'timings' => { 'update_all' => [1, 2, 3, 4, 5] } }

      expect(job_instance).to receive(:batch_metrics).and_return(metrics)

      metrics['timings'].each do |key, timings|
        summary_labels = labels.merge(operation: key)
        timings.each do |timing|
          expect(described_class.metrics[:histogram_timings]).to receive(:observe).with(summary_labels, timing)
        end
      end

      subject
    end

    it 'reports job duration' do
      freeze_time do
        expect(Time).to receive(:current).and_return(Time.zone.now - 5.seconds).ordered
        allow(Time).to receive(:current).and_call_original

        expect(described_class.metrics[:gauge_job_duration]).to receive(:set).with(labels, 5.seconds)

        subject
      end
    end

    it 'reports the total tuple count for the migration' do
      expect(described_class.metrics[:gauge_total_tuple_count]).to receive(:set).with(labels, job_record.batched_migration.total_tuple_count)

      subject
    end

    it 'reports last updated at timestamp' do
      freeze_time do
        expect(described_class.metrics[:gauge_last_update_time]).to receive(:set).with(labels, Time.current.to_i)

        subject
      end
    end
  end

  context 'when the migration job does not raise an error' do
    it 'marks the tracking record as succeeded' do
      expect(job_instance).to receive(:perform).with(1, 10, 'events', 'id', 1, pause_ms, 'id', 'other_id')

      freeze_time do
        subject

        reloaded_job_record = job_record.reload

        expect(reloaded_job_record).to be_succeeded
        expect(reloaded_job_record.finished_at).to eq(Time.current)
      end
    end
  end

  context 'when the migration job raises an error' do
    shared_examples 'an error is raised' do |error_class|
      it 'marks the tracking record as failed' do
        expect(job_instance).to receive(:perform)
          .with(1, 10, 'events', 'id', 1, pause_ms, 'id', 'other_id')
          .and_raise(error_class)

        freeze_time do
          expect { subject }.to raise_error(error_class)

          reloaded_job_record = job_record.reload

          expect(reloaded_job_record).to be_failed
          expect(reloaded_job_record.finished_at).to eq(Time.current)
        end
      end
    end

    it_behaves_like 'an error is raised', RuntimeError.new('Something broke!')
    it_behaves_like 'an error is raised', SignalException.new('SIGTERM')
    it_behaves_like 'an error is raised', ActiveRecord::StatementTimeout.new('Timeout!')
  end

  context 'when the batched background migration does not inherit from BaseJob' do
    let(:migration_class) { Class.new }

    before do
      stub_const('Gitlab::BackgroundMigration::Foo', migration_class)
    end

    let(:connection) { double(:connection) }
    let(:active_migration) { create(:batched_background_migration, :active, job_class_name: 'Foo') }
    let!(:job_record) { create(:batched_background_migration_job, batched_migration: active_migration) }

    it 'does not pass any argument' do
      expect(Gitlab::BackgroundMigration::Foo).to receive(:new).with(no_args).and_return(job_instance)

      expect(job_instance).to receive(:perform)

      described_class.new(connection: connection).perform(job_record)
    end
  end

  context 'when the batched background migration inherits from BaseJob' do
    let(:connection) { double(:connection) }
    let(:active_migration) { create(:batched_background_migration, :active, job_class_name: 'Foo') }
    let!(:job_record) { create(:batched_background_migration_job, batched_migration: active_migration) }

    let(:migration_class) { Class.new(::Gitlab::BackgroundMigration::BaseJob) }

    before do
      stub_const('Gitlab::BackgroundMigration::Foo', migration_class)
    end

    it 'passes the correct connection' do
      expect(Gitlab::BackgroundMigration::Foo).to receive(:new).with(connection: connection).and_return(job_instance)

      expect(job_instance).to receive(:perform)

      described_class.new(connection: connection).perform(job_record)
    end
  end
end