cp/mirror: avoid catch all interface{} for readers.

This commit is contained in:
Harshavardhana
2015-11-16 18:29:59 -08:00
parent 0a3cc546a5
commit 0e56a3b900
3 changed files with 114 additions and 45 deletions
+15 -16
View File
@@ -17,13 +17,10 @@
package main
import (
"fmt"
"io"
"sync"
"sync/atomic"
"time"
"github.com/minio/pb"
)
type accountingReader struct {
@@ -57,19 +54,14 @@ func newAccounter(total int64) *accounter {
return acct
}
func (a *accounter) write(current int64) string {
var speedBox string
func (a *accounter) write(current int64) float64 {
fromStart := time.Now().Sub(a.startTime)
currentFromStart := current - a.startValue
if currentFromStart > 0 {
speed := float64(currentFromStart) / (float64(fromStart) / float64(time.Second))
speedBox = pb.FormatBytes(int64(speed))
return speed
}
if speedBox == "" {
speedBox = "0 MB"
}
return speedBox + "/s"
return 0.0
}
func (a *accounter) writer() {
@@ -84,14 +76,21 @@ func (a *accounter) writer() {
}
}
func (a *accounter) Finish() string {
var message string
type accountStat struct {
Total int64
Transferred int64
Speed float64
}
func (a *accounter) Stat() accountStat {
var acntStat accountStat
a.finishOnce.Do(func() {
close(a.isFinished)
message = fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.FormatBytes(a.Total),
pb.FormatBytes(a.current), a.write(atomic.LoadInt64(&a.current)))
acntStat.Total = a.Total
acntStat.Transferred = a.current
acntStat.Speed = a.write(atomic.LoadInt64(&a.current))
})
return message
return acntStat
}
func (a *accounter) Update() {
+52 -15
View File
@@ -32,6 +32,7 @@ import (
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio-xl/pkg/probe"
"github.com/minio/pb"
)
var (
@@ -97,8 +98,28 @@ func (c copyMessage) JSON() string {
return string(copyMessageBytes)
}
// copyStatMessage container for copy accounting message
type copyStatMessage struct {
Total int64
Transferred int64
Speed float64
}
// copyStatMessage copy accounting message
func (c copyStatMessage) String() string {
speedBox := pb.FormatBytes(int64(c.Speed))
if speedBox == "" {
speedBox = "0 MB"
} else {
speedBox = speedBox + "/s"
}
message := fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.FormatBytes(c.Total),
pb.FormatBytes(c.Transferred), speedBox)
return message
}
// doCopy - Copy a singe file from source to destination
func doCopy(cpURLs copyURLs, progressReader interface{}, cpQueue <-chan bool, wg *sync.WaitGroup, statusCh chan<- copyURLs) {
func doCopy(cpURLs copyURLs, progressReader *barSend, accountingReader *accounter, cpQueue <-chan bool, wg *sync.WaitGroup, statusCh chan<- copyURLs) {
defer wg.Done() // Notify that this copy routine is done.
defer func() {
<-cpQueue
@@ -111,13 +132,13 @@ func doCopy(cpURLs copyURLs, progressReader interface{}, cpQueue <-chan bool, wg
}
if !globalQuietFlag && !globalJSONFlag {
progressReader.(*barSend).SetCaption(cpURLs.SourceContent.URL.String() + ": ")
progressReader.SetCaption(cpURLs.SourceContent.URL.String() + ": ")
}
reader, length, err := getSource(cpURLs.SourceContent.URL.String())
if err != nil {
if !globalQuietFlag && !globalJSONFlag {
progressReader.(*barSend).ErrorGet(length)
progressReader.ErrorGet(length)
}
cpURLs.Error = err.Trace()
statusCh <- cpURLs
@@ -131,16 +152,23 @@ func doCopy(cpURLs copyURLs, progressReader interface{}, cpQueue <-chan bool, wg
Target: cpURLs.TargetContent.URL.String(),
Length: cpURLs.SourceContent.Size,
})
newReader = progressReader.(*accounter).NewProxyReader(reader)
// No accounting necessary for JSON output.
if globalJSONFlag {
newReader = reader
}
// Proxy reader to accounting reader only during quiet mode.
if globalQuietFlag {
newReader = accountingReader.NewProxyReader(reader)
}
} else {
// set up progress
newReader = progressReader.(*barSend).NewProxyReader(reader)
newReader = progressReader.NewProxyReader(reader)
}
defer newReader.Close()
if err := putTarget(cpURLs.TargetContent.URL.String(), length, newReader); err != nil {
if !globalQuietFlag && !globalJSONFlag {
progressReader.(*barSend).ErrorPut(length)
progressReader.ErrorPut(length)
}
cpURLs.Error = err.Trace()
statusCh <- cpURLs
@@ -152,9 +180,9 @@ func doCopy(cpURLs copyURLs, progressReader interface{}, cpQueue <-chan bool, wg
}
// doCopyFake - Perform a fake copy to update the progress bar appropriately.
func doCopyFake(cURLs copyURLs, progressReader interface{}) {
func doCopyFake(cURLs copyURLs, progressReader *barSend) {
if !globalQuietFlag && !globalJSONFlag {
progressReader.(*barSend).Progress(cURLs.SourceContent.Size)
progressReader.Progress(cURLs.SourceContent.Size)
}
}
@@ -232,11 +260,13 @@ func doCopySession(session *sessionV3) {
doPrepareCopyURLs(session, trapCh)
}
var progressReader interface{}
// Enable accounting reader by default.
accntReader := newAccounter(session.Header.TotalBytes)
// Enable progress bar reader only during default mode.
var progressReader *barSend
if !globalQuietFlag && !globalJSONFlag { // set up progress bar
progressReader = newProgressBar(session.Header.TotalBytes)
} else {
progressReader = newAccounter(session.Header.TotalBytes)
}
// Prepare URL scanner from session data file.
@@ -262,9 +292,16 @@ func doCopySession(session *sessionV3) {
case cpURLs, ok := <-statusCh: // Receive status.
if !ok { // We are done here. Top level function has returned.
if !globalQuietFlag && !globalJSONFlag {
progressReader.(*barSend).Finish()
} else {
console.Println(console.Colorize("Copy", progressReader.(*accounter).Finish()))
progressReader.Finish()
}
if globalQuietFlag {
accntStat := accntReader.Stat()
cpStatMessage := copyStatMessage{
Total: accntStat.Total,
Transferred: accntStat.Transferred,
Speed: accntStat.Speed,
}
console.Println(console.Colorize("Copy", cpStatMessage.String()))
}
return
}
@@ -319,7 +356,7 @@ func doCopySession(session *sessionV3) {
// Account for each copy routines we start.
copyWg.Add(1)
// Do copying in background concurrently.
go doCopy(cpURLs, progressReader, cpQueue, copyWg, statusCh)
go doCopy(cpURLs, progressReader, accntReader, cpQueue, copyWg, statusCh)
}
}
copyWg.Wait()
+47 -14
View File
@@ -31,6 +31,7 @@ import (
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio-xl/pkg/probe"
"github.com/minio/pb"
)
// mirror specific flags.
@@ -95,8 +96,28 @@ func (m mirrorMessage) JSON() string {
return string(mirrorMessageBytes)
}
// mirrorStatMessage container for mirror accounting message
type mirrorStatMessage struct {
Total int64
Transferred int64
Speed float64
}
// mirrorStatMessage mirror accounting message
func (c mirrorStatMessage) String() string {
speedBox := pb.FormatBytes(int64(c.Speed))
if speedBox == "" {
speedBox = "0 MB"
} else {
speedBox = speedBox + "/s"
}
message := fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.FormatBytes(c.Total),
pb.FormatBytes(c.Transferred), speedBox)
return message
}
// doMirror - Mirror an object to multiple destination. mirrorURLs status contains a copy of sURLs and error if any.
func doMirror(sURLs mirrorURLs, progressReader interface{}, mirrorQueueCh <-chan bool, wg *sync.WaitGroup, statusCh chan<- mirrorURLs) {
func doMirror(sURLs mirrorURLs, progressReader *barSend, accountingReader *accounter, mirrorQueueCh <-chan bool, wg *sync.WaitGroup, statusCh chan<- mirrorURLs) {
defer wg.Done() // Notify that this copy routine is done.
defer func() {
<-mirrorQueueCh
@@ -109,13 +130,13 @@ func doMirror(sURLs mirrorURLs, progressReader interface{}, mirrorQueueCh <-chan
}
if !globalQuietFlag && !globalJSONFlag {
progressReader.(*barSend).SetCaption(sURLs.SourceContent.URL.String() + ": ")
progressReader.SetCaption(sURLs.SourceContent.URL.String() + ": ")
}
reader, length, err := getSource(sURLs.SourceContent.URL.String())
if err != nil {
if !globalQuietFlag && !globalJSONFlag {
progressReader.(*barSend).ErrorGet(int64(length))
progressReader.ErrorGet(int64(length))
}
sURLs.Error = err.Trace(sURLs.SourceContent.URL.String())
statusCh <- sURLs
@@ -133,17 +154,22 @@ func doMirror(sURLs mirrorURLs, progressReader interface{}, mirrorQueueCh <-chan
Source: sURLs.SourceContent.URL.String(),
Targets: targetURLs,
})
newReader = progressReader.(*accounter).NewProxyReader(reader)
if globalJSONFlag {
newReader = reader
}
if globalQuietFlag {
newReader = accountingReader.NewProxyReader(reader)
}
} else {
// set up progress
newReader = progressReader.(*barSend).NewProxyReader(reader)
newReader = progressReader.NewProxyReader(reader)
}
defer newReader.Close()
err = putTargets(targetURLs, length, newReader)
if err != nil {
if !globalQuietFlag && !globalJSONFlag {
progressReader.(*barSend).ErrorPut(int64(length))
progressReader.ErrorPut(int64(length))
}
sURLs.Error = err.Trace(targetURLs...)
statusCh <- sURLs
@@ -155,9 +181,9 @@ func doMirror(sURLs mirrorURLs, progressReader interface{}, mirrorQueueCh <-chan
}
// doMirrorFake - Perform a fake mirror to update the progress bar appropriately.
func doMirrorFake(sURLs mirrorURLs, progressReader interface{}) {
func doMirrorFake(sURLs mirrorURLs, progressReader *barSend) {
if !globalDebugFlag && !globalJSONFlag {
progressReader.(*barSend).Progress(sURLs.SourceContent.Size)
progressReader.Progress(sURLs.SourceContent.Size)
}
}
@@ -233,12 +259,13 @@ func doMirrorSession(session *sessionV3) {
doPrepareMirrorURLs(session, trapCh)
}
// Enable accounting reader by default.
accntReader := newAccounter(session.Header.TotalBytes)
// Set up progress bar.
var progressReader interface{}
var progressReader *barSend
if !globalQuietFlag && !globalJSONFlag {
progressReader = newProgressBar(session.Header.TotalBytes)
} else {
progressReader = newAccounter(session.Header.TotalBytes)
}
// Prepare URL scanner from session data file.
@@ -263,9 +290,15 @@ func doMirrorSession(session *sessionV3) {
case sURLs, ok := <-statusCh: // Receive status.
if !ok { // We are done here. Top level function has returned.
if !globalQuietFlag && !globalJSONFlag {
progressReader.(*barSend).Finish()
progressReader.Finish()
} else {
console.Println(console.Colorize("Mirror", progressReader.(*accounter).Finish()))
accntStat := accntReader.Stat()
mrStatMessage := mirrorStatMessage{
Total: accntStat.Total,
Transferred: accntStat.Transferred,
Speed: accntStat.Speed,
}
console.Println(console.Colorize("Mirror", mrStatMessage.String()))
}
return
}
@@ -321,7 +354,7 @@ func doMirrorSession(session *sessionV3) {
// Account for each mirror routines we start.
mirrorWg.Add(1)
// Do mirroring in background concurrently.
go doMirror(sURLs, progressReader, mirrorQueue, mirrorWg, statusCh)
go doMirror(sURLs, progressReader, accntReader, mirrorQueue, mirrorWg, statusCh)
}
}
mirrorWg.Wait()