Skip to content

Commit 14d72af

Browse files
authored
Prevent 500 on parallel org quota creation (#3924)
To apply this change the migration from #3923 needs to be in first. Tha-s why we ceeated the adoption to prevetn 500 on concurrent requests for organization_quota_create separately. The reason is the same as in #3899 and #3918.
1 parent 1123005 commit 14d72af

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

app/models/runtime/quota_definition.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ class QuotaDefinition < Sequel::Model
1919
:app_instance_limit, :app_task_limit, :total_service_keys, :total_reserved_route_ports,
2020
:log_rate_limit
2121

22+
def around_save
23+
yield
24+
rescue Sequel::UniqueConstraintViolation => e
25+
raise e unless e.message.include?('qd_name_index')
26+
27+
errors.add(:name, :unique)
28+
raise validation_failed_error
29+
end
30+
2231
def validate
2332
validates_presence :name
2433
validates_unique :name

spec/unit/actions/organization_quotas_create_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ module VCAP::CloudController
132132
end
133133
end
134134

135+
context 'when creating organization quotas concurrently' do
136+
let(:name) { 'awesome' }
137+
let(:message) { VCAP::CloudController::OrganizationQuotasCreateMessage.new(name:) }
138+
139+
it 'ensures one creation is successful and the other fails due to name conflict' do
140+
# First request, should succeed
141+
expect do
142+
org_quotas_create.create(message)
143+
end.not_to raise_error
144+
145+
# Mock the validation for the second request to simulate the race condition and trigger a unique constraint violation
146+
allow_any_instance_of(QuotaDefinition).to receive(:validate).and_return(true)
147+
148+
# Second request, should fail with correct error
149+
expect do
150+
org_quotas_create.create(message)
151+
end.to raise_error(OrganizationQuotasCreate::Error, "Organization Quota 'awesome' already exists.")
152+
end
153+
end
154+
135155
context 'when the org guid is invalid' do
136156
let(:invalid_org_guid) { 'invalid_org_guid' }
137157
let(:message_with_invalid_org_guid) do

0 commit comments

Comments
 (0)