sidekiq-amigo 1.9.0 → 1.10.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 330ead7f8face652362d632c9c127aa57b298d5587d1da392769b0458d1f8915
4
- data.tar.gz: 7c0b0b9b1658eecfd202ef294580417233421edf4233eef522b747ce9c411f52
3
+ metadata.gz: 3d3e3fb7d4b61921787b7e2baf6f6cdb25008877b9e33b786fc967b40bc9e305
4
+ data.tar.gz: 497eeba83785fe528b6053b6e1b8158a4459f21bdde74b7b056fe20a17a77e4e
5
5
  SHA512:
6
- metadata.gz: 898c24b82de6313c33e50b58a2c4d64ca365918a0aee2b223b452fea9428cc4d265b15975ebc2f7c7e689f547898e65b7c52d3e50edbe9bcdb062f3b25f3f0b6
7
- data.tar.gz: a5ffcb482859fcddd6727a9869bef3ed00c8edb5b1b44f0cde84b31eddb44c6f84fbac6b45d8b8fccf06c712f6792a8ce3e211b4a49083fe8c0bab76474a5d38
6
+ metadata.gz: 9c41c3a9b2483ba59ad03b7fbde4bad4c21bb7516f857e5e287805236372cc71f9a08c03da0ee5015286607d71e069b38de3310d6c13bf74b85e2509b7de08b8
7
+ data.tar.gz: 40ed4bd8c85cc532d6f31aa1bbeadb3c13fb949555d4f7e31099658bbd3021586466a0af6fe1bfefae102def7a0852fd57a4b72c57e6717415fee488f19edb1a
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Amigo
4
+ # Helper class to detect when the Redis server is under memory pressure.
5
+ # In these cases, we want to disable queue backoff behavior.
6
+ # There is a significant risk that the backoff behavior will take jobs from the queue,
7
+ # and immediately try and reschedule them. If that happens in an OOM condition,
8
+ # the re-push will fail and the job can be lost.
9
+ #
10
+ # Additionally, the backoff behavior causes delays that slow down the clearing of the queue.
11
+ #
12
+ # In these high-memory-utilization conditions, it makes more sense to disable the backoff logic
13
+ # and just brute force to try to get through the queue.
14
+ class MemoryPressure
15
+ # Percentage at which the server is considered under memory pressure.
16
+ DEFAULT_THRESHOLD = 90
17
+
18
+ # Default seconds a memory check is good for. See +check_ttl+.
19
+ DEFAULT_CHECK_TTL = 120
20
+
21
+ class << self
22
+ # Return the singleton instance, creating a cached value if needed.
23
+ def instance
24
+ return @instance ||= self.new
25
+ end
26
+
27
+ # Set the instance, or use nil to reset.
28
+ attr_writer :instance
29
+ end
30
+
31
+ # When did we last check for pressure?
32
+ attr_reader :last_checked_at
33
+
34
+ # What was the result of the last check?
35
+ # true is under pressure, false if not.
36
+ attr_reader :last_check_result
37
+
38
+ # See +DEFAULT_CHECK_TTL+.
39
+ attr_reader :check_ttl
40
+
41
+ # See +DEFAULT_THRESHOLD+.
42
+ attr_reader :threshold
43
+
44
+ def initialize(check_ttl: DEFAULT_CHECK_TTL, threshold: DEFAULT_THRESHOLD)
45
+ @last_checked_at = nil
46
+ @check_ttl = check_ttl
47
+ @threshold = threshold
48
+ @last_check_result = nil
49
+ end
50
+
51
+ # Return true if the server is under memory pressure.
52
+ # When this is the case, we want to disable backoff,
53
+ # since it will delay working through the queue,
54
+ # and can also result in a higher likelihood of lost jobs,
55
+ # since returning them back to the queue will fail.
56
+ def under_pressure?
57
+ return @last_check_result unless self.needs_check?
58
+ @last_check_result = self.calculate_under_pressure
59
+ @last_checked_at = Time.now
60
+ return @last_check_result
61
+ end
62
+
63
+ private def needs_check?
64
+ return true if @last_checked_at.nil?
65
+ return (@last_checked_at + @check_ttl) < Time.now
66
+ end
67
+
68
+ private def calculate_under_pressure
69
+ meminfo = self.get_memory_info
70
+ used_bytes = meminfo.fetch("used_memory", "0").to_f
71
+ max_bytes = meminfo.fetch("maxmemory", "0").to_f
72
+ return false if used_bytes.zero? || max_bytes.zero?
73
+ percentage = (used_bytes / max_bytes) * 100
74
+ return percentage > self.threshold
75
+ end
76
+
77
+ protected def get_memory_info
78
+ Sidekiq.redis do |c|
79
+ c.info :memory
80
+ end
81
+ end
82
+ end
83
+ end
@@ -3,6 +3,8 @@
3
3
  require "sidekiq"
