-
Notifications
You must be signed in to change notification settings - Fork 900
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
[AuthenticationMixin] authentication_status to virtual_delegate #18196
[AuthenticationMixin] authentication_status to virtual_delegate #18196
Conversation
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.
Is this really a WIP?
@@ -4,7 +4,16 @@ module AuthenticationMixin | |||
included do | |||
has_many :authentications, :as => :resource, :dependent => :destroy, :autosave => true | |||
|
|||
virtual_column :authentication_status, :type => :string | |||
has_one :authentication_status_severity_level, |
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.
<3
This is related to changes in: ManageIQ/manageiq#18196 When `authentication_status` is changed to a `virtual_delegate`, we are able to pre-fetch the values as part of the original query, and avoid an N+1.
Creates a virtual_delegate for AuthenticationMixin#authentication_status which allows it to be included in reports and put in as part of the main query. The biggest thing of note in this change is how the status_severity_arel is being used to handle the sorting properly in the SQL query. It makes use of a SQL's case statement to handle what would normally be a `sort_by` on the hash lookup of Authentication::STATUS_SEVERITY. Instead, we match each key to the status column and use that as the value for the `ORDER BY exp DESC` in the `has_one`, which allows for the same affect. This also means that it can inject itself in the `SELECT` of a `MiqReport`, and avoid N+1's on things like quadicons and such for pages that use `MiqReport#paged_view_search`.
e02cf66
to
49d854d
Compare
Checked commit NickLaMuro@49d854d with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0 |
expect(host.authentication_status).to eq("None") | ||
end | ||
|
||
it "is 'None' when accessed via ruby, but fetched via sql" 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.
Worth noting: This spec and the one above have a important distinction to make here since it is slightly confusing when looking at the result that is provided by virtual_column_sql_value
. The default value of "None"
is desired here as we want to maintain the previous functionality when calling authentication_status
.
sporadic failure strikes again - just kicked one travis job |
# | ||
STATUS_SEVERITY_AREL = Arel::Nodes::Case.new.tap do |arel_case| | ||
STATUS_SEVERITY.each do |value, sort_weight| | ||
arel_case.when(arel_table[:status].lower.eq(value)).then(sort_weight) |
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.
wow - turns out arel_table[:status]
does not access the database.
who knew?
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 like these changes.
Will give @Fryguy a little time for comments.
Otherwise I'll merge.
This is related to changes in: ManageIQ/manageiq#18196 When `authentication_status` is changed to a `virtual_delegate`, we are able to pre-fetch the values as part of the original query, and avoid an N+1.
Creates a
virtual_delegate
forAuthenticationMixin#authentication_status
which allows it to be included in reports and put in as part of the main query.The biggest thing of note in this change is how the
status_severity_arel
is being used to handle the sorting properly in the SQL query. It makes use of a SQL's case statement to handle what would normally be asort_by
on the hash lookup ofAuthentication::STATUS_SEVERITY
. Instead, we match each key to the status column and use that as the value for theORDER BY exp DESC
in thehas_one
, which allows for the same affect.This also means that it can inject itself in the
SELECT
of aMiqReport
, and avoid N+1's on things like quadicons and such for pages that useMiqReport#paged_view_search
.Metrics
An example set of runs I had with a 47
Host
provider:Before
/ems_infra/report_data
After
/ems_infra/report_data
Links
ComplianceMixin#last_compliance_status
authentication_status
to theMiqReport
in quertion (product/views/Host.yaml
)Steps for Testing/QA
Here is a script to generate test data that I was using:
https://gist.github.com/NickLaMuro/225833358423723ed17ff294415fa6b4
For testing, I did the following:
manageiq
:bin/rails r bz_1580569_db_replication_script.rb
bin/rails s
Compute -> Infrastructure -> Providers
in the side menuper_page
to 1k (just to load all of the records in a single pageHost
records, ideally)Hosts
buttonBetween
master
, and this change, you should see the number of queries per request for the last portion in the above drop by the number ofHost
records for that provider. TheMetrics
section above used this process.