Bug 1485942

Summary: duplicate key value violates unique constraint "index_katello_subscription_facets_on_user_id" on registering host
Product: Red Hat Satellite Reporter: Roman Plevka <rplevka>
Component: Subscription ManagementAssignee: Justin Sherrill <jsherril>
Status: CLOSED ERRATA QA Contact: Roman Plevka <rplevka>
Severity: high Docs Contact:
Priority: unspecified    
Version: UnspecifiedCC: bbuckingham, egolov, rplevka, shihliu
Target Milestone: UnspecifiedKeywords: Triaged
Target Release: Unused   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: rubygem-katello-3.0.0.152-1 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-09-25 18:59:44 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Roman Plevka 2017-08-28 13:41:39 UTC
Description of problem:
There seems to be yet another race condition, which occurs during registering a host and creating an org (or possibly even an user) simultaneously.
Any other registration attempt after this error is unsuccessful (it seems to generate the same, already existing id)


It looks like this started occurring with 6.2.12-1

Version-Release number of selected component (if applicable):
6.2.12-1

Steps to Reproduce:
1. Start registering a host (using satellite hammer: `hammer host subscription register --content-view-id="372" --organization-id="305" --name="hXFzBtcMHyhUroM" --lifecycle-environment-id="298")`
2. create an organization just after the step #1 (before it finishes)

Actual results:
2017-08-25 19:41:41 7e368fba [app] [I] Started POST "/api/hosts/subscriptions" for 127.0.0.1 at 2017-08-25 19:41:41 -0400
2017-08-25 19:41:41 7e368fba [app] [I] Processing by Katello::Api::V2::HostSubscriptionsController#create as JSON
2017-08-25 19:41:41 7e368fba [app] [I]   Parameters: {"name"=>"PdUZAiadiJHKLuq", "facts"=>"[FILTERED]", "lifecycle_environment_id"=>423, "content_view_id"=>470, "api_version"=>"v2", "host_subscription"=>{"name"=>"PdUZAiadiJHKLuq", "facts"=>"[FILTERED]", "lifecycle_environment_id"=>423, "content_view_id"=>470}}
2017-08-25 19:41:41 7e368fba [app] [I] Authorized user admin(Admin User
2017-08-25 19:41:46 7e368fba [audit] [I] [pduzaiadijhkluq] deleted 0 (4.8ms)
2017-08-25 19:41:46 7e368fba [audit] [I] [pduzaiadijhkluq] updated 1 (21.7ms)
2017-08-25 19:41:46 7e368fba [audit] [I] [pduzaiadijhkluq] added 0 (2.4ms)
2017-08-25 19:41:46 7e368fba [app] [I] Import facts for 'pduzaiadijhkluq' completed. Added: 0, Updated: 1, Deleted 0 facts
2017-08-25 19:41:46 7e368fba [app] [I]   Rendered /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.0.0.151/app/views/katello/api/v2/hosts/show.json.rabl within katello/api/v2/layouts/resource (22.6ms)
2017-08-25 19:41:46 7e368fba [app] [I] Completed 200 OK in 4812ms (Views: 30.9ms | ActiveRecord: 99.3ms)
...
2017-08-25 19:41:56 b6baa7e4 [app] [I] Started POST "/api/hosts/subscriptions" for 127.0.0.1 at 2017-08-25 19:41:56 -0400
2017-08-25 19:41:56 b6baa7e4 [app] [I] Processing by Katello::Api::V2::HostSubscriptionsController#create as JSON
2017-08-25 19:41:56 b6baa7e4 [app] [I]   Parameters: {"name"=>"GgFnZCmReSviXca", "facts"=>"[FILTERED]", "lifecycle_environment_id"=>421, "content_view_id"=>468, "api_version"=>"v2", "host_subscription"=>{"name"=>"GgFnZCmReSviXca", "facts"=>"[FILTERED]", "lifecycle_environment_id"=>421, "content_view_id"=>468}}
2017-08-25 19:41:56 b6baa7e4 [app] [I] Authorized user admin(Admin User)
2017-08-25 19:41:58 b6baa7e4 [app] [E] ForemanTasks::TaskError: Task a45ef952-1fb3-4e59-96a8-b9ba4516c20d: ActiveRecord::RecordNotUnique: PG::Error: ERROR:  duplicate key value violates unique constraint "index_katello_subscription_facets_on_user_id"


Another occurrence:

2017-08-26 02:09:45 1a24ce39 [app] [I] Started POST "/api/hosts/subscriptions" for 127.0.0.1 at 2017-08-26 02:09:45 -0400
2017-08-26 02:09:46 1a24ce39 [app] [I] Processing by Katello::Api::V2::HostSubscriptionsController#create as JSON
2017-08-26 02:09:46 1a24ce39 [app] [I]   Parameters: {"name"=>"hXFzBtcMHyhUroM", "facts"=>"[FILTERED]", "lifecycle_environment_id"=>298, "content_view_id"=>372, "api_version"=>"v2", "host_subscription"=>{"name"=>"hXFzBtcMHyhUroM", "facts" 
2017-08-26 02:09:46 1a24ce39 [app] [I] Authorized user admin(Admin User)
...
2017-08-26 02:09:48 f7726c0e [app] [I] Started POST "/katello/api/v2/organizations" for 10.8.0.99 at 2017-08-26 02:09:48 -0400
2017-08-26 02:09:48 f7726c0e [app] [I] Processing by Katello::Api::V2::OrganizationsController#create as */* 
2017-08-26 02:09:48 f7726c0e [app] [I]   Parameters: {"name"=>"hdpYSCNhg", "api_version"=>"v2", "organization"=>{"name"=>"hdpYSCNhg"}}
2017-08-26 02:09:48 f7726c0e [app] [I] Authorized user admin(Admin User)
2017-08-26 02:09:49  [foreman-tasks/action] [E] PG::Error: ERROR:  duplicate key value violates unique constraint "index_katello_subscription_facets_on_user_id"
 | DETAIL:  Key (user_id)=(3) already exists.
...
2017-08-26 02:09:49 1a24ce39 [app] [E] ForemanTasks::TaskError: Task 825b9c29-648e-49b9-ba5c-0c6f8b6c3fad: ActiveRecord::RecordNotUnique: PG::Error: ERROR:  duplicate key value violates unique constraint "index_katello_subscription_facets
 | DETAIL:  Key (user_id)=(3) already exists.


Expected results:
no conflict, the tasks should be atomic

Additional info:

Comment 3 Roman Plevka 2017-08-28 14:41:04 UTC
I believe this has been introduced by a fix for:
https://bugzilla.redhat.com/show_bug.cgi?id=1380117

Comment 6 Evgeni Golov 2017-08-30 09:19:20 UTC
*** Bug 1486512 has been marked as a duplicate of this bug. ***

Comment 9 Roman Plevka 2017-09-04 10:04:40 UTC
VERIFIED
on 6.2.12-2

no unique constraints error on user_id anymore.

Comment 11 errata-xmlrpc 2017-09-25 18:59:44 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2017:2803