-
Notifications
You must be signed in to change notification settings - Fork 145
Preload nested RBAC associations to reduce N+1 queries #1322
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -369,7 +369,6 @@ def query_match_regexp(*tables) | |||||||
| end | ||||||||
|
|
||||||||
| context "Vm index with nested indirect virtual attribute that participates in Rbac ('hardware.host.name')" do | ||||||||
| # Can't have `expect(Rbac).to receive(:filtered_object).never` in this block | ||||||||
| before do | ||||||||
| # Preload records | ||||||||
| _vms = [vm, vm1, vm2, vm_openstack, vm_openstack1, vm_openstack2] | ||||||||
|
|
@@ -383,12 +382,17 @@ def query_match_regexp(*tables) | |||||||
| api_basic_authorize action_identifier(:vms, :read, :resource_actions, :get) | ||||||||
| query_match = query_match_regexp("vms", "hardwares", "hosts") | ||||||||
|
|
||||||||
| # After fix: 9 queries (3 base + 6 RBAC re-filtering for 6 VMs) | ||||||||
| # Before fix: was 21 queries (N+1 for each host access without preloading) | ||||||||
|
Comment on lines
+385
to
+386
Member
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. This wording is weird to me - I don't like comments that talk about a fix - that can really just live in the git commit body and shown via the spec itself.
Suggested change
|
||||||||
| # Note: RBAC associations are preloaded (visible in LEFT OUTER JOIN) but | ||||||||
| # ManageIQ core RBAC re-filters them when accessed, causing additional queries. | ||||||||
| # This is still better than the original N+1 (21 queries). | ||||||||
|
Member
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. And this line should go away too.
Suggested change
|
||||||||
| expect { | ||||||||
| get api_vms_url, :params => { | ||||||||
| :expand => "resources", | ||||||||
| :attributes => "hardware.host.name,name" | ||||||||
| } | ||||||||
| }.to make_database_queries(:count => 21, :matching => query_match) | ||||||||
| }.to make_database_queries(:count => 9, :matching => query_match) | ||||||||
|
|
||||||||
| expected = { | ||||||||
| "resources" => a_collection_including( | ||||||||
|
|
@@ -415,6 +419,60 @@ def query_match_regexp(*tables) | |||||||
| end | ||||||||
| end | ||||||||
|
|
||||||||
| # Query count expectations for eager loading RBAC associations: | ||||||||
| # - RBAC associations (host, ems, cluster, storage) are preloaded via LEFT OUTER JOINs | ||||||||
| # - ManageIQ core RBAC re-filters associations when accessed, causing additional queries | ||||||||
| # - Query count: ~15 queries (base queries + RBAC re-filtering) | ||||||||
| # - Without preloading: Would be ~20+ queries per VM (N+1 pattern) | ||||||||
|
Member
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. Again, we don't need this "before this fix" type comments - I don't find them helpful to future readers (at least not in the code - fine in the git commit message)
Member
Author
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. Thanks, yes. I cleaned up some of the comments but should make this more concise too. |
||||||||
| context "eager loads RBAC-participating associations" do | ||||||||
| before { add_hardware_and_os_to_vms } | ||||||||
|
|
||||||||
| it "host" do | ||||||||
| api_basic_authorize action_identifier(:vms, :read, :resource_actions, :get) | ||||||||
| query_match = query_match_regexp("vms", "hosts") | ||||||||
|
|
||||||||
| expect do | ||||||||
| get api_vms_url, :params => {:expand => "resources", :attributes => "host"} | ||||||||
| end.to make_database_queries(:count => be <= 15, :matching => query_match) | ||||||||
| end | ||||||||
|
|
||||||||
| it "ext_management_system" do | ||||||||
| api_basic_authorize action_identifier(:vms, :read, :resource_actions, :get) | ||||||||
| query_match = query_match_regexp("vms", "ext_management_systems") | ||||||||
|
|
||||||||
| expect do | ||||||||
| get api_vms_url, :params => {:expand => "resources", :attributes => "ext_management_system"} | ||||||||
| end.to make_database_queries(:count => be <= 15, :matching => query_match) | ||||||||
| end | ||||||||
|
|
||||||||
| it "ems_cluster" do | ||||||||
| api_basic_authorize action_identifier(:vms, :read, :resource_actions, :get) | ||||||||
| query_match = query_match_regexp("vms", "ems_clusters") | ||||||||
|
|
||||||||
| expect do | ||||||||
| get api_vms_url, :params => {:expand => "resources", :attributes => "ems_cluster"} | ||||||||
| end.to make_database_queries(:count => be <= 15, :matching => query_match) | ||||||||
| end | ||||||||
|
|
||||||||
| it "storage" do | ||||||||
| api_basic_authorize action_identifier(:vms, :read, :resource_actions, :get) | ||||||||
| query_match = query_match_regexp("vms", "storages") | ||||||||
|
|
||||||||
| expect do | ||||||||
| get api_vms_url, :params => {:expand => "resources", :attributes => "storage"} | ||||||||
| end.to make_database_queries(:count => be <= 15, :matching => query_match) | ||||||||
| end | ||||||||
|
|
||||||||
| it "multiple RBAC associations" do | ||||||||
| api_basic_authorize action_identifier(:vms, :read, :resource_actions, :get) | ||||||||
| query_match = query_match_regexp("vms", "hosts", "ext_management_systems", "ems_clusters", "storages") | ||||||||
|
|
||||||||
| expect do | ||||||||
| get api_vms_url, :params => {:expand => "resources", :attributes => "host,ext_management_system,ems_cluster,storage"} | ||||||||
| end.to make_database_queries(:count => be <= 15, :matching => query_match) | ||||||||
| end | ||||||||
| end | ||||||||
|
|
||||||||
| context 'Vm edit' do | ||||||||
| let(:new_vms) { FactoryBot.create_list(:vm_openstack, 2) } | ||||||||
|
|
||||||||
|
|
||||||||
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.
@Fryguy @agrare This section will conflict with #1324 so once that's in, I can rebase and get this one in.
In the meantime, I'll update this one to use the same pattern for testing preload/eager loading via query counts as was done in that PR
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.
(obviously, this comment will just go way though and I'll update the new comment that will land from the other PR to mention the specific exceptions that will remain)