Red Hat Satellite engineering is moving the tracking of its product development work on Satellite to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "Satellite project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs will be migrated starting at the end of May. If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "Satellite project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/SAT-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.
Bug 1612909 - Concurrent creation of subnets can lead to records with duplicate names
Summary: Concurrent creation of subnets can lead to records with duplicate names
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Satellite
Classification: Red Hat
Component: Networking
Version: 6.3.2
Hardware: Unspecified
OS: Unspecified
unspecified
high
Target Milestone: 6.4.0
Assignee: Ivan Necas
QA Contact: Jitendra Yejare
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2018-08-06 13:59 UTC by Ivan Necas
Modified: 2021-12-10 16:53 UTC (History)
7 users (show)

Fixed In Version: foreman-1.18.0.20-1
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2018-10-16 19:02:08 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Foreman Issue Tracker 24546 0 None None None 2018-08-06 14:42:34 UTC

Description Ivan Necas 2018-08-06 13:59:43 UTC
Description of problem:
Concurrent creation of subnets can lead to records with duplicate names

Version-Release number of selected component (if applicable):
Verified in 6.3, as well as in upstream, has been around probably from the beginning

How reproducible:
Under concurrent requets

for x in {1..20}; do
   echo "Attempt $x"
   for y in {1..4}; do
     curl -k -u admin:changeme -X POST -d '{"name":"subnet-'$x'","network":"192.168.0.1","mask":"255.255.255.0"}' -H 'Content-Type: application/json' http://localhost:300/api/v2/subnets & 
   done
   sleep 1
 done

Actual results:
  
    select name, count(*) from subnets group by name having count(*) > 1;

returns multiple records

Expected results:

    select name, count(*) from subnets group by name having count(*) > 1;

returns no record.

Additional info:

Testing notes: when verifying, please first generate multiple subnets with an installation without this fix and try to migrate to the version with it.

Comment 1 Ivan Necas 2018-08-06 14:42:33 UTC
Created redmine issue https://projects.theforeman.org/issues/24546 from this bug

Comment 2 Ivan Necas 2018-08-06 14:55:42 UTC
Here is the cleanup script to merge the duplicate subnet records:

cat <<EOF | foreman-rake console
  duplicate_names = Subnet.unscoped.select(:name).group(:name).having('count(name) > 1').pluck(:name)
  Rails.logger.info("Found #{duplicate_names} duplicate subnet names.")
  duplicate_names.each do |name|
    winner, *loosers = Subnet.unscoped.where(name: name)
    Rails.logger.info("Merging duplicate subnets with name '#{name}', " \
                      "keeping #{winner.id}, merging with #{loosers.map(&:id)}")
    Hostgroup.unscoped.where(:subnet_id => loosers.map(&:id)).update_all(:subnet_id => winner.id)
    Nic::Base.unscoped.where(:subnet_id => loosers.map(&:id)).update_all(:subnet_id => winner.id)
    SubnetParameter.unscoped.where(:reference_id => loosers.map(&:id)).update_all(:reference_id => winner.id)
    loosers.each(&:destroy!)
  end
EOF

Comment 4 Satellite Program 2018-08-06 16:02:40 UTC
Upstream bug assigned to inecas

Comment 5 Satellite Program 2018-08-06 16:02:43 UTC
Upstream bug assigned to inecas

Comment 8 Satellite Program 2018-09-05 18:01:51 UTC
Moving this bug to POST for triage into Satellite 6 since the upstream issue https://projects.theforeman.org/issues/24546 has been resolved.

Comment 10 Jitendra Yejare 2018-09-11 10:08:09 UTC
Verified!

@ Satellite 6.4 snap 21

Steps:

1. Run API command to create concurrent subnets with same name(using the same script mentioned in Description of this bug)


Observation:

1. The concurrent subnets are created but not duplicates.
2. An error is returned on attempting to create duplicate subnet as:
"error": {"id":null,"errors":{"name":["has already been taken"]},"full_messages":["Name has already been taken"]}

Hence the bug is now verified.

Also, I observed sometimes the incorrect/db error is redirected while cresting duplicate subnets concurrently as:
"error": {"message":"PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint \"index_subnets_on_name\"\nDETAIL:  Key (name)=(bug_subnet-19) already exists.\n: INSERT INTO \"subnets\" (\"network\", \"mask\", \"name\", \"created_at\", \"updated_at\") VALUES ($1, $2, $3, $4, $5) RETURNING \"id\""}

I would raise another bz for same.

Comment 13 Bryan Kearney 2018-10-16 19:02:08 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/RHSA-2018:2927


Note You need to log in before you can comment on or make changes to this bug.