Skip to content

Commit 8b2a87b

Browse files
rosaclaude
andcommitted
Let the job answer for the concurrency slot it holds
concurrency_limited? now just means the job has a concurrency key, as on the Active Job side, and the code checks for a missing class where that matters. The job exposes the semaphore it got its slot from and the limit it got it with, so the semaphore asks for those instead of reading the recorded data itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 310496a commit 8b2a87b

6 files changed

Lines changed: 81 additions & 66 deletions

File tree

‎app/models/solid_queue/job/concurrency_controls.rb‎

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ module ConcurrencyControls
1111
included do
1212
has_one :blocked_execution
1313

14-
delegate :concurrency_limit, to: :job_class
15-
16-
before_destroy :unblock_next_blocked_job, if: -> { concurrency_key.present? && ready? }
14+
before_destroy :unblock_next_blocked_job, if: -> { concurrency_limited? && ready? }
1715
end
1816

1917
class_methods do
@@ -28,33 +26,39 @@ def unblock_next_blocked_job
2826
end
2927
end
3028

29+
def acquire_concurrency_lock
30+
return true unless concurrency_limited?
31+
32+
if job_class.nil?
33+
# A job whose class doesn't load skips its limits. Record that it holds no slot,
34+
# so it doesn't give one back if it runs somewhere its class does load.
35+
record_concurrency_lock(semaphore_id: nil)
36+
elsif semaphore_id = Semaphore.wait(self)
37+
record_concurrency_lock(semaphore_id:, limit: concurrency_limit)
38+
end
39+
end
40+
3141
def concurrency_limited?
32-
concurrency_key.present? && job_class.present?
42+
concurrency_key.present?
3343
end
3444

35-
def concurrency_duration
36-
job_class ? job_class.concurrency_duration : SolidQueue.default_concurrency_control_period
45+
# A job that kept its concurrency key after its class dropped limits_concurrency gets a limit of 1
46+
def concurrency_limit
47+
job_class.concurrency_limit || 1 if concurrency_limited?
3748
end
3849

39-
def acquire_concurrency_lock
40-
if concurrency_limited?
41-
if semaphore_id = Semaphore.wait(self)
42-
record_concurrency_lock(semaphore_id:, limit: concurrency_limit || 1)
43-
end
44-
else
45-
# A job whose class doesn't load skips its limits. Record that it holds no slot,
46-
# so it doesn't give one back if it runs somewhere its class does load.
47-
record_concurrency_lock(semaphore_id: nil) if concurrency_key.present?
48-
true
49-
end
50+
def concurrency_duration
51+
job_class ? job_class.concurrency_duration : SolidQueue.default_concurrency_control_period
5052
end
5153

52-
def concurrency_lock
53-
arguments[CONCURRENCY_LOCK_KEY]
54+
# The semaphore the job got its concurrency slot from
55+
def acquired_semaphore_id
56+
concurrency_lock&.dig("semaphore_id")
5457
end
5558

56-
def concurrency_lock_recorded?
57-
arguments.key?(CONCURRENCY_LOCK_KEY)
59+
# The concurrency limit the job had when it got its slot
60+
def acquired_concurrency_limit
61+
concurrency_lock&.dig("limit")
5862
end
5963

6064
def blocked?
@@ -66,8 +70,25 @@ def job_class
6670
end
6771

6872
private
69-
def concurrency_on_conflict
70-
job_class.concurrency_on_conflict.to_s.inquiry
73+
def release_concurrency_lock
74+
holds_concurrency_slot? && Semaphore.signal(self)
75+
end
76+
77+
def holds_concurrency_slot?
78+
if concurrency_lock_recorded?
79+
acquired_semaphore_id.present?
80+
else
81+
# Jobs dispatched before Solid Queue recorded their semaphore, which need their class to know their limit
82+
concurrency_limited? && job_class.present?
83+
end
84+
end
85+
86+
def concurrency_lock_recorded?
87+
arguments.key?(CONCURRENCY_LOCK_KEY)
88+
end
89+
90+
def release_next_blocked_job
91+
BlockedExecution.release_one(concurrency_key)
7192
end
7293

7394
def record_concurrency_lock(semaphore_id:, limit: nil)
@@ -76,13 +97,8 @@ def record_concurrency_lock(semaphore_id:, limit: nil)
7697
true
7798
end
7899

79-
def release_concurrency_lock
80-
if concurrency_lock_recorded?
81-
concurrency_lock["semaphore_id"].present? && Semaphore.signal(self)
82-
else
83-
# Jobs dispatched before Solid Queue recorded their semaphore
84-
concurrency_limited? && Semaphore.signal(self)
85-
end
100+
def concurrency_lock
101+
arguments[CONCURRENCY_LOCK_KEY]
86102
end
87103

