mirror of
https://github.com/octoleo/restic.git
synced 2024-11-22 21:05:10 +00:00
backend: Split RetryBackend into own package
The RetryBackend tests depend on the mock backend. When the Backend interface is eventually split from the restic package, this will lead to a dependency cycle between backend and backend/mock. Thus split the RetryBackend into a separate package to avoid this problem.
This commit is contained in:
parent
32603d49c4
commit
5c7a9a739a
@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/restic/restic/internal/backend/location"
|
"github.com/restic/restic/internal/backend/location"
|
||||||
"github.com/restic/restic/internal/backend/rclone"
|
"github.com/restic/restic/internal/backend/rclone"
|
||||||
"github.com/restic/restic/internal/backend/rest"
|
"github.com/restic/restic/internal/backend/rest"
|
||||||
|
"github.com/restic/restic/internal/backend/retry"
|
||||||
"github.com/restic/restic/internal/backend/s3"
|
"github.com/restic/restic/internal/backend/s3"
|
||||||
"github.com/restic/restic/internal/backend/sftp"
|
"github.com/restic/restic/internal/backend/sftp"
|
||||||
"github.com/restic/restic/internal/backend/swift"
|
"github.com/restic/restic/internal/backend/swift"
|
||||||
@ -445,7 +446,7 @@ func OpenRepository(ctx context.Context, opts GlobalOptions) (*repository.Reposi
|
|||||||
success := func(msg string, retries int) {
|
success := func(msg string, retries int) {
|
||||||
Warnf("%v operation successful after %d retries\n", msg, retries)
|
Warnf("%v operation successful after %d retries\n", msg, retries)
|
||||||
}
|
}
|
||||||
be = backend.NewRetryBackend(be, 10, report, success)
|
be = retry.New(be, 10, report, success)
|
||||||
|
|
||||||
// wrap backend if a test specified a hook
|
// wrap backend if a test specified a hook
|
||||||
if opts.backendTestHook != nil {
|
if opts.backendTestHook != nil {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/backend"
|
"github.com/restic/restic/internal/backend/retry"
|
||||||
"github.com/restic/restic/internal/options"
|
"github.com/restic/restic/internal/options"
|
||||||
"github.com/restic/restic/internal/repository"
|
"github.com/restic/restic/internal/repository"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
@ -172,7 +172,7 @@ func withTestEnvironment(t testing.TB) (env *testEnvironment, cleanup func()) {
|
|||||||
|
|
||||||
repository.TestUseLowSecurityKDFParameters(t)
|
repository.TestUseLowSecurityKDFParameters(t)
|
||||||
restic.TestDisableCheckPolynomial(t)
|
restic.TestDisableCheckPolynomial(t)
|
||||||
backend.TestFastRetries(t)
|
retry.TestFastRetries(t)
|
||||||
|
|
||||||
tempdir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-")
|
tempdir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-")
|
||||||
rtest.OK(t, err)
|
rtest.OK(t, err)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package backend
|
package retry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -11,9 +11,9 @@ import (
|
|||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RetryBackend retries operations on the backend in case of an error with a
|
// Backend retries operations on the backend in case of an error with a
|
||||||
// backoff.
|
// backoff.
|
||||||
type RetryBackend struct {
|
type Backend struct {
|
||||||
restic.Backend
|
restic.Backend
|
||||||
MaxTries int
|
MaxTries int
|
||||||
Report func(string, error, time.Duration)
|
Report func(string, error, time.Duration)
|
||||||
@ -21,14 +21,14 @@ type RetryBackend struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// statically ensure that RetryBackend implements restic.Backend.
|
// statically ensure that RetryBackend implements restic.Backend.
|
||||||
var _ restic.Backend = &RetryBackend{}
|
var _ restic.Backend = &Backend{}
|
||||||
|
|
||||||
// NewRetryBackend wraps be with a backend that retries operations after a
|
// New wraps be with a backend that retries operations after a
|
||||||
// backoff. report is called with a description and the error, if one occurred.
|
// backoff. report is called with a description and the error, if one occurred.
|
||||||
// success is called with the number of retries before a successful operation
|
// success is called with the number of retries before a successful operation
|
||||||
// (it is not called if it succeeded on the first try)
|
// (it is not called if it succeeded on the first try)
|
||||||
func NewRetryBackend(be restic.Backend, maxTries int, report func(string, error, time.Duration), success func(string, int)) *RetryBackend {
|
func New(be restic.Backend, maxTries int, report func(string, error, time.Duration), success func(string, int)) *Backend {
|
||||||
return &RetryBackend{
|
return &Backend{
|
||||||
Backend: be,
|
Backend: be,
|
||||||
MaxTries: maxTries,
|
MaxTries: maxTries,
|
||||||
Report: report,
|
Report: report,
|
||||||
@ -57,7 +57,7 @@ func retryNotifyErrorWithSuccess(operation backoff.Operation, b backoff.BackOff,
|
|||||||
|
|
||||||
var fastRetries = false
|
var fastRetries = false
|
||||||
|
|
||||||
func (be *RetryBackend) retry(ctx context.Context, msg string, f func() error) error {
|
func (be *Backend) retry(ctx context.Context, msg string, f func() error) error {
|
||||||
// Don't do anything when called with an already cancelled context. There would be
|
// Don't do anything when called with an already cancelled context. There would be
|
||||||
// no retries in that case either, so be consistent and abort always.
|
// no retries in that case either, so be consistent and abort always.
|
||||||
// This enforces a strict contract for backend methods: Using a cancelled context
|
// This enforces a strict contract for backend methods: Using a cancelled context
|
||||||
@ -92,7 +92,7 @@ func (be *RetryBackend) retry(ctx context.Context, msg string, f func() error) e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Save stores the data in the backend under the given handle.
|
// Save stores the data in the backend under the given handle.
|
||||||
func (be *RetryBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
|
func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
|
||||||
return be.retry(ctx, fmt.Sprintf("Save(%v)", h), func() error {
|
return be.retry(ctx, fmt.Sprintf("Save(%v)", h), func() error {
|
||||||
err := rd.Rewind()
|
err := rd.Rewind()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -125,7 +125,7 @@ func (be *RetryBackend) Save(ctx context.Context, h restic.Handle, rd restic.Rew
|
|||||||
// given offset. If length is larger than zero, only a portion of the file
|
// 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
|
// is returned. rd must be closed after use. If an error is returned, the
|
||||||
// ReadCloser must be nil.
|
// ReadCloser must be nil.
|
||||||
func (be *RetryBackend) Load(ctx context.Context, h restic.Handle, length int, offset int64, consumer func(rd io.Reader) error) (err error) {
|
func (be *Backend) Load(ctx context.Context, h restic.Handle, length int, offset int64, consumer func(rd io.Reader) error) (err error) {
|
||||||
return be.retry(ctx, fmt.Sprintf("Load(%v, %v, %v)", h, length, offset),
|
return be.retry(ctx, fmt.Sprintf("Load(%v, %v, %v)", h, length, offset),
|
||||||
func() error {
|
func() error {
|
||||||
return be.Backend.Load(ctx, h, length, offset, consumer)
|
return be.Backend.Load(ctx, h, length, offset, consumer)
|
||||||
@ -133,7 +133,7 @@ func (be *RetryBackend) Load(ctx context.Context, h restic.Handle, length int, o
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Stat returns information about the File identified by h.
|
// Stat returns information about the File identified by h.
|
||||||
func (be *RetryBackend) Stat(ctx context.Context, h restic.Handle) (fi restic.FileInfo, err error) {
|
func (be *Backend) Stat(ctx context.Context, h restic.Handle) (fi restic.FileInfo, err error) {
|
||||||
err = be.retry(ctx, fmt.Sprintf("Stat(%v)", h),
|
err = be.retry(ctx, fmt.Sprintf("Stat(%v)", h),
|
||||||
func() error {
|
func() error {
|
||||||
var innerError error
|
var innerError error
|
||||||
@ -145,14 +145,14 @@ func (be *RetryBackend) Stat(ctx context.Context, h restic.Handle) (fi restic.Fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove removes a File with type t and name.
|
// Remove removes a File with type t and name.
|
||||||
func (be *RetryBackend) Remove(ctx context.Context, h restic.Handle) (err error) {
|
func (be *Backend) Remove(ctx context.Context, h restic.Handle) (err error) {
|
||||||
return be.retry(ctx, fmt.Sprintf("Remove(%v)", h), func() error {
|
return be.retry(ctx, fmt.Sprintf("Remove(%v)", h), func() error {
|
||||||
return be.Backend.Remove(ctx, h)
|
return be.Backend.Remove(ctx, h)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test a boolean value whether a File with the name and type exists.
|
// 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) {
|
func (be *Backend) Test(ctx context.Context, h restic.Handle) (exists bool, err error) {
|
||||||
err = be.retry(ctx, fmt.Sprintf("Test(%v)", h), func() error {
|
err = be.retry(ctx, fmt.Sprintf("Test(%v)", h), func() error {
|
||||||
var innerError error
|
var innerError error
|
||||||
exists, innerError = be.Backend.Test(ctx, h)
|
exists, innerError = be.Backend.Test(ctx, h)
|
||||||
@ -166,7 +166,7 @@ func (be *RetryBackend) Test(ctx context.Context, h restic.Handle) (exists bool,
|
|||||||
// error is returned by the underlying backend, the request is retried. When fn
|
// error is returned by the underlying backend, the request is retried. When fn
|
||||||
// returns an error, the operation is aborted and the error is returned to the
|
// returns an error, the operation is aborted and the error is returned to the
|
||||||
// caller.
|
// caller.
|
||||||
func (be *RetryBackend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
func (be *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
||||||
// create a new context that we can cancel when fn returns an error, so
|
// create a new context that we can cancel when fn returns an error, so
|
||||||
// that listing is aborted
|
// that listing is aborted
|
||||||
listCtx, cancel := context.WithCancel(ctx)
|
listCtx, cancel := context.WithCancel(ctx)
|
@ -1,4 +1,4 @@
|
|||||||
package backend
|
package retry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -36,7 +36,7 @@ func TestBackendSaveRetry(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TestFastRetries(t)
|
TestFastRetries(t)
|
||||||
retryBackend := NewRetryBackend(be, 10, nil, nil)
|
retryBackend := New(be, 10, nil, nil)
|
||||||
|
|
||||||
data := test.Random(23, 5*1024*1024+11241)
|
data := test.Random(23, 5*1024*1024+11241)
|
||||||
err := retryBackend.Save(context.TODO(), restic.Handle{}, restic.NewByteReader(data, be.Hasher()))
|
err := retryBackend.Save(context.TODO(), restic.Handle{}, restic.NewByteReader(data, be.Hasher()))
|
||||||
@ -72,7 +72,7 @@ func TestBackendSaveRetryAtomic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TestFastRetries(t)
|
TestFastRetries(t)
|
||||||
retryBackend := NewRetryBackend(be, 10, nil, nil)
|
retryBackend := New(be, 10, nil, nil)
|
||||||
|
|
||||||
data := test.Random(23, 5*1024*1024+11241)
|
data := test.Random(23, 5*1024*1024+11241)
|
||||||
err := retryBackend.Save(context.TODO(), restic.Handle{}, restic.NewByteReader(data, be.Hasher()))
|
err := retryBackend.Save(context.TODO(), restic.Handle{}, restic.NewByteReader(data, be.Hasher()))
|
||||||
@ -106,7 +106,7 @@ func TestBackendListRetry(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TestFastRetries(t)
|
TestFastRetries(t)
|
||||||
retryBackend := NewRetryBackend(be, 10, nil, nil)
|
retryBackend := New(be, 10, nil, nil)
|
||||||
|
|
||||||
var listed []string
|
var listed []string
|
||||||
err := retryBackend.List(context.TODO(), restic.PackFile, func(fi restic.FileInfo) error {
|
err := retryBackend.List(context.TODO(), restic.PackFile, func(fi restic.FileInfo) error {
|
||||||
@ -136,7 +136,7 @@ func TestBackendListRetryErrorFn(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TestFastRetries(t)
|
TestFastRetries(t)
|
||||||
retryBackend := NewRetryBackend(be, 10, nil, nil)
|
retryBackend := New(be, 10, nil, nil)
|
||||||
|
|
||||||
var ErrTest = errors.New("test error")
|
var ErrTest = errors.New("test error")
|
||||||
|
|
||||||
@ -193,7 +193,7 @@ func TestBackendListRetryErrorBackend(t *testing.T) {
|
|||||||
|
|
||||||
TestFastRetries(t)
|
TestFastRetries(t)
|
||||||
const maxRetries = 2
|
const maxRetries = 2
|
||||||
retryBackend := NewRetryBackend(be, maxRetries, nil, nil)
|
retryBackend := New(be, maxRetries, nil, nil)
|
||||||
|
|
||||||
var listed []string
|
var listed []string
|
||||||
err := retryBackend.List(context.TODO(), restic.PackFile, func(fi restic.FileInfo) error {
|
err := retryBackend.List(context.TODO(), restic.PackFile, func(fi restic.FileInfo) error {
|
||||||
@ -263,7 +263,7 @@ func TestBackendLoadRetry(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TestFastRetries(t)
|
TestFastRetries(t)
|
||||||
retryBackend := NewRetryBackend(be, 10, nil, nil)
|
retryBackend := New(be, 10, nil, nil)
|
||||||
|
|
||||||
var buf []byte
|
var buf []byte
|
||||||
err := retryBackend.Load(context.TODO(), restic.Handle{}, 0, 0, func(rd io.Reader) (err error) {
|
err := retryBackend.Load(context.TODO(), restic.Handle{}, 0, 0, func(rd io.Reader) (err error) {
|
||||||
@ -283,7 +283,7 @@ func TestBackendCanceledContext(t *testing.T) {
|
|||||||
// unimplemented mock backend functions return an error by default
|
// unimplemented mock backend functions return an error by default
|
||||||
// check that we received the expected context canceled error instead
|
// check that we received the expected context canceled error instead
|
||||||
TestFastRetries(t)
|
TestFastRetries(t)
|
||||||
retryBackend := NewRetryBackend(mock.NewBackend(), 2, nil, nil)
|
retryBackend := New(mock.NewBackend(), 2, nil, nil)
|
||||||
h := restic.Handle{Type: restic.PackFile, Name: restic.NewRandomID().String()}
|
h := restic.Handle{Type: restic.PackFile, Name: restic.NewRandomID().String()}
|
||||||
|
|
||||||
// create an already canceled context
|
// create an already canceled context
|
@ -1,4 +1,4 @@
|
|||||||
package backend
|
package retry
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user