2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'securerandom'
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
# servers. It is a cheap alternative to using SQL queries and updates:
|
2016-04-02 18:10:28 +05:30
|
|
|
# you do not need to change the SQL schema to start using
|
|
|
|
# ExclusiveLease.
|
|
|
|
#
|
|
|
|
class ExclusiveLease
|
2017-09-10 17:25:29 +05:30
|
|
|
LUA_CANCEL_SCRIPT = <<~EOS.freeze
|
2017-08-17 22:00:37 +05:30
|
|
|
local key, uuid = KEYS[1], ARGV[1]
|
|
|
|
if redis.call("get", key) == uuid then
|
|
|
|
redis.call("del", key)
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
LUA_RENEW_SCRIPT = <<~EOS.freeze
|
|
|
|
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
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def self.get_uuid(key)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.get(redis_shared_state_key(key)) || false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.cancel(key, uuid)
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.eval(LUA_CANCEL_SCRIPT, keys: [redis_shared_state_key(key)], argv: [uuid])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def self.redis_shared_state_key(key)
|
2017-08-17 22:00:37 +05:30
|
|
|
"gitlab:exclusive_lease:#{key}"
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
# 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
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def initialize(key, uuid: nil, timeout:)
|
2017-09-10 17:25:29 +05:30
|
|
|
@redis_shared_state_key = self.class.redis_shared_state_key(key)
|
2017-08-17 22:00:37 +05:30
|
|
|
@timeout = timeout
|
2018-03-17 18:26:18 +05:30
|
|
|
@uuid = uuid || SecureRandom.uuid
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Try to obtain the lease. Return lease UUID on success,
|
2016-04-02 18:10:28 +05:30
|
|
|
# false if the lease is already taken.
|
|
|
|
def try_obtain
|
|
|
|
# Performing a single SET is atomic
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.set(@redis_shared_state_key, @uuid, nx: true, ex: @timeout) && @uuid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
result = redis.eval(LUA_RENEW_SCRIPT, keys: [@redis_shared_state_key], argv: [@uuid, @timeout])
|
|
|
|
result == @uuid
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
# Returns true if the key for this lease is set.
|
|
|
|
def exists?
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.exists(@redis_shared_state_key)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
ttl if ttl.positive?
|
|
|
|
end
|
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
end
|