Skip to content

feat: Update Stream to accept a bufSize for tar CopyBuffer window#26964

Merged
devanbenz merged 13 commits intomaster-1.xfrom
db/6518/backup-writer
Dec 23, 2025
Merged

feat: Update Stream to accept a bufSize for tar CopyBuffer window#26964
devanbenz merged 13 commits intomaster-1.xfrom
db/6518/backup-writer

Conversation

@devanbenz
Copy link
Copy Markdown

@devanbenz devanbenz commented Nov 6, 2025

io.Copy & io.CopyN uses a default window size of 32KB during copy

Copy(N) calls copyBuffer with buf set to nil
https://cs.opensource.google/go/go/+/refs/tags/go1.25.4:src/io/io.go;l=387-389

When buf is nil go will set the default buffer size to 32KB
https://cs.opensource.google/go/go/+/refs/tags/go1.25.4:src/io/io.go;l=418

This PR replaces io.CopyN with io.CopyBuffer in tar.Stream. It also adds a new configuration value tar-stream-buffer-size which can be adjusted to allow for larger or smaller tar buffer sizes to be used during backup operations.

It is suggested to keep the default as is, while testing different buffer sizes to be used with CopyBuffer I found that 1MB was the best performance-wise.

@devanbenz devanbenz marked this pull request as ready for review November 7, 2025 16:58
Comment on lines +170 to +174
# The size of tar buffer window size in bytes while running tar
# streaming operations such as renaming and copying tar files during backups.
# The default value is 1MB. This should only change if backups are having performance issues
# and you understand that this value is a heuristic that may need to be tweaked.
# tar-stream-buffer-size = 1048576
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like the tar buffer size is only used in the backup code paths. If this is correct, I wonder if backup-stream-buffer-size would be a better name.

Copy link
Copy Markdown
Member

@gwossum gwossum left a comment

Choose a reason for hiding this comment

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

io.CopyN did some additional error checking that io.CopyBuffer does not. We should probably do the error checking ourselves now.

Copy link
Copy Markdown
Member

@gwossum gwossum left a comment

Choose a reason for hiding this comment

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

I think we're throwing away the error now.

if err != nil {
return err
} else if bytesWritten != h.Size {
return fmt.Errorf("error while copying buffer, expected %d bytes but wrote %d", h.Size, bytesWritten)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe add the method the error occurred in so we can track it down if needed.

Copy link
Copy Markdown
Contributor

@davidby-influx davidby-influx left a comment

Choose a reason for hiding this comment

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

One tiny change.

tsdb/config.go Outdated
}

if c.TarStreamBufferSize <= 0 {
return fmt.Errorf("tar-stream-buffer-size cannot be zero and must be non-negative")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Put the erroneous value in the error with a %d to help debug problems.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added

Copy link
Copy Markdown
Contributor

@davidby-influx davidby-influx left a comment

Choose a reason for hiding this comment

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

LGTM

@philjb philjb added the 1.x label Dec 16, 2025
Copy link
Copy Markdown
Member

@gwossum gwossum left a comment

Choose a reason for hiding this comment

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

LGTM

@devanbenz devanbenz merged commit abd617f into master-1.x Dec 23, 2025
9 checks passed
@devanbenz devanbenz deleted the db/6518/backup-writer branch December 23, 2025 19:08
devanbenz added a commit that referenced this pull request Jan 2, 2026
…6964)

`io.Copy` & `io.CopyN` uses a default window size of 32KB during copy

`Copy(N)` calls `copyBuffer` with `buf` set to `nil`
https://cs.opensource.google/go/go/+/refs/tags/go1.25.4:src/io/io.go;l=387-389

When `buf` is `nil` go will set the default buffer size to 32KB
https://cs.opensource.google/go/go/+/refs/tags/go1.25.4:src/io/io.go;l=418

This PR replaces `io.CopyN` with `io.CopyBuffer` in `tar.Stream`. It also adds a new configuration value `tar-stream-buffer-size` which can be adjusted to allow for larger or smaller tar buffer sizes to be used during backup operations.

It is suggested to keep the default as is, while testing different buffer sizes to be used with `CopyBuffer` I found that `1MB` was the best performance-wise.

(cherry picked from commit abd617f)
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.

4 participants