Commit d1439f58 authored by Maxime Orefice's avatar Maxime Orefice Committed by Shinya Maeda

Remove expired pipeline artifacts

This MR introduces a new service which will be used
to remove expired pipeline artifacts.
parent 7f6fc1bc
...@@ -161,7 +161,6 @@ module Ci ...@@ -161,7 +161,6 @@ module Ci
where(file_type: types) where(file_type: types)
end end
scope :expired, -> (limit) { where('expire_at < ?', Time.current).limit(limit) }
scope :downloadable, -> { where(file_type: DOWNLOADABLE_TYPES) } scope :downloadable, -> { where(file_type: DOWNLOADABLE_TYPES) }
scope :unlocked, -> { joins(job: :pipeline).merge(::Ci::Pipeline.unlocked).order(expire_at: :desc) } scope :unlocked, -> { joins(job: :pipeline).merge(::Ci::Pipeline.unlocked).order(expire_at: :desc) }
......
...@@ -17,6 +17,8 @@ module Ci ...@@ -17,6 +17,8 @@ module Ci
zip: 2, zip: 2,
gzip: 3 gzip: 3
}, _suffix: true }, _suffix: true
scope :expired, -> (limit) { where('expire_at < ?', Time.current).limit(limit) }
end end
def each_blob(&blk) def each_blob(&blk)
......
...@@ -20,18 +20,18 @@ module Ci ...@@ -20,18 +20,18 @@ module Ci
def execute def execute
in_lock(EXCLUSIVE_LOCK_KEY, ttl: LOCK_TIMEOUT, retries: 1) do in_lock(EXCLUSIVE_LOCK_KEY, ttl: LOCK_TIMEOUT, retries: 1) do
loop_until(timeout: LOOP_TIMEOUT, limit: LOOP_LIMIT) do loop_until(timeout: LOOP_TIMEOUT, limit: LOOP_LIMIT) do
destroy_batch destroy_batch(Ci::JobArtifact) || destroy_batch(Ci::PipelineArtifact)
end end
end end
end end
private private
def destroy_batch def destroy_batch(klass)
artifact_batch = if Gitlab::Ci::Features.destroy_only_unlocked_expired_artifacts_enabled? artifact_batch = if klass == Ci::JobArtifact && Gitlab::Ci::Features.destroy_only_unlocked_expired_artifacts_enabled?
Ci::JobArtifact.expired(BATCH_SIZE).unlocked klass.expired(BATCH_SIZE).unlocked
else else
Ci::JobArtifact.expired(BATCH_SIZE) klass.expired(BATCH_SIZE)
end end
artifacts = artifact_batch.to_a artifacts = artifact_batch.to_a
......
---
title: Add index for expire_at to ci_pipeline_artifacts
merge_request: 39882
author:
type: added
# frozen_string_literal: true
class AddIndexExpireAtToPipelineArtifacts < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'index_ci_pipeline_artifacts_on_expire_at'
disable_ddl_transaction!
def up
add_concurrent_index :ci_pipeline_artifacts, :expire_at, name: INDEX_NAME
end
def down
remove_concurrent_index_by_name(:ci_pipeline_artifacts, INDEX_NAME)
end
end
85b7ffba53c9cec30e9778dd806277ca8e9877c9a18dc1d6004402c0e66b8ef1
\ No newline at end of file
...@@ -19326,6 +19326,8 @@ CREATE INDEX index_ci_job_variables_on_job_id ON public.ci_job_variables USING b ...@@ -19326,6 +19326,8 @@ CREATE INDEX index_ci_job_variables_on_job_id ON public.ci_job_variables USING b
CREATE UNIQUE INDEX index_ci_job_variables_on_key_and_job_id ON public.ci_job_variables USING btree (key, job_id); CREATE UNIQUE INDEX index_ci_job_variables_on_key_and_job_id ON public.ci_job_variables USING btree (key, job_id);
CREATE INDEX index_ci_pipeline_artifacts_on_expire_at ON public.ci_pipeline_artifacts USING btree (expire_at);
CREATE INDEX index_ci_pipeline_artifacts_on_pipeline_id ON public.ci_pipeline_artifacts USING btree (pipeline_id); CREATE INDEX index_ci_pipeline_artifacts_on_pipeline_id ON public.ci_pipeline_artifacts USING btree (pipeline_id);
CREATE UNIQUE INDEX index_ci_pipeline_artifacts_on_pipeline_id_and_file_type ON public.ci_pipeline_artifacts USING btree (pipeline_id, file_type); CREATE UNIQUE INDEX index_ci_pipeline_artifacts_on_pipeline_id_and_file_type ON public.ci_pipeline_artifacts USING btree (pipeline_id, file_type);
......
...@@ -120,5 +120,25 @@ RSpec.describe Ci::DestroyExpiredJobArtifactsService, :clean_gitlab_redis_shared ...@@ -120,5 +120,25 @@ RSpec.describe Ci::DestroyExpiredJobArtifactsService, :clean_gitlab_redis_shared
expect { subject }.to change { Ci::JobArtifact.count }.by(-2) expect { subject }.to change { Ci::JobArtifact.count }.by(-2)
end end
end end
context 'when artifact is a pipeline artifact' do
context 'when artifacts are expired' do
let!(:pipeline_artifact_1) { create(:ci_pipeline_artifact, expire_at: 1.week.ago) }
let!(:pipeline_artifact_2) { create(:ci_pipeline_artifact, expire_at: 1.week.ago) }
it 'destroys pipeline artifacts' do
expect { subject }.to change { Ci::PipelineArtifact.count }.by(-2)
end
end
context 'when artifacts are not expired' do
let!(:pipeline_artifact_1) { create(:ci_pipeline_artifact, expire_at: 2.days) }
let!(:pipeline_artifact_2) { create(:ci_pipeline_artifact, expire_at: 2.days) }
it 'do not destroy pipeline artifacts' do
expect { subject }.not_to change { Ci::PipelineArtifact.count }
end
end
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment