Bug 1357583

Summary: After converting host from discovered to managed, calling save! on the host does not persist the conversion
Product: Red Hat Satellite Reporter: Fabian von Feilitzsch <fabian>
Component: Discovery PluginAssignee: Lukas Zapletal <lzap>
Status: CLOSED DUPLICATE QA Contact: Katello QA List <katello-qa-list>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 6.2.0CC: bbuckingham, fabian, jmatthew, lzap
Target Milestone: UnspecifiedKeywords: Triaged
Target Release: Unused   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-09-22 11:11:53 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:
Bug Depends On: 1359032    
Bug Blocks: 1212602    

Description Fabian von Feilitzsch 2016-07-18 14:50:39 UTC
Description of problem:

This is a known interaction in Rails (< 4.1.6) between ActiveRecord::Base#becomes and ActiveRecord::Base#save!. The issue is that the save! call creates an UPDATE like this: 


    UPDATE "hosts" SET ... WHERE "hosts"."type" IN ('Host::Managed') AND "hosts"."id" = 18  [...]


The WHERE clause there matches nothing, because the record in the database still has a type of Host::Discovered. An UPDATE on an empty set doesn't raise an error, so execution continues, and the transaction is not rolled back. This means, if you convert a host from discovered to managed and then save! it inside a transaction, all of the SQL statements that handle deleting the discovered attributes, adding entries to hosts/puppetclasses/operating systems/hostgroups etc succeed, and at the end you are left with a broken discovered host with no attributes, that can't even be deleted because of database constraints.

This is only an issue in Rails versions <= 4.1.5, it was fixed with this commit in Rails 4.1.6: https://github.com/rails/rails/commit/257d65ed2ceb14dca6b1319355ed4f8a73540498 . Here is the associated issue: https://github.com/rails/rails/issues/14785

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

How reproducible:
100%

Steps to Reproduce:
1. Create a discovered host
2. Inside a transaction
  a. Use the HostConverter.to_managed to convert the host from discovered to managed
  b. Call host.save!

Actual results:
After the transaction, the host is of type Host::Discovered, but its attributes have been nilled out. Hostgroups/puppetclasses/operating systems all have entries related to the discovered host, which prevents me from deleting it.

Expected results:
After the transaction, the host is of type Host::Managed

Additional info:
from foreman-rake console, I ran ActiveRecord::VERSION::STRING and it output "4.1.5". If we can bump up to any Rails version >= 4.1.6, this issue should go away without any additional work.

Comment 5 Lukas Zapletal 2016-07-22 06:52:43 UTC
Fabian, thanks for report and deep investigation.

We encountered similar problems in the upstream, we haven't figured out and we removed explicit transactions from the codebase as a result. Current stable version Foreman 1.12 does not have it, also we ship it against Rails 4.2.5.

But Satellite 6.2 is a different story. The explicit transaction removal patch did not make it there yet and we still use Rails 4.1.5 version which is affected as you found out. Therefore Satellite 6.2 is affected and we need to handle this.

Comment 8 Lukas Zapletal 2016-09-22 11:11:53 UTC
Transaction were removed in 6.2.1.

*** This bug has been marked as a duplicate of bug 1347992 ***