cp/diff/mirror: Cleanup code and deprecate unnecessary flags. (#1914)
This fix also fixes an apparent crash found in diff.
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4bdc07]
goroutine 84 [running]:
panic(0x810ea0, 0xc4200120e0)
/home/builder/.gimme/versions/go1.7.3.linux.amd64/src/runtime/panic.go:500 +0x1a1
github.com/minio/mc/cmd.objectDifference.func1(0xc4200880a8, 0xc42042ee56, 0xc420315ec0, 0xc4200880b0, 0xc42042ee57, 0xc420315f20, 0xc42042ee54, 0xc42042ee55, 0xc420315501, 0xc4200880a0, ...)
/home/builder/mygo/src/github.com/minio/mc/cmd/difference.go:109 +0x197
created by github.com/minio/mc/cmd.objectDifference
/home/builder/mygo/src/github.com/minio/mc/cmd/difference.go:180 +0x2f5
```
This commit is contained in:
+33
-2
@@ -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.
|
||||
|
||||
+2
-54
@@ -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)
|
||||
|
||||
+1
-3
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
+3
-5
@@ -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 {
|
||||
|
||||
@@ -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 (
|
||||
|
||||
+2
-45
@@ -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
|
||||
|
||||
+4
-4
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user