2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-27 08:59:52 -04:00
|
|
|
require 'securerandom'
|
|
|
|
|
2016-03-10 04:41:16 -05:00
|
|
|
module Gitlab
|
|
|
|
# This class implements an 'exclusive lease'. We call it a 'lease'
|
|
|
|
# because it has a set expiry time. We call it 'exclusive' because only
|
|
|
|
# one caller may obtain a lease for a given key at a time. The
|
|
|
|
# implementation is intended to work across GitLab processes and across
|
2016-10-27 08:59:52 -04:00
|
|
|
# servers. It is a cheap alternative to using SQL queries and updates:
|
2016-03-10 04:41:16 -05:00
|
|
|
# you do not need to change the SQL schema to start using
|
|
|
|
# ExclusiveLease.
|
2016-03-10 06:37:14 -05:00
|
|
|
#
|
2016-03-10 04:41:16 -05:00
|
|
|
class ExclusiveLease
|
2020-05-06 02:09:36 -04:00
|
|
|
PREFIX = 'gitlab:exclusive_lease'
|
|
|
|
NoKey = Class.new(ArgumentError)
|
|
|
|
|
2021-03-25 23:09:21 -04:00
|
|
|
LUA_CANCEL_SCRIPT = <<~EOS
|
2016-10-27 08:59:52 -04:00
|
|
|
local key, uuid = KEYS[1], ARGV[1]
|
|
|
|
if redis.call("get", key) == uuid then
|
|
|
|
redis.call("del", key)
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
|
2021-03-25 23:09:21 -04:00
|
|
|
LUA_RENEW_SCRIPT = <<~EOS
|
2017-06-23 01:36:19 -04:00
|
|
|
local key, uuid, ttl = KEYS[1], ARGV[1], ARGV[2]
|
|
|
|
if redis.call("get", key) == uuid then
|
|
|
|
redis.call("expire", key, ttl)
|
|
|
|
return uuid
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
|
2017-09-04 13:55:04 -04:00
|
|
|
def self.get_uuid(key)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.get(redis_shared_state_key(key)) || false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-16 14:10:35 -04:00
|
|
|
# yield to the {block} at most {count} times per {period}
|
|
|
|
#
|
|
|
|
# Defaults to once per hour.
|
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# # toot the train horn at most every 20min:
|
|
|
|
# throttle(locomotive.id, count: 3, period: 1.hour) { toot_train_horn }
|
|
|
|
# # Brake suddenly at most once every minute:
|
|
|
|
# throttle(locomotive.id, period: 1.minute) { brake_suddenly }
|
|
|
|
# # Specify a uniqueness group:
|
|
|
|
# throttle(locomotive.id, group: :locomotive_brake) { brake_suddenly }
|
|
|
|
#
|
|
|
|
# If a group is not specified, each block will get a separate group to itself.
|
|
|
|
def self.throttle(key, group: nil, period: 1.hour, count: 1, &block)
|
|
|
|
group ||= block.source_location.join(':')
|
|
|
|
|
|
|
|
return if new("el:throttle:#{group}:#{key}", timeout: period.to_i / count).waiting?
|
|
|
|
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2016-10-27 08:59:52 -04:00
|
|
|
def self.cancel(key, uuid)
|
2020-05-06 02:09:36 -04:00
|
|
|
return unless key.present?
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2020-05-06 02:09:36 -04:00
|
|
|
redis.eval(LUA_CANCEL_SCRIPT, keys: [ensure_prefixed_key(key)], argv: [uuid])
|
2016-10-27 08:59:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
def self.redis_shared_state_key(key)
|
2020-05-06 02:09:36 -04:00
|
|
|
"#{PREFIX}:#{key}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ensure_prefixed_key(key)
|
|
|
|
raise NoKey unless key.present?
|
|
|
|
|
|
|
|
key.start_with?(PREFIX) ? key : redis_shared_state_key(key)
|
2016-10-27 08:59:52 -04:00
|
|
|
end
|
|
|
|
|
2018-03-09 08:32:18 -05:00
|
|
|
# Removes any existing exclusive_lease from redis
|
|
|
|
# Don't run this in a live system without making sure no one is using the leases
|
|
|
|
def self.reset_all!(scope = '*')
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.scan_each(match: redis_shared_state_key(scope)).each do |key|
|
|
|
|
redis.del(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-04 13:55:04 -04:00
|
|
|
def initialize(key, uuid: nil, timeout:)
|
2017-07-10 23:35:47 -04:00
|
|
|
@redis_shared_state_key = self.class.redis_shared_state_key(key)
|
2016-10-27 08:59:52 -04:00
|
|
|
@timeout = timeout
|
2017-09-04 13:55:04 -04:00
|
|
|
@uuid = uuid || SecureRandom.uuid
|
2016-03-10 04:41:16 -05:00
|
|
|
end
|
|
|
|
|
2016-10-27 08:59:52 -04:00
|
|
|
# Try to obtain the lease. Return lease UUID on success,
|
2016-03-10 04:41:16 -05:00
|
|
|
# false if the lease is already taken.
|
|
|
|
def try_obtain
|
2016-03-10 12:39:50 -05:00
|
|
|
# Performing a single SET is atomic
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.set(@redis_shared_state_key, @uuid, nx: true, ex: @timeout) && @uuid
|
2016-03-16 13:10:03 -04:00
|
|
|
end
|
2016-03-10 04:41:16 -05:00
|
|
|
end
|
2021-06-16 14:10:35 -04:00
|
|
|
|
|
|
|
# This lease is waiting to obtain
|
|
|
|
def waiting?
|
|
|
|
!try_obtain
|
|
|
|
end
|
2016-03-10 04:41:16 -05:00
|
|
|
|
2017-06-23 01:36:19 -04:00
|
|
|
# Try to renew an existing lease. Return lease UUID on success,
|
|
|
|
# false if the lease is taken by a different UUID or inexistent.
|
|
|
|
def renew
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
result = redis.eval(LUA_RENEW_SCRIPT, keys: [@redis_shared_state_key], argv: [@uuid, @timeout])
|
2017-06-23 01:36:19 -04:00
|
|
|
result == @uuid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-25 10:01:24 -04:00
|
|
|
# Returns true if the key for this lease is set.
|
|
|
|
def exists?
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2022-09-23 20:14:11 -04:00
|
|
|
redis.exists?(@redis_shared_state_key) # rubocop:disable CodeReuse/ActiveRecord
|
2016-10-25 10:01:24 -04:00
|
|
|
end
|
|
|
|
end
|
2018-01-04 10:49:15 -05:00
|
|
|
|
|
|
|
# Returns the TTL of the Redis key.
|
|
|
|
#
|
|
|
|
# This method will return `nil` if no TTL could be obtained.
|
|
|
|
def ttl
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
ttl = redis.ttl(@redis_shared_state_key)
|
|
|
|
|
2020-08-05 11:09:59 -04:00
|
|
|
ttl if ttl > 0
|
2018-01-04 10:49:15 -05:00
|
|
|
end
|
|
|
|
end
|
2020-05-06 02:09:36 -04:00
|
|
|
|
|
|
|
# Gives up this lease, allowing it to be obtained by others.
|
|
|
|
def cancel
|
|
|
|
self.class.cancel(@redis_shared_state_key, @uuid)
|
|
|
|
end
|
2016-03-10 04:41:16 -05:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Gitlab::ExclusiveLease.prepend_mod_with('Gitlab::ExclusiveLease')
|