Skip to content

Commit d8bd7ff

Browse files
committed
colblk: fix 386 build
When we cast the block metadata, we incorrectly calculate the key seeker metadata offset without accounting for the possible padding. A recent change (#5093) caused padding to take effect. We fix this and make the code simpler and more robust.
1 parent bb25243 commit d8bd7ff

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

sstable/colblk/colblk_64bit_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
package colblk
88

9-
import "github.com/cockroachdb/pebble/sstable/block"
9+
import (
10+
"unsafe"
11+
12+
"github.com/cockroachdb/pebble/sstable/block"
13+
)
1014

1115
// Assert that block.MetadataSize is not larger than it needs to be on a 64-bit
1216
// platform.
13-
const _ uint = uint(dataBlockDecoderSize) + KeySeekerMetadataSize - block.MetadataSize
17+
const _ uint = uint(unsafe.Sizeof(blockDecoderAndKeySeekerMetadata{})) - block.MetadataSize

sstable/colblk/data_block.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -790,24 +790,21 @@ func (rw *DataBlockRewriter) RewriteSuffixes(
790790
return start, end, rewritten, nil
791791
}
792792

793-
// dataBlockDecoderSize is the size of DataBlockDecoder, round up to 8 bytes.
794-
const dataBlockDecoderSize = (unsafe.Sizeof(DataBlockDecoder{}) + 7) &^ 7
793+
type blockDecoderAndKeySeekerMetadata struct {
794+
d DataBlockDecoder
795+
// Pad to ensure KeySeekerMetadata is 8-byte aligned.
796+
_ [(8 - unsafe.Sizeof(DataBlockDecoder{})%8) % 8]byte
797+
keySchemaMeta KeySeekerMetadata
798+
}
795799

796-
// Assert that dataBlockDecoderSize is a multiple of 8 bytes (so that
797-
// KeySeekerMetadata is also aligned).
798-
const _ uint = uint(-(dataBlockDecoderSize % 8))
800+
// Assert that keySchemaMeta is aligned to 8 bytes.
801+
const _ uint = uint(-(unsafe.Offsetof(blockDecoderAndKeySeekerMetadata{}.keySchemaMeta) % 8))
799802

800-
// Assert that a DataBlockDecoder and a KeySeekerMetadata can fit inside block.Metadata.
801-
const _ uint = block.MetadataSize - uint(dataBlockDecoderSize) - KeySeekerMetadataSize
803+
// Assert that blockDecoderAndKeySeekerMetadata fit inside block.Metadata.
804+
const _ uint = block.MetadataSize - uint(unsafe.Sizeof(blockDecoderAndKeySeekerMetadata{}))
802805

803806
// InitDataBlockMetadata initializes the metadata for a data block.
804807
func InitDataBlockMetadata(schema *KeySchema, md *block.Metadata, data []byte) (err error) {
805-
type blockDecoderAndKeySeekerMetadata struct {
806-
d DataBlockDecoder
807-
// Pad to ensure KeySeekerMetadata is 8-byte aligned.
808-
_ [dataBlockDecoderSize - unsafe.Sizeof(DataBlockDecoder{})]byte
809-
keySchemaMeta KeySeekerMetadata
810-
}
811808
metadatas := block.CastMetadataZero[blockDecoderAndKeySeekerMetadata](md)
812809
// Initialization can panic; convert panics to corruption errors (so higher
813810
// layers can add file number and offset information).
@@ -1095,9 +1092,9 @@ func (i *DataBlockIter) InitHandle(
10951092
) error {
10961093
i.suffixCmp = comparer.ComparePointSuffixes
10971094
i.split = comparer.Split
1098-
blockMeta := h.BlockMetadata()
1099-
i.d = (*DataBlockDecoder)(unsafe.Pointer(blockMeta))
1100-
keySeekerMeta := (*KeySeekerMetadata)(blockMeta[unsafe.Sizeof(DataBlockDecoder{}):])
1095+
blockMeta := (*blockDecoderAndKeySeekerMetadata)(unsafe.Pointer(h.BlockMetadata()))
1096+
i.d = &blockMeta.d
1097+
keySeekerMeta := &blockMeta.keySchemaMeta
11011098
i.h.Release()
11021099
i.h = h
11031100

0 commit comments

Comments
 (0)