gitlab-org--gitlab-foss/app/models/ci/build_trace_chunk.rb

248 lines
6.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-03-26 11:45:18 +00:00
module Ci
class BuildTraceChunk < ApplicationRecord
extend ::Gitlab::Ci::Model
include ::Comparable
include ::FastDestroyAll
include ::Checksummable
2018-07-03 07:20:27 +00:00
include ::Gitlab::ExclusiveLeaseHelpers
2018-03-26 11:45:18 +00:00
belongs_to :build, class_name: "Ci::Build", foreign_key: :build_id
2018-04-04 10:19:17 +00:00
default_value_for :data_store, :redis
after_create { metrics.increment_trace_operation(operation: :chunked) }
2018-04-05 11:39:35 +00:00
CHUNK_SIZE = 128.kilobytes
2018-05-07 08:34:47 +00:00
WRITE_LOCK_RETRY = 10
2018-05-07 09:45:38 +00:00
WRITE_LOCK_SLEEP = 0.01.seconds
2018-05-07 08:34:47 +00:00
WRITE_LOCK_TTL = 1.minute
2018-04-04 10:19:17 +00:00
2018-11-28 12:27:25 +00:00
FailedToPersistDataError = Class.new(StandardError)
# Note: The ordering of this enum is related to the precedence of persist store.
# The bottom item takes the highest precedence, and the top item takes the lowest precedence.
2018-04-04 10:19:17 +00:00
enum data_store: {
redis: 1,
database: 2,
fog: 3
2018-04-04 10:19:17 +00:00
}
scope :live, -> { redis }
scope :persisted, -> { not_redis.order(:chunk_index) }
2018-05-01 08:06:44 +00:00
class << self
def all_stores
@all_stores ||= self.data_stores.keys
end
def persistable_store
# get first available store from the back of the list
all_stores.reverse.find { |store| get_store_class(store).available? }
end
def get_store_class(store)
@stores ||= {}
@stores[store] ||= "Ci::BuildTraceChunks::#{store.capitalize}".constantize.new
end
2018-05-03 08:08:05 +00:00
##
# FastDestroyAll concerns
def begin_fast_destroy
all_stores.each_with_object({}) do |store, result|
relation = public_send(store) # rubocop:disable GitlabSecurity/PublicSend
keys = get_store_class(store).keys(relation)
result[store] = keys if keys.present?
end
2018-05-03 08:08:05 +00:00
end
##
# FastDestroyAll concerns
def finalize_fast_destroy(keys)
keys.each do |store, value|
get_store_class(store).delete_keys(value)
end
2018-05-03 08:08:05 +00:00
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.
#
def metadata_attributes
attribute_names - %w[raw_data]
end
end
2018-04-04 10:19:17 +00:00
def data
@data ||= get_data.to_s
2018-04-04 10:19:17 +00:00
end
def crc32
checksum.to_i
end
2018-04-04 10:19:17 +00:00
def truncate(offset = 0)
raise ArgumentError, 'Offset is out of range' if offset > size || offset < 0
return if offset == size # Skip the following process as it doesn't affect anything
self.append("", offset)
2018-04-04 10:19:17 +00:00
end
def append(new_data, offset)
2018-07-03 05:48:00 +00:00
raise ArgumentError, 'New data is missing' unless new_data
raise ArgumentError, 'Offset is out of range' if offset < 0 || offset > size
raise ArgumentError, 'Chunk size overflow' if CHUNK_SIZE < (offset + new_data.bytesize)
2018-04-04 10:19:17 +00:00
in_lock(*lock_params) { unsafe_append_data!(new_data, offset) }
schedule_to_persist! if full?
2018-04-04 10:19:17 +00:00
end
def size
@size ||= @data&.bytesize || current_store.size(self) || data&.bytesize
2018-04-04 10:19:17 +00:00
end
def start_offset
chunk_index * CHUNK_SIZE
end
def end_offset
start_offset + size
end
def range
(start_offset...end_offset)
end
2018-06-18 08:56:16 +00:00
def persist_data!
in_lock(*lock_params) { unsafe_persist_data! }
end
def schedule_to_persist!
return if persisted?
Ci::BuildTraceChunkFlushWorker.perform_async(id)
2018-04-04 10:19:17 +00:00
end
def persisted?
!redis?
end
def live?
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? && chunks_max_index == chunk_index
end
def <=>(other)
return unless self.build_id == other.build_id
self.chunk_index <=> other.chunk_index
end
2018-04-04 10:19:17 +00:00
private
def get_data
# Redis / database return UTF-8 encoded string by default
current_store.data(self)&.force_encoding(Encoding::BINARY)
end
def unsafe_persist_data!(new_store = self.class.persistable_store)
return if data_store == new_store.to_s
current_data = data
old_store_class = current_store
current_size = current_data&.bytesize.to_i
unless current_size == CHUNK_SIZE || final?
raise FailedToPersistDataError, <<~MSG
data is not fulfilled in a bucket
size: #{current_size}
state: #{build.pending_state.present?}
max: #{chunks_max_index}
index: #{chunk_index}
MSG
end
2018-11-28 12:27:25 +00:00
self.raw_data = nil
self.data_store = new_store
self.checksum = self.class.crc32(current_data)
##
# We need to so persist data then save a new store identifier before we
# remove data from the previous store to make this operation
# trasnaction-safe. `unsafe_set_data! calls `save!` because of this
# reason.
#
# TODO consider using callbacks and state machine to remove old data
#
2018-11-28 12:27:25 +00:00
unsafe_set_data!(current_data)
old_store_class.delete_data(self)
end
def unsafe_set_data!(value)
2018-06-25 07:19:40 +00:00
raise ArgumentError, 'New data size exceeds chunk size' if value.bytesize > CHUNK_SIZE
current_store.set_data(self, value)
@data = value
@size = value.bytesize
save! if changed?
end
def unsafe_append_data!(value, offset)
new_size = value.bytesize + offset
if new_size > CHUNK_SIZE
raise ArgumentError, 'New data size exceeds chunk size'
end
current_store.append_data(self, value, offset).then do |stored|
metrics.increment_trace_operation(operation: :appended)
raise ArgumentError, 'Trace appended incorrectly' if stored != new_size
end
@data = nil
@size = new_size
save! if changed?
end
2018-05-07 08:34:47 +00:00
def full?
2018-04-04 10:19:17 +00:00
size == CHUNK_SIZE
end
def current_store
self.class.get_store_class(data_store)
end
def chunks_max_index
build.trace_chunks.maximum(:chunk_index).to_i
end
def lock_params
["trace_write:#{build_id}:chunks:#{chunk_index}",
{ ttl: WRITE_LOCK_TTL,
2018-07-04 04:29:47 +00:00
retries: WRITE_LOCK_RETRY,
sleep_sec: WRITE_LOCK_SLEEP }]
2018-04-04 10:19:17 +00:00
end
def metrics
@metrics ||= ::Gitlab::Ci::Trace::Metrics.new
end
2018-03-26 11:45:18 +00:00
end
end