Commit b1c90b66 authored by Roger Rüttimann's avatar Roger Rüttimann

add a query variation which hopefully performs better for orphaned artifacts

parent c1903a84
Pipeline #53690750 failed with stages
in 69 minutes and 27 seconds
......@@ -85,6 +85,17 @@ module Ci
'', Ci::JobArtifact.select(1).where('ci_builds.id = ci_job_artifacts.job_id').archive)
end
# TODO: remove this if Ci::JobArtifact.orphaned_with_subquery is removed
scope :with_erasable_job_artifacts, -> do
types = Ci::JobArtifact.file_types.reject do |file_type|
Ci::JobArtifact::NON_ERASABLE_FILE_TYPES.include?(file_type)
end.values
where('EXISTS (?)', ::Ci::JobArtifact.select(1)
.where('ci_builds.id = ci_job_artifacts.job_id')
.where('ci_job_artifacts.file_type IN (?)', types))
end
scope :with_existing_job_artifacts, ->(query) do
where('EXISTS (?)', ::Ci::JobArtifact.select(1).where('ci_builds.id = ci_job_artifacts.job_id').merge(query))
end
......
......@@ -80,6 +80,19 @@ module Ci
.where.not('ci_builds.ref IN (?)', project.repository.ref_names).limit(limit)
end
scope :orphaned_with_subquery, ->(limit, project) do
# TODO remove either .orphaned of .orphaned_with_subquery depending
# on db performance
orphaned_build_ids = Ci::Build
.where(project: project)
.with_erasable_job_artifacts
.where.not(ref: project.repository.ref_names)
.group(:id)
.limit(limit)
.pluck(:id)
where(job_id: orphaned_build_ids).limit(limit)
end
delegate :filename, :exists?, :open, to: :file
enum file_type: {
......
......@@ -30,7 +30,8 @@ module Ci
private
def destroy_batch
artifacts = Ci::JobArtifact.orphaned(BATCH_SIZE, @project).to_a
pp Ci::JobArtifact.orphaned_with_subquery(BATCH_SIZE, @project).to_sql
artifacts = Ci::JobArtifact.orphaned_with_subquery(BATCH_SIZE, @project).to_a
return false if artifacts.empty?
......
......@@ -59,7 +59,7 @@ describe Ci::DestroyOrphanedBuildArtifactsService do
if raises_error
expect { subject }.to raise_error(Gitlab::Access::AccessDeniedError)
else
expect { subject }.not_to raise_error(Gitlab::Access::AccessDeniedError)
expect { subject }.not_to raise_error
end
expect(@job_deleted_branch.job_artifacts.size).to eq count_deleted_branch_artifacts
......
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