-
-
Notifications
You must be signed in to change notification settings - Fork 279
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
Reduce reaper threads #576
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e5bb177
Reduce the number of threads used for reaping
mhenrixon 089b773
Fix specs
mhenrixon bcd09d6
Fix specs
mhenrixon c4b1a1e
Fix Gemfiles
mhenrixon 4126988
Mandatory rubocop commit
mhenrixon 63fcee2
Reduce performance impact of method as block
mhenrixon 5b04e1f
Smelly
mhenrixon 3de6ed9
Update README to a more sensible default
mhenrixon 2e0867b
Reduce overhead of schedule_next_task as well
mhenrixon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# Trap interrupts to quit cleanly. See | ||
# https://twitter.com/mitchellh/status/283014103189053442 | ||
Signal.trap("INT") { abort } | ||
|
||
require "bundler/setup" | ||
require "ruby-prof" | ||
require "sidekiq-unique-jobs" | ||
|
||
SidekiqUniqueJobs.configure do |config| | ||
config.reaper_interval = 2 | ||
config.reaper_timeout = 1 | ||
config.reaper_count = 10_000 | ||
end | ||
|
||
TASK = SidekiqUniqueJobs::TimerTask.new(SidekiqUniqueJobs::Orphans::Manager.timer_task_options) do | ||
SidekiqUniqueJobs::Orphans::Manager.with_logging_context do | ||
SidekiqUniqueJobs::Orphans::Manager.redis do |_conn| | ||
SidekiqUniqueJobs::Orphans::Manager.refresh_reaper_mutex | ||
sleep(1) | ||
end | ||
end | ||
end | ||
|
||
counter = 0 | ||
result = RubyProf.profile do | ||
100.times do | ||
SidekiqUniqueJobs::Orphans::Manager.start(TASK) | ||
end | ||
|
||
while counter < 60 | ||
sleep(1) | ||
|
||
counter += 1 | ||
end | ||
end | ||
|
||
result.exclude_common_methods! | ||
printer = RubyProf::GraphPrinter.new(result) | ||
printer.print($stdout, min_percent: 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
module SidekiqUniqueJobs | ||
# @see [Concurrent::TimerTask] https://www.rubydoc.info/gems/concurrent-ruby/Concurrent/TimerTask | ||
# | ||
class TimerTask < ::Concurrent::TimerTask | ||
private | ||
|
||
def ns_initialize(opts, &task) | ||
set_deref_options(opts) | ||
|
||
self.execution_interval = opts[:execution] || opts[:execution_interval] || EXECUTION_INTERVAL | ||
self.timeout_interval = opts[:timeout] || opts[:timeout_interval] || TIMEOUT_INTERVAL | ||
@run_now = opts[:now] || opts[:run_now] | ||
@executor = Concurrent::RubySingleThreadExecutor.new | ||
@running = Concurrent::AtomicBoolean.new(false) | ||
@task = task | ||
@value = nil | ||
|
||
self.observers = Concurrent::Collection::CopyOnNotifyObserverSet.new | ||
end | ||
|
||
def schedule_next_task(interval = execution_interval) | ||
exec_task = ->(completion) { execute_task(completion) } | ||
ScheduledTask.execute(interval, args: [Concurrent::Event.new], &exec_task) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should there be prefix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added fix in this PR #577 |
||
nil | ||
end | ||
|
||
# @!visibility private | ||
def execute_task(completion) # rubocop:disable Metrics/MethodLength | ||
return nil unless @running.true? | ||
|
||
timeout_task = -> { timeout_task(completion) } | ||
|
||
Concurrent::ScheduledTask.execute( | ||
timeout_interval, | ||
args: [completion], | ||
&timeout_task | ||
) | ||
@thread_completed = Concurrent::Event.new | ||
|
||
@value = @reason = nil | ||
@executor.post do | ||
@value = @task.call(self) | ||
rescue Exception => ex # rubocop:disable Lint/RescueException | ||
@reason = ex | ||
ensure | ||
@thread_completed.set | ||
end | ||
|
||
@thread_completed.wait | ||
|
||
if completion.try? | ||
schedule_next_task | ||
time = Time.now | ||
observers.notify_observers do | ||
[time, value, @reason] | ||
end | ||
end | ||
nil | ||
end | ||
|
||
# @!visibility private | ||
def timeout_task(completion) | ||
return unless @running.true? | ||
return unless completion.try? | ||
|
||
@executor.kill | ||
@executor.wait_for_termination | ||
@executor = Concurrent::RubySingleThreadExecutor.new | ||
|
||
@thread_completed.set | ||
|
||
schedule_next_task | ||
observers.notify_observers(Time.now, nil, Concurrent::TimeoutError.new) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
150/60 = 2.5 minutes
I keep in my own config 595 seconds as timeout. It's close to interval 600s.