redis_queued_locks 1.23.0 → 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.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.claude/project-overview.md +70 -33
  3. data/.claude/rules/acquirer.md +6 -3
  4. data/.claude/rules/arguments.md +2 -2
  5. data/.claude/rules/git.md +1 -1
  6. data/.claude/rules/logic.md +5 -4
  7. data/.claude/rules/readme.md +40 -0
  8. data/.claude/rules/swarm.md +2 -2
  9. data/.claude/rules/tests.md +3 -2
  10. data/.claude/rules/type-checking.md +3 -2
  11. data/.claude/rules/visitors.md +3 -2
  12. data/.claude/skills/rql-code-review/SKILL.md +6 -3
  13. data/.claude/skills/rql-project-full-review/SKILL.md +171 -0
  14. data/.claude/skills/rql-project-full-review/templates/report.en.md +41 -0
  15. data/.claude/skills/rql-project-full-review/templates/report.ru.md +41 -0
  16. data/CHANGELOG.md +30 -9
  17. data/CLAUDE.md +7 -5
  18. data/README.md +390 -17
  19. data/lib/redis_queued_locks/acquirer/acquire_lock/dequeue_from_lock_queue.rb +60 -1
  20. data/lib/redis_queued_locks/acquirer/acquire_lock/try_to_lock/log_visitor.rb +38 -1
  21. data/lib/redis_queued_locks/acquirer/acquire_lock/try_to_lock.rb +31 -4
  22. data/lib/redis_queued_locks/acquirer/acquire_lock/with_acq_timeout.rb +6 -3
  23. data/lib/redis_queued_locks/acquirer/acquire_lock/yield_expire.rb +257 -32
  24. data/lib/redis_queued_locks/acquirer/acquire_lock.rb +103 -4
  25. data/lib/redis_queued_locks/acquirer/lock_series/try_to_lock_series.rb +27 -1
  26. data/lib/redis_queued_locks/acquirer/lock_series.rb +160 -15
  27. data/lib/redis_queued_locks/client.rb +96 -16
  28. data/lib/redis_queued_locks/config/dsl.rb +2 -0
  29. data/lib/redis_queued_locks/config.rb +35 -11
  30. data/lib/redis_queued_locks/errors.rb +19 -2
  31. data/lib/redis_queued_locks/lock_keeper/instr_visitor.rb +173 -0
  32. data/lib/redis_queued_locks/lock_keeper/lease.rb +434 -0
  33. data/lib/redis_queued_locks/lock_keeper/lock_lease.rb +106 -0
  34. data/lib/redis_queued_locks/lock_keeper/log_visitor.rb +187 -0
  35. data/lib/redis_queued_locks/lock_keeper.rb +1007 -0
  36. data/lib/redis_queued_locks/resource.rb +1 -1
  37. data/lib/redis_queued_locks/swarm/swarm_element/threaded.rb +2 -2
  38. data/lib/redis_queued_locks/utilities.rb +1 -15
  39. data/lib/redis_queued_locks/version.rb +2 -2
  40. data/lib/redis_queued_locks.rb +1 -0
  41. data/sig/redis_queued_locks/acquirer/acquire_lock/dequeue_from_lock_queue.rbs +14 -0
  42. data/sig/redis_queued_locks/acquirer/acquire_lock/try_to_lock/log_visitor.rbs +12 -0
  43. data/sig/redis_queued_locks/acquirer/acquire_lock/try_to_lock.rbs +2 -0
  44. data/sig/redis_queued_locks/acquirer/acquire_lock/yield_expire.rbs +30 -2
  45. data/sig/redis_queued_locks/acquirer/acquire_lock.rbs +6 -1
  46. data/sig/redis_queued_locks/acquirer/lock_series.rbs +12 -1
  47. data/sig/redis_queued_locks/client.rbs +14 -0
  48. data/sig/redis_queued_locks/config.rbs +15 -1
  49. data/sig/redis_queued_locks/errors.rbs +12 -0
  50. data/sig/redis_queued_locks/instrument/void_notifier.rbs +2 -2
  51. data/sig/redis_queued_locks/lock_keeper/instr_visitor.rbs +67 -0
  52. data/sig/redis_queued_locks/lock_keeper/lease.rbs +78 -0
  53. data/sig/redis_queued_locks/lock_keeper/lock_lease.rbs +19 -0
  54. data/sig/redis_queued_locks/lock_keeper/log_visitor.rbs +62 -0
  55. data/sig/redis_queued_locks/lock_keeper.rbs +108 -0
  56. data/sig/redis_queued_locks/utilities.rbs +0 -2
  57. data/sig/vendor/redis_client.rbs +1 -0
  58. metadata +15 -1
