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

Performance improvements to MiqProvisionVirtWorkflow#allowed_templates #18353

Conversation

NickLaMuro
Copy link
Member

@NickLaMuro NickLaMuro commented Jan 11, 2019

This PR introduces a number of improvements to the MiqProvisionVirtWorkflow#allowed_templates, and specifically is beneficial to environments that have EMS's with lots of templates (Amazon in particular when public images are included).

These changes specifically do as much as possible without trying to change anything in the database, but a follow up PR (will be posted shortly) will take these changes further with a few database modifications.

Notable Changes

  • Limit preloading and prefer virtual attributes (60% drop in processing, 1Gig drop in MEM usage)
  • Use hash cache for EMS relations instead of preload (Additional 5% drop in processing time)
  • Limit VM columns pulled back (Additional 10% drop in processing time, 500MB drop in MEM usage)

More specific numbers in the commit messages.

Links

Steps for Testing/QA

The main pain points with this can be recreated with a single AWS EMS that has public images enabled, and has had it's initial refresh.

I was using a combination of this script for testing the full routes:

$ cat script_1
ActiveRecord::Base.logger = Logger.new(STDOUT).tap {|l| l.level = Logger::INFO }

Rails.application.load_console
Rails.env = ENV["RAILS_ENV"]
include Rails::ConsoleMethods
MiqUiWorker.preload_for_console # required


app.post "/dashboard/authenticate", :params => { :user_name => "admin", :user_password => "smartvm" }
app.get  "/vm_cloud/explorer"
csrf_token = app.response.body.match(/.*csrf-token.*content="(?<CSRF_TOKEN>[^"]*)"/)[:CSRF_TOKEN]

headers = { :"X-CSRF-Token" => csrf_token }
app.post "/vm_cloud/x_button?pressed=instance_miq_request_new", :headers => headers
app.post "/miq_request/pre_prov/?sel_id=3192",                  :headers => headers
app.post "/vm_cloud/pre_prov?button=continue",                  :headers => headers
$ bin/rails r script_1

And this one, which tested MiqProvisionVirtWorkflow#allowed_templates on it's own:

$ cat script_2
user     = User.find(1)
wf_klass = ManageIQ::Providers::Amazon::CloudManager::ProvisionWorkflow
workflow = wf_klass.new({}, user, { :initial_pass => true, :src_vm_id => [1] })

dialog_options =  {
  :initial_pass            => true,
  :src_vm_id               => [ 1, "[SOME AWS PUBLIC IMAGE NAME HERE]" ],
  :miq_request_dialog_name => "miq_provision_amazon_dialogs_template",
  :customize_enabled       => [ "enabled" ],
  :current_tab_key         => :requester
}

profile do # use whatever profiler you want here
  workflow.init_from_dialog dialog_options
end
$ bin/rails r script_2

@NickLaMuro
Copy link
Member Author

@miq-bot add_label performance

@kbrock (and maybe @Fryguy): Ready for some 🍅 🍅 🍅 's on this one.

@NickLaMuro NickLaMuro force-pushed the miq_provision_virt_workflow_better_allowed_templates branch 3 times, most recently from 2d3c993 to 84a3280 Compare January 15, 2019 00:47
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.

This is nice.

Even without the performance improvements

app/models/miq_provision_virt_workflow.rb Outdated Show resolved Hide resolved
app/models/miq_provision_virt_workflow.rb Show resolved Hide resolved
app/models/miq_provision_virt_workflow.rb Show resolved Hide resolved
app/models/miq_provision_virt_workflow.rb Show resolved Hide resolved
app/models/miq_provision_virt_workflow.rb Show resolved Hide resolved
lib/rbac/filterer.rb Outdated Show resolved Hide resolved
@NickLaMuro NickLaMuro force-pushed the miq_provision_virt_workflow_better_allowed_templates branch from 84a3280 to 2ce540b Compare January 16, 2019 18:44
@NickLaMuro
Copy link
Member Author

@kbrock okay, fixed the code climate stuff my gaming the system. You can see the changes in the following commits:

