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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::JobArtifacts::DestroyBatchService do
let(:artifacts) { Ci::JobArtifact.where(id: [artifact_with_file.id, artifact_without_file.id, trace_artifact.id]) }
let(:skip_projects_on_refresh) { false }
let(:service) do
described_class.new(
artifacts,
pick_up_at: Time.current,
skip_projects_on_refresh: skip_projects_on_refresh
)
end
let_it_be(:artifact_with_file, refind: true) do
create(:ci_job_artifact, :zip)
end
let_it_be(:artifact_without_file, refind: true) do
create(:ci_job_artifact)
end
let_it_be(:undeleted_artifact, refind: true) do
create(:ci_job_artifact)
end
let_it_be(:trace_artifact, refind: true) do
create(:ci_job_artifact, :trace, :expired)
end
describe '.execute' do
subject(:execute) { service.execute }
it 'creates a deleted object for artifact with attached file' do
expect { subject }.to change { Ci::DeletedObject.count }.by(1)
end
it 'does not remove the attached file' do
expect { execute }.not_to change { artifact_with_file.file.exists? }
end
it 'deletes the artifact records and logs them' do
expect(Gitlab::Ci::Artifacts::Logger)
.to receive(:log_deleted)
.with(
match_array([artifact_with_file, artifact_without_file]),
'Ci::JobArtifacts::DestroyBatchService#execute'
)
expect { subject }.to change { Ci::JobArtifact.count }.by(-2)
end
it 'reports metrics for destroyed artifacts' do
expect_next_instance_of(Gitlab::Ci::Artifacts::Metrics) do |metrics|
expect(metrics).to receive(:increment_destroyed_artifacts_count).with(2).and_call_original
expect(metrics).to receive(:increment_destroyed_artifacts_bytes).with(107464).and_call_original
end
execute
end
it 'preserves trace artifacts and removes any timestamp' do
expect { subject }
.to change { trace_artifact.reload.expire_at }.from(trace_artifact.expire_at).to(nil)
.and not_change { Ci::JobArtifact.exists?(trace_artifact.id) }
end
context 'when artifact belongs to a project that is undergoing stats refresh' do
let!(:artifact_under_refresh_1) do
create(:ci_job_artifact, :zip)
end
let!(:artifact_under_refresh_2) do
create(:ci_job_artifact, :zip)
end
let!(:artifact_under_refresh_3) do
create(:ci_job_artifact, :zip, project: artifact_under_refresh_2.project)
end
let(:artifacts) do
Ci::JobArtifact.where(id: [artifact_with_file.id, artifact_under_refresh_1.id, artifact_under_refresh_2.id,
artifact_under_refresh_3.id])
end
before do
create(:project_build_artifacts_size_refresh, :created, project: artifact_with_file.project)
create(:project_build_artifacts_size_refresh, :pending, project: artifact_under_refresh_1.project)
create(:project_build_artifacts_size_refresh, :running, project: artifact_under_refresh_2.project)
end
shared_examples 'avoiding N+1 queries' do
let!(:control_artifact_on_refresh) do
create(:ci_job_artifact, :zip)
end
let!(:control_artifact_non_refresh) do
create(:ci_job_artifact, :zip)
end
let!(:other_artifact_on_refresh) do
create(:ci_job_artifact, :zip)
end
let!(:other_artifact_on_refresh_2) do
create(:ci_job_artifact, :zip)
end
let!(:other_artifact_non_refresh) do
create(:ci_job_artifact, :zip)
end
let!(:control_artifacts) do
Ci::JobArtifact.where(
id: [
control_artifact_on_refresh.id,
control_artifact_non_refresh.id
]
)
end
let!(:artifacts) do
Ci::JobArtifact.where(
id: [
other_artifact_on_refresh.id,
other_artifact_on_refresh_2.id,
other_artifact_non_refresh.id
]
)
end
let(:control_service) do
described_class.new(
control_artifacts,
pick_up_at: Time.current,
skip_projects_on_refresh: skip_projects_on_refresh
)
end
before do
create(:project_build_artifacts_size_refresh, :pending, project: control_artifact_on_refresh.project)
create(:project_build_artifacts_size_refresh, :pending, project: other_artifact_on_refresh.project)
create(:project_build_artifacts_size_refresh, :pending, project: other_artifact_on_refresh_2.project)
end
it 'does not make multiple queries when fetching multiple project refresh records' do
control = ActiveRecord::QueryRecorder.new { control_service.execute }
expect { subject }.not_to exceed_query_limit(control)
end
end
context 'and skip_projects_on_refresh is set to false (default)' do
it 'logs the projects undergoing refresh and continues with the delete', :aggregate_failures do
expect(Gitlab::ProjectStatsRefreshConflictsLogger).to receive(:warn_artifact_deletion_during_stats_refresh).with(
method: 'Ci::JobArtifacts::DestroyBatchService#execute',
project_id: artifact_under_refresh_1.project.id
).once
expect(Gitlab::ProjectStatsRefreshConflictsLogger).to receive(:warn_artifact_deletion_during_stats_refresh).with(
method: 'Ci::JobArtifacts::DestroyBatchService#execute',
project_id: artifact_under_refresh_2.project.id
).once
expect { subject }.to change { Ci::JobArtifact.count }.by(-4)
end
it_behaves_like 'avoiding N+1 queries'
end
context 'and skip_projects_on_refresh is set to true' do
let(:skip_projects_on_refresh) { true }
it 'logs the projects undergoing refresh and excludes the artifacts from deletion', :aggregate_failures do
expect(Gitlab::ProjectStatsRefreshConflictsLogger).to receive(:warn_skipped_artifact_deletion_during_stats_refresh).with(
method: 'Ci::JobArtifacts::DestroyBatchService#execute',
project_ids: match_array([artifact_under_refresh_1.project.id, artifact_under_refresh_2.project.id])
)
expect { subject }.to change { Ci::JobArtifact.count }.by(-1)
expect(Ci::JobArtifact.where(id: artifact_under_refresh_1.id)).to exist
expect(Ci::JobArtifact.where(id: artifact_under_refresh_2.id)).to exist
expect(Ci::JobArtifact.where(id: artifact_under_refresh_3.id)).to exist
end
it_behaves_like 'avoiding N+1 queries'
end
end
context 'when artifact belongs to a project not undergoing refresh' do
context 'and skip_projects_on_refresh is set to false (default)' do
it 'does not log any warnings', :aggregate_failures do
expect(Gitlab::ProjectStatsRefreshConflictsLogger).not_to receive(:warn_artifact_deletion_during_stats_refresh)
expect { subject }.to change { Ci::JobArtifact.count }.by(-2)
end
end
context 'and skip_projects_on_refresh is set to true' do
let(:skip_projects_on_refresh) { true }
it 'does not log any warnings', :aggregate_failures do
expect(Gitlab::ProjectStatsRefreshConflictsLogger).not_to receive(:warn_skipped_artifact_deletion_during_stats_refresh)
expect { subject }.to change { Ci::JobArtifact.count }.by(-2)
end
end
end
context 'ProjectStatistics' do
it 'resets project statistics' do
expect(ProjectStatistics).to receive(:increment_statistic).once
.with(artifact_with_file.project, :build_artifacts_size, -artifact_with_file.file.size)
.and_call_original
expect(ProjectStatistics).to receive(:increment_statistic).once
.with(artifact_without_file.project, :build_artifacts_size, 0)
.and_call_original
execute
end
context 'with update_stats: false' do
let_it_be(:extra_artifact_with_file) do
create(:ci_job_artifact, :zip, project: artifact_with_file.project)
end
let(:artifacts) do
Ci::JobArtifact.where(id: [artifact_with_file.id, extra_artifact_with_file.id,
artifact_without_file.id, trace_artifact.id])
end
it 'does not update project statistics' do
expect(ProjectStatistics).not_to receive(:increment_statistic)
service.execute(update_stats: false)
end
it 'returns size statistics' do
expected_updates = {
statistics_updates: {
artifact_with_file.project => -(artifact_with_file.file.size + extra_artifact_with_file.file.size),
artifact_without_file.project => 0
}
}
expect(service.execute(update_stats: false)).to match(
a_hash_including(expected_updates))
end
end
end
context 'when failed to destroy artifact' do
context 'when the import fails' do
before do
expect(Ci::DeletedObject)
.to receive(:bulk_import)
.once
.and_raise(ActiveRecord::RecordNotDestroyed)
end
it 'raises an exception and stop destroying' do
expect { execute }.to raise_error(ActiveRecord::RecordNotDestroyed)
.and not_change { Ci::JobArtifact.count }
end
end
end
context 'when there are no artifacts' do
let(:artifacts) { Ci::JobArtifact.none }
it 'does not raise error' do
expect { execute }.not_to raise_error
end
it 'reports the number of destroyed artifacts' do
is_expected.to eq(destroyed_artifacts_count: 0, statistics_updates: {}, status: :success)
end
end
context 'with artifacts that has backfilled expire_at' do
let!(:created_on_00_30_45_minutes_on_21_22_23) do
[
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-21 00:00:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-21 01:30:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-22 12:00:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-22 12:30:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-23 23:00:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-23 23:30:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-23 06:45:00.000'))
]
end
let!(:created_close_to_00_or_30_minutes) do
[
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-21 00:00:00.001')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-21 00:30:00.999'))
]
end
let!(:created_on_00_or_30_minutes_on_other_dates) do
[
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-01 00:00:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-19 12:00:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-24 23:30:00.000'))
]
end
let!(:created_at_other_times) do
[
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-19 00:00:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-19 00:30:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-24 00:00:00.000')),
create(:ci_job_artifact, expire_at: Time.zone.parse('2022-01-24 00:30:00.000'))
]
end
let(:artifacts_to_keep) { created_on_00_30_45_minutes_on_21_22_23 }
let(:artifacts_to_delete) { created_close_to_00_or_30_minutes + created_on_00_or_30_minutes_on_other_dates + created_at_other_times }
let(:all_artifacts) { artifacts_to_keep + artifacts_to_delete }
let(:artifacts) { Ci::JobArtifact.where(id: all_artifacts.map(&:id)) }
it 'deletes job artifacts that do not have expire_at on 00, 30 or 45 minute of 21, 22, 23 of the month' do
expect { subject }.to change { Ci::JobArtifact.count }.by(artifacts_to_delete.size * -1)
end
it 'keeps job artifacts that have expire_at on 00, 30 or 45 minute of 21, 22, 23 of the month' do
expect { subject }.not_to change { Ci::JobArtifact.where(id: artifacts_to_keep.map(&:id)).count }
end
it 'removes expire_at on job artifacts that have expire_at on 00, 30 or 45 minute of 21, 22, 23 of the month' do
subject
expect(artifacts_to_keep.all? { |artifact| artifact.reload.expire_at.nil? }).to be(true)
end
context 'when feature flag is disabled' do
before do
stub_feature_flags(ci_detect_wrongly_expired_artifacts: false)
end
it 'deletes all job artifacts' do
expect { subject }.to change { Ci::JobArtifact.count }.by(all_artifacts.size * -1)
end
end
context 'when fix_expire_at is false' do
let(:service) { described_class.new(artifacts, pick_up_at: Time.current, fix_expire_at: false) }
it 'deletes all job artifacts' do
expect { subject }.to change { Ci::JobArtifact.count }.by(all_artifacts.size * -1)
end
end
end
end
end
|