diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index e7c8edaf..d83d1651 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -125,6 +125,10 @@ var ( Name: "monitoring-address", Usage: "if specified, a new prometheus endpoint will be created to report mirroring activity. (eg: localhost:8081)", }, + cli.BoolFlag{ + Name: "retry", + Usage: "if specified, will enable retrying on a per object basis if errors occur", + }, } ) @@ -467,12 +471,38 @@ func (mj *mirrorJob) doMirror(ctx context.Context, sURLs URLs) URLs { sURLs.MD5 = mj.opts.md5 sURLs.DisableMultipart = mj.opts.disableMultipart - now := time.Now() - ret := uploadSourceToTargetURL(ctx, sURLs, mj.status, mj.opts.encKeyDB, mj.opts.isMetadata, false) - if ret.Error == nil { - durationMs := time.Since(now).Milliseconds() - mirrorReplicationDurations.With(prometheus.Labels{"object_size": convertSizeToTag(sURLs.SourceContent.Size)}).Observe(float64(durationMs)) + var ret URLs + + if !mj.opts.isRetriable { + now := time.Now() + ret = uploadSourceToTargetURL(ctx, sURLs, mj.status, mj.opts.encKeyDB, mj.opts.isMetadata, false) + if ret.Error == nil { + durationMs := time.Since(now).Milliseconds() + mirrorReplicationDurations.With(prometheus.Labels{"object_size": convertSizeToTag(sURLs.SourceContent.Size)}).Observe(float64(durationMs)) + } + + return ret } + + newRetryManager(ctx, time.Second, 3).retry(func(rm *retryManager) *probe.Error { + if rm.retries > 0 { + printMsg(retryMessage{ + SourceURL: sURLs.SourceContent.URL.String(), + TargetURL: sURLs.TargetContent.URL.String(), + Retries: rm.retries, + }) + } + + now := time.Now() + ret = uploadSourceToTargetURL(ctx, sURLs, mj.status, mj.opts.encKeyDB, mj.opts.isMetadata, false) + if ret.Error == nil { + durationMs := time.Since(now).Milliseconds() + mirrorReplicationDurations.With(prometheus.Labels{"object_size": convertSizeToTag(sURLs.SourceContent.Size)}).Observe(float64(durationMs)) + } + + return ret.Error + }) + return ret } @@ -909,6 +939,7 @@ func runMirror(ctx context.Context, srcURL, dstURL string, cli *cli.Context, enc isOverwrite: isOverwrite, isWatch: isWatch, isMetadata: isMetadata, + isRetriable: cli.Bool("retry"), md5: cli.Bool("md5"), disableMultipart: cli.Bool("disable-multipart"), excludeOptions: cli.StringSlice("exclude"), diff --git a/cmd/mirror-url.go b/cmd/mirror-url.go index e35a2ed6..db4348fe 100644 --- a/cmd/mirror-url.go +++ b/cmd/mirror-url.go @@ -218,6 +218,7 @@ func deltaSourceTarget(ctx context.Context, sourceURL, targetURL string, opts mi type mirrorOptions struct { isFake, isOverwrite, activeActive bool isWatch, isRemove, isMetadata bool + isRetriable bool excludeOptions, excludeStorageClasses []string encKeyDB map[string][]prefixSSEPair md5, disableMultipart bool diff --git a/cmd/retry.go b/cmd/retry.go new file mode 100644 index 00000000..17ed734c --- /dev/null +++ b/cmd/retry.go @@ -0,0 +1,68 @@ +package cmd + +import ( + "context" + "fmt" + "math/rand" + "time" + + json "github.com/minio/colorjson" + "github.com/minio/mc/pkg/probe" +) + +type retryManager struct { + retries int + maxRetries int + retryInterval time.Duration + commandCtx context.Context + retryCtx context.Context + cancelRetry context.CancelFunc +} + +func newRetryManager(ctx context.Context, retryInterval time.Duration, maxRetries int) *retryManager { + retryCtx, cancelFunc := context.WithCancel(context.Background()) + return &retryManager{ + retryInterval: retryInterval, + maxRetries: maxRetries, + commandCtx: ctx, + retryCtx: retryCtx, + cancelRetry: cancelFunc, + } +} + +type retryMessage struct { + SourceURL string `json:"sourceURL"` + TargetURL string `json:"targetURL"` + Retries int `json:"retries"` +} + +func (r retryMessage) String() string { + return fmt.Sprintf(" Retries %d: source `%s` >> target `%s`", r.Retries, r.SourceURL, r.TargetURL) +} + +func (r retryMessage) JSON() string { + jsonMessageBytes, e := json.MarshalIndent(r, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func (r *retryManager) retry(action func(rm *retryManager) *probe.Error) { + defer r.cancelRetry() + for r.retries <= r.maxRetries { + + err := action(r) + if err == nil { + return + } + + select { + case <-r.retryCtx.Done(): + return + case <-r.commandCtx.Done(): + return + case <-time.After(r.retryInterval/2 + time.Duration(rand.Int63n(int64(r.retryInterval)))): + r.retries++ + } + + } +}