88104
def handle_concurrency_conflict
@@ -93,12 +109,12 @@ def handle_concurrency_conflict
93109
end
94110
end
95111

96-
def block
97-
BlockedExecution.create_or_find_by!(job: self)
112+
def concurrency_on_conflict
113+
job_class.concurrency_on_conflict.to_s.inquiry
98114
end
99115

100-
def release_next_blocked_job
101-
BlockedExecution.release_one(concurrency_key)
116+
def block
117+
BlockedExecution.create_or_find_by!(job: self)
102118
end
103119

104120
def execution

‎app/models/solid_queue/job/executable.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ def prepare_all_for_execution(jobs)
2626
end
2727

2828
def dispatch_all(jobs)
29-
# Jobs with a concurrency key go one by one, so they record the slot they get, or that they skipped it
30-
with_concurrency_limits, without_concurrency_limits = jobs.partition { |job| job.concurrency_key.present? }
29+
with_concurrency_limits, without_concurrency_limits = jobs.partition(&:concurrency_limited?)
3130

3231
dispatch_all_at_once(without_concurrency_limits)
3332
dispatch_all_one_by_one(with_concurrency_limits)

‎app/models/solid_queue/semaphore.rb‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def signal
4747
private
4848
attr_accessor :job
4949

50+
def attempt_decrement
51+
Semaphore.available.where(key: key).update_all([ "value = value - 1, expires_at = ?", expires_at ]) > 0
52+
end
53+
5054
def attempt_creation
5155
if Semaphore.create_unique_by(key: key, value: limit - 1, expires_at: expires_at)
5256
acquired_semaphore_id
@@ -55,35 +59,36 @@ def attempt_creation
5559
end
5660
end
5761

58-
def check_limit_or_decrement
59-
acquired_semaphore_id if limit > 1 && attempt_decrement
60-
end
61-
6262
# A locking read, so it sees the latest committed rows rather than the transaction's
6363
# snapshot, which on MySQL can still include a semaphore that expired and was deleted
6464
def acquired_semaphore_id
6565
Semaphore.lock.where(key: key).pick(:id)
6666
end
6767

68-
def attempt_decrement
69-
Semaphore.available.where(key: key).update_all([ "value = value - 1, expires_at = ?", expires_at ]) > 0
68+
def check_limit_or_decrement
69+
acquired_semaphore_id if limit > 1 && attempt_decrement
7070
end
7171

7272
def attempt_increment
73-
semaphore_to_release.where(value: ...limit_for_release).update_all([ "value = value + 1, expires_at = ?", expires_at ]) > 0
73+
semaphore_to_release.where(value: ...limit_to_release).update_all([ "value = value + 1, expires_at = ?", expires_at ]) > 0
7474
end
7575

7676
# A job only gives its slot back to the semaphore it got it from. If that one expired
7777
# and was deleted, a semaphore for the same key now belongs to other jobs.
7878
# Jobs dispatched before Solid Queue recorded their semaphore give it back by key.
7979
def semaphore_to_release
80-
if job.concurrency_lock_recorded?
81-
Semaphore.where(id: job.concurrency_lock["semaphore_id"])
80+
if job.acquired_semaphore_id
81+
Semaphore.where(id: job.acquired_semaphore_id)
8282
else
8383
Semaphore.where(key: key)
8484
end
8585
end
8686

87+
# Recorded when the job got its slot, so it's known even if the job's class no longer loads
88+
def limit_to_release
89+
job.acquired_concurrency_limit || limit
90+
end
91+
8792
def key
8893
job.concurrency_key
8994
end
@@ -93,12 +98,7 @@ def expires_at
9398
end
9499

95100
def limit
96-
job.concurrency_limit || 1
97-
end
98-
99-
# Recorded when the job got its slot, so it's known even if the job's class no longer loads
100-
def limit_for_release
101-
job.concurrency_lock&.dig("limit") || limit
101+
job.concurrency_limit
102102
end
103103
end
104104
end

‎test/models/solid_queue/claimed_execution_test.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
170170
test "dispatch job with missing class and concurrency key skips concurrency controls" do
171171
job = create_job_with_missing_class(concurrency_key: "test_key")
172172

173-
assert_not job.concurrency_limited?
173+
assert_nil job.job_class
174174

175175
job.prepare_for_execution
176176

