new countlocker to balance progress bar and copy routines

This commit is contained in:
Anand Babu (AB) Periasamy
2015-05-27 04:10:51 -07:00
parent ee8d608690
commit 98299b267e
3 changed files with 163 additions and 15 deletions
+31 -15
View File
@@ -25,6 +25,7 @@ import (
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/mc/pkg/countlock"
"github.com/minio/minio/pkg/iodine"
)
@@ -66,28 +67,37 @@ func args2URLs(args cli.Args) ([]string, error) {
return URLs, nil
}
func doCopyInRoutine(cpurls *cpURLs, bar *barSend, cpQueue chan bool, ch chan error, wg *sync.WaitGroup) {
func doCopyInRoutine(cpurls *cpURLs, bar *barSend, cpQueue chan bool, errCh chan error, wg *sync.WaitGroup) {
defer wg.Done()
srcConfig, err := getHostConfig(cpurls.SourceContent.Name)
if err != nil {
ch <- err
errCh <- err
return
}
tgtConfig, err := getHostConfig(cpurls.TargetContent.Name)
if err != nil {
ch <- err
errCh <- err
return
}
if err := doCopy(cpurls.SourceContent.Name, srcConfig, cpurls.TargetContent.Name, tgtConfig, bar); err != nil {
ch <- err
errCh <- err
}
<-cpQueue
<-cpQueue // Signal that this copy routine is done.
}
func doCopyCmd(sourceURLs []string, targetURL string, bar barSend) <-chan error {
ch := make(chan error)
go func(sourceURLs []string, targetURL string, bar barSend, ch chan error) {
defer close(ch)
errCh := make(chan error)
go func(sourceURLs []string, targetURL string, bar barSend, errCh chan error) {
defer close(errCh)
var lock countlock.Locker
if !globalQuietFlag {
// Keep progress-bar and copy routines in sync.
lock = countlock.New()
defer lock.Close()
}
go func(sourceURLs []string, targetURL string) {
for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) {
if cpURLs.Error != nil {
@@ -97,25 +107,32 @@ func doCopyCmd(sourceURLs []string, targetURL string, bar barSend) <-chan error
}
if !globalQuietFlag {
bar.Extend(cpURLs.SourceContent.Size)
lock.Up() // Let copy routine know that it is catch up.
}
}
}(sourceURLs, targetURL)
// Pool limited copy routines in parallel.
cpQueue := make(chan bool, intMax(runtime.NumCPU()-1, 1))
defer close(cpQueue)
// Wait for all copy routines to complete.
wg := new(sync.WaitGroup)
for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) {
if cpURLs.Error != nil {
ch <- cpURLs.Error
errCh <- cpURLs.Error
continue
}
cpQueue <- true
cpQueue <- true // Wait for existing pool to drain.
wg.Add(1)
go doCopyInRoutine(cpURLs, &bar, cpQueue, ch, wg)
if !globalQuietFlag {
lock.Down() // Do not jump ahead of the progress bar builder above.
}
go doCopyInRoutine(cpURLs, &bar, cpQueue, errCh, wg)
}
close(cpQueue)
wg.Wait()
}(sourceURLs, targetURL, bar, ch)
return ch
}(sourceURLs, targetURL, bar, errCh)
return errCh
}
// runCopyCmd is bound to sub-command
@@ -162,7 +179,6 @@ func runCopyCmd(ctx *cli.Context) {
}
}
}
if !globalQuietFlag {
bar.Finish()
}
+88
View File
@@ -0,0 +1,88 @@
/*
* Minio Client (C) 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 countlock is useful for synchronizing multiple go routines, when
// one of them has to take a lead. Up() method increments an internal
// counter. Down() method decrements, but blocks and waits when it
// reaches zero. One can call Down() only as many times Up() is called.
package countlock
// Locker defines a counting lock
type Locker interface {
Up() // Increment the counter.
Down() // Decrement the counter. When it reaches zero, block and wait.
Close() // Release all resources and return.
}
// New returns a newly initialized counter lock object.
func New() Locker {
upCh := make(chan bool)
downCh := make(chan bool)
closeCh := make(chan bool)
go func(upCh, downCh, closeCh chan bool) {
defer close(closeCh)
defer close(downCh)
defer close(upCh)
var level int64 // Keeps track of Up and Down count.
for {
if level == 0 {
// Up() has neven been called or may
// be Down() has been called equal number
// of times.
select {
case <-upCh:
level++
case <-closeCh: // We are done.
return
}
}
// Up is called at least once. It is safe to also listen on down.
select {
case <-upCh:
level++
case <-downCh:
level--
case <-closeCh:
return
}
}
}(upCh, downCh, closeCh)
return &lock{upCh: upCh, downCh: downCh, closeCh: closeCh}
}
type lock struct {
upCh chan bool
downCh chan bool
closeCh chan bool
}
func (l lock) Up() {
l.upCh <- true
}
func (l lock) Down() {
l.downCh <- true
}
func (l lock) Close() {
l.closeCh <- true
}
+44
View File
@@ -0,0 +1,44 @@
/*
* Quick - Quick key value store for config files and persistent state files
*
* Minio Client (C) 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 countlock
import (
"testing"
. "github.com/minio/check"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestCountLock(c *C) {
lock := New()
defer lock.Close()
lock.Up()
lock.Up()
lock.Down()
lock.Down()
lock.Up()
lock.Down()
lock.Up()
lock.Down()
}