-
Notifications
You must be signed in to change notification settings - Fork 897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize number of transactions sent in refresh #14670
Merged
Fryguy
merged 5 commits into
ManageIQ:master
from
Ladas:optimize_number_of_transactions_sent_in_refresh
Apr 7, 2017
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8ad0ac2
Check for dirty records in save_inventory
blomquisg 4afb8a4
Do not do a blank transaction if the record hasn't changed
Ladas 1ae950e
Wrap updating&deleting in 1 big transaction
Ladas 77147eb
Add a comment about rails issue causing nil transaction
Ladas 2b2862e
Do a transaction only if there are items to be deleted
Ladas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,17 +53,21 @@ def save_inventory_multi(association, hashes, deletes, find_key, child_keys = [] | |
|
||
new_records = [] | ||
|
||
hashes.each do |h| | ||
found = save_inventory_with_findkey(association, h.except(*remove_keys), deletes_index, new_records, record_index) | ||
save_child_inventory(found, h, child_keys) | ||
ActiveRecord::Base.transaction do | ||
hashes.each do |h| | ||
found = save_inventory_with_findkey(association, h.except(*remove_keys), deletes_index, new_records, record_index) | ||
save_child_inventory(found, h, child_keys) | ||
end | ||
end | ||
|
||
# Delete the items no longer found | ||
deletes = deletes_index.values | ||
unless deletes.blank? | ||
type = association.proxy_association.reflection.name | ||
_log.info("[#{type}] Deleting #{log_format_deletes(deletes)}") | ||
disconnect ? deletes.each(&:disconnect_inv) : association.delete(deletes) | ||
ActiveRecord::Base.transaction do | ||
unless deletes.blank? | ||
type = association.proxy_association.reflection.name | ||
_log.info("[#{type}] Deleting #{log_format_deletes(deletes)}") | ||
disconnect ? deletes.each(&:disconnect_inv) : association.delete(deletes) | ||
end | ||
end | ||
|
||
# Add the new items | ||
|
@@ -80,7 +84,7 @@ def save_inventory_single(type, parent, hash, child_keys = [], extra_keys = [], | |
child_keys = Array.wrap(child_keys) | ||
remove_keys = Array.wrap(extra_keys) + child_keys + [:id] | ||
if child | ||
child.update_attributes!(hash.except(:type, *remove_keys)) | ||
update_attributes!(child, hash, [:type, *remove_keys]) | ||
else | ||
child = parent.send("create_#{type}!", hash.except(*remove_keys)) | ||
end | ||
|
@@ -94,12 +98,17 @@ def save_inventory_with_findkey(association, hash, deletes, new_records, record_ | |
found = association.build(hash.except(:id)) | ||
new_records << found | ||
else | ||
found.update_attributes!(hash.except(:id, :type)) | ||
update_attributes!(found, hash, [:id, :type]) | ||
deletes.delete(found) unless deletes.blank? | ||
end | ||
found | ||
end | ||
|
||
def update_attributes!(ar_model, attributes, remove_keys) | ||
ar_model.assign_attributes(attributes.except(*remove_keys)) | ||
ar_model.save! if ar_model.changed? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put a note here with a reference to the upstream issue? Maybe something like # HACK: Avoid empty BEGIN/COMMIT pair until fix is made for https://github.com/rails/rails/issues/17937 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
end | ||
|
||
def backup_keys(hash, keys) | ||
keys.each_with_object({}) { |k, backup| backup[k] = hash.delete(k) if hash.key?(k) } | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are no deletes, doesn't this introduce an empty BEGIN/COMMIT pair because of the transaction? Instead, is it better to do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, will fix it. Though this will not affect the stats much, due to the nested nature.