Skip to content

Commit

Permalink
db: keep track of virtual sstable count in metrics
Browse files Browse the repository at this point in the history
This pr adds an int to the LevelMetadata and incrementally
keeps track of the number of virtual sstables in the level.

Also adds a field in the LevelMetrics to keep track of virtual
sstables, and print the virtual sstables as part of the metrics
string.
  • Loading branch information
bananabrick committed Oct 3, 2023
1 parent 3c77871 commit a05b019
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 216 deletions.
33 changes: 29 additions & 4 deletions internal/manifest/level_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ import (
type LevelMetadata struct {
level int
totalSize uint64
tree btree
// NumVirtual is the number of virtual sstables in the level.
NumVirtual uint64
tree btree
}

// clone makes a copy of the level metadata, implicitly increasing the ref
// count of every file contained within lm.
func (lm *LevelMetadata) clone() LevelMetadata {
return LevelMetadata{
level: lm.level,
totalSize: lm.totalSize,
tree: lm.tree.Clone(),
level: lm.level,
totalSize: lm.totalSize,
NumVirtual: lm.NumVirtual,
tree: lm.tree.Clone(),
}
}

Expand All @@ -44,6 +47,9 @@ func makeLevelMetadata(cmp Compare, level int, files []*FileMetadata) LevelMetad
lm.tree, _ = makeBTree(bcmp, files)
for _, f := range files {
lm.totalSize += f.Size
if f.Virtual {
lm.NumVirtual++
}
}
return lm
}
Expand All @@ -62,11 +68,17 @@ func (lm *LevelMetadata) insert(f *FileMetadata) error {
return err
}
lm.totalSize += f.Size
if f.Virtual {
lm.NumVirtual++
}
return nil
}

func (lm *LevelMetadata) remove(f *FileMetadata) bool {
lm.totalSize -= f.Size
if f.Virtual {
lm.NumVirtual--
}
return lm.tree.Delete(f)
}

Expand Down Expand Up @@ -303,6 +315,19 @@ func (ls *LevelSlice) SizeSum() uint64 {
return sum
}

// NumVirtual returns the number of virtual sstables in the level. Its runtime is
// linear in the length of the slice.
func (ls *LevelSlice) NumVirtual() uint64 {
var n uint64
iter := ls.Iter()
for f := iter.First(); f != nil; f = iter.Next() {
if f.Virtual {
n++
}
}
return n
}