@@ -2,7 +2,7 @@
2
2
 
3
3
  # @api public
4
4
  # @since 1.0.0
5
- # @version 1.23.0
5
+ # @version 1.24.0
6
6
  # rubocop:disable Metrics/ClassLength
7
7
  class RedisQueuedLocks::Client
8
8
  # @return [RedisClient]
@@ -31,6 +31,12 @@ class RedisQueuedLocks::Client
31
31
  # @since 1.13.0
32
32
  attr_reader :config
33
33
 
34
+ # @return [RedisQueuedLocks::LockKeeper]
35
+ #
36
+ # @api private
37
+ # @since 1.24.0
38
+ attr_reader :lock_keeper
39
+
34
40
  # @param redis_client [RedisClient]
35
41
  # Redis connection manager, which will be used for the lock acquirerment and distribution.
36
42
  # It should be an instance of RedisClient.
@@ -40,11 +46,12 @@ class RedisQueuedLocks::Client
40
46
  #
41
47
  # @api public
42
48
  # @since 1.0.0
43
- # @version 1.13.0
49
+ # @version 1.24.0
44
50
  def initialize(redis_client, &configuration)
45
51
  @config = RedisQueuedLocks::Config.new(&configuration)
46
52
  @uniq_identity = config['uniq_identifier'].call
47
53
  @redis_client = redis_client
54
+ @lock_keeper = RedisQueuedLocks::LockKeeper.new(self)
48
55
  @swarm = RedisQueuedLocks::Swarm.new(self)
49
56
  @swarm.swarm! if config['swarm.auto_swarm']
50
57
  end
@@ -193,7 +200,49 @@ class RedisQueuedLocks::Client
193
200
  # Time period whe should try to acquire the lock (in seconds). Nil means "without timeout"
194
201
  # (`0` is the same as `nil`).
195
202
  # @option timed [Boolean]
196
- # Limit the invocation time period of the passed block of code by the lock's TTL.
203
+ # - Limit the invocation time period of the passed block of code by the lock's TTL;
204
+ # - with `auto_extend: true`: by `max_hold_time_during_auto_extend`, and the block of code is
205
+ # interrupted when the lock is lost;
206
+ # @option auto_extend [Boolean]
207
+ # - Keep the lock alive while the passed block of code is running: the block of code can run
208
+ # longer than the lock's TTL, the crash of the process releases the lock by the TTL (the TTL
209
+ # becomes a lease): one background thread per client extends the locks of all such blocks;
210
+ # - a lock that is lost during the block of code (the expired lease, `#unlock`) is reported by
211
+ # logs and instrumentation and the block of code continues to work (use `timed: true` to
212
+ # interrupt it); it is never revived, and the next owner of the lock does not lose it after
213
+ # the block of code;
214
+ # - has no effect without a block of code;
215
+ # - the block of code gets its lease (`RedisQueuedLocks::LockKeeper::LockLease`) when it takes
216
+ # an argument:
217
+ # `#hold_for` moves the hold limit while the block makes progress, `#lost?`,
218
+ # `#hold_time_left`;
219
+ # - see README "Lock auto extension" for the limits of a background thread;
220
+ # - pre-configured in `config['is_auto_extended_by_default']`;
221
+ # @option auto_extend_ttl [Integer,NilClass]
222
+ # - How long the lock lives after each auto extension (in milliseconds; it is not a sum: the
223
+ # lock lives this time from the moment of the extension): the lifetime of the lock after a
224
+ # crash and the tolerance of the extension to stalls of the process (2/3 of this value);
225
+ # extensions are made each `auto_extend_ttl / 3`;
226
+ # - `nil` means the lock's TTL (`ttl`);
227
+ # - pre-configured in `config['default_auto_extend_ttl']`;
228
+ # @option max_hold_time_during_auto_extend [Integer,NilClass]
229
+ # - The protection of an auto extended lock against blocks of code that never finish: a block
230
+ # that hangs forever (an endless loop, a call without a timeout) or a block abandoned midway
231
+ # (a fiber that is never resumed: its `ensure` never runs and the lock is not released); such
232
+ # a block looks like a running one, and without a limit its lock is extended till the end of
233
+ # the process;
234
+ # - the limit of the auto extension (in milliseconds, from the start of the successful lock
235
+ # acquirement attempt): the lock expires by this time and the block of code continues to
236
+ # work (use `timed: true` to interrupt it);
237
+ # - !IMPORTANT: configure it yourself, deliberately, for your logic: `nil` or a period that is
238
+ # longer than your longest block of code (a longer block of code loses its lock at the
239
+ # limit); the default of `config['default_max_hold_time_during_auto_extend']` (1 hour) is
240
+ # only a fallback;
241
+ # - a block of code that makes progress can move it
242
+ # (`RedisQueuedLocks::LockKeeper::LockLease#hold_for`);
243
+ # - it should not be less than `ttl` for an auto extended lock;
244
+ # - `nil` means no limit (a stuck block of code holds the lock till the end of the process);
245
+ # - pre-configured in `config['default_max_hold_time_during_auto_extend']`;
197
246
  # @option retry_count [Integer,NilClass]
