Skip to content

Commit

Permalink
sstable: fix row writer mangling of cache block
Browse files Browse the repository at this point in the history
Fix `RawRowWriter.addDataBlock` to make a copy of the block data when
it's not compressed (similar to the columnar writer), because in that
case the writing out can mangle the buffer.

The sstable copier (during Download) uses `addDataBlock` with a buffer
from the cache, which we are not allowed to modify.

Informs cockroachdb/cockroach#138662
  • Loading branch information
RaduBerinde committed Jan 15, 2025
1 parent 7ca3344 commit ac677f8
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion sstable/rowblk_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1942,8 +1942,21 @@ func (w *RawRowWriter) copyDataBlocks(

// addDataBlock implements RawWriter.
func (w *RawRowWriter) addDataBlock(b, sep []byte, bhp block.HandleWithProperties) error {
blockBuf := &w.dataBlockBuf.blockBuf
pb := block.CompressAndChecksum(
&blockBuf.compressedBuf,
b,
w.layout.compression,
&blockBuf.checksummer,
)
if !pb.IsCompressed() {
// If the block isn't compressed, pb's underlying data points
// directly b. Clone it before writing it, as writing can mangle the buffer.
pb = pb.Clone()
}

// layout.WriteDataBlock keeps layout.offset up-to-date for us.
bh, err := w.layout.WriteDataBlock(b, &w.dataBlockBuf.blockBuf)
bh, err := w.layout.writePrecompressedBlock(pb)
if err != nil {
return err
}
Expand Down

0 comments on commit ac677f8

Please sign in to comment.