From fbef5dd7f085f6f168490da57e2ff22d9caf0011 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 6 Jan 2023 12:17:45 -0800 Subject: [PATCH] support concurrent uploads for 'mc pipe' (#4435) --- .gitignore | 3 ++- cmd/client-s3.go | 29 +++++++++++++------------ cmd/client.go | 1 + cmd/pipe-main.go | 56 ++++++++++++++++++++++++++++++++++++++++++------ go.mod | 4 ++-- go.sum | 4 ++-- 6 files changed, 71 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 0cf1af72..8518e24d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ stage/ mc .run* .idea/ -mc.RELEASE* \ No newline at end of file +mc.RELEASE* +mc.gz \ No newline at end of file diff --git a/cmd/client-s3.go b/cmd/client-s3.go index cd0178da..89b78553 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -1042,20 +1042,21 @@ func (c *S3Client) Put(ctx context.Context, reader io.Reader, size int64, progre } opts := minio.PutObjectOptions{ - UserMetadata: metadata, - UserTags: tagsMap, - Progress: progress, - ContentType: contentType, - CacheControl: cacheControl, - ContentDisposition: contentDisposition, - ContentEncoding: contentEncoding, - ContentLanguage: contentLanguage, - StorageClass: strings.ToUpper(putOpts.storageClass), - ServerSideEncryption: putOpts.sse, - SendContentMd5: putOpts.md5, - DisableMultipart: putOpts.disableMultipart, - PartSize: putOpts.multipartSize, - NumThreads: putOpts.multipartThreads, + UserMetadata: metadata, + UserTags: tagsMap, + Progress: progress, + ContentType: contentType, + CacheControl: cacheControl, + ContentDisposition: contentDisposition, + ContentEncoding: contentEncoding, + ContentLanguage: contentLanguage, + StorageClass: strings.ToUpper(putOpts.storageClass), + ServerSideEncryption: putOpts.sse, + SendContentMd5: putOpts.md5, + DisableMultipart: putOpts.disableMultipart, + PartSize: putOpts.multipartSize, + NumThreads: putOpts.multipartThreads, + ConcurrentStreamParts: putOpts.concurrentStream, // if enabled honors NumThreads for piped() uploads } if !retainUntilDate.IsZero() && !retainUntilDate.Equal(timeSentinel) { diff --git a/cmd/client.go b/cmd/client.go index a0bc4a8e..43f0c728 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -60,6 +60,7 @@ type PutOptions struct { storageClass string multipartSize uint64 multipartThreads uint + concurrentStream bool } // StatOptions holds options of the HEAD operation diff --git a/cmd/pipe-main.go b/cmd/pipe-main.go index 4a7e4d94..7d805849 100644 --- a/cmd/pipe-main.go +++ b/cmd/pipe-main.go @@ -18,13 +18,22 @@ package cmd import ( + "io" "os" + "runtime/debug" "syscall" + "github.com/dustin/go-humanize" "github.com/minio/cli" "github.com/minio/mc/pkg/probe" + "github.com/minio/minio-go/v7" ) +func defaultPartSize() string { + _, partSize, _, _ := minio.OptimalPartInfo(-1, 0) + return humanize.IBytes(uint64(partSize)) +} + var pipeFlags = []cli.Flag{ cli.StringFlag{ Name: "encrypt", @@ -42,6 +51,16 @@ var pipeFlags = []cli.Flag{ Name: "tags", Usage: "apply one or more tags to the uploaded objects", }, + cli.IntFlag{ + Name: "concurrent", + Value: 1, + Usage: "allow N concurrent uploads [WARNING: will use more memory use it with caution]", + }, + cli.StringFlag{ + Name: "part-size", + Value: defaultPartSize(), + Usage: "customize chunk size for each concurrent upload", + }, } // Display contents of a file. @@ -89,23 +108,46 @@ EXAMPLES: `, } -func pipe(targetURL string, encKeyDB map[string][]prefixSSEPair, storageClass string, meta map[string]string) *probe.Error { +func pipe(ctx *cli.Context, targetURL string, encKeyDB map[string][]prefixSSEPair, meta map[string]string) *probe.Error { if targetURL == "" { // When no target is specified, pipe cat's stdin to stdout. return catOut(os.Stdin, -1).Trace() } + + storageClass := ctx.String("storage-class") alias, _ := url2Alias(targetURL) sseKey := getSSE(targetURL, encKeyDB[alias]) + multipartThreads := ctx.Int("concurrent") + if multipartThreads > 1 { + // We will be allocating large buffers, reduce default GC overhead + debug.SetGCPercent(20) + } + + var multipartSize uint64 + var e error + if partSizeStr := ctx.String("part-size"); partSizeStr != "" { + multipartSize, e = humanize.ParseBytes(partSizeStr) + if e != nil { + return probe.NewError(e) + } + } + // Stream from stdin to multiple objects until EOF. // Ignore size, since os.Stat() would not return proper size all the time // for local filesystem for example /proc files. opts := PutOptions{ - sse: sseKey, - storageClass: storageClass, - metadata: meta, + sse: sseKey, + storageClass: storageClass, + metadata: meta, + multipartSize: multipartSize, + multipartThreads: uint(multipartThreads), + concurrentStream: ctx.IsSet("concurrent"), } - _, err := putTargetStreamWithURL(targetURL, os.Stdin, -1, opts) + + pg := newProgressBar(0) + + _, err := putTargetStreamWithURL(targetURL, io.TeeReader(os.Stdin, pg), -1, opts) // TODO: See if this check is necessary. switch e := err.ToGoError().(type) { case *os.PathError: @@ -142,12 +184,12 @@ func mainPipe(ctx *cli.Context) error { meta["X-Amz-Tagging"] = tags } if len(ctx.Args()) == 0 { - err = pipe("", nil, ctx.String("storage-class"), meta) + err = pipe(ctx, "", nil, meta) fatalIf(err.Trace("stdout"), "Unable to write to one or more targets.") } else { // extract URLs. URLs := ctx.Args() - err = pipe(URLs[0], encKeyDB, ctx.String("storage-class"), meta) + err = pipe(ctx, URLs[0], encKeyDB, meta) fatalIf(err.Trace(URLs[0]), "Unable to write to one or more targets.") } diff --git a/go.mod b/go.mod index cc5b903b..1848884f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/minio/mc -go 1.18 +go 1.19 require ( github.com/charmbracelet/bubbletea v0.22.1 @@ -21,7 +21,7 @@ require ( github.com/minio/filepath v1.0.0 github.com/minio/madmin-go/v2 v2.0.2-0.20221221104141-93e7e5aefaf2 github.com/minio/md5-simd v1.1.2 // indirect - github.com/minio/minio-go/v7 v7.0.44 + github.com/minio/minio-go/v7 v7.0.46 github.com/minio/pkg v1.5.6 github.com/minio/selfupdate v0.5.0 github.com/minio/sha256-simd v1.0.0 diff --git a/go.sum b/go.sum index 1705c271..7fdbabf8 100644 --- a/go.sum +++ b/go.sum @@ -513,8 +513,8 @@ github.com/minio/madmin-go/v2 v2.0.2-0.20221221104141-93e7e5aefaf2/go.mod h1:5aF github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= github.com/minio/minio-go/v7 v7.0.41/go.mod h1:nCrRzjoSUQh8hgKKtu3Y708OLvRLtuASMg2/nvmbarw= -github.com/minio/minio-go/v7 v7.0.44 h1:9zUJ7iU7ax2P1jOvTp6nVrgzlZq3AZlFm0XfRFDKstM= -github.com/minio/minio-go/v7 v7.0.44/go.mod h1:nCrRzjoSUQh8hgKKtu3Y708OLvRLtuASMg2/nvmbarw= +github.com/minio/minio-go/v7 v7.0.46 h1:Vo3tNmNXuj7ME5qrvN4iadO7b4mzu/RSFdUkUhaPldk= +github.com/minio/minio-go/v7 v7.0.46/go.mod h1:nCrRzjoSUQh8hgKKtu3Y708OLvRLtuASMg2/nvmbarw= github.com/minio/pkg v1.5.4/go.mod h1:2MOaRFdmFKULD+uOLc3qHLGTQTuxCNPKNPfLBTxC8CA= github.com/minio/pkg v1.5.6 h1:4OUvRU1gDWilu/dohkJMVapylXN8q94kU5MgkOJ/x0I= github.com/minio/pkg v1.5.6/go.mod h1:EiGlHS2xaooa2VMxhJsxxAZHDObHVUB3HwtuoEXOCVE=