Commit 8970f01f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Make it possible to persist a final build trace chunk

Build trace chunk is considered to be final (is the last part of a build
log and we do not expect it to ever become full) when a runner submitted
a build pending state and there is no chunk with higher index in the
database.
parent 62a2b486
......@@ -118,6 +118,16 @@ module Ci
redis?
end
##
# Build trace chunk is final (the last one that we do not expect to ever
# become full) when a runner submitted a build pending state and there is
# no chunk with higher index in the database.
#
def final?
build.pending_state.present? &&
build.trace_chunks.maximum(:chunk_index).to_i == chunk_index
end
private
def get_data
......@@ -130,9 +140,9 @@ module Ci
current_data = data
old_store_class = current_store
current_size = current_data&.bytesize.to_i
## TODO allow final chunk update if build pending state exists
unless current_data&.bytesize.to_i == CHUNK_SIZE
unless current_size == CHUNK_SIZE || final?
raise FailedToPersistDataError, 'Data is not fulfilled in a bucket'
end
......
......@@ -177,8 +177,11 @@ module API
Gitlab::Metrics.add_event(:update_build)
::Ci::UpdateBuildStateService.new(job, params).then do |service|
service.execute.then { |result| status result.status }
service = ::Ci::UpdateBuildStateService
.new(job, declared_params(include_missing: false))
service.execute.then do |result|
status result.status
end
end
......
# frozen_string_literal: true
FactoryBot.define do
factory :ci_build_pending_state, class: 'Ci::BuildPendingState' do
build factory: :ci_build
trace_checksum { 'crc32:12345678' }
state { 'success' }
end
end
......@@ -463,6 +463,8 @@ RSpec.describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
end
describe '#persist_data!' do
let(:build) { create(:ci_build, :running) }
subject { build_trace_chunk.persist_data! }
shared_examples_for 'Atomic operation' do
......@@ -524,6 +526,18 @@ RSpec.describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
expect(Ci::BuildTraceChunks::Database.new.data(build_trace_chunk)).to be_nil
expect(Ci::BuildTraceChunks::Fog.new.data(build_trace_chunk)).to be_nil
end
context 'when chunk is a final one' do
before do
create(:ci_build_pending_state, build: build)
end
it 'persists the data' do
subject
expect(build_trace_chunk.fog?).to be_truthy
end
end
end
end
......@@ -573,6 +587,18 @@ RSpec.describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
expect(Ci::BuildTraceChunks::Database.new.data(build_trace_chunk)).to eq(data)
expect(Ci::BuildTraceChunks::Fog.new.data(build_trace_chunk)).to be_nil
end
context 'when chunk is a final one' do
before do
create(:ci_build_pending_state, build: build)
end
it 'persists the data' do
subject
expect(build_trace_chunk.fog?).to be_truthy
end
end
end
end
......@@ -622,6 +648,54 @@ RSpec.describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
end
end
describe 'final?' do
let(:build) { create(:ci_build, :running) }
context 'when build pending state exists' do
before do
create(:ci_build_pending_state, build: build)
end
context 'when chunks is not the last one' do
before do
create(:ci_build_trace_chunk, chunk_index: 1, build: build)
end
it 'is not a final chunk' do
expect(build.reload.pending_state).to be_present
expect(build_trace_chunk).not_to be_final
end
end
context 'when chunks is the last one' do
it 'is a final chunk' do
expect(build.reload.pending_state).to be_present
expect(build_trace_chunk).to be_final
end
end
end
context 'when build pending state does not exist' do
context 'when chunks is not the last one' do
before do
create(:ci_build_trace_chunk, chunk_index: 1, build: build)
end
it 'is not a final chunk' do
expect(build.reload.pending_state).not_to be_present
expect(build_trace_chunk).not_to be_final
end
end
context 'when chunks is the last one' do
it 'is not a final chunk' do
expect(build.reload.pending_state).not_to be_present
expect(build_trace_chunk).not_to be_final
end
end
end
end
describe 'deletes data in redis after a parent record destroyed' do
let(:project) { create(:project) }
......
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