Add retries 5 times currently supported for ./mc ls, ./mc mb

This commit is contained in:
Harshavardhana
2015-04-16 20:17:18 -07:00
parent 3bdcc1e993
commit 1468cc47ff
4 changed files with 114 additions and 5 deletions
+15 -3
View File
@@ -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())
+7 -1
View File
@@ -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()
+1 -1
View File
@@ -31,7 +31,7 @@ import (
)
const (
mcUpdateURL = "http://minio.io/updates/mc.json"
mcUpdateURL = "http://dl.minio.io/updates/mc.json"
)
type updateResults struct {
+91
View File
@@ -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
}