2018-08-03 03:15:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-07 04:04:55 -04:00
|
|
|
module Ci
|
|
|
|
module BuildTraceChunks
|
|
|
|
class Redis
|
|
|
|
CHUNK_REDIS_TTL = 1.week
|
2020-07-22 11:09:28 -04:00
|
|
|
LUA_APPEND_CHUNK = <<~EOS.freeze
|
|
|
|
local key, new_data, offset = KEYS[1], ARGV[1], ARGV[2]
|
|
|
|
local length = new_data:len()
|
|
|
|
local expire = #{CHUNK_REDIS_TTL.seconds}
|
|
|
|
local current_size = redis.call("strlen", key)
|
|
|
|
offset = tonumber(offset)
|
|
|
|
|
|
|
|
if offset == 0 then
|
|
|
|
-- overwrite everything
|
|
|
|
redis.call("set", key, new_data, "ex", expire)
|
|
|
|
return redis.call("strlen", key)
|
|
|
|
elseif offset > current_size then
|
|
|
|
-- offset range violation
|
|
|
|
return -1
|
|
|
|
elseif offset + length >= current_size then
|
|
|
|
-- efficiently append or overwrite and append
|
|
|
|
redis.call("expire", key, expire)
|
|
|
|
return redis.call("setrange", key, offset, new_data)
|
|
|
|
else
|
|
|
|
-- append and truncate
|
|
|
|
local current_data = redis.call("get", key)
|
|
|
|
new_data = current_data:sub(1, offset) .. new_data
|
|
|
|
redis.call("set", key, new_data, "ex", expire)
|
|
|
|
return redis.call("strlen", key)
|
|
|
|
end
|
|
|
|
EOS
|
2018-06-07 04:04:55 -04:00
|
|
|
|
|
|
|
def available?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def data(model)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.get(key(model))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-27 20:10:34 -04:00
|
|
|
def set_data(model, new_data)
|
2018-06-07 04:04:55 -04:00
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2020-08-27 20:10:34 -04:00
|
|
|
redis.set(key(model), new_data, ex: CHUNK_REDIS_TTL)
|
2018-06-07 04:04:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-22 11:09:28 -04:00
|
|
|
def append_data(model, new_data, offset)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.eval(LUA_APPEND_CHUNK, keys: [key(model)], argv: [new_data, offset])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def size(model)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.strlen(key(model))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-07 04:04:55 -04:00
|
|
|
def delete_data(model)
|
|
|
|
delete_keys([[model.build_id, model.chunk_index]])
|
|
|
|
end
|
|
|
|
|
|
|
|
def keys(relation)
|
|
|
|
relation.pluck(:build_id, :chunk_index)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_keys(keys)
|
|
|
|
return if keys.empty?
|
|
|
|
|
|
|
|
keys = keys.map { |key| key_raw(*key) }
|
|
|
|
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2020-06-24 17:08:46 -04:00
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/224171
|
|
|
|
Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
|
|
|
|
redis.del(keys)
|
|
|
|
end
|
2018-06-07 04:04:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def key(model)
|
|
|
|
key_raw(model.build_id, model.chunk_index)
|
|
|
|
end
|
|
|
|
|
|
|
|
def key_raw(build_id, chunk_index)
|
|
|
|
"gitlab:ci:trace:#{build_id.to_i}:chunks:#{chunk_index.to_i}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|