Skip to content
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

Merged
merged 3 commits into from
Jun 1, 2017
Merged

Memoize root tenant #15191

merged 3 commits into from
Jun 1, 2017

Conversation

lpichler
Copy link
Contributor

@lpichler lpichler commented May 23, 2017

First part for the BZ. (in other I will eliminate useless calls to reporting_available_fields)
From the BZ:

it takes 10-20 sec to add column to new report when report is based on big fields set like Virtual Machines

it is (also) because there is method reporting_available_fields called and there is thru other methods(MiqExpression.value2human) called Tenant.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

@miq-bot
Copy link
Member

miq-bot commented May 23, 2017

@lpichler Cannot apply the following labels because they are not recognized: , perfomance

@miq-bot miq-bot added the core label May 23, 2017
@lpichler lpichler changed the title Memoize root tenant [WIP] Memoize root tenant May 23, 2017
@miq-bot miq-bot added the wip label May 23, 2017
@lpichler lpichler changed the title [WIP] Memoize root tenant Memoize root tenant May 23, 2017
@miq-bot miq-bot removed the wip label May 23, 2017
@@ -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
Copy link
Member

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?

Copy link
Member

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)

Copy link
Contributor Author

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

  • so I added it to clear_caches method after each test here 74d6415
  • and I also found the problem with validation method, described in the commit
    ae66629

@lpichler lpichler changed the title Memoize root tenant [WIP] Memoize root tenant May 23, 2017
@miq-bot miq-bot added the wip label May 23, 2017
@lpichler lpichler force-pushed the memoize_root_tenant branch 7 times, most recently from f8ebb7e to 3ca257c Compare May 25, 2017 07:47
@lpichler lpichler force-pushed the memoize_root_tenant branch 2 times, most recently from 65140df to ae66629 Compare May 26, 2017 15:59
@lpichler lpichler changed the title [WIP] Memoize root tenant Memoize root tenant May 26, 2017
@lpichler
Copy link
Contributor Author

@miq-bot remove_label wip

@miq-bot miq-bot removed the wip label May 26, 2017
@lpichler
Copy link
Contributor Author

@miq-bot add_label performance

Copy link
Member

@gtanzillo gtanzillo left a 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.

@root_tenant ||= root_tenant_without_cash
end

def self.root_tenant_without_cash
Copy link
Member

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

Copy link
Contributor Author

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 )
@miq-bot
Copy link
Member

miq-bot commented May 30, 2017

Checked commits lpichler/manageiq@74d6415~...0ea303b with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
2 files checked, 0 offenses detected
Everything looks fine. 🍰

Copy link
Member

@kbrock kbrock left a 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

@gtanzillo gtanzillo added this to the Sprint 62 Ending Jun 5, 2017 milestone Jun 1, 2017
@gtanzillo gtanzillo merged commit 8a0fa8e into ManageIQ:master Jun 1, 2017
@lpichler lpichler deleted the memoize_root_tenant branch June 1, 2017 11:55
@lpichler
Copy link
Contributor Author

@miq-bot add_label fine/yes

simaishi pushed a commit that referenced this pull request Jun 15, 2017
@simaishi
Copy link
Contributor

Fine backport details:

$ git log -1
commit 728f98b46df442dbd68563db37050cddd5c6f3d6
Author: Gregg Tanzillo <[email protected]>
Date:   Thu Jun 1 06:38:28 2017 -0400

    Merge pull request #15191 from lpichler/memoize_root_tenant
    
    Memoize root tenant
    (cherry picked from commit 8a0fa8e92224feb4fbb62933a7a9a5ce2534f5dd)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1461958

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants