Skip to content

fix(chunk_engine): copy-on-write shrinking truncate to protect pinned concurrent readers - #461

Merged
SF-Zhou merged 1 commit into
deepseek-ai:mainfrom
ApacheWang:fix/chunk-engine-truncate-cow
Nov 27, 2026
Merged

SF-Zhou merged 1 commit into
deepseek-ai:mainfrom
ApacheWang:fix/chunk-engine-truncate-cow

Conversation

@ApacheWang

Copy link
Copy Markdown
Contributor

Summary

Route shrinking truncate operations (req.is_truncate && req.offset < old_chunk.meta().len) through Chunk::copy_on_write instead of mutating the existing chunk in place.

Root Cause

Previously, Chunk::safe_write handled shrinking truncates in place by mutating self.meta.len and self.meta.checksum on the existing chunk slot. If a concurrent reader still holds a pinned ChunkArc referencing that slot (or before commit completes), mutating the backing position/metadata breaks copy-on-write snapshot isolation for pinned readers.

Changes

  • src/storage/chunk_engine/src/alloc/chunk.rs:
    • Extend Chunk::copy_on_write with a truncate: bool flag so shrinking truncates allocate a new chunk of size offset, copy the retained [0..offset] prefix, zero-fill alignment padding, recompute the CRC32C checksum, and keep incrementing metrics.safe_write_truncate_shorten.
    • Reject shrinking truncates in Chunk::safe_write with Error::InvalidArg("shrinking truncate requires copy-on-write").
  • src/storage/chunk_engine/src/core/engine.rs:
    • Force copy_on_write in Engine::update_chunk when req.is_truncate && req.offset < old_chunk.meta().len, while preserving the existing in-place path for extending truncates.
    • Add regression tests test_engine_truncate_shrink_preserves_reader and test_engine_truncate_extend_in_place.

Verification

Rebased on main (cdded47) and ran the full chunk_engine unit test suite:

cd src/storage/chunk_engine && cargo test -- --test-threads=1
# test result: ok. 49 passed; 0 failed; 0 ignored

AI-assisted development disclosure: AI coding tools assisted with root-cause analysis, patch drafting, and regression test authoring; all changes and tests were reviewed and verified locally.

… concurrent readers

Signed-off-by: bc15575202581 <bc15575202581@gmail.com>

@SF-Zhou SF-Zhou left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants