Skip to content

Commit b9a9caa

Browse files
committed
pebble: use ReadPropertiesBlock when accessing reader props
Remove remaining reads of `Properties` on the reader struct. This commit also removes the Properties field from the reader. Closes: #4383 Closes: #4790 There are no more reads of `Properties` so we can remove this field now.
1 parent b5fb597 commit b9a9caa

File tree

9 files changed

+69
-42
lines changed

9 files changed

+69
-42
lines changed

data_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,11 +1349,15 @@ func runSSTablePropertiesCmd(t *testing.T, td *datadriven.TestData, d *DB) strin
13491349
}
13501350
defer r.Close()
13511351

1352-
props := r.Properties.String()
1352+
loadedProps, err := r.ReadPropertiesBlock(context.Background(), nil /* buffer pool */)
1353+
if err != nil {
1354+
return err.Error()
1355+
}
1356+
props := loadedProps.String()
13531357
env := sstable.ReadEnv{}
13541358
if m != nil && m.Virtual {
13551359
env.Virtual = m.VirtualParams
1356-
scaledProps := r.Properties.GetScaledProperties(m.TableBacking.Size, m.Size)
1360+
scaledProps := loadedProps.GetScaledProperties(m.TableBacking.Size, m.Size)
13571361
props = scaledProps.String()
13581362
}
13591363
if len(td.Input) == 0 {

file_cache.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,11 @@ func (h *fileCacheHandle) getTableProperties(file *tableMetadata) (*sstable.Prop
946946
defer v.Unref()
947947

948948
r := v.Value().mustSSTableReader()
949-
return &r.Properties, nil
949+
props, err := r.ReadPropertiesBlock(context.TODO(), nil /* buffer pool */)
950+
if err != nil {
951+
return nil, err
952+
}
953+
return &props, nil
950954
}
951955

952956
type fileCacheValue struct {

ingest.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,25 @@ func ingestLoad1(
281281
tf, fmv, fmv.MinTableFormat(), fmv.MaxTableFormat(),
282282
)
283283
}
284+
285+
if r.Attributes.Has(sstable.AttributeBlobValues) {
286+
return nil, keyspan.Span{}, errors.Newf(
287+
"pebble: ingesting tables with blob references is not supported")
288+
}
289+
290+
props, err := r.ReadPropertiesBlock(ctx, nil /* buffer pool */)
291+
if err != nil {
292+
return nil, keyspan.Span{}, err
293+
}
294+
295+
// If this is a columnar block, read key schema name from properties block.
284296
if tf.BlockColumnar() {
285-
if _, ok := opts.KeySchemas[r.Properties.KeySchemaName]; !ok {
297+
if _, ok := opts.KeySchemas[props.KeySchemaName]; !ok {
286298
return nil, keyspan.Span{}, errors.Newf(
287299
"pebble: table uses key schema %q unknown to the database",
288-
r.Properties.KeySchemaName)
300+
props.KeySchemaName)
289301
}
290302
}
291-
if r.Properties.NumValuesInBlobFiles > 0 {
292-
return nil, keyspan.Span{}, errors.Newf(
293-
"pebble: ingesting tables with blob references is not supported")
294-
}
295303

296304
meta = &tableMetadata{}
297305
meta.TableNum = tableNum
@@ -308,7 +316,7 @@ func ingestLoad1(
308316
// disallowing removal of an open file. Under MemFS, if we don't populate
309317
// meta.Stats here, the file will be loaded into the file cache for
310318
// calculating stats before we can remove the original link.
311-
maybeSetStatsFromProperties(meta.PhysicalMeta(), &r.Properties.CommonProperties, opts.Logger)
319+
maybeSetStatsFromProperties(meta.PhysicalMeta(), &props.CommonProperties, opts.Logger)
312320

313321
{
314322
iter, err := r.NewIter(sstable.NoTransforms, nil /* lower */, nil /* upper */, sstable.AssertNoBlobHandles)

sstable/reader.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ type Reader struct {
6565
metaindexBH block.Handle
6666
footerBH block.Handle
6767

68-
Properties Properties
6968
tableFormat TableFormat
7069
Attributes Attributes
7170
UserProperties map[string]string
@@ -958,7 +957,6 @@ func NewReader(ctx context.Context, f objstorage.Readable, o ReaderOptions) (*Re
958957
r.err = err
959958
return nil, err
960959
}
961-
r.Properties = props
962960
r.UserProperties = props.UserProperties
963961

964962
// Set which attributes are in use based on property values.

table_stats.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,13 @@ func (d *DB) loadTableStats(
310310

311311
err := d.fileCache.withReader(
312312
context.TODO(), block.NoReadEnv, meta, func(r *sstable.Reader, env sstable.ReadEnv) (err error) {
313-
props := r.Properties.CommonProperties
313+
loadedProps, err := r.ReadPropertiesBlock(context.TODO(), nil /* buffer pool */)
314+
if err != nil {
315+
return err
316+
}
317+
props := loadedProps.CommonProperties
314318
if meta.Virtual {
315-
props = r.Properties.GetScaledProperties(meta.TableBacking.Size, meta.Size)
319+
props = loadedProps.GetScaledProperties(meta.TableBacking.Size, meta.Size)
316320
}
317321
stats.NumEntries = props.NumEntries
318322
stats.NumDeletions = props.NumDeletions
@@ -504,32 +508,38 @@ func (d *DB) estimateSizesBeneath(
504508
keySum += fileProps.RawKeySize
505509
valSum += fileProps.RawValueSize
506510

507-
addPhysicalTableStats := func(r *sstable.Reader, _ sstable.ReadEnv) (err error) {
508-
fileSum += file.Size
509-
entryCount += r.Properties.NumEntries
510-
keySum += r.Properties.RawKeySize
511-
valSum += r.Properties.RawValueSize
512-
return nil
513-
}
514-
addVirtualTableStats := func(v *sstable.Reader, _ sstable.ReadEnv) (err error) {
515-
fileSum += file.Size
516-
entryCount += file.Stats.NumEntries
517-
keySum += v.Properties.RawKeySize
518-
valSum += v.Properties.RawValueSize
519-
return nil
520-
}
521-
522511
for l := level + 1; l < numLevels; l++ {
523512
// NB: This is subtle. The addPhysicalTableStats and
524513
// addVirtualTableStats functions close over file, which is the loop
525514
// variable.
526515
for file = range v.Overlaps(l, meta.UserKeyBounds()).All() {
527516
var err error
517+
var tableMeta *tableMetadata
528518
if file.Virtual {
529-
err = d.fileCache.withReader(context.TODO(), block.NoReadEnv, file.VirtualMeta(), addVirtualTableStats)
519+
tableMeta = file.VirtualMeta()
530520
} else {
531-
err = d.fileCache.withReader(context.TODO(), block.NoReadEnv, file.PhysicalMeta(), addPhysicalTableStats)
521+
tableMeta = file.PhysicalMeta()
532522
}
523+
524+
err = d.fileCache.withReader(context.TODO(), block.NoReadEnv, tableMeta, func(v *sstable.Reader, _ sstable.ReadEnv) (err error) {
525+
// TODO(xinhaoz): We should avoid reading the properties block here.
526+
// See https://github.com/cockroachdb/pebble/issues/4792.
527+
loadedProps, err := v.ReadPropertiesBlock(context.TODO(), nil /* buffer pool */)
528+
if err != nil {
529+
return err
530+
}
531+
532+
props := loadedProps.CommonProperties
533+
if meta.Virtual {
534+
props = loadedProps.GetScaledProperties(meta.TableBacking.Size, meta.Size)
535+
}
536+
537+
fileSum += file.Size
538+
entryCount += file.Stats.NumEntries
539+
keySum += props.RawKeySize
540+
valSum += props.RawValueSize
541+
return nil
542+
})
533543
if err != nil {
534544
return 0, 0, err
535545
}

testdata/compaction/value_separation

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Local tables size: 834B
140140
Compression types: snappy: 1
141141
Table stats: all loaded
142142
Block cache: 4 entries (1.6KB) hit rate: 70.3%
143-
Table cache: 2 entries (1.1KB) hit rate: 82.2%
143+
Table cache: 2 entries (792B) hit rate: 82.2%
144144
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
145145
Snapshots: 0 earliest seq num: 0
146146
Table iters: 0

testdata/event_listener

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ open: ext/0
209209
read-at(689, 61): ext/0
210210
read-at(639, 50): ext/0
211211
read-at(122, 517): ext/0
212+
read-at(122, 517): ext/0
212213
read-at(81, 41): ext/0
213214
read-at(0, 81): ext/0
214215
close: ext/0
@@ -273,13 +274,15 @@ open: ext/a
273274
read-at(689, 61): ext/a
274275
read-at(639, 50): ext/a
275276
read-at(122, 517): ext/a
277+
read-at(122, 517): ext/a
276278
read-at(81, 41): ext/a
277279
read-at(0, 81): ext/a
278280
close: ext/a
279281
open: ext/b
280282
read-at(689, 61): ext/b
281283
read-at(639, 50): ext/b
282284
read-at(122, 517): ext/b
285+
read-at(122, 517): ext/b
283286
read-at(81, 41): ext/b
284287
read-at(0, 81): ext/b
285288
close: ext/b

testdata/ingest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ Virtual tables: 0 (0B)
5454
Local tables size: 569B
5555
Compression types: snappy: 1
5656
Table stats: all loaded
57-
Block cache: 3 entries (1.1KB) hit rate: 18.2%
58-
Table cache: 1 entries (840B) hit rate: 50.0%
57+
Block cache: 3 entries (1.1KB) hit rate: 15.4%
58+
Table cache: 1 entries (480B) hit rate: 50.0%
5959
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
6060
Snapshots: 0 earliest seq num: 0
6161
Table iters: 0

testdata/metrics

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Local tables size: 755B
7878
Compression types: snappy: 1
7979
Table stats: all loaded
8080
Block cache: 2 entries (795B) hit rate: 0.0%
81-
Table cache: 1 entries (840B) hit rate: 0.0%
81+
Table cache: 1 entries (480B) hit rate: 0.0%
8282
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
8383
Snapshots: 0 earliest seq num: 0
8484
Table iters: 1
@@ -143,7 +143,7 @@ Local tables size: 1.5KB
143143
Compression types: snappy: 2
144144
Table stats: all loaded
145145
Block cache: 2 entries (795B) hit rate: 33.3%
146-
Table cache: 2 entries (1.6KB) hit rate: 66.7%
146+
Table cache: 2 entries (960B) hit rate: 66.7%
147147
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
148148
Snapshots: 0 earliest seq num: 0
149149
Table iters: 2
@@ -191,7 +191,7 @@ Local tables size: 1.5KB
191191
Compression types: snappy: 2
192192
Table stats: all loaded
193193
Block cache: 2 entries (795B) hit rate: 33.3%
194-
Table cache: 2 entries (1.6KB) hit rate: 66.7%
194+
Table cache: 2 entries (960B) hit rate: 66.7%
195195
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
196196
Snapshots: 0 earliest seq num: 0
197197
Table iters: 2
@@ -236,7 +236,7 @@ Local tables size: 1.5KB
236236
Compression types: snappy: 2
237237
Table stats: all loaded
238238
Block cache: 2 entries (795B) hit rate: 33.3%
239-
Table cache: 1 entries (840B) hit rate: 66.7%
239+
Table cache: 1 entries (480B) hit rate: 66.7%
240240
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
241241
Snapshots: 0 earliest seq num: 0
242242
Table iters: 1
@@ -541,7 +541,7 @@ Virtual tables: 0 (0B)
541541
Local tables size: 11KB
542542
Compression types: snappy: 15
543543
Table stats: all loaded
544-
Block cache: 6 entries (2.3KB) hit rate: 7.7%
544+
Block cache: 6 entries (2.3KB) hit rate: 7.3%
545545
Table cache: 0 entries (0B) hit rate: 55.0%
546546
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
547547
Snapshots: 0 earliest seq num: 0
@@ -618,7 +618,7 @@ Virtual tables: 0 (0B)
618618
Local tables size: 16KB
619619
Compression types: snappy: 22
620620
Table stats: all loaded
621-
Block cache: 6 entries (2.3KB) hit rate: 7.7%
621+
Block cache: 6 entries (2.3KB) hit rate: 7.3%
622622
Table cache: 0 entries (0B) hit rate: 55.0%
623623
Range key sets: 0 Tombstones: 0 Total missized tombstones encountered: 0
624624
Snapshots: 0 earliest seq num: 0
@@ -1215,7 +1215,7 @@ Compression types: snappy: 5
12151215
Garbage: point-deletions 502B range-deletions 1.5KB
12161216
Table stats: all loaded
12171217
Block cache: 2 entries (774B) hit rate: 0.0%
1218-
Table cache: 2 entries (1.6KB) hit rate: 0.0%
1218+
Table cache: 2 entries (960B) hit rate: 0.0%
12191219
Range key sets: 0 Tombstones: 3 Total missized tombstones encountered: 0
12201220
Snapshots: 0 earliest seq num: 0
12211221
Table iters: 0
@@ -1257,7 +1257,7 @@ Compression types: snappy: 5
12571257
Garbage: point-deletions 502B range-deletions 1.5KB
12581258
Table stats: all loaded
12591259
Block cache: 2 entries (774B) hit rate: 0.0%
1260-
Table cache: 2 entries (1.6KB) hit rate: 0.0%
1260+
Table cache: 2 entries (960B) hit rate: 0.0%
12611261
Range key sets: 0 Tombstones: 3 Total missized tombstones encountered: 0
12621262
Snapshots: 0 earliest seq num: 0
12631263
Table iters: 0

0 commit comments

Comments
 (0)