2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-06 11:00:48 +00:00

Add error handling for fileRestorer

This commit is contained in:
Alexander Weiss 2021-01-04 19:20:04 +01:00
parent 573221aa40
commit 5e22ae10f1
3 changed files with 52 additions and 56 deletions

View File

@ -9,6 +9,8 @@ import (
"sort" "sort"
"sync" "sync"
"golang.org/x/sync/errgroup"
"github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -23,20 +25,16 @@ import (
const ( const (
workerCount = 8 workerCount = 8
// fileInfo flags
fileProgress = 1
fileError = 2
largeFileBlobCount = 25 largeFileBlobCount = 25
) )
// information about regular file being restored // information about regular file being restored
type fileInfo struct { type fileInfo struct {
lock sync.Mutex lock sync.Mutex
flags int inProgress bool
size int64 size int64
location string // file on local filesystem relative to restorer basedir location string // file on local filesystem relative to restorer basedir
blobs interface{} // blobs of the file blobs interface{} // blobs of the file
} }
type fileBlobInfo struct { type fileBlobInfo struct {
@ -60,6 +58,7 @@ type fileRestorer struct {
dst string dst string
files []*fileInfo files []*fileInfo
Error func(string, error) error
} }
func newFileRestorer(dst string, func newFileRestorer(dst string,
@ -73,6 +72,7 @@ func newFileRestorer(dst string,
packLoader: packLoader, packLoader: packLoader,
filesWriter: newFilesWriter(workerCount), filesWriter: newFilesWriter(workerCount),
dst: dst, dst: dst,
Error: restorerAbortOnAllErrors,
} }
} }
@ -142,47 +142,42 @@ func (r *fileRestorer) restoreFiles(ctx context.Context) error {
} }
} }
var wg sync.WaitGroup wg, ctx := errgroup.WithContext(ctx)
downloadCh := make(chan *packInfo) downloadCh := make(chan *packInfo)
worker := func() {
defer wg.Done() worker := func() error {
for { for pack := range downloadCh {
select { if err := r.downloadPack(ctx, pack); err != nil {
case <-ctx.Done(): return err
return // context cancelled
case pack, ok := <-downloadCh:
if !ok {
return // channel closed
}
r.downloadPack(ctx, pack)
} }
} }
return nil
} }
for i := 0; i < workerCount; i++ { for i := 0; i < workerCount; i++ {
wg.Add(1) wg.Go(worker)
go worker()
} }
// the main restore loop // the main restore loop
for _, id := range packOrder { wg.Go(func() error {
pack := packs[id] for _, id := range packOrder {
select { pack := packs[id]
case <-ctx.Done(): select {
return ctx.Err() case <-ctx.Done():
case downloadCh <- pack: return ctx.Err()
debug.Log("Scheduled download pack %s", pack.id.Str()) case downloadCh <- pack:
debug.Log("Scheduled download pack %s", pack.id.Str())
}
} }
} close(downloadCh)
return nil
})
close(downloadCh) return wg.Wait()
wg.Wait()
return nil
} }
const maxBufferSize = 4 * 1024 * 1024 const maxBufferSize = 4 * 1024 * 1024
func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) { func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) error {
// calculate pack byte range and blob->[]files->[]offsets mappings // calculate pack byte range and blob->[]files->[]offsets mappings
start, end := int64(math.MaxInt64), int64(0) start, end := int64(math.MaxInt64), int64(0)
@ -237,12 +232,11 @@ func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) {
return blobs[sortedBlobs[i]].offset < blobs[sortedBlobs[j]].offset return blobs[sortedBlobs[i]].offset < blobs[sortedBlobs[j]].offset
}) })
markFileError := func(file *fileInfo, err error) { sanitizeError := func(file *fileInfo, err error) error {
file.lock.Lock() if err != nil {
defer file.lock.Unlock() err = r.Error(file.location, err)
if file.flags&fileError == 0 {
file.flags |= fileError
} }
return err
} }
h := restic.Handle{Type: restic.PackFile, Name: pack.id.String()} h := restic.Handle{Type: restic.PackFile, Name: pack.id.String()}
@ -263,7 +257,9 @@ func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) {
blobData, buf, err = r.loadBlob(bufRd, blobID, blob.length, buf) blobData, buf, err = r.loadBlob(bufRd, blobID, blob.length, buf)
if err != nil { if err != nil {
for file := range blob.files { for file := range blob.files {
markFileError(file, err) if errFile := sanitizeError(file, err); errFile != nil {
return errFile
}
} }
continue continue
} }
@ -277,37 +273,36 @@ func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) {
// - should allow concurrent writes to the file // - should allow concurrent writes to the file
// so write the first blob while holding file lock // so write the first blob while holding file lock
// write other blobs after releasing the lock // write other blobs after releasing the lock
file.lock.Lock()
create := file.flags&fileProgress == 0
createSize := int64(-1) createSize := int64(-1)
if create { file.lock.Lock()
defer file.lock.Unlock() if file.inProgress {
file.flags |= fileProgress
createSize = file.size
} else {
file.lock.Unlock() file.lock.Unlock()
} else {
defer file.lock.Unlock()
file.inProgress = true
createSize = file.size
} }
return r.filesWriter.writeToFile(r.targetPath(file.location), blobData, offset, createSize) return r.filesWriter.writeToFile(r.targetPath(file.location), blobData, offset, createSize)
} }
err := writeToFile() err := sanitizeError(file, writeToFile())
if err != nil { if err != nil {
markFileError(file, err) return err
break
} }
} }
} }
} }
return nil return nil
}) })
if err != nil { if err != nil {
for file := range pack.files { for file := range pack.files {
markFileError(file, err) if errFile := sanitizeError(file, err); errFile != nil {
return errFile
}
} }
return
} }
return nil
} }
func (r *fileRestorer) loadBlob(rd io.Reader, blobID restic.ID, length int, buf []byte) ([]byte, []byte, error) { func (r *fileRestorer) loadBlob(rd io.Reader, blobID restic.ID, length int, buf []byte) ([]byte, []byte, error) {

View File

@ -262,5 +262,5 @@ func TestErrorRestoreFiles(t *testing.T) {
r.files = repo.files r.files = repo.files
err := r.restoreFiles(context.TODO()) err := r.restoreFiles(context.TODO())
rtest.Assert(t, err != nil, "restoreFiles should have reported an error!") rtest.Equals(t, loadError, err)
} }

View File

@ -216,6 +216,7 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) error {
idx := restic.NewHardlinkIndex() idx := restic.NewHardlinkIndex()
filerestorer := newFileRestorer(dst, res.repo.Backend().Load, res.repo.Key(), res.repo.Index().Lookup) filerestorer := newFileRestorer(dst, res.repo.Backend().Load, res.repo.Key(), res.repo.Index().Lookup)
filerestorer.Error = res.Error
debug.Log("first pass for %q", dst) debug.Log("first pass for %q", dst)