2017-10-14 12:51:00 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cenkalti/backoff"
|
2017-11-30 21:05:14 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2017-10-17 19:46:38 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-10-14 12:51:00 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RetryBackend retries operations on the backend in case of an error with a
|
|
|
|
// backoff.
|
|
|
|
type RetryBackend struct {
|
|
|
|
restic.Backend
|
|
|
|
MaxTries int
|
|
|
|
Report func(string, error, time.Duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
// statically ensure that RetryBackend implements restic.Backend.
|
|
|
|
var _ restic.Backend = &RetryBackend{}
|
|
|
|
|
|
|
|
// NewRetryBackend wraps be with a backend that retries operations after a
|
|
|
|
// backoff. report is called with a description and the error, if one occurred.
|
|
|
|
func NewRetryBackend(be restic.Backend, maxTries int, report func(string, error, time.Duration)) *RetryBackend {
|
|
|
|
return &RetryBackend{
|
|
|
|
Backend: be,
|
|
|
|
MaxTries: maxTries,
|
|
|
|
Report: report,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:46:20 +00:00
|
|
|
func (be *RetryBackend) retry(ctx context.Context, msg string, f func() error) error {
|
|
|
|
err := backoff.RetryNotify(f,
|
|
|
|
backoff.WithContext(backoff.WithMaxTries(backoff.NewExponentialBackOff(), uint64(be.MaxTries)), ctx),
|
2017-10-14 12:51:00 +00:00
|
|
|
func(err error, d time.Duration) {
|
|
|
|
if be.Report != nil {
|
|
|
|
be.Report(msg, err, d)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2017-12-01 12:46:20 +00:00
|
|
|
|
|
|
|
return err
|
2017-10-14 12:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save stores the data in the backend under the given handle.
|
|
|
|
func (be *RetryBackend) Save(ctx context.Context, h restic.Handle, rd io.Reader) error {
|
2017-10-17 19:46:38 +00:00
|
|
|
seeker, ok := rd.(io.Seeker)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("reader %T is not a seeker", rd)
|
|
|
|
}
|
|
|
|
|
|
|
|
pos, err := seeker.Seek(0, io.SeekCurrent)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Seek")
|
|
|
|
}
|
|
|
|
|
|
|
|
if pos != 0 {
|
|
|
|
return errors.Errorf("reader is not at the beginning (pos %v)", pos)
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:46:20 +00:00
|
|
|
return be.retry(ctx, fmt.Sprintf("Save(%v)", h), func() error {
|
2017-10-17 19:46:38 +00:00
|
|
|
_, err := seeker.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-30 21:05:14 +00:00
|
|
|
err = be.Backend.Save(ctx, h, rd)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("Save(%v) failed with error, removing file: %v", h, err)
|
2017-12-22 17:34:17 +00:00
|
|
|
rerr := be.Backend.Remove(ctx, h)
|
2017-11-30 21:05:14 +00:00
|
|
|
if rerr != nil {
|
|
|
|
debug.Log("Remove(%v) returned error: %v", h, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// return original error
|
|
|
|
return err
|
2017-10-14 12:51:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load returns a reader that yields the contents of the file at h at the
|
|
|
|
// given offset. If length is larger than zero, only a portion of the file
|
|
|
|
// is returned. rd must be closed after use. If an error is returned, the
|
|
|
|
// ReadCloser must be nil.
|
|
|
|
func (be *RetryBackend) Load(ctx context.Context, h restic.Handle, length int, offset int64) (rd io.ReadCloser, err error) {
|
2017-12-01 12:46:20 +00:00
|
|
|
err = be.retry(ctx, fmt.Sprintf("Load(%v, %v, %v)", h, length, offset),
|
2017-10-14 12:51:00 +00:00
|
|
|
func() error {
|
|
|
|
var innerError error
|
|
|
|
rd, innerError = be.Backend.Load(ctx, h, length, offset)
|
|
|
|
|
|
|
|
return innerError
|
|
|
|
})
|
|
|
|
return rd, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns information about the File identified by h.
|
|
|
|
func (be *RetryBackend) Stat(ctx context.Context, h restic.Handle) (fi restic.FileInfo, err error) {
|
2017-12-01 12:46:20 +00:00
|
|
|
err = be.retry(ctx, fmt.Sprintf("Stat(%v)", h),
|
2017-10-14 12:51:00 +00:00
|
|
|
func() error {
|
|
|
|
var innerError error
|
|
|
|
fi, innerError = be.Backend.Stat(ctx, h)
|
|
|
|
|
|
|
|
return innerError
|
|
|
|
})
|
|
|
|
return fi, err
|
|
|
|
}
|
2017-12-22 17:34:17 +00:00
|
|
|
|
|
|
|
// Remove removes a File with type t and name.
|
|
|
|
func (be *RetryBackend) Remove(ctx context.Context, h restic.Handle) (err error) {
|
|
|
|
return be.retry(ctx, fmt.Sprintf("Remove(%v)", h), func() error {
|
|
|
|
return be.Backend.Remove(ctx, h)
|
|
|
|
})
|
|
|
|
}
|
2018-01-07 04:13:42 +00:00
|
|
|
|
|
|
|
// Test a boolean value whether a File with the name and type exists.
|
|
|
|
func (be *RetryBackend) Test(ctx context.Context, h restic.Handle) (exists bool, err error) {
|
|
|
|
err = be.retry(ctx, fmt.Sprintf("Test(%v)", h), func() error {
|
|
|
|
var innerError error
|
|
|
|
exists, innerError = be.Backend.Test(ctx, h)
|
|
|
|
|
|
|
|
return innerError
|
|
|
|
})
|
|
|
|
return exists, err
|
|
|
|
}
|
2018-01-24 15:25:40 +00:00
|
|
|
|
|
|
|
// List runs fn for each file in the backend which has the type t.
|
|
|
|
func (be *RetryBackend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
|
|
|
listed := make(map[string]struct{})
|
|
|
|
return be.retry(ctx, fmt.Sprintf("List(%v)", t), func() error {
|
|
|
|
return be.Backend.List(ctx, t, func(fi restic.FileInfo) error {
|
|
|
|
if _, ok := listed[fi.Name]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
listed[fi.Name] = struct{}{}
|
|
|
|
return fn(fi)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|