2018-01-12 06:20:12 +00:00
|
|
|
package repository_test
|
|
|
|
|
|
|
|
import (
|
2020-07-05 06:37:34 +00:00
|
|
|
"fmt"
|
2018-01-12 06:20:12 +00:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/repository"
|
|
|
|
"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) {
|
2018-01-12 06:20:12 +00:00
|
|
|
idInIdx1 := restic.NewRandomID()
|
|
|
|
idInIdx2 := restic.NewRandomID()
|
2020-06-14 11:26:10 +00:00
|
|
|
idInIdx12 := restic.NewRandomID()
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
blob1 := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
|
|
|
Type: restic.DataBlob,
|
|
|
|
ID: idInIdx1,
|
2020-06-14 11:26:10 +00:00
|
|
|
Length: uint(restic.CiphertextLength(10)),
|
2018-01-12 06:20:12 +00:00
|
|
|
Offset: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
blob2 := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
|
|
|
Type: restic.DataBlob,
|
|
|
|
ID: idInIdx2,
|
2020-06-14 11:26:10 +00:00
|
|
|
Length: uint(restic.CiphertextLength(100)),
|
2018-01-12 06:20:12 +00:00
|
|
|
Offset: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
blob12a := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
|
|
|
Type: restic.TreeBlob,
|
|
|
|
ID: idInIdx12,
|
|
|
|
Length: uint(restic.CiphertextLength(123)),
|
|
|
|
Offset: 110,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
blob12b := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
|
|
|
Type: restic.TreeBlob,
|
|
|
|
ID: idInIdx12,
|
|
|
|
Length: uint(restic.CiphertextLength(123)),
|
|
|
|
Offset: 50,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-12 06:20:12 +00:00
|
|
|
idx1 := repository.NewIndex()
|
|
|
|
idx1.Store(blob1)
|
2020-06-14 11:26:10 +00:00
|
|
|
idx1.Store(blob12a)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
idx2 := repository.NewIndex()
|
|
|
|
idx2.Store(blob2)
|
2020-06-14 11:26:10 +00:00
|
|
|
idx2.Store(blob12b)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
mIdx := repository.NewMasterIndex()
|
|
|
|
mIdx.Insert(idx1)
|
|
|
|
mIdx.Insert(idx2)
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
// test idInIdx1
|
|
|
|
found := mIdx.Has(idInIdx1, restic.DataBlob)
|
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
|
|
|
|
blobs := mIdx.Lookup(idInIdx1, restic.DataBlob)
|
2018-01-12 06:20:12 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob1}, blobs)
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
size, found := mIdx.LookupSize(idInIdx1, restic.DataBlob)
|
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
rtest.Equals(t, uint(10), size)
|
|
|
|
|
|
|
|
// test idInIdx2
|
|
|
|
found = mIdx.Has(idInIdx2, restic.DataBlob)
|
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
|
|
|
|
blobs = mIdx.Lookup(idInIdx2, restic.DataBlob)
|
2018-01-12 06:20:12 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob2}, blobs)
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
size, found = mIdx.LookupSize(idInIdx2, restic.DataBlob)
|
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
rtest.Equals(t, uint(100), size)
|
|
|
|
|
|
|
|
// test idInIdx12
|
|
|
|
found = mIdx.Has(idInIdx12, restic.TreeBlob)
|
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
|
|
|
|
blobs = mIdx.Lookup(idInIdx12, restic.TreeBlob)
|
|
|
|
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")
|
|
|
|
|
|
|
|
size, found = mIdx.LookupSize(idInIdx12, restic.TreeBlob)
|
|
|
|
rtest.Equals(t, true, found)
|
|
|
|
rtest.Equals(t, uint(123), size)
|
|
|
|
|
|
|
|
// test not in index
|
|
|
|
found = mIdx.Has(restic.NewRandomID(), restic.TreeBlob)
|
|
|
|
rtest.Assert(t, !found, "Expected no blobs when fetching with a random id")
|
|
|
|
blobs = mIdx.Lookup(restic.NewRandomID(), restic.DataBlob)
|
2018-01-12 06:20:12 +00:00
|
|
|
rtest.Assert(t, blobs == nil, "Expected no blobs when fetching with a random id")
|
2020-06-14 11:26:10 +00:00
|
|
|
size, found = mIdx.LookupSize(restic.NewRandomID(), restic.DataBlob)
|
|
|
|
rtest.Assert(t, !found, "Expected no blobs when fetching with a random id")
|
|
|
|
|
|
|
|
// Test Count
|
|
|
|
num := mIdx.Count(restic.DataBlob)
|
|
|
|
rtest.Equals(t, uint(2), num)
|
|
|
|
num = mIdx.Count(restic.TreeBlob)
|
|
|
|
rtest.Equals(t, uint(2), num)
|
2018-01-12 06:20:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:53:51 +00:00
|
|
|
func TestMasterMergeFinalIndexes(t *testing.T) {
|
|
|
|
idInIdx1 := restic.NewRandomID()
|
|
|
|
idInIdx2 := restic.NewRandomID()
|
|
|
|
|
|
|
|
blob1 := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
|
|
|
Type: restic.DataBlob,
|
|
|
|
ID: idInIdx1,
|
|
|
|
Length: 10,
|
|
|
|
Offset: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
blob2 := restic.PackedBlob{
|
|
|
|
PackID: restic.NewRandomID(),
|
|
|
|
Blob: restic.Blob{
|
|
|
|
Type: restic.DataBlob,
|
|
|
|
ID: idInIdx2,
|
|
|
|
Length: 100,
|
|
|
|
Offset: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
idx1 := repository.NewIndex()
|
|
|
|
idx1.Store(blob1)
|
|
|
|
|
|
|
|
idx2 := repository.NewIndex()
|
|
|
|
idx2.Store(blob2)
|
|
|
|
|
|
|
|
mIdx := repository.NewMasterIndex()
|
|
|
|
mIdx.Insert(idx1)
|
|
|
|
mIdx.Insert(idx2)
|
|
|
|
|
|
|
|
finalIndexes := mIdx.FinalizeNotFinalIndexes()
|
|
|
|
rtest.Equals(t, []*repository.Index{idx1, idx2}, finalIndexes)
|
|
|
|
|
|
|
|
mIdx.MergeFinalIndexes()
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
blobs := mIdx.Lookup(idInIdx1, restic.DataBlob)
|
2020-07-18 18:53:51 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob1}, blobs)
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
blobs = mIdx.Lookup(idInIdx2, restic.DataBlob)
|
2020-07-18 18:53:51 +00:00
|
|
|
rtest.Equals(t, []restic.PackedBlob{blob2}, blobs)
|
|
|
|
|
2020-06-14 11:26:10 +00:00
|
|
|
blobs = mIdx.Lookup(restic.NewRandomID(), restic.DataBlob)
|
2020-07-18 18:53:51 +00:00
|
|
|
rtest.Assert(t, blobs == nil, "Expected no blobs when fetching with a random id")
|
|
|
|
}
|
|
|
|
|
2020-07-04 05:05:51 +00:00
|
|
|
func createRandomMasterIndex(rng *rand.Rand, num, size int) (*repository.MasterIndex, restic.ID) {
|
2018-01-12 06:20:12 +00:00
|
|
|
mIdx := repository.NewMasterIndex()
|
2020-07-04 05:05:51 +00:00
|
|
|
for i := 0; i < num-1; i++ {
|
|
|
|
idx, _ := createRandomIndex(rng, size)
|
|
|
|
mIdx.Insert(idx)
|
|
|
|
}
|
|
|
|
idx1, lookupID := createRandomIndex(rng, size)
|
2018-01-12 06:20:12 +00:00
|
|
|
mIdx.Insert(idx1)
|
2020-07-18 18:53:51 +00:00
|
|
|
|
|
|
|
mIdx.FinalizeNotFinalIndexes()
|
|
|
|
mIdx.MergeFinalIndexes()
|
|
|
|
|
2020-07-04 05:05:51 +00:00
|
|
|
return mIdx, lookupID
|
|
|
|
}
|
|
|
|
|
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++ {
|
|
|
|
createRandomMasterIndex(rng, 10000, 5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 05:05:51 +00:00
|
|
|
func BenchmarkMasterIndexLookupSingleIndex(b *testing.B) {
|
|
|
|
mIdx, lookupID := createRandomMasterIndex(rand.New(rand.NewSource(0)), 1, 200000)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mIdx.Lookup(lookupID, restic.DataBlob)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupMultipleIndex(b *testing.B) {
|
2020-07-04 05:05:51 +00:00
|
|
|
mIdx, lookupID := createRandomMasterIndex(rand.New(rand.NewSource(0)), 100, 10000)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mIdx.Lookup(lookupID, restic.DataBlob)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupSingleIndexUnknown(b *testing.B) {
|
|
|
|
|
2020-07-04 05:05:51 +00:00
|
|
|
lookupID := restic.NewRandomID()
|
|
|
|
mIdx, _ := createRandomMasterIndex(rand.New(rand.NewSource(0)), 1, 200000)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mIdx.Lookup(lookupID, restic.DataBlob)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupMultipleIndexUnknown(b *testing.B) {
|
|
|
|
lookupID := restic.NewRandomID()
|
2020-07-04 05:05:51 +00:00
|
|
|
mIdx, _ := createRandomMasterIndex(rand.New(rand.NewSource(0)), 100, 10000)
|
2018-01-12 06:20:12 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mIdx.Lookup(lookupID, restic.DataBlob)
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 06:37:34 +00:00
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupParallel(b *testing.B) {
|
|
|
|
mIdx := repository.NewMasterIndex()
|
|
|
|
|
2020-07-04 05:05:51 +00:00
|
|
|
for _, numindices := range []int{25, 50, 100} {
|
2020-07-05 06:37:34 +00:00
|
|
|
var lookupID restic.ID
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
rng := rand.New(rand.NewSource(0))
|
2020-07-04 05:05:51 +00:00
|
|
|
mIdx, lookupID = createRandomMasterIndex(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() {
|
|
|
|
mIdx.Lookup(lookupID, restic.DataBlob)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
lookupID = restic.NewRandomID()
|
|
|
|
name = fmt.Sprintf("unknown,indices=%d", numindices)
|
|
|
|
b.Run(name, func(b *testing.B) {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
mIdx.Lookup(lookupID, restic.DataBlob)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-06-14 11:26:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMasterIndexLookupBlobSize(b *testing.B) {
|
|
|
|
rng := rand.New(rand.NewSource(0))
|
|
|
|
mIdx, lookupID := createRandomMasterIndex(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++ {
|
|
|
|
mIdx.LookupSize(lookupID, restic.DataBlob)
|
2020-07-05 06:37:34 +00:00
|
|
|
}
|
|
|
|
}
|