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

Reduce memory allocations in functions used to propagate contextual information between gRPC calls #7529

Merged
merged 4 commits into from
May 6, 2024

Conversation

pracucci
Copy link
Collaborator

@pracucci pracucci commented Mar 4, 2024

What this PR does

In Mimir ingesters, 10% of object (and memory) allocations come from gRPC metadata.FromIncomingContext(). metadata.FromIncomingContext() is called in few places. The problem of metadata.FromIncomingContext() is that it creates a copy of all metadata map, when we only want to lookup a single key.

Two years ago, metadata.ValueFromIncomingContext() was introduced in gRPC golang library exactly for this reason. The function is still marked as experimental but it was left untouched since was introduced 2 years ago. I propose to use it. If it will get removed, we can always revert back to metadata.FromIncomingContext().

I've done the same in grafana/dskit#502, which I'm vendoring in this PR.

Which issue(s) this PR fixes or relates to

N/A

Checklist

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

@pracucci pracucci marked this pull request as ready for review March 4, 2024 15:36
@pracucci pracucci requested review from grafanabot and a team as code owners March 4, 2024 15:36
Copy link
Contributor

@56quarters 56quarters left a comment

Choose a reason for hiding this comment

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

🙌

Copy link
Contributor

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

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

Is there ever a case where we'd want to use metadata.FromIncomingContext()? I'm wondering if we should add a linting rule to avoid re-introducing this in the future.

@dimitarvdimitrov
Copy link
Contributor

i was going to rebase and merge this, but Charles' comment makes sense, i'll leave this open

@duricanikolic
Copy link
Contributor

The CHANGELOG has just been cut to prepare for the next Mimir release. Please rebase main and eventually move the CHANGELOG entry added / updated in this PR to the top of the CHANGELOG document. Thanks!

…ServerStreamInterceptor()

goos: darwin
goarch: amd64
pkg: github.com/grafana/mimir/pkg/querier/api
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
                                                                       │  before.txt  │             after.txt              │
                                                                       │    sec/op    │   sec/op     vs base               │
ReadConsistencyServerUnaryInterceptor/with_read_consistency:_true-12      935.7n ± 2%   127.6n ± 1%  -86.36% (p=0.002 n=6)
ReadConsistencyServerUnaryInterceptor/with_read_consistency:_false-12     740.2n ± 2%   115.2n ± 2%  -84.43% (p=0.002 n=6)
ReadConsistencyServerStreamInterceptor/with_read_consistency:_true-12    1025.5n ± 3%   205.8n ± 3%  -79.94% (p=0.002 n=6)
ReadConsistencyServerStreamInterceptor/with_read_consistency:_false-12    783.1n ± 1%   159.7n ± 2%  -79.61% (p=0.002 n=6)
geomean                                                                   863.6n        148.3n       -82.83%

                                                                       │ before.txt  │               after.txt                │
                                                                       │    B/op     │    B/op     vs base                    │
ReadConsistencyServerUnaryInterceptor/with_read_consistency:_true-12     576.00 ± 0%   80.00 ± 0%   -86.11% (p=0.002 n=6)
ReadConsistencyServerUnaryInterceptor/with_read_consistency:_false-12     496.0 ± 0%     0.0 ± 0%  -100.00% (p=0.002 n=6)
ReadConsistencyServerStreamInterceptor/with_read_consistency:_true-12     640.0 ± 0%   144.0 ± 0%   -77.50% (p=0.002 n=6)
ReadConsistencyServerStreamInterceptor/with_read_consistency:_false-12   528.00 ± 0%   32.00 ± 0%   -93.94% (p=0.002 n=6)
geomean                                                                   557.4                    ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

                                                                       │ before.txt  │               after.txt                │
                                                                       │  allocs/op  │ allocs/op   vs base                    │
ReadConsistencyServerUnaryInterceptor/with_read_consistency:_true-12     11.000 ± 0%   3.000 ± 0%   -72.73% (p=0.002 n=6)
ReadConsistencyServerUnaryInterceptor/with_read_consistency:_false-12     8.000 ± 0%   0.000 ± 0%  -100.00% (p=0.002 n=6)
ReadConsistencyServerStreamInterceptor/with_read_consistency:_true-12    13.000 ± 0%   5.000 ± 0%   -61.54% (p=0.002 n=6)
ReadConsistencyServerStreamInterceptor/with_read_consistency:_false-12    9.000 ± 0%   1.000 ± 0%   -88.89% (p=0.002 n=6)
geomean                                                                   10.07                    ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

Signed-off-by: Marco Pracucci <marco@pracucci.com>
Signed-off-by: Marco Pracucci <marco@pracucci.com>
Signed-off-by: Marco Pracucci <marco@pracucci.com>
@pracucci pracucci force-pushed the optimize-FromIncomingContext branch from 03751d1 to a856384 Compare May 6, 2024 08:15
Signed-off-by: Marco Pracucci <marco@pracucci.com>
@pracucci
Copy link
Collaborator Author

pracucci commented May 6, 2024

Is there ever a case where we'd want to use metadata.FromIncomingContext()? I'm wondering if we should add a linting rule to avoid re-introducing this in the future.

Good idea. Done in b6b64c6, where I've spot another place where we can use the metadata.ValueFromIncomingContext().

@pracucci
Copy link
Collaborator Author

pracucci commented May 6, 2024

where I've spot another place where we can use the metadata.ValueFromIncomingContext()

Actually that function is unused and will get removed in #7530, which I will merge after this PR.

@pracucci pracucci enabled auto-merge (squash) May 6, 2024 08:31
@pracucci pracucci merged commit 7a887b4 into main May 6, 2024
29 checks passed
@pracucci pracucci deleted the optimize-FromIncomingContext branch May 6, 2024 08:42
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