Commit cda4b66e authored by Grzegorz Bizon's avatar Grzegorz Bizon

Ensure consistency when creating a new build trace chunk

This commit increases consistency when a build trace chunk is created.
parent 47da223c
......@@ -81,15 +81,17 @@ module Ci
# database, what is especially important in EE. This method does not
# change the behavior in CE.
#
def with_consistent_reads(project, &block)
return yield unless Feature.enabled?(
:gitlab_ci_trace_read_consistency, project, type: :development, default_enabled: false
)
def with_read_consistency(build, &block)
return yield unless consistent_reads_enabled?(build)
::Gitlab::Database::Consistency
.with_read_consistency(&block)
end
def consistent_reads_enabled?(build)
Feature.enabled?(:gitlab_ci_trace_read_consistency, build.project, type: :development, default_enabled: false)
end
##
# Sometimes we do not want to read raw data. This method makes it easier
# to find attributes that are just metadata excluding raw data.
......@@ -168,7 +170,7 @@ module Ci
in_lock(lock_key, **lock_params) do # exclusive Redis lock is acquired first
raise FailedToPersistDataError, 'Modifed build trace chunk detected' if has_changes_to_save?
self.class.with_consistent_reads(build.project) do
self.class.with_read_consistency(build) do
self.reset.then { |chunk| chunk.unsafe_persist_data! }
end
end
......
......@@ -63,7 +63,7 @@ module Gitlab
#
def trace_chunks
strong_memoize(:trace_chunks) do
Ci::BuildTraceChunks.with_read_consistency(build.project) do
::Ci::BuildTraceChunk.with_read_consistency(build) do
build.trace_chunks.persisted
.select(::Ci::BuildTraceChunk.metadata_attributes)
end
......
......@@ -227,12 +227,20 @@ module Gitlab
end
# rubocop: enable CodeReuse/ActiveRecord
def build_chunk
@chunks_cache[chunk_index] = ::Ci::BuildTraceChunk.new(build: build, chunk_index: chunk_index)
def next_chunk
@chunks_cache[chunk_index] = begin
if ::Ci::BuildTraceChunk.consistent_reads_enabled?(build)
::Ci::BuildTraceChunk
.safe_find_or_create_by(build: build, chunk_index: chunk_index)
else
::Ci::BuildTraceChunk
.new(build: build, chunk_index: chunk_index)
end
end
end
def ensure_chunk
current_chunk || build_chunk
current_chunk || next_chunk || current_chunk
end
# rubocop: disable CodeReuse/ActiveRecord
......
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