@NickLaMuro NickLaMuro changed the title [WIP] Performance improvements to MiqProvisionVirtWorkflow#allowed_templates Performance improvements to MiqProvisionVirtWorkflow#allowed_templates Jan 16, 2019
@NickLaMuro
Copy link
Member Author

@kbrock as mentioned, I think I am good with this, but wouldn't mind a second opinion from the automate team.

@syncrou I know you have looked at some of my code for this in the past, so would you mind taking a look briefly to see if anything jumps out at you and I am possibly breaking some things?

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.

This looks much nicer.
I'll give @syncrou a little time to check it out.
I'll merge tomorrow unless I hear otherwise

If we aren't in DEBUG mode for `_log` (evm.log), render the strings and
loop through the list to "print debug statements.  Also fixed some
rubocop complexity issues by moving this logic into it's own method.
@NickLaMuro NickLaMuro force-pushed the miq_provision_virt_workflow_better_allowed_templates branch from 2ce540b to e00cd2a Compare August 10, 2019 21:46
lib/rbac/filterer.rb Outdated Show resolved Hide resolved
Use virtual_attributes where possible when fetching from the `vms` table
to avoid memory bloat.

Makes use of `Rbac`'s `:extra_cols` feature.

Benchmark
---------

**Before**

|     ms | queries | query (ms) |   rows |
|   ---: |    ---: |       ---: |   ---: |
| 110281 |      36 |     3005.5 | 364586 |
| 106174 |      36 |     2786.2 | 364586 |
| 106952 |      36 |     2812.0 | 364586 |

**After**

|     ms | queries | query (ms) |   rows |
|   ---: |    ---: |       ---: |   ---: |
|  43988 |      33 |     2435.7 | 243133 |
|  44452 |      33 |     2438.9 | 243133 |
|  44009 |      33 |     2470.0 | 243133 |
In #create_hash_struct_from_vm_or_template, avoid generating the
`MiqHashStruct` until the end of the method to prevent expensive method
missing calls for every generated record.

Also does a few things to improve things:

- Use `.ems_id` instead of `.ext_management_system`, since it is a
  non-sql way of checking if there is a relation (performance)
- Fixes some rubocop complaints by changing/inverting the if statements
This creates a @_ems_allowed_templates_cache, which is used to keep
track of the IDs of ems records that have been fetched, and prevents and
additional loop over the @allowed_templates records to perform a preload
of the associated ems records.

In the case of a AWS Ems with 100k+ templates (after a refresh including
public images), this is a non-trivial amount of work that is worth
avoiding.

Benchmark
---------

**Before**

|     ms | queries | query (ms) |   rows |
|   ---: |    ---: |       ---: |   ---: |
|  44251 |      33 |     2479.0 | 243133 |
|  43795 |      33 |     2433.2 | 243133 |
|  41723 |      33 |     2468.1 | 243133 |

**After**

|     ms | queries | query (ms) |   rows |
|   ---: |    ---: |       ---: |   ---: |
|  39488 |      33 |     2439.4 | 243133 |
|  38879 |      33 |     2438.2 | 243133 |
|  39256 |      33 |     2444.3 | 243133 |
@NickLaMuro NickLaMuro force-pushed the miq_provision_virt_workflow_better_allowed_templates branch from e00cd2a to 88c1cb7 Compare August 10, 2019 21:53
@NickLaMuro NickLaMuro requested a review from kbrock August 10, 2019 21:54
@NickLaMuro
Copy link
Member Author

@tinaafitz Could you or someone from the automate team also give this a look and confirm what is here is okay?

@NickLaMuro
Copy link
Member Author

@miq-bot remove_label unmergeable

@NickLaMuro
Copy link
Member Author

@miq-bot add_label ivanchuk/yes

@tinaafitz
Copy link
Member

@mkanoor @gmcculloug @lfu Please review.

Copy link
Member

@lfu lfu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job!

Copy link
Member

