Skip to content

Commit 6d0d3c1

Browse files
committed
block: introduce blockkinds
Introduce an enum of block kinds. This replaces two existing similar enums - one in the compression analyzer and one in objiotracing. This will allow integrating the compression analyzer in the read path, and implementing a more complex `block.Compressor` which can take the kind into account.
1 parent a1bc642 commit 6d0d3c1

File tree

17 files changed

+165
-140
lines changed

17 files changed

+165
-140
lines changed

objstorage/objstorageprovider/objiotracing/obj_io_tracing.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package objiotracing
66

7-
import "github.com/cockroachdb/pebble/internal/base"
7+
import (
8+
"github.com/cockroachdb/pebble/internal/base"
9+
"github.com/cockroachdb/pebble/sstable/block/blockkind"
10+
)
811

912
// OpType indicates the type of operation.
1013
type OpType uint8
@@ -33,18 +36,6 @@ const (
3336
// TODO(radu): add ForUserFacing.
3437
)
3538

36-
// BlockType indicates the type of data block relevant to an operation.
37-
type BlockType uint8
38-
39-
// BlockType values.
40-
const (
41-
UnknownBlock BlockType = iota
42-
DataBlock
43-
ValueBlock
44-
FilterBlock
45-
MetadataBlock
46-
)
47-
4839
// Event is the on-disk format of a tracing event. It is exported here so that
4940
// trace processing tools can use it by importing this package.
5041
type Event struct {
@@ -54,7 +45,7 @@ type Event struct {
5445
StartUnixNano int64
5546
Op OpType
5647
Reason Reason
57-
BlockType BlockType
48+
BlockKind blockkind.Kind
5849
// LSM level plus one (with 0 indicating unknown level).
5950
LevelPlusOne uint8
6051
// Hardcoded padding so that struct layout doesn't depend on architecture.

objstorage/objstorageprovider/objiotracing/obj_io_tracing_off.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/cockroachdb/pebble/internal/base"
1313
"github.com/cockroachdb/pebble/objstorage"
14+
"github.com/cockroachdb/pebble/sstable/block/blockkind"
1415
"github.com/cockroachdb/pebble/vfs"
1516
)
1617

@@ -49,9 +50,9 @@ func (t *Tracer) WrapWritable(
4950
// traces created under that context).
5051
func WithReason(ctx context.Context, reason Reason) context.Context { return ctx }
5152

52-
// WithBlockType creates a context that has an associated BlockType (which ends up in
53+
// WithBlockKind creates a context that has an associated BlockType (which ends up in
5354
// traces created under that context).
54-
func WithBlockType(ctx context.Context, blockType BlockType) context.Context { return ctx }
55+
func WithBlockKind(ctx context.Context, kind blockkind.Kind) context.Context { return ctx }
5556

5657
// WithLevel creates a context that has an associated level (which ends up in
5758
// traces created under that context).

objstorage/objstorageprovider/objiotracing/obj_io_tracing_on.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/cockroachdb/pebble/internal/base"
2020
"github.com/cockroachdb/pebble/objstorage"
21+
"github.com/cockroachdb/pebble/sstable/block/blockkind"
2122
"github.com/cockroachdb/pebble/vfs"
2223
)
2324

@@ -234,7 +235,7 @@ func (rh *readHandle) RecordCacheHit(ctx context.Context, offset, size int64) {
234235

235236
type ctxInfo struct {
236237
reason Reason
237-
blockType BlockType
238+
blockKind blockkind.Kind
238239
levelPlusOne uint8
239240
}
240241

@@ -243,8 +244,8 @@ func mergeCtxInfo(base, other ctxInfo) ctxInfo {
243244
if res.reason == 0 {
244245
res.reason = base.reason
245246
}
246-
if res.blockType == 0 {
247-
res.blockType = base.blockType
247+
if res.blockKind == 0 {
248+
res.blockKind = base.blockKind
248249
}
249250
if res.levelPlusOne == 0 {
250251
res.levelPlusOne = base.levelPlusOne
@@ -274,11 +275,11 @@ func WithReason(ctx context.Context, reason Reason) context.Context {
274275
return withInfo(ctx, info)
275276
}
276277

277-
// WithBlockType creates a context that has an associated BlockType (which ends up in
278+
// WithBlockKind creates a context that has an associated BlockType (which ends up in
278279
// traces created under that context).
279-
func WithBlockType(ctx context.Context, blockType BlockType) context.Context {
280+
func WithBlockKind(ctx context.Context, kind blockkind.Kind) context.Context {
280281
info := infoFromCtx(ctx)
281-
info.blockType = blockType
282+
info.blockKind = kind
282283
return withInfo(ctx, info)
283284
}
284285

@@ -328,7 +329,7 @@ func (g *eventGenerator) add(ctx context.Context, e Event) {
328329
info := infoFromCtx(ctx)
329330
info = mergeCtxInfo(g.baseCtxInfo, info)
330331
e.Reason = info.reason
331-
e.BlockType = info.blockType
332+
e.BlockKind = info.blockKind
332333
e.LevelPlusOne = info.levelPlusOne
333334
if g.buf.num == eventsPerBuf {
334335
g.flush()

objstorage/objstorageprovider/objiotracing/obj_io_tracing_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/cockroachdb/pebble"
1515
"github.com/cockroachdb/pebble/internal/base"
1616
"github.com/cockroachdb/pebble/objstorage/objstorageprovider/objiotracing"
17+
"github.com/cockroachdb/pebble/sstable/block/blockkind"
1718
"github.com/cockroachdb/pebble/vfs"
1819
"github.com/stretchr/testify/require"
1920
)
@@ -128,6 +129,6 @@ func TestTracing(t *testing.T) {
128129
events = collectEvents()
129130
// Expect L6 data block reads.
130131
require.Greater(t, num(func(e Event) bool {
131-
return e.Op == objiotracing.ReadOp && e.BlockType == objiotracing.DataBlock && e.LevelPlusOne == 7
132+
return e.Op == objiotracing.ReadOp && e.BlockKind == blockkind.SSTableData && e.LevelPlusOne == 7
132133
}), 0)
133134
}

sstable/blob/blob.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/cockroachdb/pebble/objstorage"
1818
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
1919
"github.com/cockroachdb/pebble/sstable/block"
20+
"github.com/cockroachdb/pebble/sstable/block/blockkind"
2021
)
2122

2223
var (
@@ -460,14 +461,14 @@ func (r *FileReader) InitReadHandle(
460461
func (r *FileReader) ReadValueBlock(
461462
ctx context.Context, env block.ReadEnv, rh objstorage.ReadHandle, h block.Handle,
462463
) (block.BufferHandle, error) {
463-
return r.r.Read(ctx, env, rh, h, initBlobValueBlockMetadata)
464+
return r.r.Read(ctx, env, rh, h, blockkind.BlobValue, initBlobValueBlockMetadata)
464465
}
465466

466467
// ReadIndexBlock reads the index block from the file.
467468
func (r *FileReader) ReadIndexBlock(
468469
ctx context.Context, env block.ReadEnv, rh objstorage.ReadHandle,
469470
) (block.BufferHandle, error) {
470-
return r.r.Read(ctx, env, rh, r.footer.indexHandle, initIndexBlockMetadata)
471+
return r.r.Read(ctx, env, rh, r.footer.indexHandle, blockkind.Metadata, initIndexBlockMetadata)
471472
}
472473

473474
// IndexHandle returns the block handle for the file's index block.

sstable/blob/fetcher.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/cockroachdb/pebble/internal/invariants"
1414
"github.com/cockroachdb/pebble/objstorage"
1515
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
16-
"github.com/cockroachdb/pebble/objstorage/objstorageprovider/objiotracing"
1716
"github.com/cockroachdb/pebble/sstable/block"
1817
)
1918

@@ -212,7 +211,6 @@ func (cr *cachedReader) GetUnsafeValue(
212211
// If we already have a block loaded (eg, we're scanning retrieving multiple
213212
// values), the current block might contain the value.
214213
if !cr.currentValueBlock.loaded || cr.currentValueBlock.virtualID != vh.BlockID {
215-
ctx = objiotracing.WithBlockType(ctx, objiotracing.ValueBlock)
216214
if !cr.indexBlock.loaded {
217215
// Read the index block.
218216
var err error

sstable/block/block.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ import (
2525
"github.com/cockroachdb/pebble/internal/sstableinternal"
2626
"github.com/cockroachdb/pebble/objstorage"
2727
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
28+
"github.com/cockroachdb/pebble/objstorage/objstorageprovider/objiotracing"
29+
"github.com/cockroachdb/pebble/sstable/block/blockkind"
2830
)
2931

32+
// Kind is a convenience alias.
33+
type Kind = blockkind.Kind
34+
3035
// Handle is the file offset and length of a block.
3136
type Handle struct {
3237
// Offset identifies the offset of the block within the file.
@@ -447,6 +452,7 @@ func (r *Reader) Read(
447452
env ReadEnv,
448453
readHandle objstorage.ReadHandle,
449454
bh Handle,
455+
kind Kind,
450456
initBlockMetadataFn func(*Metadata, []byte) error,
451457
) (handle BufferHandle, _ error) {
452458
// The compaction path uses env.BufferPool, and does not coordinate read
@@ -459,7 +465,7 @@ func (r *Reader) Read(
459465
return CacheBufferHandle(cv), nil
460466
}
461467
}
462-
value, err := r.doRead(ctx, env, readHandle, bh, initBlockMetadataFn)
468+
value, err := r.doRead(ctx, env, readHandle, bh, kind, initBlockMetadataFn)
463469
if err != nil {
464470
return BufferHandle{}, env.maybeReportCorruption(err)
465471
}
@@ -492,7 +498,7 @@ func (r *Reader) Read(
492498
return CacheBufferHandle(cv), nil
493499
}
494500

495-
value, err := r.doRead(ctx, env, readHandle, bh, initBlockMetadataFn)
501+
value, err := r.doRead(ctx, env, readHandle, bh, kind, initBlockMetadataFn)
496502
if err != nil {
497503
crh.SetReadError(err)
498504
return BufferHandle{}, env.maybeReportCorruption(err)
@@ -519,8 +525,10 @@ func (r *Reader) doRead(
519525
env ReadEnv,
520526
readHandle objstorage.ReadHandle,
521527
bh Handle,
528+
kind Kind,
522529
initBlockMetadataFn func(*Metadata, []byte) error,
523530
) (Value, error) {
531+
ctx = objiotracing.WithBlockKind(ctx, kind)
524532
// First acquire loadBlockSema, if needed.
525533
if sema := r.opts.LoadBlockSema; sema != nil {
526534
if err := sema.Acquire(ctx, 1); err != nil {

sstable/block/blockkind/kind.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use
2+
// of this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
5+
package blockkind
6+
7+
import "iter"
8+
9+
// Kind identifies the type of block.
10+
type Kind uint8
11+
12+
const (
13+
Unknown Kind = iota
14+
SSTableData
15+
SSTableIndex
16+
SSTableValue
17+
BlobValue
18+
Index
19+
Filter
20+
RangeDel
21+
RangeKey
22+
Metadata
23+
24+
NumKinds
25+
)
26+
27+
var kindString = [...]string{
28+
Unknown: "unknown",
29+
SSTableData: "data",
30+
SSTableValue: "sstval",
31+
SSTableIndex: "index",
32+
BlobValue: "blobval",
33+
Filter: "filter",
34+
RangeDel: "rangedel",
35+
RangeKey: "rangekey",
36+
Metadata: "metadata",
37+
}
38+
39+
func (k Kind) String() string {
40+
return kindString[k]
41+
}
42+
43+
// All returns all block kinds.
44+
func All() iter.Seq[Kind] {
45+
return func(yield func(Kind) bool) {
46+
for i := Kind(1); i < NumKinds; i++ {
47+
if !yield(i) {
48+
break
49+
}
50+
}
51+
}
52+
}

sstable/compressionanalyzer/block_analyzer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/cockroachdb/crlib/crtime"
1212
"github.com/cockroachdb/pebble/internal/compression"
13+
"github.com/cockroachdb/pebble/sstable/block"
1314
)
1415

1516
// BlockAnalyzer is used to evaluate the performance and compressibility of different
@@ -48,7 +49,7 @@ func (a *BlockAnalyzer) Close() {
4849

4950
// Block analyzes a block by measuring its compressibility and the performance
5051
// of various compression algorithms on it.
51-
func (a *BlockAnalyzer) Block(kind BlockKind, block []byte) {
52+
func (a *BlockAnalyzer) Block(kind block.Kind, block []byte) {
5253
size := MakeBlockSize(len(block))
5354
compressibility := MakeCompressibility(len(block), len(a.minLZFastest.Compress(a.buf1[:0], block)))
5455
bucket := &a.b[kind][size][compressibility]

sstable/compressionanalyzer/buckets.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,9 @@ import (
1212
"time"
1313

1414
"github.com/cockroachdb/pebble/internal/compression"
15+
"github.com/cockroachdb/pebble/sstable/block/blockkind"
1516
)
1617

17-
// BlockKind breaks down the types of blocks into categories which might benefit
18-
// from individual compressions settings.
19-
type BlockKind uint8
20-
21-
const (
22-
DataBlock BlockKind = iota
23-
SSTableValueBlock
24-
BlobValueBlock
25-
IndexBlock
26-
// OtherBlock includes range del/key blocks, and other top-level metadata
27-
// blocks.
28-
OtherBlock
29-
numBlockKinds
30-
)
31-
32-
var blockKindString = [...]string{
33-
DataBlock: "data",
34-
SSTableValueBlock: "sstval",
35-
BlobValueBlock: "blobval",
36-
IndexBlock: "index",
37-
OtherBlock: "other",
38-
}
39-
40-
func (k BlockKind) String() string {
41-
return blockKindString[k]
42-
}
43-
4418
// BlockSize identifies a range of block sizes.
4519
type BlockSize uint8
4620

@@ -134,7 +108,7 @@ var Settings = [...]compression.Setting{
134108
const numSettings = 7
135109

136110
// Buckets holds the results of all experiments.
137-
type Buckets [numBlockKinds][numBlockSizes][numCompressibility]Bucket
111+
type Buckets [blockkind.NumKinds][numBlockSizes][numCompressibility]Bucket
138112

139113
// Bucket aggregates results for blocks of the same kind, size range, and
140114
// compressibility.
@@ -161,7 +135,7 @@ func (b *Buckets) String(minSamples int) string {
161135
fmt.Fprintf(tw, "\t%s", s.String())
162136
}
163137
fmt.Fprintf(tw, "\n")
164-
for k := BlockKind(0); k < numBlockKinds; k++ {
138+
for k := range blockkind.All() {
165139
for sz := BlockSize(0); sz < numBlockSizes; sz++ {
166140
for c := Compressibility(0); c < numCompressibility; c++ {
167141
bucket := &b[k][sz][c]
@@ -223,7 +197,7 @@ func (b *Buckets) ToCSV(minSamples int) string {
223197
fmt.Fprintf(&buf, ",%s Decomp±", s.String())
224198
}
225199
fmt.Fprintf(&buf, "\n")
226-
for k := BlockKind(0); k < numBlockKinds; k++ {
200+
for k := range blockkind.All() {
227201
for sz := BlockSize(0); sz < numBlockSizes; sz++ {
228202
for c := Compressibility(0); c < numCompressibility; c++ {
229203
bucket := &b[k][sz][c]

0 commit comments

Comments
 (0)