fix avoid multiple HEAD operations (#3173)

- Get() already has the metadata to preserve
- Stat() doesn't need to be sent multiple times
  do preserve attributes, use it once.
This commit is contained in:
Harshavardhana
2020-04-17 01:01:34 -07:00
committed by GitHub
parent 4036009dc2
commit f87c8d200a
6 changed files with 82 additions and 84 deletions
+5 -2
View File
@@ -931,8 +931,8 @@ func (c *S3Client) Put(ctx context.Context, reader io.Reader, size int64, metada
delete(metadata, AmzObjectLockMode)
}
retainUntilDateStr, ok := metadata[AmzObjectLockRetainUntilDate]
retainUntilDate := timeSentinel
retainUntilDateStr, ok := metadata[AmzObjectLockRetainUntilDate]
if ok {
delete(metadata, AmzObjectLockRetainUntilDate)
if t, e := time.Parse(time.RFC3339, retainUntilDateStr); e == nil {
@@ -952,14 +952,17 @@ func (c *S3Client) Put(ctx context.Context, reader io.Reader, size int64, metada
ServerSideEncryption: sse,
DisableMultipart: disableMultipart,
}
if retainUntilDate != timeSentinel {
if !retainUntilDate.IsZero() && !retainUntilDate.Equal(timeSentinel) {
opts.RetainUntilDate = &retainUntilDate
opts.SendContentMd5 = true
}
if lockModeStr != "" {
opts.Mode = &lockMode
opts.SendContentMd5 = true
}
if lh, ok := metadata[AmzObjectLockLegalHold]; ok {
delete(metadata, AmzObjectLockLegalHold)
opts.LegalHold = minio.LegalHoldStatus(strings.ToUpper(lh))
+69 -54
View File
@@ -148,7 +148,7 @@ func getSourceStreamMetadataFromURL(urlStr string, encKeyDB map[string][]prefixS
return nil, nil, err.Trace(urlStr)
}
sseKey := getSSE(urlStr, encKeyDB[alias])
return getSourceStream(alias, urlStrFull, true, sseKey)
return getSourceStream(alias, urlStrFull, true, sseKey, false)
}
// getSourceStreamFromURL gets a reader from URL.
@@ -158,7 +158,7 @@ func getSourceStreamFromURL(urlStr string, encKeyDB map[string][]prefixSSEPair)
return nil, err.Trace(urlStr)
}
sse := getSSE(urlStr, encKeyDB[alias])
reader, _, err = getSourceStream(alias, urlStrFull, false, sse)
reader, _, err = getSourceStream(alias, urlStrFull, false, sse, false)
return reader, err
}
@@ -186,8 +186,31 @@ func probeContentType(reader io.Reader) (ctype string, err *probe.Error) {
return ctype, nil
}
// Verify if reader is a generic ReaderAt
func isReadAt(reader io.Reader) (ok bool) {
var v *os.File
v, ok = reader.(*os.File)
if ok {
// Stdin, Stdout and Stderr all have *os.File type
// which happen to also be io.ReaderAt compatible
// we need to add special conditions for them to
// be ignored by this function.
for _, f := range []string{
"/dev/stdin",
"/dev/stdout",
"/dev/stderr",
} {
if f == v.Name() {
ok = false
break
}
}
}
return
}
// getSourceStream gets a reader from URL.
func getSourceStream(alias string, urlStr string, fetchStat bool, sse encrypt.ServerSide) (reader io.ReadCloser, metadata map[string]string, err *probe.Error) {
func getSourceStream(alias string, urlStr string, fetchStat bool, sse encrypt.ServerSide, preserve bool) (reader io.ReadCloser, metadata map[string]string, err *probe.Error) {
sourceClnt, err := newClientFromAlias(alias, urlStr)
if err != nil {
return nil, nil, err.Trace(alias, urlStr)
@@ -218,7 +241,7 @@ func getSourceStream(alias string, urlStr string, fetchStat bool, sse encrypt.Se
}
st.ETag = oinfo.ETag
} else {
st, err = sourceClnt.Stat(false, false, sse)
st, err = sourceClnt.Stat(false, preserve, sse)
if err != nil {
return nil, nil, err.Trace(alias, urlStr)
}
@@ -279,9 +302,16 @@ func putTargetStream(ctx context.Context, alias, urlStr, mode, until, legalHold
if err != nil {
return 0, err.Trace(alias, urlStr)
}
metadata[AmzObjectLockMode] = mode
metadata[AmzObjectLockRetainUntilDate] = until
metadata[AmzObjectLockLegalHold] = legalHold
if mode != "" {
metadata[AmzObjectLockMode] = mode
}
if until != "" {
metadata[AmzObjectLockRetainUntilDate] = until
}
if legalHold != "" {
metadata[AmzObjectLockLegalHold] = legalHold
}
n, err := targetClnt.Put(ctx, reader, size, metadata, progress, sse, disableMultipart)
if err != nil {
return n, err.Trace(alias, urlStr)
@@ -338,13 +368,13 @@ func filterMetadata(metadata map[string]string) map[string]string {
// getAllMetadata - returns a map of user defined function
// by combining the usermetadata of object and values passed by attr keyword
func getAllMetadata(sourceAlias, sourceURLStr string, srcSSE encrypt.ServerSide, urls URLs) (map[string]string, *probe.Error) {
func getAllMetadata(sourceAlias, sourceURLStr string, srcSSE encrypt.ServerSide, urls URLs, preserve bool) (map[string]string, *probe.Error) {
metadata := make(map[string]string)
sourceClnt, err := newClientFromAlias(sourceAlias, sourceURLStr)
if err != nil {
return nil, err.Trace(sourceAlias, sourceURLStr)
}
st, err := sourceClnt.Stat(false, false, srcSSE)
st, err := sourceClnt.Stat(false, preserve, srcSSE)
if err != nil {
return nil, err.Trace(sourceAlias, sourceURLStr)
}
@@ -363,7 +393,7 @@ func getAllMetadata(sourceAlias, sourceURLStr string, srcSSE encrypt.ServerSide,
// uploadSourceToTargetURL - uploads to targetURL from source.
// optionally optimizes copy for object sizes <= 5GiB by using
// server side copy operation.
func uploadSourceToTargetURL(ctx context.Context, urls URLs, progress io.Reader, encKeyDB map[string][]prefixSSEPair) URLs {
func uploadSourceToTargetURL(ctx context.Context, urls URLs, progress io.Reader, encKeyDB map[string][]prefixSSEPair, preserve bool) URLs {
sourceAlias := urls.SourceAlias
sourceURL := urls.SourceContent.URL
targetAlias := urls.TargetAlias
@@ -393,18 +423,19 @@ func uploadSourceToTargetURL(ctx context.Context, urls URLs, progress io.Reader,
}
}
for k, v := range urls.SourceContent.UserMetadata {
metadata[k] = v
}
for k, v := range urls.SourceContent.Metadata {
metadata[k] = v
}
// Optimize for server side copy if the host is same.
if sourceAlias == targetAlias {
for k, v := range urls.SourceContent.UserMetadata {
metadata[k] = v
}
for k, v := range urls.SourceContent.Metadata {
metadata[k] = v
}
// If no metadata populated already by the caller
// just do a Stat() to obtain the metadata.
if len(metadata) == 0 {
metadata, err = getAllMetadata(sourceAlias, sourceURL.String(), srcSSE, urls)
metadata, err = getAllMetadata(sourceAlias, sourceURL.String(), srcSSE, urls, preserve)
if err != nil {
return urls.WithError(err.Trace(sourceURL.String()))
}
@@ -418,23 +449,27 @@ func uploadSourceToTargetURL(ctx context.Context, urls URLs, progress io.Reader,
err = copySourceToTargetURL(targetAlias, targetURL.String(), sourcePath, mode, until, urls.TargetContent.LegalHold, length,
progress, srcSSE, tgtSSE, filterMetadata(metadata), urls.DisableMultipart)
} else {
if len(metadata) == 0 {
metadata, err = getAllMetadata(sourceAlias, sourceURL.String(), srcSSE, urls)
if err != nil {
return urls.WithError(err.Trace(sourceURL.String()))
}
}
if urls.SourceContent.Retention {
// If no metadata populated already by the caller
// just do a Stat() to obtain the metadata.
if len(metadata) == 0 {
metadata, err = getAllMetadata(sourceAlias, sourceURL.String(), srcSSE, urls, preserve)
if err != nil {
return urls.WithError(err.Trace(sourceURL.String()))
}
}
err = putTargetRetention(ctx, targetAlias, targetURL.String(), metadata)
return urls.WithError(err.Trace(sourceURL.String()))
}
var reader io.ReadCloser
// Proceed with regular stream copy.
reader, metadata, err = getSourceStream(sourceAlias, sourceURL.String(), true, srcSSE)
reader, metadata, err = getSourceStream(sourceAlias, sourceURL.String(), true, srcSSE, preserve)
if err != nil {
return urls.WithError(err.Trace(sourceURL.String()))
}
defer reader.Close()
// Get metadata from target content as well
for k, v := range urls.TargetContent.Metadata {
metadata[k] = v
@@ -443,8 +478,16 @@ func uploadSourceToTargetURL(ctx context.Context, urls URLs, progress io.Reader,
for k, v := range urls.TargetContent.UserMetadata {
metadata[k] = v
}
_, err = putTargetStream(ctx, targetAlias, targetURL.String(), mode, until, urls.TargetContent.LegalHold, reader, length, filterMetadata(metadata),
progress, tgtSSE, urls.DisableMultipart)
if isReadAt(reader) {
_, err = putTargetStream(ctx, targetAlias, targetURL.String(), mode, until,
urls.TargetContent.LegalHold, reader, length, filterMetadata(metadata),
progress, tgtSSE, urls.DisableMultipart)
} else {
_, err = putTargetStream(ctx, targetAlias, targetURL.String(), mode, until,
urls.TargetContent.LegalHold, io.LimitReader(reader, length),
length, filterMetadata(metadata), progress, tgtSSE, urls.DisableMultipart)
}
}
if err != nil {
return urls.WithError(err.Trace(sourceURL.String()))
@@ -497,31 +540,3 @@ func newClient(aliasedURL string) (Client, *probe.Error) {
}
return newClientFromAlias(alias, urlStrFull)
}
// Return the file attribute value present in metadata
func getFileAttrMeta(sURLs URLs, encKeyDB map[string][]prefixSSEPair) (string, *probe.Error) {
sourceAlias := sURLs.SourceAlias
sourceURL := sURLs.SourceContent.URL
sourcePath := filepath.ToSlash(filepath.Join(sourceAlias, sURLs.SourceContent.URL.Path))
srcSSE := getSSE(sourcePath, encKeyDB[sourceAlias])
statSourceURL := sURLs.SourceAlias + getKey(sURLs.SourceContent)
srcClt, err := newClient(statSourceURL)
if err != nil {
return "", err.Trace(sourceURL.String())
}
sourceMeta, err := srcClt.Stat(false, true, srcSSE)
if err != nil {
return "", err.Trace(sourceURL.String())
}
attrValue := ""
if sourceMeta.Metadata["X-Amz-Meta-Mc-Attrs"] != "" {
attrValue = sourceMeta.Metadata["X-Amz-Meta-Mc-Attrs"]
} else if sourceMeta.Metadata["X-Amz-Meta-S3cmd-Attrs"] != "" {
attrValue = sourceMeta.Metadata["X-Amz-Meta-S3cmd-Attrs"]
} else if sourceMeta.Metadata["mc-attrs"] != "" {
attrValue = sourceMeta.Metadata["mc-attrs"]
}
return attrValue, nil
}
+4 -16
View File
@@ -212,7 +212,7 @@ type ProgressReader interface {
}
// doCopy - Copy a single file from source to destination
func doCopy(ctx context.Context, cpURLs URLs, pg ProgressReader, encKeyDB map[string][]prefixSSEPair, isMvCmd bool) URLs {
func doCopy(ctx context.Context, cpURLs URLs, pg ProgressReader, encKeyDB map[string][]prefixSSEPair, isMvCmd bool, preserve bool) URLs {
if cpURLs.Error != nil {
cpURLs.Error = cpURLs.Error.Trace()
return cpURLs
@@ -238,7 +238,7 @@ func doCopy(ctx context.Context, cpURLs URLs, pg ProgressReader, encKeyDB map[st
})
}
urls := uploadSourceToTargetURL(ctx, cpURLs, pg, encKeyDB)
urls := uploadSourceToTargetURL(ctx, cpURLs, pg, encKeyDB, preserve)
if isMvCmd && urls.Error == nil {
bgRemove(sourcePath)
}
@@ -489,19 +489,7 @@ func doCopySession(cli *cli.Context, session *sessionV8, encKeyDB map[string][]p
}
}
// If one needs to store the file system information by passing -a flag
if preserve := cli.Bool("preserve"); preserve {
attrValue, pErr := getFileAttrMeta(cpURLs, encKeyDB)
if pErr != nil {
errorIf(pErr, "Unable to fetch file meta info for %s", cpURLs.SourceAlias)
continue
}
if attrValue != "" {
cpURLs.TargetContent.Metadata["mc-attrs"] = attrValue
}
}
preserve := cli.Bool("preserve")
cpURLs.DisableMultipart = cli.Bool("disable-multipart")
// Verify if previously copied, notify progress bar.
@@ -511,7 +499,7 @@ func doCopySession(cli *cli.Context, session *sessionV8, encKeyDB map[string][]p
}
} else {
queueCh <- func() URLs {
return doCopy(ctx, cpURLs, pg, encKeyDB, isMvCmd)
return doCopy(ctx, cpURLs, pg, encKeyDB, isMvCmd, preserve)
}
}
}
+1 -11
View File
@@ -311,16 +311,6 @@ func (mj *mirrorJob) doMirror(ctx context.Context, cancelMirror context.CancelFu
}
}
if mj.isPreserve {
attrValue, pErr := getFileAttrMeta(sURLs, mj.encKeyDB)
if pErr != nil {
return sURLs.WithError(pErr)
}
if attrValue != "" {
sURLs.TargetContent.Metadata["mc-attrs"] = attrValue
}
}
// Initialize additional target user metadata.
sURLs.TargetContent.UserMetadata = mj.userMetadata
@@ -333,7 +323,7 @@ func (mj *mirrorJob) doMirror(ctx context.Context, cancelMirror context.CancelFu
TotalCount: sURLs.TotalCount,
TotalSize: sURLs.TotalSize,
})
return uploadSourceToTargetURL(ctx, sURLs, mj.status, mj.encKeyDB)
return uploadSourceToTargetURL(ctx, sURLs, mj.status, mj.encKeyDB, mj.isPreserve)
}
// Update progress status
+1 -1
View File
@@ -17,7 +17,7 @@ require (
github.com/mattn/go-runewidth v0.0.5 // indirect
github.com/minio/cli v1.22.0
github.com/minio/minio v0.0.0-20200415191640-bde0f444dbab
github.com/minio/minio-go/v6 v6.0.53
github.com/minio/minio-go/v6 v6.0.54-0.20200417072347-d624b8a802c5
github.com/minio/sha256-simd v0.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/profile v1.3.0
+2
View File
@@ -259,6 +259,8 @@ github.com/minio/minio v0.0.0-20200415191640-bde0f444dbab h1:9hlqghJl3e3HorXa6AD
github.com/minio/minio v0.0.0-20200415191640-bde0f444dbab/go.mod h1:v8oQPMMaTkjDwp5cOz1WCElA4Ik+X+0y4On+VMk0fis=
github.com/minio/minio-go/v6 v6.0.53 h1:8jzpwiOzZ5Iz7/goFWqNZRICbyWYShbb5rARjrnSCNI=
github.com/minio/minio-go/v6 v6.0.53/go.mod h1:DIvC/IApeHX8q1BAMVCXSXwpmrmM+I+iBvhvztQorfI=
github.com/minio/minio-go/v6 v6.0.54-0.20200417072347-d624b8a802c5 h1:oYoTfDzXSBQiSyUXECIJrFjTlIV0mFH1kGNPEf6jb6Q=
github.com/minio/minio-go/v6 v6.0.54-0.20200417072347-d624b8a802c5/go.mod h1:DIvC/IApeHX8q1BAMVCXSXwpmrmM+I+iBvhvztQorfI=
github.com/minio/parquet-go v0.0.0-20200414234858-838cfa8aae61/go.mod h1:4trzEJ7N1nBTd5Tt7OCZT5SEin+WiAXpdJ/WgPkESA8=
github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU=
github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=