lib

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

projection_cache.rs (28242B)


      1 use futures_executor::block_on;
      2 use radroots_storage::{
      3     Error, EventStore, ProjectionStore,
      4     event::{EventSourceToken, SourceGeneration},
      5     memory::MemoryStorage,
      6     projection::{
      7         ProjectionDocument, ProjectionGeneration, ProjectionId, ProjectionSnapshot,
      8         cache::{ProjectionCacheLimits, ProjectionCacheScope},
      9     },
     10 };
     11 fn id(value: &str) -> ProjectionId {
     12     ProjectionId::parse(value).unwrap()
     13 }
     14 fn generation(value: u8) -> ProjectionGeneration {
     15     ProjectionGeneration::new([value; 32]).unwrap()
     16 }
     17 
     18 #[test]
     19 fn projection_cache_limits_and_exact_scope_validate_before_effects() {
     20     for values in [
     21         [0, 2, 32, 1],
     22         [1, 0, 32, 1],
     23         [1, 2, 0, 1],
     24         [1, 2, 32, 0],
     25         [i64::MAX as u64 + 1, 2, 32, 1],
     26     ] {
     27         assert_eq!(
     28             ProjectionCacheLimits::new(values[0], values[1], values[2], values[3]),
     29             Err(Error::InvalidProjectionCacheLimits)
     30         );
     31     }
     32     let maximum = ProjectionCacheLimits::new(
     33         i64::MAX as u64,
     34         i64::MAX as u64,
     35         i64::MAX as u64,
     36         i64::MAX as u64,
     37     )
     38     .unwrap();
     39     assert_eq!(
     40         [
     41             maximum.value_bytes(),
     42             maximum.protected_generations(),
     43             maximum.legacy_snapshots(),
     44             maximum.legacy_age_ms()
     45         ],
     46         [i64::MAX as u64; 4]
     47     );
     48     let source = EventSourceToken::new(SourceGeneration::new([9; 32]).unwrap(), 0);
     49     let scope =
     50         ProjectionCacheScope::new(id("fixture"), generation(1), source, Some(generation(1)))
     51             .unwrap();
     52     assert_eq!(scope.projection_id(), &id("fixture"));
     53     assert_eq!(scope.generation(), generation(1));
     54     assert_eq!(scope.source(), source);
     55     assert_eq!(scope.expected_active(), Some(generation(1)));
     56     assert!(!Error::InvalidProjectionCacheLimits.to_string().is_empty());
     57 }
     58 
     59 #[test]
     60 fn projection_cache_preserves_other_projection_state_and_expires_legacy_by_owner_time() {
     61     let store = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
     62     block_on(store.put_projection_document(
     63         id("settings"),
     64         generation(1),
     65         ProjectionDocument::new("owned_work".into(), vec![7]).unwrap(),
     66     ))
     67     .unwrap();
     68     block_on(store.put_projection_snapshot(
     69         ProjectionSnapshot::new(id("cache"), [1; 32], generation(1), 1, vec![8]).unwrap(),
     70     ))
     71     .unwrap();
     72     let limits = ProjectionCacheLimits::new(67_108_864, 2, 32, 1_800_000).unwrap();
     73     let usage = block_on(store.configure_projection_cache(id("cache"), limits)).unwrap();
     74     assert_eq!(usage.value_bytes, 2);
     75     assert_eq!(usage.legacy_snapshots, 1);
     76     assert_eq!(
     77         block_on(store.projection_snapshot(id("cache"), [1; 32])).unwrap(),
     78         None
     79     );
     80     let source = block_on(store.source_token()).unwrap();
     81     let foreign = ProjectionCacheScope::new(id("settings"), generation(1), source, None).unwrap();
     82     assert_eq!(
     83         block_on(store.collect_projection_cache(foreign)),
     84         Err(Error::ProjectionRevisionConflict)
     85     );
     86     let exact = ProjectionCacheScope::new(id("cache"), generation(1), source, None).unwrap();
     87     let result = block_on(store.collect_projection_cache(exact)).unwrap();
     88     assert_eq!(result.removed_rows, 1);
     89     assert_eq!(result.removed_value_bytes, 1);
     90     assert_eq!(result.usage.value_bytes, 1);
     91     assert_eq!(
     92         block_on(store.projection_document(id("settings"), generation(1), "owned_work".into()))
     93             .unwrap()
     94             .unwrap()
     95             .value(),
     96         &[7]
     97     );
     98     assert_eq!(
     99         block_on(store.put_projection_snapshot(
    100             ProjectionSnapshot::new(id("cache"), [1; 32], generation(1), 1, vec![8]).unwrap()
    101         )),
    102         Err(Error::ProjectionRevisionConflict)
    103     );
    104     assert_eq!(block_on(store.source_token()).unwrap(), source);
    105     assert_eq!(
    106         block_on(store.configure_projection_cache(
    107             id("cache"),
    108             ProjectionCacheLimits::new(1, 2, 32, 1_800_000).unwrap()
    109         )),
    110         Err(Error::ProjectionRevisionConflict)
    111     );
    112 }
    113 
    114 #[test]
    115 fn projection_cache_memory_third_protected_and_foreign_bytes_refuse_without_partial_rows() {
    116     use radroots_storage::projection::{
    117         ProjectionCheckpoint,
    118         batch::{ProjectionDocumentBatch, ProjectionWriteScope},
    119         document_query::{ProjectionDocumentGenerations, ProjectionDocumentQuery},
    120     };
    121     let store = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    122     let source = block_on(store.source_token()).unwrap();
    123     let limits = ProjectionCacheLimits::new(3, 2, 32, 1_800_000).unwrap();
    124     block_on(store.configure_projection_cache(id("cache"), limits)).unwrap();
    125     let first = ProjectionDocumentBatch::new(
    126         ProjectionWriteScope::new(id("cache"), generation(1), source, None).unwrap(),
    127         vec![ProjectionDocument::new("a".into(), vec![7]).unwrap()],
    128     )
    129     .unwrap();
    130     block_on(store.promote_projection_batch(
    131         first,
    132         ProjectionCheckpoint::new(id("cache"), generation(1), None, 1, 1).unwrap(),
    133     ))
    134     .unwrap();
    135     block_on(store.configure_projection_cache(id("second"), limits)).unwrap();
    136     let staged = ProjectionDocumentBatch::new(
    137         ProjectionWriteScope::new(id("second"), generation(2), source, None).unwrap(),
    138         vec![ProjectionDocument::new("b".into(), vec![7]).unwrap()],
    139     )
    140     .unwrap();
    141     block_on(store.put_projection_batch(staged)).unwrap();
    142     let third = ProjectionDocumentBatch::new(
    143         ProjectionWriteScope::new(id("cache"), generation(3), source, Some(generation(1))).unwrap(),
    144         vec![ProjectionDocument::new("c".into(), vec![7]).unwrap()],
    145     )
    146     .unwrap();
    147     assert_eq!(
    148         block_on(store.put_projection_batch(third)),
    149         Err(Error::SpaceInsufficient)
    150     );
    151     let query = ProjectionDocumentQuery::new(
    152         id("cache"),
    153         ProjectionDocumentGenerations::Exact(generation(3)),
    154         1,
    155     )
    156     .unwrap();
    157     assert!(
    158         block_on(store.query_projection_documents(query))
    159             .unwrap()
    160             .records()
    161             .is_empty()
    162     );
    163     assert_eq!(
    164         block_on(store.configure_projection_cache(id("cache"), limits))
    165             .unwrap()
    166             .value_bytes,
    167         2
    168     );
    169     assert_eq!(
    170         block_on(store.put_projection_document(
    171             id("foreign"),
    172             generation(1),
    173             ProjectionDocument::new("too_big".into(), vec![7; 2]).unwrap()
    174         )),
    175         Err(Error::SpaceInsufficient)
    176     );
    177     assert_eq!(
    178         block_on(store.projection_document(id("foreign"), generation(1), "too_big".into()))
    179             .unwrap(),
    180         None
    181     );
    182     assert_eq!(
    183         block_on(store.configure_projection_cache(id("cache"), limits))
    184             .unwrap()
    185             .protected_generations,
    186         2
    187     );
    188 }
    189 
    190 fn cache_batch(
    191     source: EventSourceToken,
    192     active: Option<ProjectionGeneration>,
    193     target: u8,
    194     keys: &[String],
    195 ) -> radroots_storage::projection::batch::ProjectionDocumentBatch {
    196     use radroots_storage::projection::batch::{ProjectionDocumentBatch, ProjectionWriteScope};
    197     ProjectionDocumentBatch::new(
    198         ProjectionWriteScope::new(id("cache"), generation(target), source, active).unwrap(),
    199         keys.iter()
    200             .map(|key| ProjectionDocument::new(key.clone(), vec![7]).unwrap())
    201             .collect(),
    202     )
    203     .unwrap()
    204 }
    205 fn cache_scope(
    206     source: EventSourceToken,
    207     active: Option<ProjectionGeneration>,
    208     target: u8,
    209 ) -> ProjectionCacheScope {
    210     ProjectionCacheScope::new(id("cache"), generation(target), source, active).unwrap()
    211 }
    212 fn cache_checkpoint(target: u8, count: u64) -> radroots_storage::projection::ProjectionCheckpoint {
    213     radroots_storage::projection::ProjectionCheckpoint::new(
    214         id("cache"),
    215         generation(target),
    216         None,
    217         count,
    218         1,
    219     )
    220     .unwrap()
    221 }
    222 fn cache_limits() -> ProjectionCacheLimits {
    223     ProjectionCacheLimits::new(67_108_864, 2, 32, 1_800_000).unwrap()
    224 }
    225 #[test]
    226 fn projection_cache_memory_live_staging_and_63_64_65_retirement_preserve_exact_continuation() {
    227     use radroots_storage::projection::{InvalidationReason, ProjectionInvalidation};
    228     let store = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    229     let source = block_on(store.source_token()).unwrap();
    230     block_on(store.put_projection_document(
    231         id("z_foreign"),
    232         generation(9),
    233         ProjectionDocument::new("keep".into(), vec![8]).unwrap(),
    234     ))
    235     .unwrap();
    236     block_on(store.configure_projection_cache(id("cache"), cache_limits())).unwrap();
    237     block_on(store.promote_projection_batch(
    238         cache_batch(source, None, 1, &["active".into()]),
    239         cache_checkpoint(1, 1),
    240     ))
    241     .unwrap();
    242     assert_eq!(
    243         block_on(store.retire_projection_generation(cache_scope(source, Some(generation(1)), 1))),
    244         Err(Error::ProjectionRevisionConflict)
    245     );
    246     assert_eq!(
    247         block_on(store.collect_projection_cache(cache_scope(source, Some(generation(1)), 1)))
    248             .unwrap()
    249             .removed_rows,
    250         0
    251     );
    252     assert_eq!(
    253         block_on(store.collect_projection_cache(cache_scope(
    254             EventSourceToken::new(source.generation(), 1),
    255             Some(generation(1)),
    256             1
    257         ))),
    258         Err(Error::SourceEpochChanged)
    259     );
    260     assert_eq!(
    261         block_on(store.collect_projection_cache(cache_scope(source, None, 1))),
    262         Err(Error::ProjectionRevisionConflict)
    263     );
    264     for (target, count) in [(2, 63), (3, 64), (4, 65), (5, 0)] {
    265         let keys: Vec<_> = (0..count.min(64))
    266             .map(|number| format!("key.{number:02}"))
    267             .collect();
    268         block_on(store.put_projection_batch(cache_batch(
    269             source,
    270             Some(generation(1)),
    271             target,
    272             &keys,
    273         )))
    274         .unwrap();
    275         if count == 65 {
    276             block_on(store.put_projection_batch(cache_batch(
    277                 source,
    278                 Some(generation(1)),
    279                 target,
    280                 &["key.64".into()],
    281             )))
    282             .unwrap();
    283         }
    284         assert_eq!(
    285             block_on(store.collect_projection_cache(cache_scope(
    286                 source,
    287                 Some(generation(1)),
    288                 target
    289             )))
    290             .unwrap()
    291             .removed_rows,
    292             0
    293         );
    294         block_on(store.retire_projection_generation(cache_scope(
    295             source,
    296             Some(generation(1)),
    297             target,
    298         )))
    299         .unwrap();
    300         let first = block_on(store.collect_projection_cache(cache_scope(
    301             source,
    302             Some(generation(1)),
    303             target,
    304         )))
    305         .unwrap();
    306         assert_eq!(first.removed_rows, (count + 1).min(64));
    307         assert_eq!(first.more, count >= 64);
    308         if first.more {
    309             let second = block_on(store.collect_projection_cache(cache_scope(
    310                 source,
    311                 Some(generation(1)),
    312                 target,
    313             )))
    314             .unwrap();
    315             assert_eq!(second.removed_rows, count - 63);
    316             assert!(!second.more);
    317         }
    318         assert_eq!(
    319             block_on(store.configure_projection_cache(id("cache"), cache_limits()))
    320                 .unwrap()
    321                 .value_bytes,
    322             2
    323         );
    324     }
    325     block_on(
    326         store.invalidate(
    327             ProjectionInvalidation::new(
    328                 id("cache"),
    329                 generation(1),
    330                 generation(6),
    331                 InvalidationReason::OperatorRequested,
    332                 1,
    333             )
    334             .unwrap(),
    335         ),
    336     )
    337     .unwrap();
    338     block_on(store.retire_projection_generation(cache_scope(source, Some(generation(1)), 1)))
    339         .unwrap();
    340     assert_eq!(
    341         block_on(ProjectionStore::status(&store, id("cache"))).unwrap(),
    342         None
    343     );
    344     block_on(store.promote_projection_batch(
    345         cache_batch(source, None, 6, &["successor".into()]),
    346         cache_checkpoint(6, 1),
    347     ))
    348     .unwrap();
    349     block_on(store.collect_projection_cache(cache_scope(source, Some(generation(6)), 1))).unwrap();
    350     assert_eq!(
    351         block_on(store.projection_document(id("z_foreign"), generation(9), "keep".into()))
    352             .unwrap()
    353             .unwrap()
    354             .value(),
    355         &[8]
    356     );
    357 }
    358 
    359 #[test]
    360 fn projection_cache_memory_protected_legacy_pressure_future_and_expiry_collect_without_replay() {
    361     let store = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    362     let source = block_on(store.source_token()).unwrap();
    363     let now = std::time::SystemTime::now()
    364         .duration_since(std::time::UNIX_EPOCH)
    365         .unwrap()
    366         .as_millis() as u64;
    367     block_on(store.checkpoint(cache_checkpoint(1, 0))).unwrap();
    368     for number in 1..=33 {
    369         block_on(
    370             store.put_projection_snapshot(
    371                 ProjectionSnapshot::new(id("cache"), [number; 32], generation(1), now, vec![8])
    372                     .unwrap(),
    373             ),
    374         )
    375         .unwrap();
    376     }
    377     block_on(store.put_projection_snapshot(
    378         ProjectionSnapshot::new(id("z_foreign"), [1; 32], generation(9), now, vec![8]).unwrap(),
    379     ))
    380     .unwrap();
    381     block_on(store.configure_projection_cache(id("cache"), cache_limits())).unwrap();
    382     let first =
    383         block_on(store.collect_projection_cache(cache_scope(source, Some(generation(1)), 1)))
    384             .unwrap();
    385     assert_eq!(first.removed_rows, 2);
    386     assert!(!first.more);
    387     assert_eq!(first.usage.legacy_snapshots, 32);
    388     assert_eq!(
    389         block_on(store.projection_snapshot(id("cache"), [1; 32])).unwrap(),
    390         None
    391     );
    392     assert!(
    393         block_on(store.projection_snapshot(id("cache"), [3; 32]))
    394             .unwrap()
    395             .is_some()
    396     );
    397     let future_store = MemoryStorage::new(source.generation());
    398     block_on(future_store.checkpoint(cache_checkpoint(1, 0))).unwrap();
    399     for number in 1..=65 {
    400         block_on(
    401             future_store.put_projection_snapshot(
    402                 ProjectionSnapshot::new(
    403                     id("cache"),
    404                     [number; 32],
    405                     generation(1),
    406                     now + 3_600_000,
    407                     vec![8],
    408                 )
    409                 .unwrap(),
    410             ),
    411         )
    412         .unwrap();
    413     }
    414     block_on(
    415         future_store.put_projection_snapshot(
    416             ProjectionSnapshot::new(
    417                 id("z_foreign"),
    418                 [1; 32],
    419                 generation(9),
    420                 now + 3_600_000,
    421                 vec![8],
    422             )
    423             .unwrap(),
    424         ),
    425     )
    426     .unwrap();
    427     block_on(future_store.configure_projection_cache(id("cache"), cache_limits())).unwrap();
    428     let first = block_on(future_store.collect_projection_cache(cache_scope(
    429         source,
    430         Some(generation(1)),
    431         1,
    432     )))
    433     .unwrap();
    434     assert_eq!(first.removed_rows, 64);
    435     assert!(first.more);
    436     let second = block_on(future_store.collect_projection_cache(cache_scope(
    437         source,
    438         Some(generation(1)),
    439         1,
    440     )))
    441     .unwrap();
    442     assert_eq!(second.removed_rows, 1);
    443     assert!(!second.more);
    444     assert_eq!(second.usage.legacy_snapshots, 1);
    445     assert_eq!(
    446         block_on(future_store.put_projection_snapshot(
    447             ProjectionSnapshot::new(id("cache"), [1; 32], generation(1), now, vec![8]).unwrap()
    448         )),
    449         Err(Error::ProjectionRevisionConflict)
    450     );
    451     assert!(
    452         block_on(future_store.projection_snapshot(id("z_foreign"), [1; 32]))
    453             .unwrap()
    454             .is_some()
    455     );
    456 }
    457 
    458 async fn legacy_cache_completion(backend: &dyn ProjectionStore, registered: bool, capacity: u64) {
    459     use radroots_storage::projection::{
    460         InvalidationReason, ProjectionCheckpoint, ProjectionHealth, ProjectionInvalidation,
    461         RawSourceDigest, RebuildTicket, RebuildTicketId, RebuildTransition,
    462         batch::{ProjectionDocumentBatch, ProjectionWriteScope},
    463     };
    464     let id = ProjectionId::parse("legacy_cache_completion").unwrap();
    465     let source = EventSourceToken::new(SourceGeneration::new([9; 32]).unwrap(), 0);
    466     let limits = ProjectionCacheLimits::new(67_108_864, capacity, 32, 1_800_000).unwrap();
    467     let checkpoint =
    468         |target| ProjectionCheckpoint::new(id.clone(), generation(target), None, 1, 1).unwrap();
    469     let batch = |target, active| {
    470         ProjectionDocumentBatch::new(
    471             ProjectionWriteScope::new(id.clone(), generation(target), source, active).unwrap(),
    472             vec![ProjectionDocument::new("kept".into(), vec![7]).unwrap()],
    473         )
    474         .unwrap()
    475     };
    476     let scope = |target, active| {
    477         ProjectionCacheScope::new(
    478             id.clone(),
    479             generation(target),
    480             source,
    481             Some(generation(active)),
    482         )
    483         .unwrap()
    484     };
    485     backend
    486         .configure_projection_cache(id.clone(), limits)
    487         .await
    488         .unwrap();
    489     if registered {
    490         backend
    491             .promote_projection_batch(batch(1, None), checkpoint(1))
    492             .await
    493             .unwrap();
    494     } else {
    495         backend.checkpoint(checkpoint(1)).await.unwrap();
    496     }
    497     backend
    498         .put_projection_batch(batch(2, Some(generation(1))))
    499         .await
    500         .unwrap();
    501     let invalidation = ProjectionInvalidation::new(
    502         id.clone(),
    503         generation(1),
    504         generation(3),
    505         InvalidationReason::OperatorRequested,
    506         10,
    507     )
    508     .unwrap();
    509     backend.invalidate(invalidation.clone()).await.unwrap();
    510     let ticket = RebuildTicket::requested(
    511         RebuildTicketId::new([3; 16]).unwrap(),
    512         invalidation,
    513         source.generation(),
    514         None,
    515         RawSourceDigest::new([1; 32]),
    516     )
    517     .unwrap();
    518     backend.request_rebuild(ticket.clone()).await.unwrap();
    519     let running = backend
    520         .transition_rebuild(RebuildTransition::start(
    521             ticket.ticket_id(),
    522             ticket.revision(),
    523             11,
    524         ))
    525         .await
    526         .unwrap();
    527     let before_status = backend.status(id.clone()).await.unwrap();
    528     let before_usage = backend
    529         .configure_projection_cache(id.clone(), limits)
    530         .await
    531         .unwrap();
    532     assert_eq!(before_usage.protected_generations, 2);
    533     let complete =
    534         RebuildTransition::complete(running.ticket_id(), running.revision(), 12, checkpoint(3));
    535     if registered && capacity == 2 {
    536         assert_eq!(
    537             backend.transition_rebuild(complete.clone()).await,
    538             Err(Error::SpaceInsufficient)
    539         );
    540         assert_eq!(backend.status(id.clone()).await.unwrap(), before_status);
    541         assert_eq!(
    542             backend.rebuild(ticket.ticket_id()).await.unwrap(),
    543             Some(running.clone())
    544         );
    545         assert_eq!(
    546             backend
    547                 .configure_projection_cache(id.clone(), limits)
    548                 .await
    549                 .unwrap(),
    550             before_usage
    551         );
    552         assert_eq!(
    553             backend
    554                 .collect_projection_cache(scope(1, 1))
    555                 .await
    556                 .unwrap()
    557                 .removed_rows,
    558             0
    559         );
    560         assert_eq!(
    561             backend
    562                 .collect_projection_cache(scope(2, 1))
    563                 .await
    564                 .unwrap()
    565                 .removed_rows,
    566             0
    567         );
    568         assert_eq!(
    569             backend
    570                 .retire_projection_generation(scope(2, 1))
    571                 .await
    572                 .unwrap()
    573                 .protected_generations,
    574             1
    575         );
    576     }
    577     let completed = backend.transition_rebuild(complete).await.unwrap();
    578     assert_eq!(
    579         backend.rebuild(ticket.ticket_id()).await.unwrap(),
    580         Some(completed)
    581     );
    582     let status = backend.status(id.clone()).await.unwrap().unwrap();
    583     assert_eq!(status.generation(), generation(3));
    584     assert_eq!(status.health(), ProjectionHealth::Ready);
    585     assert_eq!(status.active_rebuild(), None);
    586     assert_eq!(status.checkpoint(), Some(&checkpoint(3)));
    587     let expected = if registered && capacity == 3 { 3 } else { 2 };
    588     assert_eq!(
    589         backend
    590             .configure_projection_cache(id.clone(), limits)
    591             .await
    592             .unwrap()
    593             .protected_generations,
    594         expected
    595     );
    596     assert_eq!(
    597         backend
    598             .collect_projection_cache(scope(3, 3))
    599             .await
    600             .unwrap()
    601             .removed_rows,
    602         0
    603     );
    604     if registered {
    605         assert_eq!(
    606             backend
    607                 .collect_projection_cache(scope(1, 3))
    608                 .await
    609                 .unwrap()
    610                 .removed_rows,
    611             0
    612         );
    613         assert_eq!(
    614             backend
    615                 .retire_projection_generation(scope(1, 3))
    616                 .await
    617                 .unwrap()
    618                 .protected_generations,
    619             expected - 1
    620         );
    621         let collected = backend.collect_projection_cache(scope(1, 3)).await.unwrap();
    622         assert_eq!(collected.removed_rows, 2);
    623         assert_eq!(collected.removed_value_bytes, 1);
    624         assert!(!collected.more);
    625         assert_eq!(collected.usage.protected_generations, expected - 1);
    626     }
    627     if !registered || capacity == 3 {
    628         assert_eq!(
    629             backend
    630                 .collect_projection_cache(scope(2, 3))
    631                 .await
    632                 .unwrap()
    633                 .removed_rows,
    634             0
    635         );
    636         backend
    637             .retire_projection_generation(scope(2, 3))
    638             .await
    639             .unwrap();
    640     }
    641     let collected = backend.collect_projection_cache(scope(2, 3)).await.unwrap();
    642     assert_eq!(collected.removed_rows, 2);
    643     assert_eq!(collected.removed_value_bytes, 1);
    644     assert!(!collected.more);
    645     assert_eq!(collected.usage.protected_generations, 1);
    646     assert_eq!(backend.status(id.clone()).await.unwrap(), Some(status));
    647     assert_eq!(
    648         backend
    649             .collect_projection_cache(scope(3, 3))
    650             .await
    651             .unwrap()
    652             .removed_rows,
    653         0
    654     );
    655 }
    656 
    657 #[test]
    658 fn projection_cache_memory_legacy_completion_preserves_registry_capacity_and_atomic_refusal() {
    659     for (registered, capacity) in [(true, 2), (true, 3), (false, 2)] {
    660         let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    661         block_on(legacy_cache_completion(&memory, registered, capacity));
    662     }
    663 }
    664 
    665 async fn cache_batch_replacement(
    666     backend: &dyn ProjectionStore,
    667     quota: u64,
    668     initial: [usize; 2],
    669     replacement: [usize; 2],
    670     reverse: bool,
    671 ) {
    672     use radroots_storage::projection::batch::{ProjectionDocumentBatch, ProjectionWriteScope};
    673     let id = ProjectionId::parse("batch_replacement").unwrap();
    674     let source = EventSourceToken::new(SourceGeneration::new([9; 32]).unwrap(), 0);
    675     let batch = |sizes: [usize; 2], target, reverse| {
    676         let mut documents = vec![
    677             ProjectionDocument::new("a".into(), vec![1; sizes[0]]).unwrap(),
    678             ProjectionDocument::new("b".into(), vec![2; sizes[1]]).unwrap(),
    679         ];
    680         if reverse {
    681             documents.reverse();
    682         }
    683         ProjectionDocumentBatch::new(
    684             ProjectionWriteScope::new(id.clone(), generation(target), source, None).unwrap(),
    685             documents,
    686         )
    687         .unwrap()
    688     };
    689     backend
    690         .put_projection_batch(batch(initial, 1, false))
    691         .await
    692         .unwrap();
    693     let limits = ProjectionCacheLimits::new(quota, 2, 32, 1_800_000).unwrap();
    694     let before = backend
    695         .configure_projection_cache(id.clone(), limits)
    696         .await
    697         .unwrap();
    698     let status = backend.status(id.clone()).await.unwrap();
    699     let previous_bytes = initial.iter().sum::<usize>() as u64;
    700     let next_bytes = replacement.iter().sum::<usize>() as u64;
    701     let result = backend
    702         .put_projection_batch(batch(replacement, 1, reverse))
    703         .await;
    704     let refused = next_bytes > previous_bytes && next_bytes > quota;
    705     let expected = if refused {
    706         assert_eq!(result, Err(Error::SpaceInsufficient));
    707         assert_eq!(
    708             backend
    709                 .configure_projection_cache(id.clone(), limits)
    710                 .await
    711                 .unwrap(),
    712             before
    713         );
    714         initial
    715     } else {
    716         result.unwrap();
    717         let current = backend
    718             .configure_projection_cache(id.clone(), limits)
    719             .await
    720             .unwrap();
    721         assert_eq!(current.value_bytes, next_bytes);
    722         assert_eq!(current.protected_generations, 1);
    723         replacement
    724     };
    725     assert_eq!(backend.status(id.clone()).await.unwrap(), status);
    726     for (key, size, value) in [("a", expected[0], 1), ("b", expected[1], 2)] {
    727         assert_eq!(
    728             backend
    729                 .projection_document(id.clone(), generation(1), key.into())
    730                 .await
    731                 .unwrap()
    732                 .unwrap()
    733                 .value(),
    734             vec![value; size]
    735         );
    736     }
    737     let current = backend
    738         .configure_projection_cache(id.clone(), limits)
    739         .await
    740         .unwrap();
    741     let over = quota.saturating_sub(current.value_bytes) + 1;
    742     let foreign = ProjectionId::parse("direct_writer").unwrap();
    743     assert_eq!(
    744         backend
    745             .put_projection_document(
    746                 foreign.clone(),
    747                 generation(9),
    748                 ProjectionDocument::new("overflow".into(), vec![9; over as usize]).unwrap()
    749             )
    750             .await,
    751         Err(Error::SpaceInsufficient)
    752     );
    753     assert_eq!(
    754         backend
    755             .projection_document(foreign, generation(9), "overflow".into())
    756             .await
    757             .unwrap(),
    758         None
    759     );
    760     assert_eq!(
    761         backend
    762             .configure_projection_cache(id.clone(), limits)
    763             .await
    764             .unwrap(),
    765         current
    766     );
    767     assert_eq!(
    768         backend
    769             .put_projection_batch(
    770                 ProjectionDocumentBatch::new(
    771                     ProjectionWriteScope::new(
    772                         ProjectionId::parse("new_admission").unwrap(),
    773                         generation(2),
    774                         source,
    775                         None
    776                     )
    777                     .unwrap(),
    778                     vec![ProjectionDocument::new("a".into(), vec![7; over as usize]).unwrap()]
    779                 )
    780                 .unwrap()
    781             )
    782             .await,
    783         Err(Error::SpaceInsufficient)
    784     );
    785     assert_eq!(
    786         backend
    787             .configure_projection_cache(id.clone(), limits)
    788             .await
    789             .unwrap(),
    790         current
    791     );
    792     assert_eq!(
    793         backend
    794             .projection_document(
    795                 ProjectionId::parse("new_admission").unwrap(),
    796                 generation(2),
    797                 "a".into()
    798             )
    799             .await
    800             .unwrap(),
    801         None
    802     );
    803     assert_eq!(
    804         backend
    805             .collect_projection_cache(
    806                 ProjectionCacheScope::new(id.clone(), generation(1), source, None).unwrap()
    807             )
    808             .await
    809             .unwrap()
    810             .removed_rows,
    811         0
    812     );
    813     backend
    814         .retire_projection_generation(
    815             ProjectionCacheScope::new(id.clone(), generation(1), source, None).unwrap(),
    816         )
    817         .await
    818         .unwrap();
    819     let collected = backend
    820         .collect_projection_cache(
    821             ProjectionCacheScope::new(id, generation(1), source, None).unwrap(),
    822         )
    823         .await
    824         .unwrap();
    825     assert_eq!(collected.removed_rows, 3);
    826     assert!(!collected.more);
    827     assert_eq!(collected.usage.value_bytes, 0);
    828     assert_eq!(collected.usage.protected_generations, 0);
    829 }
    830 
    831 #[test]
    832 fn projection_cache_memory_batch_replacement_has_one_aggregate_policy_in_both_orders() {
    833     for (quota, initial, replacement) in [
    834         (4, [2, 2], [3, 1]),
    835         (4, [2, 2], [2, 1]),
    836         (6, [4, 4], [5, 2]),
    837         (6, [4, 4], [5, 4]),
    838         (6, [1, 1], [2, 2]),
    839     ] {
    840         for reverse in [false, true] {
    841             let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    842             block_on(cache_batch_replacement(
    843                 &memory,
    844                 quota,
    845                 initial,
    846                 replacement,
    847                 reverse,
    848             ));
    849         }
    850     }
    851 }