Class: Gitlab::ExclusiveLease

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/exclusive_lease.rb

Overview

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 servers. It is a cheap alternative to using SQL queries and updates: you do not need to change the SQL schema to start using ExclusiveLease.

Constant Summary collapse

PREFIX =
'gitlab:exclusive_lease'
NoKey =
Class.new(ArgumentError)
LUA_CANCEL_SCRIPT =
<<~EOS.freeze
  local key, uuid = KEYS[1], ARGV[1]
  if redis.call("get", key) == uuid then
    redis.call("del", key)
  end
EOS
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, uuid: nil, timeout:) ⇒ ExclusiveLease

Returns a new instance of ExclusiveLease.


67
68
69
70
71
# File 'lib/gitlab/exclusive_lease.rb', line 67

def initialize(key, uuid: nil, timeout:)
  @redis_shared_state_key = self.class.redis_shared_state_key(key)
  @timeout = timeout
  @uuid = uuid || SecureRandom.uuid
end

Class Method Details

.cancel(key, uuid) ⇒ Object


39
40
41
42
43
44
45
# File 'lib/gitlab/exclusive_lease.rb', line 39

def self.cancel(key, uuid)
  return unless key.present?

  Gitlab::Redis::SharedState.with do |redis|
    redis.eval(LUA_CANCEL_SCRIPT, keys: [ensure_prefixed_key(key)], argv: [uuid])
  end
end

.ensure_prefixed_key(key) ⇒ Object

Raises:


51
52
53
54
55
# File 'lib/gitlab/exclusive_lease.rb', line 51

def self.ensure_prefixed_key(key)
  raise NoKey unless key.present?

  key.start_with?(PREFIX) ? key : redis_shared_state_key(key)
end

.get_uuid(key) ⇒ Object


33
34
35
36
37
# File 'lib/gitlab/exclusive_lease.rb', line 33

def self.get_uuid(key)
  Gitlab::Redis::SharedState.with do |redis|
    redis.get(redis_shared_state_key(key)) || false
  end
end

.redis_shared_state_key(key) ⇒ Object


47
48
49
# File 'lib/gitlab/exclusive_lease.rb', line 47

def self.redis_shared_state_key(key)
  "#{PREFIX}:#{key}"
end

.reset_all!(scope = '*') ⇒ Object

Removes any existing exclusive_lease from redis Don't run this in a live system without making sure no one is using the leases


59
60
61
62
63
64
65
# File 'lib/gitlab/exclusive_lease.rb', line 59

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

Instance Method Details

#cancelObject

Gives up this lease, allowing it to be obtained by others.


110
111
112
# File 'lib/gitlab/exclusive_lease.rb', line 110

def cancel
  self.class.cancel(@redis_shared_state_key, @uuid)
end

#exists?Boolean

Returns true if the key for this lease is set.

Returns:

  • (Boolean)

92
93
94
95
96
# File 'lib/gitlab/exclusive_lease.rb', line 92

def exists?
  Gitlab::Redis::SharedState.with do |redis|
    redis.exists(@redis_shared_state_key)
  end
end

#renewObject

Try to renew an existing lease. Return lease UUID on success, false if the lease is taken by a different UUID or inexistent.


84
85
86
87
88
89
# File 'lib/gitlab/exclusive_lease.rb', line 84

def renew
  Gitlab::Redis::SharedState.with do |redis|
    result = redis.eval(LUA_RENEW_SCRIPT, keys: [@redis_shared_state_key], argv: [@uuid, @timeout])
    result == @uuid
  end
end

#try_obtainObject

Try to obtain the lease. Return lease UUID on success, false if the lease is already taken.


75
76
77
78
79
80
# File 'lib/gitlab/exclusive_lease.rb', line 75

def try_obtain
  # Performing a single SET is atomic
  Gitlab::Redis::SharedState.with do |redis|
    redis.set(@redis_shared_state_key, @uuid, nx: true, ex: @timeout) && @uuid
  end
end

#ttlObject

Returns the TTL of the Redis key.

This method will return `nil` if no TTL could be obtained.


101
102
103
104
105
106
107
# File 'lib/gitlab/exclusive_lease.rb', line 101

def ttl
  Gitlab::Redis::SharedState.with do |redis|
    ttl = redis.ttl(@redis_shared_state_key)

    ttl if ttl > 0
  end
end