diff --git a/cmd/common-methods.go b/cmd/common-methods.go index 503a8c4c..19350351 100644 --- a/cmd/common-methods.go +++ b/cmd/common-methods.go @@ -98,8 +98,8 @@ func putTargetStream(urlStr string, reader io.Reader, size int64) (int64, *probe return putTargetStreamFromAlias(alias, urlStrFull, reader, size, nil) } -// copyTargetStreamFromAlias copies to URL from source. -func copySourceStreamFromAlias(alias string, urlStr string, source string, size int64, progress io.Reader) *probe.Error { +// copySourceToTargetURL copies to targetURL from source. +func copySourceToTargetURL(alias string, urlStr string, source string, size int64, progress io.Reader) *probe.Error { targetClnt, err := newClientFromAlias(alias, urlStr) if err != nil { return err.Trace(alias, urlStr) @@ -111,6 +111,37 @@ func copySourceStreamFromAlias(alias string, urlStr string, source string, size return nil } +// uploadSourceToTargetURL - uploads to targetURL from source. +// optionally optimizes copy for object sizes <= 5GiB by using +// server side copy operation. +func uploadSourceToTargetURL(urls URLs, progress io.Reader) URLs { + sourceAlias := urls.SourceAlias + sourceURL := urls.SourceContent.URL + targetAlias := urls.TargetAlias + targetURL := urls.TargetContent.URL + length := urls.SourceContent.Size + + // Optimize for server side copy if object is <= 5GiB and the host is same. + if length <= globalMaximumPutSize && sourceAlias == targetAlias { + sourcePath := filepath.ToSlash(sourceURL.Path) + err := copySourceToTargetURL(targetAlias, targetURL.String(), sourcePath, length, progress) + if err != nil { + return urls.WithError(err.Trace(sourceURL.String())) + } + } else { + // Proceed with regular stream copy. + reader, err := getSourceStreamFromAlias(sourceAlias, sourceURL.String()) + if err != nil { + return urls.WithError(err.Trace(sourceURL.String())) + } + _, err = putTargetStreamFromAlias(targetAlias, targetURL.String(), reader, length, progress) + if err != nil { + return urls.WithError(err.Trace(targetURL.String())) + } + } + return urls.WithError(nil) +} + // newClientFromAlias gives a new client interface for matching // alias entry in the mc config file. If no matching host config entry // is found, fs client is returned. diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 4ec4b21e..0b9e014d 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -111,11 +111,6 @@ type copyStatMessage struct { Speed float64 } -const ( - // 5GiB. - fiveGB = 5 * 1024 * 1024 * 1024 -) - // copyStatMessage copy accounting message func (c copyStatMessage) String() string { speedBox := pb.Format(int64(c.Speed)).To(pb.U_BYTES).String() @@ -165,54 +160,7 @@ func doCopy(cpURLs URLs, progressReader *progressBar, accountingReader *accounte // Set up progress reader. progress = progressReader.ProgressBar } - // If source size is <= 5GB and operation is across same server type try to use Copy. - if length <= fiveGB && (sourceURL.Type == targetURL.Type) { - // FS -> FS Copy includes alias in path. - if sourceURL.Type == fileSystem { - sourcePath := filepath.ToSlash(filepath.Join(sourceAlias, sourceURL.Path)) - err := copySourceStreamFromAlias(targetAlias, targetURL.String(), sourcePath, length, progress) - if err != nil { - cpURLs.Error = err.Trace(sourceURL.String()) - return cpURLs - } - } else if sourceURL.Type == objectStorage { - // If source/target are object storage their aliases must be the same. - if sourceAlias == targetAlias { - // Do not include alias inside path for ObjStore -> ObjStore. - sourcePath := filepath.ToSlash(sourceURL.Path) - err := copySourceStreamFromAlias(targetAlias, targetURL.String(), sourcePath, length, progress) - if err != nil { - cpURLs.Error = err.Trace(sourceURL.String()) - return cpURLs - } - } else { - reader, err := getSourceStreamFromAlias(sourceAlias, sourceURL.String()) - if err != nil { - cpURLs.Error = err.Trace(sourceURL.String()) - return cpURLs - } - _, err = putTargetStreamFromAlias(targetAlias, targetURL.String(), reader, length, progress) - if err != nil { - cpURLs.Error = err.Trace(targetURL.String()) - return cpURLs - } - } - } - } else { - // Standard GET/PUT for size > 5GB. - reader, err := getSourceStreamFromAlias(sourceAlias, sourceURL.String()) - if err != nil { - cpURLs.Error = err.Trace(sourceURL.String()) - return cpURLs - } - _, err = putTargetStreamFromAlias(targetAlias, targetURL.String(), reader, length, progress) - if err != nil { - cpURLs.Error = err.Trace(targetURL.String()) - return cpURLs - } - } - cpURLs.Error = nil // just for safety - return cpURLs + return uploadSourceToTargetURL(cpURLs, progress) } // doCopyFake - Perform a fake copy to update the progress bar appropriately. @@ -293,7 +241,7 @@ func doPrepareCopyURLs(session *sessionV8, trapCh <-chan bool) { } func doCopySession(session *sessionV8) { - trapCh := signalTrap(os.Interrupt, syscall.SIGTERM) + trapCh := signalTrap(os.Interrupt, syscall.SIGTERM, syscall.SIGKILL) if !session.HasData() { doPrepareCopyURLs(session, trapCh) diff --git a/cmd/diff-main.go b/cmd/diff-main.go index aaceab24..13ce7937 100644 --- a/cmd/diff-main.go +++ b/cmd/diff-main.go @@ -170,10 +170,8 @@ func doDiffMain(firstURL, secondURL string) error { fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL)) } - watchMode := false - // Diff first and second urls. - for diffMsg := range objectDifference(firstClient, secondClient, firstURL, secondURL, watchMode) { + for diffMsg := range objectDifference(firstClient, secondClient, firstURL, secondURL) { printMsg(diffMsg) } diff --git a/cmd/difference.go b/cmd/difference.go index 0a972d6b..b539c18a 100644 --- a/cmd/difference.go +++ b/cmd/difference.go @@ -50,7 +50,7 @@ func (d differType) String() string { // objectDifference function finds the difference between all objects // recursively in sorted order from source and target. -func objectDifference(sourceClnt, targetClnt Client, sourceURL, targetURL string, watchMode bool) (diffCh chan diffMessage) { +func objectDifference(sourceClnt, targetClnt Client, sourceURL, targetURL string) (diffCh chan diffMessage) { var ( srcEOF, tgtEOF bool srcOk, tgtOk bool @@ -77,10 +77,8 @@ func objectDifference(sourceClnt, targetClnt Client, sourceURL, targetURL string // No objects from source AND target: Finish if srcEOF && tgtEOF { - if !watchMode { - close(diffCh) - break - } + close(diffCh) + break } if !srcEOF && srcCtnt.Err != nil { diff --git a/cmd/globals.go b/cmd/globals.go index 2b619130..5aa28140 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -47,6 +47,9 @@ const ( // Global error exit status. globalErrorExitStatus = 1 + + // Maximum size for a single PUT operation. + globalMaximumPutSize = 5 * 1024 * 1024 * 1024 // 5GiB. ) var ( diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index c6f454fe..952c3106 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -228,49 +228,7 @@ func (ms *mirrorSession) doMirror(sURLs URLs) URLs { TotalCount: sURLs.TotalCount, TotalSize: sURLs.TotalSize, }) - - // If source size is <= 5GB and operation is across same server type try to use Copy. - if length <= fiveGB && sourceURL.Type == targetURL.Type { - // FS -> FS Copy includes alias in path. - if sourceURL.Type == fileSystem { - sourcePath := filepath.ToSlash(filepath.Join(sourceAlias, sourceURL.Path)) - err := copySourceStreamFromAlias(targetAlias, targetURL.String(), sourcePath, length, ms.status) - if err != nil { - return sURLs.WithError(err.Trace(sourceURL.String())) - } - } else if sourceURL.Type == objectStorage { - if sourceAlias == targetAlias { - // If source/target are object storage their aliases must be the same - // Do not include alias inside path for ObjStore -> ObjStore. - sourcePath := filepath.ToSlash(sourceURL.Path) - err := copySourceStreamFromAlias(targetAlias, targetURL.String(), sourcePath, length, ms.status) - if err != nil { - return sURLs.WithError(err.Trace(sourceURL.String())) - } - } else { - reader, err := getSourceStreamFromAlias(sourceAlias, sourceURL.String()) - if err != nil { - return sURLs.WithError(err.Trace(sourceURL.String())) - } - _, err = putTargetStreamFromAlias(targetAlias, targetURL.String(), reader, length, ms.status) - if err != nil { - return sURLs.WithError(err.Trace(targetURL.String())) - } - } - } - } else { - // Standard GET/PUT for size > 5GB - reader, err := getSourceStreamFromAlias(sourceAlias, sourceURL.String()) - if err != nil { - return sURLs.WithError(err.Trace(sourceURL.String())) - } - _, err = putTargetStreamFromAlias(targetAlias, targetURL.String(), reader, length, ms.status) - if err != nil { - return sURLs.WithError(err.Trace(targetURL.String())) - } - } - - return sURLs.WithError(nil) + return uploadSourceToTargetURL(sURLs, ms.status) } // Go routine to update session status @@ -549,11 +507,10 @@ func (ms *mirrorSession) harvestSourceUrls(recursive bool) { isForce := ms.Header.CommandBoolFlags["force"] isFake := ms.Header.CommandBoolFlags["fake"] isRemove := ms.Header.CommandBoolFlags["remove"] - isWatch := ms.Header.CommandBoolFlags["watch"] defer close(ms.harvestCh) - URLsCh := prepareMirrorURLs(ms.sourceURL, ms.targetURL, isForce, isFake, isRemove, isWatch) + URLsCh := prepareMirrorURLs(ms.sourceURL, ms.targetURL, isForce, isFake, isRemove) for url := range URLsCh { // Send harvested urls. ms.harvestCh <- url diff --git a/cmd/mirror-url.go b/cmd/mirror-url.go index 7e0d8f41..b9c60054 100644 --- a/cmd/mirror-url.go +++ b/cmd/mirror-url.go @@ -76,7 +76,7 @@ func checkMirrorSyntax(ctx *cli.Context) { } } -func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake bool, isRemove bool, isWatch bool, URLsCh chan<- URLs) { +func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake bool, isRemove bool, URLsCh chan<- URLs) { // source and targets are always directories sourceSeparator := string(newClientURL(sourceURL).Separator) if !strings.HasSuffix(sourceURL, sourceSeparator) { @@ -106,7 +106,7 @@ func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake } // List both source and target, compare and return values through channel. - for diffMsg := range objectDifference(sourceClnt, targetClnt, sourceURL, targetURL, isWatch) { + for diffMsg := range objectDifference(sourceClnt, targetClnt, sourceURL, targetURL) { switch diffMsg.Diff { case differInNone: // No difference, continue. @@ -170,8 +170,8 @@ func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake } // Prepares urls that need to be copied or removed based on requested options. -func prepareMirrorURLs(sourceURL string, targetURL string, isForce bool, isFake bool, isWatch bool, isRemove bool) <-chan URLs { +func prepareMirrorURLs(sourceURL string, targetURL string, isForce bool, isFake bool, isRemove bool) <-chan URLs { URLsCh := make(chan URLs) - go deltaSourceTarget(sourceURL, targetURL, isForce, isFake, isRemove, isWatch, URLsCh) + go deltaSourceTarget(sourceURL, targetURL, isForce, isFake, isRemove, URLsCh) return URLsCh }