Skip to content

Commit

Permalink
Prevent 500 on parallel org quota creation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kathap committed Aug 8, 2024
1 parent d543a66 commit c858936
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/models/runtime/quota_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class QuotaDefinition < Sequel::Model
:app_instance_limit, :app_task_limit, :total_service_keys, :total_reserved_route_ports,
:log_rate_limit

def around_save
yield
rescue Sequel::UniqueConstraintViolation => e
raise e unless e.message.include?('qd_name_index')

errors.add(:name, :unique)
raise validation_failed_error
end

def validate
validates_presence :name
validates_unique :name
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/actions/organization_quotas_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ module VCAP::CloudController
end
end

context 'when creating organization quotas concurrently' do
let(:name) { 'awesome' }
let(:message) { VCAP::CloudController::OrganizationQuotasCreateMessage.new(name:) }

it 'ensures one creation is successful and the other fails due to name conflict' do
# First request, should succeed
expect do
org_quotas_create.create(message)
end.not_to raise_error

# Mock the validation for the second request to simulate the race condition and trigger a unique constraint violation
allow_any_instance_of(QuotaDefinition).to receive(:validate).and_return(true)

# Second request, should fail with correct error
expect do
org_quotas_create.create(message)
end.to raise_error(OrganizationQuotasCreate::Error, "Organization Quota 'awesome' already exists.")
end
end

context 'when the org guid is invalid' do
let(:invalid_org_guid) { 'invalid_org_guid' }
let(:message_with_invalid_org_guid) do
Expand Down

0 comments on commit c858936

Please sign in to comment.