@gmcculloug gmcculloug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good to me, but I suggest we hold off merging until @kbrock can review and comment on https://github.com/ManageIQ/manageiq/pull/18353/files#r312716130

@NickLaMuro
Copy link
Member Author

NickLaMuro commented Aug 16, 2019

@gmcculloug thanks!

but I suggest we hold off merging until @kbrock can review and comment on https://github.com/ManageIQ/manageiq/pull/18353/files#r312716130

Yeah, I agree. At this point, it isn't going to get merged any faster if we don't wait for Keenan to get back Monday, so let's just do that.

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.

LGTM. just the one comment/response

lib/rbac/filterer.rb Outdated Show resolved Hide resolved
This reduces the number of columns returned from the VMs table to
reduce what is returned and serialized from the DB.

Benchmark
---------

**Before**

|     ms | queries | query (ms) |   rows |
|   ---: |    ---: |       ---: |   ---: |
|  39488 |      33 |     2439.4 | 243133 |
|  38879 |      33 |     2438.2 | 243133 |
|  39256 |      33 |     2444.3 | 243133 |

**After**

|     ms | queries | query (ms) |   rows |
|   ---: |    ---: |       ---: |   ---: |
|  28440 |      33 |     1929.5 | 243133 |
|  28491 |      33 |     1933.6 | 243133 |
|  28858 |      33 |     2631.9 | 243133 |
respond_to? calls in mass are slow since they require some object
introspection, where doing a simple `nil` check is much quicker.

By including just the `cloud_tenant_id` in the SELECT clause, it allows
for the swapping of `respond_to?` with an `nil` check, with the
functionality that existed preserved.

Benchmark
---------

**Before**

|     ms | queries | query (ms) |   rows |
|   ---: |    ---: |       ---: |   ---: |
|  28440 |      33 |     1929.5 | 243133 |
|  28491 |      33 |     1933.6 | 243133 |
|  28858 |      33 |     2631.9 | 243133 |

**After**

|     ms | queries | query (ms) |   rows |
|   ---: |    ---: |       ---: |   ---: |
|  27107 |      33 |     1958.5 | 243133 |
|  26803 |      33 |     1944.2 | 243133 |
|  27642 |      33 |     1965.5 | 243133 |
@NickLaMuro NickLaMuro force-pushed the miq_provision_virt_workflow_better_allowed_templates branch from 88c1cb7 to f83c69f Compare August 19, 2019 19:49
@gmcculloug gmcculloug self-assigned this Aug 19, 2019
@miq-bot
Copy link
Member

miq-bot commented Aug 19, 2019

Checked commits NickLaMuro/manageiq@32be9ec~...f83c69f with ruby 2.4.6, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0
1 file 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.

:shipit:

app/models/miq_provision_virt_workflow.rb Show resolved Hide resolved
app/models/miq_provision_virt_workflow.rb Outdated Show resolved Hide resolved
@gmcculloug gmcculloug merged commit e1ed394 into ManageIQ:master Aug 19, 2019
@gmcculloug gmcculloug added this to the Sprint 118 Ending Aug 19, 2019 milestone Aug 19, 2019
@NickLaMuro
Copy link
Member Author

@miq-bot add_label blocker

simaishi pushed a commit that referenced this pull request Aug 23, 2019
…_better_allowed_templates

Performance improvements to MiqProvisionVirtWorkflow#allowed_templates

(cherry picked from commit e1ed394)

https://bugzilla.redhat.com/show_bug.cgi?id=1662972
@simaishi
Copy link
Contributor

Ivanchuk backport details:

$ git log -1
commit 1a1a2147ce289159621d158e40ce610625cb6603
Author: Greg McCullough <gmccullo@redhat.com>
Date:   Mon Aug 19 17:55:51 2019 -0400

    Merge pull request #18353 from NickLaMuro/miq_provision_virt_workflow_better_allowed_templates
    
    Performance improvements to MiqProvisionVirtWorkflow#allowed_templates
    
    (cherry picked from commit e1ed394bf103e5dd921748b2ee5508e6bd1a454e)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1662972

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.

7 participants