lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

commit 418acecb49437ce1200a6916822af9ef83eefec3
parent 8b3afc754910af5f77a8ff8fa0f942cc692ec59b
Author: triesap <tyson@radroots.org>
Date:   Sun, 11 Oct 2026 06:42:30 +0000

storage: collect obsolete projection cache without losing custody

- Discover retired generations through bounded provider indexes.
- Retire obsolete snapshots while preserving legacy custody documents.
- Keep original quotas, transaction fences and migration history.
- Verify provider parity, rollback, reopen, coverage and additive APIs.

Diffstat:
Mcontracts/api_baselines/radroots_storage.txt | 10++++++++++
Mcontracts/api_baselines/radroots_storage_sqlite.txt | 2++
Acontracts/architecture/decisions/projection_cache_retirement.v3.json | 46++++++++++++++++++++++++++++++++++++++++++++++
Mcontracts/coverage-profiles.toml | 3+++
Mcrates/storage/src/memory.rs | 33+++++++++++++++++++++++++++++++++
Mcrates/storage/src/memory_projection_batch.rs | 6++++++
Mcrates/storage/src/memory_projection_cache.rs | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
Acrates/storage/src/memory_projection_cache_retired.rs | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage/src/projection.rs | 20++++++++++++++++++++
Mcrates/storage_sqlite/src/migration.rs | 7++++---
Acrates/storage_sqlite/src/migration/runtime/0023_projection_retired_selection.up.sql | 3+++
Mcrates/storage_sqlite/src/migration/runtime/mod.rs | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mcrates/storage_sqlite/src/projection/cache.rs | 122++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Acrates/storage_sqlite/src/projection/cache_retired.rs | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/projection/cache_retired_owner_tests.rs | 614+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_sqlite/src/projection/mod.rs | 21+++++++++++++++++++++
16 files changed, 1207 insertions(+), 48 deletions(-)

