-
Notifications
You must be signed in to change notification settings - Fork 898
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
[WIP] RelationshipMixin: Improve performance of Relationships parent #10160
Conversation
app/models/relationship.rb
Outdated
relationships.reject { |r| r.filtered?(of_type, except_type) } | ||
else | ||
relationships = relationships.where(:resource_type => of_type) if of_type.present? | ||
relationships = relationships.where.not(:resource_type => except_type) if except_type.present? |
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.
Minor, but since we have a separate method for filtered?
, it feels like we should have a separate method for this scoping...perhaps a .filtered
class method that does these two lines.
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.
The purpose of this method filter_by_resource_type
is to filter on these 2 fields.
You're proposing a method that just does the in memory part?
of note, relationships.reject { |r| r.filtered?(of_type, except_type) }
is only used in this 1 spot.
Also, of note, :except_type
is only ever used in 1 spot of our code.
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.
I'm proposing a method that is just the scopes part... This method is essentially just a switch between scopes vs Array.
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.
Since it's a method that's just scopes, that in itself would make it a scope-method (i.e. a method that returns an AR::Relation)
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.
moved, not sure why it isn't showing up in this commit. it is showing up correctly in main PR See commit "relationships: use scope to simplify filtered"
Can you expand on the OP description a bit...those number are meaningless to me without context (e.g. is that 142s total time or 142s faster but what percentage is that) Do you have some benchmarks for "Speed[ing] up the time it takes to lookup a vm's parents"? |
Overall code-wise looks good, but would like some benchmarks to justify it better. |
5d0ed4a
to
ef942c2
Compare
Thanks for the numbers @kbrock . Sounds good. Only thing left is my comment on separate method and getting the tests to go green |
ef942c2
to
a3ada20
Compare
@Fryguy added scopes and cleared up dumb bug I introduced in fixing up cops |
@kbrock I see you made a lot more changes after I gave a 👍, and started moving more stuff around...the code was good as is, but now I have to re-review in light of all the new changes...were they necessary to move for this PR? |
a3ada20
to
d5e048b
Compare
d5e048b
to
e644549
Compare
@Fryguy I'm sorry. The only changes that I think I made are related to your comments:
I didn't mean to introduce any other changes. UPDATE: |
testing: I ran for >10,000 Vms, with the old method and the new method. and they returned the same value. (not conclusive, but a little reassuring) |
app/models/ems_folder.rb
Outdated
@@ -166,6 +166,23 @@ def folder_path_objs(*args) | |||
folders | |||
end | |||
|
|||
# similar to folder_path_objs, but takes in a scope | |||
# @param folder [Relation] relation pointin to ems folder of interest |
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.
Typo pointin
=> pointing
Also prefer EmsFolder
over ems folder
@kbrock If you remove the parent_blue_folder commit, then this would be fine to merge...that particular commit has issues that could probably be done in a separate PR. I assume the numbers in the OP are without that commit? If so, they seemed good to me without that parent_blue_folder commit. |
# from this id, relationship records can be brought back and mapped to the resource of interest | ||
# NOTE: parent_id is read from ancestry field, so it is not a db hit, but parent is an N+1 db hit. | ||
def relationship_parent_ids | ||
relationships.where.not(:ancestry => [nil, ""]).select(:ancestry).collect(&:parent_id) |
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.
I just realized...does this handle relationship_type filtering?
e644549
to
d9c1fba
Compare
majority of the meat has been extracted. (This would be great in darga, but not as much of a pressing need now that c&u is running. well. If you do want this performance boost, we'll also want to 10299) |
@Fryguy is there any legwork I can do on my side to make this easier to merge? thnx |
@@ -135,6 +135,13 @@ def parent_ids(*args) | |||
Relationship.resource_pairs(parent_rels(*args)) | |||
end | |||
|
|||
# return a scope of all of the parents of this record (must :of_type with a single parameter) |
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 of_type is required or single parameter, can you check and raise an ArgumentError (instead of the comment?)
395d824
to
72a3b19
Compare
app/models/ems_folder.rb
Outdated
folders = folders[1..-1] if options[:exclude_root_folder] | ||
folders = folders.reject(&:hidden?) if options[:exclude_non_display_folders] | ||
folders | ||
self.class.folder_options path(:of_type => "EmsFolder"), options |
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.
I'm surprised the bot is not picking up on the lack of parens on the method call (i.e. please use parens).
@kbrock Would like to sit down with you to understand this one like we did last time. I liked that last time we came to nice elegant answer that covered all use cases, and I have a feeling the same thing will happen here. |
72a3b19
to
b643f58
Compare
WIP until I rebase and produce numbers again |
Using scopes allows us to cut down on the number of queries necessary to get the parent_blue_folders. (8 -> 3)
b643f58
to
38337a1
Compare
Checked commit kbrock@38337a1 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 |
This pull request has been automatically closed because it has not been updated for at least 6 months. Feel free to reopen this pull request if these changes are still valid. Thank you for all your contributions! |
extracted #10299 - waiting to merge that to decide what would be the best to read hereMERGEDTheme:
Speed up
Capture.perf_capture_timer
(142s)by speeding up
MiqAlert.target_needs_realtime_capture?
(105s)by speeding up "detection if a resource has an alert" (105s)
by speeding up "get tags for a resource and it's parents (including blue folder)" (100s)
by speeding up "get parents for a resource" (~50% time, 80% queries)
Goal of this PR:
Speed up the time it takes to lookup a vm's parents by making it quicker to query ancestors/relationships. This speeds up
parent_blue_folder
andparent_resource_pool
https://bugzilla.redhat.com/show_bug.cgi?id=1346988
https://bugzilla.redhat.com/show_bug.cgi?id=1346999
TL;DR
19% faster, 26% fewer objects, 17% fewer queries, 30% fewer rows returned.
before
comments
before
...Metric::Targets.capture_infra_targets
..Metric::Capture.calc_targets_by_rollup_parent
..Metric::Capture.calc_tasks_by_rollup_parent
..Metric::Capture.filter_perf_capture_now
...SELECT "taggings"."id" AS t0_r0, "taggings"."taggable_id" AS t0_r1, "taggings"."tag_id" AS t0_r2, "ta
...SELECT "relationships".*
...SELECT "relationships".*
...SELECT "ems_folders".*
...SELECT "resource_pools".*
..Metric::Capture.queue_captures
after
comments
after
...Metric::Targets.capture_infra_targets
..Metric::Capture.calc_targets_by_rollup_parent
..Metric::Capture.calc_tasks_by_rollup_parent
..Metric::Capture.filter_perf_capture_now
...SELECT "taggings"."id" AS t0_r0, "taggings"."taggable_id" AS t0_r1, "taggings"."tag_id" AS t0_r2, "ta
...SELECT "relationships"."ancestry"
...SELECT "relationships".*
...SELECT "ems_folders".*
...SELECT "resource_pools".*
..Metric::Capture.queue_captures
I tried to cut out cruft from tables. let me know if you need more info