2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 22:27:35 +00:00

errcheck: Add error check for MergeFinalIndexes()

This commit is contained in:
Alexander Neumann 2021-01-30 16:35:05 +01:00
parent 75f53955ee
commit 16313bfcc9
4 changed files with 44 additions and 16 deletions

View File

@ -111,7 +111,11 @@ func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) {
} }
// Merge index before computing pack sizes, as this needs removed duplicates // Merge index before computing pack sizes, as this needs removed duplicates
c.masterIndex.MergeFinalIndexes() err = c.masterIndex.MergeFinalIndexes()
if err != nil {
// abort if an error occurs merging the indexes
return hints, append(errs, err)
}
// compute pack size using index entries // compute pack size using index entries
c.packs = c.masterIndex.PackSize(ctx, false) c.packs = c.masterIndex.PackSize(ctx, false)

View File

@ -2,6 +2,7 @@ package repository
import ( import (
"context" "context"
"fmt"
"sync" "sync"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
@ -271,7 +272,7 @@ func (mi *MasterIndex) Each(ctx context.Context) <-chan restic.PackedBlob {
// Indexes that are not final are left untouched. // Indexes that are not final are left untouched.
// This merging can only be called after all index files are loaded - as // This merging can only be called after all index files are loaded - as
// removing of superseded index contents is only possible for unmerged indexes. // removing of superseded index contents is only possible for unmerged indexes.
func (mi *MasterIndex) MergeFinalIndexes() { func (mi *MasterIndex) MergeFinalIndexes() error {
mi.idxMutex.Lock() mi.idxMutex.Lock()
defer mi.idxMutex.Unlock() defer mi.idxMutex.Unlock()
@ -284,10 +285,15 @@ func (mi *MasterIndex) MergeFinalIndexes() {
if !idx.Final() { if !idx.Final() {
newIdx = append(newIdx, idx) newIdx = append(newIdx, idx)
} else { } else {
mi.idx[0].merge(idx) err := mi.idx[0].merge(idx)
if err != nil {
return fmt.Errorf("MergeFinalIndexes: %w", err)
}
} }
} }
mi.idx = newIdx mi.idx = newIdx
return nil
} }
const saveIndexParallelism = 4 const saveIndexParallelism = 4

View File

@ -163,7 +163,11 @@ func TestMasterMergeFinalIndexes(t *testing.T) {
finalIndexes := mIdx.FinalizeNotFinalIndexes() finalIndexes := mIdx.FinalizeNotFinalIndexes()
rtest.Equals(t, []*repository.Index{idx1, idx2}, finalIndexes) rtest.Equals(t, []*repository.Index{idx1, idx2}, finalIndexes)
mIdx.MergeFinalIndexes() err := mIdx.MergeFinalIndexes()
if err != nil {
t.Fatal(err)
}
allIndexes := mIdx.All() allIndexes := mIdx.All()
rtest.Equals(t, 1, len(allIndexes)) rtest.Equals(t, 1, len(allIndexes))
@ -191,7 +195,11 @@ func TestMasterMergeFinalIndexes(t *testing.T) {
finalIndexes = mIdx.FinalizeNotFinalIndexes() finalIndexes = mIdx.FinalizeNotFinalIndexes()
rtest.Equals(t, []*repository.Index{idx3}, finalIndexes) rtest.Equals(t, []*repository.Index{idx3}, finalIndexes)
mIdx.MergeFinalIndexes() err = mIdx.MergeFinalIndexes()
if err != nil {
t.Fatal(err)
}
allIndexes = mIdx.All() allIndexes = mIdx.All()
rtest.Equals(t, 1, len(allIndexes)) rtest.Equals(t, 1, len(allIndexes))
@ -209,7 +217,7 @@ func TestMasterMergeFinalIndexes(t *testing.T) {
rtest.Equals(t, 2, blobCount) rtest.Equals(t, 2, blobCount)
} }
func createRandomMasterIndex(rng *rand.Rand, num, size int) (*repository.MasterIndex, restic.BlobHandle) { func createRandomMasterIndex(t testing.TB, rng *rand.Rand, num, size int) (*repository.MasterIndex, restic.BlobHandle) {
mIdx := repository.NewMasterIndex() mIdx := repository.NewMasterIndex()
for i := 0; i < num-1; i++ { for i := 0; i < num-1; i++ {
idx, _ := createRandomIndex(rng, size) idx, _ := createRandomIndex(rng, size)
@ -219,7 +227,10 @@ func createRandomMasterIndex(rng *rand.Rand, num, size int) (*repository.MasterI
mIdx.Insert(idx1) mIdx.Insert(idx1)
mIdx.FinalizeNotFinalIndexes() mIdx.FinalizeNotFinalIndexes()
mIdx.MergeFinalIndexes() err := mIdx.MergeFinalIndexes()
if err != nil {
t.Fatal(err)
}
return mIdx, lookupBh return mIdx, lookupBh
} }
@ -229,12 +240,12 @@ func BenchmarkMasterIndexAlloc(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
createRandomMasterIndex(rng, 10000, 5) createRandomMasterIndex(b, rng, 10000, 5)
} }
} }
func BenchmarkMasterIndexLookupSingleIndex(b *testing.B) { func BenchmarkMasterIndexLookupSingleIndex(b *testing.B) {
mIdx, lookupBh := createRandomMasterIndex(rand.New(rand.NewSource(0)), 1, 200000) mIdx, lookupBh := createRandomMasterIndex(b, rand.New(rand.NewSource(0)), 1, 200000)
b.ResetTimer() b.ResetTimer()
@ -244,7 +255,7 @@ func BenchmarkMasterIndexLookupSingleIndex(b *testing.B) {
} }
func BenchmarkMasterIndexLookupMultipleIndex(b *testing.B) { func BenchmarkMasterIndexLookupMultipleIndex(b *testing.B) {
mIdx, lookupBh := createRandomMasterIndex(rand.New(rand.NewSource(0)), 100, 10000) mIdx, lookupBh := createRandomMasterIndex(b, rand.New(rand.NewSource(0)), 100, 10000)
b.ResetTimer() b.ResetTimer()
@ -256,7 +267,7 @@ func BenchmarkMasterIndexLookupMultipleIndex(b *testing.B) {
func BenchmarkMasterIndexLookupSingleIndexUnknown(b *testing.B) { func BenchmarkMasterIndexLookupSingleIndexUnknown(b *testing.B) {
lookupBh := restic.NewRandomBlobHandle() lookupBh := restic.NewRandomBlobHandle()
mIdx, _ := createRandomMasterIndex(rand.New(rand.NewSource(0)), 1, 200000) mIdx, _ := createRandomMasterIndex(b, rand.New(rand.NewSource(0)), 1, 200000)
b.ResetTimer() b.ResetTimer()
@ -267,7 +278,7 @@ func BenchmarkMasterIndexLookupSingleIndexUnknown(b *testing.B) {
func BenchmarkMasterIndexLookupMultipleIndexUnknown(b *testing.B) { func BenchmarkMasterIndexLookupMultipleIndexUnknown(b *testing.B) {
lookupBh := restic.NewRandomBlobHandle() lookupBh := restic.NewRandomBlobHandle()
mIdx, _ := createRandomMasterIndex(rand.New(rand.NewSource(0)), 100, 10000) mIdx, _ := createRandomMasterIndex(b, rand.New(rand.NewSource(0)), 100, 10000)
b.ResetTimer() b.ResetTimer()
@ -284,7 +295,7 @@ func BenchmarkMasterIndexLookupParallel(b *testing.B) {
b.StopTimer() b.StopTimer()
rng := rand.New(rand.NewSource(0)) rng := rand.New(rand.NewSource(0))
mIdx, lookupBh = createRandomMasterIndex(rng, numindices, 10000) mIdx, lookupBh = createRandomMasterIndex(b, rng, numindices, 10000)
b.StartTimer() b.StartTimer()
name := fmt.Sprintf("known,indices=%d", numindices) name := fmt.Sprintf("known,indices=%d", numindices)
@ -310,7 +321,7 @@ func BenchmarkMasterIndexLookupParallel(b *testing.B) {
func BenchmarkMasterIndexLookupBlobSize(b *testing.B) { func BenchmarkMasterIndexLookupBlobSize(b *testing.B) {
rng := rand.New(rand.NewSource(0)) rng := rand.New(rand.NewSource(0))
mIdx, lookupBh := createRandomMasterIndex(rand.New(rng), 5, 200000) mIdx, lookupBh := createRandomMasterIndex(b, rand.New(rng), 5, 200000)
b.ResetTimer() b.ResetTimer()

View File

@ -419,7 +419,11 @@ func (r *Repository) saveIndex(ctx context.Context, indexes ...*Index) error {
debug.Log("Saved index %d as %v", i, sid) debug.Log("Saved index %d as %v", i, sid)
} }
r.idx.MergeFinalIndexes()
err := r.idx.MergeFinalIndexes()
if err != nil {
return err
}
return nil return nil
} }
@ -461,7 +465,10 @@ func (r *Repository) LoadIndex(ctx context.Context) error {
return errors.Fatal(err.Error()) return errors.Fatal(err.Error())
} }
r.idx.MergeFinalIndexes() err = r.idx.MergeFinalIndexes()
if err != nil {
return err
}
// remove index files from the cache which have been removed in the repo // remove index files from the cache which have been removed in the repo
return r.PrepareCache(validIndex) return r.PrepareCache(validIndex)