Skip to content

Commit 3e3c617

Browse files
committed
db: consolidate backing props annotations
1 parent 6b149af commit 3e3c617

File tree

2 files changed

+45
-75
lines changed

2 files changed

+45
-75
lines changed

db.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,8 +1877,9 @@ func (d *DB) Metrics() *Metrics {
18771877
metrics.private.optionsFileSize = d.optionsFileSize
18781878

18791879
// TODO(jackson): Consider making these metrics optional.
1880-
metrics.Keys.RangeKeySetsCount = *rangeKeySetsAnnotator.MultiLevelAnnotation(vers.RangeKeyLevels[:])
1881-
metrics.Keys.TombstoneCount = *tombstonesAnnotator.MultiLevelAnnotation(vers.Levels[:])
1880+
aggProps := tablePropsAnnotator.MultiLevelAnnotation(vers.Levels[:])
1881+
metrics.Keys.RangeKeySetsCount = aggProps.NumRangeKeySets
1882+
metrics.Keys.TombstoneCount = aggProps.NumDeletions
18821883

18831884
delBytes := deletionBytesAnnotator.MultiLevelAnnotation(vers.Levels[:])
18841885
metrics.Table.Garbage.PointDeletionsBytesEstimate = delBytes.PointDels
@@ -1909,9 +1910,9 @@ func (d *DB) Metrics() *Metrics {
19091910
metrics.Table.InitialStatsCollectionComplete = d.mu.tableStats.loadedInitial
19101911

19111912
for i := 0; i < numLevels; i++ {
1912-
metrics.Levels[i].Additional.ValueBlocksSize = *valueBlockSizeAnnotator.LevelAnnotation(vers.Levels[i])
1913-
compressionMetrics := compressionStatsAnnotator.LevelAnnotation(vers.Levels[i])
1914-
metrics.Table.Compression.MergeWith(compressionMetrics)
1913+
aggProps := tablePropsAnnotator.LevelAnnotation(vers.Levels[i])
1914+
metrics.Levels[i].Additional.ValueBlocksSize = aggProps.ValueBlocksSize
1915+
metrics.Table.Compression.MergeWith(&aggProps.CompressionMetrics)
19151916
}
19161917

19171918
blobCompressionMetrics := blobCompressionStatsAnnotator.Annotation(&vers.BlobFiles)

table_stats.go

Lines changed: 39 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,40 +1072,6 @@ func newCombinedDeletionKeyspanIter(
10721072
return mIter, nil
10731073
}
10741074

1075-
// rangeKeySetsAnnotator is a manifest.TableAnnotator that annotates B-Tree nodes
1076-
// with the sum of the files' counts of range key fragments. The count of range
1077-
// key sets may change once a table's stats are loaded asynchronously, so its
1078-
// values are marked as cacheable only if a file's stats have been loaded.
1079-
var rangeKeySetsAnnotator = manifest.SumAnnotator(func(f *manifest.TableMetadata) (uint64, bool) {
1080-
if props, ok := f.TableBacking.Properties(); ok {
1081-
return f.ScaleStatistic(props.NumRangeKeySets), true
1082-
}
1083-
return 0, false
1084-
})
1085-
1086-
// tombstonesAnnotator is a manifest.TableAnnotator that annotates B-Tree nodes
1087-
// with the sum of the files' counts of tombstones (DEL, SINGLEDEL and RANGEDEL
1088-
// keys). The count of tombstones may change once a table's stats are loaded
1089-
// asynchronously, so its values are marked as cacheable only if a file's stats
1090-
// have been loaded.
1091-
var tombstonesAnnotator = manifest.SumAnnotator(func(f *manifest.TableMetadata) (uint64, bool) {
1092-
if props, ok := f.TableBacking.Properties(); ok {
1093-
return f.ScaleStatistic(props.NumDeletions), true
1094-
}
1095-
return 0, false
1096-
})
1097-
1098-
// valueBlocksSizeAnnotator is a manifest.TableAnnotator that annotates B-Tree
1099-
// nodes with the sum of the files' Properties.ValueBlocksSize. The value block
1100-
// size may change once a table's stats are loaded asynchronously, so its
1101-
// values are marked as cacheable only if a file's stats have been loaded.
1102-
var valueBlockSizeAnnotator = manifest.SumAnnotator(func(f *manifest.TableMetadata) (uint64, bool) {
1103-
if props, ok := f.TableBacking.Properties(); ok {
1104-
return f.ScaleStatistic(props.ValueBlocksSize), true
1105-
}
1106-
return 0, false
1107-
})
1108-
11091075
type deletionBytes struct {
11101076
// PointDels contains a sum of TableStats.PointDeletionsBytesEstimate.
11111077
PointDels uint64
@@ -1129,44 +1095,47 @@ var deletionBytesAnnotator = manifest.NewTableAnnotator[deletionBytes](manifest.
11291095
},
11301096
})
11311097

1132-
// compressionStatsAnnotator is a manifest.TableAnnotator that annotates B-tree nodes
1133-
// with the compression statistics for tables. Its annotation type is
1134-
// block.CompressionStats. The compression type may change once a table's stats
1135-
// are loaded asynchronously, so its values are marked as cacheable only if a
1136-
// file's stats have been loaded. Statistics for virtual tables are estimated
1137-
// from the physical table statistics, proportional to the estimated virtual
1138-
// table size.
1139-
var compressionStatsAnnotator = manifest.NewTableAnnotator[CompressionMetrics](compressionStatsAggregator{})
1140-
1141-
type compressionStatsAggregator struct{}
1142-
1143-
func (a compressionStatsAggregator) Zero() *CompressionMetrics {
1144-
return &CompressionMetrics{}
1145-
}
1146-
1147-
func (a compressionStatsAggregator) Accumulate(
1148-
f *manifest.TableMetadata, dst *CompressionMetrics,
1149-
) (v *CompressionMetrics, cacheOK bool) {
1150-
stats, statsValid := f.TableBacking.Properties()
1151-
if !statsValid || stats.CompressionStats.IsEmpty() {
1152-
dst.CompressedBytesWithoutStats += f.Size
1153-
return dst, statsValid
1154-
}
1155-
compressionStats := stats.CompressionStats
1156-
if f.Virtual {
1157-
// Scale the compression stats for virtual tables.
1158-
compressionStats = compressionStats.Scale(f.Size, f.TableBacking.Size)
1159-
}
1160-
dst.Add(&compressionStats)
1161-
return dst, true
1098+
// annotatedTableProps are properties derived from TableBackingProperties that
1099+
// are aggregated for metrics.
1100+
type aggregatedTableProps struct {
1101+
// NumRangeKeySets is the sum of the tables' counts of range key fragments.
1102+
NumRangeKeySets uint64
1103+
// NumDeletions is the sum of the tables' counts of tombstones (DEL, SINGLEDEL
1104+
// and RANGEDEL keys).
1105+
NumDeletions uint64
1106+
// ValueBlocksSize is the sum of the tables' Properties.ValueBlocksSize.
1107+
ValueBlocksSize uint64
1108+
1109+
CompressionMetrics CompressionMetrics
11621110
}
11631111

1164-
func (a compressionStatsAggregator) Merge(
1165-
src *CompressionMetrics, dst *CompressionMetrics,
1166-
) *CompressionMetrics {
1167-
dst.MergeWith(src)
1168-
return dst
1169-
}
1112+
var tablePropsAnnotator = manifest.NewTableAnnotator[aggregatedTableProps](manifest.SumAggregator[aggregatedTableProps]{
1113+
AddFunc: func(src, dst *aggregatedTableProps) {
1114+
dst.NumRangeKeySets += src.NumRangeKeySets
1115+
dst.NumDeletions += src.NumDeletions
1116+
dst.ValueBlocksSize += src.ValueBlocksSize
1117+
dst.CompressionMetrics.MergeWith(&src.CompressionMetrics)
1118+
},
1119+
AccumulateFunc: func(f *manifest.TableMetadata) (v aggregatedTableProps, cacheOK bool) {
1120+
props, propsValid := f.TableBacking.Properties()
1121+
if propsValid {
1122+
v.NumRangeKeySets = props.NumRangeKeySets
1123+
v.NumDeletions = props.NumDeletions
1124+
v.ValueBlocksSize = props.ValueBlocksSize
1125+
}
1126+
if !propsValid || props.CompressionStats.IsEmpty() {
1127+
v.CompressionMetrics.CompressedBytesWithoutStats = f.ScaleStatistic(f.Size)
1128+
} else {
1129+
compressionStats := props.CompressionStats
1130+
if f.Virtual {
1131+
// Scale the compression stats for virtual tables.
1132+
compressionStats = compressionStats.Scale(f.Size, f.TableBacking.Size)
1133+
}
1134+
v.CompressionMetrics.Add(&compressionStats)
1135+
}
1136+
return v, propsValid
1137+
},
1138+
})
11701139

11711140
// compressionStatsAnnotator is a manifest.TableAnnotator that annotates B-tree nodes
11721141
// with the compression statistics for tables. Its annotation type is

0 commit comments

Comments
 (0)