4
4
  require "sidekiq/api"
5
5
 
6
+ require "amigo/memory_pressure"
7
+
6
8
  # Queue backoff jobs are used for jobs that should not saturate workers,
7
9
  # such that jobs on dependent queues end up not running for a while.
8
10
  #
@@ -45,6 +47,11 @@ require "sidekiq/api"
45
47
  # This is a fast call (it just gets the last item), but it's not free,
46
48
  # so users should be aware of it.
47
49
  #
50
+ # == High Memory Utilization
51
+ #
52
+ # Queue backoff behavior is automatically disabled under high memory utilization,
53
+ # as per +Amigo::MemoryPressure+.
54
+ #
48
55
  module Amigo
49
56
  module QueueBackoffJob
50
57
  def self.included(cls)
@@ -126,6 +133,7 @@ module Amigo
126
133
  module PrependedMethods
127
134
  def perform(*args)
128
135
  return super unless ::Amigo::QueueBackoffJob.enabled?
136
+ return super if ::Amigo::MemoryPressure.instance.under_pressure?
129
137
  # rubocop:disable Style/GuardClause, Lint/NonLocalExitFromIterator
130
138
  dependent_queues.each do |qname|
131
139
  latency = Amigo::QueueBackoffJob.check_latency(qname)
@@ -2,9 +2,8 @@
2
2
 
3
3
  require "sidekiq"
4
4
 
5
- module Amigo
6
- # This is a placeholder until it's migrated to Amigo proper
7
- end
5
+ require "amigo"
6
+ require "amigo/memory_pressure"
8
7
 
9
8
  # Semaphore backoff jobs can reschedule themselves to happen at a later time
10
9
  # if there is too high a contention on a semaphore.
@@ -48,6 +47,11 @@ end
48
47
  # around missing decrements), if we ever detect a negative 'jobs running',
49
48
  # we warn and remove the key entirely.
50
49
  #
50
+ # == High Memory Utilization
51
+ #
52
+ # Queue backoff behavior is automatically disabled under high memory utilization,
53
+ # as per +Amigo::MemoryPressure+.
54
+ #
51
55
  module Amigo
52
56
  module SemaphoreBackoffJob
53
57
  def self.included(cls)
@@ -94,6 +98,7 @@ module Amigo
94
98
  def perform(*args)
95
99
  self.before_perform(*args) if self.respond_to?(:before_perform)
96
100
  return super unless ::Amigo::SemaphoreBackoffJob.enabled?
101
+ return super if ::Amigo::MemoryPressure.instance.under_pressure?
97
102
  key = self.semaphore_key
98
103
  size = self.semaphore_size
99
104
  # Create a simple counter for the semaphore key.
data/lib/amigo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Amigo
4
- VERSION = "1.9.0"
4
+ VERSION = "1.10.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-amigo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lithic Technology
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-03 00:00:00.000000000 Z
11
+ date: 2025-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -179,6 +179,7 @@ files:
179
179
  - lib/amigo/autoscaler/heroku.rb
180
180
  - lib/amigo/deprecated_jobs.rb
181
181
  - lib/amigo/job.rb
182
+ - lib/amigo/memory_pressure.rb
182
183
  - lib/amigo/queue_backoff_job.rb
183
184
  - lib/amigo/rate_limited_error_handler.rb
184
185
  - lib/amigo/retry.rb