Skip to content

Commit

Permalink
db: Add EstimateDiskUsageByBackingType
Browse files Browse the repository at this point in the history
  • Loading branch information
dt committed Jul 19, 2023
1 parent 628c410 commit d9bbb98
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2017,11 +2017,20 @@ func (d *DB) SSTables(opts ...SSTablesOption) ([][]SSTableInfo, error) {
// - There may also exist WAL entries for unflushed keys in this range. This
// estimation currently excludes space used for the range in the WAL.
func (d *DB) EstimateDiskUsage(start, end []byte) (uint64, error) {
bytes, _, _, err := d.EstimateDiskUsageByBackingType(start, end)
return bytes, err
}

// EstimateDiskUsageByBackingType is like EstimateDiskUsage but additionally
// returns the subsets of that size in remote ane external files.
func (d *DB) EstimateDiskUsageByBackingType(
start, end []byte,
) (totalSize, remoteSize, externalSize uint64, _ error) {
if err := d.closed.Load(); err != nil {
panic(err)
}
if d.opts.Comparer.Compare(start, end) > 0 {
return 0, errors.New("invalid key-range specified (start > end)")
return 0, 0, 0, errors.New("invalid key-range specified (start > end)")
}

// Grab and reference the current readState. This prevents the underlying
Expand All @@ -2030,7 +2039,6 @@ func (d *DB) EstimateDiskUsage(start, end []byte) (uint64, error) {
readState := d.loadReadState()
defer readState.unref()

var totalSize uint64
for level, files := range readState.current.Levels {
iter := files.Iter()
if level > 0 {
Expand All @@ -2045,6 +2053,16 @@ func (d *DB) EstimateDiskUsage(start, end []byte) (uint64, error) {
d.opts.Comparer.Compare(file.Largest.UserKey, end) <= 0 {
// The range fully contains the file, so skip looking it up in
// table cache/looking at its indexes, and add the full file size.
meta, err := d.objProvider.Lookup(fileTypeTable, file.FileBacking.DiskFileNum)
if err != nil {
return 0, 0, 0, err
}
if meta.IsShared() {
remoteSize += file.Size
if meta.Shared.CleanupMethod == objstorage.SharedNoCleanup {
externalSize += file.Size
}
}
totalSize += file.Size
} else if d.opts.Comparer.Compare(file.Smallest.UserKey, end) <= 0 &&
d.opts.Comparer.Compare(start, file.Largest.UserKey) <= 0 {
Expand All @@ -2068,13 +2086,23 @@ func (d *DB) EstimateDiskUsage(start, end []byte) (uint64, error) {
)
}
if err != nil {
return 0, err
return 0, 0, 0, err
}
meta, err := d.objProvider.Lookup(fileTypeTable, file.FileBacking.DiskFileNum)
if err != nil {
return 0, 0, 0, err
}
if meta.IsShared() {
remoteSize += size
if meta.Shared.CleanupMethod == objstorage.SharedNoCleanup {
externalSize += size
}
}
totalSize += size
}
}
}
return totalSize, nil
return totalSize, remoteSize, externalSize, nil
}

func (d *DB) walPreallocateSize() int {
Expand Down

0 comments on commit d9bbb98

Please sign in to comment.