Skip to content

Commit 52f3c67

Browse files
committed
db: add method to get remote table size, count
This patch adds a method to our `Metrics` struct, `RemoteTablesTotal`, that will compute the total size and count of remote tables. Informs: #143850
1 parent dc2d326 commit 52f3c67

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

metrics.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,26 @@ func (m *Metrics) Total() LevelMetrics {
443443
return total
444444
}
445445

446+
// RemoteTablesTotal returns the total number of remote tables and their total
447+
// size. Remote tables are computed as the difference between total tables
448+
// (live + obsolete + zombie) and local tables.
449+
func (m *Metrics) RemoteTablesTotal() (count uint64, size uint64) {
450+
var liveTables, liveTableBytes int64
451+
for level := 0; level < numLevels; level++ {
452+
liveTables += m.Levels[level].NumFiles
453+
liveTableBytes += m.Levels[level].Size
454+
}
455+
totalCount := liveTables + m.Table.ObsoleteCount + m.Table.ZombieCount
456+
localCount := m.Table.Local.LiveCount + m.Table.Local.ObsoleteCount + m.Table.Local.ZombieCount
457+
remoteCount := uint64(totalCount) - localCount
458+
459+
totalSize := uint64(liveTableBytes) + m.Table.ObsoleteSize + m.Table.ZombieSize
460+
localSize := m.Table.Local.LiveSize + m.Table.Local.ObsoleteSize + m.Table.Local.ZombieSize
461+
remoteSize := totalSize - localSize
462+
463+
return remoteCount, remoteSize
464+
}
465+
446466
// String pretty-prints the metrics as below:
447467
//
448468
// | | | | ingested | moved | written | | amp

metrics_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ func TestMetrics(t *testing.T) {
395395
panic(fmt.Sprintf("invalid level %d", l))
396396
}
397397
buf.WriteString(fmt.Sprintf("%d\n", m.Levels[l].NumVirtualFiles))
398+
} else if line == "remote-count" {
399+
count, _ := m.RemoteTablesTotal()
400+
buf.WriteString(fmt.Sprintf("%d\n", count))
401+
} else if line == "remote-size" {
402+
_, size := m.RemoteTablesTotal()
403+
buf.WriteString(fmt.Sprintf("%s\n", humanize.Bytes.Uint64(size)))
398404
} else {
399405
panic(fmt.Sprintf("invalid field: %s", line))
400406
}
@@ -427,6 +433,7 @@ func TestMetrics(t *testing.T) {
427433
return fmt.Sprintf("unknown command: %s", td.Cmd)
428434
}
429435
})
436+
430437
}
431438

432439
func TestMetricsWAmpDisableWAL(t *testing.T) {

testdata/metrics

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,13 @@ set b 2
870870
ingest ext1.sst
871871
----
872872

873+
metrics-value
874+
remote-count
875+
remote-size
876+
----
877+
2
878+
1.5KB
879+
873880
lsm
874881
----
875882
L0.0:

0 commit comments

Comments
 (0)