repository: Increase index size for repo version 2

A compressed index is only about one third the size of an uncompressed
one. Thus increase the number of entries in an index to avoid cluttering
the repository with small indexes.
This commit is contained in:
Michael Eischer 2022-04-10 12:20:15 +02:00
parent 2535524132
commit 7132df529e
5 changed files with 23 additions and 8 deletions

View file

@ -1470,7 +1470,7 @@ func TestRebuildIndexAlwaysFull(t *testing.T) {
defer func() { defer func() {
repository.IndexFull = indexFull repository.IndexFull = indexFull
}() }()
repository.IndexFull = func(*repository.Index) bool { return true } repository.IndexFull = func(*repository.Index, bool) bool { return true }
testRebuildIndex(t, nil) testRebuildIndex(t, nil)
} }

View file

@ -93,12 +93,13 @@ func (idx *Index) Final() bool {
} }
const ( const (
indexMaxBlobs = 50000 indexMaxBlobs = 50000
indexMaxAge = 10 * time.Minute indexMaxBlobsCompressed = 3 * indexMaxBlobs
indexMaxAge = 10 * time.Minute
) )
// IndexFull returns true iff the index is "full enough" to be saved as a preliminary index. // IndexFull returns true iff the index is "full enough" to be saved as a preliminary index.
var IndexFull = func(idx *Index) bool { var IndexFull = func(idx *Index, compress bool) bool {
idx.m.Lock() idx.m.Lock()
defer idx.m.Unlock() defer idx.m.Unlock()
@ -109,12 +110,18 @@ var IndexFull = func(idx *Index) bool {
blobs += idx.byType[typ].len() blobs += idx.byType[typ].len()
} }
age := time.Since(idx.created) age := time.Since(idx.created)
var maxBlobs uint
if compress {
maxBlobs = indexMaxBlobsCompressed
} else {
maxBlobs = indexMaxBlobs
}
switch { switch {
case age >= indexMaxAge: case age >= indexMaxAge:
debug.Log("index %p is old enough", idx, age) debug.Log("index %p is old enough", idx, age)
return true return true
case blobs >= indexMaxBlobs: case blobs >= maxBlobs:
debug.Log("index %p has %d blobs", idx, blobs) debug.Log("index %p has %d blobs", idx, blobs)
return true return true
} }

View file

@ -16,6 +16,7 @@ type MasterIndex struct {
idx []*Index idx []*Index
pendingBlobs restic.BlobSet pendingBlobs restic.BlobSet
idxMutex sync.RWMutex idxMutex sync.RWMutex
compress bool
} }
// NewMasterIndex creates a new master index. // NewMasterIndex creates a new master index.
@ -28,6 +29,10 @@ func NewMasterIndex() *MasterIndex {
return &MasterIndex{idx: idx, pendingBlobs: restic.NewBlobSet()} return &MasterIndex{idx: idx, pendingBlobs: restic.NewBlobSet()}
} }
func (mi *MasterIndex) markCompressed() {
mi.compress = true
}
// Lookup queries all known Indexes for the ID and returns all matches. // Lookup queries all known Indexes for the ID and returns all matches.
func (mi *MasterIndex) Lookup(bh restic.BlobHandle) (pbs []restic.PackedBlob) { func (mi *MasterIndex) Lookup(bh restic.BlobHandle) (pbs []restic.PackedBlob) {
mi.idxMutex.RLock() mi.idxMutex.RLock()
@ -206,7 +211,7 @@ func (mi *MasterIndex) FinalizeFullIndexes() []*Index {
continue continue
} }
if IndexFull(idx) { if IndexFull(idx, mi.compress) {
debug.Log("index %p is full", idx) debug.Log("index %p is full", idx)
idx.Finalize() idx.Finalize()
list = append(list, idx) list = append(list, idx)
@ -334,7 +339,7 @@ func (mi *MasterIndex) Save(ctx context.Context, repo restic.Repository, packBla
for pbs := range idx.EachByPack(ctx, packBlacklist) { for pbs := range idx.EachByPack(ctx, packBlacklist) {
newIndex.StorePack(pbs.packID, pbs.blobs) newIndex.StorePack(pbs.packID, pbs.blobs)
p.Add(1) p.Add(1)
if IndexFull(newIndex) { if IndexFull(newIndex, mi.compress) {
select { select {
case ch <- newIndex: case ch <- newIndex:
case <-ctx.Done(): case <-ctx.Done():

View file

@ -698,6 +698,9 @@ func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int
} else if err != nil { } else if err != nil {
return errors.Fatalf("config cannot be loaded: %v", err) return errors.Fatalf("config cannot be loaded: %v", err)
} }
if r.Config().Version >= 2 {
r.idx.markCompressed()
}
return nil return nil
} }

View file

@ -367,7 +367,7 @@ func TestRepositoryIncrementalIndex(t *testing.T) {
repo := r.(*repository.Repository) repo := r.(*repository.Repository)
repository.IndexFull = func(*repository.Index) bool { return true } repository.IndexFull = func(*repository.Index, bool) bool { return true }
// add 15 packs // add 15 packs
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {