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

TSDB Index reuses slices, adds pools #5630

Merged
merged 9 commits into from
Mar 17, 2022
Merged

Conversation

owen-d
Copy link
Member

@owen-d owen-d commented Mar 15, 2022

This PR changes the index interface to accept slices. This allows us to reuse memory more easily. To make this easier, I've introduced a number of releveant pools for us to use.

It also adds a configurable rounds parameter (default 8) for the benchmarking script.

ref #5428

benchmarks:

name                                         old time/op    new time/op    delta
Query_PostingsForMatchers/match_ns-4           4.03µs ± 1%    4.02µs ± 0%     ~     (p=0.159 n=7+8)
Query_PostingsForMatchers/match_ns_regexp-4    4.03µs ± 0%    4.02µs ± 0%   -0.28%  (p=0.031 n=8+8)
Query_GetChunkRefs/match_ns-4                  27.4ms ± 0%    14.2ms ± 0%  -48.36%  (p=0.001 n=6+8)
Query_GetChunkRefs/match_ns_regexp-4           27.5ms ± 1%    14.3ms ± 1%  -47.97%  (p=0.000 n=8+7)
Query_GetChunkRefsSharded/match_ns-4            120ms ± 1%     102ms ± 1%  -15.29%  (p=0.000 n=8+8)
Query_GetChunkRefsSharded/match_ns_regexp-4     119ms ± 1%     102ms ± 1%  -14.64%  (p=0.000 n=7+8)

name                                         old alloc/op   new alloc/op   delta
Query_PostingsForMatchers/match_ns-4            80.0B ± 0%     80.0B ± 0%     ~     (all equal)
Query_PostingsForMatchers/match_ns_regexp-4     80.0B ± 0%     80.0B ± 0%     ~     (all equal)
Query_GetChunkRefs/match_ns-4                  40.4MB ± 0%     7.4MB ± 0%  -81.71%  (p=0.000 n=8+8)
Query_GetChunkRefs/match_ns_regexp-4           40.4MB ± 0%     7.4MB ± 0%  -81.70%  (p=0.000 n=8+7)
Query_GetChunkRefsSharded/match_ns-4           48.6MB ± 0%    15.0MB ± 0%  -69.20%  (p=0.001 n=7+7)
Query_GetChunkRefsSharded/match_ns_regexp-4    48.6MB ± 0%    15.0MB ± 0%  -69.20%  (p=0.001 n=8+6)

name                                         old allocs/op  new allocs/op  delta
Query_PostingsForMatchers/match_ns-4             4.00 ± 0%      4.00 ± 0%     ~     (all equal)
Query_PostingsForMatchers/match_ns_regexp-4      4.00 ± 0%      4.00 ± 0%     ~     (all equal)
Query_GetChunkRefs/match_ns-4                    278k ± 0%      278k ± 0%   -0.01%  (p=0.000 n=8+8)
Query_GetChunkRefs/match_ns_regexp-4             278k ± 0%      278k ± 0%   -0.01%  (p=0.000 n=8+8)
Query_GetChunkRefsSharded/match_ns-4             394k ± 0%      394k ± 0%   -0.12%  (p=0.000 n=8+8)
Query_GetChunkRefsSharded/match_ns_regexp-4      394k ± 0%      394k ± 0%   -0.12%  (p=0.000 n=8+8)

@owen-d owen-d requested a review from a team as a code owner March 15, 2022 20:51
@owen-d owen-d changed the title adds a pool for ChunkMetas TSDB Index reuses slices, adds pools Mar 15, 2022
groups, err := i.forIndices(ctx, from, through, func(ctx context.Context, idx Index) (interface{}, error) {
return idx.GetChunkRefs(ctx, userID, from, through, shard, matchers...)
refs := ChunkRefsPool.Get()
err := idx.GetChunkRefs(ctx, userID, from, through, &refs, shard, matchers...)
Copy link
Contributor

Choose a reason for hiding this comment

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

if the final append was inside here you wouldn't need to Get from the pool multiple times, just reset the slice for each indice.

Same for the Multi Series.

Copy link
Member Author

Choose a reason for hiding this comment

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

I used independent slices because i.forIndices runs all these calls in parallel.

Copy link
Contributor

@cyriltovena cyriltovena left a comment

Choose a reason for hiding this comment

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

LGTM

I think we should keep the interface to return results, feels more natural to me.

Comment on lines 95 to 96
func (i *MultiIndex) GetChunkRefs(ctx context.Context, userID string, from, through model.Time, res *[]ChunkRef, shard *index.ShardAnnotation, matchers ...*labels.Matcher) error {
*res = (*res)[:0]
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is (*MultiIndex).GetChunkRefs() called from? If a newly retrieved *[]ChunkRef is passed to the function, why do we need to reset the slice?

Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't actually called from anywhere yet -- we're just building the prototype for how we will interact with the tsdb-based index. Regarding *[]ChunkRef, it was to allow the caller to supply their own slice to avoid allocations. It's encouraged that the slice could be reused across multiple calls to avoid allocations, so we reset it.

@owen-d owen-d merged commit 3f28a33 into grafana:main Mar 17, 2022
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.

3 participants