Skip to content

Commit 30d2cf6

Browse files
itsbilalnicktrav
authored andcommitted
*: add metric for sstable compression types
Previously, we didn't have a metric tracking the compression type(s) of sstables. This change adds that, as a table stat that's collected through the LevelMetadata as an annotator field. This unfortunately had to be a table stat as the compression type isn't known until we open a reader and look at the properties block. Pebble side of cockroachdb/cockroach#123952.
1 parent 2ac449b commit 30d2cf6

File tree

10 files changed

+136
-0
lines changed

10 files changed

+136
-0
lines changed

db.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,11 @@ func (d *DB) Metrics() *Metrics {
20152015
}
20162016
for i := 0; i < numLevels; i++ {
20172017
metrics.Levels[i].Additional.ValueBlocksSize = valueBlocksSizeForLevel(vers, i)
2018+
unknown, snappy, none, zstd := compressionTypesForLevel(vers, i)
2019+
metrics.Table.CompressedCountUnknown += int64(unknown)
2020+
metrics.Table.CompressedCountSnappy += int64(snappy)
2021+
metrics.Table.CompressedCountZstd += int64(zstd)
2022+
metrics.Table.CompressedCountNone += int64(none)
20182023
}
20192024

20202025
d.mu.Unlock()

ingest_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ func TestIngestLoadRand(t *testing.T) {
172172
},
173173
path: paths[i],
174174
}
175+
expected[i].fileMetadata.Stats.CompressionType = sstable.SnappyCompression
175176
expected[i].StatsMarkValid()
176177

