Compare commits

...

3 commits

Author SHA1 Message Date
Michael Eischer
e1bb2129ad report result of delete operation after failed upload 2021-03-08 21:54:46 +01:00
Michael Eischer
95b7f8dd81 s3: Fix sanity check
The sanity check shouldn't replace the error message if there is already
one.
2021-03-08 21:54:46 +01:00
Michael Eischer
29e39e247a stat file after retrying an upload 2021-03-08 21:54:46 +01:00
3 changed files with 22 additions and 2 deletions

View file

@ -57,6 +57,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.
func (be *RetryBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
firstTry := true
return be.retry(ctx, fmt.Sprintf("Save(%v)", h), func() error {
err := rd.Rewind()
if err != nil {
@ -65,13 +66,27 @@ func (be *RetryBackend) Save(ctx context.Context, h restic.Handle, rd restic.Rew
err = be.Backend.Save(ctx, h, rd)
if err == nil {
if !firstTry {
fi, err := be.Backend.Stat(ctx, h)
if err != nil {
return err
}
if fi.Size != rd.Length() {
return fmt.Errorf("Stat after save found unexpected length %q instead of %q", fi.Size, rd.Length())
}
}
return nil
}
firstTry = false
debug.Log("Save(%v) failed with error, removing file: %v", h, err)
rerr := be.Backend.Remove(ctx, h)
if rerr != nil {
debug.Log("Remove(%v) returned error: %v", h, err)
if be.Report != nil {
be.Report(fmt.Sprintf("Delete-failed-Save(%v)", h), err, time.Duration(0))
}
}
// return original error

View file

@ -16,6 +16,7 @@ import (
func TestBackendSaveRetry(t *testing.T) {
buf := bytes.NewBuffer(nil)
errcount := 0
uploadSize := int64(0)
be := &mock.Backend{
SaveFn: func(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
if errcount == 0 {
@ -28,9 +29,13 @@ func TestBackendSaveRetry(t *testing.T) {
return errors.New("injected error")
}
_, err := io.Copy(buf, rd)
var err error
uploadSize, err = io.Copy(buf, rd)
return err
},
StatFn: func(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
return restic.FileInfo{Name: h.Name, Size: uploadSize}, nil
},
}
retryBackend := NewRetryBackend(be, 10, nil)

View file

@ -277,7 +277,7 @@ func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRe
debug.Log("%v -> %v bytes, err %#v: %v", objName, info.Size, err, err)
// sanity check
if err != nil && info.Size != rd.Length() {
if err == nil && info.Size != rd.Length() {
return errors.Errorf("wrote %d bytes instead of the expected %d bytes", info.Size, rd.Length())
}