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

mysqlctl: open backup files with fadvise(2) and FADV_SEQUENTIAL #16441

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

mattrobenolt
Copy link
Contributor

@mattrobenolt mattrobenolt commented Jul 22, 2024

On Linux, the fadvise(2) syscall can be leveraged to hint to the kernel to readahead disk pages since the file is intended to be read sequentially.

This works well with our use of opening this file, then slamming it into io.Copy.

In benchmarking, this alone yields around ~50% improvement on the read side without other tunings.

In a naive test, the setup was simply an io.Copy with an os.File for src, and io.Discard for dest:

no fadvise: 222.41 MB/s
with fadvise: 344.69 MB/s

This is with no other tunings to the kernel.

Related Issue(s)

#16442

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Copy link
Contributor

vitess-bot bot commented Jul 22, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Jul 22, 2024
@github-actions github-actions bot added this to the v21.0.0 milestone Jul 22, 2024
Copy link

codecov bot commented Jul 23, 2024

Codecov Report

Attention: Patch coverage is 64.28571% with 5 lines in your changes missing coverage. Please review.

Project coverage is 68.63%. Comparing base (3186413) to head (567f754).
Report is 13 commits behind head on main.

Files Patch % Lines
go/vt/mysqlctl/builtinbackupengine_linux.go 58.33% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16441      +/-   ##
==========================================
- Coverage   68.66%   68.63%   -0.04%     
==========================================
  Files        1548     1551       +3     
  Lines      199124   199468     +344     
==========================================
+ Hits       136731   136900     +169     
- Misses      62393    62568     +175     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@deepthi deepthi removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jul 23, 2024
@deepthi deepthi added Component: Backup and Restore Type: Performance and removed NeedsIssue A linked issue is missing for this Pull Request labels Jul 23, 2024
Comment on lines 199 to 204
// hint to the kernel that the intent is to read this file sequentially
if err := fadviseSequential(fd); err != nil {
// close the open fd since we're not using it anymore
fd.Close()
return nil, vterrors.Wrapf(err, "could not fadvise sequential read for %v", name)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

One alternative here, and not sure if there's a preference, this whole block can swapped out to a openForSequential and have the two implementation that both encapsulate the os.Open call as well.

//go:build linux
func openForSequential(name string) (*os.File, error) {
  f, err := os.Open(name)
  if err != nil {
    return nil, err
  }
  fstat, err := f.Stat()
  if err != nil {
      f.Close()
      return nil, err
  }
  if err := unix.Fadvise(int(f.Fd()), 0, fstat.Size(), unix.FADV_SEQUENTIAL); err != nil {
    f.Close()
    return nil, err
  }
  return f, nil
}
//go:build !linux
func openForSequential(name string) (*os.File, error) {
  return os.Open(name)
}

I dunno if the project would have a preference for this instead.

Copy link
Member

Choose a reason for hiding this comment

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

I like this approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mattrobenolt Seems like a good approach for this to have those separate implementations then.

Copy link
Contributor

Choose a reason for hiding this comment

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

Technically the build tag is likely not linux though (see for example https://man.freebsd.org/cgi/man.cgi?query=posix_fadvise&sektion=2&manpath=FreeBSD+8.3-RELEASE)? But we only really support Vitess properly here and it's purely an optimization so Linux specific also seems fine if it's problematic to have a better build tag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From what I can tell, the golang.org/x/sys/unix only exposes unix.Fadvise for linux targets.

Also a GitHub search for this, every usage I can find also explicitly only targets linux: https://github.com/search?q=unix.Fadvise+language%3AGo&type=code&l=Go

I tried to look for a more generic build tag, but I don't think there's anything applicable, unless we directly used syscall.Syscall6 or whatever directly, and I don't think that's worth it. Seems more like if there's freebsd support, that should get into the unix package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will swap this out tho for a generic openForSequential.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

On Linux, the fadvise(2) syscall can be leveraged to hint to the kernel
to readahead disk pages since the file is intended to be read
sequentially.

This works well with our use of opening this file, then slamming it into
`io.Copy`.

In benchmarking, this alone yields around ~50% improvement on the read
side without other tunings.

In a naive test, the setup was simply an io.Copy with an os.File for
src, and io.Discard for dest:

  no fadvise: 222.41 MB/s
with fadvise: 344.69 MB/s

This is with no other tunings to the kernel.

Refs vitessio#3931

Signed-off-by: Matt Robenolt <[email protected]>
Copy link
Member

@mdlayher mdlayher left a comment

Choose a reason for hiding this comment

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

LGTM!

@dbussink dbussink merged commit f481a77 into vitessio:main Jul 24, 2024
129 checks passed
@mattrobenolt mattrobenolt deleted the fadvise branch July 24, 2024 19:59
venkatraju pushed a commit to slackhq/vitess that referenced this pull request Aug 29, 2024
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.

4 participants