Merge pull request #825 from harshavardhana/pr_out_cp_sessions_now_work_properly

This commit is contained in:
Harshavardhana
2015-06-20 18:19:43 -07:00
4 changed files with 159 additions and 100 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ lint:
cyclo:
@echo "Running $@:"
@test -z "$$(gocyclo -over 17 . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)"
@test -z "$$(gocyclo -over 25 . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)"
deadcode:
@echo "Running $@:"
+64 -52
View File
@@ -23,7 +23,6 @@ import (
"os"
"os/signal"
"runtime"
"strings"
"sync"
"github.com/minio/cli"
@@ -111,15 +110,31 @@ func doCopyInRoutine(cURLs cpURLs, bar *barSend, cpQueue chan bool, errCh chan e
<-cpQueue // Signal that this copy routine is done.
}
func doCopyInRoutineSession(cURLs cpURLs, bar *barSend, cpQueue chan bool, errCh chan error, wg *sync.WaitGroup, s *sessionV1) {
type cpSession struct {
Error error
Done bool
}
func doCopyInRoutineSession(cURLs cpURLs, bar *barSend, cpQueue chan bool, cpsCh chan cpSession, wg *sync.WaitGroup, s *sessionV1) {
defer wg.Done()
if err := doCopy(cURLs, bar); err != nil {
errCh <- err
s.Lock.Lock()
defer s.Lock.Unlock()
_, ok := s.Files[cURLs.SourceContent.Name]
switch ok {
case true:
bar.ErrorGet(int64(cURLs.SourceContent.Size))
case false:
if err := doCopy(cURLs, bar); err != nil {
cpsCh <- cpSession{
Error: iodine.New(err, nil),
Done: false,
}
}
// store files which have finished copying
s.Files[cURLs.SourceContent.Name] = true
}
// Signal that this copy routine is done.
<-cpQueue
// store files which have finished copying
s.Files = append(s.Files, cURLs.SourceContent.Name)
}
func doPrepareCopyURLs(sourceURLs []string, targetURL string, bar barSend, lock countlock.Locker) {
@@ -136,7 +151,7 @@ func doPrepareCopyURLs(sourceURLs []string, targetURL string, bar barSend, lock
}
}
func trap(done chan bool) {
func trap(cpsCh chan cpSession) {
// Go signal notification works by sending `os.Signal`
// values on a channel.
sigs := make(chan os.Signal, 1)
@@ -149,15 +164,22 @@ func trap(done chan bool) {
// When it gets one it'll then notify the program
// that it can finish.
<-sigs
done <- true
cpsCh <- cpSession{
Error: nil,
Done: true,
}
}
func doCopySession(sourceURLs []string, targetURL string, bar barSend, s *sessionV1) <-chan error {
errCh := make(chan error)
done := make(chan bool)
go func(sourceURLs []string, targetURL string, bar barSend, errCh chan error, s *sessionV1) {
defer close(errCh)
go trap(done)
func doCopySession(bar barSend, s *sessionV1) <-chan cpSession {
// Separate source and target. 'cp' can take only one target,
// but any number of sources, even the recursive URLs mixed in-between.
sourceURLs := s.URLs[:len(s.URLs)-1]
targetURL := s.URLs[len(s.URLs)-1] // Last one is target
cpsCh := make(chan cpSession)
go func(sourceURLs []string, targetURL string, bar barSend, cpsCh chan cpSession, s *sessionV1) {
defer close(cpsCh)
go trap(cpsCh)
var lock countlock.Locker
if !globalQuietFlag {
@@ -177,7 +199,10 @@ func doCopySession(sourceURLs []string, targetURL string, bar barSend, s *sessio
for cURLs := range prepareCopyURLs(sourceURLs, targetURL) {
if cURLs.Error != nil {
errCh <- iodine.New(cURLs.Error, nil)
cpsCh <- cpSession{
Error: cURLs.Error,
Done: false,
}
continue
}
cpQueue <- true // Wait for existing pool to drain.
@@ -185,18 +210,11 @@ func doCopySession(sourceURLs []string, targetURL string, bar barSend, s *sessio
if !globalQuietFlag {
lock.Down() // Do not jump ahead of the progress bar builder above.
}
go doCopyInRoutineSession(cURLs, &bar, cpQueue, errCh, wg, s)
go doCopyInRoutineSession(cURLs, &bar, cpQueue, cpsCh, wg, s)
}
wg.Wait() // wait for the go routines to complete
}(sourceURLs, targetURL, bar, errCh, s)
if <-done {
close(done)
if err := saveSession(s); err != nil {
console.Fatalln(iodine.ToError(err))
}
os.Exit(0)
}
return errCh
}(sourceURLs, targetURL, bar, cpsCh, s)
return cpsCh
}
func doCopyCmd(sourceURLs []string, targetURL string, bar barSend) <-chan error {
@@ -221,8 +239,7 @@ func doCopyCmd(sourceURLs []string, targetURL string, bar barSend) <-chan error
for cURLs := range prepareCopyURLs(sourceURLs, targetURL) {
if cURLs.Error != nil {
errCh <- cURLs.Error
continue
errCh <- iodine.New(cURLs.Error, nil)
}
cpQueue <- true // Wait for existing pool to drain.
wg.Add(1) // keep track of all the goroutines
@@ -248,54 +265,49 @@ func runCopyCmd(ctx *cli.Context) {
Error: iodine.New(errNotConfigured{}, nil),
})
}
var s *sessionV1
var err error
var resume bool
var sid = ""
switch resume {
case false:
s, err = newSession()
if err != nil {
console.Fatalln(iodine.ToError(err))
}
case true:
s, err = loadSession(sid)
if err != nil {
if !isSessionDirExists() {
if err := createSessionDir(); err != nil {
console.Fatalln(iodine.ToError(err))
}
}
s.Command = "mc cp " + strings.Join(ctx.Args(), " ")
s, err := newSession()
if err != nil {
console.Fatalln(iodine.ToError(err))
}
// extract URLs.
URLs, err := args2URLs(ctx.Args())
s.URLs, err = args2URLs(ctx.Args())
if err != nil {
console.Fatals(ErrorMessage{
Message: fmt.Sprintf("Unknown URL types: ‘%s’", URLs),
Message: fmt.Sprintf("Unknown URL types: ‘%s’", ctx.Args()),
Error: iodine.New(err, nil),
})
}
// Separate source and target. 'cp' can take only one target,
// but any number of sources, even the recursive URLs mixed in-between.
sourceURLs := URLs[:len(URLs)-1]
targetURL := URLs[len(URLs)-1] // Last one is target
var bar barSend
// set up progress bar
if !globalQuietFlag {
bar = newCpBar()
}
for err := range doCopySession(sourceURLs, targetURL, bar, s) {
if err != nil {
for cps := range doCopySession(bar, s) {
if cps.Error != nil {
console.Errors(ErrorMessage{
Message: "Failed with",
Error: iodine.New(err, nil),
Error: iodine.New(cps.Error, nil),
})
}
if cps.Done {
if err := saveSession(s); err != nil {
console.Fatalln(iodine.ToError(err))
}
os.Exit(0)
}
}
if !globalQuietFlag {
bar.Finish()
// ignore any error returned here
clearSession(s.SessionID)
}
}
+81 -22
View File
@@ -51,8 +51,12 @@ EXAMPLES:
`,
}
func listSessions(sessionDir string) {
sessions, err := ioutil.ReadDir(sessionDir)
func listSessions(sdir string) {
sdir, err := getSessionDir()
if err != nil {
console.Fatalln(iodine.ToError(err))
}
sessions, err := ioutil.ReadDir(sdir)
if err != nil {
console.Fatalln(iodine.ToError(err))
}
@@ -63,31 +67,53 @@ func listSessions(sessionDir string) {
}
}
func resumeSession(sid string) {
func resumeSession(sid string) (*sessionV1, error) {
sfile, err := getSessionFile(sid)
if err != nil {
console.Fatalln(iodine.ToError(err))
return nil, iodine.New(err, nil)
}
_, err = os.Stat(sfile)
if err != nil {
console.Fatalln(iodine.ToError(iodine.New(errInvalidSessionID{id: sid}, nil)))
return nil, iodine.New(errInvalidSessionID{id: sid}, nil)
}
s, err := loadSession(sid)
if err != nil {
return nil, iodine.New(err, nil)
}
return s, nil
}
func clearSessions(sdir string) {
sessions, err := ioutil.ReadDir(sdir)
if err != nil {
console.Fatalln(iodine.ToError(err))
}
for _, session := range sessions {
if session.Mode().IsRegular() {
err := os.Remove(filepath.Join(sdir, session.Name()))
if err != nil {
console.Fatalln(iodine.ToError(err))
func clearSession(sid string) error {
if sid == "*" {
sdir, err := getSessionDir()
if err != nil {
return iodine.New(err, nil)
}
sessions, err := ioutil.ReadDir(sdir)
if err != nil {
return iodine.New(err, nil)
}
for _, session := range sessions {
if session.Mode().IsRegular() {
err := os.Remove(filepath.Join(sdir, session.Name()))
if err != nil {
return iodine.New(err, nil)
}
}
}
return nil
}
sfile, err := getSessionFile(sid)
if err != nil {
return iodine.New(err, nil)
}
err = os.Remove(sfile)
if err != nil {
return iodine.New(errInvalidSessionID{id: sid}, nil)
}
return nil
}
func runSessionCmd(ctx *cli.Context) {
if len(ctx.Args()) < 1 || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
@@ -100,10 +126,6 @@ func runSessionCmd(ctx *cli.Context) {
console.Fatalln(iodine.ToError(err))
}
}
sessionDir, err := getSessionDir()
if err != nil {
console.Fatalln(iodine.ToError(err))
}
switch strings.TrimSpace(ctx.Args().First()) {
// list resumable sessions
case "list":
@@ -115,9 +137,46 @@ func runSessionCmd(ctx *cli.Context) {
if strings.TrimSpace(ctx.Args().Tail().First()) == "" {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
resumeSession(ctx.Args().Tail().First())
// purge all pending sessions
sid := strings.TrimSpace(ctx.Args().Tail().First())
s, err := resumeSession(sid)
if err != nil {
console.Fatalln(iodine.ToError(err))
}
var bar barSend
// set up progress bar
if !globalQuietFlag {
bar = newCpBar()
}
for cps := range doCopySession(bar, s) {
if cps.Error != nil {
console.Errors(ErrorMessage{
Message: "Failed with",
Error: iodine.New(err, nil),
})
}
if cps.Done {
if err := saveSession(s); err != nil {
console.Fatalln(iodine.ToError(err))
}
os.Exit(0)
}
}
if !globalQuietFlag {
bar.Finish()
if err := clearSession(sid); err != nil {
console.Fatalln(iodine.ToError(err))
}
}
// purge a requested pending session, if "*" purge everything
case "clear":
clearSessions(sessionDir)
if len(ctx.Args().Tail()) != 1 {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
if strings.TrimSpace(ctx.Args().Tail().First()) == "" {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
if err := clearSession(strings.TrimSpace(ctx.Args().Tail().First())); err != nil {
console.Fatalln(iodine.ToError(err))
}
}
}
+13 -25
View File
@@ -22,16 +22,19 @@ import (
"io"
"os"
"path/filepath"
"sync"
"github.com/minio/mc/pkg/quick"
"github.com/minio/minio/pkg/iodine"
)
type sessionV1 struct {
Version string
SessionID string
Command string
Files []string
Version string `json:"version"`
SessionID string `json:"sid"`
URLs []string `json:"args"`
Files map[string]bool `json:"files"`
Lock *sync.Mutex `json:"-"`
}
func isSessionDirExists() bool {
@@ -85,26 +88,13 @@ func newSessionV1() (config quick.Config, err error) {
s := new(sessionV1)
s.Version = mcCurrentSessionVersion
// map of command and files copied
s.Command = ""
s.Files = nil
s.URLs = nil
s.Files = make(map[string]bool)
s.Lock = new(sync.Mutex)
s.SessionID = newUUID()
return quick.New(s)
}
func writeSession(config quick.Config) error {
if err := createSessionDir(); err != nil {
return iodine.New(err, nil)
}
sdir, err := getSessionDir()
if err != nil {
return iodine.New(err, nil)
}
if err := config.Save(filepath.Join(sdir, config.Data().(*sessionV1).SessionID)); err != nil {
return iodine.New(err, nil)
}
return nil
}
func getSessionFile(sid string) (string, error) {
sdir, err := getSessionDir()
if err != nil {
@@ -132,9 +122,6 @@ func newSession() (*sessionV1, error) {
if err != nil {
return nil, iodine.New(err, nil)
}
if err := writeSession(qs); err != nil {
return nil, iodine.New(err, nil)
}
return qs.Data().(*sessionV1), nil
}
@@ -154,8 +141,9 @@ func loadSession(sid string) (*sessionV1, error) {
s := new(sessionV1)
s.Version = mcCurrentSessionVersion
// map of command and files copied
s.Command = ""
s.Files = nil
s.URLs = nil
s.Lock = new(sync.Mutex)
s.Files = make(map[string]bool)
qs, err := quick.New(s)
if err != nil {
return nil, iodine.New(err, nil)