redis_queued_locks 1.20.0 → 1.22.0
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/.claude/project-overview.md +249 -36
- data/.claude/rules/acquirer.md +16 -14
- data/.claude/rules/arguments.md +2 -2
- data/.claude/rules/changelog.md +27 -0
- data/.claude/rules/git.md +1 -1
- data/.claude/rules/logic.md +34 -13
- data/.claude/rules/swarm.md +4 -5
- data/.claude/rules/tests.md +9 -6
- data/.claude/rules/type-checking.md +13 -14
- data/.claude/rules/visitors.md +13 -14
- data/.claude/skills/rql-code-review/SKILL.md +134 -43
- data/CHANGELOG.md +105 -125
- data/CLAUDE.md +11 -9
- data/README.md +501 -136
- data/Rakefile +11 -0
- data/lib/redis_queued_locks/acquirer/acquire_lock/dequeue_from_lock_queue/log_visitor.rb +1 -0
- data/lib/redis_queued_locks/acquirer/acquire_lock/instr_visitor.rb +68 -10
- data/lib/redis_queued_locks/acquirer/acquire_lock/log_visitor.rb +82 -15
- data/lib/redis_queued_locks/acquirer/acquire_lock/try_to_lock/log_visitor.rb +92 -14
- data/lib/redis_queued_locks/acquirer/acquire_lock/try_to_lock.rb +441 -63
- data/lib/redis_queued_locks/acquirer/acquire_lock/with_acq_timeout.rb +2 -2
- data/lib/redis_queued_locks/acquirer/acquire_lock/yield_expire/log_visitor.rb +1 -0
- data/lib/redis_queued_locks/acquirer/acquire_lock/yield_expire.rb +5 -5
- data/lib/redis_queued_locks/acquirer/acquire_lock.rb +176 -39
- data/lib/redis_queued_locks/acquirer/clear_dead_requests.rb +322 -49
- data/lib/redis_queued_locks/acquirer/extend_lock_ttl.rb +125 -13
- data/lib/redis_queued_locks/acquirer/is_locked.rb +0 -1
- data/lib/redis_queued_locks/acquirer/is_queued.rb +0 -1
- data/lib/redis_queued_locks/acquirer/keys.rb +16 -3
- data/lib/redis_queued_locks/acquirer/lock_info.rb +31 -39
- data/lib/redis_queued_locks/acquirer/lock_series_poc/instr_visitor.rb +11 -5
- data/lib/redis_queued_locks/acquirer/lock_series_poc/log_visitor.rb +20 -10
- data/lib/redis_queued_locks/acquirer/lock_series_poc.rb +105 -36
- data/lib/redis_queued_locks/acquirer/locks.rb +32 -11
- data/lib/redis_queued_locks/acquirer/queue_info.rb +30 -8
- data/lib/redis_queued_locks/acquirer/queues.rb +26 -6
- data/lib/redis_queued_locks/acquirer/reindex_locks.rb +117 -4
- data/lib/redis_queued_locks/acquirer/release_all_locks.rb +37 -6
- data/lib/redis_queued_locks/acquirer/release_lock.rb +33 -7
- data/lib/redis_queued_locks/acquirer/release_locks_of.rb +45 -6
- data/lib/redis_queued_locks/acquirer/release_read_lock.rb +22 -3
- data/lib/redis_queued_locks/client.rb +140 -56
- data/lib/redis_queued_locks/config.rb +105 -14
- data/lib/redis_queued_locks/errors.rb +34 -4
- data/lib/redis_queued_locks/instrument/sampler.rb +4 -7
- data/lib/redis_queued_locks/logging/sampler.rb +4 -8
- data/lib/redis_queued_locks/resource.rb +137 -12
- data/lib/redis_queued_locks/swarm/flush_zombies.rb +1 -0
- data/lib/redis_queued_locks/swarm/redis_client_builder.rb +8 -4
- data/lib/redis_queued_locks/swarm/swarm_element/threaded.rb +8 -6
- data/lib/redis_queued_locks/swarm/zombie_info.rb +6 -2
- data/lib/redis_queued_locks/swarm.rb +86 -5
- data/lib/redis_queued_locks/utilities.rb +1 -1
- data/lib/redis_queued_locks/version.rb +2 -2
- data/rbs_collection.lock.yaml +4 -0
- data/rbs_collection.yaml +2 -1
- data/sig/manifest.yml +1 -0
- data/sig/redis_queued_locks/acquirer/acquire_lock/dequeue_from_lock_queue/log_visitor.rbs +2 -2
- data/sig/redis_queued_locks/acquirer/acquire_lock/dequeue_from_lock_queue.rbs +2 -2
- data/sig/redis_queued_locks/acquirer/acquire_lock/instr_visitor.rbs +25 -5
- data/sig/redis_queued_locks/acquirer/acquire_lock/log_visitor.rbs +32 -12
- data/sig/redis_queued_locks/acquirer/acquire_lock/try_to_lock/log_visitor.rbs +61 -40
- data/sig/redis_queued_locks/acquirer/acquire_lock/try_to_lock.rbs +59 -11
- data/sig/redis_queued_locks/acquirer/acquire_lock/{yield_with_expire → yield_expire}/log_visitor.rbs +4 -4
- data/sig/redis_queued_locks/acquirer/acquire_lock/yield_expire.rbs +4 -4
- data/sig/redis_queued_locks/acquirer/acquire_lock.rbs +6 -4
- data/sig/redis_queued_locks/acquirer/clear_dead_requests.rbs +23 -1
- data/sig/redis_queued_locks/acquirer/extend_lock_ttl.rbs +16 -3
- data/sig/redis_queued_locks/acquirer/queues.rbs +1 -1
- data/sig/redis_queued_locks/acquirer/reindex_locks.rbs +22 -1
- data/sig/redis_queued_locks/acquirer/release_lock.rbs +3 -2
- data/sig/redis_queued_locks/acquirer/release_read_lock.rbs +1 -1
- data/sig/redis_queued_locks/client.rbs +24 -10
- data/sig/redis_queued_locks/config.rbs +54 -1
- data/sig/redis_queued_locks/errors.rbs +5 -2
- data/sig/redis_queued_locks/instrument/sampler.rbs +3 -3
- data/sig/redis_queued_locks/instrument/void_notifier.rbs +3 -1
- data/sig/redis_queued_locks/logging/sampler.rbs +3 -3
- data/sig/redis_queued_locks/resource.rbs +23 -13
- data/sig/redis_queued_locks/swarm/swarm_element/threaded.rbs +1 -1
- data/sig/redis_queued_locks/utilities.rbs +2 -1
- data/sig/redis_queued_locks.rbs +7 -0
- data/sig/vendor/redis_client.rbs +1 -0
- metadata +4 -3
- /data/sig/redis_queued_locks/{acquier.rbs → acquirer.rbs} +0 -0
|
@@ -2,23 +2,40 @@
|
|
|
2
2
|
|
|
3
3
|
# @api private
|
|
4
4
|
# @since 1.0.0
|
|
5
|
-
# @version 1.
|
|
5
|
+
# @version 1.22.0
|
|
6
|
+
# rubocop:disable Metrics/ModuleLength
|
|
6
7
|
module RedisQueuedLocks::Acquirer::ClearDeadRequests
|
|
8
|
+
# @since 1.21.0
|
|
9
|
+
extend RedisQueuedLocks::Utilities
|
|
10
|
+
|
|
11
|
+
# rubocop:disable Metrics/ClassLength
|
|
7
12
|
class << self
|
|
8
13
|
# Drops dead lock requests (requests older than `dead_ttl`) from the lock queues (write and
|
|
9
|
-
# read):
|
|
10
|
-
# - the disabled lock index: the lock queues found in one SCAN pass,
|
|
11
|
-
#
|
|
14
|
+
# read; the requests of any priority: see `RedisQueuedLocks::Resource::LOCK_PRIORITY_BAND`):
|
|
15
|
+
# - the disabled lock index: the lock queues found in one SCAN pass, processed in batches of
|
|
16
|
+
# `scan_size` lock queues (batches are collected across SCAN pages);
|
|
12
17
|
# - the enabled lock index: the lock queues of the locks of the lock index: one ZSCAN pass
|
|
13
|
-
# over the lock names (the keyspace is never scanned),
|
|
14
|
-
#
|
|
15
|
-
#
|
|
18
|
+
# over the lock names (the keyspace is never scanned), processed in batches of `scan_size`
|
|
19
|
+
# lock names (both lock queues of each lock name); expired lock names are dropped from the
|
|
20
|
+
# lock index first;
|
|
21
|
+
# - the batch round trips (see `drop_dead_requests`): the lock requests of the batch queues
|
|
22
|
+
# are read, then the dead requests of each priority band that has them are dropped (two
|
|
23
|
+
# round trips, one without dead requests); `by_priority`: only the first lock request of
|
|
24
|
+
# each band is read (a walk over the bands of the batch queues: the most bands of a batch
|
|
25
|
+
# queue + 1 round trips, the dead requests are dropped on the way, see
|
|
26
|
+
# `drop_dead_requests_by_priority`);
|
|
27
|
+
# - the operation is reported with one log and one instrumentation event
|
|
28
|
+
# (`redis_queued_locks.clear_dead_requests`) with the time of the whole operation;
|
|
16
29
|
#
|
|
17
30
|
# @param redis_client [RedisClient]
|
|
18
31
|
# @param scan_size [Integer]
|
|
19
32
|
# SCAN count and the number of lock queues in one batch (the disabled lock index) or ZSCAN
|
|
20
33
|
# count and the number of lock names in one batch (the enabled lock index).
|
|
21
34
|
# @param dead_ttl [Integer] In milliseconds
|
|
35
|
+
# @param by_priority [Boolean]
|
|
36
|
+
# Read the first lock request of each priority band of the lock queues instead of all lock
|
|
37
|
+
# requests (more round trips: one per band of the batch queue with the most bands + 1; the
|
|
38
|
+
# transfer does not depend on the number of lock requests).
|
|
22
39
|
# @param lock_index_enabled [Boolean]
|
|
23
40
|
# Find lock queues via the lock index instead of the keyspace SCAN.
|
|
24
41
|
# @param logger [::Logger,#debug]
|
|
@@ -36,11 +53,13 @@ module RedisQueuedLocks::Acquirer::ClearDeadRequests
|
|
|
36
53
|
#
|
|
37
54
|
# @api private
|
|
38
55
|
# @since 1.0.0
|
|
39
|
-
# @version 1.
|
|
56
|
+
# @version 1.22.0
|
|
57
|
+
# rubocop:disable Metrics/MethodLength
|
|
40
58
|
def clear_dead_requests(
|
|
41
59
|
redis_client,
|
|
42
60
|
scan_size,
|
|
43
61
|
dead_ttl,
|
|
62
|
+
by_priority,
|
|
44
63
|
lock_index_enabled,
|
|
45
64
|
logger,
|
|
46
65
|
instrumenter,
|
|
@@ -53,11 +72,94 @@ module RedisQueuedLocks::Acquirer::ClearDeadRequests
|
|
|
53
72
|
instr_sampling_percent,
|
|
54
73
|
instr_sampler,
|
|
55
74
|
instr_sample_this
|
|
75
|
+
)
|
|
76
|
+
# NOTE: (arguments) the same ranges as the config keys: zero/negative sizes fail SCAN/ZSCAN
|
|
77
|
+
# (`COUNT 0`) and the batching, a non-positive dead ttl drops all lock requests;
|
|
78
|
+
if !scan_size.is_a?(::Integer) || scan_size <= 0
|
|
79
|
+
raise(
|
|
80
|
+
RedisQueuedLocks::ArgumentError,
|
|
81
|
+
"`:scan_size` argument should be a positive Integer, got #{scan_size.inspect}."
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
if !dead_ttl.is_a?(::Integer) || dead_ttl <= 0
|
|
85
|
+
raise(
|
|
86
|
+
RedisQueuedLocks::ArgumentError,
|
|
87
|
+
"`:dead_ttl` argument should be a positive Integer, got #{dead_ttl.inspect}."
|
|
88
|
+
)
|
|
89
|
+
end
|
|
90
|
+
if by_priority != true && by_priority != false
|
|
91
|
+
raise(
|
|
92
|
+
RedisQueuedLocks::ArgumentError,
|
|
93
|
+
"`:by_priority` argument should be a boolean, got #{by_priority.inspect}."
|
|
94
|
+
)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
rel_start_time = clock_gettime
|
|
98
|
+
processed_queues = fully_clear_dead_requests(
|
|
99
|
+
redis_client, scan_size, dead_ttl, by_priority, lock_index_enabled
|
|
100
|
+
)
|
|
101
|
+
time_at = Time.now.to_f
|
|
102
|
+
rel_end_time = clock_gettime
|
|
103
|
+
rel_time = ((rel_end_time - rel_start_time) / 1_000.0).ceil(2)
|
|
104
|
+
|
|
105
|
+
log_sampled = RedisQueuedLocks::Logging.should_log?(
|
|
106
|
+
log_sampling_enabled,
|
|
107
|
+
log_sample_this,
|
|
108
|
+
log_sampling_percent,
|
|
109
|
+
log_sampler
|
|
110
|
+
)
|
|
111
|
+
instr_sampled = RedisQueuedLocks::Instrument.should_instrument?(
|
|
112
|
+
instr_sampling_enabled,
|
|
113
|
+
instr_sample_this,
|
|
114
|
+
instr_sampling_percent,
|
|
115
|
+
instr_sampler
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
run_non_critical do
|
|
119
|
+
logger.debug do
|
|
120
|
+
"[redis_queued_locks.clear_dead_requests] " \
|
|
121
|
+
"dead_ttl => #{dead_ttl} " \
|
|
122
|
+
"prc_queue_cnt => #{processed_queues.size} " \
|
|
123
|
+
"rel_time => #{rel_time} " \
|
|
124
|
+
"at => #{time_at}"
|
|
125
|
+
end
|
|
126
|
+
end if log_sampled
|
|
127
|
+
|
|
128
|
+
run_non_critical do
|
|
129
|
+
instrumenter.notify('redis_queued_locks.clear_dead_requests', {
|
|
130
|
+
dead_ttl: dead_ttl,
|
|
131
|
+
prc_queue_cnt: processed_queues.size,
|
|
132
|
+
rel_time: rel_time,
|
|
133
|
+
at: time_at,
|
|
134
|
+
instrument: instrument
|
|
135
|
+
})
|
|
136
|
+
end if instr_sampled
|
|
137
|
+
|
|
138
|
+
{ ok: true, result: { processed_queues: } }
|
|
139
|
+
end
|
|
140
|
+
# rubocop:enable Metrics/MethodLength
|
|
141
|
+
|
|
142
|
+
private
|
|
143
|
+
|
|
144
|
+
# Drops dead lock requests from the lock queues (see `clear_dead_requests`).
|
|
145
|
+
#
|
|
146
|
+
# @param redis_client [RedisClient]
|
|
147
|
+
# @param scan_size [Integer]
|
|
148
|
+
# @param dead_ttl [Integer] In milliseconds
|
|
149
|
+
# @param by_priority [Boolean]
|
|
150
|
+
# @param lock_index_enabled [Boolean]
|
|
151
|
+
# @return [Set<String>] The processed lock queues (write and read lock request queues).
|
|
152
|
+
#
|
|
153
|
+
# @api private
|
|
154
|
+
# @since 1.21.0
|
|
155
|
+
# @version 1.22.0
|
|
156
|
+
# rubocop:disable Metrics/MethodLength
|
|
157
|
+
def fully_clear_dead_requests(
|
|
158
|
+
redis_client, scan_size, dead_ttl, by_priority, lock_index_enabled
|
|
56
159
|
)
|
|
57
160
|
dead_score = RedisQueuedLocks::Resource.acquirer_dead_score(dead_ttl / 1_000.0)
|
|
58
161
|
|
|
59
|
-
|
|
60
|
-
result = Set.new.tap do |processed_queues|
|
|
162
|
+
Set.new.tap do |processed_queues|
|
|
61
163
|
# @type var processed_queues: Set[String]
|
|
62
164
|
redis_client.with do |rconn|
|
|
63
165
|
if lock_index_enabled
|
|
@@ -70,12 +172,15 @@ module RedisQueuedLocks::Acquirer::ClearDeadRequests
|
|
|
70
172
|
|
|
71
173
|
# Step 2 (lock index): drop dead requests from the lock queues of the lock index
|
|
72
174
|
each_lock_name_batch(rconn, scan_size) do |lock_names|
|
|
73
|
-
drop_dead_requests_of_locks(
|
|
175
|
+
drop_dead_requests_of_locks(
|
|
176
|
+
rconn, lock_names, dead_score, by_priority, processed_queues
|
|
177
|
+
)
|
|
74
178
|
end
|
|
75
179
|
else
|
|
76
180
|
# Step 1 (keyspace): drop dead requests from the lock queues found in one SCAN pass
|
|
77
|
-
# NOTE: dead requests are dropped
|
|
78
|
-
#
|
|
181
|
+
# NOTE: dead requests are dropped per batch of `scan_size` queues (the round trips of
|
|
182
|
+
# `drop_dead_requests`; batches are collected across SCAN pages: lock queues can be
|
|
183
|
+
# rare among other keys);
|
|
79
184
|
# @type var lock_queues: Array[String]
|
|
80
185
|
lock_queues = []
|
|
81
186
|
|
|
@@ -84,19 +189,18 @@ module RedisQueuedLocks::Acquirer::ClearDeadRequests
|
|
|
84
189
|
lock_queues << lock_queue
|
|
85
190
|
next if lock_queues.size < scan_size
|
|
86
191
|
|
|
87
|
-
drop_dead_requests(rconn, lock_queues, dead_score)
|
|
192
|
+
drop_dead_requests(rconn, lock_queues, dead_score, by_priority)
|
|
88
193
|
lock_queues.clear
|
|
89
194
|
end
|
|
90
195
|
|
|
91
|
-
|
|
196
|
+
unless lock_queues.empty?
|
|
197
|
+
drop_dead_requests(rconn, lock_queues, dead_score, by_priority)
|
|
198
|
+
end
|
|
92
199
|
end
|
|
93
200
|
end
|
|
94
201
|
end
|
|
95
|
-
|
|
96
|
-
{ ok: true, result: { processed_queues: result } }
|
|
97
202
|
end
|
|
98
|
-
|
|
99
|
-
private
|
|
203
|
+
# rubocop:enable Metrics/MethodLength
|
|
100
204
|
|
|
101
205
|
# Write and read lock queues in one SCAN pass (the disabled lock index).
|
|
102
206
|
#
|
|
@@ -122,62 +226,226 @@ module RedisQueuedLocks::Acquirer::ClearDeadRequests
|
|
|
122
226
|
end
|
|
123
227
|
end
|
|
124
228
|
|
|
125
|
-
# Drops dead requests of the given lock queues
|
|
229
|
+
# Drops dead requests of the given lock queues (both lookups; `by_priority`: the first lock
|
|
230
|
+
# request of each priority band is read instead of all lock requests, see
|
|
231
|
+
# `drop_dead_requests_by_priority`):
|
|
232
|
+
# - the first round trip reads the lock requests of the lock queues;
|
|
233
|
+
# - the second round trip drops the dead requests of each priority band that has them: one
|
|
234
|
+
# `ZREMRANGEBYSCORE` per band from the lower edge of the band to its time barrier (see
|
|
235
|
+
# `RedisQueuedLocks::Resource::LOCK_PRIORITY_BAND`), so the number of commands depends on
|
|
236
|
+
# the bands of the queued requests, not on the priority range; no round trip without dead
|
|
237
|
+
# requests (an empty pipeline sends nothing);
|
|
238
|
+
# - the requests of a band are adjacent and ordered by the enqueue time (the bands are
|
|
239
|
+
# disjoint position ranges): the first request of a band decides whether the band has dead
|
|
240
|
+
# requests;
|
|
241
|
+
# - requests queued (or re-queued) between the round trips are never dropped: they are after
|
|
242
|
+
# the time barrier of their band;
|
|
243
|
+
# - (infinite positions) RQL never writes them (manual writes of the queues, see
|
|
244
|
+
# `RedisQueuedLocks::Resource.lock_request_priority`): the requests at `-inf` are dead and
|
|
245
|
+
# are dropped by their position (`-inf` .. `-inf`), the requests at `+inf` are never dead;
|
|
126
246
|
#
|
|
127
247
|
# @param redis_client [RedisClient]
|
|
128
248
|
# @param lock_queues [Array<String>]
|
|
129
|
-
# @param dead_score [Float]
|
|
130
|
-
# @
|
|
249
|
+
# @param dead_score [Float] Requests queued before this time are dead.
|
|
250
|
+
# @param by_priority [Boolean]
|
|
251
|
+
# @return [Array<Array<Array<String,Float>>>]
|
|
252
|
+
# The lock requests (acquirer id, position) of each lock queue before the removal (the first
|
|
253
|
+
# lock request with `by_priority`), in the order of `lock_queues` (an empty list for a missing
|
|
254
|
+
# lock queue).
|
|
131
255
|
#
|
|
132
256
|
# @api private
|
|
133
257
|
# @since 1.20.0
|
|
134
|
-
|
|
135
|
-
|
|
258
|
+
# @version 1.22.0
|
|
259
|
+
# rubocop:disable Metrics/MethodLength
|
|
260
|
+
def drop_dead_requests(redis_client, lock_queues, dead_score, by_priority)
|
|
261
|
+
return drop_dead_requests_by_priority(redis_client, lock_queues, dead_score) if by_priority
|
|
262
|
+
|
|
263
|
+
# NOTE: (pipeline layout) one ZRANGE WITHSCORES per lock queue (the `lock_queues` order)
|
|
264
|
+
# @type var queued_requests: Array[Array[[String, Float]]]
|
|
265
|
+
queued_requests = redis_client.pipelined do |pipeline|
|
|
136
266
|
lock_queues.each do |lock_queue|
|
|
137
|
-
pipeline.call('
|
|
267
|
+
pipeline.call('ZRANGE', lock_queue, '0', '-1', 'WITHSCORES')
|
|
138
268
|
end
|
|
139
269
|
end
|
|
270
|
+
|
|
271
|
+
redis_client.pipelined do |pipeline|
|
|
272
|
+
lock_queues.each_with_index do |lock_queue, index|
|
|
273
|
+
# @type var checked_band: Integer?
|
|
274
|
+
checked_band = nil
|
|
275
|
+
|
|
276
|
+
# NOTE: (infinite positions) the requests at `-inf` (manual writes: out of all bands, see
|
|
277
|
+
# `RedisQueuedLocks::Resource.lock_request_priority`) are dead: they are the first
|
|
278
|
+
# requests of the queue and are dropped by their position (no band removal covers them);
|
|
279
|
+
first_request = queued_requests[index].first
|
|
280
|
+
if first_request && first_request[1] == -Float::INFINITY
|
|
281
|
+
pipeline.call('ZREMRANGEBYSCORE', lock_queue, '-inf', '-inf')
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
queued_requests[index].each do |lock_request|
|
|
285
|
+
position = lock_request[1]
|
|
286
|
+
next if position == -Float::INFINITY
|
|
287
|
+
|
|
288
|
+
priority = RedisQueuedLocks::Resource.lock_request_priority(position)
|
|
289
|
+
next if priority == checked_band
|
|
290
|
+
|
|
291
|
+
# NOTE: the first request of the band: the band has dead requests when it is dead
|
|
292
|
+
checked_band = priority
|
|
293
|
+
band_dead_score =
|
|
294
|
+
RedisQueuedLocks::Resource.lock_priority_band_dead_score(dead_score, priority)
|
|
295
|
+
next if position > band_dead_score
|
|
296
|
+
|
|
297
|
+
pipeline.call(
|
|
298
|
+
'ZREMRANGEBYSCORE', lock_queue,
|
|
299
|
+
RedisQueuedLocks::Resource.lock_priority_band_floor(priority), band_dead_score
|
|
300
|
+
)
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
queued_requests
|
|
140
306
|
end
|
|
307
|
+
# rubocop:enable Metrics/MethodLength
|
|
141
308
|
|
|
142
|
-
# Drops dead requests of
|
|
143
|
-
# (the
|
|
309
|
+
# Drops dead requests of the given lock queues by the first lock request of each priority band
|
|
310
|
+
# (`by_priority`, both lookups): the lock requests of the lock queues are never read at once:
|
|
311
|
+
# - a walk over the bands of each lock queue: each round trip reads the first lock request of
|
|
312
|
+
# the next band of each walked lock queue (`ZRANGE <queue> <bound> +inf BYSCORE LIMIT 0 1
|
|
313
|
+
# WITHSCORES`, the first round trip from `-inf`; the next bound is the lower edge of the
|
|
314
|
+
# next, lower, band: `RedisQueuedLocks::Resource.lock_priority_band_floor(priority - 1)`),
|
|
315
|
+
# so the transfer depends on the number of bands, not on the number of lock requests;
|
|
316
|
+
# - the first request of a band decides whether the band has dead requests (see
|
|
317
|
+
# `drop_dead_requests`, the same removal commands): they are dropped by the next round trip
|
|
318
|
+
# (in the same pipeline after the reads: the removal never touches the next bands);
|
|
319
|
+
# - a lock queue is walked till no more lock requests are found, so the round trips of
|
|
320
|
+
# a batch are the most bands of a batch queue + 1 (the last read finds nothing), with or
|
|
321
|
+
# without dead requests;
|
|
322
|
+
# - requests queued (or re-queued) during the walk are never dropped: they are after the time
|
|
323
|
+
# barrier of their band;
|
|
324
|
+
# - (infinite positions) the requests at `-inf` are dead and are dropped by their position
|
|
325
|
+
# (the walk goes on after them: `(-inf`), the walk of a lock queue stops at a request at
|
|
326
|
+
# `+inf` (never dead, the last requests of the queue);
|
|
327
|
+
# - (no progress) a position out of the float precision of the band edges (manual writes far
|
|
328
|
+
# out of the priority range): the lower edge of the next band rounds to the position itself
|
|
329
|
+
# (Redis parses the bound to the same Float), so the walk goes on after the position (an
|
|
330
|
+
# exclusive bound) instead of reading the same request again;
|
|
331
|
+
#
|
|
332
|
+
# @param redis_client [RedisClient]
|
|
333
|
+
# @param lock_queues [Array<String>]
|
|
334
|
+
# @param dead_score [Float] Requests queued before this time are dead.
|
|
335
|
+
# @return [Array<Array<Array<String,Float>>>]
|
|
336
|
+
# The first lock request (acquirer id, position) of each lock queue before the removal, in the
|
|
337
|
+
# order of `lock_queues` (an empty list for a missing lock queue).
|
|
338
|
+
#
|
|
339
|
+
# @api private
|
|
340
|
+
# @since 1.22.0
|
|
341
|
+
# rubocop:disable Metrics/MethodLength
|
|
342
|
+
def drop_dead_requests_by_priority(redis_client, lock_queues, dead_score)
|
|
343
|
+
# NOTE: the walked lock queues (their indexes in `lock_queues`) and the lower bound of the
|
|
344
|
+
# next read of each lock queue
|
|
345
|
+
walked_queues = Array.new(lock_queues.size) { |index| index }
|
|
346
|
+
# @type var bounds: Array[String | Float]
|
|
347
|
+
bounds = Array.new(lock_queues.size, '-inf')
|
|
348
|
+
# @type var removals: Array[[String, String | Integer, String | Float]]
|
|
349
|
+
removals = []
|
|
350
|
+
# @type var first_requests: Array[Array[[String, Float]]]
|
|
351
|
+
first_requests = []
|
|
352
|
+
|
|
353
|
+
until walked_queues.empty?
|
|
354
|
+
# NOTE: (pipeline layout) one ZRANGE per walked lock queue (the `walked_queues` order),
|
|
355
|
+
# then the removals of the dead requests found by the previous round trip (not read)
|
|
356
|
+
# @type var band_heads: Array[Array[[String, Float]]]
|
|
357
|
+
band_heads = redis_client.pipelined do |pipeline|
|
|
358
|
+
walked_queues.each do |index|
|
|
359
|
+
pipeline.call(
|
|
360
|
+
'ZRANGE', lock_queues[index], bounds[index], '+inf',
|
|
361
|
+
'BYSCORE', 'LIMIT', '0', '1', 'WITHSCORES'
|
|
362
|
+
)
|
|
363
|
+
end
|
|
364
|
+
removals.each do |lock_queue, lowest_position, dead_position|
|
|
365
|
+
pipeline.call('ZREMRANGEBYSCORE', lock_queue, lowest_position, dead_position)
|
|
366
|
+
end
|
|
367
|
+
end
|
|
368
|
+
removals.clear
|
|
369
|
+
# NOTE: the first round trip reads all lock queues (in the `lock_queues` order, no removals)
|
|
370
|
+
first_requests = band_heads if first_requests.empty?
|
|
371
|
+
|
|
372
|
+
# NOTE: a lock queue with a found request is walked further (a removal of its band is sent
|
|
373
|
+
# with the next read of the lock queue: no removal is left after the walk)
|
|
374
|
+
# @type var next_queues: Array[Integer]
|
|
375
|
+
next_queues = []
|
|
376
|
+
walked_queues.each_with_index do |index, read_index|
|
|
377
|
+
band_head = band_heads[read_index].first
|
|
378
|
+
next unless band_head
|
|
379
|
+
|
|
380
|
+
position = band_head[1]
|
|
381
|
+
next if position == Float::INFINITY
|
|
382
|
+
|
|
383
|
+
lock_queue = lock_queues[index]
|
|
384
|
+
if position == -Float::INFINITY
|
|
385
|
+
removals << [lock_queue, '-inf', '-inf']
|
|
386
|
+
bounds[index] = '(-inf'
|
|
387
|
+
else
|
|
388
|
+
priority = RedisQueuedLocks::Resource.lock_request_priority(position)
|
|
389
|
+
band_dead_score =
|
|
390
|
+
RedisQueuedLocks::Resource.lock_priority_band_dead_score(dead_score, priority)
|
|
391
|
+
if position <= band_dead_score
|
|
392
|
+
removals << [
|
|
393
|
+
lock_queue,
|
|
394
|
+
RedisQueuedLocks::Resource.lock_priority_band_floor(priority),
|
|
395
|
+
band_dead_score
|
|
396
|
+
]
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
# NOTE: (no progress) see the method description
|
|
400
|
+
next_band_floor =
|
|
401
|
+
RedisQueuedLocks::Resource.lock_priority_band_floor(priority - 1).to_f
|
|
402
|
+
bounds[index] = (next_band_floor > position) ? next_band_floor : "(#{position}"
|
|
403
|
+
end
|
|
404
|
+
next_queues << index
|
|
405
|
+
end
|
|
406
|
+
walked_queues = next_queues
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
first_requests
|
|
410
|
+
end
|
|
411
|
+
# rubocop:enable Metrics/MethodLength
|
|
412
|
+
|
|
413
|
+
# Drops dead requests of both lock queues of the given lock names (the enabled lock index; the
|
|
414
|
+
# round trips of `drop_dead_requests`).
|
|
144
415
|
#
|
|
145
416
|
# @param redis_client [RedisClient]
|
|
146
417
|
# @param lock_names [Array<String>]
|
|
147
418
|
# @param dead_score [Float]
|
|
419
|
+
# @param by_priority [Boolean]
|
|
148
420
|
# @param processed_queues [Set<String>] Collects the existing (processed) lock queues.
|
|
149
421
|
# @return [void]
|
|
150
422
|
#
|
|
151
423
|
# @api private
|
|
152
424
|
# @since 1.20.0
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
pipeline.call('EXISTS', read_lock_queue)
|
|
164
|
-
pipeline.call('ZREMRANGEBYSCORE', read_lock_queue, '-inf', dead_score)
|
|
165
|
-
end
|
|
425
|
+
# @version 1.22.0
|
|
426
|
+
def drop_dead_requests_of_locks(
|
|
427
|
+
redis_client, lock_names, dead_score, by_priority, processed_queues
|
|
428
|
+
)
|
|
429
|
+
# NOTE: both lock queues of each lock name in the batch order (write, read)
|
|
430
|
+
# @type var lock_queues: Array[String]
|
|
431
|
+
lock_queues = []
|
|
432
|
+
lock_names.each do |lock_name|
|
|
433
|
+
lock_queues << RedisQueuedLocks::Resource.prepare_lock_queue(lock_name)
|
|
434
|
+
lock_queues << RedisQueuedLocks::Resource.prepare_read_lock_queue(lock_name)
|
|
166
435
|
end
|
|
167
436
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
processed_queues << RedisQueuedLocks::Resource.prepare_read_lock_queue(lock_name)
|
|
174
|
-
end
|
|
437
|
+
queued_requests = drop_dead_requests(redis_client, lock_queues, dead_score, by_priority)
|
|
438
|
+
|
|
439
|
+
# NOTE: a lock queue is processed when it exists (Redis never keeps empty sorted sets)
|
|
440
|
+
lock_queues.each_with_index do |lock_queue, index|
|
|
441
|
+
processed_queues << lock_queue unless queued_requests[index].empty?
|
|
175
442
|
end
|
|
176
443
|
end
|
|
177
444
|
|
|
178
445
|
# Iterates over the lock names of the lock index (one ZSCAN pass) in batches of `scan_size`
|
|
179
446
|
# lock names (batches are collected across ZSCAN pages). Expired lock names (not dropped from
|
|
180
|
-
# the index yet) are skipped: all their keys are expired
|
|
447
|
+
# the index yet) are skipped: all their keys are expired (`+inf` names, keys without TTL, never
|
|
448
|
+
# expire: see `RedisQueuedLocks::Resource.lock_index_score_expired?`).
|
|
181
449
|
#
|
|
182
450
|
# NOTE: each operation module iterates over the lock index by itself (duplicated on purpose:
|
|
183
451
|
# operation modules are independent of each other, see `Locks.each_lock_name_batch`);
|
|
@@ -190,6 +458,7 @@ module RedisQueuedLocks::Acquirer::ClearDeadRequests
|
|
|
190
458
|
#
|
|
191
459
|
# @api private
|
|
192
460
|
# @since 1.20.0
|
|
461
|
+
# @version 1.22.0
|
|
193
462
|
def each_lock_name_batch(redis_client, scan_size)
|
|
194
463
|
lock_index_time = RedisQueuedLocks::Resource.lock_index_time
|
|
195
464
|
# @type var lock_names: Array[String]
|
|
@@ -198,7 +467,9 @@ module RedisQueuedLocks::Acquirer::ClearDeadRequests
|
|
|
198
467
|
redis_client.zscan(
|
|
199
468
|
RedisQueuedLocks::Resource::LOCK_INDEX_KEY, count: scan_size
|
|
200
469
|
) do |lock_name, lock_index_score|
|
|
201
|
-
next if
|
|
470
|
+
next if RedisQueuedLocks::Resource.lock_index_score_expired?(
|
|
471
|
+
lock_index_score, lock_index_time
|
|
472
|
+
)
|
|
202
473
|
|
|
203
474
|
lock_names << lock_name
|
|
204
475
|
next if lock_names.size < scan_size
|
|
@@ -210,4 +481,6 @@ module RedisQueuedLocks::Acquirer::ClearDeadRequests
|
|
|
210
481
|
yield(lock_names) unless lock_names.empty?
|
|
211
482
|
end
|
|
212
483
|
end
|
|
484
|
+
# rubocop:enable Metrics/ClassLength
|
|
213
485
|
end
|
|
486
|
+
# rubocop:enable Metrics/ModuleLength
|