198
247
  # How many times we should try to acquire a lock. Nil means "infinite retries".
199
248
  # @option retry_delay [Integer]
@@ -205,7 +254,7 @@ class RedisQueuedLocks::Client
205
254
  # @option identity [String]
206
255
  # Unique acquire identifier that is also should be unique between processes and pods
207
256
  # on different machines. By default the uniq identity string is
208
- # represented as 10 bytes hexstr.
257
+ # a 16-character hex string (`SecureRandom.hex(8)`).
209
258
  # @option fail_fast [Boolean]
210
259
  # - Should the required lock to be checked before the try and exit immidietly if lock is
211
260
  # already obtained;
@@ -359,14 +408,16 @@ class RedisQueuedLocks::Client
359
408
  # despite the enabled instrumentation sampling;
360
409
  # - makes sense when instrumentation sampling is enabled;
361
410
  # @yield A block of code that should be executed after the successfully acquired lock.
362
- # @yieldreturn [Any]
411
+ # @yieldparam lease [RedisQueuedLocks::LockKeeper::LockLease]
412
+ # The lease of an auto extended block of code (`auto_extend: true`, a block with arguments).
413
+ # @yieldreturn [Any]
363
414
  # @return [Hash<Symbol,Any>,yield]
364
415
  # - Format: { ok: true/false, result: Symbol/Hash }.
365
416
  # - If block is given the result of block's yield will be returned.
366
417
  #
367
418
  # @api public
368
419
  # @since 1.0.0
369
- # @version 1.23.0
420
+ # @version 1.24.0
370
421
  # rubocop:disable Metrics/MethodLength
