Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abort Ruby Reaper when sidekiq queues are full #690

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .reek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ detectors:
- SidekiqUniqueJobs::OnConflict::Reject#push_to_deadset
- SidekiqUniqueJobs::Orphans::RubyReaper#active?
- SidekiqUniqueJobs::Orphans::RubyReaper#entries
- SidekiqUniqueJobs::Orphans::RubyReaper#queues_very_full?
- SidekiqUniqueJobs::Redis::Entity#exist?
- SidekiqUniqueJobs::Web::Helpers#cparams
- SidekiqUniqueJobs::Web::Helpers#display_lock_args
Expand Down Expand Up @@ -115,6 +116,7 @@ detectors:
- SidekiqUniqueJobs::OnConflict::Reject#push_to_deadset
- SidekiqUniqueJobs::Orphans::RubyReaper#active?
- SidekiqUniqueJobs::Orphans::RubyReaper#enqueued?
- SidekiqUniqueJobs::Orphans::RubyReaper#queues_very_full?
- SidekiqUniqueJobs::UpgradeLocks#keys_for_digest
RepeatedConditional:
exclude:
Expand Down Expand Up @@ -156,6 +158,7 @@ detectors:
- SidekiqUniqueJobs::Orphans::RubyReaper#enqueued?
- SidekiqUniqueJobs::Orphans::RubyReaper#entries
- SidekiqUniqueJobs::Orphans::RubyReaper#orphans
- SidekiqUniqueJobs::Orphans::RubyReaper#queues_very_full?
- SidekiqUniqueJobs::Profiler#self.stop
- SidekiqUniqueJobs::Script::Caller#call_script
- SidekiqUniqueJobs::Script::Caller#extract_args
Expand Down
21 changes: 21 additions & 0 deletions lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class RubyReaper < Reaper
# @return [String] the suffix for :RUN locks
RUN_SUFFIX = ":RUN"
#
# @return [Integer] the maximum combined length of sidekiq queues for running the reaper
MAX_QUEUE_LENGTH = 1000
mhenrixon marked this conversation as resolved.
Show resolved Hide resolved
#
# @!attribute [r] digests
# @return [SidekiqUniqueJobs::Digests] digest collection
attr_reader :digests
Expand Down Expand Up @@ -46,6 +49,8 @@ def initialize(conn)
# @return [Integer] the number of reaped locks
#
def call
return if queues_very_full?

BatchDelete.call(orphans, conn)
end

Expand Down Expand Up @@ -212,6 +217,22 @@ def entries(conn, queue, &block) # rubocop:disable Metrics/MethodLength
end
end

# If sidekiq queues are very full, it becomes highly inefficient for the reaper
# because it must check every queued job to verify a digest is safe to delete
# The reaper checks queued jobs in batches of 50, adding 2 reads per digest
# With a queue length of 1,000 jobs, that's over 20 extra reads per digest.
def queues_very_full?
total_queue_size = 0
Sidekiq.redis do |conn|
queues(conn) do |queue|
total_queue_size += conn.llen("queue:#{queue}")

return true if total_queue_size > MAX_QUEUE_LENGTH
end
end
false
end

#
# Checks a sorted set for the existance of this digest
#
Expand Down
12 changes: 12 additions & 0 deletions spec/sidekiq_unique_jobs/orphans/ruby_reaper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,16 @@
end
end
end

describe "#call" do
before do
stub_const("SidekiqUniqueJobs::Orphans::RubyReaper::MAX_QUEUE_LENGTH", 3)
4.times { push_item(item) }
end
it 'quits early if sidekiq queues are very full' do
expect(service).not_to receive(:orphans)

service.call
end
end
end