Skip to content

Commit 0265c88

Browse files
committed
db: incorporate blob files into EstimateDiskUsage
This patch ensures that blob files are also included in disk usage estimation. Fixes: #4621
1 parent 939f319 commit 0265c88

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

db.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,12 @@ type DB struct {
501501
// annotators contains various instances of manifest.Annotator which
502502
// should be protected from concurrent access.
503503
annotators struct {
504-
totalSize *manifest.Annotator[uint64]
505-
remoteSize *manifest.Annotator[uint64]
506-
externalSize *manifest.Annotator[uint64]
504+
// totalFileSize is the sum of the size of all files in the
505+
// database. This includes local, remote, and external sstables --
506+
// along with blob files.
507+
totalFileSize *manifest.Annotator[uint64]
508+
remoteSize *manifest.Annotator[uint64]
509+
externalSize *manifest.Annotator[uint64]
507510
}
508511
}
509512

@@ -2375,24 +2378,30 @@ func (d *DB) SSTables(opts ...SSTablesOption) ([][]SSTableInfo, error) {
23752378
return destLevels, nil
23762379
}
23772380

2378-
// makeFileSizeAnnotator returns an annotator that computes the total size of
2379-
// files that meet some criteria defined by filter.
2381+
// makeFileSizeAnnotator returns an annotator that computes the total
2382+
// storage size of files that meet some criteria defined by filter. When
2383+
// applicable, this includes both the sstable size and the size of any
2384+
// referenced blob files.
23802385
func (d *DB) makeFileSizeAnnotator(filter func(f *tableMetadata) bool) *manifest.Annotator[uint64] {
23812386
return &manifest.Annotator[uint64]{
23822387
Aggregator: manifest.SumAggregator{
23832388
AccumulateFunc: func(f *tableMetadata) (uint64, bool) {
23842389
if filter(f) {
2385-
return f.Size, true
2390+
return f.Size + f.EstimatedReferenceSize(), true
23862391
}
23872392
return 0, true
23882393
},
23892394
AccumulatePartialOverlapFunc: func(f *tableMetadata, bounds base.UserKeyBounds) uint64 {
23902395
if filter(f) {
2391-
size, err := d.fileCache.estimateSize(f, bounds.Start, bounds.End.Key)
2396+
overlappingFileSize, err := d.fileCache.estimateSize(f, bounds.Start, bounds.End.Key)
23922397
if err != nil {
23932398
return 0
23942399
}
2395-
return size
2400+
overlapFraction := float64(overlappingFileSize) / float64(f.Size)
2401+
// Scale the blob reference size proportionally to the file
2402+
// overlap from the bounds to approximate only the blob
2403+
// references that overlap with the requested bounds.
2404+
return overlappingFileSize + uint64(float64(f.EstimatedReferenceSize())*overlapFraction)
23962405
}
23972406
return 0
23982407
},
@@ -2438,9 +2447,10 @@ func (d *DB) EstimateDiskUsageByBackingType(
24382447
readState := d.loadReadState()
24392448
defer readState.unref()
24402449

2441-
totalSize = *d.mu.annotators.totalSize.VersionRangeAnnotation(readState.current, bounds)
2450+
totalSize = *d.mu.annotators.totalFileSize.VersionRangeAnnotation(readState.current, bounds)
24422451
remoteSize = *d.mu.annotators.remoteSize.VersionRangeAnnotation(readState.current, bounds)
24432452
externalSize = *d.mu.annotators.externalSize.VersionRangeAnnotation(readState.current, bounds)
2453+
24442454
return
24452455
}
24462456

open.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func Open(dirname string, opts *Options) (db *DB, err error) {
417417
d.newIters = d.fileCache.newIters
418418
d.tableNewRangeKeyIter = tableNewRangeKeyIter(d.newIters)
419419

420-
d.mu.annotators.totalSize = d.makeFileSizeAnnotator(func(f *manifest.TableMetadata) bool {
420+
d.mu.annotators.totalFileSize = d.makeFileSizeAnnotator(func(f *manifest.TableMetadata) bool {
421421
return true
422422
})
423423
d.mu.annotators.remoteSize = d.makeFileSizeAnnotator(func(f *manifest.TableMetadata) bool {

tool/testdata/db_space

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,49 @@ db space
22
----
33
accepts 1 arg(s), received 0
44

5-
# covers the whole 4.sst
6-
5+
# Covers the whole 4.sst.
76
db space --start=a --end=z
87
../testdata/db-stage-4
98
----
109
709
1110

12-
# covers from left of 4.sst to its only data block
13-
11+
# Covers from left of 4.sst to its only data block.
1412
db space --start=a --end=bar
1513
../testdata/db-stage-4
1614
----
1715
62
1816

19-
# covers from 4.sst's only data block to its right
20-
17+
# Covers from 4.sst's only data block to its right.
2118
db space --start=foo --end=z
2219
../testdata/db-stage-4
2320
----
2421
62
2522

26-
# covers non-overlapping range to left of 4.sst
27-
23+
# Covers non-overlapping range to left of 4.sst.
2824
db space --start=a --end=a
2925
../testdata/db-stage-4
3026
----
3127
0
3228

33-
# covers non-overlapping range to right of 4.sst
34-
29+
# Covers non-overlapping range to right of 4.sst.
3530
db space --start=z --end=z
3631
../testdata/db-stage-4
3732
----
3833
0
34+
35+
# Covers the whole 000005.sst and referenced blob file 000006.blob. The size of
36+
# the referenced blob file is 74 bytes. Note that this sst only has 1 data block
37+
# with in bounds [a, d].
38+
db space --start=a --end=z
39+
testdata/find-db-val-sep
40+
----
41+
888
42+
43+
# Covers part of 000005.sst and part of referenced blob file 000006.blob. Note
44+
# that the estimated size of the sst from [c, d] is the size of the entire data
45+
# block + the estimated partial size of 000006.blob (14 bytes as more bytes are
46+
# in [a, b]).
47+
db space --start=c --end=d
48+
testdata/find-db-val-sep
49+
----
50+
173

0 commit comments

Comments
 (0)