sidekiq-fast-enq 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 40869777a1a6d37a3e5eab0b5f020934a49aef19
4
+ data.tar.gz: 18d7f23470bed6ff4c82dbd63b3f6476bec77fea
5
+ SHA512:
6
+ metadata.gz: 39c51cd9e46ec73bda022e0486fb18383012833d6427b8772af8c091e412ff0618788488a20f0771184be3d0419b2940585c3fc951f11a4f94be7addfdef8ede
7
+ data.tar.gz: c0d00501fc81d12b4bbf4e3bb8a839ac4e0a2b55a85d1f6ba8de7237e4f3a20b733b9b6df64b4986344394ea3dd13d639d03380a76c545890d34a38786a10b12
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 WHI, Inc.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ This gem provides a much more efficien implementation for checking the Sidekiq scheduled and retry queues. This can provide a significant performance boost for large sidekiq implementations that utilize many processes. It can also reduce load on the redis server.
2
+
3
+ ### TL;DR
4
+
5
+ The default implementation included with the sidekiq gem works very well when there are only a few processes running. However, with a large number of processes it is vulnerable to race conditions when there are a lot of scheduled jobs and a lot of sidekiq processes. Each process will maintain a thread that checks the scheduled and retry job queues. These queues are kept in sorted sets with the timestamp to run the jobs as the sorting key. Each process will run a redis command to sort the set and return the first element whose key is less than the current timestamp. It then removes the job from the scheduled queue and adds it to the appropriate job queue. The problem is that if there are a lot schedule jobs, the set sorting operation can take a not insignificant amount of time. If there are lot of processes this will lead to a lot of race conditions where for each job run the large set will be sorted many times.
6
+
7
+ This gem re-implements the same logic, but using a server side Lua script so that the sorting and popping from the list become an atomic operation eliminating the race conditions as well as using more efficient redis commands.
8
+
9
+ On a single sidekiq process this implementation is about twice as fast. With 64 processes it's about nine times as fast and puts significantly less load on redis.
10
+
11
+ This plugin does not alter any sidekiq internal code or data structures.
12
+
13
+ ### Usage
14
+
15
+ In your sidekiq configuration you need to set the `:scheduled_enq` option to `SidekiqFastEnq` (only available in sidekiq 3.4.0 and later). You might also want to hard code a value for the `:poll_interval_average` option as well. If this option is not set the polling interval for checking the scheduled queues is based on the number of processes in an effort to reduce the effects of the race condition. It is not needed with this code and scheduled jobs will be enqueued closer to their scheduled time without it.
16
+
17
+ ```ruby
18
+ Sidekiq.options[:scheduled_enq] = SidekiqFastEnq
19
+ Sidekiq.options[:poll_interval_average] = 30
20
+ ```
21
+
22
+ Since this gem eliminates the race condition of having too many processes processing th
23
+ Note: this gem utilizes server side Lua scripting so you must be using Redis Server 2.6.0 or later.
@@ -0,0 +1,57 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Default: run unit tests.'
4
+ task :default => :test
5
+
6
+ desc 'RVM likes to call it tests'
7
+ task :tests => :test
8
+
9
+ begin
10
+ require 'rspec'
11
+ require 'rspec/core/rake_task'
12
+ desc 'Run the unit tests'
13
+ RSpec::Core::RakeTask.new(:test)
14
+ rescue LoadError
15
+ task :test do
16
+ STDERR.puts "You must have rspec 2.0 installed to run the tests"
17
+ end
18
+ end
19
+
20
+ task :load_test, [:jobs_size, :workers, :fast] do |t, args|
21
+ require 'celluloid'
22
+ require File.expand_path('../lib/sidekiq-fast-enq', __FILE__)
23
+ require 'sidekiq/scheduled'
24
+ require 'sidekiq/api'
25
+
26
+ class FastEnqLoadTestWorker
27
+ include Sidekiq::Worker
28
+ def perform()
29
+ end
30
+ end
31
+
32
+ jobs_size = args[:jobs_size].to_i
33
+ workers_size = args[:workers].to_i
34
+ klass = (args[:fast] == 'fast' ? SidekiqFastEnq : Sidekiq::Scheduled::Enq)
35
+
36
+ Sidekiq.configure_server do |config|
37
+ config.redis = {:namespace => "sidekiq_fast_enq_load_test"}
38
+ end
39
+
40
+ Sidekiq::ScheduledSet.new.clear
41
+ jobs_size.times do
42
+ FastEnqLoadTestWorker.perform_in(rand)
43
+ end
44
+
45
+ t = Time.now
46
+ workers_size.times do
47
+ fork do
48
+ klass.new.enqueue_jobs
49
+ end
50
+ end
51
+
52
+ workers_size.times do
53
+ Process.wait
54
+ end
55
+
56
+ puts "Enqueued #{jobs_size} jobs in #{Time.now - t} seconds"
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,121 @@
1
+ require 'sidekiq'
2
+
3
+ # Implementation of the Sidekiq::Scheduled::Enq class that uses a server side Lua script
4
+ # to atomically get the next scheduled job to run and then pops it from the list. This
5
+ # works much better in large sidekiq deployments with many processes because it eliminates
6
+ # race conditions checking the scheduled queues.
7
+ class SidekiqFastEnq
8
+ DEFAULT_BATCH_SIZE = 1000
9
+
10
+ def initialize(batch_size = nil)
11
+ batch_size ||= (Sidekiq.options[:fast_enq_batch_size] || DEFAULT_BATCH_SIZE)
12
+ @script = lua_script(batch_size)
13
+ Sidekiq.redis do |conn|
14
+ @script_sha_1 = conn.script(:load, @script)
15
+ end
16
+ end
17
+
18
+ def enqueue_jobs(now = Time.now.to_f.to_s, sorted_sets = nil)
19
+ sorted_sets ||= Sidekiq::Scheduled::SETS
20
+ logger = Sidekiq::Logging.logger
21
+
22
+ # A job's "score" in Redis is the time at which it should be processed.
23
+ # Just check Redis for the set of jobs with a timestamp before now.
24
+ Sidekiq.redis do |conn|
25
+ namespace = conn.namespace if conn.respond_to?(:namespace)
26
+ sorted_sets.each do |sorted_set|
27
+ redis_set = (namespace ? "#{namespace}:#{sorted_set}" : sorted_set)
28
+ jobs_count = 0
29
+ start_time = Time.now
30
+ pop_time = 0.0
31
+ enqueue_time = 0.0
32
+
33
+ # Get the next item in the queue if it's score (time to execute) is <= now.
34
+ # We need to go through the list one at a time to reduce the risk of something
35
+ # going wrong between the time jobs are popped from the scheduled queue and when
36
+ # they are pushed onto a work queue and losing the jobs.
37
+ loop do
38
+ t = Time.now
39
+ job = pop_job(conn, redis_set, now)
40
+ pop_time += (Time.now - t)
41
+ break if job.nil?
42
+ t = Time.now
43
+ Sidekiq::Client.push(Sidekiq.load_json(job))
44
+ enqueue_time += (Time.now - t)
45
+ jobs_count += 1
46
+ logger.debug("enqueued #{sorted_set}: #{job}") if logger && logger.debug?
47
+ end
48
+
49
+ if jobs_count > 0 && logger && logger.info?
50
+ loop_time = Time.now - start_time
51
+ logger.info("SidekiqFastEnq enqueued #{jobs_count} from #{sorted_set} in #{loop_time.round(3)}s (pop: #{pop_time.round(3)}s; enqueue: #{enqueue_time.round(3)}s)")
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ # Invoke a Lua script on the server to pop the next job from a sorted set that should have
60
+ # been run before "now".
61
+ def pop_job(conn, sorted_set, now)
62
+ eval_script(conn, @script, @script_sha_1, [sorted_set, now])
63
+ end
64
+
65
+ # Evaluate and execute a Lua script on the redis server.
66
+ def eval_script(conn, script, sha1, argv=[])
67
+ begin
68
+ conn.evalsha(sha1, [], argv)
69
+ rescue Redis::CommandError => e
70
+ if e.message.include?('NOSCRIPT'.freeze)
71
+ t = Time.now
72
+ sha1 = conn.script(:load, script)
73
+ Sidekiq::Logging.logger.info("loaded script #{sha1} in #{Time.now - t}s")
74
+ retry
75
+ else
76
+ raise e
77
+ end
78
+ end
79
+ end
80
+
81
+ # Lua script that will atomically get the next element from the sorted set of scheduled jobs
82
+ # and pop it from the list.
83
+ def lua_script(batch_size)
84
+ batch_size = batch_size.to_i
85
+ batch_size = DEFAULT_BATCH_SIZE if batch_size <= 0
86
+ <<-LUA
87
+ local sorted_set = ARGV[1]
88
+ local now = tonumber(ARGV[2])
89
+ local ready_cache = sorted_set .. '.cache'
90
+
91
+ while true do
92
+ -- Check a cached list of jobs that are ready to execute
93
+ local job = redis.call('lpop', ready_cache)
94
+ if not job then
95
+ -- If no jobs in the cache then get the next 100 jobs ready to be executed
96
+ local ready_jobs = redis.call('zrangebyscore', sorted_set, '-inf', now, 'LIMIT', 0, #{batch_size})
97
+ if #ready_jobs == 1 then
98
+ job = ready_jobs[1]
99
+ elseif #ready_jobs > 1 then
100
+ -- If more than one job is ready, throw them in the cache which is faster to access than the sorted set
101
+ redis.call('rpush', ready_cache, unpack(ready_jobs))
102
+ -- Set an expiration on the cache since it's just a cache. The sorted set is still the canonical list.
103
+ redis.call('expire', ready_cache, 60)
104
+ job = redis.call('lpop', ready_cache)
105
+ end
106
+ end
107
+
108
+ if job then
109
+ -- Verify that the job was still in the sorted set when we remove. Could happen if
110
+ -- another sidekiq process is still using the standard Enq mechanism.
111
+ local removed = redis.call('zrem', sorted_set, job)
112
+ if removed > 0 then
113
+ return job
114
+ end
115
+ else
116
+ return nil
117
+ end
118
+ end
119
+ LUA
120
+ end
121
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sidekiq-fast-enq"
7
+ spec.version = File.read(File.expand_path("../VERSION", __FILE__)).chomp
8
+ spec.authors = ["We Heart It", "Brian Durand"]
9
+ spec.email = ["dev@weheartit.com", "bbdurand@gmail.com"]
10
+ spec.summary = "More efficient scheduled job queue implementation for sidekiq"
11
+ spec.description = "More efficient scheduled job queue implementation for sidekiq to increase throughput in large installations."
12
+ spec.homepage = "https://github.com/weheartit/sidekiq_fast_enq"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency('sidekiq', '~>3.4')
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "timecop"
26
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe SidekiqFastEnq do
4
+
5
+ let(:scheduled_set){ Sidekiq::ScheduledSet.new }
6
+ let(:retry_set){ Sidekiq::RetrySet.new }
7
+ let(:default_queue){ Sidekiq::Queue.new }
8
+
9
+ before :each do
10
+ scheduled_set.clear
11
+ retry_set.clear
12
+ default_queue.clear
13
+ end
14
+
15
+ it "should return without doing anything if there are no scheduled jobs" do
16
+ SidekiqFastEnq.new.enqueue_jobs
17
+ expect(scheduled_set.size).to eq(0)
18
+ expect(retry_set.size).to eq(0)
19
+ expect(default_queue.size).to eq(0)
20
+ end
21
+
22
+ it "should enqueue a single elligible job from the scheduled jobs queue" do
23
+ Timecop.travel(Time.now - 3600){ FastEnqTestWorker.perform_in(60, 'one') }
24
+ SidekiqFastEnq.new.enqueue_jobs
25
+ expect(scheduled_set.size).to eq(0)
26
+ expect(retry_set.size).to eq(0)
27
+ expect(default_queue.size).to eq(1)
28
+ end
29
+
30
+ it "should enqueue all elligible jobs from the scheduled jobs queue" do
31
+ Timecop.travel(Time.now - 3600){ FastEnqTestWorker.perform_in(60, 'one') }
32
+ Timecop.travel(Time.now - 3600){ FastEnqTestWorker.perform_in(900, 'two') }
33
+ FastEnqTestWorker.perform_in(10, 'three')
34
+ SidekiqFastEnq.new.enqueue_jobs
35
+ expect(scheduled_set.size).to eq(1)
36
+ expect(retry_set.size).to eq(0)
37
+ expect(default_queue.size).to eq(2)
38
+ end
39
+
40
+ it "should enqueue all elligible jobs from the scheduled jobs queue when there are a lot of them" do
41
+ Timecop.travel(Time.now - 3600) do
42
+ 200.times do
43
+ FastEnqTestWorker.perform_in(60, 'one')
44
+ end
45
+ end
46
+ FastEnqTestWorker.perform_in(10, 'three')
47
+ SidekiqFastEnq.new.enqueue_jobs
48
+ expect(scheduled_set.size).to eq(1)
49
+ expect(retry_set.size).to eq(0)
50
+ expect(default_queue.size).to eq(200)
51
+ end
52
+
53
+ end
@@ -0,0 +1,35 @@
1
+ # Breaks if not required. Sidekiq doesn't directly require in
2
+ # the load process.
3
+
4
+ sidekiq_version = Array(ENV["SIDEKIQ_VERSION"] || "~>3.0")
5
+ gem 'sidekiq', *sidekiq_version
6
+
7
+ require File.expand_path('../../lib/sidekiq-fast-enq', __FILE__)
8
+ require 'timecop'
9
+ require 'celluloid'
10
+ require 'sidekiq/scheduled'
11
+ require 'sidekiq/api'
12
+
13
+ RSpec.configure do |config|
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+
23
+ Sidekiq.configure_server do |config|
24
+ config.redis = {:namespace => "sidekiq_fast_enq_test"}
25
+ end
26
+ Sidekiq.options[:scheduled_enq] = SidekiqFastEnq
27
+ Sidekiq::Logging.logger = Logger.new(StringIO.new)
28
+ end
29
+
30
+ class FastEnqTestWorker
31
+ include Sidekiq::Worker
32
+
33
+ def perform(arg)
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-fast-enq
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - We Heart It
8
+ - Brian Durand
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-10-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sidekiq
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.4'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.4'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.3'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: timecop
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: More efficient scheduled job queue implementation for sidekiq to increase
85
+ throughput in large installations.
86
+ email:
87
+ - dev@weheartit.com
88
+ - bbdurand@gmail.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".gitignore"
94
+ - MIT_LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - VERSION
98
+ - lib/sidekiq-fast-enq.rb
99
+ - sidekiq_fast_enq.gemspec
100
+ - spec/sidekiq-fast-enq_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: https://github.com/weheartit/sidekiq_fast_enq
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.5
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: More efficient scheduled job queue implementation for sidekiq
126
+ test_files:
127
+ - spec/sidekiq-fast-enq_spec.rb
128
+ - spec/spec_helper.rb