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

Fixes #224. I believe this is a Postgres only issue #231

Merged
merged 4 commits into from
Aug 6, 2024
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
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ GEM

PLATFORMS
arm64-darwin-22
arm64-darwin-23
x86_64-darwin-21
x86_64-darwin-23
x86_64-linux
Expand Down
22 changes: 17 additions & 5 deletions app/models/solid_queue/semaphore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ def signal(job)
def signal_all(jobs)
Proxy.signal_all(jobs)
end

# Requires a unique index on key
def create_unique_by(attributes)
if connection.supports_insert_conflict_target?
insert({ **attributes }, unique_by: :key).any?
else
create!(**attributes)
end
rescue ActiveRecord::RecordNotUnique
false
end
end

class Proxy
Expand All @@ -41,18 +52,19 @@ def signal
end

private

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this project I'm following the style of not having a space after private when the class has public and private methods, so this one was on purpose 😅

attr_accessor :job

def attempt_creation
Semaphore.create!(key: key, value: limit - 1, expires_at: expires_at)
true
rescue ActiveRecord::RecordNotUnique
if limit == 1 then false
if Semaphore.create_unique_by(key: key, value: limit - 1, expires_at: expires_at)
true
else
attempt_decrement
check_limit_or_decrement
end
end

def check_limit_or_decrement = limit == 1 ? false : attempt_decrement

def attempt_decrement
Semaphore.available.where(key: key).update_all([ "value = value - 1, expires_at = ?", expires_at ]) > 0
end
Expand Down
15 changes: 15 additions & 0 deletions test/integration/concurrency_controls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,22 @@ class ConcurrencyControlsTest < ActiveSupport::TestCase
assert job.reload.ready?
end

test "verify transactions remain valid after Job creation conflicts via limits_concurrency" do
ActiveRecord::Base.transaction do
SequentialUpdateResultJob.perform_later(@result, name: "A", pause: 0.2.seconds)
SequentialUpdateResultJob.perform_later(@result, name: "B")

begin
assert_equal 2, SolidQueue::Job.count
assert true, "Transaction state valid"
rescue ActiveRecord::StatementInvalid
assert false, "Transaction state unexpectedly invalid"
end
end
end
Copy link
Member

@rosa rosa Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... actually, I'm not completely sure if we need this test at all, since it seems it's testing more something internal of Active Record, rather than something we have in Solid Queue 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this really is more of a test of AR, its value is that we have a confirmation of outcomes with the two different ways to add records to the Semaphore table and we show the two mechanisms "side-by-side". I realized that it was missing an assert to meet that goal. See what you think with the extra line.

In the end, if you still feel strongly about cleaning this test up a little, I'll remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! I don't feel strongly about this one 😊


private

def assert_stored_sequence(result, *sequences)
expected = sequences.map { |sequence| "seq: " + sequence.map { |name| "s#{name}c#{name}" }.join }
skip_active_record_query_cache do
Expand Down
Loading