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

Pool file handles used by StreamBinaryReader components #3703

Merged
merged 20 commits into from
Dec 13, 2022

Conversation

56quarters
Copy link
Contributor

@56quarters 56quarters commented Dec 12, 2022

What this PR does

Add a pool of file handles to avoid the cost of opening and closing files for each operation that needs access to the index-header file (symbols and posting offsets). The default number of handles to keep per index-header is 1 to maintain parity with the existing mmap implementation (which keeps a single file handle for the index-header open for the duration of the reader).

Which issue(s) this PR fixes or relates to

See #3465

Benchmark

$ benchstat -delta-test=none old.txt new.txt 
name                                   old time/op    new time/op    delta
DecbufFactory_NewDecbufAtUnchecked-16    2.98µs ± 0%    0.86µs ± 0%  -71.12%

name                                   old alloc/op   new alloc/op   delta
DecbufFactory_NewDecbufAtUnchecked-16      232B ± 0%       48B ± 0%  -79.31%

name                                   old allocs/op  new allocs/op  delta
DecbufFactory_NewDecbufAtUnchecked-16      4.00 ± 0%      1.00 ± 0%  -75.00%

Checklist

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

@56quarters 56quarters marked this pull request as ready for review December 12, 2022 15:28
@56quarters 56quarters requested review from a team as code owners December 12, 2022 15:28
CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

@osg-grafana osg-grafana left a comment

Choose a reason for hiding this comment

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

Unblocking with feedback

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.

Looks good to me. It might be good to add some tests to exercise the pooling behaviour, for example:

  • create a second Decbuf while holding the first open
  • retrieve a new Decbuf from the factory after closing one (and therefore returning the file handle to the pool) and check that the file handle is reset correctly etc.

pkg/storegateway/indexheader/encoding/factory.go Outdated Show resolved Hide resolved
Copy link
Contributor

@colega colega left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Add a pool of file handles to avoid the cost of opening and closing files
for each operation that needs access to the index-header file (symbols and
posting offsets).

See #3465

Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
* Test for multiple Decbuf created at the same time
* Unexport FileReader

Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Copy link
Collaborator

@pracucci pracucci left a comment

Choose a reason for hiding this comment

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

Great job! I did a relatively quick review, but I couldn't spot any issue. I just left few minor comments.

pkg/storegateway/indexheader/encoding/factory.go Outdated Show resolved Hide resolved
pkg/storegateway/indexheader/encoding/factory.go Outdated Show resolved Hide resolved
pkg/storegateway/indexheader/header.go Outdated Show resolved Hide resolved
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
@56quarters
Copy link
Contributor Author

I ended up implementing the change described here because the logic for cleanup was getting a little confusing being split between the FileReader, Decbuf, and DecbufFactory. Now callers just call Decbuf.Close() (io.Closer) and anything that was pooled is put back into whatever pool it came from.

Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
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.

Three very minor nits but otherwise LGTM.

pkg/storegateway/indexheader/encoding/factory.go Outdated Show resolved Hide resolved
pkg/storegateway/indexheader/encoding/factory.go Outdated Show resolved Hide resolved
pkg/storegateway/indexheader/encoding/factory.go Outdated Show resolved Hide resolved
56quarters and others added 2 commits December 13, 2022 17:34
Co-authored-by: Charles Korn <charleskorn@users.noreply.github.com>
Co-authored-by: Charles Korn <charleskorn@users.noreply.github.com>
@56quarters 56quarters merged commit d848283 into main Dec 13, 2022
@56quarters 56quarters deleted the 56quarters/file-handle-pool branch December 13, 2022 23:00
masonmei pushed a commit to udmire/mimir that referenced this pull request Dec 16, 2022
Add a pool of file handles to avoid the cost of opening and closing files
for each operation that needs access to the index-header file (symbols and
posting offsets).

See grafana#3465

Signed-off-by: Nick Pillitteri <nick.pillitteri@grafana.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants