2019-04-25 00:04:40 +08:00
|
|
|
package operations
|
|
|
|
|
|
|
|
import (
|
2023-06-02 20:00:06 +08:00
|
|
|
"bufio"
|
2019-04-25 00:04:40 +08:00
|
|
|
"context"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-04-25 00:04:40 +08:00
|
|
|
"io"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2023-07-25 23:19:37 +08:00
|
|
|
"github.com/rclone/rclone/lib/readers"
|
2019-04-25 00:04:40 +08:00
|
|
|
"golang.org/x/sync/errgroup"
|
2023-07-25 23:19:37 +08:00
|
|
|
"golang.org/x/sync/semaphore"
|
2019-04-25 00:04:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-07-25 23:19:37 +08:00
|
|
|
multithreadChunkSize = 64 << 10
|
2019-04-25 00:04:40 +08:00
|
|
|
)
|
|
|
|
|
2023-06-02 20:00:06 +08:00
|
|
|
// An offsetWriter maps writes at offset base to offset base+off in the underlying writer.
|
|
|
|
//
|
|
|
|
// Modified from the go source code. Can be replaced with
|
|
|
|
// io.OffsetWriter when we no longer need to support go1.19
|
|
|
|
type offsetWriter struct {
|
|
|
|
w io.WriterAt
|
|
|
|
off int64 // the current offset
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOffsetWriter returns an offsetWriter that writes to w
|
|
|
|
// starting at offset off.
|
|
|
|
func newOffsetWriter(w io.WriterAt, off int64) *offsetWriter {
|
|
|
|
return &offsetWriter{w, off}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *offsetWriter) Write(p []byte) (n int, err error) {
|
|
|
|
n, err = o.w.WriteAt(p, o.off)
|
|
|
|
o.off += int64(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-13 05:09:40 +08:00
|
|
|
// Return a boolean as to whether we should use multi thread copy for
|
|
|
|
// this transfer
|
2020-11-05 19:33:32 +08:00
|
|
|
func doMultiThreadCopy(ctx context.Context, f fs.Fs, src fs.Object) bool {
|
|
|
|
ci := fs.GetConfig(ctx)
|
|
|
|
|
2019-08-13 05:09:40 +08:00
|
|
|
// Disable multi thread if...
|
|
|
|
|
|
|
|
// ...it isn't configured
|
2020-11-05 19:33:32 +08:00
|
|
|
if ci.MultiThreadStreams <= 1 {
|
2019-08-13 05:09:40 +08:00
|
|
|
return false
|
|
|
|
}
|
2023-05-10 00:40:58 +08:00
|
|
|
// ...if the source doesn't support it
|
|
|
|
if src.Fs().Features().NoMultiThreading {
|
|
|
|
return false
|
|
|
|
}
|
2019-08-13 05:09:40 +08:00
|
|
|
// ...size of object is less than cutoff
|
2020-11-05 19:33:32 +08:00
|
|
|
if src.Size() < int64(ci.MultiThreadCutoff) {
|
2019-08-13 05:09:40 +08:00
|
|
|
return false
|
|
|
|
}
|
2023-05-10 00:40:58 +08:00
|
|
|
// ...destination doesn't support it
|
2019-08-13 05:09:40 +08:00
|
|
|
dstFeatures := f.Features()
|
2023-07-25 23:19:37 +08:00
|
|
|
if dstFeatures.OpenChunkWriter == nil && dstFeatures.OpenWriterAt == nil {
|
2019-08-13 05:09:40 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// ...if --multi-thread-streams not in use and source and
|
|
|
|
// destination are both local
|
2020-11-05 19:33:32 +08:00
|
|
|
if !ci.MultiThreadSet && dstFeatures.IsLocal && src.Fs().Features().IsLocal {
|
2019-08-13 05:09:40 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-25 00:04:40 +08:00
|
|
|
// state for a multi-thread copy
|
|
|
|
type multiThreadCopyState struct {
|
2023-07-25 23:19:37 +08:00
|
|
|
ctx context.Context
|
|
|
|
partSize int64
|
|
|
|
size int64
|
|
|
|
src fs.Object
|
|
|
|
acc *accounting.Account
|
|
|
|
streams int
|
|
|
|
numChunks int
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy a single stream into place
|
2023-07-25 23:19:37 +08:00
|
|
|
func (mc *multiThreadCopyState) copyStream(ctx context.Context, stream int, writer fs.ChunkWriter) (err error) {
|
2019-04-25 00:04:40 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2023-07-25 23:19:37 +08:00
|
|
|
fs.Debugf(mc.src, "multi-thread copy: stream %d/%d failed: %v", stream+1, mc.numChunks, err)
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
start := int64(stream) * mc.partSize
|
|
|
|
if start >= mc.size {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
end := start + mc.partSize
|
|
|
|
if end > mc.size {
|
|
|
|
end = mc.size
|
|
|
|
}
|
|
|
|
|
2023-07-25 23:19:37 +08:00
|
|
|
fs.Debugf(mc.src, "multi-thread copy: stream %d/%d (%d-%d) size %v starting", stream+1, mc.numChunks, start, end, fs.SizeSuffix(end-start))
|
2019-04-25 00:04:40 +08:00
|
|
|
|
2023-06-01 19:54:19 +08:00
|
|
|
rc, err := Open(ctx, mc.src, &fs.RangeOption{Start: start, End: end - 1})
|
2019-04-25 00:04:40 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("multipart copy: failed to open source: %w", err)
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
defer fs.CheckClose(rc, &err)
|
|
|
|
|
2023-07-25 23:19:37 +08:00
|
|
|
bytesWritten, err := writer.WriteChunk(stream, readers.NewRepeatableReader(rc))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
2023-07-25 23:19:37 +08:00
|
|
|
// FIXME: Wrap ReadSeeker for Accounting
|
|
|
|
// However, to ensure reporting is correctly seeks have to be handled properly
|
|
|
|
errAccRead := mc.acc.AccountRead(int(bytesWritten))
|
|
|
|
if errAccRead != nil {
|
|
|
|
return errAccRead
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
2023-07-25 23:19:37 +08:00
|
|
|
fs.Debugf(mc.src, "multi-thread copy: stream %d/%d (%d-%d) size %v finished", stream+1, mc.numChunks, start, end, fs.SizeSuffix(bytesWritten))
|
2019-04-25 00:04:40 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 23:19:37 +08:00
|
|
|
// Given a file size and a chunkSize
|
|
|
|
// it returns the number of chunks, so that chunkSize * numChunks >= size
|
|
|
|
func calculateNumChunks(size int64, chunkSize int64) int {
|
|
|
|
numChunks := size / chunkSize
|
|
|
|
if size%chunkSize != 0 {
|
|
|
|
numChunks++
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
2023-07-25 23:19:37 +08:00
|
|
|
|
|
|
|
return int(numChunks)
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
2023-07-25 23:19:37 +08:00
|
|
|
// Copy src to (f, remote) using streams download threads. It tries to use the OpenChunkWriter feature
|
|
|
|
// and if that's not available it creates an adapter using OpenWriterAt
|
2019-07-16 19:56:20 +08:00
|
|
|
func multiThreadCopy(ctx context.Context, f fs.Fs, remote string, src fs.Object, streams int, tr *accounting.Transfer) (newDst fs.Object, err error) {
|
2023-07-25 23:19:37 +08:00
|
|
|
openChunkWriter := f.Features().OpenChunkWriter
|
|
|
|
ci := fs.GetConfig(ctx)
|
|
|
|
if openChunkWriter == nil {
|
|
|
|
openWriterAt := f.Features().OpenWriterAt
|
|
|
|
if openWriterAt == nil {
|
|
|
|
return nil, errors.New("multi-part copy: neither OpenChunkWriter nor OpenWriterAt supported")
|
|
|
|
}
|
|
|
|
openChunkWriter = openChunkWriterFromOpenWriterAt(openWriterAt, int64(ci.MultiThreadChunkSize), int64(ci.MultiThreadWriteBufferSize), f)
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
2023-07-25 23:19:37 +08:00
|
|
|
|
2019-04-25 00:04:40 +08:00
|
|
|
if src.Size() < 0 {
|
2023-07-25 23:19:37 +08:00
|
|
|
return nil, fmt.Errorf("multi-thread copy: can't copy unknown sized file")
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
if src.Size() == 0 {
|
2023-07-25 23:19:37 +08:00
|
|
|
return nil, fmt.Errorf("multi-thread copy: can't copy zero sized file")
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 16:33:21 +08:00
|
|
|
g, gCtx := errgroup.WithContext(ctx)
|
2023-07-25 23:19:37 +08:00
|
|
|
chunkSize, chunkWriter, err := openChunkWriter(ctx, remote, src)
|
|
|
|
|
|
|
|
if chunkSize > src.Size() {
|
|
|
|
fs.Debugf(src, "multi-thread copy: chunk size %v was bigger than source file size %v", fs.SizeSuffix(chunkSize), fs.SizeSuffix(src.Size()))
|
|
|
|
chunkSize = src.Size()
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
2023-07-25 23:19:37 +08:00
|
|
|
numChunks := calculateNumChunks(src.Size(), chunkSize)
|
|
|
|
if streams > numChunks {
|
|
|
|
fs.Debugf(src, "multi-thread copy: number of streams '%d' was bigger than number of chunks '%d'", streams, numChunks)
|
|
|
|
streams = numChunks
|
|
|
|
}
|
|
|
|
|
|
|
|
mc := &multiThreadCopyState{
|
|
|
|
ctx: gCtx,
|
|
|
|
size: src.Size(),
|
|
|
|
src: src,
|
|
|
|
partSize: chunkSize,
|
|
|
|
streams: streams,
|
|
|
|
numChunks: numChunks,
|
|
|
|
}
|
2019-04-25 00:04:40 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2023-07-25 23:19:37 +08:00
|
|
|
return nil, fmt.Errorf("multipart copy: failed to open chunk writer: %w", err)
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
2023-07-25 23:19:37 +08:00
|
|
|
// Make accounting
|
|
|
|
mc.acc = tr.Account(ctx, nil)
|
|
|
|
|
|
|
|
fs.Debugf(src, "Starting multi-thread copy with %d parts of size %v with %v parallel streams", mc.numChunks, fs.SizeSuffix(mc.partSize), mc.streams)
|
|
|
|
sem := semaphore.NewWeighted(int64(mc.streams))
|
|
|
|
for chunk := 0; chunk < mc.numChunks; chunk++ {
|
|
|
|
fs.Debugf(src, "Acquiring semaphore...")
|
|
|
|
if err := sem.Acquire(ctx, 1); err != nil {
|
|
|
|
fs.Errorf(src, "Failed to acquire semaphore: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
currChunk := chunk
|
2019-04-25 00:04:40 +08:00
|
|
|
g.Go(func() (err error) {
|
2023-07-25 23:19:37 +08:00
|
|
|
defer sem.Release(1)
|
|
|
|
return mc.copyStream(gCtx, currChunk, chunkWriter)
|
2019-04-25 00:04:40 +08:00
|
|
|
})
|
|
|
|
}
|
2023-07-25 23:19:37 +08:00
|
|
|
|
2019-04-25 00:04:40 +08:00
|
|
|
err = g.Wait()
|
2023-07-25 23:19:37 +08:00
|
|
|
closeErr := chunkWriter.Close()
|
2019-04-25 00:04:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-24 18:22:09 +08:00
|
|
|
if closeErr != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("multi-thread copy: failed to close object after copy: %w", closeErr)
|
2020-02-24 18:22:09 +08:00
|
|
|
}
|
2019-04-25 00:04:40 +08:00
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
obj, err := f.NewObject(ctx, remote)
|
2019-04-25 00:04:40 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("multi-thread copy: failed to find object after copy: %w", err)
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
2023-07-25 23:19:37 +08:00
|
|
|
if f.Features().PartialUploads {
|
|
|
|
err = obj.SetModTime(ctx, src.ModTime(ctx))
|
|
|
|
switch err {
|
|
|
|
case nil, fs.ErrorCantSetModTime, fs.ErrorCantSetModTimeWithoutDelete:
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("multi-thread copy: failed to set modification time: %w", err)
|
|
|
|
}
|
2019-04-25 00:04:40 +08:00
|
|
|
}
|
|
|
|
|
2023-07-25 23:19:37 +08:00
|
|
|
fs.Debugf(src, "Finished multi-thread copy with %d parts of size %v", mc.numChunks, fs.SizeSuffix(mc.partSize))
|
2019-04-25 00:04:40 +08:00
|
|
|
return obj, nil
|
|
|
|
}
|
2023-07-25 23:19:37 +08:00
|
|
|
|
|
|
|
type writerAtChunkWriter struct {
|
|
|
|
ctx context.Context
|
|
|
|
remote string
|
|
|
|
size int64
|
|
|
|
writerAt fs.WriterAtCloser
|
|
|
|
chunkSize int64
|
|
|
|
chunks int
|
|
|
|
writeBufferSize int64
|
|
|
|
f fs.Fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w writerAtChunkWriter) WriteChunk(chunkNumber int, reader io.ReadSeeker) (int64, error) {
|
|
|
|
fs.Debugf(w.remote, "writing chunk %v", chunkNumber)
|
|
|
|
|
|
|
|
bytesToWrite := w.chunkSize
|
|
|
|
if chunkNumber == (w.chunks-1) && w.size%w.chunkSize != 0 {
|
|
|
|
bytesToWrite = w.size % w.chunkSize
|
|
|
|
}
|
|
|
|
|
|
|
|
var writer io.Writer = newOffsetWriter(w.writerAt, int64(chunkNumber)*w.chunkSize)
|
|
|
|
if w.writeBufferSize > 0 {
|
|
|
|
writer = bufio.NewWriterSize(writer, int(w.writeBufferSize))
|
|
|
|
}
|
|
|
|
n, err := io.Copy(writer, reader)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if n != bytesToWrite {
|
|
|
|
return -1, fmt.Errorf("expected to write %v bytes for chunk %v, but wrote %v bytes", bytesToWrite, chunkNumber, n)
|
|
|
|
}
|
|
|
|
// if we were buffering, flush do disk
|
|
|
|
switch w := writer.(type) {
|
|
|
|
case *bufio.Writer:
|
|
|
|
er2 := w.Flush()
|
|
|
|
if er2 != nil {
|
|
|
|
return -1, fmt.Errorf("multipart copy: flush failed: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w writerAtChunkWriter) Close() error {
|
|
|
|
return w.writerAt.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w writerAtChunkWriter) Abort() error {
|
|
|
|
obj, err := w.f.NewObject(w.ctx, w.remote)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("multi-thread copy: failed to find temp file when aborting chunk writer: %w", err)
|
|
|
|
}
|
|
|
|
return obj.Remove(w.ctx)
|
|
|
|
}
|
|
|
|
|
2023-08-15 01:04:42 +08:00
|
|
|
// openChunkWriterFromOpenWriterAt adapts an OpenWriterAtFn into an OpenChunkWriterFn using chunkSize and writeBufferSize
|
|
|
|
func openChunkWriterFromOpenWriterAt(openWriterAt fs.OpenWriterAtFn, chunkSize int64, writeBufferSize int64, f fs.Fs) fs.OpenChunkWriterFn {
|
2023-07-25 23:19:37 +08:00
|
|
|
return func(ctx context.Context, remote string, src fs.ObjectInfo, options ...fs.OpenOption) (chunkSizeResult int64, writer fs.ChunkWriter, err error) {
|
|
|
|
writerAt, err := openWriterAt(ctx, remote, src.Size())
|
|
|
|
if err != nil {
|
|
|
|
return -1, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if writeBufferSize > 0 {
|
|
|
|
fs.Debugf(src.Remote(), "multi-thread copy: write buffer set to %v", writeBufferSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkWriter := &writerAtChunkWriter{
|
|
|
|
ctx: ctx,
|
|
|
|
remote: remote,
|
|
|
|
size: src.Size(),
|
|
|
|
chunkSize: chunkSize,
|
|
|
|
chunks: calculateNumChunks(src.Size(), chunkSize),
|
|
|
|
writerAt: writerAt,
|
|
|
|
writeBufferSize: writeBufferSize,
|
|
|
|
f: f,
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunkSize, chunkWriter, nil
|
|
|
|
}
|
|
|
|
}
|