From 1468cc47ff44b20a5be036d631ba542ef29be168 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 16 Apr 2015 19:48:36 -0700 Subject: [PATCH] Add retries 5 times currently supported for ./mc ls, ./mc mb --- cmd-ls.go | 18 ++++++++-- cmd-mb.go | 8 ++++- cmd-update.go | 2 +- retry.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 retry.go diff --git a/cmd-ls.go b/cmd-ls.go index edc21ffe..29a04a16 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -71,7 +71,7 @@ func doListCmd(ctx *cli.Context) { log.Debug.Println(iodine.New(err, nil)) console.Fatalln("mc: Unable to parse URL") } - client, err := getNewClient(targetURLParser, globalDebugFlag) + clnt, err := getNewClient(targetURLParser, globalDebugFlag) if err != nil { log.Debug.Println(iodine.New(err, nil)) console.Fatalln("mc: Unable to initiate new client") @@ -79,14 +79,26 @@ func doListCmd(ctx *cli.Context) { // ListBuckets() will not be called for fsClient() as its not needed. if targetURLParser.bucketName == "" && targetURLParser.scheme != urlFS { - buckets, err := client.ListBuckets() + var err error + var buckets []*client.Bucket + for r := retries.init(); r.retry(); { + buckets, err = clnt.ListBuckets() + if !isValidRetry(err) { + break + } + } if err != nil { log.Debug.Println(iodine.New(err, nil)) console.Fatalln("mc: Unable to list buckets for ", targetURLParser.String()) } printBuckets(buckets) } else { - items, err = client.ListObjects(targetURLParser.bucketName, targetURLParser.objectName) + for r := retries.init(); r.retry(); { + items, err = clnt.ListObjects(targetURLParser.bucketName, targetURLParser.objectName) + if !isValidRetry(err) { + break + } + } if err != nil { log.Debug.Println(iodine.New(err, nil)) console.Fatalln("mc: Unable to list objects for ", targetURLParser.String()) diff --git a/cmd-mb.go b/cmd-mb.go index fe91f98c..9cbf0ae2 100644 --- a/cmd-mb.go +++ b/cmd-mb.go @@ -54,7 +54,13 @@ func doMakeBucketCmd(ctx *cli.Context) { log.Debug.Println(iodine.New(err, nil)) console.Fatalf("mc: Unable to create new client to: %s", targetURLParser.String()) } - err = clnt.PutBucket(targetURLParser.bucketName) + // retry - 5 times + for r := retries.init(); r.retry(); { + err = clnt.PutBucket(targetURLParser.bucketName) + if !isValidRetry(err) { + break + } + } if err != nil { log.Debug.Println(iodine.New(err, nil)) // error message returned properly by PutBucket() diff --git a/cmd-update.go b/cmd-update.go index be53c374..e6bab040 100644 --- a/cmd-update.go +++ b/cmd-update.go @@ -31,7 +31,7 @@ import ( ) const ( - mcUpdateURL = "http://minio.io/updates/mc.json" + mcUpdateURL = "http://dl.minio.io/updates/mc.json" ) type updateResults struct { diff --git a/retry.go b/retry.go new file mode 100644 index 00000000..edebc5d3 --- /dev/null +++ b/retry.go @@ -0,0 +1,91 @@ +package main + +import ( + "io" + "net" + "time" + + "github.com/minio-io/mc/pkg/console" + "github.com/minio-io/minio/pkg/iodine" +) + +// isValidRetry - check if we should retry for the given error sequence +func isValidRetry(err error) bool { + if err == nil { + return false + } + // handle io Errors separately since they are typed as *errors.String + switch iodine.ToError(err) { + case io.ErrUnexpectedEOF: + return true + } + // DNSError, Network Operation error + switch e := iodine.ToError(err).(type) { + case *net.DNSError: + return true + case *net.OpError: + switch e.Op { + case "read", "write", "dial": + return true + } + } + return false +} + +// TODO: this is global, make it configurable +var retries = waitTime{ + duration: 5 * time.Second, + delayDuration: 1 * time.Second, +} + +// waitTime represents sequence waiting for an action to complete successfully. +type waitTime struct { + duration time.Duration // total duration of retries. + delayDuration time.Duration // delay interval between each retry +} + +type retryOperation struct { + waittime waitTime + last time.Time + end time.Time + force bool + count int +} + +// instantiate new sequence of retries for the given waittime. +func (s waitTime) init() *retryOperation { + now := time.Now() + console.Error("Retrying... ") + return &retryOperation{ + waittime: s, + last: now, + end: now.Add(s.duration), + force: true, + } +} + +func (a *retryOperation) retry() bool { + now := time.Now() + sleep := a.nextSleep(now) + if !a.force && !now.Add(sleep).Before(a.end) { + console.Infoln() // print a new line + return false + } + a.force = false + if sleep > 0 && a.count > 0 { + time.Sleep(sleep) + now = time.Now() + } + a.count++ + a.last = now + console.Infof("%d ", a.count) + return true +} + +func (a *retryOperation) nextSleep(now time.Time) time.Duration { + sleep := a.waittime.delayDuration - now.Sub(a.last) + if sleep < 0 { + return 0 + } + return sleep +}