‎test/models/solid_queue/concurrency_lock_test.rb‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,33 @@ def perform(name)
9494
test "a blocked job records the semaphore it gets when it's released" do
9595
a = claim(NonOverlappingJob.perform_later("A"))
9696
b = job_for(NonOverlappingJob.perform_later("B"))
97-
assert a.job.concurrency_lock["semaphore_id"].present?
98-
assert_not b.concurrency_lock_recorded?
97+
assert a.job.acquired_semaphore_id.present?
98+
assert_nil b.acquired_semaphore_id
9999

100100
a.perform
101101

102102
assert b.reload.ready?
103-
assert_equal semaphore.id, b.concurrency_lock["semaphore_id"]
104-
assert_equal 1, b.concurrency_lock["limit"]
103+
assert_equal semaphore.id, b.acquired_semaphore_id
104+
assert_equal 1, b.acquired_concurrency_limit
105105
end
106106

107107
test "jobs enqueued in bulk record the semaphore they get" do
108108
ActiveJob.perform_all_later([ NonOverlappingJob.new("A"), NonOverlappingJob.new("B") ])
109109
a, b = SolidQueue::Job.where(class_name: NonOverlappingJob.name).order(:id).to_a
110110

111111
assert a.ready?
112-
assert_equal semaphore.id, a.concurrency_lock["semaphore_id"]
112+
assert_equal semaphore.id, a.acquired_semaphore_id
113113
assert b.blocked?
114-
assert_not b.concurrency_lock_recorded?
114+
assert_nil b.acquired_semaphore_id
115115
end
116116

117117
test "jobs sharing a semaphore give their slots back to it" do
118118
a = claim(TwoAtATimeJob.perform_later("A"))
119119
b = claim(TwoAtATimeJob.perform_later("B"))
120120
c = job_for(TwoAtATimeJob.perform_later("C"))
121121

122-
assert_equal [ semaphore.id ] * 2, [ a, b ].map { |execution| execution.job.concurrency_lock["semaphore_id"] }
123-
assert_equal 2, a.job.concurrency_lock["limit"]
122+
assert_equal [ semaphore.id ] * 2, [ a, b ].map { |execution| execution.job.acquired_semaphore_id }
123+
assert_equal 2, a.job.acquired_concurrency_limit
124124
assert c.blocked?
125125

126126
a.perform
@@ -150,23 +150,23 @@ def perform(name)
150150
test "a retried job records the semaphore it gets on the retry" do
151151
a = claim(NonOverlappingJob.perform_later("A"))
152152
a.failed_with(ExpectedTestError.new)
153-
first_semaphore_id = a.job.reload.concurrency_lock["semaphore_id"]
153+
first_semaphore_id = a.job.reload.acquired_semaphore_id
154154

155155
travel_to 2.minutes.from_now
156156
SolidQueue::Semaphore.expired.delete_all
157157

158158
a.job.failed_execution.retry
159159

160160
assert a.job.reload.ready?
161-
assert_not_equal first_semaphore_id, a.job.concurrency_lock["semaphore_id"]
162-
assert_equal semaphore.id, a.job.concurrency_lock["semaphore_id"]
161+
assert_not_equal first_semaphore_id, a.job.acquired_semaphore_id
162+
assert_equal semaphore.id, a.job.acquired_semaphore_id
163163
end
164164

165165
test "a job records the limit it got its slot with, even when its class doesn't set one" do
166166
# A job that kept its concurrency key after its class dropped limits_concurrency
167167
a = SolidQueue::Job.create!(queue_name: "default", class_name: AddToBufferJob.name, active_job_id: SecureRandom.uuid,
168168
arguments: { "job_class" => AddToBufferJob.name, "arguments" => [ 1 ] }, concurrency_key: "concurrency-lock-test")
169-
assert_equal 1, a.concurrency_lock["limit"]
169+
assert_equal 1, a.acquired_concurrency_limit
170170

171171
# ...and then runs where the class is missing
172172
a.update_columns(class_name: "RemovedJobClass")

‎test/models/solid_queue/semaphore_test.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def perform(key)
3434
job = SolidQueue::Job.enqueue(NonOverlappingUpdateResultJob.new(@result))
3535

3636
assert job.ready?
37-
assert_not_equal expired.id, job.concurrency_lock["semaphore_id"]
38-
assert_equal SolidQueue::Semaphore.lock.find_by!(key: job.concurrency_key).id, job.concurrency_lock["semaphore_id"]
37+
assert_not_equal expired.id, job.acquired_semaphore_id
38+
assert_equal SolidQueue::Semaphore.lock.find_by!(key: job.concurrency_key).id, job.acquired_semaphore_id
3939
end
4040
end
4141

0 commit comments

Comments
 (0)