sidekiq-amigo 1.8.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 +4 -4
- data/lib/amigo/autoscaler/heroku.rb +0 -1
- data/lib/amigo/autoscaler.rb +19 -2
- data/lib/amigo/memory_pressure.rb +83 -0
- data/lib/amigo/queue_backoff_job.rb +8 -0
- data/lib/amigo/semaphore_backoff_job.rb +8 -3
- data/lib/amigo/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d3e3fb7d4b61921787b7e2baf6f6cdb25008877b9e33b786fc967b40bc9e305
|
4
|
+
data.tar.gz: 497eeba83785fe528b6053b6e1b8158a4459f21bdde74b7b056fe20a17a77e4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c41c3a9b2483ba59ad03b7fbde4bad4c21bb7516f857e5e287805236372cc71f9a08c03da0ee5015286607d71e069b38de3310d6c13bf74b85e2509b7de08b8
|
7
|
+
data.tar.gz: 40ed4bd8c85cc532d6f31aa1bbeadb3c13fb949555d4f7e31099658bbd3021586466a0af6fe1bfefae102def7a0852fd57a4b72c57e6717415fee488f19edb1a
|
data/lib/amigo/autoscaler.rb
CHANGED
@@ -92,6 +92,13 @@ module Amigo
|
|
92
92
|
# Proc/callable called with (level, message, params={}).
|
93
93
|
# By default, use +Amigo.log+ (which logs to the Sidekiq logger).
|
94
94
|
attr_reader :log
|
95
|
+
# Proc called with an exception that occurs while the thread is running.
|
96
|
+
# If the handler returns +true+, then the thread will keep going.
|
97
|
+
# All other values will kill the thread, which breaks autoscaling.
|
98
|
+
# Note that Amigo automatically logs unhandled exceptions at :error level.
|
99
|
+
# If you use an error reporter like Sentry, you can pass in something like:
|
100
|
+
# -> (e) { Sentry.capture_exception(e) }
|
101
|
+
attr_reader :on_unhandled_exception
|
95
102
|
|
96
103
|
def initialize(
|
97
104
|
poll_interval: 20,
|
@@ -101,9 +108,9 @@ module Amigo
|
|
101
108
|
alert_interval: 120,
|
102
109
|
latency_restored_threshold: latency_threshold,
|
103
110
|
latency_restored_handlers: [:log],
|
104
|
-
log: ->(level, message, params={}) { Amigo.log(nil, level, message, params) }
|
111
|
+
log: ->(level, message, params={}) { Amigo.log(nil, level, message, params) },
|
112
|
+
on_unhandled_exception: nil
|
105
113
|
)
|
106
|
-
|
107
114
|
raise ArgumentError, "latency_threshold must be > 0" if
|
108
115
|
latency_threshold <= 0
|
109
116
|
raise ArgumentError, "latency_restored_threshold must be >= 0" if
|
@@ -118,8 +125,10 @@ module Amigo
|
|
118
125
|
@latency_restored_threshold = latency_restored_threshold
|
119
126
|
@latency_restored_handlers = latency_restored_handlers.freeze
|
120
127
|
@log = log
|
128
|
+
@on_unhandled_exception = on_unhandled_exception
|
121
129
|
end
|
122
130
|
|
131
|
+
# @return [Thread]
|
123
132
|
def polling_thread
|
124
133
|
return @polling_thread
|
125
134
|
end
|
@@ -189,6 +198,14 @@ module Amigo
|
|
189
198
|
end
|
190
199
|
|
191
200
|
def check
|
201
|
+
self._check
|
202
|
+
rescue StandardError => e
|
203
|
+
self._log(:error, "async_autoscaler_unhandled_error", exception: e)
|
204
|
+
handled = self.on_unhandled_exception&.call(e)
|
205
|
+
raise e unless handled.eql?(true)
|
206
|
+
end
|
207
|
+
|
208
|
+
def _check
|
192
209
|
now = Time.now
|
193
210
|
skip_check = now < (@last_alerted + self.alert_interval)
|
194
211
|
if skip_check
|
@@ -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
|
-
|
6
|
-
|
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
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.
|
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:
|
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
|