-
Notifications
You must be signed in to change notification settings - Fork 896
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
Memoize root tenant #15191
Memoize root tenant #15191
Conversation
@lpichler Cannot apply the following labels because they are not recognized: , perfomance |
6fc1d8b
to
ff2d593
Compare
app/models/tenant.rb
Outdated
@@ -220,6 +220,10 @@ def self.root_tenant | |||
in_my_region.roots.first | |||
end | |||
|
|||
def self.root_tenant_name | |||
@root_tenant_name ||= root_tenant.name |
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 am thinking, what about we memoize root_tenant
, i.e.:
def self.root_tenant
@root_tenant ||= in_my_region.roots.first
end
I think the root_tenant shouldn't change, but its name could. Additionally, you wouldn't need this method (and modify callers).
Perhaps I am missing something?
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.
Ah, I see specs fail horribly after memoizing root_tenant
.
What about if we do the same magic as the MiqServer.myserver
does:
diff --git a/app/models/tenant.rb b/app/models/tenant.rb
index 5c53243..7d1ef32 100644
--- a/app/models/tenant.rb
+++ b/app/models/tenant.rb
@@ -216,13 +216,11 @@ class Tenant < ApplicationRecord
# from this tenant, all tenants are positioned
#
# @return [Tenant] the root tenant
- def self.root_tenant
- in_my_region.roots.first
- end
+ cache_with_timeout(:root_tenant) { in_my_region.roots.first }
# NOTE: returns the root tenant
def self.seed
- root_tenant || create!(:use_config_for_attributes => true) do |_|
+ in_my_region.roots.first || create!(:use_config_for_attributes => true) do |_|
_log.info("Creating root tenant")
end
end
diff --git a/spec/support/evm_spec_helper.rb b/spec/support/evm_spec_helper.rb
index 2eafc0c..9ccb6a3 100644
--- a/spec/support/evm_spec_helper.rb
+++ b/spec/support/evm_spec_helper.rb
@@ -77,6 +77,7 @@ module EvmSpecHelper
end
def self.remote_miq_server(attrs = {})
+ Tenant.root_tenant_clear_cache
Tenant.root_tenant || Tenant.create!(:use_config_for_attributes => false)
FactoryGirl.create(:miq_server, attrs)
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.
@isimluk thanks a lot for looking on it, I tried it but it was causing failures in other specs
but after some playing, I realized that during switching rspec contexts the database is cleared out but not classes variables on the models and it was causing most of CI failures
f8ebb7e
to
3ca257c
Compare
5010724
to
da83572
Compare
65140df
to
ae66629
Compare
@miq-bot remove_label wip |
@miq-bot add_label performance |
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.
LGTM 👍 , Just need to fix typo in the method name.
app/models/tenant.rb
Outdated
@root_tenant ||= root_tenant_without_cash | ||
end | ||
|
||
def self.root_tenant_without_cash |
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, should be root_tenant_without_cache
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.
done 👍
Validation validate_only_one_root was using memoized method Tenant#root_tenant which is called before after_save hook which is creating default group (Tenant#create_tenant_group) and thanks to this fact in variable (@root_tenant) was filled without created default group created in (Tenant#create_tenant_group) and it was causing problems for specs. (then tests were working with instance of root tenant without default group )
ae66629
to
0ea303b
Compare
Checked commits lpichler/manageiq@74d6415~...0ea303b with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 |
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.
👍 Surprised this would save us that much. LGTM
@miq-bot add_label fine/yes |
Memoize root tenant (cherry picked from commit 8a0fa8e) https://bugzilla.redhat.com/show_bug.cgi?id=1461958
Fine backport details:
|
First part for the BZ. (in other I will eliminate useless calls to
reporting_available_fields
)From the BZ:
it is (also) because there is method
reporting_available_fields
called and there is thru other methods(MiqExpression.value2human
) calledTenant.root_tenant
which is producing 1 query for each call and for getting the just name of root tenant and name is unchangeable.Numbers for
MiqExpression.reporting_available_fields('Vm', nil)
:before:
:total_queries: 249 (:rows_by_class Tenant:242 )
after:
:total_queries: 8 (:rows_by_class Tenant:1 )
Links
https://bugzilla.redhat.com/show_bug.cgi?id=1446542
@miq-bot assign @gtanzillo
@miq-bot add_label core, perfomance