-
-
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
Add digest scores for faster deletes in sorted sets #835
Merged
mhenrixon
merged 2 commits into
mhenrixon:main
from
ezekg:feature/add-score-to-unique-jobs-for-sorted-sets
Feb 22, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
20 changes: 10 additions & 10 deletions
20
lib/sidekiq_unique_jobs/lua/shared/_delete_from_queue.lua
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 |
---|---|---|
@@ -1,22 +1,22 @@ | ||
local function delete_from_queue(queue, digest) | ||
local per = 50 | ||
local total = redis.call("LLEN", queue) | ||
local index = 0 | ||
local result = nil | ||
local total = redis.call("LLEN", queue) | ||
local per = 50 | ||
|
||
for index = 0, total, per do | ||
local items = redis.call("LRANGE", queue, index, index + per - 1) | ||
|
||
while (index < total) do | ||
local items = redis.call("LRANGE", queue, index, index + per -1) | ||
if #items == 0 then | ||
break | ||
end | ||
|
||
for _, item in pairs(items) do | ||
if string.find(item, digest) then | ||
redis.call("LREM", queue, 1, item) | ||
result = item | ||
break | ||
|
||
return item | ||
end | ||
end | ||
index = index + per | ||
end | ||
return result | ||
|
||
return nil | ||
end |
30 changes: 20 additions & 10 deletions
30
lib/sidekiq_unique_jobs/lua/shared/_delete_from_sorted_set.lua
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 |
---|---|---|
@@ -1,19 +1,29 @@ | ||
local function delete_from_sorted_set(name, digest) | ||
local per = 50 | ||
local total = redis.call("zcard", name) | ||
local index = 0 | ||
local result | ||
local score = redis.call("ZSCORE", "uniquejobs:digests", digest) | ||
local total = redis.call("ZCARD", name) | ||
local per = 50 | ||
|
||
for offset = 0, total, per do | ||
local items | ||
|
||
if score then | ||
items = redis.call("ZRANGE", name, score, "+inf", "BYSCORE", "LIMIT", offset, per) | ||
else | ||
items = redis.call("ZRANGE", name, offset, offset + per -1) | ||
end | ||
|
||
if #items == 0 then | ||
break | ||
end | ||
|
||
while (index < total) do | ||
local items = redis.call("ZRANGE", name, index, index + per -1) | ||
for _, item in pairs(items) do | ||
if string.find(item, digest) then | ||
redis.call("ZREM", name, item) | ||
result = item | ||
break | ||
|
||
return item | ||
ezekg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
index = index + per | ||
end | ||
return result | ||
|
||
return nil | ||
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
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe UniqueJobOnConflictReplace, :perf do | ||
let(:lock_prefix) { described_class.sidekiq_options.fetch("lock_prefix") { SidekiqUniqueJobs.config.lock_prefix } } | ||
let(:lock_timeout) { described_class.sidekiq_options.fetch("lock_timeout") { SidekiqUniqueJobs.config.lock_timeout } } | ||
let(:lock_ttl) { described_class.sidekiq_options.fetch("lock_ttl") { SidekiqUniqueJobs.config.lock_ttl } } | ||
let(:queue) { described_class.sidekiq_options["queue"] } | ||
let(:on_conflict) { described_class.sidekiq_options["on_conflict"] } | ||
let(:lock) { described_class.sidekiq_options["lock"] } | ||
|
||
before do | ||
flushdb | ||
end | ||
|
||
context "when schedule queue is large" do | ||
it "locks and replaces quickly" do | ||
(0..100_000).each_slice(1_000) do |nums| | ||
redis do |conn| | ||
conn.pipelined do |pipeline| | ||
nums.each do |num| | ||
created_at = Time.now.to_f | ||
scheduled_at = created_at + rand(3_600..2_592_000) | ||
|
||
payload = { | ||
"retry" => true, | ||
"queue" => queue, | ||
"lock" => lock, | ||
"on_conflict" => on_conflict, | ||
"class" => described_class.name, | ||
"args" => [num, { "type" => "extremely unique" }], | ||
"jid" => SecureRandom.hex(12), | ||
"created_at" => created_at, | ||
"lock_timeout" => lock_timeout, | ||
"lock_ttl" => lock_ttl, | ||
"lock_prefix" => lock_prefix, | ||
"lock_args" => [num, { "type" => "extremely unique" }], | ||
"lock_digest" => "#{lock_prefix}:#{SecureRandom.hex}", | ||
} | ||
|
||
pipeline.zadd("schedule", scheduled_at, payload.to_json) | ||
end | ||
end | ||
end | ||
end | ||
|
||
# queueing it once at the end of the queue should succeed | ||
expect(described_class.perform_in(2_592_000, 100_000, { "type" => "extremely unique" })).not_to be_nil | ||
|
||
# queueing it again should be performant | ||
expect do | ||
described_class.perform_in(2_592_000, 100_000, { "type" => "extremely unique" }) | ||
end.to perform_under(10).ms | ||
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
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
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.
I left this alone because I don't fully understand what the expiring digests set is used for and if this optimization would be applicable to jobs utilizing an
until_expired
lock strategy.