Merge pull request #827 from harshavardhana/pr_out_add_sync_session_deprecate_uuid_usage_move_to_a_fixed_length_random_english_characters

This commit is contained in:
Harshavardhana
2015-06-20 20:40:43 -07:00
5 changed files with 177 additions and 60 deletions
+4 -2
View File
@@ -146,7 +146,7 @@ func doPrepareCopyURLs(sourceURLs []string, targetURL string, bar barSend, lock
}
}
func trap(cpsCh chan cpSession) {
func trapCp(cpsCh chan cpSession) {
// Go signal notification works by sending `os.Signal`
// values on a channel.
sigs := make(chan os.Signal, 1)
@@ -174,7 +174,7 @@ func doCopyCmdSession(bar barSend, s *sessionV1) <-chan cpSession {
cpsCh := make(chan cpSession)
go func(sourceURLs []string, targetURL string, bar barSend, cpsCh chan cpSession, s *sessionV1) {
defer close(cpsCh)
go trap(cpsCh)
go trapCp(cpsCh)
var lock countlock.Locker
if !globalQuietFlag {
@@ -234,6 +234,7 @@ func runCopyCmd(ctx *cli.Context) {
if err != nil {
console.Fatalln(iodine.ToError(err))
}
s.CommandType = "cp"
// extract URLs.
s.URLs, err = args2URLs(ctx.Args())
@@ -261,6 +262,7 @@ func runCopyCmd(ctx *cli.Context) {
if err := saveSession(s); err != nil {
console.Fatalln(iodine.ToError(err))
}
// this os.Exit is needed really to exit in-case of "os.Interrupt"
os.Exit(0)
}
}
+29 -10
View File
@@ -147,18 +147,37 @@ func runSessionCmd(ctx *cli.Context) {
if !globalQuietFlag {
bar = newCpBar()
}
for cps := range doCopyCmdSession(bar, s) {
if cps.Error != nil {
console.Errors(ErrorMessage{
Message: "Failed with",
Error: iodine.New(cps.Error, nil),
})
switch s.CommandType {
case "cp":
for cps := range doCopyCmdSession(bar, s) {
if cps.Error != nil {
console.Errors(ErrorMessage{
Message: "Failed with",
Error: iodine.New(cps.Error, nil),
})
}
if cps.Done {
if err := saveSession(s); err != nil {
console.Fatalln(iodine.ToError(err))
}
os.Exit(0)
}
}
if cps.Done {
if err := saveSession(s); err != nil {
console.Fatalln(iodine.ToError(err))
case "sync":
for ss := range doSyncCmdSession(bar, s) {
if ss.Error != nil {
console.Errors(ErrorMessage{
Message: "Failed with",
Error: iodine.New(err, nil),
})
}
if ss.Done {
if err := saveSession(s); err != nil {
console.Fatalln(iodine.ToError(err))
}
// this os.Exit is needed really to exit in-case of "os.Interrupt"
os.Exit(0)
}
os.Exit(0)
}
}
if !globalQuietFlag {
+92 -22
View File
@@ -20,6 +20,8 @@ import (
"fmt"
"io"
"math"
"os"
"os/signal"
"runtime"
"sync"
@@ -67,18 +69,31 @@ EXAMPLES:
`,
}
// doSync - Sync an object to multiple destination
func doSync(sURLs syncURLs, bar *barSend, syncQueue chan bool, errCh chan error, wg *sync.WaitGroup) {
// doSyncSession - Sync an object to multiple destination
func doSyncSession(sURLs syncURLs, bar *barSend, syncQueue chan bool, ssCh chan syncSession, wg *sync.WaitGroup, s *sessionV1) {
defer wg.Done()
s.Lock.Lock()
defer s.Lock.Unlock()
if !globalQuietFlag {
bar.SetCaption(sURLs.SourceContent.Name + ": ")
}
_, ok := s.Files[sURLs.SourceContent.Name]
if ok {
if !globalQuietFlag {
bar.ErrorGet(int64(sURLs.SourceContent.Size))
}
<-syncQueue // Signal that this copy routine is done.
return
}
reader, length, err := getSource(sURLs.SourceContent.Name)
if err != nil {
if !globalQuietFlag {
bar.ErrorGet(int64(length))
}
errCh <- iodine.New(err, map[string]string{"URL": sURLs.SourceContent.Name})
ssCh <- syncSession{
Error: iodine.New(err, map[string]string{"URL": sURLs.SourceContent.Name}),
Done: false,
}
}
defer reader.Close()
@@ -100,10 +115,15 @@ func doSync(sURLs syncURLs, bar *barSend, syncQueue chan bool, errCh chan error,
if !globalQuietFlag {
bar.ErrorPut(int64(length))
}
errCh <- iodine.New(err, nil)
ssCh <- syncSession{
Error: iodine.New(err, nil),
Done: false,
}
}
}
<-syncQueue // Signal that this copy routine is done.
// store files which have finished copying
s.Files[sURLs.SourceContent.Name] = true
}
func doPrepareSyncURLs(sourceURL string, targetURLs []string, bar barSend, lock countlock.Locker) {
@@ -120,10 +140,41 @@ func doPrepareSyncURLs(sourceURL string, targetURLs []string, bar barSend, lock
}
}
func doSyncCmd(sourceURL string, targetURLs []string, bar barSend) <-chan error {
errCh := make(chan error)
go func(sourceURL string, targetURLs []string, bar barSend, errCh chan error) {
defer close(errCh)
type syncSession struct {
Error error
Done bool
}
func trapSync(ssCh chan syncSession) {
// Go signal notification works by sending `os.Signal`
// values on a channel.
sigs := make(chan os.Signal, 1)
// `signal.Notify` registers the given channel to
// receive notifications of the specified signals.
signal.Notify(sigs, os.Kill, os.Interrupt)
// This executes a blocking receive for signals.
// When it gets one it'll then notify the program
// that it can finish.
<-sigs
ssCh <- syncSession{
Error: nil,
Done: true,
}
}
func doSyncCmdSession(bar barSend, s *sessionV1) <-chan syncSession {
ssCh := make(chan syncSession)
// Separate source and target. 'sync' can take only one source.
// but any number of targets, even the recursive URLs mixed in-between.
sourceURL := s.URLs[0] // first one is source
targetURLs := s.URLs[1:]
go func(sourceURL string, targetURLs []string, bar barSend, ssCh chan syncSession, s *sessionV1) {
defer close(ssCh)
go trapSync(ssCh)
var lock countlock.Locker
if !globalQuietFlag {
// Keep progress-bar and copy routines in sync.
@@ -139,7 +190,10 @@ func doSyncCmd(sourceURL string, targetURLs []string, bar barSend) <-chan error
for sURLs := range prepareSyncURLs(sourceURL, targetURLs) {
if sURLs.Error != nil {
errCh <- iodine.New(sURLs.Error, nil)
ssCh <- syncSession{
Error: iodine.New(sURLs.Error, nil),
Done: false,
}
continue
}
syncQueue <- true
@@ -147,11 +201,11 @@ func doSyncCmd(sourceURL string, targetURLs []string, bar barSend) <-chan error
if !globalQuietFlag {
lock.Down() // Do not jump ahead of the progress bar builder above.
}
go doSync(sURLs, &bar, syncQueue, errCh, wg)
go doSyncSession(sURLs, &bar, syncQueue, ssCh, wg, s)
}
wg.Wait()
}(sourceURL, targetURLs, bar, errCh)
return errCh
}(sourceURL, targetURLs, bar, ssCh, s)
return ssCh
}
func runSyncCmd(ctx *cli.Context) {
@@ -166,36 +220,52 @@ func runSyncCmd(ctx *cli.Context) {
})
}
if !isSessionDirExists() {
if err := createSessionDir(); err != nil {
console.Fatalln(iodine.ToError(err))
}
}
s, err := newSession()
if err != nil {
console.Fatalln(iodine.ToError(err))
}
s.CommandType = "sync"
// 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 found: ‘%s’", URLs),
Message: fmt.Sprintf("Unknown URL types found: ‘%s’", ctx.Args()),
Error: iodine.New(err, nil),
})
}
// Separate source and target. 'sync' can take only one source.
// but any number of targets, even the recursive URLs mixed in-between.
sourceURL := URLs[0] // first one is source
targetURLs := URLs[1:]
var bar barSend
// set up progress bar
if !globalQuietFlag {
bar = newCpBar()
}
for err := range doSyncCmd(sourceURL, targetURLs, bar) {
if err != nil {
for ss := range doSyncCmdSession(bar, s) {
if ss.Error != nil {
console.Errors(ErrorMessage{
Message: "Failed with",
Error: iodine.New(err, nil),
Error: iodine.New(ss.Error, nil),
})
}
if ss.Done {
if err := saveSession(s); err != nil {
console.Fatalln(iodine.ToError(err))
}
// this os.Exit is needed really to exit in-case of "os.Interrupt"
os.Exit(0)
}
}
if !globalQuietFlag {
bar.Finish()
// ignore any error returned here
clearSession(s.SessionID)
}
}
+16 -19
View File
@@ -17,22 +17,22 @@
package main
import (
"crypto/rand"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"sync"
"time"
"github.com/minio/mc/pkg/quick"
"github.com/minio/minio/pkg/iodine"
)
type sessionV1 struct {
Version string `json:"version"`
SessionID string `json:"sid"`
URLs []string `json:"args"`
Files map[string]bool `json:"files"`
Version string `json:"version"`
CommandType string `json:"command-type"`
SessionID string `json:"sid"`
URLs []string `json:"args"`
Files map[string]bool `json:"files"`
Lock *sync.Mutex `json:"-"`
}
@@ -69,19 +69,16 @@ func getSessionDir() (string, error) {
}
var mcCurrentSessionVersion = mcCurrentConfigVersion
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// newUUID generates a random UUID according to RFC 4122
func newUUID() string {
uuid := make([]byte, 16)
_, err := io.ReadFull(rand.Reader, uuid)
if err != nil {
panic(err)
// newSID generates a random session id of regular lower case and uppercase english characters
func newSID(n int) string {
rand.Seed(time.Now().UTC().UnixNano())
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
// variant bits; see section 4.1.1
uuid[8] = uuid[8]&^0xc0 | 0x80
// version 4 (pseudo-random); see section 4.1.3
uuid[6] = uuid[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
return string(b)
}
func newSessionV1() (config quick.Config, err error) {
@@ -91,7 +88,7 @@ func newSessionV1() (config quick.Config, err error) {
s.URLs = nil
s.Files = make(map[string]bool)
s.Lock = new(sync.Mutex)
s.SessionID = newUUID()
s.SessionID = newSID(8)
return quick.New(s)
}
+36 -7
View File
@@ -45,12 +45,20 @@ func (s *CmdTestSuite) TestSyncTypeA(c *C) {
defer os.RemoveAll(target)
targetPath := filepath.Join(target, "newObject1")
for err := range doSyncCmd(sourcePath, []string{targetPath}, barSync) {
ss, err := newSession()
c.Assert(err, IsNil)
ss.URLs = append(ss.URLs, sourcePath)
ss.URLs = append(ss.URLs, targetPath)
for err := range doSyncCmdSession(barSync, ss) {
c.Assert(err, IsNil)
}
ss, err = newSession()
c.Assert(err, IsNil)
targetURL := server.URL + "/bucket/newObject"
for err := range doSyncCmd(sourcePath, []string{targetURL}, barSync) {
ss.URLs = append(ss.URLs, sourcePath)
ss.URLs = append(ss.URLs, targetURL)
for err := range doSyncCmdSession(barSync, ss) {
c.Assert(err, IsNil)
}
}
@@ -71,12 +79,20 @@ func (s *CmdTestSuite) TestSyncTypeB(c *C) {
c.Assert(err, IsNil)
defer os.RemoveAll(target)
for err := range doSyncCmd(sourcePath, []string{target}, barSync) {
ss, err := newSession()
c.Assert(err, IsNil)
ss.URLs = append(ss.URLs, sourcePath)
ss.URLs = append(ss.URLs, target)
for err := range doSyncCmdSession(barSync, ss) {
c.Assert(err, IsNil)
}
ss, err = newSession()
c.Assert(err, IsNil)
targetURL := server.URL + "/bucket"
for err := range doSyncCmd(sourcePath, []string{targetURL}, barSync) {
ss.URLs = append(ss.URLs, sourcePath)
ss.URLs = append(ss.URLs, targetURL)
for err := range doSyncCmdSession(barSync, ss) {
c.Assert(err, IsNil)
}
}
@@ -99,12 +115,20 @@ func (s *CmdTestSuite) TestSyncTypeC(c *C) {
c.Assert(err, IsNil)
defer os.RemoveAll(target)
for err := range doSyncCmd(source+"...", []string{target}, barSync) {
ss, err := newSession()
c.Assert(err, IsNil)
ss.URLs = append(ss.URLs, source+"...")
ss.URLs = append(ss.URLs, target)
for err := range doSyncCmdSession(barSync, ss) {
c.Assert(err, IsNil)
}
targetURL := server.URL + "/bucket"
for err := range doSyncCmd(source+"...", []string{targetURL}, barSync) {
ss, err = newSession()
c.Assert(err, IsNil)
ss.URLs = append(ss.URLs, source+"...")
ss.URLs = append(ss.URLs, targetURL)
for err := range doSyncCmdSession(barSync, ss) {
c.Assert(err, IsNil)
}
@@ -116,7 +140,12 @@ func (s *CmdTestSuite) TestSyncTypeC(c *C) {
c.Assert(err, IsNil)
defer os.RemoveAll(target2)
for err := range doSyncCmd(source+"...", []string{target1, target2}, barSync) {
ss, err = newSession()
c.Assert(err, IsNil)
ss.URLs = append(ss.URLs, source+"...")
ss.URLs = append(ss.URLs, target1)
ss.URLs = append(ss.URLs, target2)
for err := range doSyncCmdSession(barSync, ss) {
c.Assert(err, IsNil)
}
}