refactor(cold): require &mut self for ColdStorage write methods#42
refactor(cold): require &mut self for ColdStorage write methods#42
Conversation
…1979) Change append_block, append_blocks, truncate_above, and drain_above from &self to &mut self so the borrow checker enforces exclusive write access at compile time. Add Clone as a supertrait bound. Adapt the task runner by wrapping the backend in tokio::sync::RwLock<B>. Reads acquire the read lock, writes acquire the write lock, and stream producers clone the backend to avoid holding the lock during long-lived streams. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Should impl ColdStorage for EitherCold also include an override for fn drain_above?
| /// that write. | ||
| /// | ||
| pub trait ColdStorage: Send + Sync + 'static { | ||
| pub trait ColdStorage: Clone + Send + Sync + 'static { |
There was a problem hiding this comment.
I think it might be helpful to split the single trait into ColdStorageRead: Clone + Send + Sync + 'static and ColdStorageWrite: Send + 'static.
We can then avoid the task runner having to hold the backend in a RwLock: it would hold a single, non-cloneable instance of the backend as the write portion, and would have a cloneable instance of the backend as the read portion.
This would help enforce the architectural intent: the task runner is the only one that's capable of calling write methods, and the read tasks and stream producers receive a clone of the ColdStorageRead, which ensures they can't call any write methods (we'd only have impl<B: ColdStorageRead> ColdStorageTaskInner<B> { ... } for the read-only tasks.)
Summary
ColdStoragewrite methods (append_block,append_blocks,truncate_above,drain_above) from&selfto&mut selfso the borrow checker enforces exclusive write access at compile timeCloneas a supertrait bound onColdStorage(all backends are cheaply clonable viaArc-wrapped internals)tokio::sync::RwLock<B>— reads acquire the read lock, writes acquire the write lockCloses ENG-1979
Test plan
cargo clippyclean on all 4 affected crates with both--all-featuresand--no-default-featuresmem_backend_conformancepassesmdbx_backend_conformancepassesdrain_above_*)./scripts/test-postgres.sh(requires Postgres)🤖 Generated with Claude Code