2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 03:18:55 +00:00

Don't retry when "no space left on device" in local backend

Also adds relevant documentation to the restic.Backend interface.
This commit is contained in:
greatroar 2020-12-16 13:58:02 +01:00
parent e96677cafb
commit f7784bddb3
4 changed files with 77 additions and 17 deletions

View File

@ -13,6 +13,8 @@ import (
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/fs"
"github.com/cenkalti/backoff/v4"
) )
// Local is a backend in a local directory. // Local is a backend in a local directory.
@ -80,7 +82,7 @@ func (b *Local) IsNotExist(err error) bool {
} }
// Save stores data in the backend at the handle. // Save stores data in the backend at the handle.
func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error { func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) (err error) {
debug.Log("Save %v", h) debug.Log("Save %v", h)
if err := h.Valid(); err != nil { if err := h.Valid(); err != nil {
return err return err
@ -88,8 +90,16 @@ func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReade
filename := b.Filename(h) filename := b.Filename(h)
defer func() {
// Mark non-retriable errors as such (currently only
// "no space left on device").
if errors.Is(err, syscall.ENOSPC) {
err = backoff.Permanent(err)
}
}()
// create new file // create new file
f, err := fs.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, backend.Modes.File) f, err := openFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, backend.Modes.File)
if b.IsNotExist(err) { if b.IsNotExist(err) {
debug.Log("error %v: creating dir", err) debug.Log("error %v: creating dir", err)
@ -100,7 +110,7 @@ func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReade
debug.Log("error creating dir %v: %v", filepath.Dir(filename), mkdirErr) debug.Log("error creating dir %v: %v", filepath.Dir(filename), mkdirErr)
} else { } else {
// try again // try again
f, err = fs.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, backend.Modes.File) f, err = openFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, backend.Modes.File)
} }
} }
@ -141,6 +151,8 @@ func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReade
return nil return nil
} }
var openFile = fs.OpenFile // Overridden by test.
// Load runs fn with a reader that yields the contents of the file at h at the // Load runs fn with a reader that yields the contents of the file at h at the
// given offset. // given offset.
func (b *Local) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error { func (b *Local) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error {

View File

@ -0,0 +1,42 @@
package local
import (
"context"
"errors"
"os"
"syscall"
"testing"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
"github.com/cenkalti/backoff/v4"
)
func TestNoSpacePermanent(t *testing.T) {
oldOpenFile := openFile
defer func() {
openFile = oldOpenFile
}()
openFile = func(name string, flags int, mode os.FileMode) (*os.File, error) {
// The actual error from os.OpenFile is *os.PathError.
// Other functions called inside Save may return *os.SyscallError.
return nil, os.NewSyscallError("open", syscall.ENOSPC)
}
dir, cleanup := rtest.TempDir(t)
defer cleanup()
be, err := Open(context.Background(), Config{Path: dir})
rtest.OK(t, err)
defer be.Close()
h := restic.Handle{Type: restic.ConfigFile}
err = be.Save(context.Background(), h, nil)
_, ok := err.(*backoff.PermanentError)
rtest.Assert(t, ok,
"error type should be backoff.PermanentError, got %T", err)
rtest.Assert(t, errors.Is(err, syscall.ENOSPC),
"could not recover original ENOSPC error")
}

View File

@ -3,6 +3,7 @@ package errors
import ( import (
"net/url" "net/url"
"github.com/cenkalti/backoff/v4"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -34,20 +35,19 @@ func Cause(err error) error {
} }
for { for {
// unwrap *url.Error switch e := err.(type) {
if urlErr, ok := err.(*url.Error); ok { case Causer: // github.com/pkg/errors
err = urlErr.Err err = e.Cause()
continue case *backoff.PermanentError:
} err = e.Err
case *url.Error:
// if err is a Causer, return the cause for this error. err = e.Err
if c, ok := err.(Causer); ok { default:
err = c.Cause()
continue
}
break
}
return err return err
}
}
} }
// Go 1.13-style error handling.
func Is(x, y error) bool { return errors.Is(x, y) }

View File

@ -6,6 +6,12 @@ import (
) )
// Backend is used to store and access data. // Backend is used to store and access data.
//
// Backend operations that return an error will be retried when a Backend is
// wrapped in a RetryBackend. To prevent that from happening, the operations
// should return a github.com/cenkalti/backoff/v4.PermanentError. Errors from
// the context package need not be wrapped, as context cancellation is checked
// separately by the retrying logic.
type Backend interface { type Backend interface {
// Location returns a string that describes the type and location of the // Location returns a string that describes the type and location of the
// repository. // repository.