redis_queued_locks 0.0.20 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +3 -1
- data/lib/redis_queued_locks/acquier/{delay.rb → acquire_lock/delay_execution.rb} +2 -2
- data/lib/redis_queued_locks/acquier/{try.rb → acquire_lock/try_to_lock.rb} +1 -1
- data/lib/redis_queued_locks/acquier/acquire_lock/with_acq_timeout.rb +29 -0
- data/lib/redis_queued_locks/acquier/{yield_expire.rb → acquire_lock/yield_with_expire.rb} +1 -1
- data/lib/redis_queued_locks/acquier/acquire_lock.rb +270 -0
- data/lib/redis_queued_locks/acquier/extend_lock_ttl.rb +18 -0
- data/lib/redis_queued_locks/acquier/is_locked.rb +18 -0
- data/lib/redis_queued_locks/acquier/is_queued.rb +18 -0
- data/lib/redis_queued_locks/acquier/keys.rb +26 -0
- data/lib/redis_queued_locks/acquier/lock_info.rb +57 -0
- data/lib/redis_queued_locks/acquier/locks.rb +26 -0
- data/lib/redis_queued_locks/acquier/queue_info.rb +50 -0
- data/lib/redis_queued_locks/acquier/queues.rb +26 -0
- data/lib/redis_queued_locks/acquier/release_all_locks.rb +85 -0
- data/lib/redis_queued_locks/acquier/release_lock.rb +73 -0
- data/lib/redis_queued_locks/acquier.rb +11 -557
- data/lib/redis_queued_locks/client.rb +19 -11
- data/lib/redis_queued_locks/utilities.rb +16 -0
- data/lib/redis_queued_locks/version.rb +2 -2
- data/lib/redis_queued_locks.rb +1 -0
- metadata +18 -6
- data/lib/redis_queued_locks/acquier/release.rb +0 -60
@@ -2,562 +2,16 @@
|
|
2
2
|
|
3
3
|
# @api private
|
4
4
|
# @since 0.1.0
|
5
|
-
# rubocop:disable Metrics/ModuleLength
|
6
5
|
module RedisQueuedLocks::Acquier
|
7
|
-
require_relative 'acquier/
|
8
|
-
require_relative 'acquier/
|
9
|
-
require_relative 'acquier/
|
10
|
-
require_relative 'acquier/
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
# @since 0.1.0
|
19
|
-
extend Release
|
20
|
-
|
21
|
-
# @return [Integer] Redis expiration error in milliseconds.
|
22
|
-
#
|
23
|
-
# @api private
|
24
|
-
# @since 0.1.0
|
25
|
-
REDIS_EXPIRE_ERROR = 1
|
26
|
-
|
27
|
-
# rubocop:disable Metrics/ClassLength
|
28
|
-
class << self
|
29
|
-
# @param redis [RedisClient]
|
30
|
-
# Redis connection client.
|
31
|
-
# @param lock_name [String]
|
32
|
-
# Lock name to be acquier.
|
33
|
-
# @option process_id [Integer,String]
|
34
|
-
# The process that want to acquire a lock.
|
35
|
-
# @option thread_id [Integer,String]
|
36
|
-
# The process's thread that want to acquire a lock.
|
37
|
-
# @option fiber_id [Integer,String]
|
38
|
-
# A current fiber that want to acquire a lock.
|
39
|
-
# @option ractor_id [Integer,String]
|
40
|
-
# The current ractor that want to acquire a lock.
|
41
|
-
# @option ttl [Integer,NilClass]
|
42
|
-
# Lock's time to live (in milliseconds). Nil means "without timeout".
|
43
|
-
# @option queue_ttl [Integer]
|
44
|
-
# Lifetime of the acuier's lock request. In seconds.
|
45
|
-
# @option timeout [Integer]
|
46
|
-
# Time period whe should try to acquire the lock (in seconds).
|
47
|
-
# @option timed [Boolean]
|
48
|
-
# Limit the invocation time period of the passed block of code by the lock's TTL.
|
49
|
-
# @option retry_count [Integer,NilClass]
|
50
|
-
# How many times we should try to acquire a lock. Nil means "infinite retries".
|
51
|
-
# @option retry_delay [Integer]
|
52
|
-
# A time-interval between the each retry (in milliseconds).
|
53
|
-
# @option retry_jitter [Integer]
|
54
|
-
# Time-shift range for retry-delay (in milliseconds).
|
55
|
-
# @option raise_errors [Boolean]
|
56
|
-
# Raise errors on exceptional cases.
|
57
|
-
# @option instrumenter [#notify]
|
58
|
-
# See RedisQueuedLocks::Instrument::ActiveSupport for example.
|
59
|
-
# @option identity [String]
|
60
|
-
# Unique acquire identifier that is also should be unique between processes and pods
|
61
|
-
# on different machines. By default the uniq identity string is
|
62
|
-
# represented as 10 bytes hexstr.
|
63
|
-
# @option fail_fast [Boolean]
|
64
|
-
# Should the required lock to be checked before the try and exit immidetly if lock is
|
65
|
-
# already obtained.
|
66
|
-
# @option metadata [NilClass,Any]
|
67
|
-
# - A custom metadata wich will be passed to the instrumenter's payload with :meta key;
|
68
|
-
# @param [Block]
|
69
|
-
# A block of code that should be executed after the successfully acquired lock.
|
70
|
-
# @return [RedisQueuedLocks::Data,Hash<Symbol,Any>,yield]
|
71
|
-
# - Format: { ok: true/false, result: Any }
|
72
|
-
# - If block is given the result of block's yeld will be returned.
|
73
|
-
#
|
74
|
-
# @api private
|
75
|
-
# @since 0.1.0
|
76
|
-
# rubocop:disable Metrics/MethodLength, Metrics/BlockNesting
|
77
|
-
def acquire_lock!(
|
78
|
-
redis,
|
79
|
-
lock_name,
|
80
|
-
process_id:,
|
81
|
-
thread_id:,
|
82
|
-
fiber_id:,
|
83
|
-
ractor_id:,
|
84
|
-
ttl:,
|
85
|
-
queue_ttl:,
|
86
|
-
timeout:,
|
87
|
-
timed:,
|
88
|
-
retry_count:,
|
89
|
-
retry_delay:,
|
90
|
-
retry_jitter:,
|
91
|
-
raise_errors:,
|
92
|
-
instrumenter:,
|
93
|
-
identity:,
|
94
|
-
fail_fast:,
|
95
|
-
metadata:,
|
96
|
-
&block
|
97
|
-
)
|
98
|
-
# Step 1: prepare lock requirements (generate lock name, calc lock ttl, etc).
|
99
|
-
acquier_id = RedisQueuedLocks::Resource.acquier_identifier(
|
100
|
-
process_id,
|
101
|
-
thread_id,
|
102
|
-
fiber_id,
|
103
|
-
ractor_id,
|
104
|
-
identity
|
105
|
-
)
|
106
|
-
# NOTE:
|
107
|
-
# - think aobut the redis expiration error
|
108
|
-
# - (ttl - REDIS_EXPIRE_ERROR).yield_self { |val| (val == 0) ? ttl : val }
|
109
|
-
lock_ttl = ttl
|
110
|
-
lock_key = RedisQueuedLocks::Resource.prepare_lock_key(lock_name)
|
111
|
-
lock_key_queue = RedisQueuedLocks::Resource.prepare_lock_queue(lock_name)
|
112
|
-
acquier_position = RedisQueuedLocks::Resource.calc_initial_acquier_position
|
113
|
-
|
114
|
-
# Step X: intermediate result observer
|
115
|
-
acq_process = {
|
116
|
-
lock_info: {},
|
117
|
-
should_try: true,
|
118
|
-
tries: 0,
|
119
|
-
acquired: false,
|
120
|
-
result: nil,
|
121
|
-
acq_time: nil, # NOTE: in milliseconds
|
122
|
-
hold_time: nil, # NOTE: in milliseconds
|
123
|
-
rel_time: nil # NOTE: in milliseconds
|
124
|
-
}
|
125
|
-
acq_dequeue = -> { dequeue_from_lock_queue(redis, lock_key_queue, acquier_id) }
|
126
|
-
|
127
|
-
# Step 2: try to lock with timeout
|
128
|
-
with_acq_timeout(timeout, lock_key, raise_errors, on_timeout: acq_dequeue) do
|
129
|
-
acq_start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
130
|
-
|
131
|
-
# Step 2.1: caclically try to obtain the lock
|
132
|
-
while acq_process[:should_try]
|
133
|
-
try_to_lock(
|
134
|
-
redis,
|
135
|
-
lock_key,
|
136
|
-
lock_key_queue,
|
137
|
-
acquier_id,
|
138
|
-
acquier_position,
|
139
|
-
lock_ttl,
|
140
|
-
queue_ttl,
|
141
|
-
fail_fast
|
142
|
-
) => { ok:, result: }
|
143
|
-
|
144
|
-
acq_end_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
145
|
-
acq_time = ((acq_end_time - acq_start_time) * 1_000).ceil(2)
|
146
|
-
|
147
|
-
# Step X: save the intermediate results to the result observer
|
148
|
-
acq_process[:result] = result
|
149
|
-
acq_process[:acq_end_time] = acq_end_time
|
150
|
-
|
151
|
-
# Step 2.1: analyze an acquirement attempt
|
152
|
-
if ok
|
153
|
-
# Step X (instrumentation): lock obtained
|
154
|
-
instrumenter.notify('redis_queued_locks.lock_obtained', {
|
155
|
-
lock_key: result[:lock_key],
|
156
|
-
ttl: result[:ttl],
|
157
|
-
acq_id: result[:acq_id],
|
158
|
-
ts: result[:ts],
|
159
|
-
acq_time: acq_time,
|
160
|
-
meta: metadata
|
161
|
-
})
|
162
|
-
|
163
|
-
# Step 2.1.a: successfully acquired => build the result
|
164
|
-
acq_process[:lock_info] = {
|
165
|
-
lock_key: result[:lock_key],
|
166
|
-
acq_id: result[:acq_id],
|
167
|
-
ts: result[:ts],
|
168
|
-
ttl: result[:ttl]
|
169
|
-
}
|
170
|
-
acq_process[:acquired] = true
|
171
|
-
acq_process[:should_try] = false
|
172
|
-
acq_process[:acq_time] = acq_time
|
173
|
-
acq_process[:acq_end_time] = acq_end_time
|
174
|
-
elsif fail_fast && acq_process[:result] == :fail_fast_no_try
|
175
|
-
acq_process[:should_try] = false
|
176
|
-
if raise_errors
|
177
|
-
raise(RedisQueuedLocks::LockAlreadyObtainedError, <<~ERROR_MESSAGE.strip)
|
178
|
-
Lock "#{lock_key}" is already obtained.
|
179
|
-
ERROR_MESSAGE
|
180
|
-
end
|
181
|
-
else
|
182
|
-
# Step 2.1.b: failed acquirement => retry
|
183
|
-
acq_process[:tries] += 1
|
184
|
-
|
185
|
-
if (retry_count != nil && acq_process[:tries] >= retry_count) || fail_fast
|
186
|
-
# NOTE:
|
187
|
-
# - reached the retry limit => quit from the loop
|
188
|
-
# - should fail fast => quit from the loop
|
189
|
-
acq_process[:should_try] = false
|
190
|
-
acq_process[:result] = fail_fast ? :fail_fast_after_try : :retry_limit_reached
|
191
|
-
|
192
|
-
# NOTE:
|
193
|
-
# - reached the retry limit => dequeue from the lock queue
|
194
|
-
# - should fail fast => dequeue from the lock queue
|
195
|
-
acq_dequeue.call
|
196
|
-
|
197
|
-
# NOTE: check and raise an error
|
198
|
-
if fail_fast && raise_errors
|
199
|
-
raise(RedisQueuedLocks::LockAlreadyObtainedError, <<~ERROR_MESSAGE.strip)
|
200
|
-
Lock "#{lock_key}" is already obtained.
|
201
|
-
ERROR_MESSAGE
|
202
|
-
elsif raise_errors
|
203
|
-
raise(RedisQueuedLocks::LockAcquiermentRetryLimitError, <<~ERROR_MESSAGE.strip)
|
204
|
-
Failed to acquire the lock "#{lock_key}"
|
205
|
-
for the given retry_count limit (#{retry_count} times).
|
206
|
-
ERROR_MESSAGE
|
207
|
-
end
|
208
|
-
else
|
209
|
-
# NOTE:
|
210
|
-
# delay the exceution in order to prevent chaotic attempts
|
211
|
-
# and to allow other processes and threads to obtain the lock too.
|
212
|
-
delay_execution(retry_delay, retry_jitter)
|
213
|
-
end
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
# Step 3: analyze acquirement result
|
219
|
-
if acq_process[:acquired]
|
220
|
-
# Step 3.a: acquired successfully => run logic or return the result of acquirement
|
221
|
-
if block_given?
|
222
|
-
begin
|
223
|
-
yield_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
224
|
-
ttl_shift = ((yield_time - acq_process[:acq_end_time]) * 1000).ceil(2)
|
225
|
-
yield_with_expire(redis, lock_key, timed, ttl_shift, ttl, &block)
|
226
|
-
ensure
|
227
|
-
acq_process[:rel_time] = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
228
|
-
acq_process[:hold_time] = (
|
229
|
-
(acq_process[:rel_time] - acq_process[:acq_end_time]) * 1000
|
230
|
-
).ceil(2)
|
231
|
-
|
232
|
-
# Step X (instrumentation): lock_hold_and_release
|
233
|
-
run_non_critical do
|
234
|
-
instrumenter.notify('redis_queued_locks.lock_hold_and_release', {
|
235
|
-
hold_time: acq_process[:hold_time],
|
236
|
-
ttl: acq_process[:lock_info][:ttl],
|
237
|
-
acq_id: acq_process[:lock_info][:acq_id],
|
238
|
-
ts: acq_process[:lock_info][:ts],
|
239
|
-
lock_key: acq_process[:lock_info][:lock_key],
|
240
|
-
acq_time: acq_process[:acq_time],
|
241
|
-
meta: metadata
|
242
|
-
})
|
243
|
-
end
|
244
|
-
end
|
245
|
-
else
|
246
|
-
RedisQueuedLocks::Data[ok: true, result: acq_process[:lock_info]]
|
247
|
-
end
|
248
|
-
else
|
249
|
-
if acq_process[:result] != :retry_limit_reached &&
|
250
|
-
acq_process[:result] != :fail_fast_no_try &&
|
251
|
-
acq_process[:result] != :fail_fast_after_try
|
252
|
-
# NOTE: we have only two situations if lock is not acquired withou fast-fail flag:
|
253
|
-
# - time limit is reached
|
254
|
-
# - retry count limit is reached
|
255
|
-
# In other cases the lock obtaining time and tries count are infinite.
|
256
|
-
acq_process[:result] = :timeout_reached
|
257
|
-
end
|
258
|
-
# Step 3.b: lock is not acquired (acquier is dequeued by timeout callback)
|
259
|
-
RedisQueuedLocks::Data[ok: false, result: acq_process[:result]]
|
260
|
-
end
|
261
|
-
end
|
262
|
-
# rubocop:enable Metrics/MethodLength, Metrics/BlockNesting
|
263
|
-
|
264
|
-
# Release the concrete lock:
|
265
|
-
# - 1. clear lock queue: al; related processes released
|
266
|
-
# from the lock aquierment and should retry;
|
267
|
-
# - 2. delete the lock: drop lock key from Redis;
|
268
|
-
# It is safe because the lock obtain logic is transactional and
|
269
|
-
# watches the original lock for changes.
|
270
|
-
#
|
271
|
-
# @param redis [RedisClient]
|
272
|
-
# Redis connection client.
|
273
|
-
# @param lock_name [String]
|
274
|
-
# The lock name that should be released.
|
275
|
-
# @param isntrumenter [#notify]
|
276
|
-
# See RedisQueuedLocks::Instrument::ActiveSupport for example.
|
277
|
-
# @return [RedisQueuedLocks::Data,Hash<Symbol,Any>]
|
278
|
-
# Format: { ok: true/false, result: Hash<Symbil,Numeric|String> }
|
279
|
-
#
|
280
|
-
# @api private
|
281
|
-
# @since 0.1.0
|
282
|
-
def release_lock!(redis, lock_name, instrumenter)
|
283
|
-
lock_key = RedisQueuedLocks::Resource.prepare_lock_key(lock_name)
|
284
|
-
lock_key_queue = RedisQueuedLocks::Resource.prepare_lock_queue(lock_name)
|
285
|
-
|
286
|
-
rel_start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
287
|
-
fully_release_lock(redis, lock_key, lock_key_queue) => { ok:, result: }
|
288
|
-
time_at = Time.now.to_i
|
289
|
-
rel_end_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
290
|
-
rel_time = ((rel_end_time - rel_start_time) * 1_000).ceil(2)
|
291
|
-
|
292
|
-
run_non_critical do
|
293
|
-
instrumenter.notify('redis_queued_locks.explicit_lock_release', {
|
294
|
-
lock_key: lock_key,
|
295
|
-
lock_key_queue: lock_key_queue,
|
296
|
-
rel_time: rel_time,
|
297
|
-
at: time_at
|
298
|
-
})
|
299
|
-
end
|
300
|
-
|
301
|
-
RedisQueuedLocks::Data[
|
302
|
-
ok: true,
|
303
|
-
result: { rel_time: rel_time, rel_key: lock_key, rel_queue: lock_key_queue }
|
304
|
-
]
|
305
|
-
end
|
306
|
-
|
307
|
-
# Release all locks:
|
308
|
-
# - 1. clear all lock queus: drop them all from Redis database by the lock queue pattern;
|
309
|
-
# - 2. delete all locks: drop lock keys from Redis by the lock key pattern;
|
310
|
-
#
|
311
|
-
# @param redis [RedisClient]
|
312
|
-
# Redis connection client.
|
313
|
-
# @param batch_size [Integer]
|
314
|
-
# The number of lock keys that should be released in a time.
|
315
|
-
# @param isntrumenter [#notify]
|
316
|
-
# See RedisQueuedLocks::Instrument::ActiveSupport for example.
|
317
|
-
# @return [RedisQueuedLocks::Data,Hash<Symbol,Any>]
|
318
|
-
# Format: { ok: true/false, result: Hash<Symbol,Numeric> }
|
319
|
-
#
|
320
|
-
# @api private
|
321
|
-
# @since 0.1.0
|
322
|
-
def release_all_locks!(redis, batch_size, instrumenter)
|
323
|
-
rel_start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
324
|
-
fully_release_all_locks(redis, batch_size) => { ok:, result: }
|
325
|
-
time_at = Time.now.to_i
|
326
|
-
rel_end_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
327
|
-
rel_time = ((rel_end_time - rel_start_time) * 1_000).ceil(2)
|
328
|
-
|
329
|
-
run_non_critical do
|
330
|
-
instrumenter.notify('redis_queued_locks.explicit_all_locks_release', {
|
331
|
-
at: time_at,
|
332
|
-
rel_time: rel_time,
|
333
|
-
rel_keys: result[:rel_keys]
|
334
|
-
})
|
335
|
-
end
|
336
|
-
|
337
|
-
RedisQueuedLocks::Data[
|
338
|
-
ok: true,
|
339
|
-
result: { rel_key_cnt: result[:rel_keys], rel_time: rel_time }
|
340
|
-
]
|
341
|
-
end
|
342
|
-
|
343
|
-
# @param redis_client [RedisClient]
|
344
|
-
# @param lock_name [String]
|
345
|
-
# @return [Boolean]
|
346
|
-
#
|
347
|
-
# @api private
|
348
|
-
# @since 0.1.0
|
349
|
-
def locked?(redis_client, lock_name)
|
350
|
-
lock_key = RedisQueuedLocks::Resource.prepare_lock_key(lock_name)
|
351
|
-
redis_client.call('EXISTS', lock_key) == 1
|
352
|
-
end
|
353
|
-
|
354
|
-
# @param redis_client [RedisClient]
|
355
|
-
# @param lock_name [String]
|
356
|
-
# @return [Boolean]
|
357
|
-
#
|
358
|
-
# @api private
|
359
|
-
# @since 0.1.0
|
360
|
-
def queued?(redis_client, lock_name)
|
361
|
-
lock_key_queue = RedisQueuedLocks::Resource.prepare_lock_queue(lock_name)
|
362
|
-
redis_client.call('EXISTS', lock_key_queue) == 1
|
363
|
-
end
|
364
|
-
|
365
|
-
# @param redis_client [RedisClient]
|
366
|
-
# @param lock_name [String]
|
367
|
-
# @return [Hash<Symbol,String|Numeric>,NilClass]
|
368
|
-
# - `nil` is returned when lock key does not exist or expired;
|
369
|
-
# - result format: {
|
370
|
-
# lock_key: "rql:lock:your_lockname", # acquired lock key
|
371
|
-
# acq_id: "rql:acq:process_id/thread_id", # lock acquier identifier
|
372
|
-
# ts: 123456789, # <locked at> time stamp (epoch)
|
373
|
-
# ini_ttl: 123456789, # initial lock key ttl (milliseconds),
|
374
|
-
# rem_ttl: 123456789, # remaining lock key ttl (milliseconds)
|
375
|
-
# }
|
376
|
-
#
|
377
|
-
# @api private
|
378
|
-
# @since 0.1.0
|
379
|
-
def lock_info(redis_client, lock_name)
|
380
|
-
lock_key = RedisQueuedLocks::Resource.prepare_lock_key(lock_name)
|
381
|
-
|
382
|
-
result = redis_client.multi(watch: [lock_key]) do |transact|
|
383
|
-
transact.call('HGETALL', lock_key)
|
384
|
-
transact.call('PTTL', lock_key)
|
385
|
-
end
|
386
|
-
|
387
|
-
if result == nil
|
388
|
-
# NOTE:
|
389
|
-
# - nil result means that during transaction invocation the lock is changed (CAS):
|
390
|
-
# - lock is expired;
|
391
|
-
# - lock is released;
|
392
|
-
# - lock is expired + re-obtained;
|
393
|
-
nil
|
394
|
-
else
|
395
|
-
hget_cmd_res = result[0]
|
396
|
-
pttl_cmd_res = result[1]
|
397
|
-
|
398
|
-
if hget_cmd_res == {} || pttl_cmd_res == -2 # NOTE: key does not exist
|
399
|
-
nil
|
400
|
-
else
|
401
|
-
# NOTE: the result of MULTI-command is an array of results of each internal command
|
402
|
-
# - result[0] (HGETALL) (Hash<String,String>)
|
403
|
-
# - result[1] (PTTL) (Integer)
|
404
|
-
{
|
405
|
-
lock_key: lock_key,
|
406
|
-
acq_id: hget_cmd_res['acq_id'],
|
407
|
-
ts: Integer(hget_cmd_res['ts']),
|
408
|
-
ini_ttl: Integer(hget_cmd_res['ini_ttl']),
|
409
|
-
rem_ttl: ((pttl_cmd_res == -1) ? Infinity : pttl_cmd_res)
|
410
|
-
}
|
411
|
-
end
|
412
|
-
end
|
413
|
-
end
|
414
|
-
|
415
|
-
# Returns an information about the required lock queue by the lock name. The result
|
416
|
-
# represnts the ordered lock request queue that is ordered by score (Redis sets) and shows
|
417
|
-
# lock acquirers and their position in queue. Async nature with redis communcation can lead
|
418
|
-
# the sitaution when the queue becomes empty during the queue data extraction. So sometimes
|
419
|
-
# you can receive the lock queue info with empty queue.
|
420
|
-
#
|
421
|
-
# @param redis_client [RedisClient]
|
422
|
-
# @param lock_name [String]
|
423
|
-
# @return [Hash<Symbol,String|Array<Hash<Symbol,String|Float>>,NilClass]
|
424
|
-
# - `nil` is returned when lock queue does not exist;
|
425
|
-
# - result format: {
|
426
|
-
# lock_queue: "rql:lock_queue:your_lock_name", # lock queue key in redis,
|
427
|
-
# queue: [
|
428
|
-
# { acq_id: "rql:acq:process_id/thread_id", score: 123 },
|
429
|
-
# { acq_id: "rql:acq:process_id/thread_id", score: 456 },
|
430
|
-
# ] # ordered set (by score) with information about an acquier and their position in queue
|
431
|
-
# }
|
432
|
-
#
|
433
|
-
# @api private
|
434
|
-
# @since 0.1.0
|
435
|
-
def queue_info(redis_client, lock_name)
|
436
|
-
lock_key_queue = RedisQueuedLocks::Resource.prepare_lock_queue(lock_name)
|
437
|
-
|
438
|
-
result = redis_client.pipelined do |pipeline|
|
439
|
-
pipeline.call('EXISTS', lock_key_queue)
|
440
|
-
pipeline.call('ZRANGE', lock_key_queue, '0', '-1', 'WITHSCORES')
|
441
|
-
end
|
442
|
-
|
443
|
-
exists_cmd_res = result[0]
|
444
|
-
zrange_cmd_res = result[1]
|
445
|
-
|
446
|
-
if exists_cmd_res == 1
|
447
|
-
# NOTE: queue existed during the piepline invocation
|
448
|
-
{
|
449
|
-
lock_queue: lock_key_queue,
|
450
|
-
queue: zrange_cmd_res.map { |val| { acq_id: val[0], score: val[1] } }
|
451
|
-
}
|
452
|
-
else
|
453
|
-
# NOTE: queue did not exist during the pipeline invocation
|
454
|
-
nil
|
455
|
-
end
|
456
|
-
end
|
457
|
-
|
458
|
-
# @param redis_client [RedisClient]
|
459
|
-
# @param lock_name [String]
|
460
|
-
# @param milliseconds [Integer]
|
461
|
-
# @return [?]
|
462
|
-
#
|
463
|
-
# @api private
|
464
|
-
# @since 0.1.0
|
465
|
-
def extend_lock_ttl(redis_client, lock_name, milliseconds)
|
466
|
-
# TODO: realize
|
467
|
-
end
|
468
|
-
|
469
|
-
# @param redis_client [RedisClient]
|
470
|
-
# @option scan_size [Integer]
|
471
|
-
# @return [Set<String>]
|
472
|
-
#
|
473
|
-
# @api private
|
474
|
-
# @since 0.1.0
|
475
|
-
def locks(redis_client, scan_size:)
|
476
|
-
Set.new.tap do |lock_keys|
|
477
|
-
redis_client.scan(
|
478
|
-
'MATCH',
|
479
|
-
RedisQueuedLocks::Resource::LOCK_PATTERN,
|
480
|
-
count: scan_size
|
481
|
-
) do |lock_key|
|
482
|
-
# TODO: reduce unnecessary iterations
|
483
|
-
lock_keys.add(lock_key)
|
484
|
-
end
|
485
|
-
end
|
486
|
-
end
|
487
|
-
|
488
|
-
# @param redis_client [RedisClient]
|
489
|
-
# @param scan_size [Integer]
|
490
|
-
# @return [Set<String>]
|
491
|
-
#
|
492
|
-
# @api private
|
493
|
-
# @since 0.1.0
|
494
|
-
def queues(redis_client, scan_size:)
|
495
|
-
Set.new.tap do |lock_queues|
|
496
|
-
redis_client.scan(
|
497
|
-
'MATCH',
|
498
|
-
RedisQueuedLocks::Resource::LOCK_QUEUE_PATTERN,
|
499
|
-
count: scan_size
|
500
|
-
) do |lock_queue|
|
501
|
-
# TODO: reduce unnecessary iterations
|
502
|
-
lock_queues.add(lock_queue)
|
503
|
-
end
|
504
|
-
end
|
505
|
-
end
|
506
|
-
|
507
|
-
# @param redis_client [RedisClient]
|
508
|
-
# @option scan_size [Integer]
|
509
|
-
# @return [Array<String>]
|
510
|
-
#
|
511
|
-
# @api private
|
512
|
-
# @since 0.1.0
|
513
|
-
def keys(redis_client, scan_size:)
|
514
|
-
Set.new.tap do |keys|
|
515
|
-
redis_client.scan(
|
516
|
-
'MATCH',
|
517
|
-
RedisQueuedLocks::Resource::KEY_PATTERN,
|
518
|
-
count: scan_size
|
519
|
-
) do |key|
|
520
|
-
# TODO: reduce unnecessary iterations
|
521
|
-
keys.add(key)
|
522
|
-
end
|
523
|
-
end
|
524
|
-
end
|
525
|
-
|
526
|
-
private
|
527
|
-
|
528
|
-
# @param timeout [NilClass,Integer]
|
529
|
-
# Time period after which the logic will fail with timeout error.
|
530
|
-
# @param lock_key [String]
|
531
|
-
# Lock name.
|
532
|
-
# @param raise_errors [Boolean]
|
533
|
-
# Raise erros on exceptional cases.
|
534
|
-
# @option on_timeout [Proc,NilClass]
|
535
|
-
# Callback invoked on Timeout::Error.
|
536
|
-
# @return [Any]
|
537
|
-
#
|
538
|
-
# @api private
|
539
|
-
# @since 0.1.0
|
540
|
-
def with_acq_timeout(timeout, lock_key, raise_errors, on_timeout: nil, &block)
|
541
|
-
::Timeout.timeout(timeout, &block)
|
542
|
-
rescue ::Timeout::Error
|
543
|
-
on_timeout.call unless on_timeout == nil
|
544
|
-
|
545
|
-
if raise_errors
|
546
|
-
raise(RedisQueuedLocks::LockAcquiermentTimeoutError, <<~ERROR_MESSAGE.strip)
|
547
|
-
Failed to acquire the lock "#{lock_key}" for the given timeout (#{timeout} seconds).
|
548
|
-
ERROR_MESSAGE
|
549
|
-
end
|
550
|
-
end
|
551
|
-
|
552
|
-
# @param block [Block]
|
553
|
-
# @return [Any]
|
554
|
-
#
|
555
|
-
# @api private
|
556
|
-
# @since 0.1.0
|
557
|
-
def run_non_critical(&block)
|
558
|
-
yield rescue nil
|
559
|
-
end
|
560
|
-
end
|
561
|
-
# rubocop:enable Metrics/ClassLength
|
6
|
+
require_relative 'acquier/acquire_lock'
|
7
|
+
require_relative 'acquier/release_lock'
|
8
|
+
require_relative 'acquier/release_all_locks'
|
9
|
+
require_relative 'acquier/is_locked'
|
10
|
+
require_relative 'acquier/is_queued'
|
11
|
+
require_relative 'acquier/lock_info'
|
12
|
+
require_relative 'acquier/queue_info'
|
13
|
+
require_relative 'acquier/locks'
|
14
|
+
require_relative 'acquier/queues'
|
15
|
+
require_relative 'acquier/keys'
|
16
|
+
require_relative 'acquier/extend_lock_ttl'
|
562
17
|
end
|
563
|
-
# rubocop:enable Metrics/ModuleLength
|
@@ -117,7 +117,7 @@ class RedisQueuedLocks::Client
|
|
117
117
|
metadata: nil,
|
118
118
|
&block
|
119
119
|
)
|
120
|
-
RedisQueuedLocks::Acquier.acquire_lock
|
120
|
+
RedisQueuedLocks::Acquier::AcquireLock.acquire_lock(
|
121
121
|
redis_client,
|
122
122
|
lock_name,
|
123
123
|
process_id: RedisQueuedLocks::Resource.get_process_id,
|
@@ -183,7 +183,11 @@ class RedisQueuedLocks::Client
|
|
183
183
|
# @api public
|
184
184
|
# @since 0.1.0
|
185
185
|
def unlock(lock_name)
|
186
|
-
RedisQueuedLocks::Acquier.release_lock
|
186
|
+
RedisQueuedLocks::Acquier::ReleaseLock.release_lock(
|
187
|
+
redis_client,
|
188
|
+
lock_name,
|
189
|
+
config[:instrumenter]
|
190
|
+
)
|
187
191
|
end
|
188
192
|
|
189
193
|
# @param lock_name [String]
|
@@ -192,7 +196,7 @@ class RedisQueuedLocks::Client
|
|
192
196
|
# @api public
|
193
197
|
# @since 0.1.0
|
194
198
|
def locked?(lock_name)
|
195
|
-
RedisQueuedLocks::Acquier.locked?(redis_client, lock_name)
|
199
|
+
RedisQueuedLocks::Acquier::IsLocked.locked?(redis_client, lock_name)
|
196
200
|
end
|
197
201
|
|
198
202
|
# @param lock_name [String]
|
@@ -201,7 +205,7 @@ class RedisQueuedLocks::Client
|
|
201
205
|
# @api public
|
202
206
|
# @since 0.1.0
|
203
207
|
def queued?(lock_name)
|
204
|
-
RedisQueuedLocks::Acquier.queued?(redis_client, lock_name)
|
208
|
+
RedisQueuedLocks::Acquier::IsQueued.queued?(redis_client, lock_name)
|
205
209
|
end
|
206
210
|
|
207
211
|
# @param lock_name [String]
|
@@ -210,7 +214,7 @@ class RedisQueuedLocks::Client
|
|
210
214
|
# @api public
|
211
215
|
# @since 0.1.0
|
212
216
|
def lock_info(lock_name)
|
213
|
-
RedisQueuedLocks::Acquier.lock_info(redis_client, lock_name)
|
217
|
+
RedisQueuedLocks::Acquier::LockInfo.lock_info(redis_client, lock_name)
|
214
218
|
end
|
215
219
|
|
216
220
|
# @param lock_name [String]
|
@@ -219,7 +223,7 @@ class RedisQueuedLocks::Client
|
|
219
223
|
# @api public
|
220
224
|
# @since 0.1.0
|
221
225
|
def queue_info(lock_name)
|
222
|
-
RedisQueuedLocks::Acquier.queue_info(redis_client, lock_name)
|
226
|
+
RedisQueuedLocks::Acquier::QueueInfo.queue_info(redis_client, lock_name)
|
223
227
|
end
|
224
228
|
|
225
229
|
# @param lock_name [String]
|
@@ -229,7 +233,7 @@ class RedisQueuedLocks::Client
|
|
229
233
|
# @api public
|
230
234
|
# @since 0.1.0
|
231
235
|
def extend_lock_ttl(lock_name, milliseconds)
|
232
|
-
RedisQueuedLocks::Acquier.extend_lock_ttl(redis_client, lock_name)
|
236
|
+
RedisQueuedLocks::Acquier::ExtendLockTTL.extend_lock_ttl(redis_client, lock_name)
|
233
237
|
end
|
234
238
|
|
235
239
|
# @option batch_size [Integer]
|
@@ -239,7 +243,11 @@ class RedisQueuedLocks::Client
|
|
239
243
|
# @api public
|
240
244
|
# @since 0.1.0
|
241
245
|
def clear_locks(batch_size: config[:lock_release_batch_size])
|
242
|
-
RedisQueuedLocks::Acquier.release_all_locks
|
246
|
+
RedisQueuedLocks::Acquier::ReleaseAllLocks.release_all_locks(
|
247
|
+
redis_client,
|
248
|
+
batch_size,
|
249
|
+
config[:instrumenter]
|
250
|
+
)
|
243
251
|
end
|
244
252
|
|
245
253
|
# @option scan_size [Integer]
|
@@ -248,7 +256,7 @@ class RedisQueuedLocks::Client
|
|
248
256
|
# @api public
|
249
257
|
# @since 0.1.0
|
250
258
|
def locks(scan_size: config[:key_extraction_batch_size])
|
251
|
-
RedisQueuedLocks::Acquier.locks(redis_client, scan_size:)
|
259
|
+
RedisQueuedLocks::Acquier::Locks.locks(redis_client, scan_size:)
|
252
260
|
end
|
253
261
|
|
254
262
|
# @option scan_size [Integer]
|
@@ -257,7 +265,7 @@ class RedisQueuedLocks::Client
|
|
257
265
|
# @api public
|
258
266
|
# @since 0.1.0
|
259
267
|
def queues(scan_size: config[:key_extraction_batch_size])
|
260
|
-
RedisQueuedLocks::Acquier.queues(redis_client, scan_size:)
|
268
|
+
RedisQueuedLocks::Acquier::Queues.queues(redis_client, scan_size:)
|
261
269
|
end
|
262
270
|
|
263
271
|
# @option scan_size [Integer]
|
@@ -266,7 +274,7 @@ class RedisQueuedLocks::Client
|
|
266
274
|
# @api public
|
267
275
|
# @since 0.1.0
|
268
276
|
def keys(scan_size: config[:key_extraction_batch_size])
|
269
|
-
RedisQueuedLocks::Acquier.keys(redis_client, scan_size:)
|
277
|
+
RedisQueuedLocks::Acquier::Keys.keys(redis_client, scan_size:)
|
270
278
|
end
|
271
279
|
end
|
272
280
|
# rubocop:enable Metrics/ClassLength
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.1.0
|
5
|
+
module RedisQueuedLocks::Utilities
|
6
|
+
module_function
|
7
|
+
|
8
|
+
# @param block [Block]
|
9
|
+
# @return [Any]
|
10
|
+
#
|
11
|
+
# @api private
|
12
|
+
# @since 0.1.0
|
13
|
+
def run_non_critical(&block)
|
14
|
+
yield rescue nil
|
15
|
+
end
|
16
|
+
end
|