From d9bbb98b0910d582584fe5bf60415306663b3837 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 18 Jul 2023 20:08:05 +0000 Subject: [PATCH] db: Add EstimateDiskUsageByBackingType --- db.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/db.go b/db.go index bdccfb8cfe..63a403538d 100644 --- a/db.go +++ b/db.go @@ -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 @@ -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 { @@ -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 { @@ -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 {