// Reslice constructs a new slice backed by the same underlying level, with
// new start and end positions. Reslice invokes the provided function, passing
// two LevelIterators: one positioned to i's inclusive start and one
Expand Down
39 changes: 22 additions & 17 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type LevelMetrics struct {
Sublevels int32
// The total number of files in the level.
NumFiles int64
// The total number of virtual sstables in the level.
NumVirtualFiles uint64
// The total size in bytes of the files in the level.
Size int64
// The level's compaction score. This is the compensatedScoreRatio in the
Expand Down Expand Up @@ -107,6 +109,7 @@ type LevelMetrics struct {
// Add updates the counter metrics for the level.
func (m *LevelMetrics) Add(u *LevelMetrics) {
m.NumFiles += u.NumFiles
m.NumVirtualFiles += u.NumVirtualFiles
m.Size += u.Size
m.BytesIn += u.BytesIn
m.BytesIngested += u.BytesIngested
Expand Down Expand Up @@ -361,24 +364,25 @@ func (m *Metrics) Total() LevelMetrics {

// String pretty-prints the metrics as below:
//
// | | | | ingested | moved | written | | amp
// level | tables size val-bl | score | in | tables size | tables size | tables size | read | r w
// ------+---------------------+-------+-------+--------------+--------------+--------------+-------+---------
// 0 | 101 102B 0B | 103.0 | 104B | 112 104B | 113 106B | 221 217B | 107B | 1 2.1
// 1 | 201 202B 0B | 203.0 | 204B | 212 204B | 213 206B | 421 417B | 207B | 2 2.0
// 2 | 301 302B 0B | 303.0 | 304B | 312 304B | 313 306B | 621 617B | 307B | 3 2.0
// 3 | 401 402B 0B | 403.0 | 404B | 412 404B | 413 406B | 821 817B | 407B | 4 2.0
// 4 | 501 502B 0B | 503.0 | 504B | 512 504B | 513 506B | 1.0K 1017B | 507B | 5 2.0
// 5 | 601 602B 0B | 603.0 | 604B | 612 604B | 613 606B | 1.2K 1.2KB | 607B | 6 2.0
// 6 | 701 702B 0B | - | 704B | 712 704B | 713 706B | 1.4K 1.4KB | 707B | 7 2.0
// total | 2.8K 2.7KB 0B | - | 2.8KB | 2.9K 2.8KB | 2.9K 2.8KB | 5.7K 8.4KB | 2.8KB | 28 3.0
// -----------------------------------------------------------------------------------------------------------
// | | | | ingested | moved | written | | amp
// level | tables size val-bl vtables | score | in | tables size | tables size | tables size | read | r w
// ------+-----------------------------+-------+-------+--------------+--------------+--------------+-------+---------
// 0 | 101 102B 0B 0 | 103.0 | 104B | 112 104B | 113 106B | 221 217B | 107B | 1 2.1
// 1 | 201 202B 0B 0 | 203.0 | 204B | 212 204B | 213 206B | 421 417B | 207B | 2 2.0
// 2 | 301 302B 0B 0 | 303.0 | 304B | 312 304B | 313 306B | 621 617B | 307B | 3 2.0
// 3 | 401 402B 0B 0 | 403.0 | 404B | 412 404B | 413 406B | 821 817B | 407B | 4 2.0
// 4 | 501 502B 0B 0 | 503.0 | 504B | 512 504B | 513 506B | 1.0K 1017B | 507B | 5 2.0
// 5 | 601 602B 0B 0 | 603.0 | 604B | 612 604B | 613 606B | 1.2K 1.2KB | 607B | 6 2.0
// 6 | 701 702B 0B 0 | - | 704B | 712 704B | 713 706B | 1.4K 1.4KB | 707B | 7 2.0
// total | 2.8K 2.7KB 0B 0 | - | 2.8KB | 2.9K 2.8KB | 2.9K 2.8KB | 5.7K 8.4KB | 2.8KB | 28 3.0
// -------------------------------------------------------------------------------------------------------------------
// WAL: 22 files (24B) in: 25B written: 26B (4% overhead)
// Flushes: 8
// Compactions: 5 estimated debt: 6B in progress: 2 (7B)
// default: 27 delete: 28 elision: 29 move: 30 read: 31 rewrite: 32 multi-level: 33
// MemTables: 12 (11B) zombie: 14 (13B)
// Zombie tables: 16 (15B)
// Backing tables: 0 (0B)
// Block cache: 2 entries (1B) hit rate: 42.9%
// Table cache: 18 entries (17B) hit rate: 48.7%
// Secondary cache: 40 entries (40B) hit rate: 49.9%
Expand Down Expand Up @@ -416,13 +420,13 @@ func (m *Metrics) SafeFormat(w redact.SafePrinter, _ rune) {
w.SafeString("\n")
}

w.SafeString(" | | | | ingested | moved | written | | amp")
w.SafeString(" | | | | ingested | moved | written | | amp")
appendIfMulti(" | multilevel")
newline()
w.SafeString("level | tables size val-bl | score | in | tables size | tables size | tables size | read | r w")
w.SafeString("level | tables size val-bl vtables | score | in | tables size | tables size | tables size | read | r w")
appendIfMulti(" | top in read")
newline()
w.SafeString("------+---------------------+-------+-------+--------------+--------------+--------------+-------+---------")
w.SafeString("------+-----------------------------+-------+-------+--------------+--------------+--------------+-------+---------")
appendIfMulti("-+------------------")
newline()

Expand All @@ -447,10 +451,11 @@ func (m *Metrics) SafeFormat(w redact.SafePrinter, _ rune) {
wampStr = fmt.Sprintf("%.1f", wamp)
}

w.Printf("| %5s %6s %6s | %5s | %5s | %5s %6s | %5s %6s | %5s %6s | %5s | %3d %4s",
w.Printf("| %5s %6s %6s %7s | %5s | %5s | %5s %6s | %5s %6s | %5s %6s | %5s | %3d %4s",
humanize.Count.Int64(m.NumFiles),
humanize.Bytes.Int64(m.Size),
humanize.Bytes.Uint64(m.Additional.ValueBlocksSize),
humanize.Count.Uint64(m.NumVirtualFiles),
redact.Safe(scoreStr),
humanize.Bytes.Uint64(m.BytesIn),
humanize.Count.Uint64(m.TablesIngested),
Expand Down Expand Up @@ -495,7 +500,7 @@ func (m *Metrics) SafeFormat(w redact.SafePrinter, _ rune) {
w.SafeString("total ")
formatRow(&total, math.NaN())

w.SafeString("-----------------------------------------------------------------------------------------------------------")
w.SafeString("-------------------------------------------------------------------------------------------------------------------")
appendIfMulti("--------------------")
newline()
w.Printf("WAL: %d files (%s) in: %s written: %s (%.0f%% overhead)\n",
Expand Down
11 changes: 11 additions & 0 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func exampleMetrics() Metrics {
base := uint64((i + 1) * 100)
l.Sublevels = int32(i + 1)
l.NumFiles = int64(base) + 1
l.NumVirtualFiles = uint64(base) / 2
l.Size = int64(base) + 2
l.Score = float64(base) + 3
l.BytesIn = base + 4
Expand Down Expand Up @@ -263,6 +264,16 @@ func TestMetrics(t *testing.T) {
buf.WriteString(fmt.Sprintf("%d\n", m.Table.BackingTableCount))
} else if line == "backing-size" {
buf.WriteString(fmt.Sprintf("%s\n", humanize.Bytes.Uint64(m.Table.BackingTableSize)))
} else if strings.HasPrefix(line, "num-virtual") {
splits := strings.Split(line, " ")
l, err := strconv.Atoi(splits[1])
if err != nil {
panic(err)
}
if l >= numLevels {
panic(fmt.Sprintf("invalid level %d", l))
}
buf.WriteString(fmt.Sprintf("%d\n", m.Levels[l].NumVirtualFiles))
} else {
panic(fmt.Sprintf("invalid field: %s", line))
}
Expand Down
48 changes: 24 additions & 24 deletions testdata/event_listener
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,18 @@ remove: ext/0

metrics
----
| | | | ingested | moved | written | | amp
level | tables size val-bl | score | in | tables size | tables size | tables size | read | r w
------+---------------------+-------+-------+--------------+--------------+--------------+-------+---------
0 | 2 1.3KB 0B | 0.40 | 81B | 1 717B | 0 0B | 3 1.9KB | 0B | 2 24.5
1 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
2 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
3 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
4 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
5 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
6 | 1 662B 0B | - | 1.3KB | 0 0B | 0 0B | 1 662B | 1.3KB | 1 0.5
total | 3 2.0KB 0B | - | 825B | 1 717B | 0 0B | 4 3.4KB | 1.3KB | 3 4.2
-----------------------------------------------------------------------------------------------------------
| | | | ingested | moved | written | | amp
level | tables size val-bl vtables | score | in | tables size | tables size | tables size | read | r w
------+-----------------------------+-------+-------+--------------+--------------+--------------+-------+---------
0 | 2 1.3KB 0B 0 | 0.40 | 81B | 1 717B | 0 0B | 3 1.9KB | 0B | 2 24.5
1 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
2 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
3 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
4 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
5 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
6 | 1 662B 0B 0 | - | 1.3KB | 0 0B | 0 0B | 1 662B | 1.3KB | 1 0.5
total | 3 2.0KB 0B 0 | - | 825B | 1 717B | 0 0B | 4 3.4KB | 1.3KB | 3 4.2
-------------------------------------------------------------------------------------------------------------------
WAL: 1 files (27B) in: 48B written: 108B (125% overhead)
Flushes: 3
Compactions: 1 estimated debt: 2.0KB in progress: 0 (0B)
Expand Down Expand Up @@ -366,18 +366,18 @@ sync: db/MANIFEST-000023

metrics
----
| | | | ingested | moved | written | | amp
level | tables size val-bl | score | in | tables size | tables size | tables size | read | r w
------+---------------------+-------+-------+--------------+--------------+--------------+-------+---------
0 | 4 2.7KB 0B | 0.80 | 81B | 2 1.4KB | 0 0B | 4 2.6KB | 0B | 4 32.7
1 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
2 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
3 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
4 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
5 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
6 | 2 1.3KB 0B | - | 1.3KB | 1 717B | 0 0B | 1 662B | 1.3KB | 1 0.5
total | 6 4.0KB 0B | - | 2.2KB | 3 2.1KB | 0 0B | 5 5.4KB | 1.3KB | 5 2.5
-----------------------------------------------------------------------------------------------------------
| | | | ingested | moved | written | | amp
level | tables size val-bl vtables | score | in | tables size | tables size | tables size | read | r w
------+-----------------------------+-------+-------+--------------+--------------+--------------+-------+---------
0 | 4 2.7KB 0B 0 | 0.80 | 81B | 2 1.4KB | 0 0B | 4 2.6KB | 0B | 4 32.7
1 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
2 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
3 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
4 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
5 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
6 | 2 1.3KB 0B 0 | - | 1.3KB | 1 717B | 0 0B | 1 662B | 1.3KB | 1 0.5
total | 6 4.0KB 0B 0 | - | 2.2KB | 3 2.1KB | 0 0B | 5 5.4KB | 1.3KB | 5 2.5
-------------------------------------------------------------------------------------------------------------------
WAL: 1 files (29B) in: 82B written: 110B (34% overhead)
Flushes: 6
Compactions: 1 estimated debt: 4.0KB in progress: 0 (0B)
Expand Down
24 changes: 12 additions & 12 deletions testdata/ingest
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ lsm

metrics
----
| | | | ingested | moved | written | | amp
level | tables size val-bl | score | in | tables size | tables size | tables size | read | r w
------+---------------------+-------+-------+--------------+--------------+--------------+-------+---------
0 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
1 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
2 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
3 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
4 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
5 | 0 0B 0B | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
6 | 1 696B 0B | - | 0B | 1 696B | 0 0B | 0 0B | 0B | 1 0.0
total | 1 696B 0B | - | 696B | 1 696B | 0 0B | 0 696B | 0B | 1 1.0
-----------------------------------------------------------------------------------------------------------
| | | | ingested | moved | written | | amp
level | tables size val-bl vtables | score | in | tables size | tables size | tables size | read | r w
------+-----------------------------+-------+-------+--------------+--------------+--------------+-------+---------
0 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
1 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
2 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
3 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
4 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
5 | 0 0B 0B 0 | 0.00 | 0B | 0 0B | 0 0B | 0 0B | 0B | 0 0.0
6 | 1 696B 0B 0 | - | 0B | 1 696B | 0 0B | 0 0B | 0B | 1 0.0
total | 1 696B 0B 0 | - | 696B | 1 696B | 0 0B | 0 696B | 0B | 1 1.0
-------------------------------------------------------------------------------------------------------------------
WAL: 1 files (0B) in: 0B written: 0B (0% overhead)
Flushes: 0
Compactions: 0 estimated debt: 0B in progress: 0 (0B)
Expand Down
Loading

0 comments on commit a05b019

Please sign in to comment.