177178
func() {

internal/manifest/version.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ type TableStats struct {
7272
RangeDeletionsBytesEstimate uint64
7373
// Total size of value blocks and value index block.
7474
ValueBlocksSize uint64
75+
// CompressionType is the compression type of the table.
76+
CompressionType sstable.Compression
7577
}
7678

7779
// boundType represents the type of key (point or range) present as the smallest

metrics.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,16 @@ type Metrics struct {
265265
BackingTableCount uint64
266266
// The sum of the sizes of the BackingTableCount sstables that are backing virtual tables.
267267
BackingTableSize uint64
268+
// The number of sstables that are compressed with an unknown compression
269+
// algorithm.
270+
CompressedCountUnknown int64
271+
// The number of sstables that are compressed with the default compression
272+
// algorithm, snappy.
273+
CompressedCountSnappy int64
274+
// The number of sstables that are compressed with zstd.
275+
CompressedCountZstd int64
276+
// The number of sstables that are uncompressed.
277+
CompressedCountNone int64
268278

269279
// Local file sizes.
270280
Local struct {
@@ -598,6 +608,20 @@ func (m *Metrics) SafeFormat(w redact.SafePrinter, _ rune) {
598608
redact.Safe(m.NumVirtual()),
599609
humanize.Bytes.Uint64(m.VirtualSize()))
600610
w.Printf("Local tables size: %s\n", humanize.Bytes.Uint64(m.Table.Local.LiveSize))
611+
w.SafeString("Compression types:")
612+
if count := m.Table.CompressedCountSnappy; count > 0 {
613+
w.Printf(" snappy: %d", redact.Safe(count))
614+
}
615+
if count := m.Table.CompressedCountZstd; count > 0 {
616+
w.Printf(" zstd: %d", redact.Safe(count))
617+
}
618+
if count := m.Table.CompressedCountNone; count > 0 {
619+
w.Printf(" none: %d", redact.Safe(count))
620+
}
621+
if count := m.Table.CompressedCountUnknown; count > 0 {
622+
w.Printf(" unknown: %d", redact.Safe(count))
623+
}
624+
w.Print("\n")
601625

602626
formatCacheMetrics := func(m *CacheMetrics, name redact.SafeString) {
603627
w.Printf("%s: %s entries (%s) hit rate: %.1f%%\n",

sstable/options.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ func (c Compression) String() string {
4747
}
4848
}
4949

50+
// CompressionFromString returns an sstable.Compression from its
51+
// string representation. Inverse of c.String() above.
52+
func CompressionFromString(s string) Compression {
53+
switch s {
54+
case "Default":
55+
return DefaultCompression
56+
case "NoCompression":
57+
return NoCompression
58+
case "Snappy":
59+
return SnappyCompression
60+
case "ZSTD":
61+
return ZstdCompression
62+
default:
63+
return DefaultCompression
64+
}
65+
}
66+
5067
// FilterType exports the base.FilterType type.
5168
type FilterType = base.FilterType
5269

table_stats.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ func maybeSetStatsFromProperties(meta physicalMeta, props *sstable.Properties) b
677677
meta.Stats.PointDeletionsBytesEstimate = pointEstimate
678678
meta.Stats.RangeDeletionsBytesEstimate = 0
679679
meta.Stats.ValueBlocksSize = props.ValueBlocksSize
680+
meta.Stats.CompressionType = sstable.CompressionFromString(props.CompressionName)
680681
meta.StatsMarkValid()
681682
return true
682683
}
@@ -1127,3 +1128,66 @@ func valueBlocksSizeForLevel(v *version, level int) (count uint64) {
11271128
}
11281129
return *v.Levels[level].Annotation(valueBlocksSizeAnnotator{}).(*uint64)
11291130
}
1131+
1132+
// compressionTypeAnnotator implements manifest.Annotator, annotating B-tree
1133+
// nodes with the compression type of the file. Its annotation type is a
1134+
// *compressionTypes. The compression type may change once a table's stats are
1135+
// loaded asynchronously, so its values are marked as cacheable only if a file's
1136+
// stats have been loaded.
1137+
type compressionTypeAnnotator struct{}
1138+
1139+
type compressionTypes struct {
1140+
snappy, zstd, none, unknown uint64
1141+
}
1142+
1143+
var _ manifest.Annotator = compressionTypeAnnotator{}
1144+
1145+
func (a compressionTypeAnnotator) Zero(dst interface{}) interface{} {
1146+
if dst == nil {
1147+
return new(compressionTypes)
1148+
}
1149+
v := dst.(*compressionTypes)
1150+
*v = compressionTypes{}
1151+
return v
1152+
}
1153+
1154+
func (a compressionTypeAnnotator) Accumulate(
1155+
f *fileMetadata, dst interface{},
1156+
) (v interface{}, cacheOK bool) {
1157+
vptr := dst.(*compressionTypes)
1158+
switch f.Stats.CompressionType {
1159+
case sstable.SnappyCompression:
1160+
vptr.snappy++
1161+
case sstable.ZstdCompression:
1162+
vptr.zstd++
1163+
case sstable.NoCompression:
1164+
vptr.none++
1165+
default:
1166+
vptr.unknown++
1167+
}
1168+
return vptr, f.StatsValid()
1169+
}
1170+
1171+
func (a compressionTypeAnnotator) Merge(src interface{}, dst interface{}) interface{} {
1172+
srcV := src.(*compressionTypes)
1173+
dstV := dst.(*compressionTypes)
1174+
dstV.snappy = dstV.snappy + srcV.snappy
1175+
dstV.zstd = dstV.zstd + srcV.zstd
1176+
dstV.none = dstV.none + srcV.none
1177+
dstV.unknown = dstV.unknown + srcV.unknown
1178+
return dstV
1179+
}
1180+
1181+
// compressionTypesForLevel returns the count of sstables by compression type
1182+
// used for a level in the LSM. Sstables with compression type snappy or zstd
1183+
// are returned, while others are ignored.
1184+
func compressionTypesForLevel(v *version, level int) (unknown, snappy, none, zstd uint64) {
1185+
if v.Levels[level].Empty() {
1186+
return
1187+
}
1188+
compression := v.Levels[level].Annotation(compressionTypeAnnotator{}).(*compressionTypes)
1189+
if compression == nil {
1190+
return
1191+
}
1192+
return compression.unknown, compression.snappy, compression.none, compression.zstd
1193+
}

testdata/event_listener

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ Zombie tables: 0 (0B, local: 0B)
228228
Backing tables: 0 (0B)
229229
Virtual tables: 0 (0B)
230230
Local tables size: 1.7KB
231+
Compression types: snappy: 3
231232
Block cache: 6 entries (970B) hit rate: 0.0%
232233
Table cache: 1 entries (760B) hit rate: 40.0%
233234
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -328,6 +329,7 @@ Zombie tables: 0 (0B, local: 0B)
328329
Backing tables: 0 (0B)
329330
Virtual tables: 0 (0B)
330331
Local tables size: 3.5KB
332+
Compression types: snappy: 6
331333
Block cache: 12 entries (1.9KB) hit rate: 7.7%
332334
Table cache: 1 entries (760B) hit rate: 50.0%
333335
Secondary cache: 0 entries (0B) hit rate: 0.0%

testdata/ingest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Zombie tables: 0 (0B, local: 0B)
5252
Backing tables: 0 (0B)
5353
Virtual tables: 0 (0B)
5454
Local tables size: 569B
55+
Compression types: snappy: 1
5556
Block cache: 6 entries (945B) hit rate: 30.8%
5657
Table cache: 1 entries (760B) hit rate: 50.0%
5758
Secondary cache: 0 entries (0B) hit rate: 0.0%

testdata/metrics

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Zombie tables: 16 (15B, local: 30B)
2121
Backing tables: 1 (2.0MB)
2222
Virtual tables: 2807 (2.8KB)
2323
Local tables size: 28B
24+
Compression types:
2425
Block cache: 2 entries (1B) hit rate: 42.9%
2526
Table cache: 18 entries (17B) hit rate: 48.7%
2627
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -72,6 +73,7 @@ Zombie tables: 0 (0B, local: 0B)
7273
Backing tables: 0 (0B)
7374
Virtual tables: 0 (0B)
7475
Local tables size: 589B
76+
Compression types: snappy: 1
7577
Block cache: 3 entries (484B) hit rate: 0.0%
7678
Table cache: 1 entries (760B) hit rate: 0.0%
7779
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -127,6 +129,7 @@ Zombie tables: 2 (1.2KB, local: 1.2KB)
127129
Backing tables: 0 (0B)
128130
Virtual tables: 0 (0B)
129131
Local tables size: 595B
132+
Compression types: snappy: 1
130133
Block cache: 5 entries (946B) hit rate: 33.3%
131134
Table cache: 2 entries (1.5KB) hit rate: 66.7%
132135
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -169,6 +172,7 @@ Zombie tables: 2 (1.2KB, local: 1.2KB)
169172
Backing tables: 0 (0B)
170173
Virtual tables: 0 (0B)
171174
Local tables size: 595B
175+
Compression types: snappy: 1
172176
Block cache: 5 entries (946B) hit rate: 33.3%
173177
Table cache: 2 entries (1.5KB) hit rate: 66.7%
174178
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -208,6 +212,7 @@ Zombie tables: 1 (589B, local: 589B)
208212
Backing tables: 0 (0B)
209213
Virtual tables: 0 (0B)
210214
Local tables size: 595B
215+
Compression types: snappy: 1
211216
Block cache: 3 entries (484B) hit rate: 33.3%
212217
Table cache: 1 entries (760B) hit rate: 66.7%
213218
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -251,6 +256,7 @@ Zombie tables: 0 (0B, local: 0B)
251256
Backing tables: 0 (0B)
252257
Virtual tables: 0 (0B)
253258
Local tables size: 595B
259+
Compression types: snappy: 1
254260
Block cache: 0 entries (0B) hit rate: 33.3%
255261
Table cache: 0 entries (0B) hit rate: 66.7%
256262
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -321,6 +327,7 @@ Zombie tables: 0 (0B, local: 0B)
321327
Backing tables: 0 (0B)
322328
Virtual tables: 0 (0B)
323329
Local tables size: 2.6KB
330+
Compression types: snappy: 4
324331
Block cache: 0 entries (0B) hit rate: 33.3%
325332
Table cache: 0 entries (0B) hit rate: 66.7%
326333
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -375,6 +382,7 @@ Zombie tables: 0 (0B, local: 0B)
375382
Backing tables: 0 (0B)
376383
Virtual tables: 0 (0B)
377384
Local tables size: 2.0KB
385+
Compression types: snappy: 3
378386
Block cache: 0 entries (0B) hit rate: 14.3%
379387
Table cache: 0 entries (0B) hit rate: 58.3%
380388
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -478,6 +486,7 @@ Zombie tables: 0 (0B, local: 0B)
478486
Backing tables: 0 (0B)
479487
Virtual tables: 0 (0B)
480488
Local tables size: 4.3KB
489+
Compression types: snappy: 7
481490
Block cache: 12 entries (1.9KB) hit rate: 16.7%
482491
Table cache: 1 entries (760B) hit rate: 60.0%
483492
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -540,6 +549,7 @@ Zombie tables: 0 (0B, local: 0B)
540549
Backing tables: 0 (0B)
541550
Virtual tables: 0 (0B)
542551
Local tables size: 6.1KB
552+
Compression types: snappy: 10
543553
Block cache: 12 entries (1.9KB) hit rate: 16.7%
544554
Table cache: 1 entries (760B) hit rate: 60.0%
545555
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -616,6 +626,7 @@ Zombie tables: 0 (0B, local: 0B)
616626
Backing tables: 2 (1.2KB)
617627
Virtual tables: 2 (102B)
618628
Local tables size: 6.7KB
629+
Compression types: snappy: 9 unknown: 2
619630
Block cache: 0 entries (0B) hit rate: 0.0%
620631
Table cache: 0 entries (0B) hit rate: 0.0%
621632
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -717,6 +728,7 @@ Zombie tables: 0 (0B, local: 0B)
717728
Backing tables: 0 (0B)
718729
Virtual tables: 0 (0B)
719730
Local tables size: 3.8KB
731+
Compression types: snappy: 6
720732
Block cache: 0 entries (0B) hit rate: 0.0%
721733
Table cache: 0 entries (0B) hit rate: 0.0%
722734
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -770,6 +782,7 @@ Zombie tables: 0 (0B, local: 0B)
770782
Backing tables: 0 (0B)
771783
Virtual tables: 0 (0B)
772784
Local tables size: 604B
785+
Compression types: snappy: 1
773786
Block cache: 0 entries (0B) hit rate: 0.0%
774787
Table cache: 0 entries (0B) hit rate: 0.0%
775788
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -807,6 +820,7 @@ Zombie tables: 0 (0B, local: 0B)
807820
Backing tables: 0 (0B)
808821
Virtual tables: 0 (0B)
809822
Local tables size: 0B
823+
Compression types: unknown: 1
810824
Block cache: 1 entries (440B) hit rate: 0.0%
811825
Table cache: 1 entries (760B) hit rate: 0.0%
812826
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -853,6 +867,7 @@ Zombie tables: 0 (0B, local: 0B)
853867
Backing tables: 0 (0B)
854868
Virtual tables: 0 (0B)
855869
Local tables size: 0B
870+
Compression types: snappy: 1 unknown: 1
856871
Block cache: 6 entries (996B) hit rate: 0.0%
857872
Table cache: 1 entries (760B) hit rate: 50.0%
858873
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -900,6 +915,7 @@ Zombie tables: 0 (0B, local: 0B)
900915
Backing tables: 0 (0B)
901916
Virtual tables: 0 (0B)
902917
Local tables size: 589B
918+
Compression types: snappy: 2 unknown: 1
903919
Block cache: 6 entries (996B) hit rate: 0.0%
904920
Table cache: 1 entries (760B) hit rate: 50.0%
905921
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -938,6 +954,7 @@ Zombie tables: 0 (0B, local: 0B)
938954
Backing tables: 0 (0B)
939955
Virtual tables: 0 (0B)
940956
Local tables size: 589B
957+
Compression types: unknown: 3
941958
Block cache: 0 entries (0B) hit rate: 0.0%
942959
Table cache: 0 entries (0B) hit rate: 0.0%
943960
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -975,6 +992,7 @@ Zombie tables: 0 (0B, local: 0B)
975992
Backing tables: 0 (0B)
976993
Virtual tables: 0 (0B)
977994
Local tables size: 603B
995+
Compression types: snappy: 1
978996
Block cache: 0 entries (0B) hit rate: 0.0%
979997
Table cache: 0 entries (0B) hit rate: 0.0%
980998
Secondary cache: 0 entries (0B) hit rate: 0.0%

tool/testdata/db_lsm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Zombie tables: 0 (0B, local: 0B)
3131
Backing tables: 0 (0B)
3232
Virtual tables: 0 (0B)
3333
Local tables size: 709B
34+
Compression types: unknown: 1
3435
Block cache: 0 entries (0B) hit rate: 0.0%
3536
Table cache: 0 entries (0B) hit rate: 0.0%
3637
Secondary cache: 0 entries (0B) hit rate: 0.0%
@@ -64,6 +65,7 @@ Zombie tables: 0 (0B, local: 0B)
6465
Backing tables: 0 (0B)
6566
Virtual tables: 0 (0B)
6667
Local tables size: 709B
68+
Compression types: unknown: 1
6769
Block cache: 0 entries (0B) hit rate: 0.0%
6870
Table cache: 0 entries (0B) hit rate: 0.0%
6971
Secondary cache: 0 entries (0B) hit rate: 0.0%

0 commit comments

Comments
 (0)