diff --git a/contracts/api_baselines/radroots_storage.txt b/contracts/api_baselines/radroots_storage.txt @@ -1081,7 +1081,9 @@ pub fn radroots_storage::memory::MemoryStorage::status(&self) -> radroots_transp pub fn radroots_storage::memory::MemoryStorage::tombstone(&self, radroots_storage::private_artifact::PrivateArtifactId, radroots_storage::private_artifact::PrivateArtifactRevision, u64, radroots_storage::private_artifact::DeletionReason) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::private_artifact::PrivateArtifactMetadata, radroots_storage::Error>> impl radroots_storage::projection::ProjectionStore for radroots_storage::memory::MemoryStorage pub fn radroots_storage::memory::MemoryStorage::checkpoint(&self, radroots_storage::projection::ProjectionCheckpoint) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::ProjectionStatus, radroots_storage::Error>> +pub fn radroots_storage::memory::MemoryStorage::collect_obsolete_projection_snapshots(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::collect_projection_cache(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> +pub fn radroots_storage::memory::MemoryStorage::collect_retired_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::event::EventSourceToken, core::option::Option<radroots_storage::projection::ProjectionGeneration>) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::configure_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::projection::cache::ProjectionCacheLimits) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheUsage, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::event_index_checkpoint(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexCheckpoint>, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::event_index_manifest(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexManifest>, radroots_storage::Error>> @@ -1705,7 +1707,9 @@ pub const radroots_storage::projection::PROJECTION_DOCUMENT_VALUE_MAX_BYTES: usi pub const radroots_storage::projection::PROJECTION_ID_MAX_BYTES: usize pub trait radroots_storage::projection::ProjectionStore: core::marker::Send + core::marker::Sync pub fn radroots_storage::projection::ProjectionStore::checkpoint(&self, radroots_storage::projection::ProjectionCheckpoint) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::ProjectionStatus, radroots_storage::Error>> +pub fn radroots_storage::projection::ProjectionStore::collect_obsolete_projection_snapshots(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::projection::ProjectionStore::collect_projection_cache(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> +pub fn radroots_storage::projection::ProjectionStore::collect_retired_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::event::EventSourceToken, core::option::Option<radroots_storage::projection::ProjectionGeneration>) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::projection::ProjectionStore::configure_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::projection::cache::ProjectionCacheLimits) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheUsage, radroots_storage::Error>> pub fn radroots_storage::projection::ProjectionStore::event_index_checkpoint(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexCheckpoint>, radroots_storage::Error>> pub fn radroots_storage::projection::ProjectionStore::event_index_manifest(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexManifest>, radroots_storage::Error>> @@ -1728,7 +1732,9 @@ pub fn radroots_storage::projection::ProjectionStore::status(&self, radroots_sto pub fn radroots_storage::projection::ProjectionStore::transition_rebuild(&self, radroots_storage::projection::RebuildTransition) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::RebuildTicket, radroots_storage::Error>> impl radroots_storage::projection::ProjectionStore for radroots_storage::memory::MemoryStorage pub fn radroots_storage::memory::MemoryStorage::checkpoint(&self, radroots_storage::projection::ProjectionCheckpoint) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::ProjectionStatus, radroots_storage::Error>> +pub fn radroots_storage::memory::MemoryStorage::collect_obsolete_projection_snapshots(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::collect_projection_cache(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> +pub fn radroots_storage::memory::MemoryStorage::collect_retired_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::event::EventSourceToken, core::option::Option<radroots_storage::projection::ProjectionGeneration>) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::configure_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::projection::cache::ProjectionCacheLimits) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheUsage, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::event_index_checkpoint(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexCheckpoint>, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::event_index_manifest(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexManifest>, radroots_storage::Error>> @@ -2038,7 +2044,9 @@ pub fn radroots_storage::memory::MemoryStorage::release(&self, radroots_storage: pub fn radroots_storage::memory::MemoryStorage::status(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::outbox::OutboxStatus, radroots_storage::Error>> pub trait radroots_storage::ProjectionStore: core::marker::Send + core::marker::Sync pub fn radroots_storage::ProjectionStore::checkpoint(&self, radroots_storage::projection::ProjectionCheckpoint) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::ProjectionStatus, radroots_storage::Error>> +pub fn radroots_storage::ProjectionStore::collect_obsolete_projection_snapshots(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::ProjectionStore::collect_projection_cache(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> +pub fn radroots_storage::ProjectionStore::collect_retired_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::event::EventSourceToken, core::option::Option<radroots_storage::projection::ProjectionGeneration>) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::ProjectionStore::configure_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::projection::cache::ProjectionCacheLimits) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheUsage, radroots_storage::Error>> pub fn radroots_storage::ProjectionStore::event_index_checkpoint(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexCheckpoint>, radroots_storage::Error>> pub fn radroots_storage::ProjectionStore::event_index_manifest(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexManifest>, radroots_storage::Error>> @@ -2061,7 +2069,9 @@ pub fn radroots_storage::ProjectionStore::status(&self, radroots_storage::projec pub fn radroots_storage::ProjectionStore::transition_rebuild(&self, radroots_storage::projection::RebuildTransition) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::RebuildTicket, radroots_storage::Error>> impl radroots_storage::projection::ProjectionStore for radroots_storage::memory::MemoryStorage pub fn radroots_storage::memory::MemoryStorage::checkpoint(&self, radroots_storage::projection::ProjectionCheckpoint) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::ProjectionStatus, radroots_storage::Error>> +pub fn radroots_storage::memory::MemoryStorage::collect_obsolete_projection_snapshots(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::collect_projection_cache(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> +pub fn radroots_storage::memory::MemoryStorage::collect_retired_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::event::EventSourceToken, core::option::Option<radroots_storage::projection::ProjectionGeneration>) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::configure_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::projection::cache::ProjectionCacheLimits) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheUsage, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::event_index_checkpoint(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexCheckpoint>, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::event_index_manifest(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexManifest>, radroots_storage::Error>> diff --git a/contracts/api_baselines/radroots_storage_sqlite.txt b/contracts/api_baselines/radroots_storage_sqlite.txt @@ -613,7 +613,9 @@ pub fn radroots_storage_sqlite::SqliteStorage::status(&self) -> radroots_transpo pub fn radroots_storage_sqlite::SqliteStorage::tombstone(&self, radroots_storage::private_artifact::PrivateArtifactId, radroots_storage::private_artifact::PrivateArtifactRevision, u64, radroots_storage::private_artifact::DeletionReason) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::private_artifact::PrivateArtifactMetadata, radroots_storage::error::Error>> impl radroots_storage::projection::ProjectionStore for radroots_storage_sqlite::SqliteStorage pub fn radroots_storage_sqlite::SqliteStorage::checkpoint(&self, radroots_storage::projection::ProjectionCheckpoint) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::ProjectionStatus, radroots_storage::error::Error>> +pub fn radroots_storage_sqlite::SqliteStorage::collect_obsolete_projection_snapshots(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::error::Error>> pub fn radroots_storage_sqlite::SqliteStorage::collect_projection_cache(&self, radroots_storage::projection::cache::ProjectionCacheScope) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::error::Error>> +pub fn radroots_storage_sqlite::SqliteStorage::collect_retired_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::event::EventSourceToken, core::option::Option<radroots_storage::projection::ProjectionGeneration>) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheCollection, radroots_storage::error::Error>> pub fn radroots_storage_sqlite::SqliteStorage::configure_projection_cache(&self, radroots_storage::projection::ProjectionId, radroots_storage::projection::cache::ProjectionCacheLimits) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::projection::cache::ProjectionCacheUsage, radroots_storage::error::Error>> pub fn radroots_storage_sqlite::SqliteStorage::event_index_checkpoint(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexCheckpoint>, radroots_storage::error::Error>> pub fn radroots_storage_sqlite::SqliteStorage::event_index_manifest(&self, radroots_storage::projection::ProjectionGeneration) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::projection::EventIndexManifest>, radroots_storage::error::Error>> diff --git a/contracts/architecture/decisions/projection_cache_retirement.v3.json b/contracts/architecture/decisions/projection_cache_retirement.v3.json @@ -0,0 +1,46 @@ +{ + "schema": "radroots.projection-cache-retirement.v3", + "status": "implemented_candidate", + "owners": [ + "radroots_storage", + "radroots_storage_sqlite" + ], + "authority": "Explicit cache identity registration and immutable positive caller-owned limits. Application policy remains outside Lib. Unsupported providers refuse the new methods.", + "accounting": "One store-wide logical value-byte, protected-generation and legacy-snapshot budget includes legacy, active and staging projection rows. Scalar migration initialization is explicit; subsequent declared writers update cached counters under their owner lock or transaction. Growing admission refuses capacity without partial rows or status. Collection may reduce pre-existing excess.", + "retirement": "Exact registered cache owner, canonical source token and expected active generation are compared before effects. A current Ready active generation is protected. Obsolete, staged or explicitly invalidated generations can be retired. Stale active retirement clears its derived status atomically; canonical events, authored work, intents, receipts and media custody are untouched. An exact unconfigured legacy checkpoint may release metadata protection only when explicitly Invalidated, without an active rebuild or live batch registration, under the complete canonical source token and expected-active fence. Its checkpoint source generation must match the canonical store. Preserve Invalidated status and all document/snapshot bytes. Exact repeated retirement is idempotent and grants no cache-owner registration or collection authority.", + "collection": "At most64 total exact-generation document, legacy snapshot and registration rows per pass. Active/staging documents are protected. Unrelated projection identities cannot be collected unless explicitly registered as derived cache by their owner. Continuation reflects remaining eligible work. Metadata selection and deletion occur in one transaction or lock, without payload hydration.", + "legacy_snapshots": "Configured cache identities create no new whole-view snapshot sessions. Existing exact duplicate replay cannot extend fixed expiry. Owner wall time and a durable high-water govern legacy expiry; caller as-of is never a clock input. Configured legacy snapshot reads on a read-only provider refuse continuation. Exact unconfigured lookups validate scalar lengths and fixed digests before hydration.", + "compatibility": "Historical migrations1-21 are immutable. Forward22 owns only derived-cache accounting, registration and indexes. Unconfigured inventory semantics remain supported. Physical database/WAL pages are separate from logical live value-byte quota and are not promised to shrink after collection.", + "non_goals": [ + "leases", + "new per-read snapshots", + "application policy", + "canonical data deletion", + "new database", + "worker", + "vacuum", + "physical-device qualification" + ], + "batch_admission": "One final aggregate policy independent of input order. Up to64 scalar old lengths are preflighted in BEGIN IMMEDIATE. True aggregate growth beyond immutable configured limits refuses before payload effects; non-growing replacement can reduce pre-existing excess. A private final-byte reservation exists only inside that transaction, shrinking rows precede growth, and exact counters/reservation are checked and cleared before commit. Failure, cancellation and crash roll back the transaction. Direct writers retain ordinary per-write guards. Historical migrations1-21 remain unchanged.", + "supersedes_without_mutation": { + "path": "contracts/architecture/decisions/projection_cache_retirement.v2.json", + "sha256": "27d294d6cd326417b6b1b394589ccae7f1e2ef799115595848e8465fdfc83117" + }, + "retired_owner_collection": { + "method": "ProjectionStore::collect_retired_projection_cache", + "inputs": [ + "ProjectionId", + "EventSourceToken", + "Option<ProjectionGeneration> expected active" + ], + "admission": "Exact registered cache owner, complete current canonical source token and expected active generation, even when no retired work exists. Default unsupported and read-only refusal remain BackendUnavailable.", + "selection": "One retired generation in fixed-size key order, excluding active, under the existing transaction or lock. An exact Memory scalar retired-generation index and forward23 SQLite partial index avoid full-generation or payload scans.", + "collection": "Reuse the same exact-generation collection with at most64 total derived rows. more includes eligible work in the selected or another retired generation. Zero work reports zero removals and honest completion. Never widen registration or retire protected state implicitly.", + "compatibility": "All v2 exact-generation, budget, owner-time and custody semantics and historical1-22 SQL/descriptors remain unchanged. Forward23 owns only one partial index. No application schema, policy, worker, lease, vacuum or new database." + }, + "obsolete_snapshot_collection": { + "minimum_additional_method": "ProjectionStore::collect_obsolete_projection_snapshots(ProjectionCacheScope) -> BoxFuture<Result<ProjectionCacheCollection, Error>>", + "admission": "Registered exact cache owner/current canonical source/expected-active fence. Refuse a target reserved by a live batch, and refuse a current active target unless explicitly Invalidated without an active rebuild. Other generations have already ceased to be active. Unsupported/read-only providers refuse.", + "effect": "Remove at most64 exact-generation obsolete snapshot rows under the same transaction or lock, without payload hydration. Never delete documents, generation registration, status, invalidation, source, authored work, intents, receipts or media custody. more refers to remaining snapshot rows in that exact target. This explicit safety retirement does not wait for owner-time expiry; old exact collection/expiry APIs stay unchanged." + } +} diff --git a/contracts/coverage-profiles.toml b/contracts/coverage-profiles.toml @@ -27,3 +27,6 @@ test_threads = 1 no_default_features = false features = ["full"] test_threads = 1 + +[profiles.crates."radroots_storage"] +test_packages = ["radroots_storage_sqlite"] diff --git a/crates/storage/src/memory.rs b/crates/storage/src/memory.rs @@ -72,6 +72,8 @@ use crate::{ mod projection_batch; #[path = "memory_projection_cache.rs"] mod projection_cache; +#[path = "memory_projection_cache_retired.rs"] +mod projection_cache_retired; #[derive(Clone)] struct EventEntry { @@ -928,6 +930,37 @@ impl ProjectionStore for MemoryStorage { ) }) } + fn collect_retired_projection_cache( + &self, + id: ProjectionId, + source: EventSourceToken, + expected_active: Option<ProjectionGeneration>, + ) -> BoxFuture<'_, Result<crate::projection::cache::ProjectionCacheCollection, Error>> { + Box::pin(async move { + projection_cache_retired::collect( + &mut *self.state()?, + self.generation, + id, + source, + expected_active, + projection_cache::now()?, + ) + }) + } + fn collect_obsolete_projection_snapshots( + &self, + scope: crate::projection::cache::ProjectionCacheScope, + ) -> BoxFuture<'_, Result<crate::projection::cache::ProjectionCacheCollection, Error>> { + Box::pin(async move { + projection_cache::collect_kind( + &mut *self.state()?, + self.generation, + scope, + projection_cache::now()?, + projection_cache::CollectionKind::ObsoleteSnapshots, + ) + }) + } fn status( &self, projection_id: ProjectionId, diff --git a/crates/storage/src/memory_projection_batch.rs b/crates/storage/src/memory_projection_batch.rs @@ -177,6 +177,12 @@ pub(super) fn write( .get_mut(&(identity.0.clone(), active)) { previous.phase = Phase::Retired; + state + .projection_cache + .retired + .entry(identity.0.clone()) + .or_default() + .insert(active); } if let Some(prior) = state .projections diff --git a/crates/storage/src/memory_projection_cache.rs b/crates/storage/src/memory_projection_cache.rs @@ -23,6 +23,7 @@ pub(super) struct Cache { pub(super) limits: Option<ProjectionCacheLimits>, pub(super) owners: BTreeSet<ProjectionId>, pub(super) protected: BTreeSet<Identity>, + pub(super) retired: BTreeMap<ProjectionId, BTreeSet<ProjectionGeneration>>, pub(super) snapshot_order: BTreeSet<(ProjectionId, ProjectionGeneration, u64, [u8; 32])>, pub(super) value_bytes: u64, pub(super) owner_time_ms: u64, @@ -33,6 +34,7 @@ impl Cache { limits: None, owners: BTreeSet::new(), protected: BTreeSet::new(), + retired: BTreeMap::new(), snapshot_order: BTreeSet::new(), value_bytes: 0, owner_time_ms: 0, @@ -103,20 +105,23 @@ pub(super) fn configure( state.projection_cache.time(now); Ok(state.projection_cache.usage()) } -fn fence( +pub(super) fn fence_owner( state: &State, generation: SourceGeneration, - scope: &ProjectionCacheScope, + id: &ProjectionId, + source: EventSourceToken, + active: Option<ProjectionGeneration>, ) -> Result<(), Error> { - if scope.source() != EventSourceToken::new(generation, state.event_mutation_epoch) { + ProjectionId::parse(id.as_str())?; + if source != EventSourceToken::new(generation, state.event_mutation_epoch) { return Err(Error::SourceEpochChanged); } if state .projections .iter() - .find(|value| value.projection_id() == scope.projection_id()) + .find(|value| value.projection_id() == id) .map(|value| value.generation()) - != scope.expected_active() + != active { return Err(Error::ProjectionRevisionConflict); } @@ -127,7 +132,13 @@ pub(super) fn retire( source: SourceGeneration, scope: ProjectionCacheScope, ) -> Result<ProjectionCacheUsage, Error> { - fence(state, source, &scope)?; + fence_owner( + state, + source, + scope.projection_id(), + scope.source(), + scope.expected_active(), + )?; let identity = (scope.projection_id().clone(), scope.generation()); let prior = state.projection_generations.get(&identity); let configured = state @@ -184,6 +195,12 @@ pub(super) fn retire( }, ); state.projection_cache.protected.remove(&identity); + state + .projection_cache + .retired + .entry(identity.0.clone()) + .or_default() + .insert(identity.1); if configured && scope.expected_active() == Some(scope.generation()) { state .projections @@ -197,7 +214,29 @@ pub(super) fn collect( scope: ProjectionCacheScope, now: u64, ) -> Result<ProjectionCacheCollection, Error> { - fence(state, source, &scope)?; + collect_kind(state, source, scope, now, CollectionKind::Generation) +} + +#[derive(Clone, Copy, Eq, PartialEq)] +pub(super) enum CollectionKind { + Generation, + ObsoleteSnapshots, +} + +pub(super) fn collect_kind( + state: &mut State, + source: SourceGeneration, + scope: ProjectionCacheScope, + now: u64, + kind: CollectionKind, +) -> Result<ProjectionCacheCollection, Error> { + fence_owner( + state, + source, + scope.projection_id(), + scope.source(), + scope.expected_active(), + )?; if !state .projection_cache .owners @@ -210,9 +249,26 @@ pub(super) fn collect( .projection_generations .get(&identity) .is_some_and(|value| value.phase != projection_batch::Phase::Retired); - let protected = scope.expected_active() == Some(scope.generation()) || reserved; + let active = scope.expected_active() == Some(scope.generation()); + let snapshot_only = kind == CollectionKind::ObsoleteSnapshots; + if snapshot_only { + let invalidated = active + && state.projections.iter().any(|status| { + status.projection_id() == scope.projection_id() + && status.health() == ProjectionHealth::Invalidated + && status.active_rebuild().is_none() + }); + let staging = state + .projection_generations + .get(&identity) + .is_some_and(|value| value.phase == projection_batch::Phase::Staging); + if ((reserved || active) && !invalidated) || staging { + return Err(Error::ProjectionRevisionConflict); + } + } + let protected = !snapshot_only && (active || reserved); let now = state.projection_cache.owner_time_ms.max(now); - let documents: Vec<_> = if protected { + let documents: Vec<_> = if protected || snapshot_only { Vec::new() } else { state @@ -294,6 +350,7 @@ pub(super) fn collect( state.projection_cache.value_bytes = value_bytes; state.projection_cache.time(now); let document_more = !protected + && !snapshot_only && state .projection_documents .range(( @@ -329,15 +386,23 @@ pub(super) fn collect( .next() .is_some_and(|(id, generation, _, _)| id == &identity.0 && generation == &identity.1); if !protected + && !snapshot_only && !document_more && !snapshot_more && state.projection_generations.contains_key(&identity) && removed < PROJECTION_CACHE_COLLECTION_ROWS_MAX as u64 { state.projection_generations.remove(&identity); + if let Some(generations) = state.projection_cache.retired.get_mut(&identity.0) { + generations.remove(&identity.1); + if generations.is_empty() { + state.projection_cache.retired.remove(&identity.0); + } + } removed += 1; } - let registration_more = !protected && state.projection_generations.contains_key(&identity); + let registration_more = + !protected && !snapshot_only && state.projection_generations.contains_key(&identity); Ok(ProjectionCacheCollection { removed_rows: removed, removed_value_bytes: freed, diff --git a/crates/storage/src/memory_projection_cache_retired.rs b/crates/storage/src/memory_projection_cache_retired.rs @@ -0,0 +1,58 @@ +use super::{Error, State, projection_batch, projection_cache}; +use crate::{ + event::{EventSourceToken, SourceGeneration}, + projection::{ + ProjectionGeneration, ProjectionId, + cache::{ProjectionCacheCollection, ProjectionCacheScope}, + }, +}; + +fn next( + state: &State, + id: &ProjectionId, + active: Option<ProjectionGeneration>, +) -> Option<ProjectionGeneration> { + state + .projection_cache + .retired + .get(id) + .and_then(|generations| { + generations + .iter() + .find(|generation| Some(**generation) != active) + .copied() + }) +} + +pub(super) fn collect( + state: &mut State, + generation: SourceGeneration, + id: ProjectionId, + source: EventSourceToken, + active: Option<ProjectionGeneration>, + now: u64, +) -> Result<ProjectionCacheCollection, Error> { + projection_cache::fence_owner(state, generation, &id, source, active)?; + if !state.projection_cache.owners.contains(&id) { + return Err(Error::ProjectionRevisionConflict); + } + let Some(retired) = next(state, &id, active) else { + return Ok(ProjectionCacheCollection { + removed_rows: 0, + removed_value_bytes: 0, + more: false, + usage: state.projection_cache.usage(), + }); + }; + if state + .projection_generations + .get(&(id.clone(), retired)) + .is_none_or(|value| value.phase != projection_batch::Phase::Retired) + { + return Err(Error::CorruptProjectionRecord); + } + let scope = ProjectionCacheScope::new(id.clone(), retired, source, active)?; + let mut result = projection_cache::collect(state, generation, scope, now)?; + result.more |= next(state, &id, active).is_some(); + Ok(result) +} diff --git a/crates/storage/src/projection.rs b/crates/storage/src/projection.rs @@ -1055,6 +1055,26 @@ pub trait ProjectionStore: Send + Sync { ) -> BoxFuture<'_, Result<cache::ProjectionCacheCollection, Error>> { Box::pin(async { Err(Error::BackendUnavailable) }) } + /// Collects at most64 derived rows from one indexed retired generation. + /// The registered owner, current source and active identity are fenced under + /// the same provider transaction or lock; `more` includes other retired work. + fn collect_retired_projection_cache( + &self, + _id: ProjectionId, + _source: crate::event::EventSourceToken, + _expected_active: Option<ProjectionGeneration>, + ) -> BoxFuture<'_, Result<cache::ProjectionCacheCollection, Error>> { + Box::pin(async { Err(Error::BackendUnavailable) }) + } + /// Retires only obsolete snapshots, preserving documents and other owner state. + /// A current target must be explicitly Invalidated without a rebuild; live + /// staging is refused. Exact registered-owner/source/active fences apply. + fn collect_obsolete_projection_snapshots( + &self, + _scope: cache::ProjectionCacheScope, + ) -> BoxFuture<'_, Result<cache::ProjectionCacheCollection, Error>> { + Box::pin(async { Err(Error::BackendUnavailable) }) + } /// Atomically stages a bounded batch under a durable source and generation fence. fn put_projection_batch( &self, diff --git a/crates/storage_sqlite/src/migration.rs b/crates/storage_sqlite/src/migration.rs @@ -469,7 +469,7 @@ fn schema_metadata_error(source: &sqlx::Error, database: &'static str) -> Error fn validate_plan(plan: &MigrationPlan) -> Result<(), Error> { let valid = plan.minimum_version > 0 && plan.minimum_version <= plan.current_version - && plan.current_version <= 22 + && plan.current_version <= 23 && plan.steps.len() == usize::try_from(plan.current_version).unwrap_or(usize::MAX) && plan .steps @@ -598,6 +598,7 @@ const fn set_user_version_sql(version: u32) -> Option<&'static str> { 20 => Some("PRAGMA user_version = 20"), 21 => Some("PRAGMA user_version = 21"), 22 => Some("PRAGMA user_version = 22"), + 23 => Some("PRAGMA user_version = 23"), _ => None, } } @@ -882,8 +883,8 @@ mod tests { .await .expect("application id"); let newer_version = runtime::CURRENT_VERSION + 1; - assert_eq!(newer_version, 23); - sqlx::raw_sql("PRAGMA user_version = 23") + assert_eq!(newer_version, 24); + sqlx::raw_sql("PRAGMA user_version = 24") .execute(&mut newer) .await .expect("newer version"); diff --git a/crates/storage_sqlite/src/migration/runtime/0023_projection_retired_selection.up.sql b/crates/storage_sqlite/src/migration/runtime/0023_projection_retired_selection.up.sql @@ -0,0 +1,3 @@ +CREATE INDEX radroots_runtime_projection_retired_generation_idx +ON radroots_runtime_projection_generations(projection_id,generation) +WHERE phase='retired'; diff --git a/crates/storage_sqlite/src/migration/runtime/mod.rs b/crates/storage_sqlite/src/migration/runtime/mod.rs @@ -6,7 +6,7 @@ /// Lowest runtime schema version this package can recognize. pub const MINIMUM_VERSION: u32 = 1; /// Current runtime schema version created by this package. -pub const CURRENT_VERSION: u32 = 22; +pub const CURRENT_VERSION: u32 = 23; const RUNTIME_V1_SQL: &str = include_str!("0001_runtime.up.sql"); const CANONICAL_EVENT_STORAGE_V2_SQL: &str = include_str!("0002_canonical_event_storage.up.sql"); @@ -39,6 +39,8 @@ const CANONICAL_VISIBILITY_INDEX_V19_SQL: &str = include_str!("0019_canonical_visibility_index.up.sql"); const PROJECTION_CACHE_BUDGET_V22_SQL: &str = include_str!("0022_projection_cache_budget.up.sql"); +const PROJECTION_RETIRED_SELECTION_V23_SQL: &str = + include_str!("0023_projection_retired_selection.up.sql"); const PROJECTION_ATOMIC_GENERATIONS_V21_SQL: &str = include_str!("0021_projection_atomic_generations.up.sql"); @@ -1451,6 +1453,134 @@ const RUNTIME_V22_OBJECTS: &[&str] = &[ "radroots_runtime_visibility_progress", ]; +const RUNTIME_V23_OBJECTS: &[&str] = &[ + "radroots_runtime_atomic_commits", + "radroots_runtime_authored_artifacts", + "radroots_runtime_authored_artifacts_admission_ready_idx", + "radroots_runtime_authored_artifacts_signed_fact_guard", + "radroots_runtime_authored_artifacts_signing_ready_idx", + "radroots_runtime_authored_atomic_commits", + "radroots_runtime_authored_atomic_commits_delete_guard", + "radroots_runtime_authored_atomic_commits_update_guard", + "radroots_runtime_authored_atomic_target_phase_idx", + "radroots_runtime_authored_delivery_attempts", + "radroots_runtime_authored_delivery_claims", + "radroots_runtime_authored_delivery_claims_delete_guard", + "radroots_runtime_authored_delivery_claims_update_guard", + "radroots_runtime_authored_delivery_facts", + "radroots_runtime_authored_delivery_facts_delete_guard", + "radroots_runtime_authored_delivery_facts_update_guard", + "radroots_runtime_authored_delivery_plans", + "radroots_runtime_authored_delivery_ready_idx", + "radroots_runtime_authored_delivery_reconciliations", + "radroots_runtime_authored_delivery_reconciliations_delete_guard", + "radroots_runtime_authored_delivery_reconciliations_update_guard", + "radroots_runtime_authored_delivery_stop_guard", + "radroots_runtime_authored_delivery_targets", + "radroots_runtime_authored_draft_author_head_idx", + "radroots_runtime_authored_draft_revisions", + "radroots_runtime_authored_draft_revisions_delete_guard", + "radroots_runtime_authored_draft_revisions_update_guard", + "radroots_runtime_authored_draft_scope_head_idx", + "radroots_runtime_authored_migration_evidence", + "radroots_runtime_authored_migration_evidence_delete_guard", + "radroots_runtime_authored_migration_evidence_update_guard", + "radroots_runtime_authored_operations", + "radroots_runtime_delivery_evidence", + "radroots_runtime_delivery_evidence_item_idx", + "radroots_runtime_event_index_checkpoints", + "radroots_runtime_event_index_manifests", + "radroots_runtime_event_index_shards", + "radroots_runtime_event_provenance", + "radroots_runtime_event_provenance_observed_idx", + "radroots_runtime_events", + "radroots_runtime_events_admission_idx", + "radroots_runtime_events_contract_metadata_guard", + "radroots_runtime_events_contract_metadata_insert_guard", + "radroots_runtime_events_delete_guard", + "radroots_runtime_events_event_id_idx", + "radroots_runtime_events_mutation_idx", + "radroots_runtime_events_mutation_insert", + "radroots_runtime_events_mutation_update", + "radroots_runtime_events_raw_update_guard", + "radroots_runtime_journal_idempotency_idx", + "radroots_runtime_journal_operations", + "radroots_runtime_journal_recovery_idx", + "radroots_runtime_legacy_event_staging", + "radroots_runtime_legacy_event_staging_delete_guard", + "radroots_runtime_legacy_event_staging_insert_guard", + "radroots_runtime_legacy_event_staging_update_guard", + "radroots_runtime_legacy_import_commit_delete_guard", + "radroots_runtime_legacy_import_commit_update_guard", + "radroots_runtime_legacy_import_commits", + "radroots_runtime_legacy_import_delete_guard", + "radroots_runtime_legacy_import_identity_guard", + "radroots_runtime_legacy_import_member_delete_guard", + "radroots_runtime_legacy_import_member_identity_guard", + "radroots_runtime_legacy_import_member_state_guard", + "radroots_runtime_legacy_import_members", + "radroots_runtime_legacy_import_state_guard", + "radroots_runtime_legacy_import_state_idx", + "radroots_runtime_legacy_imports", + "radroots_runtime_legacy_outbox_staging", + "radroots_runtime_legacy_outbox_staging_delete_guard", + "radroots_runtime_legacy_outbox_staging_insert_guard", + "radroots_runtime_legacy_outbox_staging_parent_idx", + "radroots_runtime_legacy_outbox_staging_update_guard", + "radroots_runtime_outbox_items", + "radroots_runtime_outbox_operation_idx", + "radroots_runtime_outbox_ready_idx", + "radroots_runtime_outbox_targets", + "radroots_runtime_projection_cache_budget", + "radroots_runtime_projection_cache_capacity_guard", + "radroots_runtime_projection_cache_checkpoint_delete", + "radroots_runtime_projection_cache_checkpoint_insert", + "radroots_runtime_projection_cache_checkpoint_update", + "radroots_runtime_projection_cache_document_delete", + "radroots_runtime_projection_cache_document_insert", + "radroots_runtime_projection_cache_document_update", + "radroots_runtime_projection_cache_generation_delete", + "radroots_runtime_projection_cache_generation_insert", + "radroots_runtime_projection_cache_generation_update", + "radroots_runtime_projection_cache_owners", + "radroots_runtime_projection_cache_snapshot_delete", + "radroots_runtime_projection_cache_snapshot_insert", + "radroots_runtime_projection_checkpoints", + "radroots_runtime_projection_documents", + "radroots_runtime_projection_generations", + "radroots_runtime_projection_generations_staging_idx", + "radroots_runtime_projection_invalidations", + "radroots_runtime_projection_rebuilds", + "radroots_runtime_projection_rebuilds_stage_idx", + "radroots_runtime_projection_retired_generation_idx", + "radroots_runtime_projection_snapshots", + "radroots_runtime_projection_snapshots_created_idx", + "radroots_runtime_projection_snapshots_generation_created_idx", + "radroots_runtime_projection_snapshots_update_guard", + "radroots_runtime_source_generations", + "radroots_runtime_source_generations_active_idx", + "radroots_runtime_source_generations_delete_guard", + "radroots_runtime_source_generations_identity_guard", + "radroots_runtime_source_generations_mutation_guard", + "radroots_runtime_source_generations_sequence_guard", + "radroots_runtime_visibility_deletion_addresses", + "radroots_runtime_visibility_deletion_addresses_request_idx", + "radroots_runtime_visibility_deletion_events", + "radroots_runtime_visibility_deletion_events_request_idx", + "radroots_runtime_visibility_empty_source", + "radroots_runtime_visibility_events", + "radroots_runtime_visibility_events_head_idx", + "radroots_runtime_visibility_events_stage_idx", + "radroots_runtime_visibility_heads", + "radroots_runtime_visibility_members", + "radroots_runtime_visibility_members_count_insert", + "radroots_runtime_visibility_members_count_update", + "radroots_runtime_visibility_members_event_idx", + "radroots_runtime_visibility_members_page_idx", + "radroots_runtime_visibility_pending", + "radroots_runtime_visibility_progress", +]; + pub const MIGRATIONS: &[MigrationDescriptor] = &[ MigrationDescriptor { version: 1, @@ -1584,6 +1714,12 @@ pub const MIGRATIONS: &[MigrationDescriptor] = &[ up_sha256: "fa0b0c26e0ac782b77abc072f67f79c135549c8970dc9ced093220460451531d", owned_objects: RUNTIME_V22_OBJECTS, }, + MigrationDescriptor { + version: 23, + name: "projection_retired_selection", + up_sha256: "5cbe0dbd80e8b700ccda31346c33419698294a28196f8408a2ff21b45956c365", + owned_objects: RUNTIME_V23_OBJECTS, + }, ]; pub(crate) const fn migration_sql(version: u32) -> Option<&'static str> { @@ -1610,6 +1746,7 @@ pub(crate) const fn migration_sql(version: u32) -> Option<&'static str> { 20 => Some(INDEXED_VISIBILITY_READS_V20_SQL), 21 => Some(PROJECTION_ATOMIC_GENERATIONS_V21_SQL), 22 => Some(PROJECTION_CACHE_BUDGET_V22_SQL), + 23 => Some(PROJECTION_RETIRED_SELECTION_V23_SQL), _ => None, } } @@ -1653,8 +1790,8 @@ mod tests { fn migration_plan_matches_governed_snapshot() { let snapshot = toml::from_str::<PlanSnapshot>(PLAN_SNAPSHOT).expect("valid snapshot"); assert_eq!(MINIMUM_VERSION, 1); - assert_eq!(CURRENT_VERSION, 22); - assert_eq!(MIGRATIONS.len(), 22); + assert_eq!(CURRENT_VERSION, 23); + assert_eq!(MIGRATIONS.len(), 23); let migration = MIGRATIONS[8]; assert_eq!(snapshot.schema_version, 1); assert_eq!(snapshot.database, "runtime.sqlite"); diff --git a/crates/storage_sqlite/src/projection/cache.rs b/crates/storage_sqlite/src/projection/cache.rs @@ -43,7 +43,9 @@ pub(super) async fn owner( .map_err(map)?; Ok(row.is_some()) } -async fn usage(transaction: &mut Transaction<'_, Sqlite>) -> Result<ProjectionCacheUsage, Error> { +pub(super) async fn usage( + transaction: &mut Transaction<'_, Sqlite>, +) -> Result<ProjectionCacheUsage, Error> { let row = sqlx::query("SELECT value_bytes,protected_generations,legacy_snapshots FROM radroots_runtime_projection_cache_budget WHERE singleton=1").fetch_one(&mut **transaction).await.map_err(map)?; Ok(ProjectionCacheUsage { value_bytes: u64_from_i64(row.try_get("value_bytes").map_err(map_corrupt)?)?, @@ -103,28 +105,31 @@ pub(super) async fn configure( transaction.commit().await.map_err(map)?; Ok(current) } -async fn fence( +pub(super) async fn fence_owner( transaction: &mut Transaction<'_, Sqlite>, store: &SqliteStorage, - scope: &ProjectionCacheScope, + id: &ProjectionId, + source: radroots_storage::event::EventSourceToken, + active: Option<radroots_storage::projection::ProjectionGeneration>, ) -> Result<Option<radroots_storage::projection::ProjectionStatus>, Error> { - if scope.source().generation() != store.generation { + ProjectionId::parse(id.as_str())?; + if source.generation() != store.generation { return Err(Error::SourceEpochChanged); } - let epoch: Option<i64> = sqlx::query_scalar("SELECT mutation_epoch FROM radroots_runtime_source_generations WHERE generation=? AND state='active'").bind(scope.source().generation().as_bytes().as_slice()).fetch_optional(&mut **transaction).await.map_err(map)?; - if epoch.map(u64_from_i64).transpose()? != Some(scope.source().epoch()) { + let epoch: Option<i64> = sqlx::query_scalar("SELECT mutation_epoch FROM radroots_runtime_source_generations WHERE generation=? AND state='active'").bind(source.generation().as_bytes().as_slice()).fetch_optional(&mut **transaction).await.map_err(map)?; + if epoch.map(u64_from_i64).transpose()? != Some(source.epoch()) { return Err(Error::SourceEpochChanged); } let status = sqlx::query("SELECT * FROM radroots_runtime_projection_checkpoints WHERE projection_id=?") - .bind(scope.projection_id().as_str()) + .bind(id.as_str()) .fetch_optional(&mut **transaction) .await .map_err(map)? .as_ref() .map(decode_status) .transpose()?; - if status.as_ref().map(|value| value.generation()) != scope.expected_active() { + if status.as_ref().map(|value| value.generation()) != active { return Err(Error::ProjectionRevisionConflict); } Ok(status) @@ -139,7 +144,14 @@ pub(super) async fn retire( .begin_with("BEGIN IMMEDIATE") .await .map_err(map)?; - let status = fence(&mut transaction, store, &scope).await?; + let status = fence_owner( + &mut transaction, + store, + scope.projection_id(), + scope.source(), + scope.expected_active(), + ) + .await?; let row = sqlx::query("SELECT source_generation,source_epoch,phase FROM radroots_runtime_projection_generations WHERE projection_id=? AND generation=?").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).fetch_optional(&mut *transaction).await.map_err(map)?; let configured = owner(&mut transaction, scope.projection_id()).await?; if !configured { @@ -191,41 +203,87 @@ pub(super) async fn collect( store: &SqliteStorage, scope: ProjectionCacheScope, ) -> Result<ProjectionCacheCollection, Error> { + collect_kind(store, scope, CollectionKind::Generation).await +} + +#[derive(Clone, Copy, Eq, PartialEq)] +pub(super) enum CollectionKind { + Generation, + ObsoleteSnapshots, +} + +pub(super) async fn collect_kind( + store: &SqliteStorage, + scope: ProjectionCacheScope, + kind: CollectionKind, +) -> Result<ProjectionCacheCollection, Error> { store.require_projection_writer()?; let mut transaction = store .pool() .begin_with("BEGIN IMMEDIATE") .await .map_err(map)?; - fence(&mut transaction, store, &scope).await?; - if !owner(&mut transaction, scope.projection_id()).await? { + let result = collect_in(&mut transaction, store, &scope, kind).await?; + transaction.commit().await.map_err(map)?; + Ok(result) +} +pub(super) async fn collect_in( + transaction: &mut Transaction<'_, Sqlite>, + store: &SqliteStorage, + scope: &ProjectionCacheScope, + kind: CollectionKind, +) -> Result<ProjectionCacheCollection, Error> { + let status = fence_owner( + transaction, + store, + scope.projection_id(), + scope.source(), + scope.expected_active(), + ) + .await?; + if !owner(transaction, scope.projection_id()).await? { return Err(Error::ProjectionRevisionConflict); } - let phase: Option<String> = sqlx::query_scalar("SELECT phase FROM radroots_runtime_projection_generations WHERE projection_id=? AND generation=?").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).fetch_optional(&mut *transaction).await.map_err(map)?; - let protected = scope.expected_active() == Some(scope.generation()) - || phase.as_deref().is_some_and(|value| value != "retired"); - let now = time(&mut transaction, now()?).await?; - let limit = limits(&mut transaction) + let phase: Option<String> = sqlx::query_scalar("SELECT phase FROM radroots_runtime_projection_generations WHERE projection_id=? AND generation=?").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).fetch_optional(&mut **transaction).await.map_err(map)?; + let active = scope.expected_active() == Some(scope.generation()); + let reserved = phase.as_deref().is_some_and(|value| value != "retired"); + let snapshot_only = kind == CollectionKind::ObsoleteSnapshots; + if snapshot_only { + let invalidated = active + && status.as_ref().is_some_and(|status| { + status.health() == ProjectionHealth::Invalidated + && status.active_rebuild().is_none() + }); + let staging = phase + .as_deref() + .is_some_and(|value| value != "retired" && value != "promoted"); + if ((reserved || active) && !invalidated) || staging { + return Err(Error::ProjectionRevisionConflict); + } + } + let protected = !snapshot_only && (active || reserved); + let now = time(transaction, now()?).await?; + let limit = limits(transaction) .await? .ok_or(Error::ProjectionRevisionConflict)?; let mut removed = 0u64; let mut freed = 0u64; - if !protected { + if !protected && !snapshot_only { let rows = sqlx::query("SELECT document_key,length(value) AS bytes FROM radroots_runtime_projection_documents WHERE projection_id=? AND generation=? ORDER BY document_key LIMIT 64") - .bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).fetch_all(&mut *transaction).await.map_err(map)?; + .bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).fetch_all(&mut **transaction).await.map_err(map)?; for row in rows { let key: String = row.try_get("document_key").map_err(map_corrupt)?; let bytes = u64_from_i64(row.try_get("bytes").map_err(map_corrupt)?)?; - sqlx::query("DELETE FROM radroots_runtime_projection_documents WHERE projection_id=? AND generation=? AND document_key=?").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).bind(key).execute(&mut *transaction).await.map_err(map)?; + sqlx::query("DELETE FROM radroots_runtime_projection_documents WHERE projection_id=? AND generation=? AND document_key=?").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).bind(key).execute(&mut **transaction).await.map_err(map)?; removed += 1; freed = freed .checked_add(bytes) .ok_or(Error::CorruptProjectionRecord)?; } } - let mut current = usage(&mut transaction).await?; + let mut current = usage(transaction).await?; let rows = sqlx::query("SELECT snapshot_id,created_at_unix_ms,length(value) AS bytes FROM radroots_runtime_projection_snapshots INDEXED BY radroots_runtime_projection_snapshots_generation_created_idx WHERE projection_id=? AND generation=? ORDER BY created_at_unix_ms,snapshot_id LIMIT ?") - .bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).bind((PROJECTION_CACHE_COLLECTION_ROWS_MAX as u64-removed) as i64).fetch_all(&mut *transaction).await.map_err(map)?; + .bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).bind((PROJECTION_CACHE_COLLECTION_ROWS_MAX as u64-removed) as i64).fetch_all(&mut **transaction).await.map_err(map)?; let mut future_after = now; let mut future_id = vec![255; 32]; for row in rows { @@ -239,7 +297,7 @@ pub(super) async fn collect( } let id: Vec<u8> = row.try_get("snapshot_id").map_err(map_corrupt)?; let bytes = u64_from_i64(row.try_get("bytes").map_err(map_corrupt)?)?; - delete_snapshot(&mut transaction, scope.projection_id(), &id).await?; + delete_snapshot(transaction, scope.projection_id(), &id).await?; if created > now { future_after = created; future_id = id; @@ -252,18 +310,18 @@ pub(super) async fn collect( } if protected && removed < PROJECTION_CACHE_COLLECTION_ROWS_MAX as u64 { let rows = sqlx::query("SELECT snapshot_id,length(value) AS bytes FROM radroots_runtime_projection_snapshots INDEXED BY radroots_runtime_projection_snapshots_generation_created_idx WHERE projection_id=? AND generation=? AND (created_at_unix_ms,snapshot_id)>(?,?) ORDER BY created_at_unix_ms,snapshot_id LIMIT ?") - .bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).bind(i64_from_u64(future_after)?).bind(future_id).bind((PROJECTION_CACHE_COLLECTION_ROWS_MAX as u64-removed) as i64).fetch_all(&mut *transaction).await.map_err(map)?; + .bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).bind(i64_from_u64(future_after)?).bind(future_id).bind((PROJECTION_CACHE_COLLECTION_ROWS_MAX as u64-removed) as i64).fetch_all(&mut **transaction).await.map_err(map)?; for row in rows { let id: Vec<u8> = row.try_get("snapshot_id").map_err(map_corrupt)?; - delete_snapshot(&mut transaction, scope.projection_id(), &id).await?; + delete_snapshot(transaction, scope.projection_id(), &id).await?; removed += 1; freed = freed .checked_add(u64_from_i64(row.try_get("bytes").map_err(map_corrupt)?)?) .ok_or(Error::CorruptProjectionRecord)?; } } - let document_more: bool = !protected && sqlx::query_scalar::<_,i64>("SELECT EXISTS(SELECT 1 FROM radroots_runtime_projection_documents WHERE projection_id=? AND generation=?)").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).fetch_one(&mut *transaction).await.map_err(map)? != 0; - current = usage(&mut transaction).await?; + let document_more: bool = !protected && !snapshot_only && sqlx::query_scalar::<_,i64>("SELECT EXISTS(SELECT 1 FROM radroots_runtime_projection_documents WHERE projection_id=? AND generation=?)").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).fetch_one(&mut **transaction).await.map_err(map)? != 0; + current = usage(transaction).await?; let snapshot_sql = "SELECT EXISTS(SELECT 1 FROM radroots_runtime_projection_snapshots INDEXED BY radroots_runtime_projection_snapshots_generation_created_idx WHERE projection_id=? AND generation=? AND created_at_unix_ms<=?)"; let cutoff = if !protected || current.legacy_snapshots > limit.legacy_snapshots() { i64::MAX as u64 @@ -274,19 +332,20 @@ pub(super) async fn collect( .bind(scope.projection_id().as_str()) .bind(scope.generation().as_bytes().as_slice()) .bind(i64_from_u64(cutoff)?) - .fetch_one(&mut *transaction) + .fetch_one(&mut **transaction) .await .map_err(map)?; - let future_more: i64 = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM radroots_runtime_projection_snapshots INDEXED BY radroots_runtime_projection_snapshots_generation_created_idx WHERE projection_id=? AND generation=? AND created_at_unix_ms>?)").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).bind(i64_from_u64(now)?).fetch_one(&mut *transaction).await.map_err(map)?; + let future_more: i64 = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM radroots_runtime_projection_snapshots INDEXED BY radroots_runtime_projection_snapshots_generation_created_idx WHERE projection_id=? AND generation=? AND created_at_unix_ms>?)").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).bind(i64_from_u64(now)?).fetch_one(&mut **transaction).await.map_err(map)?; let snapshot_more = expired_more | future_more; - let mut registration_more = !protected && phase.is_some(); + let mut registration_more = !protected && !snapshot_only && phase.is_some(); if !protected + && !snapshot_only && !document_more && snapshot_more == 0 && phase.is_some() && removed < PROJECTION_CACHE_COLLECTION_ROWS_MAX as u64 { - sqlx::query("DELETE FROM radroots_runtime_projection_generations WHERE projection_id=? AND generation=?").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).execute(&mut *transaction).await.map_err(map)?; + sqlx::query("DELETE FROM radroots_runtime_projection_generations WHERE projection_id=? AND generation=?").bind(scope.projection_id().as_str()).bind(scope.generation().as_bytes().as_slice()).execute(&mut **transaction).await.map_err(map)?; removed += 1; registration_more = false; } @@ -294,9 +353,8 @@ pub(super) async fn collect( removed_rows: removed, removed_value_bytes: freed, more: document_more || snapshot_more != 0 || registration_more, - usage: usage(&mut transaction).await?, + usage: usage(transaction).await?, }; - transaction.commit().await.map_err(map)?; Ok(result) } async fn delete_snapshot( diff --git a/crates/storage_sqlite/src/projection/cache_retired.rs b/crates/storage_sqlite/src/projection/cache_retired.rs @@ -0,0 +1,82 @@ +use super::{SqliteStorage, cache}; +use radroots_storage::{ + Error, + event::EventSourceToken, + projection::{ + ProjectionGeneration, ProjectionId, + cache::{ProjectionCacheCollection, ProjectionCacheScope}, + }, +}; +use sqlx::{Sqlite, Transaction}; + +const SELECTION: &str = "SELECT CASE WHEN length(generation)=32 THEN generation END + FROM radroots_runtime_projection_generations INDEXED BY radroots_runtime_projection_retired_generation_idx + WHERE projection_id=? AND phase='retired' AND (? IS NULL OR generation!=?) + ORDER BY generation LIMIT 1"; + +async fn next( + transaction: &mut Transaction<'_, Sqlite>, + id: &ProjectionId, + active: Option<ProjectionGeneration>, +) -> Result<Option<ProjectionGeneration>, Error> { + let active = active.map(|generation| generation.as_bytes().to_vec()); + let value: Option<Option<Vec<u8>>> = sqlx::query_scalar(SELECTION) + .bind(id.as_str()) + .bind(active.clone()) + .bind(active) + .fetch_optional(&mut **transaction) + .await + .map_err(cache::map)?; + value + .map(|value| { + let bytes = value.ok_or(Error::CorruptProjectionRecord)?; + ProjectionGeneration::new( + bytes + .try_into() + .map_err(|_| Error::CorruptProjectionRecord)?, + ) + .map_err(|_| Error::CorruptProjectionRecord) + }) + .transpose() +} + +pub(super) async fn collect( + store: &SqliteStorage, + id: ProjectionId, + source: EventSourceToken, + active: Option<ProjectionGeneration>, +) -> Result<ProjectionCacheCollection, Error> { + store.require_projection_writer()?; + let mut transaction = store + .pool() + .begin_with("BEGIN IMMEDIATE") + .await + .map_err(cache::map)?; + cache::fence_owner(&mut transaction, store, &id, source, active).await?; + if !cache::owner(&mut transaction, &id).await? { + return Err(Error::ProjectionRevisionConflict); + } + let mut result = if let Some(retired) = next(&mut transaction, &id, active).await? { + cache::collect_in( + &mut transaction, + store, + &ProjectionCacheScope::new(id.clone(), retired, source, active)?, + cache::CollectionKind::Generation, + ) + .await? + } else { + ProjectionCacheCollection { + removed_rows: 0, + removed_value_bytes: 0, + more: false, + usage: cache::usage(&mut transaction).await?, + } + }; + result.more |= next(&mut transaction, &id, active).await?.is_some(); + transaction.commit().await.map_err(cache::map)?; + Ok(result) +} + +#[cfg(test)] +#[path = "cache_retired_owner_tests.rs"] +mod tests; diff --git a/crates/storage_sqlite/src/projection/cache_retired_owner_tests.rs b/crates/storage_sqlite/src/projection/cache_retired_owner_tests.rs @@ -0,0 +1,614 @@ +use super::*; +use crate::{OpenMode, OpenOptions, Paths}; +use radroots_storage::{ + ProjectionStore, Storage, + event::SourceGeneration, + memory::MemoryStorage, + projection::{ + InvalidationReason, ProjectionCheckpoint, ProjectionDocument, ProjectionHealth, + ProjectionInvalidation, ProjectionSnapshot, + batch::{ProjectionDocumentBatch, ProjectionWriteScope}, + cache::{ProjectionCacheLimits, ProjectionCacheScope, ProjectionCacheUsage}, + document_query::{ProjectionDocumentGenerations, ProjectionDocumentQuery}, + }, +}; +use sqlx::Row; +use tempfile::TempDir; + +fn id() -> ProjectionId { + ProjectionId::parse("retired_owner_fixture").unwrap() +} +fn foreign() -> ProjectionId { + ProjectionId::parse("foreign_fixture").unwrap() +} +fn generation(value: u8) -> ProjectionGeneration { + ProjectionGeneration::new([value; 32]).unwrap() +} +fn limits() -> ProjectionCacheLimits { + ProjectionCacheLimits::new(67_108_864, 2, 32, 1_800_000).unwrap() +} +fn query() -> ProjectionDocumentQuery { + ProjectionDocumentQuery::new(id(), ProjectionDocumentGenerations::All, 256).unwrap() +} +async fn open(temp: &TempDir, mode: OpenMode) -> SqliteStorage { + let options = OpenOptions::new(Paths::from_directory(temp.path()).unwrap(), mode); + let options = if mode == OpenMode::Create { + options + .with_source_generation(SourceGeneration::new([9; 32]).unwrap(), 9) + .unwrap() + } else { + options + }; + SqliteStorage::open(options).await.unwrap() +} +fn batch( + source: EventSourceToken, + active: Option<ProjectionGeneration>, + target: u8, + start: usize, + count: usize, +) -> ProjectionDocumentBatch { + ProjectionDocumentBatch::new( + ProjectionWriteScope::new(id(), generation(target), source, active).unwrap(), + (start..start + count) + .map(|index| ProjectionDocument::new(format!("row.{index:03}"), vec![target]).unwrap()) + .collect(), + ) + .unwrap() +} +async fn seed(backend: &dyn Storage) -> EventSourceToken { + let source = backend.source_token().await.unwrap(); + backend + .put_projection_snapshot( + ProjectionSnapshot::new(id(), [7; 32], generation(1), 1, vec![7; 8]).unwrap(), + ) + .await + .unwrap(); + backend + .configure_projection_cache(id(), limits()) + .await + .unwrap(); + backend + .promote_projection_batch( + batch(source, None, 1, 0, 63), + ProjectionCheckpoint::new(id(), generation(1), None, 63, 1).unwrap(), + ) + .await + .unwrap(); + backend + .put_projection_batch(batch(source, Some(generation(1)), 2, 0, 64)) + .await + .unwrap(); + backend + .promote_projection_batch( + batch(source, Some(generation(1)), 2, 64, 1), + ProjectionCheckpoint::new(id(), generation(2), None, 65, 2).unwrap(), + ) + .await + .unwrap(); + backend + .promote_projection_batch( + batch(source, Some(generation(2)), 3, 0, 1), + ProjectionCheckpoint::new(id(), generation(3), None, 1, 3).unwrap(), + ) + .await + .unwrap(); + backend + .put_projection_batch(batch(source, Some(generation(3)), 4, 0, 1)) + .await + .unwrap(); + backend + .put_projection_document( + foreign(), + generation(5), + ProjectionDocument::new("keep".into(), vec![5]).unwrap(), + ) + .await + .unwrap(); + source +} +async fn preserved(backend: &dyn Storage, source: EventSourceToken) { + assert_eq!(backend.source_token().await.unwrap(), source); + assert_eq!( + ProjectionStore::status(backend, id()) + .await + .unwrap() + .unwrap() + .generation(), + generation(3) + ); + for target in [3, 4] { + assert_eq!( + backend + .projection_document(id(), generation(target), "row.000".into()) + .await + .unwrap() + .unwrap() + .value(), + &[target] + ); + } + assert_eq!( + backend + .projection_document(foreign(), generation(5), "keep".into()) + .await + .unwrap() + .unwrap() + .value(), + &[5] + ); +} + +#[tokio::test] +async fn projection_retired_owner_collection_bounds_every_pass_and_preserves_active_staging_foreign() + { + let temp = TempDir::new().unwrap(); + let store = open(&temp, OpenMode::Create).await; + let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap()); + for backend in [&memory as &dyn Storage, &store as &dyn Storage] { + let source = seed(backend).await; + let before = backend + .configure_projection_cache(id(), limits()) + .await + .unwrap(); + assert_eq!(before.value_bytes, 139); + assert_eq!(before.protected_generations, 2); + assert_eq!(before.legacy_snapshots, 1); + let mut freed = 0; + for (index, removed) in [64, 1, 64, 2].into_iter().enumerate() { + let result = backend + .collect_retired_projection_cache(id(), source, Some(generation(3))) + .await + .unwrap(); + assert_eq!(result.removed_rows, removed); + assert!(result.removed_rows <= 64); + assert_eq!(result.more, index < 3); + freed += result.removed_value_bytes; + preserved(backend, source).await; + } + assert_eq!(freed, 136); + let done = backend + .collect_retired_projection_cache(id(), source, Some(generation(3))) + .await + .unwrap(); + assert_eq!(done.removed_rows, 0); + assert_eq!(done.removed_value_bytes, 0); + assert!(!done.more); + assert_eq!( + done.usage, + ProjectionCacheUsage { + value_bytes: 3, + protected_generations: 2, + legacy_snapshots: 0 + } + ); + assert_eq!( + backend + .query_projection_documents(query()) + .await + .unwrap() + .records() + .len(), + 2 + ); + } + store.close().await.unwrap(); +} + +#[tokio::test] +async fn projection_retired_owner_collection_fences_no_work_and_refuses_before_effects() { + let temp = TempDir::new().unwrap(); + let store = open(&temp, OpenMode::Create).await; + let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap()); + for backend in [&memory as &dyn Storage, &store as &dyn Storage] { + let source = backend.source_token().await.unwrap(); + assert_eq!( + backend + .collect_retired_projection_cache(id(), source, None) + .await, + Err(Error::ProjectionRevisionConflict) + ); + let empty_id = ProjectionId::parse("empty_fixture").unwrap(); + backend + .configure_projection_cache(empty_id.clone(), limits()) + .await + .unwrap(); + let empty = backend + .collect_retired_projection_cache(empty_id.clone(), source, None) + .await + .unwrap(); + assert_eq!(empty.removed_rows, 0); + assert!(!empty.more); + let stale = EventSourceToken::new(source.generation(), source.epoch() + 1); + assert_eq!( + backend + .collect_retired_projection_cache(empty_id.clone(), stale, None) + .await, + Err(Error::SourceEpochChanged) + ); + assert_eq!( + backend + .collect_retired_projection_cache(empty_id, source, Some(generation(3))) + .await, + Err(Error::ProjectionRevisionConflict) + ); + let source = seed(backend).await; + let before = backend + .query_projection_documents(query()) + .await + .unwrap() + .into_records(); + let usage = backend + .configure_projection_cache(id(), limits()) + .await + .unwrap(); + for changed in [ + stale, + EventSourceToken::new(SourceGeneration::new([8; 32]).unwrap(), 0), + ] { + assert_eq!( + backend + .collect_retired_projection_cache(id(), changed, Some(generation(3))) + .await, + Err(Error::SourceEpochChanged) + ); + } + assert_eq!( + backend + .collect_retired_projection_cache(id(), source, Some(generation(2))) + .await, + Err(Error::ProjectionRevisionConflict) + ); + assert_eq!( + backend + .collect_retired_projection_cache(foreign(), source, None) + .await, + Err(Error::ProjectionRevisionConflict) + ); + assert_eq!( + backend + .query_projection_documents(query()) + .await + .unwrap() + .into_records(), + before + ); + assert_eq!( + backend + .configure_projection_cache(id(), limits()) + .await + .unwrap(), + usage + ); + preserved(backend, source).await; + } + store.close().await.unwrap(); +} + +#[tokio::test] +async fn projection_retired_owner_collection_rolls_back_and_recovers_across_read_race_and_reopen() { + let temp = TempDir::new().unwrap(); + let store = open(&temp, OpenMode::Create).await; + let source = seed(&store).await; + let before = store + .query_projection_documents(query()) + .await + .unwrap() + .into_records(); + let usage = store + .configure_projection_cache(id(), limits()) + .await + .unwrap(); + sqlx::raw_sql("CREATE TRIGGER fixture_retired_delete BEFORE DELETE ON radroots_runtime_projection_documents WHEN OLD.document_key='row.010' BEGIN SELECT RAISE(ABORT,'fixture'); END;").execute(store.pool()).await.unwrap(); + assert!( + store + .collect_retired_projection_cache(id(), source, Some(generation(3))) + .await + .is_err() + ); + assert_eq!( + store + .query_projection_documents(query()) + .await + .unwrap() + .into_records(), + before + ); + assert_eq!( + store + .configure_projection_cache(id(), limits()) + .await + .unwrap(), + usage + ); + sqlx::raw_sql("DROP TRIGGER fixture_retired_delete") + .execute(store.pool()) + .await + .unwrap(); + let current = ProjectionDocumentQuery::new( + id(), + ProjectionDocumentGenerations::Exact(generation(3)), + 20, + ) + .unwrap(); + let (read, collected) = tokio::join!( + store.query_projection_documents_at(current, source, generation(3)), + store.collect_retired_projection_cache(id(), source, Some(generation(3))), + ); + assert_eq!(read.unwrap().records().len(), 1); + assert_eq!(collected.unwrap().removed_rows, 64); + store.close().await.unwrap(); + let reopened = open(&temp, OpenMode::ReadWriteExisting).await; + for (removed, more) in [(1, true), (64, true), (2, false)] { + let result = reopened + .collect_retired_projection_cache(id(), source, Some(generation(3))) + .await + .unwrap(); + assert_eq!((result.removed_rows, result.more), (removed, more)); + } + preserved(&reopened, source).await; + reopened.close().await.unwrap(); + let readonly = open(&temp, OpenMode::ReadOnly).await; + assert_eq!( + readonly + .collect_retired_projection_cache(id(), source, Some(generation(3))) + .await, + Err(Error::BackendUnavailable) + ); + preserved(&readonly, source).await; + readonly.close().await.unwrap(); +} + +#[tokio::test] +async fn projection_retired_owner_selection_uses_partial_index_without_payload_scan() { + let temp = TempDir::new().unwrap(); + let store = open(&temp, OpenMode::Create).await; + seed(&store).await; + let mut explain = sqlx::QueryBuilder::<sqlx::Sqlite>::new("EXPLAIN QUERY PLAN "); + explain.push(SELECTION); + let details = explain + .build() + .bind(id().as_str()) + .bind(generation(3).as_bytes().as_slice()) + .bind(generation(3).as_bytes().as_slice()) + .fetch_all(store.pool()) + .await + .unwrap(); + let details: Vec<String> = details + .iter() + .map(|row| row.try_get("detail").unwrap()) + .collect(); + assert!( + details.iter().any(|detail| detail.contains("SEARCH") + && detail.contains("radroots_runtime_projection_retired_generation_idx")), + "{details:?}" + ); + assert!( + !details + .iter() + .any(|detail| detail.contains("SCAN") || detail.contains("TEMP B-TREE")), + "{details:?}" + ); + assert_eq!( + store + .projection_document(id(), generation(1), "row.000".into()) + .await + .unwrap() + .unwrap() + .value(), + &[1] + ); + store.close().await.unwrap(); +} + +#[tokio::test] +async fn projection_obsolete_snapshot_collection_preserves_legacy_custody_and_requires_safe_scope() +{ + let temp = TempDir::new().unwrap(); + let store = open(&temp, OpenMode::Create).await; + let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap()); + let legacy = ProjectionId::parse("legacy_custody_fixture").unwrap(); + for backend in [&memory as &dyn Storage, &store as &dyn Storage] { + let source = backend.source_token().await.unwrap(); + for target in [9, 10] { + backend + .put_projection_document( + legacy.clone(), + generation(target), + ProjectionDocument::new("custody".into(), vec![target; 16]).unwrap(), + ) + .await + .unwrap(); + } + backend + .checkpoint( + ProjectionCheckpoint::new(legacy.clone(), generation(10), None, 1, 1).unwrap(), + ) + .await + .unwrap(); + for index in 1..=65 { + backend + .put_projection_snapshot( + ProjectionSnapshot::new( + legacy.clone(), + [index; 32], + generation(9), + 1, + vec![7; 2], + ) + .unwrap(), + ) + .await + .unwrap(); + } + backend + .put_projection_snapshot( + ProjectionSnapshot::new(legacy.clone(), [200; 32], generation(10), 1, vec![8; 2]) + .unwrap(), + ) + .await + .unwrap(); + let old = || { + ProjectionCacheScope::new(legacy.clone(), generation(9), source, Some(generation(10))) + .unwrap() + }; + assert_eq!( + backend.collect_obsolete_projection_snapshots(old()).await, + Err(Error::ProjectionRevisionConflict) + ); + let before = backend + .configure_projection_cache(legacy.clone(), limits()) + .await + .unwrap(); + assert_eq!(before.legacy_snapshots, 66); + let current = || { + ProjectionCacheScope::new(legacy.clone(), generation(10), source, Some(generation(10))) + .unwrap() + }; + assert_eq!( + backend + .collect_obsolete_projection_snapshots(current()) + .await, + Err(Error::ProjectionRevisionConflict) + ); + assert_eq!( + backend + .collect_obsolete_projection_snapshots( + ProjectionCacheScope::new( + legacy.clone(), + generation(9), + EventSourceToken::new(source.generation(), 1), + Some(generation(10)) + ) + .unwrap() + ) + .await, + Err(Error::SourceEpochChanged) + ); + assert_eq!( + backend + .collect_obsolete_projection_snapshots( + ProjectionCacheScope::new(legacy.clone(), generation(9), source, None).unwrap() + ) + .await, + Err(Error::ProjectionRevisionConflict) + ); + backend + .put_projection_batch( + ProjectionDocumentBatch::new( + ProjectionWriteScope::new( + legacy.clone(), + generation(11), + source, + Some(generation(10)), + ) + .unwrap(), + vec![ProjectionDocument::new("staging".into(), vec![11]).unwrap()], + ) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!( + backend + .collect_obsolete_projection_snapshots( + ProjectionCacheScope::new( + legacy.clone(), + generation(11), + source, + Some(generation(10)) + ) + .unwrap() + ) + .await, + Err(Error::ProjectionRevisionConflict) + ); + for (removed, more) in [(64, true), (1, false)] { + let result = backend + .collect_obsolete_projection_snapshots(old()) + .await + .unwrap(); + assert_eq!((result.removed_rows, result.more), (removed, more)); + assert_eq!(result.removed_value_bytes, removed * 2); + } + let invalidated = backend + .invalidate( + ProjectionInvalidation::new( + legacy.clone(), + generation(10), + generation(12), + InvalidationReason::ProjectionGenerationChanged, + 2, + ) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(invalidated.health(), ProjectionHealth::Invalidated); + let result = backend + .collect_obsolete_projection_snapshots(current()) + .await + .unwrap(); + assert_eq!(result.removed_rows, 1); + assert_eq!(result.removed_value_bytes, 2); + assert!(!result.more); + assert_eq!(result.usage.legacy_snapshots, 0); + assert_eq!(result.usage.protected_generations, 2); + assert_eq!(backend.source_token().await.unwrap(), source); + assert_eq!( + ProjectionStore::status(backend, legacy.clone()) + .await + .unwrap(), + Some(invalidated) + ); + for target in [9, 10] { + assert_eq!( + backend + .projection_document(legacy.clone(), generation(target), "custody".into()) + .await + .unwrap() + .unwrap() + .value(), + &[target; 16] + ); + } + assert_eq!( + backend + .projection_document(legacy.clone(), generation(11), "staging".into()) + .await + .unwrap() + .unwrap() + .value(), + &[11] + ); + } + store.close().await.unwrap(); + let readonly = open(&temp, OpenMode::ReadOnly).await; + let source = radroots_storage::EventStore::source_token(&readonly) + .await + .unwrap(); + assert_eq!( + readonly + .collect_obsolete_projection_snapshots( + ProjectionCacheScope::new( + legacy.clone(), + generation(9), + source, + Some(generation(10)) + ) + .unwrap() + ) + .await, + Err(Error::BackendUnavailable) + ); + assert_eq!( + readonly + .projection_document(legacy, generation(9), "custody".into()) + .await + .unwrap() + .unwrap() + .value(), + &[9; 16] + ); + readonly.close().await.unwrap(); +} diff --git a/crates/storage_sqlite/src/projection/mod.rs b/crates/storage_sqlite/src/projection/mod.rs @@ -16,6 +16,7 @@ use sqlx::{Row, Sqlite, SqliteConnection}; mod batch; mod cache; +mod cache_retired; mod document_query; #[cfg_attr(coverage_nightly, coverage(off))] @@ -42,6 +43,26 @@ impl ProjectionStore for SqliteStorage { { Box::pin(cache::collect(self, scope)) } + fn collect_retired_projection_cache( + &self, + id: ProjectionId, + source: radroots_storage::event::EventSourceToken, + expected_active: Option<ProjectionGeneration>, + ) -> BoxFuture<'_, Result<radroots_storage::projection::cache::ProjectionCacheCollection, Error>> + { + Box::pin(cache_retired::collect(self, id, source, expected_active)) + } + fn collect_obsolete_projection_snapshots( + &self, + scope: radroots_storage::projection::cache::ProjectionCacheScope, + ) -> BoxFuture<'_, Result<radroots_storage::projection::cache::ProjectionCacheCollection, Error>> + { + Box::pin(cache::collect_kind( + self, + scope, + cache::CollectionKind::ObsoleteSnapshots, + )) + } fn status( &self, projection_id: ProjectionId,