Multi-thread copies from a local source hand the chunk writer an unbuffered ReOpen. Backends such as s3, azureblob and oracleobjectstorage checksum the chunk with io.Copy before uploading it, and as ReOpen didn't implement io.WriterTo, io.Copy read the file in 32 KiB pieces. ReOpen now implements io.WriterTo: - If the destination implements io.ReaderFrom, that is used so the destination's own buffering is kept. This means multi-thread downloads (bufio.Writer over an OffsetWriter, sized by --multi-thread-write-buffer-size) and HTTP request bodies behave as before and don't gain an extra buffer. - If less than 1 MiB remains it uses io.Copy as before, so small files don't take a 1 MiB buffer. - Otherwise it copies through a 1 MiB buffer from the global pool, so the memory is reused and is counted by --max-buffer-memory. This replaces the s3 specific io.CopyBuffer froma68480093(reverted in the previous commit) which allocated a fresh 1 MiB buffer for every chunk outside of --max-buffer-memory, and gives the same speedup to every backend which io.Copies from the chunk reader. Benchmarks on Linux, 16 cores, source file on tmpfs, warm cache. "orig" is master witha68480093reverted (32 KiB reads), "master" hasa68480093(make 1 MiB per chunk), "branch" is this commit. Checksum pass only - ReOpen of a local 128 MiB range into md5, 5 runs: 1 stream MB/s B/op orig 752-759 34 KiB master 751-784 1025 KiB branch 764-784 1.2 KiB 16 streams MB/s B/op orig 4772-5495 547 KiB master 4008-4607 16405 KiB branch 4525-5052 22 KiB On Linux the reads are cheap so the larger reads are worth ~2-3% for a single stream and are lost in the noise with 16 streams. The point here is that branch matches master's reads without allocating. The same benchmark on Windows 10 (VirtualBox VM, 8 vCPUs on a Ryzen 7 PRO 5850U, 64 MiB chunk per stream, 1 GiB file in the file cache), where reads are more expensive, shows the speedup. Median of 5 runs: streams orig master branch 1 560 MB/s 594 MB/s (+6.1%) 610 MB/s (+9.0%) 4 1438 MB/s 1594 MB/s (+10.9%) 1610 MB/s (+12.0%) 8 1878 MB/s 2123 MB/s (+13.0%) 2143 MB/s (+14.1%) allocated per op (all streams) 1 33 KiB 1026 KiB 1 KiB 4 134 KiB 4102 KiB 5 KiB 8 267 KiB 8204 KiB 10 KiB With 4 and 8 streams the ranges of orig and branch don't overlap. End to end - rclone copy of a 2 GiB local file to MinIO on localhost (server bound, so wall time doesn't change), mean of 4 runs, max RSS and number of GCs from GODEBUG=gctrace=1: default (5 MiB chunks, 4 streams) wall max RSS GCs orig 6.13s 84 MiB 14 master 6.29s 93 MiB 54 branch 6.30s 90 MiB 8 --s3-upload-concurrency 16 --s3-chunk-size 64M wall max RSS GCs orig 4.76s 83 MiB 6 master 4.80s 112 MiB 7 branch 4.80s 97 MiB 6 Multi-thread download of the same file from MinIO to local disk (checks the io.ReaderFrom path), mean of 4 runs: wall max RSS GCs orig 4.31s 80 MiB 5 master 4.31s 80 MiB 5 branch 4.32s 80 MiB 5 Uploads with --max-buffer-memory 2M and 4M (with --s3-upload-concurrency 16 --s3-chunk-size 64M) complete normally in 5-7s, throttled rather than stalled.
548 lines
14 KiB
Go
548 lines
14 KiB
Go
package operations
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
"github.com/rclone/rclone/fs/hash"
|
|
"github.com/rclone/rclone/fstest/mockobject"
|
|
"github.com/rclone/rclone/lib/pool"
|
|
"github.com/rclone/rclone/lib/readers"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// check interfaces
|
|
var (
|
|
_ io.ReadSeekCloser = (*ReOpen)(nil)
|
|
_ io.ReaderAt = (*ReOpen)(nil)
|
|
_ pool.DelayAccountinger = (*ReOpen)(nil)
|
|
_ io.WriterTo = (*ReOpen)(nil)
|
|
)
|
|
|
|
var errorTestError = errors.New("test error")
|
|
|
|
// this is a wrapper for a mockobject with a custom Open function
|
|
//
|
|
// breaks indicate the number of bytes to read before returning an
|
|
// error
|
|
type reOpenTestObject struct {
|
|
fs.Object
|
|
t *testing.T
|
|
wantStart int64
|
|
breaks []int64
|
|
unknownSize bool
|
|
}
|
|
|
|
// Open opens the file for read. Call Close() on the returned io.ReadCloser
|
|
//
|
|
// This will break after reading the number of bytes in breaks
|
|
func (o *reOpenTestObject) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadCloser, error) {
|
|
// Lots of backends do this - make sure it works as it modifies options
|
|
fs.FixRangeOption(options, o.Size())
|
|
gotHash := false
|
|
gotRange := false
|
|
startPos := int64(0)
|
|
for _, option := range options {
|
|
switch x := option.(type) {
|
|
case *fs.HashesOption:
|
|
gotHash = true
|
|
case *fs.RangeOption:
|
|
gotRange = true
|
|
startPos = x.Start
|
|
if o.unknownSize {
|
|
assert.Equal(o.t, int64(-1), x.End)
|
|
}
|
|
case *fs.SeekOption:
|
|
startPos = x.Offset
|
|
}
|
|
}
|
|
assert.Equal(o.t, o.wantStart, startPos)
|
|
// Check if ranging, mustn't have hash if offset != 0
|
|
if gotHash && gotRange {
|
|
assert.Equal(o.t, int64(0), startPos)
|
|
}
|
|
rc, err := o.Object.Open(ctx, options...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(o.breaks) > 0 {
|
|
// Pop a breakpoint off
|
|
N := o.breaks[0]
|
|
o.breaks = o.breaks[1:]
|
|
o.wantStart += N
|
|
// If 0 then return an error immediately
|
|
if N == 0 {
|
|
return nil, errorTestError
|
|
}
|
|
// Read N bytes then an error
|
|
r := io.MultiReader(&io.LimitedReader{R: rc, N: N}, readers.ErrorReader{Err: errorTestError})
|
|
// Wrap with Close in a new readCloser
|
|
rc = readCloser{Reader: r, Closer: rc}
|
|
}
|
|
return rc, nil
|
|
}
|
|
|
|
func TestReOpen(t *testing.T) {
|
|
for _, testName := range []string{"Normal", "WithRangeOption", "WithSeekOption", "UnknownSize"} {
|
|
t.Run(testName, func(t *testing.T) {
|
|
// Contents for the mock object
|
|
var (
|
|
reOpenTestcontents = []byte("0123456789")
|
|
expectedRead = reOpenTestcontents
|
|
rangeOption *fs.RangeOption
|
|
seekOption *fs.SeekOption
|
|
unknownSize = false
|
|
)
|
|
switch testName {
|
|
case "Normal":
|
|
case "WithRangeOption":
|
|
rangeOption = &fs.RangeOption{Start: 1, End: 7} // range is inclusive
|
|
expectedRead = reOpenTestcontents[1:8]
|
|
case "WithSeekOption":
|
|
seekOption = &fs.SeekOption{Offset: 2}
|
|
expectedRead = reOpenTestcontents[2:]
|
|
case "UnknownSize":
|
|
rangeOption = &fs.RangeOption{Start: 1, End: -1}
|
|
expectedRead = reOpenTestcontents[1:]
|
|
unknownSize = true
|
|
default:
|
|
panic("bad test name")
|
|
}
|
|
|
|
// Start the test with the given breaks
|
|
testReOpen := func(breaks []int64, maxRetries int) (*ReOpen, *reOpenTestObject, error) {
|
|
srcOrig := mockobject.New("potato").WithContent(reOpenTestcontents, mockobject.SeekModeNone)
|
|
srcOrig.SetUnknownSize(unknownSize)
|
|
src := &reOpenTestObject{
|
|
Object: srcOrig,
|
|
t: t,
|
|
breaks: breaks,
|
|
unknownSize: unknownSize,
|
|
}
|
|
opts := []fs.OpenOption{}
|
|
if rangeOption == nil && seekOption == nil {
|
|
opts = append(opts, &fs.HashesOption{Hashes: hash.NewHashSet(hash.MD5)})
|
|
}
|
|
if rangeOption != nil {
|
|
opts = append(opts, rangeOption)
|
|
src.wantStart = rangeOption.Start
|
|
}
|
|
if seekOption != nil {
|
|
opts = append(opts, seekOption)
|
|
src.wantStart = seekOption.Offset
|
|
}
|
|
rc, err := NewReOpen(context.Background(), src, maxRetries, opts...)
|
|
return rc, src, err
|
|
}
|
|
|
|
// Reset the start after a seek, taking into account the offset
|
|
setWantStart := func(src *reOpenTestObject, x int64) {
|
|
src.wantStart = x
|
|
if rangeOption != nil {
|
|
src.wantStart += rangeOption.Start
|
|
} else if seekOption != nil {
|
|
src.wantStart += seekOption.Offset
|
|
}
|
|
}
|
|
|
|
t.Run("Basics", func(t *testing.T) {
|
|
// open
|
|
h, _, err := testReOpen(nil, 10)
|
|
assert.NoError(t, err)
|
|
|
|
// Check contents read correctly
|
|
got, err := io.ReadAll(h)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expectedRead, got)
|
|
|
|
// Check read after end
|
|
var buf = make([]byte, 1)
|
|
n, err := h.Read(buf)
|
|
assert.Equal(t, 0, n)
|
|
assert.Equal(t, io.EOF, err)
|
|
|
|
// Rewind the stream
|
|
_, err = h.Seek(0, io.SeekStart)
|
|
require.NoError(t, err)
|
|
|
|
// Check contents read correctly
|
|
got, err = io.ReadAll(h)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expectedRead, got)
|
|
|
|
// Check close
|
|
assert.NoError(t, h.Close())
|
|
|
|
// Check double close
|
|
assert.Equal(t, errFileClosed, h.Close())
|
|
|
|
// Check read after close
|
|
n, err = h.Read(buf)
|
|
assert.Equal(t, 0, n)
|
|
assert.Equal(t, errFileClosed, err)
|
|
})
|
|
|
|
t.Run("ErrorAtStart", func(t *testing.T) {
|
|
// open with immediate breaking
|
|
h, _, err := testReOpen([]int64{0}, 10)
|
|
assert.Equal(t, errorTestError, err)
|
|
assert.Nil(t, h)
|
|
})
|
|
|
|
t.Run("WithErrors", func(t *testing.T) {
|
|
// open with a few break points but less than the max
|
|
h, _, err := testReOpen([]int64{2, 1, 3}, 10)
|
|
assert.NoError(t, err)
|
|
|
|
// check contents
|
|
got, err := io.ReadAll(h)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expectedRead, got)
|
|
|
|
// check close
|
|
assert.NoError(t, h.Close())
|
|
})
|
|
|
|
t.Run("TooManyErrors", func(t *testing.T) {
|
|
// open with a few break points but >= the max
|
|
h, _, err := testReOpen([]int64{2, 1, 3}, 3)
|
|
assert.NoError(t, err)
|
|
|
|
// check contents
|
|
got, err := io.ReadAll(h)
|
|
assert.Equal(t, errorTestError, err)
|
|
assert.Equal(t, expectedRead[:6], got)
|
|
|
|
// check old error is returned
|
|
var buf = make([]byte, 1)
|
|
n, err := h.Read(buf)
|
|
assert.Equal(t, 0, n)
|
|
assert.Equal(t, errTooManyTries, err)
|
|
|
|
// Check close
|
|
assert.Equal(t, errFileClosed, h.Close())
|
|
})
|
|
|
|
t.Run("ReadAt", func(t *testing.T) {
|
|
// open
|
|
h, src, err := testReOpen([]int64{2, 1, 3}, 10)
|
|
assert.NoError(t, err)
|
|
|
|
buf := make([]byte, 5)
|
|
|
|
// Read at 0
|
|
n, err := h.ReadAt(buf, 0)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 5, n)
|
|
assert.Equal(t, expectedRead[:n], buf[:n])
|
|
|
|
// Read at 1
|
|
setWantStart(src, 1)
|
|
n, err = h.ReadAt(buf[:3], 1)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 3, n)
|
|
assert.Equal(t, expectedRead[1:n+1], buf[:n])
|
|
|
|
// check position unchanged
|
|
pos, err := h.Seek(0, io.SeekCurrent)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(0), pos)
|
|
|
|
// check close
|
|
assert.NoError(t, h.Close())
|
|
_, err = h.Seek(0, io.SeekCurrent)
|
|
assert.Equal(t, errFileClosed, err)
|
|
})
|
|
|
|
t.Run("Seek", func(t *testing.T) {
|
|
// open
|
|
h, src, err := testReOpen([]int64{2, 1, 3}, 10)
|
|
assert.NoError(t, err)
|
|
|
|
// Seek to end
|
|
pos, err := h.Seek(int64(len(expectedRead)), io.SeekStart)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(len(expectedRead)), pos)
|
|
|
|
// Seek to start
|
|
pos, err = h.Seek(0, io.SeekStart)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), pos)
|
|
|
|
// Should not allow seek past end
|
|
pos, err = h.Seek(int64(len(expectedRead))+1, io.SeekCurrent)
|
|
if !unknownSize {
|
|
assert.Equal(t, errSeekPastEnd, err)
|
|
assert.Equal(t, len(expectedRead), int(pos))
|
|
} else {
|
|
assert.Equal(t, nil, err)
|
|
assert.Equal(t, len(expectedRead)+1, int(pos))
|
|
|
|
// Seek back to start to get tests in sync
|
|
pos, err = h.Seek(0, io.SeekStart)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), pos)
|
|
}
|
|
|
|
// Should not allow seek to negative position start
|
|
pos, err = h.Seek(-1, io.SeekCurrent)
|
|
assert.Equal(t, errNegativeSeek, err)
|
|
assert.Equal(t, 0, int(pos))
|
|
|
|
// Should not allow seek with invalid whence
|
|
pos, err = h.Seek(0, 3)
|
|
assert.Equal(t, errInvalidWhence, err)
|
|
assert.Equal(t, 0, int(pos))
|
|
|
|
// check read
|
|
dst := make([]byte, 5)
|
|
n, err := h.Read(dst)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 5, n)
|
|
assert.Equal(t, expectedRead[:5], dst)
|
|
|
|
// Test io.SeekCurrent
|
|
pos, err = h.Seek(-3, io.SeekCurrent)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 2, int(pos))
|
|
|
|
// check read
|
|
setWantStart(src, 2)
|
|
n, err = h.Read(dst)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 5, n)
|
|
assert.Equal(t, expectedRead[2:7], dst)
|
|
|
|
pos, err = h.Seek(-2, io.SeekCurrent)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 5, int(pos))
|
|
|
|
// Test io.SeekEnd
|
|
pos, err = h.Seek(-3, io.SeekEnd)
|
|
if !unknownSize {
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, len(expectedRead)-3, int(pos))
|
|
} else {
|
|
assert.Equal(t, errBadEndSeek, err)
|
|
assert.Equal(t, 0, int(pos))
|
|
|
|
// sync
|
|
pos, err = h.Seek(1, io.SeekCurrent)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 6, int(pos))
|
|
}
|
|
|
|
// check read
|
|
dst = make([]byte, 3)
|
|
setWantStart(src, int64(len(expectedRead)-3))
|
|
n, err = h.Read(dst)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 3, n)
|
|
assert.Equal(t, expectedRead[len(expectedRead)-3:], dst)
|
|
|
|
// check close
|
|
assert.NoError(t, h.Close())
|
|
_, err = h.Seek(0, io.SeekCurrent)
|
|
assert.Equal(t, errFileClosed, err)
|
|
})
|
|
|
|
t.Run("AccountRead", func(t *testing.T) {
|
|
h, _, err := testReOpen(nil, 10)
|
|
assert.NoError(t, err)
|
|
|
|
var total int
|
|
h.SetAccounting(func(n int) error {
|
|
total += n
|
|
return nil
|
|
})
|
|
|
|
dst := make([]byte, 3)
|
|
n, err := h.Read(dst)
|
|
assert.Equal(t, 3, n)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 3, total)
|
|
})
|
|
|
|
t.Run("AccountReadDelay", func(t *testing.T) {
|
|
h, _, err := testReOpen(nil, 10)
|
|
assert.NoError(t, err)
|
|
|
|
var total int
|
|
h.SetAccounting(func(n int) error {
|
|
total += n
|
|
return nil
|
|
})
|
|
|
|
rewind := func() {
|
|
_, err := h.Seek(0, io.SeekStart)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
h.DelayAccounting(3)
|
|
|
|
dst := make([]byte, 16)
|
|
|
|
n, err := h.Read(dst)
|
|
assert.Equal(t, len(expectedRead), n)
|
|
assert.Equal(t, io.EOF, err)
|
|
assert.Equal(t, 0, total)
|
|
rewind()
|
|
|
|
n, err = h.Read(dst)
|
|
assert.Equal(t, len(expectedRead), n)
|
|
assert.Equal(t, io.EOF, err)
|
|
assert.Equal(t, 0, total)
|
|
rewind()
|
|
|
|
n, err = h.Read(dst)
|
|
assert.Equal(t, len(expectedRead), n)
|
|
assert.Equal(t, io.EOF, err)
|
|
assert.Equal(t, len(expectedRead), total)
|
|
rewind()
|
|
|
|
n, err = h.Read(dst)
|
|
assert.Equal(t, len(expectedRead), n)
|
|
assert.Equal(t, io.EOF, err)
|
|
assert.Equal(t, 2*len(expectedRead), total)
|
|
rewind()
|
|
})
|
|
|
|
t.Run("WriteTo", func(t *testing.T) {
|
|
h, _, err := testReOpen([]int64{2, 1, 3}, 10)
|
|
require.NoError(t, err)
|
|
|
|
// Hide ReadFrom so WriteTo does the copy
|
|
var buf bytes.Buffer
|
|
n, err := h.WriteTo(struct{ io.Writer }{&buf})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(len(expectedRead)), n)
|
|
assert.Equal(t, expectedRead, buf.Bytes())
|
|
assert.NoError(t, h.Close())
|
|
})
|
|
|
|
t.Run("WriteToReaderFrom", func(t *testing.T) {
|
|
h, _, err := testReOpen([]int64{2, 1, 3}, 10)
|
|
require.NoError(t, err)
|
|
|
|
w := &readFromRecorder{}
|
|
n, err := h.WriteTo(w)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(len(expectedRead)), n)
|
|
assert.Equal(t, expectedRead, w.buf.Bytes())
|
|
assert.True(t, w.readFrom)
|
|
assert.NoError(t, h.Close())
|
|
})
|
|
|
|
t.Run("AccountReadError", func(t *testing.T) {
|
|
// Test accounting errors
|
|
h, _, err := testReOpen(nil, 10)
|
|
assert.NoError(t, err)
|
|
|
|
h.SetAccounting(func(n int) error {
|
|
return errorTestError
|
|
})
|
|
|
|
dst := make([]byte, 3)
|
|
n, err := h.Read(dst)
|
|
assert.Equal(t, 3, n)
|
|
assert.Equal(t, errorTestError, err)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
// readFromRecorder is an io.Writer with io.ReaderFrom which records
|
|
// whether ReadFrom was used
|
|
type readFromRecorder struct {
|
|
buf bytes.Buffer
|
|
readFrom bool
|
|
}
|
|
|
|
func (w *readFromRecorder) Write(p []byte) (int, error) {
|
|
return w.buf.Write(p)
|
|
}
|
|
|
|
func (w *readFromRecorder) ReadFrom(r io.Reader) (int64, error) {
|
|
w.readFrom = true
|
|
if _, ok := r.(io.WriterTo); ok {
|
|
return 0, errors.New("ReadFrom passed a WriterTo")
|
|
}
|
|
return w.buf.ReadFrom(r)
|
|
}
|
|
|
|
// poolInUseRecorder is an io.Writer which records the maximum number
|
|
// of global pool buffers in use when it is written to
|
|
type poolInUseRecorder struct {
|
|
buf bytes.Buffer
|
|
maxInUse int
|
|
}
|
|
|
|
func (w *poolInUseRecorder) Write(p []byte) (int, error) {
|
|
w.maxInUse = max(w.maxInUse, pool.Global().InUse())
|
|
return w.buf.Write(p)
|
|
}
|
|
|
|
func TestReOpenWriteToPool(t *testing.T) {
|
|
ctx := context.Background()
|
|
for _, test := range []struct {
|
|
name string
|
|
size int
|
|
breaks []int64
|
|
wantPool bool
|
|
}{
|
|
{"Small", pool.BufferSize - 1, []int64{1000, 2000}, false},
|
|
{"Large", 3*pool.BufferSize + 17, []int64{1000, int64(pool.BufferSize) + 5}, true},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
contents := make([]byte, test.size)
|
|
for i := range contents {
|
|
contents[i] = byte(i * 7)
|
|
}
|
|
srcOrig := mockobject.New("potato").WithContent(contents, mockobject.SeekModeNone)
|
|
src := &reOpenTestObject{
|
|
Object: srcOrig,
|
|
t: t,
|
|
breaks: test.breaks,
|
|
}
|
|
h, err := NewReOpen(ctx, src, 10)
|
|
require.NoError(t, err)
|
|
|
|
var total int
|
|
h.SetAccounting(func(n int) error {
|
|
total += n
|
|
return nil
|
|
})
|
|
h.DelayAccounting(2)
|
|
|
|
// First pass isn't accounted
|
|
inUse := pool.Global().InUse()
|
|
w := &poolInUseRecorder{}
|
|
n, err := h.WriteTo(w)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(test.size), n)
|
|
assert.Equal(t, contents, w.buf.Bytes())
|
|
assert.Equal(t, 0, total)
|
|
assert.Equal(t, test.wantPool, w.maxInUse > inUse)
|
|
assert.Equal(t, inUse, pool.Global().InUse())
|
|
|
|
// Second pass is accounted
|
|
src.wantStart = 0
|
|
_, err = h.Seek(0, io.SeekStart)
|
|
require.NoError(t, err)
|
|
w = &poolInUseRecorder{}
|
|
n, err = h.WriteTo(w)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(test.size), n)
|
|
assert.Equal(t, contents, w.buf.Bytes())
|
|
assert.Equal(t, test.size, total)
|
|
|
|
require.NoError(t, h.Close())
|
|
})
|
|
}
|
|
}
|