371
422
  def lock(
372
423
  lock_name,
@@ -375,6 +426,9 @@ class RedisQueuedLocks::Client
375
426
  lock_index_grace_period: config['lock_index_grace_period'],
376
427
  timeout: config['try_to_lock_timeout'],
377
428
  timed: config['is_timed_by_default'],
429
+ auto_extend: config['is_auto_extended_by_default'],
430
+ auto_extend_ttl: config['default_auto_extend_ttl'],
431
+ max_hold_time_during_auto_extend: config['default_max_hold_time_during_auto_extend'],
378
432
  retry_count: config['retry_count'],
379
433
  retry_delay: config['retry_delay'],
380
434
  retry_jitter: config['retry_jitter'],
@@ -414,6 +468,10 @@ class RedisQueuedLocks::Client
414
468
  lock_index_grace_period:,
415
469
  timeout:,
416
470
  timed:,
471
+ auto_extend:,
472
+ auto_extend_ttl:,
473
+ max_hold_time_during_auto_extend:,
474
+ lock_keeper:,
417
475
  retry_count:,
418
476
  retry_delay:,
419
477
  retry_jitter:,
@@ -463,6 +521,9 @@ class RedisQueuedLocks::Client
463
521
  # @option priority [Integer,Symbol]
464
522
  # The lock request priority of the series. See `#lock`.
465
523
  # @yield A block of code that should be executed under all obtained locks.
524
+ # @yieldparam lease [RedisQueuedLocks::LockKeeper::LockLease]
525
+ # The lease of an auto extended series (`auto_extend: true`, a block with arguments): one
526
+ # lease for all locks of the series.
466
527
  # @yieldreturn [Any]
467
528
  # @return [Hash<Symbol,Any>,yield]
468
529
  # - the result of the block when the series is obtained with a block;
@@ -474,10 +535,10 @@ class RedisQueuedLocks::Client
474
535
  #
475
536
  # @note See #lock method signature for all other options.
476
537
  #
477
- # rubocop:disable Metrics/MethodLength
478
538
  # @api public
479
539
  # @since 1.16.0
480
- # @version 1.23.0
540
+ # @version 1.24.0
541
+ # rubocop:disable Metrics/MethodLength
481
542
  def lock_series(
482
543
  *lock_names,
483
544
  detailed_result: false,
@@ -486,6 +547,9 @@ class RedisQueuedLocks::Client
486
547
  lock_index_grace_period: config['lock_index_grace_period'],
487
548
  timeout: config['try_to_lock_timeout'],
488
549
  timed: config['is_timed_by_default'],
550
+ auto_extend: config['is_auto_extended_by_default'],
551
+ auto_extend_ttl: config['default_auto_extend_ttl'],
552
+ max_hold_time_during_auto_extend: config['default_max_hold_time_during_auto_extend'],
489
553
  retry_count: config['retry_count'],
490
554
  retry_delay: config['retry_delay'],
491
555
  retry_jitter: config['retry_jitter'],
@@ -526,6 +590,10 @@ class RedisQueuedLocks::Client
526
590
  lock_index_grace_period:,
527
591
  timeout:,
528
592
  timed:,
593
+ auto_extend:,
594
+ auto_extend_ttl:,
595
+ max_hold_time_during_auto_extend:,
596
+ lock_keeper:,
529
597
  retry_count:,
530
598
  retry_delay:,
531
599
  retry_jitter:,
@@ -558,10 +626,10 @@ class RedisQueuedLocks::Client
558
626
 
559
627
  # @note See #lock_series method signature.
560
628
  #
561
- # rubocop:disable Metrics/MethodLength
562
629
  # @api public
563
630
  # @since 1.16.0
564
- # @version 1.23.0
631
+ # @version 1.24.0
632
+ # rubocop:disable Metrics/MethodLength
565
633
  def lock_series!(
566
634
  *lock_names,
567
635
  detailed_result: false,
@@ -570,6 +638,9 @@ class RedisQueuedLocks::Client
570
638
  lock_index_grace_period: config['lock_index_grace_period'],
571
639
  timeout: config['try_to_lock_timeout'],
572
640
  timed: config['is_timed_by_default'],
641
+ auto_extend: config['is_auto_extended_by_default'],
642
+ auto_extend_ttl: config['default_auto_extend_ttl'],
643
+ max_hold_time_during_auto_extend: config['default_max_hold_time_during_auto_extend'],
573
644
  retry_count: config['retry_count'],
574
645
  retry_delay: config['retry_delay'],
575
646
  retry_jitter: config['retry_jitter'],
@@ -603,6 +674,9 @@ class RedisQueuedLocks::Client
603
674
  lock_index_grace_period:,
604
675
  timeout:,
605
676
  timed:,
677
+ auto_extend:,
678
+ auto_extend_ttl:,
679
+ max_hold_time_during_auto_extend:,
606
680
  retry_count:,
607
681
  retry_delay:,
608
682
  retry_jitter:,
@@ -634,10 +708,10 @@ class RedisQueuedLocks::Client
634
708
 
635
709
  # @note See #lock method signature.
636
710
  #
637
- # rubocop:disable Metrics/MethodLength
638
711
  # @api public
639
712
  # @since 1.0.0
640
- # @version 1.23.0
713
+ # @version 1.24.0
714
+ # rubocop:disable Metrics/MethodLength
641
715
  def lock!(
642
716
  lock_name,
643
717
  ttl: config['default_lock_ttl'],
@@ -645,6 +719,9 @@ class RedisQueuedLocks::Client
645
719
  lock_index_grace_period: config['lock_index_grace_period'],
646
720
  timeout: config['try_to_lock_timeout'],
647
721
  timed: config['is_timed_by_default'],
722
+ auto_extend: config['is_auto_extended_by_default'],
723
+ auto_extend_ttl: config['default_auto_extend_ttl'],
724
+ max_hold_time_during_auto_extend: config['default_max_hold_time_during_auto_extend'],
648
725
  retry_count: config['retry_count'],
649
726
  retry_delay: config['retry_delay'],
650
727
  retry_jitter: config['retry_jitter'],
@@ -677,6 +754,9 @@ class RedisQueuedLocks::Client
677
754
  lock_index_grace_period:,
678
755
  timeout:,
679
756
  timed:,
757
+ auto_extend:,
758
+ auto_extend_ttl:,
759
+ max_hold_time_during_auto_extend:,
680
760
  retry_count:,
681
761
  retry_delay:,
682
762
  retry_jitter:,
@@ -1295,7 +1375,7 @@ class RedisQueuedLocks::Client
1295
1375
  # Extracts lock keys with their info. See #locks(with_info: true) for details.
1296
1376
  #
1297
1377
  # @option scan_size [Integer]
1298
- # @return [Set<Hash<String,Any>>]
1378
+ # @return [Set<Hash<Symbol,Any>>]
1299
1379
  #
1300
1380
  # @api public
1301
1381
  # @since 1.0.0
@@ -1311,12 +1391,12 @@ class RedisQueuedLocks::Client
1311
1391
  # consider that during info extration the lock queue may become empty. The queue key extraction
1312
1392
  # (SCAN) without queue info extraction is doing first.
1313
1393
  # Possible options:
1314
- # - `true` => returns a set of hashes that represents the queue state: <queue:, containing:>
1394
+ # - `true` => returns a set of hashes that represents the queue state: <queue:, requests:>
1315
1395
  # - :queue (String) - the lock queue key in Redis database;
1316
1396
  # - :requests (Array<Hash<String,Any>>) - queue state in redis. see #queue_info for details;
1317
1397
  # - `false` => returns a set of strings that represents an active queues
1318
1398
  # at the moment of Redis'es SCAN;
1319
- # @return [Set<String>,String<Hash<Symbol,Any>>]
1399
+ # @return [Set<String>,Set<Hash<Symbol,Any>>]
1320
1400
  #
1321
1401
  # NOTE: lock queues are found via the keyspace SCAN or via the lock index (ZSCAN, the enabled
1322
1402
  # `config['lock_index_enabled']`: `scan_size` is the number of locks in one batch);
@@ -1384,7 +1464,7 @@ class RedisQueuedLocks::Client
1384
1464
  # @option instr_sampler [#sampling_happened?,Module<RedisQueuedLocks::Instrument::Sampler>]
1385
1465
  # @option instr_sample_this [Boolean]
1386
1466
  # @return [Hash<Symbol,Boolean|Hash<Symbol,Set<String>>>]
1387
- # Format: { ok: true, result: { processed_queus: Set<String> } }
1467
+ # Format: { ok: true, result: { processed_queues: Set<String> } }
1388
1468
  #
1389
1469
  # @api public
1390
1470
  # @since 1.0.0
@@ -47,6 +47,8 @@ module RedisQueuedLocks::Config::DSL
47
47
  end
48
48
  end
49
49
 
50
+ # @api private
51
+ # @since 1.13.0
50
52
  module InstanceMethods
51
53
  # @return [Hash<String,Blcok>]
52
54
  #
@@ -12,11 +12,11 @@ class RedisQueuedLocks::Config
12
12
  include DSL
13
13
 
14
14
  setting('retry_count', 3)
15
- setting('retry_delay', 200) # NOTE: in milliseconds)
16
- setting('retry_jitter', 25) # NOTE: in milliseconds)
17
- setting('try_to_lock_timeout', 10) # NOTE: in seconds)
18
- setting('default_lock_ttl', 5_000) # NOTE: in milliseconds)
19
- setting('default_queue_ttl', 15) # NOTE: in seconds)
15
+ setting('retry_delay', 200) # NOTE: in milliseconds
16
+ setting('retry_jitter', 25) # NOTE: in milliseconds
17
+ setting('try_to_lock_timeout', 10) # NOTE: in seconds
18
+ setting('default_lock_ttl', 5_000) # NOTE: in milliseconds
19
+ setting('default_queue_ttl', 15) # NOTE: in seconds
20
20
  setting('detailed_acq_timeout_error', false)
21
21
  setting('lock_release_batch_size', 100)
22
22
  setting('clear_locks_of__lock_scan_size', 300)
@@ -26,10 +26,20 @@ class RedisQueuedLocks::Config
26
26
  setting('uniq_identifier', -> { RedisQueuedLocks::Resource.calc_uniq_identity })
27
27
  setting('logger', RedisQueuedLocks::Logging::VoidLogger)
28
28
  setting('log_lock_try', false)
29
- setting('dead_request_ttl', 1 * 24 * 60 * 60 * 1000) # NOTE: 1 day in milliseconds)
29
+ setting('dead_request_ttl', 1 * 24 * 60 * 60 * 1000) # NOTE: 1 day in milliseconds
30
30
  setting('lock_index_enabled', false)
31
- setting('lock_index_grace_period', 60_000) # NOTE: in milliseconds)
31
+ setting('lock_index_grace_period', 60_000) # NOTE: in milliseconds
32
32
  setting('is_timed_by_default', false)
33
+ setting('is_auto_extended_by_default', false)
34
+ setting('default_auto_extend_ttl', nil) # NOTE: in milliseconds (nil: the ttl of the lock)
35
+ setting('default_max_hold_time_during_auto_extend', 3_600_000) # NOTE: 1h in ms (nil: no limit)
36
+ setting('lock_keeper.idle_timeout', 60_000) # NOTE: in milliseconds
37
+ setting('lock_keeper.min_idle_wake_up_interval', 100) # NOTE: in milliseconds
38
+ setting('lock_keeper.extension_retry_ratio', 3)
39
+ setting('lock_keeper.extension_batching_ratio', 10)
40
+ setting('lock_keeper.schedule_slot', 10) # NOTE: in milliseconds
41
+ setting('lock_keeper.fiber_interruption_timeout', 1_000) # NOTE: in milliseconds
42
+ setting('lock_keeper.fiber_interruption_wait_step', 0.001) # NOTE: in seconds
33
43
  setting('default_conflict_strategy', :wait_for_lock)
34
44
  setting('default_access_strategy', :queued)
35
45
  setting('lock_priorities', { lowest: -100, low: -50, normal: 0, high: 50, highest: 100 }.freeze)
@@ -41,18 +51,18 @@ class RedisQueuedLocks::Config
41
51
  setting('instr_sampling_percent', 15)
42
52
  setting('instr_sampler', RedisQueuedLocks::Instrument::Sampler)
43
53
  setting('swarm.auto_swarm', false)
44
- setting('swarm.supervisor.liveness_probing_period', 2) # NOTE: in seconds)
54
+ setting('swarm.supervisor.liveness_probing_period', 2) # NOTE: in seconds
45
55
  setting('swarm.probe_hosts.enabled_for_swarm', true)
46
- setting('swarm.probe_hosts.probe_period', 2) # NOTE: in seconds)
56
+ setting('swarm.probe_hosts.probe_period', 2) # NOTE: in seconds
47
57
  setting('swarm.probe_hosts.redis_config.sentinel', false)
48
58
  setting('swarm.probe_hosts.redis_config.pooled', false)
49
59
  setting('swarm.probe_hosts.redis_config.config', {})
50
60
  setting('swarm.probe_hosts.redis_config.pool_config', {})
51
61
  setting('swarm.flush_zombies.enabled_for_swarm', true)
52
- setting('swarm.flush_zombies.zombie_ttl', 15_000) # NOTE: in milliseconds)
62
+ setting('swarm.flush_zombies.zombie_ttl', 15_000) # NOTE: in milliseconds
53
63
  setting('swarm.flush_zombies.zombie_lock_scan_size', 500)
54
64
  setting('swarm.flush_zombies.zombie_queue_scan_size', 500)
55
- setting('swarm.flush_zombies.zombie_flush_period', 10)
65
+ setting('swarm.flush_zombies.zombie_flush_period', 10) # NOTE: in seconds
56
66
  setting('swarm.flush_zombies.redis_config.sentinel', false)
57
67
  setting('swarm.flush_zombies.redis_config.pooled', false)
58
68
  setting('swarm.flush_zombies.redis_config.config', {})
@@ -97,6 +107,20 @@ class RedisQueuedLocks::Config
97
107
  validate('lock_index_enabled') { |val| val == true || val == false }
98
108
  validate('lock_index_grace_period') { |val| val.is_a?(Integer) && val >= 0 }
99
109
  validate('is_timed_by_default') { |val| val == true || val == false }
110
+ validate('is_auto_extended_by_default') { |val| val == true || val == false }
111
+ validate('default_auto_extend_ttl') { |val| val == nil || (val.is_a?(Integer) && val > 0) }
112
+ validate('default_max_hold_time_during_auto_extend') do |val|
113
+ val == nil || (val.is_a?(Integer) && val > 0)
114
+ end
115
+ validate('lock_keeper.idle_timeout') { |val| val.is_a?(Integer) && val > 0 }
116
+ validate('lock_keeper.min_idle_wake_up_interval') { |val| val.is_a?(Integer) && val > 0 }
117
+ validate('lock_keeper.extension_retry_ratio') { |val| val.is_a?(Integer) && val > 0 }
118
+ validate('lock_keeper.extension_batching_ratio') { |val| val.is_a?(Integer) && val > 0 }
119
+ validate('lock_keeper.schedule_slot') { |val| val.is_a?(Integer) && val > 0 }
120
+ validate('lock_keeper.fiber_interruption_timeout') { |val| val.is_a?(Integer) && val > 0 }
121
+ validate('lock_keeper.fiber_interruption_wait_step') do |val|
122
+ (val.is_a?(Float) || val.is_a?(Integer)) && val > 0
123
+ end
100
124
  validate('log_sampler') { |val| RedisQueuedLocks::Logging.valid_sampler?(val) }
101
125
  validate('log_sampling_enabled') { |val| val == true || val == false }
102
126
  validate('log_sampling_percent') { |val| val.is_a?(Integer) && val >= 0 && val <= 100 }
@@ -51,8 +51,9 @@ module RedisQueuedLocks
51
51
  # @since 1.0.0
52
52
  class LockAcquirementRetryLimitError < Error; end
53
53
 
54
- # Interrupts the `timed: true` block after the lock ttl (`Timeout.timeout`); rescued by
55
- # `YieldExpire` (=> `TimedLockTimeoutError`).
54
+ # Interrupts the `timed: true` block after the lock ttl (`Timeout.timeout`), after the hold limit
55
+ # of the auto extended lock (`Timeout.timeout`) and after the loss of the auto extended lock
56
+ # (`LockKeeper`); rescued by `YieldExpire` (=> `TimedLockTimeoutError`).
56
57
  # NOTE: (why `Exception`) a `StandardError` is swallowed by `rescue => e` / `rescue nil` of
57
58
  # the user block: the block continues after the lock ttl (without the mutual exclusion:
58
59
  # another acquirer can obtain the expired lock, and the write lock release after the block
@@ -67,6 +68,22 @@ module RedisQueuedLocks
67
68
  # @since 1.0.0
68
69
  class TimedLockTimeoutError < Error; end
69
70
 
71
+ # @api public
72
+ # @since 1.23.1
73
+ class LockTtlTimeBudgetIsSpentError < TimedLockTimeoutError; end
74
+
75
+ # @api public
76
+ # @since 1.23.1
77
+ class TimedBlockExceedsLockTtlError < TimedLockTimeoutError; end
78
+
79
+ # @api public
80
+ # @since 1.24.0
81
+ class TimedBlockExceedsMaxHoldTimeError < TimedLockTimeoutError; end
82
+
83
+ # @api public
84
+ # @since 1.24.0
85
+ class TimedBlockLostLockError < TimedLockTimeoutError; end
86
+
70
87
  # @api public
71
88
  # @since 1.3.0
72
89
  class ConflictLockObtainError < Error; end
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 1.24.0
5
+ module RedisQueuedLocks::LockKeeper::InstrVisitor
6
+ class << self
7
+ # @param instrumenter [#notify]
8
+ # @param instr_sampled [Boolean]
9
+ # @param lock_key [String]
10
+ # @param rw_mode [Symbol]
11
+ # @param ext_ttl [Integer]
12
+ # @param rem_ttl [Integer]
13
+ # @param acq_id [String]
14
+ # @param hst_id [String]
15
+ # @param instrument [NilClass,Any]
16
+ # @return [void]
17
+ #
18
+ # @api private
19
+ # @since 1.24.0
20
+ def lock_auto_extended(
21
+ instrumenter,
22
+ instr_sampled,
23
+ lock_key,
24
+ rw_mode,
25
+ ext_ttl,
26
+ rem_ttl,
27
+ acq_id,
28
+ hst_id,
29
+ instrument
30
+ )
31
+ return unless instr_sampled
32
+
33
+ instrumenter.notify('redis_queued_locks.lock_auto_extended', {
34
+ lock_key:, rw_mode:, ext_ttl:, rem_ttl:, acq_id:, hst_id:, at: Time.now.to_f, instrument:
35
+ }) rescue nil
36
+ end
37
+
38
+ # @param instrumenter [#notify]
39
+ # @param instr_sampled [Boolean]
40
+ # @param lock_key [String]
41
+ # @param rw_mode [Symbol]
42
+ # @param ext_ttl [Integer]
43
+ # @param acq_id [String]
44
+ # @param hst_id [String]
45
+ # @param error [String] The class name of the error.
46
+ # @param instrument [NilClass,Any]
47
+ # @return [void]
48
+ #
49
+ # @api private
50
+ # @since 1.24.0
51
+ def lock_auto_extension_failed(
52
+ instrumenter,
53
+ instr_sampled,
54
+ lock_key,
55
+ rw_mode,
56
+ ext_ttl,
57
+ acq_id,
58
+ hst_id,
59
+ error,
60
+ instrument
61
+ )
62
+ return unless instr_sampled
63
+
64
+ instrumenter.notify('redis_queued_locks.lock_auto_extension_failed', {
65
+ lock_key:, rw_mode:, ext_ttl:, acq_id:, hst_id:, error:, at: Time.now.to_f, instrument:
66
+ }) rescue nil
67
+ end
68
+
69
+ # @param instrumenter [#notify]
70
+ # @param instr_sampled [Boolean]
71
+ # @param lock_key [String]
72
+ # @param rw_mode [Symbol]
73
+ # @param acq_id [String]
74
+ # @param hst_id [String]
75
+ # @param hold_time [Integer]
76
+ # @param instrument [NilClass,Any]
77
+ # @return [void]
78
+ #
79
+ # @api private
80
+ # @since 1.24.0
81
+ def auto_extended_lock_lost(
82
+ instrumenter,
83
+ instr_sampled,
84
+ lock_key,
85
+ rw_mode,
86
+ acq_id,
87
+ hst_id,
88
+ hold_time,
89
+ instrument
90
+ )
91
+ return unless instr_sampled
92
+
93
+ instrumenter.notify('redis_queued_locks.auto_extended_lock_lost', {
94
+ lock_key:, rw_mode:, acq_id:, hst_id:, hold_time:, at: Time.now.to_f, instrument:
95
+ }) rescue nil
96
+ end
97
+
98
+ # @param instrumenter [#notify]
99
+ # @param instr_sampled [Boolean]
100
+ # @param lock_key [String]
101
+ # @param rw_mode [Symbol]
102
+ # @param max_hold_time_during_auto_extend [Integer]
103
+ # @param acq_id [String]
104
+ # @param hst_id [String]
105
+ # @param instrument [NilClass,Any]
106
+ # @return [void]
107
+ #
108
+ # @api private
109
+ # @since 1.24.0
110
+ def lock_hold_limit_reached(
111
+ instrumenter,
112
+ instr_sampled,
113
+ lock_key,
114
+ rw_mode,
115
+ max_hold_time_during_auto_extend,
116
+ acq_id,
117
+ hst_id,
118
+ instrument
119
+ )
120
+ return unless instr_sampled
121
+
122
+ instrumenter.notify('redis_queued_locks.lock_hold_limit_reached', {
123
+ lock_key:, rw_mode:, max_hold_time_during_auto_extend:, acq_id:, hst_id:, at: Time.now.to_f,
124
+ instrument:
125
+ }) rescue nil
126
+ end
127
+
128
+ # @param instrumenter [#notify]
129
+ # @param instr_sampled [Boolean]
130
+ # @param lock_key [String]
131
+ # @param rw_mode [Symbol]
132
+ # @param hold_time [Integer]
133
+ # The requested time (`RedisQueuedLocks::LockKeeper::LockLease#hold_for`).
134
+ # @param rem_hold_time [Integer,NilClass] The time left till the hold limit, `nil`: no limit.
135
+ # @param ext_ttl [Integer]
136
+ # @param rem_ttl [Integer]
137
+ # @param acq_id [String]
138
+ # @param hst_id [String]
139
+ # @param instrument [NilClass,Any]
140
+ # @return [void]
141
+ #
142
+ # @api private
143
+ # @since 1.24.0
144
+ def lock_hold_limit_extended(
145
+ instrumenter,
146
+ instr_sampled,
147
+ lock_key,
148
+ rw_mode,
149
+ hold_time,
150
+ rem_hold_time,
151
+ ext_ttl,
152
+ rem_ttl,
153
+ acq_id,
154
+ hst_id,
155
+ instrument
156
+ )
157
+ return unless instr_sampled
158
+
159
+ instrumenter.notify('redis_queued_locks.lock_hold_limit_extended', {
160
+ lock_key:,
161
+ rw_mode:,
162
+ hold_time:,
163
+ rem_hold_time:,
164
+ ext_ttl:,
165
+ rem_ttl:,
166
+ acq_id:,
167
+ hst_id:,
168
+ at: Time.now.to_f,
169
+ instrument:
170
+ }) rescue nil
171
+ end
172
+ end
173
+ end