Commit 68d138e8 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'remove-trace-efficiently' into 'master'

Remove redundant query when removing trace

See merge request gitlab-org/gitlab-ce!20324
parents 82f6b4b0 f60ffd54
...@@ -386,6 +386,10 @@ module Ci ...@@ -386,6 +386,10 @@ module Ci
trace.exist? trace.exist?
end end
def has_old_trace?
old_trace.present?
end
def trace=(data) def trace=(data)
raise NotImplementedError raise NotImplementedError
end end
...@@ -395,6 +399,8 @@ module Ci ...@@ -395,6 +399,8 @@ module Ci
end end
def erase_old_trace! def erase_old_trace!
return unless has_old_trace?
update_column(:trace, nil) update_column(:trace, nil)
end end
......
---
title: Remove redundant query when removing trace
merge_request: 20324
author:
type: performance
...@@ -101,14 +101,17 @@ module Gitlab ...@@ -101,14 +101,17 @@ module Gitlab
end end
def erase! def erase!
trace_artifact&.destroy ##
# Erase the archived trace
paths.each do |trace_path| trace_artifact&.destroy!
FileUtils.rm(trace_path, force: true)
end ##
# Erase the live trace
job.trace_chunks.fast_destroy_all job.trace_chunks.fast_destroy_all # Destroy chunks of a live trace
job.erase_old_trace! FileUtils.rm_f(current_path) if current_path # Remove a trace file of a live trace
job.erase_old_trace! if job.has_old_trace? # Remove a trace in database of a live trace
ensure
@current_path = nil
end end
def archive! def archive!
......
...@@ -514,6 +514,22 @@ describe Ci::Build do ...@@ -514,6 +514,22 @@ describe Ci::Build do
end end
end end
describe '#has_old_trace?' do
subject { build.has_old_trace? }
context 'when old trace exists' do
before do
build.update_column(:trace, 'old trace')
end
it { is_expected.to be_truthy }
end
context 'when old trace does not exist' do
it { is_expected.to be_falsy }
end
end
describe '#trace=' do describe '#trace=' do
it "expect to fail trace=" do it "expect to fail trace=" do
expect { build.trace = "new" }.to raise_error(NotImplementedError) expect { build.trace = "new" }.to raise_error(NotImplementedError)
...@@ -533,16 +549,32 @@ describe Ci::Build do ...@@ -533,16 +549,32 @@ describe Ci::Build do
end end
describe '#erase_old_trace!' do describe '#erase_old_trace!' do
subject { build.send(:read_attribute, :trace) } subject { build.erase_old_trace! }
before do context 'when old trace exists' do
build.send(:write_attribute, :trace, 'old trace') before do
build.update_column(:trace, 'old trace')
end
it "erases old trace" do
subject
expect(build.old_trace).to be_nil
end
it "executes UPDATE query" do
recorded = ActiveRecord::QueryRecorder.new { subject }
expect(recorded.log.select { |l| l.match?(/UPDATE.*ci_builds/) }.count).to eq(1)
end
end end
it "expect to receive data from database" do context 'when old trace does not exist' do
build.erase_old_trace! it 'does not execute UPDATE query' do
recorded = ActiveRecord::QueryRecorder.new { subject }
is_expected.to be_nil expect(recorded.log.select { |l| l.match?(/UPDATE.*ci_builds/) }.count).to eq(0)
end
end end
end end
......
...@@ -611,6 +611,55 @@ shared_examples_for 'trace with disabled live trace feature' do ...@@ -611,6 +611,55 @@ shared_examples_for 'trace with disabled live trace feature' do
end end
end end
end end
describe '#erase!' do
subject { trace.erase! }
context 'when it is a live trace' do
context 'when trace is stored in database' do
let(:build) { create(:ci_build) }
before do
build.update_column(:trace, 'sample trace')
end
it { expect(trace.raw).not_to be_nil }
it "removes trace" do
subject
expect(trace.raw).to be_nil
end
end
context 'when trace is stored in file storage' do
let(:build) { create(:ci_build, :trace_live) }
it { expect(trace.raw).not_to be_nil }
it "removes trace" do
subject
expect(trace.raw).to be_nil
end
end
end
context 'when it is an archived trace' do
let(:build) { create(:ci_build, :trace_artifact) }
it "has trace at first" do
expect(trace.raw).not_to be_nil
end
it "removes trace" do
subject
build.reload
expect(trace.raw).to be_nil
end
end
end
end end
shared_examples_for 'trace with enabled live trace feature' do shared_examples_for 'trace with enabled live trace feature' do
...@@ -798,4 +847,35 @@ shared_examples_for 'trace with enabled live trace feature' do ...@@ -798,4 +847,35 @@ shared_examples_for 'trace with enabled live trace feature' do
end end
end end
end end
describe '#erase!' do
subject { trace.erase! }
context 'when it is a live trace' do
let(:build) { create(:ci_build, :trace_live) }
it { expect(trace.raw).not_to be_nil }
it "removes trace" do
subject
expect(trace.raw).to be_nil
end
end
context 'when it is an archived trace' do
let(:build) { create(:ci_build, :trace_artifact) }
it "has trace at first" do
expect(trace.raw).not_to be_nil
end
it "removes trace" do
subject
build.reload
expect(trace.raw).to be_nil
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