Skip to content

Commit 507d3bd

Browse files
committed
sstable: add RawKeySize and RawValueSize to TableStats
Add these properties to table stats so we don't need to load the properties block when calculating table stats. Closes: #4792
1 parent 4b3a0ff commit 507d3bd

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

ingest_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,17 @@ func TestIngestLoadRand(t *testing.T) {
216216
w := sstable.NewRawWriter(objstorageprovider.NewFileWritable(f), sstable.WriterOptions{
217217
TableFormat: version.MaxTableFormat(),
218218
})
219-
var count uint64
219+
var count, rawKeySize uint64
220220
for i := range keys {
221221
if i > 0 && base.InternalCompare(cmp, keys[i-1], keys[i]) == 0 {
222222
// Duplicate key, ignore.
223223
continue
224224
}
225225
require.NoError(t, w.Add(keys[i], nil, false /* forceObsolete */))
226226
count++
227+
rawKeySize += uint64(keys[i].Size())
227228
}
229+
expected[i].Stats.RawKeySize = rawKeySize
228230
expected[i].Stats.NumEntries = count
229231
require.NoError(t, w.Close())
230232

internal/manifest/table_metadata.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,8 @@ type TableStats struct {
11971197
// This statistic is used to determine eligibility for a tombstone density
11981198
// compaction.
11991199
TombstoneDenseBlocksRatio float64
1200+
RawKeySize uint64
1201+
RawValueSize uint64
12001202
}
12011203

12021204
// CompactionState is the compaction state of a file.

internal/manifest/table_metadata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestTableMetadataSize(t *testing.T) {
148148
}
149149
structSize := unsafe.Sizeof(TableMetadata{})
150150

151-
const tableMetadataSize = 304
151+
const tableMetadataSize = 320
152152
if structSize != tableMetadataSize {
153153
t.Errorf("TableMetadata struct size (%d bytes) is not expected size (%d bytes)",
154154
structSize, tableMetadataSize)

table_stats.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ func (d *DB) loadTableStats(
319319
stats.NumDeletions = props.NumDeletions
320320
stats.NumRangeKeySets = props.NumRangeKeySets
321321
stats.ValueBlocksSize = props.ValueBlocksSize
322+
stats.RawKeySize = props.RawKeySize
323+
stats.RawValueSize = props.RawValueSize
322324
stats.CompressionType = block.CompressionFromString(props.CompressionName)
323325
if props.NumDataBlocks > 0 {
324326
stats.TombstoneDenseBlocksRatio = float64(props.NumTombstoneDenseBlocks) / float64(props.NumDataBlocks)
@@ -509,9 +511,15 @@ func (d *DB) estimateSizesBeneath(
509511

510512
for l := level + 1; l < numLevels; l++ {
511513
for tableBeneath := range v.Overlaps(l, meta.UserKeyBounds()).All() {
514+
fileSum += tableBeneath.Size
515+
if tableBeneath.StatsValid() {
516+
entryCount += tableBeneath.Stats.NumEntries
517+
keySum += tableBeneath.Stats.RawKeySize
518+
valSum += tableBeneath.Stats.RawValueSize
519+
continue
520+
}
521+
// If stats aren't available, we need to read the properties block.
512522
err := d.fileCache.withReader(ctx, block.NoReadEnv, tableBeneath, func(v *sstable.Reader, _ sstable.ReadEnv) (err error) {
513-
// TODO(xinhaoz): We should avoid reading the properties block here.
514-
// See https://github.com/cockroachdb/pebble/issues/4792.
515523
loadedProps, err := v.ReadPropertiesBlock(ctx, nil /* buffer pool */)
516524
if err != nil {
517525
return err
@@ -521,8 +529,7 @@ func (d *DB) estimateSizesBeneath(
521529
props = loadedProps.GetScaledProperties(tableBeneath.TableBacking.Size, tableBeneath.Size)
522530
}
523531

524-
fileSum += tableBeneath.Size
525-
entryCount += tableBeneath.Stats.NumEntries
532+
entryCount += props.NumEntries
526533
keySum += props.RawKeySize
527534
valSum += props.RawValueSize
528535
return nil
@@ -730,6 +737,8 @@ func maybeSetStatsFromProperties(
730737
meta.Stats.PointDeletionsBytesEstimate = pointEstimate
731738
meta.Stats.RangeDeletionsBytesEstimate = 0
732739
meta.Stats.ValueBlocksSize = props.ValueBlocksSize
740+
meta.Stats.RawKeySize = props.RawKeySize
741+
meta.Stats.RawValueSize = props.RawValueSize
733742
meta.Stats.CompressionType = block.CompressionFromString(props.CompressionName)
734743
meta.StatsMarkValid()
735744
sanityCheckStats(meta, logger, "stats from properties")

0 commit comments

Comments
 (0)