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

Batch Iterator optimization #5237

Merged
merged 11 commits into from
Mar 29, 2023
Merged

Conversation

alanprot
Copy link
Member

@alanprot alanprot commented Mar 29, 2023

What this PR does:

This PR is till a work in progress but seems we can speed up some query ranges quite a bit:

We noticed a very high cpu utilization by the seek method when running query ranges that does not have any overlapping steps:
Screen Shot 2023-03-28 at 8 17 44 PM

Ex: q=sum(rate(up[1m]))&step=120 (1 min matrix selector with a 2 min step)

This type of queries and steps are quite common since grafana default to 120s when displaying an 1 day graph.

In our test environment this change reduced for the query sum(rate(up[1m]))&step=120&start=now-600m&end=now over 30K series from 30s to 5s

One interesting point is that if we set the step to 60s the seek is not performed and the query returns in 9s instead of 43s

We can see on the benchmarks bellow that there is a sweet spot and this change can be slower than the current implementation when the steps are skipping lots of samples inside the same chunk (20% slower). Said that, this change seems to still make sense as: 1. It improves 90%+ the most commons use cases; 2. The penalty only happens in cases that is already very fast (~15ms) 3. it makes the latency way less sensitive to the steps (before a 30s step was taking 600ms and 300s step was taking 12ms and now all the cases are under 20ms.

The Crux of this change is that the seek cause the iterator to transverse all the samples from the beginning of the chunk to the time t. In other words, with a step=60s, we we transverse the samples O(n) times but with the step=120s we transverse them O(n*s) where s is the number of steps.

See this:

if i.it.FindAtOrAfter(model.Time(t)) {

func (p *prometheusChunkIterator) FindAtOrAfter(time model.Time) bool {
// FindAtOrAfter must return OLDEST value at given time. That means we need to start with a fresh iterator,
// otherwise we cannot guarantee OLDEST.
p.it = p.c.Iterator(p.it)
return p.it.Seek(int64(time)) != chunkenc.ValNone
}

https://github.com/prometheus/prometheus/blob/211ae4f1f0a2cdaae09c4c52735f75345c1817c6/tsdb/chunkenc/xor.go#L247

We could create a new iterator only if t is before the current at this place but the batching makes this not possible.

NewChunkMergeIterator_Seek/scrapeInterval_30s_seekStep:_15s-16       618ms ± 0%      19ms ± 0%  -96.88%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_30s_seekStep:_30s-16       617ms ± 0%      18ms ± 0%  -97.01%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_30s_seekStep:_60s-16       306ms ± 0%      18ms ± 0%  -94.10%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_30s_seekStep:_300s-16     58.6ms ± 0%    20.0ms ± 0%  -65.91%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_30s_seekStep:_900s-16     17.2ms ± 0%    18.5ms ± 0%   +7.29%  (p=0.029 n=4+4)
NewChunkMergeIterator_Seek/scrapeInterval_30s_seekStep:_1500s-16    12.3ms ± 0%    15.8ms ± 0%  +28.27%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_30s_seekStep:_3000s-16    6.00ms ± 0%    7.79ms ± 0%  +29.81%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_30s_seekStep:_6000s-16    2.87ms ± 0%    2.88ms ± 0%   +0.29%  (p=0.032 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_10s_seekStep:_5s-16        615ms ± 0%      19ms ± 0%  -96.88%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_10s_seekStep:_10s-16       614ms ± 0%      18ms ± 0%  -97.01%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_10s_seekStep:_20s-16       305ms ± 0%      18ms ± 0%  -94.09%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_10s_seekStep:_100s-16     58.4ms ± 0%    19.9ms ± 0%  -65.93%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_10s_seekStep:_300s-16     17.1ms ± 0%    18.5ms ± 0%   +7.98%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_10s_seekStep:_500s-16     12.2ms ± 0%    15.8ms ± 0%  +28.78%  (p=0.008 n=5+5)
NewChunkMergeIterator_Seek/scrapeInterval_10s_seekStep:_1000s-16    5.99ms ± 0%    7.79ms ± 1%  +30.09%  (p=0.016 n=4+5)
NewChunkMergeIterator_Seek/scrapeInterval_10s_seekStep:_2000s-16    2.87ms ± 0%    2.88ms ± 0%   +0.36%  (p=0.016 n=5+5)

** Memory allocations benchmarks were omitted as there is no change at all on them **

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@alanprot alanprot force-pushed the batch-opmization branch 2 times, most recently from 79742f8 to 050f9d8 Compare March 29, 2023 00:33
@pull-request-size pull-request-size bot added size/L and removed size/M labels Mar 29, 2023
@alanprot alanprot changed the title Batch Iterator Opmization Batch Iterator optimization Mar 29, 2023
Copy link
Collaborator

@yeya24 yeya24 left a comment

Choose a reason for hiding this comment

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

Great work overall. This is huge! Thanks

pkg/querier/batch/merge.go Outdated Show resolved Hide resolved
pkg/querier/batch/batch.go Show resolved Hide resolved
pkg/querier/batch/batch.go Outdated Show resolved Hide resolved
pkg/querier/batch/batch.go Show resolved Hide resolved
@pull-request-size pull-request-size bot added size/M and removed size/L labels Mar 29, 2023
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
@alanprot alanprot marked this pull request as ready for review March 29, 2023 17:46
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Copy link
Collaborator

@yeya24 yeya24 left a comment

Choose a reason for hiding this comment

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

Thanks!

@yeya24 yeya24 merged commit 50c9ccf into cortexproject:master Mar 29, 2023
14 checks passed
@alanprot alanprot deleted the batch-opmization branch March 29, 2023 23:41
yeya24 pushed a commit to yeya24/cortex that referenced this pull request Mar 31, 2023
* Batch Opmization

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Add test bacj

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Testing Multiples scrape intervals

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* no assimption

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Using max chunk ts

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* test with scrape 10

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* rename method

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* using next

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* change test name

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* changelog/comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

---------

Signed-off-by: Alan Protasio <alanprot@gmail.com>
yeya24 pushed a commit to yeya24/cortex that referenced this pull request Mar 31, 2023
* Batch Opmization

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Add test bacj

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Testing Multiples scrape intervals

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* no assimption

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Using max chunk ts

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* test with scrape 10

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* rename method

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* using next

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* change test name

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* changelog/comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

---------

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>
@alvinlin123 alvinlin123 added this to the Release 1.15 milestone Mar 31, 2023
yeya24 added a commit that referenced this pull request Apr 1, 2023
* Batch Iterator optimization (#5237)

* Batch Opmization

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Add test bacj

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Testing Multiples scrape intervals

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* no assimption

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Using max chunk ts

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* test with scrape 10

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* rename method

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* using next

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* change test name

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* changelog/comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

---------

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>

* Store Gateway: Convert metrics from summary to histograms (#5239)

* Convert following metrics from summary to histogram

cortex_bucket_store_series_blocks_queried
cortex_bucket_store_series_data_fetched
cortex_bucket_store_series_data_size_touched_bytes
cortex_bucket_store_series_data_size_fetched_bytes
cortex_bucket_store_series_data_touched
cortex_bucket_store_series_result_series

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>

* Update changelog

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>

* fix changelog

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>

---------

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>

* update changelog

Signed-off-by: Ben Ye <benye@amazon.com>

* Catch context error in the s3 bucket client (#5240)

Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>

* bump RC version

Signed-off-by: Ben Ye <benye@amazon.com>

---------

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>
Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>
Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
Co-authored-by: Alan Protasio <approtas@amazon.com>
Co-authored-by: Friedrich Gonzalez <friedrichg@gmail.com>
Co-authored-by: Xiaochao Dong <the.xcdong@gmail.com>
friedrichg added a commit that referenced this pull request Apr 23, 2023
* prepare 1.15.0-rc release (#5235)

Signed-off-by: Ben Ye <benye@amazon.com>

* Cherry-pick fixes to release 1.15 branch (#5241)

* Batch Iterator optimization (#5237)

* Batch Opmization

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Add test bacj

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Testing Multiples scrape intervals

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* no assimption

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Using max chunk ts

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* test with scrape 10

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* rename method

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* using next

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* change test name

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* changelog/comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

---------

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>

* Store Gateway: Convert metrics from summary to histograms (#5239)

* Convert following metrics from summary to histogram

cortex_bucket_store_series_blocks_queried
cortex_bucket_store_series_data_fetched
cortex_bucket_store_series_data_size_touched_bytes
cortex_bucket_store_series_data_size_fetched_bytes
cortex_bucket_store_series_data_touched
cortex_bucket_store_series_result_series

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>

* Update changelog

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>

* fix changelog

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>

---------

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>

* update changelog

Signed-off-by: Ben Ye <benye@amazon.com>

* Catch context error in the s3 bucket client (#5240)

Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>

* bump RC version

Signed-off-by: Ben Ye <benye@amazon.com>

---------

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>
Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>
Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
Co-authored-by: Alan Protasio <approtas@amazon.com>
Co-authored-by: Friedrich Gonzalez <friedrichg@gmail.com>
Co-authored-by: Xiaochao Dong <the.xcdong@gmail.com>

* Cherry-pick fixes to release 1.15 for new RC (#5259)

* fix remote read error in query frontend (#5257)

* fix remote read error in query frontend

Signed-off-by: Ben Ye <benye@amazon.com>

* fix integration test

Signed-off-by: Ben Ye <benye@amazon.com>

* add extra one query

Signed-off-by: Ben Ye <benye@amazon.com>

---------

Signed-off-by: Ben Ye <benye@amazon.com>

* bump RC version

Signed-off-by: Ben Ye <benye@amazon.com>

---------

Signed-off-by: Ben Ye <benye@amazon.com>

* Fix splitByInterval incorrect error response format (#5260) (#5261)

* fix query frontend incorrect error response format



* update changelog



* fix integration test



---------

Signed-off-by: Ben Ye <benye@amazon.com>

* release 1.15.0 (#5274)

Signed-off-by: Ben Ye <benye@amazon.com>

* merge 1.15 into master and resolve changelog conflicts

Signed-off-by: Ben Ye <benye@amazon.com>

---------

Signed-off-by: Ben Ye <benye@amazon.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>
Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
Co-authored-by: Alan Protasio <approtas@amazon.com>
Co-authored-by: Friedrich Gonzalez <friedrichg@gmail.com>
Co-authored-by: Xiaochao Dong <the.xcdong@gmail.com>
yeya24 pushed a commit to yeya24/cortex that referenced this pull request Apr 28, 2023
* Batch Opmization

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Add test bacj

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Testing Multiples scrape intervals

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* no assimption

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Using max chunk ts

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* test with scrape 10

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* rename method

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* using next

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* change test name

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* changelog/comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

---------

Signed-off-by: Alan Protasio <alanprot@gmail.com>
yeya24 added a commit to yeya24/cortex that referenced this pull request Apr 28, 2023
* prepare 1.15.0-rc release (cortexproject#5235)

Signed-off-by: Ben Ye <benye@amazon.com>

* Cherry-pick fixes to release 1.15 branch (cortexproject#5241)

* Batch Iterator optimization (cortexproject#5237)

* Batch Opmization

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Add test bacj

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Testing Multiples scrape intervals

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* no assimption

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* Using max chunk ts

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* test with scrape 10

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* rename method

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* using next

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* change test name

Signed-off-by: Alan Protasio <alanprot@gmail.com>

* changelog/comments

Signed-off-by: Alan Protasio <alanprot@gmail.com>

---------

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>

* Store Gateway: Convert metrics from summary to histograms (cortexproject#5239)

* Convert following metrics from summary to histogram

cortex_bucket_store_series_blocks_queried
cortex_bucket_store_series_data_fetched
cortex_bucket_store_series_data_size_touched_bytes
cortex_bucket_store_series_data_size_fetched_bytes
cortex_bucket_store_series_data_touched
cortex_bucket_store_series_result_series

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>

* Update changelog

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>

* fix changelog

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>

---------

Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>

* update changelog

Signed-off-by: Ben Ye <benye@amazon.com>

* Catch context error in the s3 bucket client (cortexproject#5240)

Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>

* bump RC version

Signed-off-by: Ben Ye <benye@amazon.com>

---------

Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Ben Ye <benye@amazon.com>
Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>
Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
Co-authored-by: Alan Protasio <approtas@amazon.com>
Co-authored-by: Friedrich Gonzalez <friedrichg@gmail.com>
Co-authored-by: Xiaochao Dong <the.xcdong@gmail.com>

* Cherry-pick fixes to release 1.15 for new RC (cortexproject#5259)

* fix remote read error in query frontend (cortexproject#5257)

* fix remote read error in query frontend

Signed-off-by: Ben Ye <benye@amazon.com>

* fix integration test

Signed-off-by: Ben Ye <benye@amazon.com>

* add extra one query

Signed-off-by: Ben Ye <benye@amazon.com>

---------

Signed-off-by: Ben Ye <benye@amazon.com>

* bump RC version

Signed-off-by: Ben Ye <benye@amazon.com>

---------

Signed-off-by: Ben Ye <benye@amazon.com>

* Fix splitByInterval incorrect error response format (cortexproject#5260) (cortexproject#5261)

* fix query frontend incorrect error response format

* update changelog

* fix integration test

---------

Signed-off-by: Ben Ye <benye@amazon.com>

* release 1.15.0 (cortexproject#5274)

Signed-off-by: Ben Ye <benye@amazon.com>

* merge 1.15 into master and resolve changelog conflicts

Signed-off-by: Ben Ye <benye@amazon.com>

---------

Signed-off-by: Ben Ye <benye@amazon.com>
Signed-off-by: Alan Protasio <alanprot@gmail.com>
Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>
Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
Co-authored-by: Alan Protasio <approtas@amazon.com>
Co-authored-by: Friedrich Gonzalez <friedrichg@gmail.com>
Co-authored-by: Xiaochao Dong <the.xcdong@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants