Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colblk: cache KeyspanDecoder in block metadata #4082

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions sstable/colblk/data_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,6 @@ const _ uint = uint(-(dataBlockDecoderSize % 8))
// Assert that a DataBlockDecoder and a KeySeekerMetadata can fit inside block.Metadata.
const _ uint = block.MetadataSize - uint(dataBlockDecoderSize) - KeySeekerMetadataSize

// Assert that an IndexBlockDecoder can fit inside block.Metadata.
const _ uint = block.MetadataSize - uint(unsafe.Sizeof(IndexBlockDecoder{}))

// InitDataBlockMetadata initializes the metadata for a data block.
func InitDataBlockMetadata(schema KeySchema, md *block.Metadata, data []byte) (err error) {
if uintptr(unsafe.Pointer(md))%8 != 0 {
Expand All @@ -785,6 +782,9 @@ func InitDataBlockMetadata(schema KeySchema, md *block.Metadata, data []byte) (e
return nil
}

// Assert that an IndexBlockDecoder can fit inside block.Metadata.
const _ uint = block.MetadataSize - uint(unsafe.Sizeof(IndexBlockDecoder{}))

// InitIndexBlockMetadata initializes the metadata for an index block.
func InitIndexBlockMetadata(md *block.Metadata, data []byte) (err error) {
if uintptr(unsafe.Pointer(md))%8 != 0 {
Expand All @@ -795,7 +795,27 @@ func InitIndexBlockMetadata(md *block.Metadata, data []byte) (err error) {
// layers can add file number and offset information).
defer func() {
if r := recover(); r != nil {
err = base.CorruptionErrorf("error initializing data block metadata: %v", r)
err = base.CorruptionErrorf("error initializing index block metadata: %v", r)
}
}()
d.Init(data)
return nil
}

// Assert that a IndexBlockDecoder can fit inside block.Metadata.
const _ uint = block.MetadataSize - uint(unsafe.Sizeof(KeyspanDecoder{}))

// InitKeyspanBlockMetadata initializes the metadata for a rangedel or range key block.
func InitKeyspanBlockMetadata(md *block.Metadata, data []byte) (err error) {
if uintptr(unsafe.Pointer(md))%8 != 0 {
return errors.AssertionFailedf("metadata is not 8-byte aligned")
}
d := (*KeyspanDecoder)(unsafe.Pointer(md))
// Initialization can panic; convert panics to corruption errors (so higher
// layers can add file number and offset information).
defer func() {
if r := recover(); r != nil {
err = base.CorruptionErrorf("error initializing keyspan block metadata: %v", r)
}
}()
d.Init(data)
Expand Down
13 changes: 4 additions & 9 deletions sstable/colblk/keyspan.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os"
"sync"
"unsafe"

"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/internal/base"
Expand Down Expand Up @@ -327,12 +328,8 @@ func NewKeyspanIter(
i := keyspanIterPool.Get().(*KeyspanIter)
i.closeCheck = invariants.CloseChecker{}
i.handle = h
// TODO(jackson): We can teach the block cache to stash a *KeyspanDecoder.
// Then all iters would use the same decoder rather than needing to allocate
// their own KeyspanDecoder and parse the high-level block structure
// themselves.
i.allocDecoder.Init(h.BlockData())
i.init(cmp, &i.allocDecoder, transforms)
d := (*KeyspanDecoder)(unsafe.Pointer(h.BlockMetadata()))
i.init(cmp, d, transforms)
return i
}

Expand All @@ -353,8 +350,7 @@ var keyspanIterPool = sync.Pool{
// keyspan.FragmentIterator interface.
type KeyspanIter struct {
keyspanIter
handle block.BufferHandle
allocDecoder KeyspanDecoder
handle block.BufferHandle

closeCheck invariants.CloseChecker
}
Expand All @@ -372,7 +368,6 @@ func (i *KeyspanIter) Close() {
}

i.keyspanIter.Close()
i.allocDecoder = KeyspanDecoder{}
i.closeCheck.Close()
keyspanIterPool.Put(i)
}
Expand Down
3 changes: 3 additions & 0 deletions sstable/colblk/keyspan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"
"testing"
"time"
"unsafe"

"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/pebble/internal/base"
Expand Down Expand Up @@ -86,6 +87,8 @@ func TestKeyspanBlockPooling(t *testing.T) {
defer c.Unref()
v := block.Alloc(len(b), nil)
copy(v.BlockData(), b)
d := (*KeyspanDecoder)(unsafe.Pointer(v.BlockMetadata()))
d.Init(v.BlockData())
v.MakeHandle(c, cache.ID(1), base.DiskFileNum(1), 0).Release()

getBlockAndIterate := func() {
Expand Down
13 changes: 11 additions & 2 deletions sstable/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,23 @@ func (r *Reader) readRangeDelBlock(
ctx context.Context, env readBlockEnv, readHandle objstorage.ReadHandle, bh block.Handle,
) (block.BufferHandle, error) {
ctx = objiotracing.WithBlockType(ctx, objiotracing.MetadataBlock)
return r.readBlockInternal(ctx, env, readHandle, bh, noInitBlockMetadataFn)
return r.readBlockInternal(ctx, env, readHandle, bh, r.initKeyspanBlockMetadata)
}

func (r *Reader) readRangeKeyBlock(
ctx context.Context, env readBlockEnv, readHandle objstorage.ReadHandle, bh block.Handle,
) (block.BufferHandle, error) {
ctx = objiotracing.WithBlockType(ctx, objiotracing.MetadataBlock)
return r.readBlockInternal(ctx, env, readHandle, bh, noInitBlockMetadataFn)
return r.readBlockInternal(ctx, env, readHandle, bh, r.initKeyspanBlockMetadata)
}

// initKeyspanBlockMetadata initializes the Metadata for a rangedel or range key
// block. This will later be used (and reused) when reading from the block.
func (r *Reader) initKeyspanBlockMetadata(metadata *block.Metadata, data []byte) error {
if r.tableFormat.BlockColumnar() {
return colblk.InitKeyspanBlockMetadata(metadata, data)
}
return nil
}

func (r *Reader) readValueBlock(
Expand Down
Loading