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