2020-02-27 13:09:21 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'digest'
|
2022-11-02 08:11:04 -04:00
|
|
|
require 'msgpack'
|
2020-02-27 13:09:21 -05:00
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module SidekiqMiddleware
|
|
|
|
module DuplicateJobs
|
|
|
|
# This class defines an identifier of a job in a queue
|
|
|
|
# The identifier based on a job's class and arguments.
|
|
|
|
#
|
|
|
|
# As strategy decides when to keep track of the job in redis and when to
|
|
|
|
# remove it.
|
|
|
|
#
|
|
|
|
# Storing the deduplication key in redis can be done by calling `check!`
|
|
|
|
# check returns the `jid` of the job if it was scheduled, or the `jid` of
|
|
|
|
# the duplicate job if it was already scheduled
|
|
|
|
#
|
|
|
|
# When new jobs can be scheduled again, the strategy calls `#delete`.
|
|
|
|
class DuplicateJob
|
2021-09-16 11:12:47 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-10-29 05:10:11 -04:00
|
|
|
DEFAULT_DUPLICATE_KEY_TTL = 6.hours
|
2020-06-16 20:16:40 -04:00
|
|
|
DEFAULT_STRATEGY = :until_executing
|
2021-06-04 11:10:25 -04:00
|
|
|
STRATEGY_NONE = :none
|
2021-09-16 11:12:47 -04:00
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
attr_reader :existing_jid
|
|
|
|
|
2020-06-16 20:16:40 -04:00
|
|
|
def initialize(job, queue_name)
|
2020-02-27 13:09:21 -05:00
|
|
|
@job = job
|
|
|
|
@queue_name = queue_name
|
|
|
|
end
|
|
|
|
|
|
|
|
# This will continue the middleware chain if the job should be scheduled
|
|
|
|
# It will return false if the job needs to be cancelled
|
|
|
|
def schedule(&block)
|
|
|
|
Strategies.for(strategy).new(self).schedule(job, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# This will continue the server middleware chain if the job should be
|
|
|
|
# executed.
|
|
|
|
# It will return false if the job should not be executed.
|
|
|
|
def perform(&block)
|
|
|
|
Strategies.for(strategy).new(self).perform(job, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# This method will return the jid that was set in redis
|
2021-10-29 05:10:11 -04:00
|
|
|
def check!(expiry = duplicate_key_ttl)
|
2022-11-09 13:07:50 -05:00
|
|
|
my_cookie = {
|
|
|
|
'jid' => jid,
|
|
|
|
'offsets' => {},
|
|
|
|
'wal_locations' => {},
|
|
|
|
'existing_wal_locations' => job_wal_locations
|
|
|
|
}
|
|
|
|
|
|
|
|
# There are 3 possible scenarios. In order of decreasing likelyhood:
|
|
|
|
# 1. SET NX succeeds.
|
|
|
|
# 2. SET NX fails, GET succeeds.
|
|
|
|
# 3. SET NX fails, the key expires and GET fails. In this case we must retry.
|
|
|
|
actual_cookie = {}
|
|
|
|
while actual_cookie.empty?
|
|
|
|
set_succeeded = with_redis { |r| r.set(cookie_key, my_cookie.to_msgpack, nx: true, ex: expiry) }
|
|
|
|
actual_cookie = set_succeeded ? my_cookie : get_cookie
|
2020-02-27 13:09:21 -05:00
|
|
|
end
|
2022-11-09 13:07:50 -05:00
|
|
|
|
|
|
|
job['idempotency_key'] = idempotency_key
|
|
|
|
|
|
|
|
self.existing_wal_locations = actual_cookie['existing_wal_locations']
|
|
|
|
self.existing_jid = actual_cookie['jid']
|
2020-02-27 13:09:21 -05:00
|
|
|
end
|
|
|
|
|
2021-09-16 11:12:47 -04:00
|
|
|
def update_latest_wal_location!
|
|
|
|
return unless job_wal_locations.present?
|
|
|
|
|
2022-11-09 13:07:50 -05:00
|
|
|
argv = []
|
|
|
|
job_wal_locations.each do |connection_name, location|
|
|
|
|
argv += [connection_name, pg_wal_lsn_diff(connection_name), location]
|
2021-09-16 11:12:47 -04:00
|
|
|
end
|
2022-11-09 13:07:50 -05:00
|
|
|
|
|
|
|
with_redis { |r| r.eval(UPDATE_WAL_COOKIE_SCRIPT, keys: [cookie_key], argv: argv) }
|
2021-09-16 11:12:47 -04:00
|
|
|
end
|
|
|
|
|
2022-11-09 13:07:50 -05:00
|
|
|
# Generally speaking, updating a Redis key by deserializing and
|
|
|
|
# serializing it on the Redis server is bad for performance. However in
|
|
|
|
# the case of DuplicateJobs we know that key updates are rare, and the
|
|
|
|
# most common operations are setting, getting and deleting the key. The
|
|
|
|
# aim of this design is to make the common operations as fast as
|
|
|
|
# possible.
|
|
|
|
UPDATE_WAL_COOKIE_SCRIPT = <<~LUA
|
|
|
|
local cookie_msgpack = redis.call("get", KEYS[1])
|
|
|
|
if not cookie_msgpack then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local cookie = cmsgpack.unpack(cookie_msgpack)
|
|
|
|
|
|
|
|
for i = 1, #ARGV, 3 do
|
|
|
|
local connection = ARGV[i]
|
|
|
|
local current_offset = cookie.offsets[connection]
|
|
|
|
local new_offset = tonumber(ARGV[i+1])
|
|
|
|
if not current_offset or current_offset < new_offset then
|
|
|
|
cookie.offsets[connection] = new_offset
|
|
|
|
cookie.wal_locations[connection] = ARGV[i+2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
redis.call("set", KEYS[1], cmsgpack.pack(cookie), "ex", redis.call("ttl", KEYS[1]))
|
|
|
|
LUA
|
|
|
|
|
2021-09-16 11:12:47 -04:00
|
|
|
def latest_wal_locations
|
|
|
|
return {} unless job_wal_locations.present?
|
|
|
|
|
|
|
|
strong_memoize(:latest_wal_locations) do
|
2022-11-09 13:07:50 -05:00
|
|
|
get_cookie.fetch('wal_locations', {})
|
2021-09-16 11:12:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
def delete!
|
2022-11-09 13:07:50 -05:00
|
|
|
with_redis { |redis| redis.del(cookie_key) }
|
2020-02-27 13:09:21 -05:00
|
|
|
end
|
|
|
|
|
2021-10-22 11:19:11 -04:00
|
|
|
def reschedule
|
|
|
|
Gitlab::SidekiqLogging::DeduplicationLogger.instance.rescheduled_log(job)
|
|
|
|
|
|
|
|
worker_klass.perform_async(*arguments)
|
|
|
|
end
|
|
|
|
|
2020-06-16 20:16:40 -04:00
|
|
|
def scheduled?
|
|
|
|
scheduled_at.present?
|
|
|
|
end
|
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
def duplicate?
|
|
|
|
raise "Call `#check!` first to check for existing duplicates" unless existing_jid
|
|
|
|
|
|
|
|
jid != existing_jid
|
|
|
|
end
|
|
|
|
|
2021-10-29 05:10:11 -04:00
|
|
|
def set_deduplicated_flag!(expiry = duplicate_key_ttl)
|
2021-10-22 11:19:11 -04:00
|
|
|
return unless reschedulable?
|
|
|
|
|
2022-11-09 13:07:50 -05:00
|
|
|
with_redis { |redis| redis.eval(DEDUPLICATED_SCRIPT, keys: [cookie_key]) }
|
2021-10-22 11:19:11 -04:00
|
|
|
end
|
|
|
|
|
2022-10-28 08:11:31 -04:00
|
|
|
DEDUPLICATED_SCRIPT = <<~LUA
|
|
|
|
local cookie_msgpack = redis.call("get", KEYS[1])
|
|
|
|
if not cookie_msgpack then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local cookie = cmsgpack.unpack(cookie_msgpack)
|
|
|
|
cookie.deduplicated = "1"
|
|
|
|
redis.call("set", KEYS[1], cmsgpack.pack(cookie), "ex", redis.call("ttl", KEYS[1]))
|
|
|
|
LUA
|
|
|
|
|
2021-10-22 11:19:11 -04:00
|
|
|
def should_reschedule?
|
2022-11-09 13:07:50 -05:00
|
|
|
reschedulable? && get_cookie['deduplicated'].present?
|
2021-10-22 11:19:11 -04:00
|
|
|
end
|
|
|
|
|
2020-06-16 20:16:40 -04:00
|
|
|
def scheduled_at
|
|
|
|
job['at']
|
|
|
|
end
|
|
|
|
|
|
|
|
def options
|
|
|
|
return {} unless worker_klass
|
|
|
|
return {} unless worker_klass.respond_to?(:get_deduplication_options)
|
|
|
|
|
|
|
|
worker_klass.get_deduplication_options
|
2020-03-09 14:07:59 -04:00
|
|
|
end
|
|
|
|
|
2020-11-24 19:09:24 -05:00
|
|
|
def idempotent?
|
|
|
|
return false unless worker_klass
|
|
|
|
return false unless worker_klass.respond_to?(:idempotent?)
|
|
|
|
|
|
|
|
worker_klass.idempotent?
|
|
|
|
end
|
|
|
|
|
2021-10-29 05:10:11 -04:00
|
|
|
def duplicate_key_ttl
|
|
|
|
options[:ttl] || DEFAULT_DUPLICATE_KEY_TTL
|
|
|
|
end
|
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
private
|
|
|
|
|
2021-09-23 18:34:10 -04:00
|
|
|
attr_writer :existing_wal_locations
|
2020-06-16 20:16:40 -04:00
|
|
|
attr_reader :queue_name, :job
|
2020-02-27 13:09:21 -05:00
|
|
|
attr_writer :existing_jid
|
|
|
|
|
2020-06-16 20:16:40 -04:00
|
|
|
def worker_klass
|
|
|
|
@worker_klass ||= worker_class_name.to_s.safe_constantize
|
|
|
|
end
|
|
|
|
|
2021-09-23 18:34:10 -04:00
|
|
|
def job_wal_locations
|
|
|
|
job['wal_locations'] || {}
|
|
|
|
end
|
|
|
|
|
2021-09-16 11:12:47 -04:00
|
|
|
def pg_wal_lsn_diff(connection_name)
|
2022-10-21 08:11:29 -04:00
|
|
|
model = Gitlab::Database.database_base_models[connection_name.to_sym]
|
2021-11-15 10:10:57 -05:00
|
|
|
|
|
|
|
model.connection.load_balancer.wal_diff(
|
|
|
|
job_wal_locations[connection_name],
|
|
|
|
existing_wal_locations[connection_name]
|
|
|
|
)
|
2021-09-16 11:12:47 -04:00
|
|
|
end
|
|
|
|
|
2020-06-16 20:16:40 -04:00
|
|
|
def strategy
|
|
|
|
return DEFAULT_STRATEGY unless worker_klass
|
|
|
|
return DEFAULT_STRATEGY unless worker_klass.respond_to?(:idempotent?)
|
2021-06-04 11:10:25 -04:00
|
|
|
return STRATEGY_NONE unless worker_klass.deduplication_enabled?
|
2020-06-16 20:16:40 -04:00
|
|
|
|
|
|
|
worker_klass.get_deduplicate_strategy
|
|
|
|
end
|
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
def worker_class_name
|
|
|
|
job['class']
|
|
|
|
end
|
|
|
|
|
|
|
|
def arguments
|
|
|
|
job['args']
|
|
|
|
end
|
|
|
|
|
|
|
|
def jid
|
|
|
|
job['jid']
|
|
|
|
end
|
|
|
|
|
2022-10-28 08:11:31 -04:00
|
|
|
def cookie_key
|
2022-11-02 17:09:03 -04:00
|
|
|
"#{idempotency_key}:cookie:v2"
|
2022-10-28 08:11:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_cookie
|
|
|
|
with_redis { |redis| MessagePack.unpack(redis.get(cookie_key) || "\x80") }
|
2022-10-21 08:11:29 -04:00
|
|
|
end
|
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
def idempotency_key
|
2021-06-02 08:10:05 -04:00
|
|
|
@idempotency_key ||= job['idempotency_key'] || "#{namespace}:#{idempotency_hash}"
|
2020-02-27 13:09:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def idempotency_hash
|
|
|
|
Digest::SHA256.hexdigest(idempotency_string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace
|
|
|
|
"#{Gitlab::Redis::Queues::SIDEKIQ_NAMESPACE}:duplicate:#{queue_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def idempotency_string
|
2021-07-06 08:07:43 -04:00
|
|
|
"#{worker_class_name}:#{Sidekiq.dump_json(arguments)}"
|
2020-02-27 13:09:21 -05:00
|
|
|
end
|
2021-09-16 11:12:47 -04:00
|
|
|
|
2021-09-23 18:34:10 -04:00
|
|
|
def existing_wal_locations
|
|
|
|
@existing_wal_locations ||= {}
|
2021-09-16 11:12:47 -04:00
|
|
|
end
|
|
|
|
|
2021-10-22 11:19:11 -04:00
|
|
|
def reschedulable?
|
|
|
|
!scheduled? && options[:if_deduplicated] == :reschedule_once
|
|
|
|
end
|
2022-06-03 11:09:26 -04:00
|
|
|
|
|
|
|
def with_redis
|
|
|
|
if Feature.enabled?(:use_primary_and_secondary_stores_for_duplicate_jobs) ||
|
|
|
|
Feature.enabled?(:use_primary_store_as_default_for_duplicate_jobs)
|
|
|
|
# TODO: Swap for Gitlab::Redis::SharedState after store transition
|
|
|
|
# https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/923
|
|
|
|
Gitlab::Redis::DuplicateJobs.with { |redis| yield redis }
|
|
|
|
else
|
|
|
|
# Keep the old behavior intact if neither feature flag is turned on
|
2022-10-04 05:09:18 -04:00
|
|
|
Sidekiq.redis { |redis| yield redis } # rubocop:disable Cop/SidekiqRedisCall
|
2022-06-03 11:09:26 -04:00
|
|
|
end
|
|
|
|
end
|
2020-02-27 13:09:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|