redis_queued_locks 1.23.1 → 1.24.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 +70 -33
- data/.claude/rules/acquirer.md +6 -4
- data/.claude/rules/arguments.md +2 -2
- data/.claude/rules/git.md +1 -1
- data/.claude/rules/logic.md +5 -4
- data/.claude/rules/readme.md +40 -0
- data/.claude/rules/swarm.md +2 -2
- data/.claude/rules/tests.md +3 -2
- data/.claude/rules/type-checking.md +3 -2
- data/.claude/rules/visitors.md +3 -2
- data/.claude/skills/rql-code-review/SKILL.md +6 -3
- data/.claude/skills/rql-project-full-review/SKILL.md +171 -0
- data/.claude/skills/rql-project-full-review/templates/report.en.md +41 -0
- data/.claude/skills/rql-project-full-review/templates/report.ru.md +41 -0
- data/CHANGELOG.md +25 -10
- data/CLAUDE.md +7 -5
- data/README.md +381 -13
- data/lib/redis_queued_locks/acquirer/acquire_lock/dequeue_from_lock_queue.rb +60 -1
- data/lib/redis_queued_locks/acquirer/acquire_lock/try_to_lock/log_visitor.rb +38 -1
- data/lib/redis_queued_locks/acquirer/acquire_lock/try_to_lock.rb +31 -4
- data/lib/redis_queued_locks/acquirer/acquire_lock/with_acq_timeout.rb +6 -3
- data/lib/redis_queued_locks/acquirer/acquire_lock/yield_expire.rb +243 -35
- data/lib/redis_queued_locks/acquirer/acquire_lock.rb +93 -3
- data/lib/redis_queued_locks/acquirer/lock_series/try_to_lock_series.rb +27 -1
- data/lib/redis_queued_locks/acquirer/lock_series.rb +150 -14
- data/lib/redis_queued_locks/client.rb +96 -16
- data/lib/redis_queued_locks/config/dsl.rb +2 -0
- data/lib/redis_queued_locks/config.rb +35 -11
- data/lib/redis_queued_locks/errors.rb +11 -2
- data/lib/redis_queued_locks/lock_keeper/instr_visitor.rb +173 -0
- data/lib/redis_queued_locks/lock_keeper/lease.rb +434 -0
- data/lib/redis_queued_locks/lock_keeper/lock_lease.rb +106 -0
- data/lib/redis_queued_locks/lock_keeper/log_visitor.rb +187 -0
- data/lib/redis_queued_locks/lock_keeper.rb +1007 -0
- data/lib/redis_queued_locks/resource.rb +1 -1
- data/lib/redis_queued_locks/swarm/swarm_element/threaded.rb +2 -2
- data/lib/redis_queued_locks/utilities.rb +1 -15
- data/lib/redis_queued_locks/version.rb +2 -2
- data/lib/redis_queued_locks.rb +1 -0
- data/sig/redis_queued_locks/acquirer/acquire_lock/dequeue_from_lock_queue.rbs +14 -0
- data/sig/redis_queued_locks/acquirer/acquire_lock/try_to_lock/log_visitor.rbs +12 -0
- data/sig/redis_queued_locks/acquirer/acquire_lock/try_to_lock.rbs +2 -0
- data/sig/redis_queued_locks/acquirer/acquire_lock/yield_expire.rbs +30 -2
- data/sig/redis_queued_locks/acquirer/acquire_lock.rbs +6 -1
- data/sig/redis_queued_locks/acquirer/lock_series.rbs +12 -1
- data/sig/redis_queued_locks/client.rbs +14 -0
- data/sig/redis_queued_locks/config.rbs +15 -1
- data/sig/redis_queued_locks/errors.rbs +6 -0
- data/sig/redis_queued_locks/instrument/void_notifier.rbs +2 -2
- data/sig/redis_queued_locks/lock_keeper/instr_visitor.rbs +67 -0
- data/sig/redis_queued_locks/lock_keeper/lease.rbs +78 -0
- data/sig/redis_queued_locks/lock_keeper/lock_lease.rbs +19 -0
- data/sig/redis_queued_locks/lock_keeper/log_visitor.rbs +62 -0
- data/sig/redis_queued_locks/lock_keeper.rbs +108 -0
- data/sig/redis_queued_locks/utilities.rbs +0 -2
- data/sig/vendor/redis_client.rbs +1 -0
- metadata +15 -1
|
@@ -0,0 +1,1007 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Keeps the locks of the running blocks of code alive (the `auto_extend: true` option of `#lock`
|
|
4
|
+
# and `#lock_series`): one background thread per client extends the locks of all blocks while they
|
|
5
|
+
# are running, so the lock ttl is a lease: the crash of the process releases the lock by the ttl,
|
|
6
|
+
# whatever the duration of the block of code is:
|
|
7
|
+
# - the thread and its Redis connection are created on demand and stop after the idle timeout
|
|
8
|
+
# without new leases (`config['lock_keeper.idle_timeout']`);
|
|
9
|
+
# - the thread has its own connection (from the config of the client): the client can be a plain
|
|
10
|
+
# `RedisClient` (one connection), it can not be shared with the threads of the application;
|
|
11
|
+
# - an extension checks the owner of the lock, never revives a lost lock and never shortens the
|
|
12
|
+
# lock: the lock lives the extension ttl from the moment of the extension, it is not a sum (the
|
|
13
|
+
# lifetime after a crash does not depend on the number of extensions);
|
|
14
|
+
# - an extension is a Lua script: the owner check and all commands of a lock are one atomic step
|
|
15
|
+
# in one round trip of the whole tick (mutual exclusion stays with `WATCH`/`MULTI` of the
|
|
16
|
+
# acquirement: an extension changes the ttl of an already held lock only);
|
|
17
|
+
# - the lock of a stuck block is not kept forever: see `max_hold_time_during_auto_extend` (the
|
|
18
|
+
# extension just stops, nothing depends on the client: the lock expires by itself);
|
|
19
|
+
# - the block of code can move its hold limit while it makes progress
|
|
20
|
+
# (`RedisQueuedLocks::LockKeeper::LockLease#hold_for`, see `#hold_for`); this thread interrupts
|
|
21
|
+
# a `timed` block of code at the hold limit (a `Timeout` can not be moved): `Thread#raise` in
|
|
22
|
+
# a blocking fiber, the fiber scheduler in a non-blocking one (see `#interrupt_timed_block`);
|
|
23
|
+
# - the leases are planned in a timer wheel (`config['lock_keeper.schedule_slot']`): a wake-up
|
|
24
|
+
# takes the due slots only and `#keep`/`#forget` change one slot, so the work under the lock
|
|
25
|
+
# does not grow with the number of running blocks of code;
|
|
26
|
+
# - `fork`: the thread does not survive and the locks of the parent process are not the locks of
|
|
27
|
+
# the child: the first lease of the child process starts with an empty state;
|
|
28
|
+
# - the settings of the keeper are `config['lock_keeper.*']` of the client (read when the
|
|
29
|
+
# thread starts, see `#load_settings`);
|
|
30
|
+
# - a thread of the same process has the same limits as the block of code: see README "Lock auto
|
|
31
|
+
# extension" (the Global VM Lock, the garbage collector);
|
|
32
|
+
#
|
|
33
|
+
# @api private
|
|
34
|
+
# @since 1.24.0
|
|
35
|
+
# rubocop:disable Metrics/ClassLength
|
|
36
|
+
class RedisQueuedLocks::LockKeeper
|
|
37
|
+
require_relative 'lock_keeper/log_visitor'
|
|
38
|
+
require_relative 'lock_keeper/instr_visitor'
|
|
39
|
+
require_relative 'lock_keeper/lease'
|
|
40
|
+
require_relative 'lock_keeper/lock_lease'
|
|
41
|
+
|
|
42
|
+
# @since 1.24.0
|
|
43
|
+
include RedisQueuedLocks::Utilities
|
|
44
|
+
|
|
45
|
+
# Extends the write lock of the acquirer (`EXTEND_LOCK_LUA_LOST_LOCK_REPLY` if the lock is lost,
|
|
46
|
+
# the remaining ttl otherwise):
|
|
47
|
+
# - `GT`: the extension never shortens the lock (the ttl of a reentrant lock extension);
|
|
48
|
+
# - (lock index) the score is the absolute upper bound of the lock lifetime (`XX`: a lock
|
|
49
|
+
# without an index entry is not indexed, `GT`: the score never decreases); `KEYS[2]` is sent
|
|
50
|
+
# with the enabled lock index only;
|
|
51
|
+
#
|
|
52
|
+
# @return [String]
|
|
53
|
+
#
|
|
54
|
+
# @api private
|
|
55
|
+
# @since 1.24.0
|
|
56
|
+
EXTEND_WRITE_LOCK = <<~LUA_SCRIPT.strip.tr("\n", '').freeze
|
|
57
|
+
if redis.call("HGET", KEYS[1], "acq_id") ~= ARGV[1] then return -3 end;
|
|
58
|
+
redis.call("PEXPIRE", KEYS[1], ARGV[2], "GT");
|
|
59
|
+
if KEYS[2] then redis.call("ZADD", KEYS[2], "XX", "GT", ARGV[3], ARGV[4]) end;
|
|
60
|
+
return redis.call("PTTL", KEYS[1]);
|
|
61
|
+
LUA_SCRIPT
|
|
62
|
+
|
|
63
|
+
# Extends the read lock of the acquirer (`EXTEND_LOCK_LUA_LOST_LOCK_REPLY` if the lock is lost,
|
|
64
|
+
# the remaining ttl otherwise):
|
|
65
|
+
# - an expired read lock is never "revived": a writer could obtain the expired lock;
|
|
66
|
+
# - the registry and the read lock data keep the longest ttl (`PEXPIRE NX` + `GT`);
|
|
67
|
+
# - (lock index) see `EXTEND_WRITE_LOCK`, `KEYS[4]`;
|
|
68
|
+
#
|
|
69
|
+
# @return [String]
|
|
70
|
+
#
|
|
71
|
+
# @api private
|
|
72
|
+
# @since 1.24.0
|
|
73
|
+
EXTEND_READ_LOCK = <<~LUA_SCRIPT.strip.tr("\n", '').freeze
|
|
74
|
+
if redis.call("EXISTS", KEYS[1]) == 1 then return -3 end;
|
|
75
|
+
local redis_time = redis.call("TIME");
|
|
76
|
+
local now = redis_time[1] * 1000 + math.floor(redis_time[2] / 1000);
|
|
77
|
+
local expiration = redis.call("ZSCORE", KEYS[2], ARGV[1]);
|
|
78
|
+
if (not expiration) or tonumber(expiration) <= now then return -3 end;
|
|
79
|
+
redis.call("ZADD", KEYS[2], "XX", "GT", now + ARGV[2], ARGV[1]);
|
|
80
|
+
redis.call("PEXPIRE", KEYS[2], ARGV[2], "NX");
|
|
81
|
+
redis.call("PEXPIRE", KEYS[2], ARGV[2], "GT");
|
|
82
|
+
redis.call("PEXPIRE", KEYS[3], ARGV[2], "NX");
|
|
83
|
+
redis.call("PEXPIRE", KEYS[3], ARGV[2], "GT");
|
|
84
|
+
if KEYS[4] then redis.call("ZADD", KEYS[4], "XX", "GT", ARGV[3], ARGV[4]) end;
|
|
85
|
+
return redis.call("ZSCORE", KEYS[2], ARGV[1]) - now;
|
|
86
|
+
LUA_SCRIPT
|
|
87
|
+
|
|
88
|
+
# The reply of the Lua scripts of the lock extension (`EXTEND_WRITE_LOCK`, `EXTEND_READ_LOCK`)
|
|
89
|
+
# for a lost lock: the lock is not owned anymore (not a `PTTL` reply: they return `-3` literally).
|
|
90
|
+
#
|
|
91
|
+
# @return [Integer]
|
|
92
|
+
#
|
|
93
|
+
# @api private
|
|
94
|
+
# @since 1.24.0
|
|
95
|
+
EXTEND_LOCK_LUA_LOST_LOCK_REPLY = -3
|
|
96
|
+
|
|
97
|
+
# @return [RedisQueuedLocks::Client]
|
|
98
|
+
#
|
|
99
|
+
# @api private
|
|
100
|
+
# @since 1.24.0
|
|
101
|
+
attr_reader :rql_client
|
|
102
|
+
|
|
103
|
+
# @return [RedisQueuedLocks::Utilities::Lock]
|
|
104
|
+
#
|
|
105
|
+
# @api private
|
|
106
|
+
# @since 1.24.0
|
|
107
|
+
attr_reader :sync
|
|
108
|
+
|
|
109
|
+
# @return [Thread,NilClass]
|
|
110
|
+
#
|
|
111
|
+
# @api private
|
|
112
|
+
# @since 1.24.0
|
|
113
|
+
attr_reader :thread
|
|
114
|
+
|
|
115
|
+
# NOTE: (settings) `config['lock_keeper.*']` of the client, read when the keeper thread starts
|
|
116
|
+
# (`#load_settings`): a changed setting takes effect at the next start of the thread (after
|
|
117
|
+
# its idle timeout or in a new process); the schedule is rebuilt with the new slot length;
|
|
118
|
+
|
|
119
|
+
# @return [Integer]
|
|
120
|
+
# The keeper thread stops after this time without new leases (in milliseconds,
|
|
121
|
+
# `config['lock_keeper.idle_timeout']`).
|
|
122
|
+
#
|
|
123
|
+
# @api private
|
|
124
|
+
# @since 1.24.0
|
|
125
|
+
attr_reader :idle_timeout
|
|
126
|
+
|
|
127
|
+
# An idle keeper follows the rhythm of the extensions, but it does not wake up more often than
|
|
128
|
+
# this (in milliseconds, `config['lock_keeper.min_idle_wake_up_interval']`): a lease with a
|
|
129
|
+
# shorter interval just wakes it up by itself.
|
|
130
|
+
#
|
|
131
|
+
# @return [Integer]
|
|
132
|
+
#
|
|
133
|
+
# @api private
|
|
134
|
+
# @since 1.24.0
|
|
135
|
+
attr_reader :min_idle_wake_up_interval
|
|
136
|
+
|
|
137
|
+
# A failed extension is retried sooner: `extension_interval / extension_retry_ratio`
|
|
138
|
+
# (`config['lock_keeper.extension_retry_ratio']`).
|
|
139
|
+
#
|
|
140
|
+
# @return [Integer]
|
|
141
|
+
#
|
|
142
|
+
# @api private
|
|
143
|
+
# @since 1.24.0
|
|
144
|
+
attr_reader :extension_retry_ratio
|
|
145
|
+
|
|
146
|
+
# The leases that are due in `extension_interval / extension_batching_ratio` are extended with the
|
|
147
|
+
# due ones (`config['lock_keeper.extension_batching_ratio']`): one pipeline instead of a wake-up
|
|
148
|
+
# per lease.
|
|
149
|
+
#
|
|
150
|
+
# @return [Integer]
|
|
151
|
+
#
|
|
152
|
+
# @api private
|
|
153
|
+
# @since 1.24.0
|
|
154
|
+
attr_reader :extension_batching_ratio
|
|
155
|
+
|
|
156
|
+
# The schedule of the leases is a set of slots of this length (in milliseconds,
|
|
157
|
+
# `config['lock_keeper.schedule_slot']`) by the time of their next extension: a wake-up looks at
|
|
158
|
+
# the leases of the due slots only, not at all leases (the work under the lock does not grow with
|
|
159
|
+
# the number of the running blocks of code).
|
|
160
|
+
#
|
|
161
|
+
# @return [Integer]
|
|
162
|
+
#
|
|
163
|
+
# @api private
|
|
164
|
+
# @since 1.24.0
|
|
165
|
+
attr_reader :schedule_slot
|
|
166
|
+
|
|
167
|
+
# How long the fiber of a `timed` block of code waits for the interruption that is sent through
|
|
168
|
+
# its fiber scheduler when the block ends before it comes (in milliseconds,
|
|
169
|
+
# `config['lock_keeper.fiber_interruption_timeout']`, see `#forget`): the scheduler raises it at
|
|
170
|
+
# the next suspension point of the fiber, and a scheduler that does not raise it in this time is
|
|
171
|
+
# not waited for anymore.
|
|
172
|
+
#
|
|
173
|
+
# @return [Integer]
|
|
174
|
+
#
|
|
175
|
+
# @api private
|
|
176
|
+
# @since 1.24.0
|
|
177
|
+
attr_reader :fiber_interruption_timeout
|
|
178
|
+
|
|
179
|
+
# The step of this wait (in seconds, `Kernel#sleep`,
|
|
180
|
+
# `config['lock_keeper.fiber_interruption_wait_step']`): each step suspends the fiber, so its
|
|
181
|
+
# scheduler gets the control and raises the interruption.
|
|
182
|
+
#
|
|
183
|
+
# @return [Float,Integer]
|
|
184
|
+
#
|
|
185
|
+
# @api private
|
|
186
|
+
# @since 1.24.0
|
|
187
|
+
attr_reader :fiber_interruption_wait_step
|
|
188
|
+
|
|
189
|
+
# @param rql_client [RedisQueuedLocks::Client]
|
|
190
|
+
# The client of the keeper (its connection and its config).
|
|
191
|
+
# @return [void]
|
|
192
|
+
#
|
|
193
|
+
# @api private
|
|
194
|
+
# @since 1.24.0
|
|
195
|
+
def initialize(rql_client)
|
|
196
|
+
@rql_client = rql_client
|
|
197
|
+
@sync = RedisQueuedLocks::Utilities::Lock.new
|
|
198
|
+
@leases = {} #: Hash[Lease,bool]
|
|
199
|
+
# NOTE: the schedule (under the lock): the leases by the slot of their next extension and the
|
|
200
|
+
# sorted slots (the first one is the nearest);
|
|
201
|
+
@buckets = {} #: Hash[Integer,Hash[Lease,bool]]
|
|
202
|
+
@slot_keys = [] #: Array[Integer]
|
|
203
|
+
@wakeups = ::Thread::Queue.new
|
|
204
|
+
@wake_at = nil
|
|
205
|
+
@last_lease_at = 0
|
|
206
|
+
@thread = nil
|
|
207
|
+
@process_id = ::Process.pid
|
|
208
|
+
load_settings
|
|
209
|
+
@idle_wake_up_interval = idle_timeout
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Prepares the keeper before the lock is obtained: the thread and its connection have time to
|
|
213
|
+
# start before the first extension (a connection takes several round trips, a slow start under
|
|
214
|
+
# the load of the process eats the lifetime of the first lock).
|
|
215
|
+
#
|
|
216
|
+
# @return [void]
|
|
217
|
+
#
|
|
218
|
+
# @api private
|
|
219
|
+
# @since 1.24.0
|
|
220
|
+
def start
|
|
221
|
+
sync.synchronize { run_keeper_thread }
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Starts to keep the locks of the block of code alive.
|
|
225
|
+
#
|
|
226
|
+
# @param lock_keys [Array<String>]
|
|
227
|
+
# @param lock_readers_keys [Array<String>]
|
|
228
|
+
# @param read_lock_keys [Array<String,NilClass>]
|
|
229
|
+
# @param rw_modes [Array<Symbol>] The modes of the really held locks.
|
|
230
|
+
# @param lock_names [Array<String>]
|
|
231
|
+
# @param acquirer_id [String]
|
|
232
|
+
# @param host_id [String]
|
|
233
|
+
# @param try_start_time [Integer]
|
|
234
|
+
# The start of the attempt that obtained the locks (`Utilities#clock_gettime`, microseconds):
|
|
235
|
+
# the lock ttl and the hold limit start inside the attempt (the moment is unknown on the
|
|
236
|
+
# client), so all deadlines are counted from an earlier moment.
|
|
237
|
+
# @param ttl [Integer]
|
|
238
|
+
# The time the locks live from the attempt (in milliseconds): the ttl of the obtained (or
|
|
239
|
+
# extended) locks, `0` when the remaining ttl is unknown (a reentrant lock that is not extended
|
|
240
|
+
# by the attempt: the first extension comes at once).
|
|
241
|
+
# @param extension_ttl [Integer] How long the lock lives after an extension (in milliseconds).
|
|
242
|
+
# @param max_hold_time_during_auto_extend [Integer,NilClass]
|
|
243
|
+
# The hold limit (in milliseconds), `nil`: no limit.
|
|
244
|
+
# @param lock_index_enabled [Boolean]
|
|
245
|
+
# @param lock_index_grace_period [Integer]
|
|
246
|
+
# @param logger [::Logger,#debug]
|
|
247
|
+
# @param log_sampled [Boolean]
|
|
248
|
+
# @param instrumenter [#notify]
|
|
249
|
+
# @param instr_sampled [Boolean]
|
|
250
|
+
# @param instrument [NilClass,Any]
|
|
251
|
+
# @return [RedisQueuedLocks::LockKeeper::Lease]
|
|
252
|
+
#
|
|
253
|
+
# @api private
|
|
254
|
+
# @since 1.24.0
|
|
255
|
+
# rubocop:disable Metrics/MethodLength
|
|
256
|
+
def keep(
|
|
257
|
+
lock_keys,
|
|
258
|
+
lock_readers_keys,
|
|
259
|
+
read_lock_keys,
|
|
260
|
+
rw_modes,
|
|
261
|
+
lock_names,
|
|
262
|
+
acquirer_id,
|
|
263
|
+
host_id,
|
|
264
|
+
try_start_time,
|
|
265
|
+
ttl,
|
|
266
|
+
extension_ttl,
|
|
267
|
+
max_hold_time_during_auto_extend,
|
|
268
|
+
lock_index_enabled,
|
|
269
|
+
lock_index_grace_period,
|
|
270
|
+
logger,
|
|
271
|
+
log_sampled,
|
|
272
|
+
instrumenter,
|
|
273
|
+
instr_sampled,
|
|
274
|
+
instrument
|
|
275
|
+
)
|
|
276
|
+
lease = Lease.new(
|
|
277
|
+
lock_keys,
|
|
278
|
+
lock_readers_keys,
|
|
279
|
+
read_lock_keys,
|
|
280
|
+
rw_modes,
|
|
281
|
+
lock_names,
|
|
282
|
+
acquirer_id,
|
|
283
|
+
host_id,
|
|
284
|
+
try_start_time / 1_000,
|
|
285
|
+
ttl,
|
|
286
|
+
extension_ttl,
|
|
287
|
+
max_hold_time_during_auto_extend,
|
|
288
|
+
lock_index_enabled,
|
|
289
|
+
lock_index_grace_period,
|
|
290
|
+
logger,
|
|
291
|
+
log_sampled,
|
|
292
|
+
instrumenter,
|
|
293
|
+
instr_sampled,
|
|
294
|
+
instrument
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
sync.synchronize do
|
|
298
|
+
run_keeper_thread
|
|
299
|
+
@leases[lease] = true
|
|
300
|
+
schedule(lease)
|
|
301
|
+
@last_lease_at = monotonic_time
|
|
302
|
+
@idle_wake_up_interval = [lease.extension_interval, min_idle_wake_up_interval].max
|
|
303
|
+
# NOTE: the thread sleeps till the nearest extension (or till its idle wake-up): a later one
|
|
304
|
+
# needs no wake-up (no context switch per block of code);
|
|
305
|
+
wake_at = @wake_at
|
|
306
|
+
@wakeups << true if !wake_at || lease.next_extension_at < wake_at
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
lease
|
|
310
|
+
end
|
|
311
|
+
# rubocop:enable Metrics/MethodLength
|
|
312
|
+
|
|
313
|
+
# Allows the keeper to interrupt the `timed` block of code when a lock is lost or the hold limit
|
|
314
|
+
# is reached. It is called inside the `rescue` scope of the interruption
|
|
315
|
+
# (`YieldExpire#yield_with_timeout`) and `#forget` is the end of the interruption period: an
|
|
316
|
+
# internal error can not reach the application:
|
|
317
|
+
# - a lock that is already lost (or the reached hold limit) interrupts the block here (the
|
|
318
|
+
# block is not invoked);
|
|
319
|
+
# - a blocking fiber is interrupted with `Thread#raise`;
|
|
320
|
+
# - a non-blocking fiber (a fiber scheduler task, for example) can be suspended, and
|
|
321
|
+
# `Thread#raise` reaches the current fiber of the thread only (it would interrupt another
|
|
322
|
+
# task), `Fiber#raise` can not be called by another thread: such a block is interrupted
|
|
323
|
+
# through its scheduler (`Fiber::Scheduler#fiber_interrupt`, Ruby 4.0: it may be called by
|
|
324
|
+
# another thread); a scheduler without this hook (or no scheduler) can not interrupt it: its
|
|
325
|
+
# hold limit is a `Timeout`, a lost lock interrupts it only when the block finds it by itself
|
|
326
|
+
# (`#hold_for`);
|
|
327
|
+
#
|
|
328
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
329
|
+
# @return [void]
|
|
330
|
+
#
|
|
331
|
+
# @raise [RedisQueuedLocks::TimedLockIntermediateTimeoutError]
|
|
332
|
+
#
|
|
333
|
+
# @api private
|
|
334
|
+
# @since 1.24.0
|
|
335
|
+
def interrupt_timed_block(lease)
|
|
336
|
+
fiber = ::Fiber.current
|
|
337
|
+
fiber_scheduler = fiber.blocking? ? nil : ::Fiber.scheduler
|
|
338
|
+
|
|
339
|
+
sync.synchronize do
|
|
340
|
+
if lease.lost?
|
|
341
|
+
raise(RedisQueuedLocks::TimedLockIntermediateTimeoutError, 'A lock is already lost')
|
|
342
|
+
end
|
|
343
|
+
if lease.hold_stopped?
|
|
344
|
+
raise(RedisQueuedLocks::TimedLockIntermediateTimeoutError, 'The hold limit is reached')
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
lease.timed_fiber = fiber
|
|
348
|
+
if fiber.blocking?
|
|
349
|
+
lease.interrupted_thread = ::Thread.current
|
|
350
|
+
elsif fiber_scheduler.respond_to?(:fiber_interrupt)
|
|
351
|
+
lease.fiber_scheduler = fiber_scheduler
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
# Stops to keep the locks of the block of code (it is safe to call it several times). A running
|
|
357
|
+
# tick can still send its extension: the extension checks the owner of the lock.
|
|
358
|
+
#
|
|
359
|
+
# An interruption sent through the fiber scheduler can not be cancelled, and it comes at the next
|
|
360
|
+
# suspension point of the fiber: when the block of code ends before it comes, the fiber waits for
|
|
361
|
+
# it here (inside the `rescue` scope of the interruption, see `#fiber_interruption_timeout`), so
|
|
362
|
+
# it never reaches the code of the application after the block.
|
|
363
|
+
#
|
|
364
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
365
|
+
# @param raised_error [Exception,NilClass]
|
|
366
|
+
# The error that has stopped the `timed` block of code (an interruption that has already come).
|
|
367
|
+
# @return [void]
|
|
368
|
+
#
|
|
369
|
+
# @raise [RedisQueuedLocks::TimedLockIntermediateTimeoutError]
|
|
370
|
+
#
|
|
371
|
+
# @api private
|
|
372
|
+
# @since 1.24.0
|
|
373
|
+
def forget(lease, raised_error = nil)
|
|
374
|
+
pending_interruption = sync.synchronize do
|
|
375
|
+
unschedule(lease) if @leases.delete(lease)
|
|
376
|
+
# NOTE: the end of the interruption period: a kept lease never interrupts the code after the
|
|
377
|
+
# block (`#hold_for` of a lost lock returns `:lock_lost` then);
|
|
378
|
+
lease.timed_fiber = nil
|
|
379
|
+
lease.take_pending_interruption(raised_error)
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
wait_for_interruption if pending_interruption
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
# Moves the hold limit of the block of code (`RedisQueuedLocks::LockKeeper::LockLease#hold_for`):
|
|
386
|
+
# its locks are kept at least `hold_time` from now (the limit never moves back) and they are
|
|
387
|
+
# extended at once, in the thread of the caller:
|
|
388
|
+
# - at once: the last extensions before the limit are shortened to the limit, so a limit that is
|
|
389
|
+
# moved right before it could miss the next tick (a delayed keeper thread); `ok: true` means
|
|
390
|
+
# that all locks are still owned and extended;
|
|
391
|
+
# - with the connection of the client: the connection of the keeper belongs to its thread;
|
|
392
|
+
# - under the lock of the keeper: a stopped lease is never extended again, and a tick that has
|
|
393
|
+
# found the old limit reached does not stop the moved one (`#stop_hold` checks it again);
|
|
394
|
+
# - a lost lock is reported once (`auto_extended_lock_lost`) and interrupts a `timed` block of
|
|
395
|
+
# code (the error is raised here when the block of code itself calls it, in any fiber; a
|
|
396
|
+
# block that the keeper can not interrupt is interrupted here by an earlier loss too);
|
|
397
|
+
# - a dead keeper thread is revived (the next extensions are its work);
|
|
398
|
+
#
|
|
399
|
+
# @param redis [RedisClient] The connection of the client.
|
|
400
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
401
|
+
# @param hold_time [Integer] In milliseconds.
|
|
402
|
+
# @return [Hash<Symbol,Boolean|Symbol>] See `RedisQueuedLocks::LockKeeper::LockLease#hold_for`.
|
|
403
|
+
#
|
|
404
|
+
# @raise [RedisQueuedLocks::TimedLockIntermediateTimeoutError]
|
|
405
|
+
#
|
|
406
|
+
# @api private
|
|
407
|
+
# @since 1.24.0
|
|
408
|
+
# rubocop:disable Metrics/MethodLength
|
|
409
|
+
def hold_for(redis, lease, hold_time)
|
|
410
|
+
# NOTE: the time before the commands: the new limit is counted from an earlier moment;
|
|
411
|
+
time = monotonic_time
|
|
412
|
+
interrupts_caller = false
|
|
413
|
+
status = sync.synchronize do
|
|
414
|
+
if lease.lost?
|
|
415
|
+
# NOTE: a `timed` block of code that the keeper can not interrupt (a non-blocking fiber
|
|
416
|
+
# without an interrupting fiber scheduler) is interrupted when it finds the lost lock;
|
|
417
|
+
interrupts_caller = !lease.interruptible? && lease.timed_fiber.equal?(::Fiber.current)
|
|
418
|
+
:lock_lost
|
|
419
|
+
elsif lease.hold_stopped?
|
|
420
|
+
:hold_limit_reached
|
|
421
|
+
elsif @process_id != ::Process.pid || !@leases.key?(lease)
|
|
422
|
+
# NOTE: the block of code is completed (or it is a lease of the parent process: `fork`);
|
|
423
|
+
:released
|
|
424
|
+
else
|
|
425
|
+
run_keeper_thread
|
|
426
|
+
lease.hold_until(time + hold_time)
|
|
427
|
+
nil
|
|
428
|
+
end
|
|
429
|
+
end
|
|
430
|
+
if interrupts_caller
|
|
431
|
+
raise(RedisQueuedLocks::TimedLockIntermediateTimeoutError, 'A lock is already lost')
|
|
432
|
+
end
|
|
433
|
+
return { ok: false, result: status } if status
|
|
434
|
+
|
|
435
|
+
lock_indexes = extended_lock_indexes(lease)
|
|
436
|
+
# NOTE: (pipeline layout) one reply per lock of `lock_indexes`, in their order;
|
|
437
|
+
# @type var replies: Array[Integer]
|
|
438
|
+
replies = redis.pipelined do |pipeline|
|
|
439
|
+
pipeline_lease_extension(pipeline, lease, lock_indexes, time)
|
|
440
|
+
end
|
|
441
|
+
lock_lost = false
|
|
442
|
+
|
|
443
|
+
lock_indexes.each_with_index do |lock_index, reply_position|
|
|
444
|
+
reply = replies[reply_position]
|
|
445
|
+
|
|
446
|
+
if reply == EXTEND_LOCK_LUA_LOST_LOCK_REPLY
|
|
447
|
+
lock_lost = true
|
|
448
|
+
report_lost_lock(lease, lock_index, time)
|
|
449
|
+
else
|
|
450
|
+
report_held_lock(lease, lock_index, time, hold_time, reply)
|
|
451
|
+
end
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
lock_lost ? { ok: false, result: :lock_lost } : { ok: true, result: :extended }
|
|
455
|
+
end
|
|
456
|
+
# rubocop:enable Metrics/MethodLength
|
|
457
|
+
|
|
458
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
459
|
+
# @return [Integer,NilClass] The time left till the hold limit (in milliseconds), `nil`: no limit.
|
|
460
|
+
#
|
|
461
|
+
# @api private
|
|
462
|
+
# @since 1.24.0
|
|
463
|
+
def hold_time_left(lease)
|
|
464
|
+
lease.hold_time_left(monotonic_time)
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
private
|
|
468
|
+
|
|
469
|
+
# NOTE: (fork) only the thread that called `fork` survives: the leases of the parent process are
|
|
470
|
+
# dropped, the parent process extends its locks by itself (an extension from the child process
|
|
471
|
+
# would keep alive the locks of a dead parent process);
|
|
472
|
+
#
|
|
473
|
+
# @return [void]
|
|
474
|
+
#
|
|
475
|
+
# @api private
|
|
476
|
+
# @since 1.24.0
|
|
477
|
+
def run_keeper_thread
|
|
478
|
+
if @process_id != ::Process.pid
|
|
479
|
+
@process_id = ::Process.pid
|
|
480
|
+
@leases = {}
|
|
481
|
+
@buckets = {}
|
|
482
|
+
@slot_keys = []
|
|
483
|
+
@wake_at = nil
|
|
484
|
+
@thread = nil
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
return if @thread&.alive?
|
|
488
|
+
|
|
489
|
+
# NOTE: the client is built here (not in the thread): a client without the connection config
|
|
490
|
+
# fails in the lock logic, not silently in the background;
|
|
491
|
+
redis = rql_client.redis_client.config.new_client
|
|
492
|
+
# NOTE: a new thread takes the leases of a dead one over: a killed thread can leave the due
|
|
493
|
+
# leases of its tick out of the schedule (the registry keeps all leases); the slots are
|
|
494
|
+
# rebuilt with the new settings;
|
|
495
|
+
load_settings
|
|
496
|
+
rebuild_schedule
|
|
497
|
+
@last_lease_at = monotonic_time
|
|
498
|
+
thread = ::Thread.new do
|
|
499
|
+
# NOTE: a new thread inherits the interrupt mask of the current one (ruby/timeout#41);
|
|
500
|
+
# NOTE: (steep) `Thread.handle_interrupt` has no block in the RBS of the Ruby core;
|
|
501
|
+
::Thread.handle_interrupt(Object => :immediate) { keep_locks(redis) } # steep:ignore
|
|
502
|
+
end
|
|
503
|
+
thread.name = 'rql.lock_keeper'
|
|
504
|
+
@thread = thread
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
# @param redis [RedisClient]
|
|
508
|
+
# @return [void]
|
|
509
|
+
#
|
|
510
|
+
# @api private
|
|
511
|
+
# @since 1.24.0
|
|
512
|
+
def keep_locks(redis)
|
|
513
|
+
# NOTE: connect before the first extension (see `#start`); an unavailable Redis is reported by
|
|
514
|
+
# the extensions;
|
|
515
|
+
run_non_critical { redis.call('PING') }
|
|
516
|
+
while (leases = wait_for_due_leases)
|
|
517
|
+
extend_locks(redis, leases)
|
|
518
|
+
end
|
|
519
|
+
ensure
|
|
520
|
+
redis.close
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
# @return [Array<RedisQueuedLocks::LockKeeper::Lease>,NilClass]
|
|
524
|
+
# The leases that should be extended now, `nil` when the thread should stop (no leases).
|
|
525
|
+
#
|
|
526
|
+
# @api private
|
|
527
|
+
# @since 1.24.0
|
|
528
|
+
def wait_for_due_leases
|
|
529
|
+
loop do
|
|
530
|
+
wait_time = sync.synchronize do
|
|
531
|
+
now = monotonic_time
|
|
532
|
+
@wakeups.clear
|
|
533
|
+
next_extension_at = planned_extension_at
|
|
534
|
+
|
|
535
|
+
if next_extension_at == nil
|
|
536
|
+
# NOTE: (idle) counted from the last lease, not from the last lease seen here: a short
|
|
537
|
+
# block of code ends before this thread gets the GVL, and the leases are in use;
|
|
538
|
+
idle_time = now - @last_lease_at
|
|
539
|
+
|
|
540
|
+
if idle_time >= idle_timeout
|
|
541
|
+
# NOTE: under the lock: a new lease sees the stopped thread and starts a new one;
|
|
542
|
+
@thread = nil
|
|
543
|
+
return nil
|
|
544
|
+
end
|
|
545
|
+
|
|
546
|
+
# NOTE: (idle) the rhythm of the extensions: the next lease with the same ttl is due later
|
|
547
|
+
# than this wake-up, so a stream of short blocks does not wake the thread per block;
|
|
548
|
+
idle_wait = [idle_timeout - idle_time, @idle_wake_up_interval].min #: Integer
|
|
549
|
+
@wake_at = now + idle_wait
|
|
550
|
+
idle_wait
|
|
551
|
+
elsif next_extension_at > now
|
|
552
|
+
@wake_at = next_extension_at
|
|
553
|
+
next_extension_at - now
|
|
554
|
+
else
|
|
555
|
+
return take_due_leases(now)
|
|
556
|
+
end
|
|
557
|
+
end
|
|
558
|
+
|
|
559
|
+
@wakeups.pop(timeout: wait_time / 1_000.0)
|
|
560
|
+
end
|
|
561
|
+
end
|
|
562
|
+
|
|
563
|
+
# NOTE: (the schedule) all methods of the schedule are called under the lock (`sync`);
|
|
564
|
+
#
|
|
565
|
+
# @return [void]
|
|
566
|
+
#
|
|
567
|
+
# @api private
|
|
568
|
+
# @since 1.24.0
|
|
569
|
+
def rebuild_schedule
|
|
570
|
+
@buckets = {}
|
|
571
|
+
@slot_keys = []
|
|
572
|
+
@leases.each_key { |lease| schedule(lease) }
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
# The empty slots in front of the schedule are dropped here (see `#unschedule`).
|
|
576
|
+
#
|
|
577
|
+
# @return [Integer,NilClass] The nearest planned extension, `nil`: no leases.
|
|
578
|
+
#
|
|
579
|
+
# @api private
|
|
580
|
+
# @since 1.24.0
|
|
581
|
+
def planned_extension_at
|
|
582
|
+
while (slot = @slot_keys.first)
|
|
583
|
+
bucket = @buckets.fetch(slot)
|
|
584
|
+
return bucket.each_key.map(&:next_extension_at).min unless bucket.empty?
|
|
585
|
+
|
|
586
|
+
@slot_keys.shift
|
|
587
|
+
@buckets.delete(slot)
|
|
588
|
+
end
|
|
589
|
+
end
|
|
590
|
+
|
|
591
|
+
# Takes the due leases and the leases that are due in their batching interval out of the
|
|
592
|
+
# schedule (the slots till the batching interval of the nearest lease are looked at): one
|
|
593
|
+
# pipeline instead of a wake-up per lease. The tick puts them back (`#reschedule`).
|
|
594
|
+
#
|
|
595
|
+
# @param now [Integer]
|
|
596
|
+
# @return [Array<RedisQueuedLocks::LockKeeper::Lease>]
|
|
597
|
+
#
|
|
598
|
+
# @api private
|
|
599
|
+
# @since 1.24.0
|
|
600
|
+
def take_due_leases(now)
|
|
601
|
+
first_slot = @slot_keys.first #: Integer
|
|
602
|
+
nearest_lease = @buckets.fetch(first_slot).each_key.min_by(&:next_extension_at) #: Lease
|
|
603
|
+
horizon = now + (nearest_lease.extension_interval / extension_batching_ratio)
|
|
604
|
+
due_leases = [] #: Array[Lease]
|
|
605
|
+
later_leases = [] #: Array[Lease]
|
|
606
|
+
|
|
607
|
+
while (slot = @slot_keys.first) && slot * schedule_slot <= horizon
|
|
608
|
+
@slot_keys.shift
|
|
609
|
+
@buckets.delete(slot)&.each_key do |lease|
|
|
610
|
+
if lease.next_extension_at <= now + (lease.extension_interval / extension_batching_ratio)
|
|
611
|
+
due_leases << lease
|
|
612
|
+
else
|
|
613
|
+
later_leases << lease
|
|
614
|
+
end
|
|
615
|
+
end
|
|
616
|
+
end
|
|
617
|
+
|
|
618
|
+
later_leases.each { |lease| schedule(lease) }
|
|
619
|
+
due_leases
|
|
620
|
+
end
|
|
621
|
+
|
|
622
|
+
# Puts the leases of a tick back into the schedule (the leases of the completed blocks of code
|
|
623
|
+
# and the dropped ones are not in the registry anymore): one section for the whole tick.
|
|
624
|
+
#
|
|
625
|
+
# @param leases [Array<RedisQueuedLocks::LockKeeper::Lease>]
|
|
626
|
+
# @return [void]
|
|
627
|
+
#
|
|
628
|
+
# @api private
|
|
629
|
+
# @since 1.24.0
|
|
630
|
+
def reschedule(leases)
|
|
631
|
+
sync.synchronize do
|
|
632
|
+
leases.each { |lease| schedule(lease) if @leases.key?(lease) }
|
|
633
|
+
end
|
|
634
|
+
end
|
|
635
|
+
|
|
636
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
637
|
+
# @return [void]
|
|
638
|
+
#
|
|
639
|
+
# @api private
|
|
640
|
+
# @since 1.24.0
|
|
641
|
+
def schedule(lease)
|
|
642
|
+
slot = lease.next_extension_at / schedule_slot
|
|
643
|
+
bucket = @buckets[slot]
|
|
644
|
+
|
|
645
|
+
if bucket
|
|
646
|
+
bucket[lease] = true
|
|
647
|
+
else
|
|
648
|
+
@buckets[slot] = { lease => true }
|
|
649
|
+
slot_index = @slot_keys.bsearch_index { |slot_key| slot_key >= slot } || @slot_keys.size
|
|
650
|
+
@slot_keys.insert(slot_index, slot)
|
|
651
|
+
end
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
# NOTE: the slot is found by the planned extension: it changes only while the lease is out of
|
|
655
|
+
# the schedule (a due lease of a tick); a lease out of the schedule is not found (a no-op);
|
|
656
|
+
# an empty slot stays till its time (`#planned_extension_at` drops it): a stream of short
|
|
657
|
+
# blocks of code reuses it (no new slot per block under the lock);
|
|
658
|
+
#
|
|
659
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
660
|
+
# @return [void]
|
|
661
|
+
#
|
|
662
|
+
# @api private
|
|
663
|
+
# @since 1.24.0
|
|
664
|
+
def unschedule(lease)
|
|
665
|
+
@buckets[lease.next_extension_at / schedule_slot]&.delete(lease)
|
|
666
|
+
end
|
|
667
|
+
|
|
668
|
+
# @param redis [RedisClient]
|
|
669
|
+
# @param due_leases [Array<RedisQueuedLocks::LockKeeper::Lease>]
|
|
670
|
+
# @return [void]
|
|
671
|
+
#
|
|
672
|
+
# @api private
|
|
673
|
+
# @since 1.24.0
|
|
674
|
+
def extend_locks(redis, due_leases)
|
|
675
|
+
# NOTE: the time before the commands: all deadlines are counted from an earlier moment;
|
|
676
|
+
time = monotonic_time
|
|
677
|
+
stopped_leases, leases = due_leases.partition { |lease| lease.hold_limit_reached?(time) }
|
|
678
|
+
# NOTE: a limit that is moved after this check (`#hold_for`) keeps the lease: it is extended
|
|
679
|
+
# with the others (the due leases are out of the schedule till the end of the tick);
|
|
680
|
+
stopped_leases.each { |lease| leases << lease if stop_hold(lease, time) == :moved }
|
|
681
|
+
return if leases.empty?
|
|
682
|
+
|
|
683
|
+
# NOTE: the locks that are not lost yet: the thread of a block of code can lose a lock during
|
|
684
|
+
# the tick (`#hold_for`), so the replies are read by these indexes;
|
|
685
|
+
lock_indexes = leases.map { |lease| extended_lock_indexes(lease) }
|
|
686
|
+
|
|
687
|
+
begin
|
|
688
|
+
# NOTE: (pipeline layout) one reply per lock of `lock_indexes`, in the order of the leases;
|
|
689
|
+
# @type var replies: Array[Integer]
|
|
690
|
+
replies = redis.pipelined do |pipeline|
|
|
691
|
+
leases.each_with_index do |lease, lease_position|
|
|
692
|
+
pipeline_lease_extension(pipeline, lease, lock_indexes[lease_position], time)
|
|
693
|
+
end
|
|
694
|
+
end
|
|
695
|
+
rescue ::StandardError => error
|
|
696
|
+
# NOTE: the lock can still live: the next try comes sooner, a lost lock is found by it; the
|
|
697
|
+
# blocks of code that are completed during the tick have nothing to report;
|
|
698
|
+
kept_leases = sync.synchronize { leases.select { |lease| @leases.key?(lease) } }
|
|
699
|
+
kept_leases.each { |lease| report_failed_extension(lease, time, error) }
|
|
700
|
+
reschedule(leases)
|
|
701
|
+
return
|
|
702
|
+
end
|
|
703
|
+
|
|
704
|
+
process_extension_replies(leases, lock_indexes, replies, time)
|
|
705
|
+
reschedule(leases)
|
|
706
|
+
end
|
|
707
|
+
|
|
708
|
+
# @param leases [Array<RedisQueuedLocks::LockKeeper::Lease>]
|
|
709
|
+
# @param lock_indexes [Array<Array<Integer>>] The extended locks of each lease.
|
|
710
|
+
# @param replies [Array<Integer>] See the pipeline layout of `#extend_locks`.
|
|
711
|
+
# @param time [Integer]
|
|
712
|
+
# @return [void]
|
|
713
|
+
#
|
|
714
|
+
# @api private
|
|
715
|
+
# @since 1.24.0
|
|
716
|
+
def process_extension_replies(leases, lock_indexes, replies, time)
|
|
717
|
+
reply_position = 0
|
|
718
|
+
# NOTE: one section for the whole tick (not one per lease);
|
|
719
|
+
kept_leases = sync.synchronize { leases.map { |lease| @leases.key?(lease) } }
|
|
720
|
+
|
|
721
|
+
leases.each_with_index do |lease, lease_position|
|
|
722
|
+
lease.schedule_extension(time + lease.extension_interval)
|
|
723
|
+
lease_kept = kept_leases[lease_position]
|
|
724
|
+
|
|
725
|
+
lock_indexes[lease_position].each do |lock_index|
|
|
726
|
+
reply = replies[reply_position]
|
|
727
|
+
reply_position += 1
|
|
728
|
+
# NOTE: the block of code is completed and its lock is released during the tick, or the
|
|
729
|
+
# lock is found lost by the thread of the block meanwhile (`#hold_for`);
|
|
730
|
+
next if !lease_kept || lease.lock_lost?(lock_index)
|
|
731
|
+
|
|
732
|
+
if reply == EXTEND_LOCK_LUA_LOST_LOCK_REPLY
|
|
733
|
+
report_lost_lock(lease, lock_index, time)
|
|
734
|
+
else
|
|
735
|
+
report_extended_lock(lease, lock_index, time, reply)
|
|
736
|
+
end
|
|
737
|
+
end
|
|
738
|
+
end
|
|
739
|
+
end
|
|
740
|
+
|
|
741
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
742
|
+
# @return [Array<Integer>] The indexes of the locks that are not lost.
|
|
743
|
+
#
|
|
744
|
+
# @api private
|
|
745
|
+
# @since 1.24.0
|
|
746
|
+
def extended_lock_indexes(lease)
|
|
747
|
+
lease.lock_keys.each_index.reject { |lock_index| lease.lock_lost?(lock_index) }
|
|
748
|
+
end
|
|
749
|
+
|
|
750
|
+
# @param pipeline [RedisClient::Pipeline]
|
|
751
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
752
|
+
# @param lock_indexes [Array<Integer>] The locks to extend (one reply per lock, in this order).
|
|
753
|
+
# @param time [Integer]
|
|
754
|
+
# @return [void]
|
|
755
|
+
#
|
|
756
|
+
# @api private
|
|
757
|
+
# @since 1.24.0
|
|
758
|
+
# rubocop:disable Metrics/MethodLength
|
|
759
|
+
def pipeline_lease_extension(pipeline, lease, lock_indexes, time)
|
|
760
|
+
extension_ttl = lease.extension_ttl_at(time)
|
|
761
|
+
index_score =
|
|
762
|
+
if lease.lock_index_enabled
|
|
763
|
+
RedisQueuedLocks::Resource.lock_index_time + extension_ttl + lease.lock_index_grace_period
|
|
764
|
+
end
|
|
765
|
+
|
|
766
|
+
lock_indexes.each do |lock_index|
|
|
767
|
+
lock_key = lease.lock_keys[lock_index]
|
|
768
|
+
read_lock = lease.rw_modes[lock_index] == :read
|
|
769
|
+
|
|
770
|
+
if lease.lock_index_enabled && read_lock
|
|
771
|
+
pipeline.call(
|
|
772
|
+
'EVAL', EXTEND_READ_LOCK, 4,
|
|
773
|
+
lock_key, lease.lock_readers_keys[lock_index], lease.read_lock_keys[lock_index],
|
|
774
|
+
RedisQueuedLocks::Resource::LOCK_INDEX_KEY,
|
|
775
|
+
lease.acquirer_id, extension_ttl, index_score, lease.lock_names[lock_index]
|
|
776
|
+
)
|
|
777
|
+
elsif lease.lock_index_enabled
|
|
778
|
+
pipeline.call(
|
|
779
|
+
'EVAL', EXTEND_WRITE_LOCK, 2,
|
|
780
|
+
lock_key, RedisQueuedLocks::Resource::LOCK_INDEX_KEY,
|
|
781
|
+
lease.acquirer_id, extension_ttl, index_score, lease.lock_names[lock_index]
|
|
782
|
+
)
|
|
783
|
+
elsif read_lock
|
|
784
|
+
pipeline.call(
|
|
785
|
+
'EVAL', EXTEND_READ_LOCK, 3,
|
|
786
|
+
lock_key, lease.lock_readers_keys[lock_index], lease.read_lock_keys[lock_index],
|
|
787
|
+
lease.acquirer_id, extension_ttl
|
|
788
|
+
)
|
|
789
|
+
else
|
|
790
|
+
pipeline.call('EVAL', EXTEND_WRITE_LOCK, 1, lock_key, lease.acquirer_id, extension_ttl)
|
|
791
|
+
end
|
|
792
|
+
end
|
|
793
|
+
end
|
|
794
|
+
# rubocop:enable Metrics/MethodLength
|
|
795
|
+
|
|
796
|
+
# The hold limit is reached: the lock is not extended anymore (it expires by itself), a `timed`
|
|
797
|
+
# block of code is interrupted.
|
|
798
|
+
#
|
|
799
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
800
|
+
# @param time [Integer] The time of the tick.
|
|
801
|
+
# @return [Symbol]
|
|
802
|
+
# - `:stopped` - the hold limit is reached;
|
|
803
|
+
# - `:moved` - the limit is moved after the tick has found it reached (`#hold_for`);
|
|
804
|
+
# - `:released` - the block of code is completed during the tick;
|
|
805
|
+
#
|
|
806
|
+
# @api private
|
|
807
|
+
# @since 1.24.0
|
|
808
|
+
def stop_hold(lease, time)
|
|
809
|
+
# @type var status: Symbol
|
|
810
|
+
status = sync.synchronize do
|
|
811
|
+
next :released unless @leases.key?(lease)
|
|
812
|
+
next :moved unless lease.hold_limit_reached?(time)
|
|
813
|
+
|
|
814
|
+
@leases.delete(lease)
|
|
815
|
+
lease.stop_hold
|
|
816
|
+
# NOTE: under the lock: see `#report_lost_lock`;
|
|
817
|
+
lease.interrupt(
|
|
818
|
+
"The hold limit (#{lease.max_hold_time_during_auto_extend}) of the locks is reached"
|
|
819
|
+
)
|
|
820
|
+
:stopped
|
|
821
|
+
end
|
|
822
|
+
return status unless status == :stopped
|
|
823
|
+
|
|
824
|
+
lease.lock_keys.each_with_index do |lock_key, lock_index|
|
|
825
|
+
next if lease.lock_lost?(lock_index)
|
|
826
|
+
|
|
827
|
+
LogVisitor.lock_hold_limit_reached(
|
|
828
|
+
lease.logger, lease.log_sampled, lock_key, lease.rw_modes[lock_index],
|
|
829
|
+
lease.max_hold_time_during_auto_extend, lease.acquirer_id, lease.host_id
|
|
830
|
+
)
|
|
831
|
+
InstrVisitor.lock_hold_limit_reached(
|
|
832
|
+
lease.instrumenter, lease.instr_sampled, lock_key, lease.rw_modes[lock_index],
|
|
833
|
+
lease.max_hold_time_during_auto_extend, lease.acquirer_id, lease.host_id, lease.instrument
|
|
834
|
+
)
|
|
835
|
+
end
|
|
836
|
+
status
|
|
837
|
+
end
|
|
838
|
+
|
|
839
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
840
|
+
# @param lock_index [Integer]
|
|
841
|
+
# @param time [Integer]
|
|
842
|
+
# @param rem_ttl [Integer]
|
|
843
|
+
# @return [void]
|
|
844
|
+
#
|
|
845
|
+
# @api private
|
|
846
|
+
# @since 1.24.0
|
|
847
|
+
def report_extended_lock(lease, lock_index, time, rem_ttl)
|
|
848
|
+
LogVisitor.lock_auto_extended(
|
|
849
|
+
lease.logger, lease.log_sampled, lease.lock_keys[lock_index], lease.rw_modes[lock_index],
|
|
850
|
+
lease.extension_ttl_at(time), rem_ttl, lease.acquirer_id, lease.host_id
|
|
851
|
+
)
|
|
852
|
+
InstrVisitor.lock_auto_extended(
|
|
853
|
+
lease.instrumenter, lease.instr_sampled, lease.lock_keys[lock_index],
|
|
854
|
+
lease.rw_modes[lock_index], lease.extension_ttl_at(time), rem_ttl, lease.acquirer_id,
|
|
855
|
+
lease.host_id, lease.instrument
|
|
856
|
+
)
|
|
857
|
+
end
|
|
858
|
+
|
|
859
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
860
|
+
# @param time [Integer]
|
|
861
|
+
# @param error [StandardError]
|
|
862
|
+
# @return [void]
|
|
863
|
+
#
|
|
864
|
+
# @api private
|
|
865
|
+
# @since 1.24.0
|
|
866
|
+
def report_failed_extension(lease, time, error)
|
|
867
|
+
lease.schedule_extension(
|
|
868
|
+
time + [lease.extension_interval / extension_retry_ratio, 1].max
|
|
869
|
+
)
|
|
870
|
+
|
|
871
|
+
lease.lock_keys.each_with_index do |lock_key, lock_index|
|
|
872
|
+
next if lease.lock_lost?(lock_index)
|
|
873
|
+
|
|
874
|
+
LogVisitor.lock_auto_extension_failed(
|
|
875
|
+
lease.logger, lease.log_sampled, lock_key, lease.rw_modes[lock_index],
|
|
876
|
+
lease.extension_ttl_at(time), lease.acquirer_id, lease.host_id, error.class.name.to_s
|
|
877
|
+
)
|
|
878
|
+
InstrVisitor.lock_auto_extension_failed(
|
|
879
|
+
lease.instrumenter, lease.instr_sampled, lock_key, lease.rw_modes[lock_index],
|
|
880
|
+
lease.extension_ttl_at(time), lease.acquirer_id, lease.host_id, error.class.name.to_s,
|
|
881
|
+
lease.instrument
|
|
882
|
+
)
|
|
883
|
+
end
|
|
884
|
+
end
|
|
885
|
+
|
|
886
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
887
|
+
# @param lock_index [Integer]
|
|
888
|
+
# @param time [Integer]
|
|
889
|
+
# @return [void]
|
|
890
|
+
#
|
|
891
|
+
# @api private
|
|
892
|
+
# @since 1.24.0
|
|
893
|
+
# rubocop:disable Metrics/MethodLength
|
|
894
|
+
def report_lost_lock(lease, lock_index, time)
|
|
895
|
+
interrupts_caller = false
|
|
896
|
+
# NOTE: under the lock: `#forget` returns only when the block can not be interrupted anymore
|
|
897
|
+
# (an interruption after the block of code would reach the code of the application);
|
|
898
|
+
is_lost = sync.synchronize do
|
|
899
|
+
# NOTE: a lock is lost once: the keeper thread and the thread of the block (`#hold_for`) can
|
|
900
|
+
# find it lost at the same time;
|
|
901
|
+
next false if !@leases.key?(lease) || lease.lock_lost?(lock_index)
|
|
902
|
+
|
|
903
|
+
lease.lose_lock(lock_index)
|
|
904
|
+
|
|
905
|
+
# NOTE: a dropped lease is unscheduled too: `#hold_for` can find a lock lost while the lease
|
|
906
|
+
# waits in the schedule (a due lease of a tick is not in it: a no-op);
|
|
907
|
+
if lease.timed_fiber.equal?(::Fiber.current) || lease.interrupted_thread == ::Thread.current
|
|
908
|
+
# NOTE: `#hold_for` of the `timed` block of code itself (in any fiber, a fiber without an
|
|
909
|
+
# interrupting scheduler too): the error is raised after the report (the block is
|
|
910
|
+
# running: no race with `#forget`);
|
|
911
|
+
unschedule(lease) if @leases.delete(lease)
|
|
912
|
+
interrupts_caller = true
|
|
913
|
+
elsif lease.interruptible?
|
|
914
|
+
unschedule(lease) if @leases.delete(lease)
|
|
915
|
+
lease.interrupt("The lock \"#{lease.lock_keys[lock_index]}\" is lost")
|
|
916
|
+
elsif lease.all_locks_lost?
|
|
917
|
+
unschedule(lease) if @leases.delete(lease)
|
|
918
|
+
end
|
|
919
|
+
|
|
920
|
+
true
|
|
921
|
+
end
|
|
922
|
+
return unless is_lost
|
|
923
|
+
|
|
924
|
+
LogVisitor.auto_extended_lock_lost(
|
|
925
|
+
lease.logger, lease.log_sampled, lease.lock_keys[lock_index], lease.rw_modes[lock_index],
|
|
926
|
+
lease.acquirer_id, lease.host_id, time - lease.started_at
|
|
927
|
+
)
|
|
928
|
+
InstrVisitor.auto_extended_lock_lost(
|
|
929
|
+
lease.instrumenter, lease.instr_sampled, lease.lock_keys[lock_index],
|
|
930
|
+
lease.rw_modes[lock_index], lease.acquirer_id, lease.host_id, time - lease.started_at,
|
|
931
|
+
lease.instrument
|
|
932
|
+
)
|
|
933
|
+
return unless interrupts_caller
|
|
934
|
+
|
|
935
|
+
raise(
|
|
936
|
+
RedisQueuedLocks::TimedLockIntermediateTimeoutError,
|
|
937
|
+
"The lock \"#{lease.lock_keys[lock_index]}\" is lost"
|
|
938
|
+
)
|
|
939
|
+
end
|
|
940
|
+
# rubocop:enable Metrics/MethodLength
|
|
941
|
+
|
|
942
|
+
# @param lease [RedisQueuedLocks::LockKeeper::Lease]
|
|
943
|
+
# @param lock_index [Integer]
|
|
944
|
+
# @param time [Integer]
|
|
945
|
+
# @param hold_time [Integer] The requested time (in milliseconds).
|
|
946
|
+
# @param rem_ttl [Integer]
|
|
947
|
+
# @return [void]
|
|
948
|
+
#
|
|
949
|
+
# @api private
|
|
950
|
+
# @since 1.24.0
|
|
951
|
+
def report_held_lock(lease, lock_index, time, hold_time, rem_ttl)
|
|
952
|
+
LogVisitor.lock_hold_limit_extended(
|
|
953
|
+
lease.logger, lease.log_sampled, lease.lock_keys[lock_index], lease.rw_modes[lock_index],
|
|
954
|
+
hold_time, lease.hold_time_left(time), lease.extension_ttl_at(time), rem_ttl,
|
|
955
|
+
lease.acquirer_id, lease.host_id
|
|
956
|
+
)
|
|
957
|
+
InstrVisitor.lock_hold_limit_extended(
|
|
958
|
+
lease.instrumenter, lease.instr_sampled, lease.lock_keys[lock_index],
|
|
959
|
+
lease.rw_modes[lock_index], hold_time, lease.hold_time_left(time),
|
|
960
|
+
lease.extension_ttl_at(time), rem_ttl, lease.acquirer_id, lease.host_id, lease.instrument
|
|
961
|
+
)
|
|
962
|
+
end
|
|
963
|
+
|
|
964
|
+
# Waits in the fiber of the `timed` block of code for the interruption that is sent through its
|
|
965
|
+
# fiber scheduler (see `#forget`): the interruption is raised out of `sleep` (each step suspends
|
|
966
|
+
# the fiber and gives the control to the scheduler).
|
|
967
|
+
#
|
|
968
|
+
# NOTE: an error of another source that comes during this wait stops it: the interruption can
|
|
969
|
+
# still come after the block of code then (like two asynchronous errors at once in a thread);
|
|
970
|
+
#
|
|
971
|
+
# @return [void]
|
|
972
|
+
#
|
|
973
|
+
# @raise [RedisQueuedLocks::TimedLockIntermediateTimeoutError]
|
|
974
|
+
#
|
|
975
|
+
# @api private
|
|
976
|
+
# @since 1.24.0
|
|
977
|
+
def wait_for_interruption
|
|
978
|
+
deadline = monotonic_time + fiber_interruption_timeout
|
|
979
|
+
sleep(fiber_interruption_wait_step) while monotonic_time < deadline
|
|
980
|
+
end
|
|
981
|
+
|
|
982
|
+
# Reads the settings of the keeper from the config of the client (`config['lock_keeper.*']`).
|
|
983
|
+
#
|
|
984
|
+
# @return [void]
|
|
985
|
+
#
|
|
986
|
+
# @api private
|
|
987
|
+
# @since 1.24.0
|
|
988
|
+
def load_settings
|
|
989
|
+
config = rql_client.config
|
|
990
|
+
@idle_timeout = config['lock_keeper.idle_timeout']
|
|
991
|
+
@min_idle_wake_up_interval = config['lock_keeper.min_idle_wake_up_interval']
|
|
992
|
+
@extension_retry_ratio = config['lock_keeper.extension_retry_ratio']
|
|
993
|
+
@extension_batching_ratio = config['lock_keeper.extension_batching_ratio']
|
|
994
|
+
@schedule_slot = config['lock_keeper.schedule_slot']
|
|
995
|
+
@fiber_interruption_timeout = config['lock_keeper.fiber_interruption_timeout']
|
|
996
|
+
@fiber_interruption_wait_step = config['lock_keeper.fiber_interruption_wait_step']
|
|
997
|
+
end
|
|
998
|
+
|
|
999
|
+
# @return [Integer] In milliseconds.
|
|
1000
|
+
#
|
|
1001
|
+
# @api private
|
|
1002
|
+
# @since 1.24.0
|
|
1003
|
+
def monotonic_time
|
|
1004
|
+
clock_gettime / 1_000
|
|
1005
|
+
end
|
|
1006
|
+
end
|
|
1007
|
+
# rubocop:enable Metrics/ClassLength
|