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

Replace remove duplicate timestamp by sql version for chargeback #17538

Conversation

lpichler
Copy link
Contributor

@lpichler lpichler commented Jun 6, 2018

This is first part of decreasing memory in chargeback. I think that there is not too much reducing memory but this change is absolutely important for next step in other PRs in Links section.

This is adding possibility to avoid array of AR objects and we go with query.

This code is written by @NickLaMuro (almost). Many thanks! it helped me a lot in order do further changes with signification memory reduction.

Links

https://bugzilla.redhat.com/show_bug.cgi?id=1566452

#17538 - first part
#17552 - second part
#17558 - third part - Can be merged before first and second part
#17560 - last part

cc @NickLaMuro @kbrock

@miq-bot assign @gtanzillo

- rewrite remove_duplicate_timestamps to SQL
- rewrite also "group by tenant" feature
- reduce returned object from BIG base_rollup query
@lpichler lpichler force-pushed the replace_remove_duplicate_timestamp_by_sql_version branch from c7ddebd to a518a17 Compare June 6, 2018 15:12
@miq-bot miq-bot added the wip label Jun 6, 2018
@miq-bot
Copy link
Member

miq-bot commented Jun 6, 2018

Checked commit lpichler@a518a17 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 0 offenses detected
Everything looks fine. 👍

@lpichler lpichler changed the title [WIP] Replace remove duplicate timestamp by sql version for chargeback Replace remove duplicate timestamp by sql version for chargeback Jun 6, 2018
@miq-bot miq-bot removed the wip label Jun 6, 2018
@gtanzillo gtanzillo requested a review from NickLaMuro June 7, 2018 13:47
@gtanzillo gtanzillo self-assigned this Jun 7, 2018
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 introduces many extra queries for each record.
I think I'm missing the whole picture though.

options.group_with(records).each_value do |metric_rollup_records|

records.each_value do |rollup_record_ids|
metric_rollup_records = base_rollup.where(:id => rollup_record_ids).to_a
Copy link
Member

Choose a reason for hiding this comment

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

you're introducing an N+1 here. Is this really what you want?


def self.uniq_timestamp_record_map(report_scope, group_by_tenant = false)
main_select = MetricRollup.select(:id, :resource_id).arel.ast.to_sql
.gsub("SELECT", "DISTINCT ON (resource_type, resource_id, timestamp)")
Copy link
Member

Choose a reason for hiding this comment

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

have you tried
.distinct(:resource_type, :resource_id, :timestamp)

I'd almost prefer raw sql over sql generation and munging.

@JPrause
Copy link
Member

JPrause commented Jun 11, 2018

@miq-bot add_label blocker

@JPrause
Copy link
Member

JPrause commented Jun 11, 2018

@lpichler if this can be backported, please add the gaprindashvili/yes label

@lpichler
Copy link
Contributor Author

@kbrock thanks for now, I'll add big picture in GH issue , briefly - reduce memory in chargeback, this is first part of 4.

records = cb_class.where_clause(records, options, region)
records = Metric::Helper.remove_duplicate_timestamps(records)
Copy link
Member

Choose a reason for hiding this comment

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

It seems like this will result in a change in behavior, maybe it's ok but I want to acknowledge it here - the original code in remove_duplicate_timestamps merges all metric rows it finds with the same timestamp - https://github.com/manageiq/manageiq/blob/b6bd309650ee46fedcdf6d9e00af4f06685bd7f8/app/models/metric/helper.rb#L139. This code will keep one of them and discard the other.

@jrafanie
Copy link
Member

@lpichler please review feedback here, thanks!

@lpichler
Copy link
Contributor Author

lpichler commented Jun 12, 2018

@jrafanie yes and I probably found solution also for @gtanzillo's finding in #17538 (review) comment. And also I am putting together some stats.

@gtanzillo gtanzillo added this to the Sprint 88 Ending Jun 18, 2018 milestone Jun 13, 2018
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.

Merging this now. @lpichler will have additional changes that will deal with merging metrics with duplicate timestamps.

@gtanzillo gtanzillo merged commit 887cc81 into ManageIQ:master Jun 13, 2018
@lpichler lpichler mentioned this pull request Jun 15, 2018
6 tasks
@lpichler lpichler deleted the replace_remove_duplicate_timestamp_by_sql_version branch June 15, 2018 09:57
@lpichler
Copy link
Contributor Author

@miq-bot add_label gaprindashvili/yes

simaishi pushed a commit that referenced this pull request Jun 15, 2018
…stamp_by_sql_version

Replace remove duplicate timestamp by sql version for chargeback
(cherry picked from commit 887cc81)

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

Gaprindashvili backport details:

$ git log -1
commit 9310fadb24285de5dbd986f6fc0a634e64cd260d
Author: Gregg Tanzillo <gtanzill@redhat.com>
Date:   Wed Jun 13 11:47:21 2018 -0400

    Merge pull request #17538 from lpichler/replace_remove_duplicate_timestamp_by_sql_version
    
    Replace remove duplicate timestamp by sql version for chargeback
    (cherry picked from commit 887cc81051bb528784dd7599408270ea9b66894b)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1591939

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