Skip to content

feat: harden snapshot read transactions — refuse writes, expose lifetime, conformance-test the ReadOptions funnel - #833

Merged
QuantumExplorer merged 5 commits into
developfrom
claude/grovedb-issue-832-e6cb57
Aug 26, 2026
Merged

QuantumExplorer merged 5 commits into
developfrom
claude/grovedb-issue-832-e6cb57

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Aug 26, 2026 •

Copy link
Copy Markdown
Member

Closes #832. Follow-up hardening for #831 (start_snapshot_read_transaction). None of these were holes in what shipped — both existing callers are read-only and millisecond-scoped — but the primitive's API surface left three traps for future callers. All three are closed by one mechanism.

The mechanism: the transaction type is now a recognizable wrapper

Storage::Transaction for the RocksDB backend (and therefore grovedb's Transaction alias) is now a wrapper struct (storage::rocksdb_storage::Tx) around the raw RocksDB transaction, carrying the snapshot-read marker set at creation. The raw un-optioned accessors are private to the storage module, so the wrapper is the single funnel for everything done through a transaction. This is the "recognizable wrapper" option from the issue — no registry, no keying problem, no lookup on the hot path.

1. Read-only enforced instead of documented

Every write entry point refuses a snapshot read transaction with a new typed error, Error::SnapshotReadOnlyTransaction(&'static str) (payload names the refused operation):

  • the immediate context's put/put_aux/put_root/put_meta/delete* family (refused immediately),
  • the batch apply (rebuild_from_writebatch, covering commit_multi_context_batch and the immediate context's commit_batch),
  • commit / commit_transaction.

Previously such writes would fail nondeterministically with Busy/TryAgain at commit time — or worse, commit — because set_snapshot arms commit-time conflict detection. Now the failure is deterministic and typed. rollback stays allowed: it is a harmless no-op there and refusing it would only complicate callers' cleanup paths. Refusals don't poison the transaction — it still serves pinned reads afterwards (pinned by test).

2. Snapshot lifetime observable; loud debug warning on long holds

The read-only marker doubles as a creation timestamp: is_snapshot_read() and snapshot_age() are public on the transaction. Debug builds additionally log a loud warning — once per transaction — when a read executes on a snapshot held longer than 1s (three orders of magnitude above the intended millisecond-scoped holds), naming the compaction-debt failure mode. The failure mode is a loud log, not a slow node; release builds carry no check on the read path.

3. Silent-regression channel on the read funnel closed

Both prefixed transaction contexts now read exclusively through the wrapper's get/get_cf/raw_iterator, each of which injects the transaction's snapshot into that read's ReadOptions (the per-context read_options() helpers are gone). A read method added later cannot reach the raw un-optioned accessors from outside the storage module, so it cannot silently revert to latest-committed reads.

A conformance test walks every read method of the storage-context trait — get, get_aux, get_root, get_meta, and every raw-iterator navigation variant (seek_to_first/forward, seek_to_last/backward, seek, seek_for_prev) — on both context types under a snapshot transaction with a commit landed after creation (overwrite + insert + delete in every column family), asserting pre-commit visibility per method, with a plain-transaction control asserting post-commit visibility. A forgotten new read method surfaces as an obvious test-extension failure.

Compatibility

  • Plain transactions are byte-for-byte unaffected: their snapshot handle is null, which RocksDB documents as reading the latest committed state; start_transaction behavior is unchanged.
  • The wrapper keeps commit()/rollback() inherent methods, so existing direct call sites compile unchanged (commit() now returns the storage Error instead of rocksdb::Error).
  • The feat: snapshot-pinned read transactions #831 pin tests (branched axis read, recursive proof generation) pass unchanged.

Non-goal (per the issue)

The platform-side consistency asymmetry (branched IN reads snapshot-pinned; other unproved reads latest-committed per operation) is deliberate and remains documented, not extended.

Testing

  • cargo nextest run --workspace --all-features: 5090 passed.
  • cargo clippy --workspace --all-features -- -D warnings: clean.
  • cargo check --workspace --all-features --all-targets and cargo build --no-default-features --features verify -p grovedb: clean.
  • New: 4 storage-level tests (two per-context conformance walks, write-refusal walk, lifetime introspection) + 2 grovedb-level tests (typed refusal through insert/delete/commit_transaction with reads surviving refusals; lifetime observability).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added snapshot read transactions that provide a consistent, pinned view of data.
    • Snapshot transactions now report their age and remain read-only.
    • Rollback is supported for snapshot transactions.
  • Bug Fixes

    • Write, delete, batch, and commit attempts on snapshot transactions now return a clear read-only error.
    • Confirmed rejected writes do not modify committed data.
  • Documentation

    • Expanded guidance on snapshot lifetime, age monitoring, compaction impact, and long-held snapshots.

…ime, conformance-test the ReadOptions funnel

Closes #832. Follow-up hardening for #831, whose primitive left three
traps open for future callers.

The transaction type returned by the storage layer is now a wrapper
(storage Tx) around the raw RocksDB transaction, carrying the
snapshot-read marker set by start_snapshot_read_transaction. The raw
un-optioned accessors are private to the storage module, making the
wrapper the single funnel for everything done through a transaction:

1. Read-only is enforced, not documented. Every write entry point —
   the immediate context's put/delete family, the batch apply
   (rebuild_from_writebatch), and commit — refuses a snapshot read
   transaction with a typed SnapshotReadOnlyTransaction error instead
   of leaving writes to fail nondeterministically with Busy at commit
   (set_snapshot arms commit-time conflict detection). Rollback stays
   allowed as the harmless cleanup path.

2. Snapshot lifetime is observable. The marker doubles as a creation
   timestamp: is_snapshot_read() and snapshot_age() are exposed on the
   transaction (and thus on grovedb's Transaction alias), and debug
   builds log loudly — once per transaction — when a read executes on
   a snapshot held longer than one second, three orders of magnitude
   above the intended millisecond-scoped holds.

3. The silent-regression channel on the read funnel is closed. Both
   prefixed transaction contexts now read exclusively through the
   wrapper's get/get_cf/raw_iterator, which inject the transaction's
   snapshot into every read's options; a read method added later
   cannot reach the raw un-optioned accessors from outside the storage
   module. A conformance test walks every read method of the
   storage-context trait (get/get_aux/get_root/get_meta and every
   raw-iterator navigation variant) on both context types under a
   snapshot transaction with a commit landed after creation, asserting
   pre-commit visibility per method — a forgotten new method surfaces
   as an obvious test-extension failure.

Plain transactions are byte-for-byte unaffected: their snapshot handle
is null, which RocksDB documents as reading latest-committed state.

Co-Authored-By: Claude Fable 5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

Next included review available in 50 minutes.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: eb60ee80-13bc-48d6-8c84-213b644202a3

📥 Commits

Reviewing files that changed from the base of the PR and between fab5862 and 4bcf5b3.

📒 Files selected for processing (13)
  • grovedb/src/batch/mod.rs
  • grovedb/src/operations/indexed_tree.rs
  • grovedb/src/operations/proof/indexed_axis/envelope.rs
  • grovedb/src/operations/proof/indexed_axis/generate.rs
  • grovedb/src/operations/proof/mod.rs
  • grovedb/src/operations/proof/verify.rs
  • merk/src/proofs/query/aggregate_count/emit.rs
  • merk/src/proofs/query/aggregate_count_and_sum/emit.rs
  • merk/src/proofs/query/aggregate_sum/emit.rs
  • merk/src/proofs/query/count_offset/verify.rs
  • merk/src/tree_type/mod.rs
  • storage/src/rocksdb_storage/storage.rs
  • storage/src/rocksdb_storage/tests.rs

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 14f84b89-7921-43e0-9d5b-6602ed969c64

📥 Commits

Reviewing files that changed from the base of the PR and between f7e9d1b and fab5862.

📒 Files selected for processing (11)
  • grovedb/src/lib.rs
  • grovedb/src/tests/mod.rs
  • grovedb/src/tests/snapshot_read_transaction_tests.rs
  • grovedb/src/util.rs
  • storage/src/error.rs
  • storage/src/rocksdb_storage.rs
  • storage/src/rocksdb_storage/storage.rs
  • storage/src/rocksdb_storage/storage_context/context_immediate.rs
  • storage/src/rocksdb_storage/storage_context/context_tx.rs
  • storage/src/rocksdb_storage/storage_context/raw_iterator.rs
  • storage/src/rocksdb_storage/tests.rs

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The storage layer now wraps RocksDB transactions to enforce snapshot read-only behavior, pin reads and iterators, track snapshot age, and report typed errors. Grovedb adds documentation and tests for rejected mutations, rollback, pinned reads, and snapshot metadata.

Changes

Snapshot transactions

Layer / File(s) Summary
Transaction wrapper and error contract
storage/src/error.rs, storage/src/rocksdb_storage.rs, storage/src/rocksdb_storage/storage.rs, grovedb/src/util.rs
Tx wraps RocksDB transactions, rejects snapshot writes and commits with SnapshotReadOnlyTransaction, tracks snapshot age, and preserves plain transaction behavior.
Snapshot-aware storage contexts
storage/src/rocksdb_storage/storage_context/*
Transactional and immediate contexts use wrapper reads and iterators. Raw iterator implementations use RawTx.
Behavior validation and API documentation
storage/src/rocksdb_storage/tests.rs, grovedb/src/tests/snapshot_read_transaction_tests.rs, grovedb/src/tests/mod.rs, grovedb/src/lib.rs
Tests cover pinned reads, rejected mutations, rollback, snapshot classification, and non-decreasing age. Documentation describes read-only enforcement and snapshot lifetime.cond

Estimated code review effort: 4 (Complex) | ~45 minutes

Merge Risk: ⚪ Minimal · up to fab58

The change hardens snapshot-read transaction behavior without any identified merge-blocking risk; it is merge-ready after normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant Storage as RocksDbStorage
  participant Context as StorageContext
  participant Tx as Tx
  participant RocksDB
  Storage->>Tx: create snapshot-read transaction
  Storage->>Context: execute reads and iterators
  Context->>Tx: request snapshot-aware reads
  Tx->>RocksDB: read pinned snapshot
  Storage->>Tx: attempt write or commit
  Tx-->>Storage: SnapshotReadOnlyTransaction
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 54.69% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 64 functions across 11 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main changes: snapshot read transactions reject writes, expose lifetime information, and receive conformance tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/grovedb-issue-832-e6cb57

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.53%. Comparing base (f7e9d1b) to head (4bcf5b3).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #833      +/-   ##
===========================================
+ Coverage    92.48%   92.53%   +0.04%     
===========================================
  Files          292      292              
  Lines        90424    90484      +60     
===========================================
+ Hits         83628    83727      +99     
+ Misses        6796     6757      -39     
Components Coverage Δ
grovedb-core 90.67% <100.00%> (-0.01%) ⬇️
merk 93.27% <ø> (ø)
storage 91.95% <100.00%> (+2.70%) ⬆️
commitment-tree 96.38% <ø> (ø)
mmr 95.12% <ø> (ø)
bulk-append-tree 92.75% <ø> (ø)
element 97.98% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

QuantumExplorer and others added 4 commits August 26, 2026 13:18
The wrapper hid every raw-transaction method it did not re-export,
and set_savepoint / rollback_to_savepoint were not re-exported because
nothing in-tree calls them — but platform's block processing does
(per-state-transition savepoints in prepare/process proposal and
process_raw_state_transitions), so the wrapper as first pushed would
have broken the platform build with method-not-found.

Pass them through with the same policy as rollback: allowed on a
snapshot read transaction, since the savepoint family only ever
unwinds writes, which such a transaction cannot accumulate.
rollback_to_savepoint returns the storage Error like the rest of the
wrapper surface; platform's three hand-wrapped RocksDBError sites
become one-line compiler-caught fixes.

Co-Authored-By: Claude Fable 5 <[email protected]>
@QuantumExplorer

Copy link
Copy Markdown
Member Author

Reviewed

@QuantumExplorer
QuantumExplorer merged commit 0b85d7e into develop Aug 26, 2026
10 checks passed
@QuantumExplorer
QuantumExplorer deleted the claude/grovedb-issue-832-e6cb57 branch August 26, 2026 12:43
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.

Harden snapshot read transactions: refuse writes, expose lifetime, conformance-test the ReadOptions funnel

1 participant