2018-03-26 11:45:18 +00:00
|
|
|
module Ci
|
2018-04-26 06:06:04 +00:00
|
|
|
class BuildTraceChunk < ActiveRecord::Base
|
2018-03-26 11:45:18 +00:00
|
|
|
extend Gitlab::Ci::Model
|
|
|
|
|
2018-04-26 06:06:04 +00:00
|
|
|
belongs_to :build, class_name: "Ci::Build", foreign_key: :build_id
|
2018-04-04 10:19:17 +00:00
|
|
|
|
|
|
|
after_destroy :redis_delete_data, if: :redis?
|
|
|
|
|
|
|
|
default_value_for :data_store, :redis
|
|
|
|
|
2018-04-06 15:08:35 +00:00
|
|
|
WriteError = Class.new(StandardError)
|
|
|
|
|
2018-04-05 11:39:35 +00:00
|
|
|
CHUNK_SIZE = 128.kilobytes
|
2018-04-06 13:43:12 +00:00
|
|
|
CHUNK_REDIS_TTL = 1.week
|
2018-04-26 06:18:08 +00:00
|
|
|
WRITE_LOCK_RETRY = 100
|
|
|
|
WRITE_LOCK_SLEEP = 1
|
|
|
|
WRITE_LOCK_TTL = 5.minutes
|
2018-04-04 10:19:17 +00:00
|
|
|
|
|
|
|
enum data_store: {
|
|
|
|
redis: 1,
|
2018-04-05 15:57:05 +00:00
|
|
|
db: 2
|
2018-04-04 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 07:30:27 +00:00
|
|
|
##
|
|
|
|
# Data is memoized for optimizing #size and #end_offset
|
2018-04-04 10:19:17 +00:00
|
|
|
def data
|
2018-04-30 05:52:29 +00:00
|
|
|
@data ||= get_data.to_s
|
2018-04-04 10:19:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def truncate(offset = 0)
|
2018-04-26 07:30:27 +00:00
|
|
|
self.append("", offset) if offset < size
|
2018-04-04 10:19:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def append(new_data, offset)
|
2018-04-30 05:52:29 +00:00
|
|
|
raise ArgumentError, 'Offset is out of range' if offset > data.bytesize || offset < 0
|
|
|
|
raise ArgumentError, 'Chunk size overflow' if CHUNK_SIZE < (offset + new_data.bytesize)
|
2018-04-04 10:19:17 +00:00
|
|
|
|
2018-04-30 05:52:29 +00:00
|
|
|
set_data(data.byteslice(0, offset) + new_data)
|
2018-04-04 10:19:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
data&.bytesize.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_offset
|
|
|
|
chunk_index * CHUNK_SIZE
|
|
|
|
end
|
|
|
|
|
|
|
|
def end_offset
|
|
|
|
start_offset + size
|
|
|
|
end
|
|
|
|
|
|
|
|
def range
|
|
|
|
(start_offset...end_offset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_database!
|
2018-04-06 15:08:35 +00:00
|
|
|
in_lock do
|
2018-04-23 06:20:55 +00:00
|
|
|
break if db?
|
|
|
|
break unless size > 0
|
2018-04-04 10:19:17 +00:00
|
|
|
|
2018-04-06 15:08:35 +00:00
|
|
|
self.update!(raw_data: data, data_store: :db)
|
|
|
|
redis_delete_data
|
|
|
|
end
|
2018-04-04 10:19:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-04-26 07:30:27 +00:00
|
|
|
def get_data
|
|
|
|
if redis?
|
|
|
|
redis_data
|
|
|
|
elsif db?
|
|
|
|
raw_data
|
|
|
|
else
|
|
|
|
raise 'Unsupported data store'
|
|
|
|
end&.force_encoding(Encoding::BINARY) # Redis/Database return UTF-8 string as default
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_data(value)
|
|
|
|
raise ArgumentError, 'too much data' if value.bytesize > CHUNK_SIZE
|
|
|
|
|
|
|
|
in_lock do
|
|
|
|
if redis?
|
|
|
|
redis_set_data(value)
|
|
|
|
elsif db?
|
|
|
|
self.raw_data = value
|
|
|
|
else
|
|
|
|
raise 'Unsupported data store'
|
|
|
|
end
|
|
|
|
|
|
|
|
@data = value
|
|
|
|
|
|
|
|
save! if changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
schedule_to_db if fullfilled?
|
|
|
|
end
|
|
|
|
|
2018-04-04 10:19:17 +00:00
|
|
|
def schedule_to_db
|
|
|
|
return if db?
|
|
|
|
|
2018-05-02 07:52:54 +00:00
|
|
|
BuildTraceChunkFlushToDBWorker.perform_async(id)
|
2018-04-04 10:19:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def fullfilled?
|
|
|
|
size == CHUNK_SIZE
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis_data
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2018-04-06 15:08:35 +00:00
|
|
|
redis.get(redis_data_key)
|
2018-04-04 10:19:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis_set_data(data)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2018-04-06 15:08:35 +00:00
|
|
|
redis.set(redis_data_key, data, ex: CHUNK_REDIS_TTL)
|
2018-04-04 10:19:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis_delete_data
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2018-04-06 15:08:35 +00:00
|
|
|
redis.del(redis_data_key)
|
2018-04-04 10:19:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-06 15:08:35 +00:00
|
|
|
def redis_data_key
|
2018-04-30 06:27:05 +00:00
|
|
|
"gitlab:ci:trace:#{build_id}:chunks:#{chunk_index}"
|
2018-04-06 15:08:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def redis_lock_key
|
2018-04-26 06:18:08 +00:00
|
|
|
"trace_write:#{build_id}:chunks:#{chunk_index}"
|
2018-04-06 15:08:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def in_lock
|
2018-04-26 06:18:08 +00:00
|
|
|
lease = Gitlab::ExclusiveLease.new(redis_lock_key, timeout: WRITE_LOCK_TTL)
|
2018-04-06 15:08:35 +00:00
|
|
|
retry_count = 0
|
|
|
|
|
|
|
|
until uuid = lease.try_obtain
|
|
|
|
# Keep trying until we obtain the lease. To prevent hammering Redis too
|
|
|
|
# much we'll wait for a bit between retries.
|
2018-04-26 06:18:08 +00:00
|
|
|
sleep(WRITE_LOCK_SLEEP)
|
|
|
|
break if WRITE_LOCK_RETRY < (retry_count += 1)
|
2018-04-06 15:08:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
raise WriteError, 'Failed to obtain write lock' unless uuid
|
|
|
|
|
|
|
|
self.reload if self.persisted?
|
|
|
|
return yield
|
|
|
|
ensure
|
|
|
|
Gitlab::ExclusiveLease.cancel(redis_lock_key, uuid)
|
2018-04-04 10:19:17 +00:00
|
|
|
end
|
2018-03-26 11:45:18 +00:00
|
|
|
end
|
|
|
|
end
|