Merge pull request #1176 from harshavardhana/accounting-reader-for-quiet-mode
Add new accounting reader which prints Total transferred and Average …
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Minio Client (C) 2014, 2015 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
type accountingReader struct {
|
||||
io.ReadCloser
|
||||
acct *accounter
|
||||
}
|
||||
|
||||
type accounter struct {
|
||||
current int64
|
||||
|
||||
Total int64
|
||||
startTime time.Time
|
||||
startValue int64
|
||||
refreshRate time.Duration
|
||||
currentValue int64
|
||||
finishOnce sync.Once
|
||||
isFinished chan struct{}
|
||||
}
|
||||
|
||||
// Instantiate a new accounter
|
||||
func newAccounter(total int64) *accounter {
|
||||
acct := &accounter{
|
||||
Total: total,
|
||||
startTime: time.Now(),
|
||||
startValue: 0,
|
||||
refreshRate: time.Millisecond * 200,
|
||||
isFinished: make(chan struct{}),
|
||||
currentValue: -1,
|
||||
}
|
||||
go acct.writer()
|
||||
return acct
|
||||
}
|
||||
|
||||
func (a *accounter) write(current int64) string {
|
||||
var speedBox string
|
||||
fromStart := time.Now().Sub(a.startTime)
|
||||
currentFromStart := current - a.startValue
|
||||
|
||||
if currentFromStart > 0 {
|
||||
speed := float64(currentFromStart) / (float64(fromStart) / float64(time.Second))
|
||||
speedBox = humanize.IBytes(uint64(speed))
|
||||
}
|
||||
return speedBox + "/s"
|
||||
}
|
||||
|
||||
func (a *accounter) writer() {
|
||||
a.Update()
|
||||
for {
|
||||
select {
|
||||
case <-a.isFinished:
|
||||
return
|
||||
case <-time.After(a.refreshRate):
|
||||
a.Update()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *accounter) Finish() string {
|
||||
var message string
|
||||
a.finishOnce.Do(func() {
|
||||
close(a.isFinished)
|
||||
message = fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", humanize.IBytes(uint64(a.Total)),
|
||||
humanize.IBytes(uint64(a.current)), a.write(atomic.LoadInt64(&a.current)))
|
||||
})
|
||||
return message
|
||||
}
|
||||
|
||||
func (a *accounter) Update() {
|
||||
c := atomic.LoadInt64(&a.current)
|
||||
if c != a.currentValue {
|
||||
a.write(c)
|
||||
a.currentValue = c
|
||||
}
|
||||
}
|
||||
|
||||
func (a *accounter) Add(n int64) int64 {
|
||||
return atomic.AddInt64(&a.current, n)
|
||||
}
|
||||
|
||||
func (a *accounter) NewProxyReader(r io.ReadCloser) *accountingReader {
|
||||
return &accountingReader{r, a}
|
||||
}
|
||||
|
||||
func (a *accountingReader) Read(p []byte) (n int, err error) {
|
||||
n, err = a.ReadCloser.Read(p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
a.acct.Add(int64(n))
|
||||
return
|
||||
}
|
||||
+17
-14
@@ -86,7 +86,7 @@ func (c CopyMessage) JSON() string {
|
||||
}
|
||||
|
||||
// doCopy - Copy a singe file from source to destination
|
||||
func doCopy(cpURLs copyURLs, bar *barSend, cpQueue <-chan bool, wg *sync.WaitGroup, statusCh chan<- copyURLs) {
|
||||
func doCopy(cpURLs copyURLs, progressReader interface{}, cpQueue <-chan bool, wg *sync.WaitGroup, statusCh chan<- copyURLs) {
|
||||
defer wg.Done() // Notify that this copy routine is done.
|
||||
defer func() {
|
||||
<-cpQueue
|
||||
@@ -99,13 +99,13 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue <-chan bool, wg *sync.WaitGro
|
||||
}
|
||||
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar.SetCaption(cpURLs.SourceContent.Name + ": ")
|
||||
progressReader.(*barSend).SetCaption(cpURLs.SourceContent.Name + ": ")
|
||||
}
|
||||
|
||||
reader, length, err := getSource(cpURLs.SourceContent.Name)
|
||||
if err != nil {
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar.ErrorGet(length)
|
||||
progressReader.(*barSend).ErrorGet(length)
|
||||
}
|
||||
cpURLs.Error = err.Trace()
|
||||
statusCh <- cpURLs
|
||||
@@ -119,16 +119,16 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue <-chan bool, wg *sync.WaitGro
|
||||
Target: cpURLs.TargetContent.Name,
|
||||
Length: cpURLs.SourceContent.Size,
|
||||
})
|
||||
newReader = reader
|
||||
newReader = progressReader.(*accounter).NewProxyReader(reader)
|
||||
} else {
|
||||
// set up progress
|
||||
newReader = bar.NewProxyReader(reader)
|
||||
newReader = progressReader.(*barSend).NewProxyReader(reader)
|
||||
}
|
||||
defer newReader.Close()
|
||||
|
||||
if err := putTarget(cpURLs.TargetContent.Name, length, newReader); err != nil {
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar.ErrorPut(length)
|
||||
progressReader.(*barSend).ErrorPut(length)
|
||||
}
|
||||
cpURLs.Error = err.Trace()
|
||||
statusCh <- cpURLs
|
||||
@@ -140,9 +140,9 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue <-chan bool, wg *sync.WaitGro
|
||||
}
|
||||
|
||||
// doCopyFake - Perform a fake copy to update the progress bar appropriately.
|
||||
func doCopyFake(cURLs copyURLs, bar *barSend) {
|
||||
func doCopyFake(cURLs copyURLs, progressReader interface{}) {
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar.Progress(cURLs.SourceContent.Size)
|
||||
progressReader.(*barSend).Progress(cURLs.SourceContent.Size)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,10 +216,11 @@ func doCopySession(session *sessionV2) {
|
||||
doPrepareCopyURLs(session, trapCh)
|
||||
}
|
||||
|
||||
var bar barSend
|
||||
var progressReader interface{}
|
||||
if !globalQuietFlag && !globalJSONFlag { // set up progress bar
|
||||
bar = newProgressBar()
|
||||
bar.Extend(session.Header.TotalBytes)
|
||||
progressReader = newProgressBar(session.Header.TotalBytes)
|
||||
} else {
|
||||
progressReader = newAccounter(session.Header.TotalBytes)
|
||||
}
|
||||
|
||||
// Prepare URL scanner from session data file.
|
||||
@@ -245,7 +246,9 @@ func doCopySession(session *sessionV2) {
|
||||
case cpURLs, ok := <-statusCh: // Receive status.
|
||||
if !ok { // We are done here. Top level function has returned.
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar.Finish()
|
||||
progressReader.(*barSend).Finish()
|
||||
} else {
|
||||
console.Println(console.Colorize("Copy", progressReader.(*accounter).Finish()))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -291,7 +294,7 @@ func doCopySession(session *sessionV2) {
|
||||
var cpURLs copyURLs
|
||||
json.Unmarshal([]byte(scanner.Text()), &cpURLs)
|
||||
if isCopied(cpURLs.SourceContent.Name) {
|
||||
doCopyFake(cpURLs, &bar)
|
||||
doCopyFake(cpURLs, progressReader)
|
||||
} else {
|
||||
// Wait for other copy routines to
|
||||
// complete. We only have limited CPU
|
||||
@@ -300,7 +303,7 @@ func doCopySession(session *sessionV2) {
|
||||
// Account for each copy routines we start.
|
||||
copyWg.Add(1)
|
||||
// Do copying in background concurrently.
|
||||
go doCopy(cpURLs, &bar, cpQueue, copyWg, statusCh)
|
||||
go doCopy(cpURLs, progressReader, cpQueue, copyWg, statusCh)
|
||||
}
|
||||
}
|
||||
copyWg.Wait()
|
||||
|
||||
+17
-14
@@ -82,7 +82,7 @@ func (m MirrorMessage) JSON() string {
|
||||
}
|
||||
|
||||
// doMirror - Mirror an object to multiple destination. mirrorURLs status contains a copy of sURLs and error if any.
|
||||
func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *sync.WaitGroup, statusCh chan<- mirrorURLs) {
|
||||
func doMirror(sURLs mirrorURLs, progressReader interface{}, mirrorQueueCh <-chan bool, wg *sync.WaitGroup, statusCh chan<- mirrorURLs) {
|
||||
defer wg.Done() // Notify that this copy routine is done.
|
||||
defer func() {
|
||||
<-mirrorQueueCh
|
||||
@@ -95,13 +95,13 @@ func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *syn
|
||||
}
|
||||
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar.SetCaption(sURLs.SourceContent.Name + ": ")
|
||||
progressReader.(*barSend).SetCaption(sURLs.SourceContent.Name + ": ")
|
||||
}
|
||||
|
||||
reader, length, err := getSource(sURLs.SourceContent.Name)
|
||||
if err != nil {
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar.ErrorGet(int64(length))
|
||||
progressReader.(*barSend).ErrorGet(int64(length))
|
||||
}
|
||||
sURLs.Error = err.Trace(sURLs.SourceContent.Name)
|
||||
statusCh <- sURLs
|
||||
@@ -119,17 +119,17 @@ func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *syn
|
||||
Source: sURLs.SourceContent.Name,
|
||||
Targets: targetURLs,
|
||||
})
|
||||
newReader = reader
|
||||
newReader = progressReader.(*accounter).NewProxyReader(reader)
|
||||
} else {
|
||||
// set up progress
|
||||
newReader = bar.NewProxyReader(reader)
|
||||
newReader = progressReader.(*barSend).NewProxyReader(reader)
|
||||
}
|
||||
defer newReader.Close()
|
||||
|
||||
err = putTargets(targetURLs, length, newReader)
|
||||
if err != nil {
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar.ErrorPut(int64(length))
|
||||
progressReader.(*barSend).ErrorPut(int64(length))
|
||||
}
|
||||
sURLs.Error = err.Trace(targetURLs...)
|
||||
statusCh <- sURLs
|
||||
@@ -141,9 +141,9 @@ func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *syn
|
||||
}
|
||||
|
||||
// doMirrorFake - Perform a fake mirror to update the progress bar appropriately.
|
||||
func doMirrorFake(sURLs mirrorURLs, bar *barSend) {
|
||||
func doMirrorFake(sURLs mirrorURLs, progressReader interface{}) {
|
||||
if !globalDebugFlag && !globalJSONFlag {
|
||||
bar.Progress(sURLs.SourceContent.Size)
|
||||
progressReader.(*barSend).Progress(sURLs.SourceContent.Size)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,10 +216,11 @@ func doMirrorSession(session *sessionV2) {
|
||||
}
|
||||
|
||||
// Set up progress bar.
|
||||
var bar barSend
|
||||
var progressReader interface{}
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar = newProgressBar()
|
||||
bar.Extend(session.Header.TotalBytes)
|
||||
progressReader = newProgressBar(session.Header.TotalBytes)
|
||||
} else {
|
||||
progressReader = newAccounter(session.Header.TotalBytes)
|
||||
}
|
||||
|
||||
// Prepare URL scanner from session data file.
|
||||
@@ -244,7 +245,9 @@ func doMirrorSession(session *sessionV2) {
|
||||
case sURLs, ok := <-statusCh: // Receive status.
|
||||
if !ok { // We are done here. Top level function has returned.
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
bar.Finish()
|
||||
progressReader.(*barSend).Finish()
|
||||
} else {
|
||||
console.Println(console.Colorize("Mirror", progressReader.(*accounter).Finish()))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -291,7 +294,7 @@ func doMirrorSession(session *sessionV2) {
|
||||
var sURLs mirrorURLs
|
||||
json.Unmarshal([]byte(scanner.Text()), &sURLs)
|
||||
if isCopied(sURLs.SourceContent.Name) {
|
||||
doMirrorFake(sURLs, &bar)
|
||||
doMirrorFake(sURLs, progressReader)
|
||||
} else {
|
||||
// Wait for other mirror routines to
|
||||
// complete. We only have limited CPU
|
||||
@@ -300,7 +303,7 @@ func doMirrorSession(session *sessionV2) {
|
||||
// Account for each mirror routines we start.
|
||||
mirrorWg.Add(1)
|
||||
// Do mirroring in background concurrently.
|
||||
go doMirror(sURLs, &bar, mirrorQueue, mirrorWg, statusCh)
|
||||
go doMirror(sURLs, progressReader, mirrorQueue, mirrorWg, statusCh)
|
||||
}
|
||||
}
|
||||
mirrorWg.Wait()
|
||||
|
||||
+6
-16
@@ -21,7 +21,6 @@ import (
|
||||
"io"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
@@ -36,8 +35,7 @@ import (
|
||||
type pbBar int
|
||||
|
||||
const (
|
||||
pbBarExtend pbBar = iota
|
||||
pbBarProgress
|
||||
pbBarProgress pbBar = iota
|
||||
pbBarFinish
|
||||
pbBarPutError
|
||||
pbBarGetError
|
||||
@@ -73,10 +71,6 @@ func (b *barSend) NewProxyReader(r io.ReadCloser) *proxyReader {
|
||||
return &proxyReader{r, b}
|
||||
}
|
||||
|
||||
func (b barSend) Extend(total int64) {
|
||||
b.opCh <- barMsg{Op: pbBarExtend, Arg: total}
|
||||
}
|
||||
|
||||
func (b barSend) Progress(progress int64) {
|
||||
b.opCh <- barMsg{Op: pbBarProgress, Arg: progress}
|
||||
}
|
||||
@@ -147,16 +141,16 @@ func getFixedWidth(width, percent int) int {
|
||||
}
|
||||
|
||||
// newProgressBar - instantiate a pbBar.
|
||||
func newProgressBar() barSend {
|
||||
func newProgressBar(total int64) *barSend {
|
||||
console.SetCustomPalette(map[string]*color.Color{
|
||||
"Bar": color.New(color.FgGreen, color.Bold),
|
||||
})
|
||||
cmdCh := make(chan barMsg)
|
||||
finishCh := make(chan bool)
|
||||
go func(cmdCh <-chan barMsg, finishCh chan<- bool) {
|
||||
go func(total int64, cmdCh <-chan barMsg, finishCh chan<- bool) {
|
||||
var started bool
|
||||
var totalBytesRead int64 // total amounts of bytes read
|
||||
bar := pb.New64(0)
|
||||
bar := pb.New64(total)
|
||||
bar.SetUnits(pb.U_BYTES)
|
||||
bar.SetRefreshRate(time.Millisecond * 125)
|
||||
bar.NotPrint = true
|
||||
@@ -177,10 +171,6 @@ func newProgressBar() barSend {
|
||||
switch msg.Op {
|
||||
case pbBarSetCaption:
|
||||
bar.Prefix(fixateBarCaption(msg.Arg.(string), getFixedWidth(bar.GetWidth(), 18)))
|
||||
case pbBarExtend:
|
||||
if msg.Arg.(int64) > 0 {
|
||||
atomic.AddInt64(&bar.Total, msg.Arg.(int64))
|
||||
}
|
||||
case pbBarProgress:
|
||||
if bar.Total > 0 && !started {
|
||||
started = true
|
||||
@@ -206,8 +196,8 @@ func newProgressBar() barSend {
|
||||
return
|
||||
}
|
||||
}
|
||||
}(cmdCh, finishCh)
|
||||
return barSend{cmdCh, finishCh}
|
||||
}(total, cmdCh, finishCh)
|
||||
return &barSend{cmdCh, finishCh}
|
||||
}
|
||||
|
||||
/******************************** Scan Bar ************************************/
|
||||
|
||||
Reference in New Issue
Block a user