2022-06-12 12:43:43 +00:00
|
|
|
package index_test
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
import (
|
2020-08-04 14:42:38 +00:00
|
|
|
"context"
|
2020-07-05 06:37:34 +00:00
|
|
|
"fmt"
|
2018-01-12 06:20:12 +00:00
|
|
|
"math/rand"
|
2022-10-22 21:37:31 +00:00
|
|
|
"runtime"
|
2018-01-12 06:20:12 +00:00
|
|
|
"testing"
|
2020-10-10 05:42:22 +00:00
|
|
|
"time"
|
2018-01-12 06:20:12 +00:00
|
|
|
|
2020-10-10 05:42:22 +00:00
|
|
|
"github.com/restic/restic/internal/checker"
|
2022-06-12 12:48:30 +00:00
|
|
|
"github.com/restic/restic/internal/crypto"
|
2018-01-12 06:20:12 +00:00
|
|
|
"github.com/restic/restic/internal/repository"
|
2024-05-24 21:06:44 +00:00
|
|
|
"github.com/restic/restic/internal/repository/index"
|
2018-01-12 06:20:12 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
func TestMasterIndex(t *testing.T) {
|
2020-11-05 21:18:00 +00:00
|
|
|
bhInIdx1 := restic.NewRandomBlobHandle()
|
|
|
|
bhInIdx2 := restic.NewRandomBlobHandle()
|
|
|
|
bhInIdx12 := restic.BlobHandle{ID: restic.NewRandomID(), Type: restic.TreeBlob}
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
blob1 := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
2020-11-05 21:18:00 +00:00
|
|
|
BlobHandle: bhInIdx1,
|
2022-06-12 12:48:30 +00:00
|
|
|
Length: uint(crypto.CiphertextLength(10)),
|
2020-11-05 21:18:00 +00:00
|
|
|
Offset: 0,
|
2018-01-12 06:20:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
blob2 := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
2022-04-29 21:17:01 +00:00
|
|
|
BlobHandle: bhInIdx2,
|
2022-06-12 12:48:30 +00:00
|
|
|
Length: uint(crypto.CiphertextLength(100)),
|
2022-04-29 21:17:01 +00:00
|
|
|
Offset: 10,
|
|
|
|
UncompressedLength: 200,
|
2018-01-12 06:20:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
blob12a := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
2023-08-19 18:04:22 +00:00
|
|
|
BlobHandle: bhInIdx12,
|
|
|
|
Length: uint(crypto.CiphertextLength(123)),
|
|
|
|
Offset: 110,
|
|
|
|
UncompressedLength: 80,
|
2020-06-14 11:26:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
blob12b := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
2022-04-29 21:17:01 +00:00
|
|
|
BlobHandle: bhInIdx12,
|
2022-06-12 12:48:30 +00:00
|
|
|
Length: uint(crypto.CiphertextLength(123)),
|
2022-04-29 21:17:01 +00:00
|
|
|
Offset: 50,
|
|
|
|
UncompressedLength: 80,
|
2020-06-14 11:26:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-12 12:43:43 +00:00
|
|
|
idx1 := index.NewIndex()
|
2022-05-26 11:41:06 +00:00
|
|
|
idx1.StorePack(blob1.PackID, []restic.Blob{blob1.Blob})
|
|
|
|
idx1.StorePack(blob12a.PackID, []restic.Blob{blob12a.Blob})
|
2018-01-12 06:20:12 +00:00
|
|
|
|
2022-06-12 12:43:43 +00:00
|
|
|
idx2 := index.NewIndex()
|
2022-05-26 11:41:06 +00:00
|
|
|
idx2.StorePack(blob2.PackID, []restic.Blob{blob2.Blob})
|
|
|
|
idx2.StorePack(blob12b.PackID, []restic.Blob{blob12b.Blob})
|
2018-01-12 06:20:12 +00:00
|
|
|
|
2022-06-12 12:43:43 +00:00
|
|
|
mIdx := index.NewMasterIndex()
|
2018-01-12 06:20:12 +00:00
|
|
|
mIdx.Insert(idx1)
|
|
|
|
mIdx.Insert(idx2)
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
// test idInIdx1
|
2020-11-05 21:18:00 +00:00
|
|
|
found := mIdx.Has(bhInIdx1)
|
2020-06-14 11:26:10 +00:00
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
blobs := mIdx.Lookup(bhInIdx1)
|
2018-01-12 06:20:12 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob1}, blobs)
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
size, found := mIdx.LookupSize(bhInIdx1)
|
2020-06-14 11:26:10 +00:00
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
rtest.Equals(t, uint(10), size)
|
|
|
|
|
|
|
|
// test idInIdx2
|
2020-11-05 21:18:00 +00:00
|
|
|
found = mIdx.Has(bhInIdx2)
|
2020-06-14 11:26:10 +00:00
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
blobs = mIdx.Lookup(bhInIdx2)
|
2018-01-12 06:20:12 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob2}, blobs)
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
size, found = mIdx.LookupSize(bhInIdx2)
|
2020-06-14 11:26:10 +00:00
|
|
|
rtest.Equals(t, true, found)
|
2022-04-29 21:17:01 +00:00
|
|
|
rtest.Equals(t, uint(200), size)
|
2020-06-14 11:26:10 +00:00
|
|
|
|
|
|
|
// test idInIdx12
|
2020-11-05 21:18:00 +00:00
|
|
|
found = mIdx.Has(bhInIdx12)
|
2020-06-14 11:26:10 +00:00
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
blobs = mIdx.Lookup(bhInIdx12)
|
2020-06-14 11:26:10 +00:00
|
|
|
rtest.Equals(t, 2, len(blobs))
|
|
|
|
|
|
|
|
// test Lookup result for blob12a
|
|
|
|
found = false
|
|
|
|
if blobs[0] == blob12a || blobs[1] == blob12a {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
rtest.Assert(t, found, "blob12a not found in result")
|
|
|
|
|
|
|
|
// test Lookup result for blob12b
|
|
|
|
found = false
|
|
|
|
if blobs[0] == blob12b || blobs[1] == blob12b {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
rtest.Assert(t, found, "blob12a not found in result")
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
size, found = mIdx.LookupSize(bhInIdx12)
|
2020-06-14 11:26:10 +00:00
|
|
|
rtest.Equals(t, true, found)
|
2023-08-19 18:04:22 +00:00
|
|
|
rtest.Equals(t, uint(80), size)
|
2020-06-14 11:26:10 +00:00
|
|
|
|
|
|
|
// test not in index
|
2020-11-05 21:18:00 +00:00
|
|
|
found = mIdx.Has(restic.BlobHandle{ID: restic.NewRandomID(), Type: restic.TreeBlob})
|
2020-06-14 11:26:10 +00:00
|
|
|
rtest.Assert(t, !found, "Expected no blobs when fetching with a random id")
|
2020-11-05 21:18:00 +00:00
|
|
|
blobs = mIdx.Lookup(restic.NewRandomBlobHandle())
|
2018-01-12 06:20:12 +00:00
|
|
|
rtest.Assert(t, blobs == nil, "Expected no blobs when fetching with a random id")
|
2020-11-05 21:18:00 +00:00
|
|
|
_, found = mIdx.LookupSize(restic.NewRandomBlobHandle())
|
2020-06-14 11:26:10 +00:00
|
|
|
rtest.Assert(t, !found, "Expected no blobs when fetching with a random id")
|
2018-01-12 06:20:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:53:51 +00:00
|
|
|
func TestMasterMergeFinalIndexes(t *testing.T) {
|
2020-11-05 21:18:00 +00:00
|
|
|
bhInIdx1 := restic.NewRandomBlobHandle()
|
|
|
|
bhInIdx2 := restic.NewRandomBlobHandle()
|
2020-07-18 18:53:51 +00:00
|
|
|
|
|
|
|
blob1 := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
2020-11-05 21:18:00 +00:00
|
|
|
BlobHandle: bhInIdx1,
|
|
|
|
Length: 10,
|
|
|
|
Offset: 0,
|
2020-07-18 18:53:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
blob2 := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
2022-04-29 21:17:01 +00:00
|
|
|
BlobHandle: bhInIdx2,
|
|
|
|
Length: 100,
|
|
|
|
Offset: 10,
|
|
|
|
UncompressedLength: 200,
|
2020-07-18 18:53:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-12 12:43:43 +00:00
|
|
|
idx1 := index.NewIndex()
|
2022-05-26 11:41:06 +00:00
|
|
|
idx1.StorePack(blob1.PackID, []restic.Blob{blob1.Blob})
|
2020-07-18 18:53:51 +00:00
|
|
|
|
2022-06-12 12:43:43 +00:00
|
|
|
idx2 := index.NewIndex()
|
2022-05-26 11:41:06 +00:00
|
|
|
idx2.StorePack(blob2.PackID, []restic.Blob{blob2.Blob})
|
2020-07-18 18:53:51 +00:00
|
|
|
|
2022-06-12 12:43:43 +00:00
|
|
|
mIdx := index.NewMasterIndex()
|
2020-07-18 18:53:51 +00:00
|
|
|
mIdx.Insert(idx1)
|
|
|
|
mIdx.Insert(idx2)
|
|
|
|
|
2023-06-02 18:27:47 +00:00
|
|
|
rtest.Equals(t, restic.NewIDSet(), mIdx.IDs())
|
|
|
|
|
|
|
|
finalIndexes, idxCount, ids := index.TestMergeIndex(t, mIdx)
|
2022-06-12 12:43:43 +00:00
|
|
|
rtest.Equals(t, []*index.Index{idx1, idx2}, finalIndexes)
|
2022-06-05 19:57:16 +00:00
|
|
|
rtest.Equals(t, 1, idxCount)
|
2023-06-02 18:27:47 +00:00
|
|
|
rtest.Equals(t, ids, mIdx.IDs())
|
2020-08-04 14:42:38 +00:00
|
|
|
|
|
|
|
blobCount := 0
|
2024-04-05 20:20:14 +00:00
|
|
|
rtest.OK(t, mIdx.Each(context.TODO(), func(pb restic.PackedBlob) {
|
2020-08-04 14:42:38 +00:00
|
|
|
blobCount++
|
2024-04-05 20:20:14 +00:00
|
|
|
}))
|
2020-08-04 14:42:38 +00:00
|
|
|
rtest.Equals(t, 2, blobCount)
|
2020-07-18 18:53:51 +00:00
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
blobs := mIdx.Lookup(bhInIdx1)
|
2020-07-18 18:53:51 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob1}, blobs)
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
blobs = mIdx.Lookup(bhInIdx2)
|
2020-07-18 18:53:51 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob2}, blobs)
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
blobs = mIdx.Lookup(restic.NewRandomBlobHandle())
|
2020-07-18 18:53:51 +00:00
|
|
|
rtest.Assert(t, blobs == nil, "Expected no blobs when fetching with a random id")
|
2020-08-04 14:42:38 +00:00
|
|
|
|
|
|
|
// merge another index containing identical blobs
|
2022-06-12 12:43:43 +00:00
|
|
|
idx3 := index.NewIndex()
|
2022-05-26 11:41:06 +00:00
|
|
|
idx3.StorePack(blob1.PackID, []restic.Blob{blob1.Blob})
|
|
|
|
idx3.StorePack(blob2.PackID, []restic.Blob{blob2.Blob})
|
2020-08-04 14:42:38 +00:00
|
|
|
|
|
|
|
mIdx.Insert(idx3)
|
2023-06-02 18:27:47 +00:00
|
|
|
finalIndexes, idxCount, newIDs := index.TestMergeIndex(t, mIdx)
|
2022-06-12 12:43:43 +00:00
|
|
|
rtest.Equals(t, []*index.Index{idx3}, finalIndexes)
|
2022-06-05 19:57:16 +00:00
|
|
|
rtest.Equals(t, 1, idxCount)
|
2023-06-02 18:27:47 +00:00
|
|
|
ids.Merge(newIDs)
|
|
|
|
rtest.Equals(t, ids, mIdx.IDs())
|
2020-08-04 14:42:38 +00:00
|
|
|
|
|
|
|
// Index should have same entries as before!
|
2020-11-05 21:18:00 +00:00
|
|
|
blobs = mIdx.Lookup(bhInIdx1)
|
2020-08-04 14:42:38 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob1}, blobs)
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
blobs = mIdx.Lookup(bhInIdx2)
|
2020-08-04 14:42:38 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob2}, blobs)
|
|
|
|
|
|
|
|
blobCount = 0
|
2024-04-05 20:20:14 +00:00
|
|
|
rtest.OK(t, mIdx.Each(context.TODO(), func(pb restic.PackedBlob) {
|
2020-08-04 14:42:38 +00:00
|
|
|
blobCount++
|
2024-04-05 20:20:14 +00:00
|
|
|
}))
|
2020-08-04 14:42:38 +00:00
|
|
|
rtest.Equals(t, 2, blobCount)
|
2020-07-18 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:43:43 +00:00
|
|
|
func createRandomMasterIndex(t testing.TB, rng *rand.Rand, num, size int) (*index.MasterIndex, restic.BlobHandle) {
|
|
|
|
mIdx := index.NewMasterIndex()
|
2020-07-04 05:05:51 +00:00
|
|
|
for i := 0; i < num-1; i++ {
|
|
|
|
idx, _ := createRandomIndex(rng, size)
|
|
|
|
mIdx.Insert(idx)
|
|
|
|
}
|
2020-11-05 21:18:00 +00:00
|
|
|
idx1, lookupBh := createRandomIndex(rng, size)
|
2018-01-12 06:20:12 +00:00
|
|
|
mIdx.Insert(idx1)
|
2020-07-18 18:53:51 +00:00
|
|
|
|
2022-06-12 12:43:43 +00:00
|
|
|
index.TestMergeIndex(t, mIdx)
|
2020-07-18 18:53:51 +00:00
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
return mIdx, lookupBh
|
2020-07-04 05:05:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 04:28:24 +00:00
|
|
|
func BenchmarkMasterIndexAlloc(b *testing.B) {
|
|
|
|
rng := rand.New(rand.NewSource(0))
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-01-30 15:35:05 +00:00
|
|
|
createRandomMasterIndex(b, rng, 10000, 5)
|
2020-08-05 04:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 05:05:51 +00:00
|
|
|
func BenchmarkMasterIndexLookupSingleIndex(b *testing.B) {
|
2021-01-30 15:35:05 +00:00
|
|
|
mIdx, lookupBh := createRandomMasterIndex(b, rand.New(rand.NewSource(0)), 1, 200000)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-11-05 21:18:00 +00:00
|
|
|
mIdx.Lookup(lookupBh)
|
2018-01-12 06:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupMultipleIndex(b *testing.B) {
|
2021-01-30 15:35:05 +00:00
|
|
|
mIdx, lookupBh := createRandomMasterIndex(b, rand.New(rand.NewSource(0)), 100, 10000)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-11-05 21:18:00 +00:00
|
|
|
mIdx.Lookup(lookupBh)
|
2018-01-12 06:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupSingleIndexUnknown(b *testing.B) {
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
lookupBh := restic.NewRandomBlobHandle()
|
2021-01-30 15:35:05 +00:00
|
|
|
mIdx, _ := createRandomMasterIndex(b, rand.New(rand.NewSource(0)), 1, 200000)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-11-05 21:18:00 +00:00
|
|
|
mIdx.Lookup(lookupBh)
|
2018-01-12 06:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupMultipleIndexUnknown(b *testing.B) {
|
2020-11-05 21:18:00 +00:00
|
|
|
lookupBh := restic.NewRandomBlobHandle()
|
2021-01-30 15:35:05 +00:00
|
|
|
mIdx, _ := createRandomMasterIndex(b, rand.New(rand.NewSource(0)), 100, 10000)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-11-05 21:18:00 +00:00
|
|
|
mIdx.Lookup(lookupBh)
|
2018-01-12 06:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 06:37:34 +00:00
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupParallel(b *testing.B) {
|
2020-07-04 05:05:51 +00:00
|
|
|
for _, numindices := range []int{25, 50, 100} {
|
2020-11-05 21:18:00 +00:00
|
|
|
var lookupBh restic.BlobHandle
|
2020-07-05 06:37:34 +00:00
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
rng := rand.New(rand.NewSource(0))
|
2022-05-26 10:49:03 +00:00
|
|
|
mIdx, lookupBh := createRandomMasterIndex(b, rng, numindices, 10000)
|
2020-07-05 06:37:34 +00:00
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
name := fmt.Sprintf("known,indices=%d", numindices)
|
|
|
|
b.Run(name, func(b *testing.B) {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
2020-11-05 21:18:00 +00:00
|
|
|
mIdx.Lookup(lookupBh)
|
2020-07-05 06:37:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-11-05 21:18:00 +00:00
|
|
|
lookupBh = restic.NewRandomBlobHandle()
|
2020-07-05 06:37:34 +00:00
|
|
|
name = fmt.Sprintf("unknown,indices=%d", numindices)
|
|
|
|
b.Run(name, func(b *testing.B) {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
2020-11-05 21:18:00 +00:00
|
|
|
mIdx.Lookup(lookupBh)
|
2020-07-05 06:37:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-06-14 11:26:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupBlobSize(b *testing.B) {
|
|
|
|
rng := rand.New(rand.NewSource(0))
|
2021-01-30 15:35:05 +00:00
|
|
|
mIdx, lookupBh := createRandomMasterIndex(b, rand.New(rng), 5, 200000)
|
2020-07-05 06:37:34 +00:00
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-11-05 21:18:00 +00:00
|
|
|
mIdx.LookupSize(lookupBh)
|
2020-07-05 06:37:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-10 05:42:22 +00:00
|
|
|
|
2022-09-10 22:24:39 +00:00
|
|
|
func BenchmarkMasterIndexEach(b *testing.B) {
|
|
|
|
rng := rand.New(rand.NewSource(0))
|
|
|
|
mIdx, _ := createRandomMasterIndex(b, rand.New(rng), 5, 200000)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
entries := 0
|
2024-04-05 20:20:14 +00:00
|
|
|
rtest.OK(b, mIdx.Each(context.TODO(), func(pb restic.PackedBlob) {
|
2022-09-10 22:24:39 +00:00
|
|
|
entries++
|
2024-04-05 20:20:14 +00:00
|
|
|
}))
|
2022-09-10 22:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 21:37:31 +00:00
|
|
|
func BenchmarkMasterIndexGC(b *testing.B) {
|
|
|
|
mIdx, _ := createRandomMasterIndex(b, rand.New(rand.NewSource(0)), 100, 10000)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
runtime.GC()
|
|
|
|
}
|
|
|
|
runtime.KeepAlive(mIdx)
|
|
|
|
}
|
|
|
|
|
2020-10-10 05:42:22 +00:00
|
|
|
var (
|
|
|
|
snapshotTime = time.Unix(1470492820, 207401672)
|
|
|
|
depth = 3
|
|
|
|
)
|
|
|
|
|
2023-07-16 13:55:05 +00:00
|
|
|
func createFilledRepo(t testing.TB, snapshots int, version uint) restic.Repository {
|
2024-05-10 14:59:09 +00:00
|
|
|
repo, _ := repository.TestRepositoryWithVersion(t, version)
|
2020-10-10 05:42:22 +00:00
|
|
|
|
2023-05-18 17:31:42 +00:00
|
|
|
for i := 0; i < snapshots; i++ {
|
2023-07-16 13:55:05 +00:00
|
|
|
restic.TestCreateSnapshot(t, repo, snapshotTime.Add(time.Duration(i)*time.Second), depth)
|
2020-10-10 05:42:22 +00:00
|
|
|
}
|
2022-12-11 09:41:22 +00:00
|
|
|
return repo
|
2020-10-10 05:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndexSave(t *testing.T) {
|
2022-04-29 21:16:16 +00:00
|
|
|
repository.TestAllVersions(t, testIndexSave)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIndexSave(t *testing.T, version uint) {
|
2024-05-19 19:08:19 +00:00
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
|
|
|
saver func(idx *index.MasterIndex, repo restic.Repository) error
|
|
|
|
}{
|
|
|
|
{"rewrite no-op", func(idx *index.MasterIndex, repo restic.Repository) error {
|
|
|
|
return idx.Rewrite(context.TODO(), repo, nil, nil, nil, index.MasterIndexRewriteOpts{})
|
|
|
|
}},
|
|
|
|
{"rewrite skip-all", func(idx *index.MasterIndex, repo restic.Repository) error {
|
|
|
|
return idx.Rewrite(context.TODO(), repo, nil, restic.NewIDSet(), nil, index.MasterIndexRewriteOpts{})
|
|
|
|
}},
|
|
|
|
{"SaveFallback", func(idx *index.MasterIndex, repo restic.Repository) error {
|
|
|
|
err := restic.ParallelRemove(context.TODO(), repo, idx.IDs(), restic.IndexFile, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return idx.SaveFallback(context.TODO(), repo, restic.NewIDSet(), nil)
|
|
|
|
}},
|
|
|
|
} {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
repo := createFilledRepo(t, 3, version)
|
|
|
|
|
|
|
|
idx := index.NewMasterIndex()
|
|
|
|
rtest.OK(t, idx.Load(context.TODO(), repo, nil, nil))
|
|
|
|
blobs := make(map[restic.PackedBlob]struct{})
|
|
|
|
rtest.OK(t, idx.Each(context.TODO(), func(pb restic.PackedBlob) {
|
|
|
|
blobs[pb] = struct{}{}
|
|
|
|
}))
|
|
|
|
|
|
|
|
rtest.OK(t, test.saver(idx, repo))
|
|
|
|
idx = index.NewMasterIndex()
|
|
|
|
rtest.OK(t, idx.Load(context.TODO(), repo, nil, nil))
|
|
|
|
|
|
|
|
rtest.OK(t, idx.Each(context.TODO(), func(pb restic.PackedBlob) {
|
|
|
|
if _, ok := blobs[pb]; ok {
|
|
|
|
delete(blobs, pb)
|
|
|
|
} else {
|
|
|
|
t.Fatalf("unexpected blobs %v", pb)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
rtest.Equals(t, 0, len(blobs), "saved index is missing blobs")
|
|
|
|
|
|
|
|
checker.TestCheckRepo(t, repo, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndexSavePartial(t *testing.T) {
|
|
|
|
repository.TestAllVersions(t, testIndexSavePartial)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIndexSavePartial(t *testing.T, version uint) {
|
2023-07-16 13:55:05 +00:00
|
|
|
repo := createFilledRepo(t, 3, version)
|
2020-10-10 05:42:22 +00:00
|
|
|
|
2024-05-19 19:08:19 +00:00
|
|
|
// capture blob list before adding fourth snapshot
|
2024-05-19 13:48:25 +00:00
|
|
|
idx := index.NewMasterIndex()
|
|
|
|
rtest.OK(t, idx.Load(context.TODO(), repo, nil, nil))
|
|
|
|
blobs := make(map[restic.PackedBlob]struct{})
|
|
|
|
rtest.OK(t, idx.Each(context.TODO(), func(pb restic.PackedBlob) {
|
|
|
|
blobs[pb] = struct{}{}
|
|
|
|
}))
|
2020-10-10 05:42:22 +00:00
|
|
|
|
2024-05-19 19:08:19 +00:00
|
|
|
// add+remove new snapshot and track its pack files
|
|
|
|
packsBefore := listPacks(t, repo)
|
|
|
|
sn := restic.TestCreateSnapshot(t, repo, snapshotTime.Add(time.Duration(4)*time.Second), depth)
|
|
|
|
rtest.OK(t, repo.RemoveUnpacked(context.TODO(), restic.SnapshotFile, *sn.ID()))
|
|
|
|
packsAfter := listPacks(t, repo)
|
|
|
|
newPacks := packsAfter.Sub(packsBefore)
|
|
|
|
|
|
|
|
// rewrite index and remove pack files of new snapshot
|
2024-05-19 13:48:25 +00:00
|
|
|
idx = index.NewMasterIndex()
|
|
|
|
rtest.OK(t, idx.Load(context.TODO(), repo, nil, nil))
|
2024-05-19 19:08:19 +00:00
|
|
|
rtest.OK(t, idx.Rewrite(context.TODO(), repo, newPacks, nil, nil, index.MasterIndexRewriteOpts{}))
|
2020-10-10 05:42:22 +00:00
|
|
|
|
2024-05-19 19:08:19 +00:00
|
|
|
// check blobs
|
|
|
|
idx = index.NewMasterIndex()
|
|
|
|
rtest.OK(t, idx.Load(context.TODO(), repo, nil, nil))
|
2024-05-19 13:48:25 +00:00
|
|
|
rtest.OK(t, idx.Each(context.TODO(), func(pb restic.PackedBlob) {
|
|
|
|
if _, ok := blobs[pb]; ok {
|
|
|
|
delete(blobs, pb)
|
|
|
|
} else {
|
|
|
|
t.Fatalf("unexpected blobs %v", pb)
|
2020-10-10 05:42:22 +00:00
|
|
|
}
|
2024-05-19 13:48:25 +00:00
|
|
|
}))
|
|
|
|
rtest.Equals(t, 0, len(blobs), "saved index is missing blobs")
|
|
|
|
|
2024-05-19 19:08:19 +00:00
|
|
|
// remove pack files to make check happy
|
|
|
|
rtest.OK(t, restic.ParallelRemove(context.TODO(), repo, newPacks, restic.PackFile, nil, nil))
|
|
|
|
|
2024-05-19 13:48:25 +00:00
|
|
|
checker.TestCheckRepo(t, repo, false)
|
2020-10-10 05:42:22 +00:00
|
|
|
}
|
2024-05-19 19:08:19 +00:00
|
|
|
|
|
|
|
func listPacks(t testing.TB, repo restic.Lister) restic.IDSet {
|
|
|
|
s := restic.NewIDSet()
|
|
|
|
rtest.OK(t, repo.List(context.TODO(), restic.PackFile, func(id restic.ID, _ int64) error {
|
|
|
|
s.Insert(id)
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
return s
|
|
|
|
}
|