2018-10-30 15:05:07 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-04 06:19:17 -04:00
|
|
|
##
|
|
|
|
# This class is compatible with IO class (https://ruby-doc.org/core-2.3.1/IO.html)
|
|
|
|
# source: https://gitlab.com/snippets/1685610
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Trace
|
|
|
|
class ChunkedIO
|
2018-04-26 02:06:04 -04:00
|
|
|
CHUNK_SIZE = ::Ci::BuildTraceChunk::CHUNK_SIZE
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
FailedToGetChunkError = Class.new(StandardError)
|
|
|
|
|
2018-04-26 02:06:04 -04:00
|
|
|
attr_reader :build
|
2018-04-04 06:19:17 -04:00
|
|
|
attr_reader :tell, :size
|
2018-11-23 10:46:33 -05:00
|
|
|
attr_reader :chunk_data, :chunk_range
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
alias_method :pos, :tell
|
|
|
|
|
2018-04-26 02:06:04 -04:00
|
|
|
def initialize(build, &block)
|
|
|
|
@build = build
|
2018-04-04 06:19:17 -04:00
|
|
|
@chunks_cache = []
|
|
|
|
@tell = 0
|
2018-04-06 06:30:23 -04:00
|
|
|
@size = calculate_size
|
2018-04-04 14:16:51 -04:00
|
|
|
yield self if block_given?
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
# no-op
|
|
|
|
end
|
|
|
|
|
|
|
|
def binmode
|
|
|
|
# no-op
|
|
|
|
end
|
|
|
|
|
|
|
|
def binmode?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def seek(pos, where = IO::SEEK_SET)
|
|
|
|
new_pos =
|
|
|
|
case where
|
|
|
|
when IO::SEEK_END
|
|
|
|
size + pos
|
|
|
|
when IO::SEEK_SET
|
|
|
|
pos
|
|
|
|
when IO::SEEK_CUR
|
|
|
|
tell + pos
|
|
|
|
else
|
|
|
|
-1
|
|
|
|
end
|
|
|
|
|
2018-04-06 06:30:23 -04:00
|
|
|
raise ArgumentError, 'new position is outside of file' if new_pos < 0 || new_pos > size
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
@tell = new_pos
|
|
|
|
end
|
|
|
|
|
|
|
|
def eof?
|
|
|
|
tell == size
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_line
|
|
|
|
until eof?
|
|
|
|
line = readline
|
|
|
|
break if line.nil?
|
|
|
|
|
|
|
|
yield(line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-30 15:05:07 -04:00
|
|
|
def read(length = nil, outbuf = nil)
|
|
|
|
out = []
|
2018-04-04 06:19:17 -04:00
|
|
|
|
2018-04-05 11:57:05 -04:00
|
|
|
length ||= size - tell
|
2018-04-05 10:43:21 -04:00
|
|
|
|
|
|
|
until length <= 0 || eof?
|
2018-04-04 06:19:17 -04:00
|
|
|
data = chunk_slice_from_offset
|
2020-10-20 11:08:57 -04:00
|
|
|
raise FailedToGetChunkError if data.to_s.empty?
|
2018-04-04 06:19:17 -04:00
|
|
|
|
2018-04-05 10:43:21 -04:00
|
|
|
chunk_bytes = [CHUNK_SIZE - chunk_offset, length].min
|
2018-11-23 10:46:33 -05:00
|
|
|
chunk_data_slice = data.byteslice(0, chunk_bytes)
|
2018-04-04 14:16:51 -04:00
|
|
|
|
2018-11-23 10:46:33 -05:00
|
|
|
out << chunk_data_slice
|
|
|
|
@tell += chunk_data_slice.bytesize
|
|
|
|
length -= chunk_data_slice.bytesize
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
|
|
|
|
2018-10-30 15:05:07 -04:00
|
|
|
out = out.join
|
|
|
|
|
2018-04-05 02:26:57 -04:00
|
|
|
# If outbuf is passed, we put the output into the buffer. This supports IO.copy_stream functionality
|
|
|
|
if outbuf
|
2018-10-30 15:05:07 -04:00
|
|
|
outbuf.replace(out)
|
2018-04-05 02:26:57 -04:00
|
|
|
end
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
out
|
|
|
|
end
|
|
|
|
|
|
|
|
def readline
|
2018-10-30 15:05:07 -04:00
|
|
|
out = []
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
until eof?
|
|
|
|
data = chunk_slice_from_offset
|
2020-10-20 11:08:57 -04:00
|
|
|
raise FailedToGetChunkError if data.to_s.empty?
|
2018-11-23 10:46:33 -05:00
|
|
|
|
2018-04-04 06:19:17 -04:00
|
|
|
new_line = data.index("\n")
|
|
|
|
|
|
|
|
if !new_line.nil?
|
2018-11-23 10:46:33 -05:00
|
|
|
raw_data = data[0..new_line]
|
|
|
|
out << raw_data
|
|
|
|
@tell += raw_data.bytesize
|
2018-04-04 06:19:17 -04:00
|
|
|
break
|
|
|
|
else
|
|
|
|
out << data
|
|
|
|
@tell += data.bytesize
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-30 15:05:07 -04:00
|
|
|
out.join
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def write(data)
|
2018-04-04 14:16:51 -04:00
|
|
|
start_pos = tell
|
2018-04-04 06:19:17 -04:00
|
|
|
|
2018-04-04 14:16:51 -04:00
|
|
|
while tell < start_pos + data.bytesize
|
2018-04-04 06:19:17 -04:00
|
|
|
# get slice from current offset till the end where it falls into chunk
|
|
|
|
chunk_bytes = CHUNK_SIZE - chunk_offset
|
2018-11-23 10:46:33 -05:00
|
|
|
data_slice = data.byteslice(tell - start_pos, chunk_bytes)
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
# append data to chunk, overwriting from that point
|
2018-11-23 10:46:33 -05:00
|
|
|
ensure_chunk.append(data_slice, chunk_offset)
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
# move offsets within buffer
|
2018-11-23 10:46:33 -05:00
|
|
|
@tell += data_slice.bytesize
|
2018-04-04 14:16:51 -04:00
|
|
|
@size = [size, tell].max
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
2018-04-05 10:43:21 -04:00
|
|
|
|
|
|
|
tell - start_pos
|
|
|
|
ensure
|
|
|
|
invalidate_chunk_cache
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-04-04 06:19:17 -04:00
|
|
|
def truncate(offset)
|
2018-05-02 01:46:04 -04:00
|
|
|
raise ArgumentError, 'Outside of file' if offset > size || offset < 0
|
2018-05-02 01:27:28 -04:00
|
|
|
return if offset == size # Skip the following process as it doesn't affect anything
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
@tell = offset
|
|
|
|
@size = offset
|
|
|
|
|
|
|
|
# remove all next chunks
|
2018-04-26 03:39:56 -04:00
|
|
|
trace_chunks.where('chunk_index > ?', chunk_index).fast_destroy_all
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
# truncate current chunk
|
2018-04-26 03:30:27 -04:00
|
|
|
current_chunk.truncate(chunk_offset)
|
2018-04-05 10:43:21 -04:00
|
|
|
ensure
|
|
|
|
invalidate_chunk_cache
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-04-04 06:19:17 -04:00
|
|
|
|
|
|
|
def flush
|
|
|
|
# no-op
|
|
|
|
end
|
|
|
|
|
|
|
|
def present?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-04-24 09:13:46 -04:00
|
|
|
def destroy!
|
2019-07-18 05:22:46 -04:00
|
|
|
# TODO: Remove this logging once we confirmed new live trace architecture is functional.
|
|
|
|
# See https://gitlab.com/gitlab-com/gl-infra/infrastructure/issues/4667.
|
|
|
|
unless build.has_archived_trace?
|
|
|
|
Sidekiq.logger.warn(message: 'The job does not have archived trace but going to be destroyed.',
|
|
|
|
job_id: build.id)
|
|
|
|
end
|
|
|
|
|
2018-04-26 03:39:56 -04:00
|
|
|
trace_chunks.fast_destroy_all
|
2018-04-05 10:43:21 -04:00
|
|
|
@tell = @size = 0
|
|
|
|
ensure
|
2018-04-04 06:19:17 -04:00
|
|
|
invalidate_chunk_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
##
|
|
|
|
# The below methods are not implemented in IO class
|
|
|
|
#
|
|
|
|
def in_range?
|
|
|
|
@chunk_range&.include?(tell)
|
|
|
|
end
|
|
|
|
|
|
|
|
def chunk_slice_from_offset
|
|
|
|
unless in_range?
|
|
|
|
current_chunk.tap do |chunk|
|
|
|
|
raise FailedToGetChunkError unless chunk
|
|
|
|
|
2018-11-23 10:46:33 -05:00
|
|
|
@chunk_data = chunk.data
|
2018-04-04 06:19:17 -04:00
|
|
|
@chunk_range = chunk.range
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-23 10:46:33 -05:00
|
|
|
@chunk_data.byteslice(chunk_offset, CHUNK_SIZE)
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def chunk_offset
|
|
|
|
tell % CHUNK_SIZE
|
|
|
|
end
|
|
|
|
|
|
|
|
def chunk_index
|
|
|
|
tell / CHUNK_SIZE
|
|
|
|
end
|
|
|
|
|
|
|
|
def chunk_start
|
|
|
|
chunk_index * CHUNK_SIZE
|
|
|
|
end
|
|
|
|
|
|
|
|
def chunk_end
|
|
|
|
[chunk_start + CHUNK_SIZE, size].min
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalidate_chunk_cache
|
|
|
|
@chunks_cache = []
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-04-04 06:19:17 -04:00
|
|
|
def current_chunk
|
2018-04-26 02:06:04 -04:00
|
|
|
@chunks_cache[chunk_index] ||= trace_chunks.find_by(chunk_index: chunk_index)
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-04-04 06:19:17 -04:00
|
|
|
|
2021-02-03 16:09:17 -05:00
|
|
|
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
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_chunk
|
2021-02-03 16:09:17 -05:00
|
|
|
current_chunk || next_chunk || current_chunk
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-04-26 02:06:04 -04:00
|
|
|
def trace_chunks
|
|
|
|
::Ci::BuildTraceChunk.where(build: build)
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-04-06 06:30:23 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-04-06 06:30:23 -04:00
|
|
|
def calculate_size
|
2018-04-26 02:06:04 -04:00
|
|
|
trace_chunks.order(chunk_index: :desc).first.try(&:end_offset).to_i
|
2018-04-06 06:30:23 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-04-04 06:19:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|