cache_legacy_retirement_tests.rs (11613B)
1 use super::*; 2 use crate::{OpenMode, OpenOptions, Paths}; 3 use radroots_storage::{ 4 EventStore, ProjectionStore, Storage, 5 event::{EventSourceToken, SourceGeneration}, 6 memory::MemoryStorage, 7 projection::{ 8 InvalidationReason, ProjectionCheckpoint, ProjectionDocument, ProjectionGeneration, 9 ProjectionInvalidation, RawSourceDigest, RebuildTicket, RebuildTicketId, RebuildTransition, 10 batch::{ProjectionDocumentBatch, ProjectionWriteScope}, 11 }, 12 }; 13 use tempfile::TempDir; 14 15 fn legacy() -> ProjectionId { 16 ProjectionId::parse("legacy_coordination").unwrap() 17 } 18 fn rows() -> ProjectionId { 19 ProjectionId::parse("derived_rows").unwrap() 20 } 21 fn generation(value: u8) -> ProjectionGeneration { 22 ProjectionGeneration::new([value; 32]).unwrap() 23 } 24 fn limits() -> ProjectionCacheLimits { 25 ProjectionCacheLimits::new(67_108_864, 2, 32, 1_800_000).unwrap() 26 } 27 fn exact(source: EventSourceToken) -> ProjectionCacheScope { 28 ProjectionCacheScope::new(legacy(), generation(9), source, Some(generation(9))).unwrap() 29 } 30 fn invalidation() -> ProjectionInvalidation { 31 ProjectionInvalidation::new( 32 legacy(), 33 generation(9), 34 generation(10), 35 InvalidationReason::ProjectionGenerationChanged, 36 1, 37 ) 38 .unwrap() 39 } 40 async fn open(temp: &TempDir) -> SqliteStorage { 41 SqliteStorage::open( 42 OpenOptions::new( 43 Paths::from_directory(temp.path()).unwrap(), 44 OpenMode::Create, 45 ) 46 .with_source_generation(SourceGeneration::new([9; 32]).unwrap(), 9) 47 .unwrap(), 48 ) 49 .await 50 .unwrap() 51 } 52 async fn seed(backend: &dyn Storage) { 53 backend 54 .put_projection_document( 55 legacy(), 56 generation(9), 57 ProjectionDocument::new("original".into(), b"legacy".to_vec()).unwrap(), 58 ) 59 .await 60 .unwrap(); 61 backend 62 .put_projection_snapshot( 63 ProjectionSnapshot::new(legacy(), [3; 32], generation(9), 1, b"snapshot".to_vec()) 64 .unwrap(), 65 ) 66 .await 67 .unwrap(); 68 backend 69 .checkpoint(ProjectionCheckpoint::new(legacy(), generation(9), None, 1, 1).unwrap()) 70 .await 71 .unwrap(); 72 } 73 fn batch( 74 source: EventSourceToken, 75 target: u8, 76 active: Option<ProjectionGeneration>, 77 ) -> ProjectionDocumentBatch { 78 ProjectionDocumentBatch::new( 79 ProjectionWriteScope::new(rows(), generation(target), source, active).unwrap(), 80 vec![ProjectionDocument::new("row".into(), vec![7]).unwrap()], 81 ) 82 .unwrap() 83 } 84 85 #[tokio::test] 86 async fn projection_cache_legacy_retirement_releases_only_metadata_with_memory_sqlite_parity() { 87 let temp = TempDir::new().unwrap(); 88 let store = open(&temp).await; 89 let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap()); 90 for backend in [&memory as &dyn Storage, &store as &dyn Storage] { 91 seed(backend).await; 92 let source = EventStore::source_token(backend).await.unwrap(); 93 backend 94 .configure_projection_cache(rows(), limits()) 95 .await 96 .unwrap(); 97 assert_eq!( 98 backend.retire_projection_generation(exact(source)).await, 99 Err(Error::ProjectionRevisionConflict) 100 ); 101 assert_eq!( 102 backend 103 .retire_projection_generation(exact(EventSourceToken::new(source.generation(), 1))) 104 .await, 105 Err(Error::SourceEpochChanged) 106 ); 107 assert_eq!( 108 backend 109 .retire_projection_generation( 110 ProjectionCacheScope::new(legacy(), generation(9), source, None).unwrap() 111 ) 112 .await, 113 Err(Error::ProjectionRevisionConflict) 114 ); 115 backend 116 .promote_projection_batch( 117 batch(source, 1, None), 118 ProjectionCheckpoint::new(rows(), generation(1), None, 1, 2).unwrap(), 119 ) 120 .await 121 .unwrap(); 122 let before = backend 123 .configure_projection_cache(rows(), limits()) 124 .await 125 .unwrap(); 126 assert_eq!(before.protected_generations, 2); 127 assert_eq!( 128 backend 129 .put_projection_batch(batch(source, 2, Some(generation(1)))) 130 .await, 131 Err(Error::SpaceInsufficient) 132 ); 133 let status = backend.invalidate(invalidation()).await.unwrap(); 134 let retired = backend 135 .retire_projection_generation(exact(source)) 136 .await 137 .unwrap(); 138 assert_eq!(retired.protected_generations, 1); 139 assert_eq!(retired.value_bytes, before.value_bytes); 140 assert_eq!( 141 ProjectionStore::status(backend, legacy()).await.unwrap(), 142 Some(status.clone()) 143 ); 144 assert_eq!(status.health(), ProjectionHealth::Invalidated); 145 assert_eq!( 146 backend 147 .retire_projection_generation(exact(source)) 148 .await 149 .unwrap(), 150 retired 151 ); 152 assert_eq!( 153 backend.collect_projection_cache(exact(source)).await, 154 Err(Error::ProjectionRevisionConflict) 155 ); 156 assert_eq!( 157 backend 158 .projection_document(legacy(), generation(9), "original".into()) 159 .await 160 .unwrap() 161 .unwrap() 162 .value(), 163 b"legacy" 164 ); 165 assert_eq!( 166 backend 167 .projection_snapshot(legacy(), [3; 32]) 168 .await 169 .unwrap() 170 .unwrap() 171 .value(), 172 b"snapshot" 173 ); 174 assert_eq!( 175 backend 176 .checkpoint(ProjectionCheckpoint::new(legacy(), generation(9), None, 1, 3).unwrap()) 177 .await, 178 Err(Error::ProjectionRevisionConflict) 179 ); 180 backend 181 .put_projection_batch(batch(source, 2, Some(generation(1)))) 182 .await 183 .unwrap(); 184 assert_eq!( 185 backend 186 .configure_projection_cache(rows(), limits()) 187 .await 188 .unwrap() 189 .protected_generations, 190 2 191 ); 192 } 193 } 194 195 #[tokio::test] 196 async fn projection_cache_legacy_retirement_preserves_refusal_and_counters_on_sqlite_reopen() { 197 let temp = TempDir::new().unwrap(); 198 let store = open(&temp).await; 199 seed(&store).await; 200 let source = store.source_token().await.unwrap(); 201 store 202 .configure_projection_cache(rows(), limits()) 203 .await 204 .unwrap(); 205 let expected = store.invalidate(invalidation()).await.unwrap(); 206 let usage = store 207 .retire_projection_generation(exact(source)) 208 .await 209 .unwrap(); 210 assert_eq!(usage.protected_generations, 0); 211 drop(store); 212 let reopened = open(&temp).await; 213 assert_eq!( 214 ProjectionStore::status(&reopened, legacy()).await.unwrap(), 215 Some(expected) 216 ); 217 assert_eq!( 218 reopened 219 .configure_projection_cache(rows(), limits()) 220 .await 221 .unwrap(), 222 usage 223 ); 224 assert_eq!( 225 reopened 226 .retire_projection_generation(exact(source)) 227 .await 228 .unwrap(), 229 usage 230 ); 231 assert_eq!( 232 reopened.collect_projection_cache(exact(source)).await, 233 Err(Error::ProjectionRevisionConflict) 234 ); 235 assert_eq!( 236 reopened 237 .projection_document(legacy(), generation(9), "original".into()) 238 .await 239 .unwrap() 240 .unwrap() 241 .value(), 242 b"legacy" 243 ); 244 reopened 245 .promote_projection_batch( 246 batch(source, 1, None), 247 ProjectionCheckpoint::new(rows(), generation(1), None, 1, 2).unwrap(), 248 ) 249 .await 250 .unwrap(); 251 reopened 252 .put_projection_batch(batch(source, 2, Some(generation(1)))) 253 .await 254 .unwrap(); 255 assert_eq!( 256 reopened 257 .configure_projection_cache(rows(), limits()) 258 .await 259 .unwrap() 260 .protected_generations, 261 2 262 ); 263 } 264 265 #[tokio::test] 266 async fn projection_cache_legacy_retirement_refuses_requested_and_running_rebuilds() { 267 let temp = TempDir::new().unwrap(); 268 let store = open(&temp).await; 269 let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap()); 270 for backend in [&memory as &dyn Storage, &store as &dyn Storage] { 271 seed(backend).await; 272 let source = EventStore::source_token(backend).await.unwrap(); 273 backend.invalidate(invalidation()).await.unwrap(); 274 let requested = backend 275 .request_rebuild( 276 RebuildTicket::requested( 277 RebuildTicketId::new([4; 16]).unwrap(), 278 invalidation(), 279 source.generation(), 280 None, 281 RawSourceDigest::new([1; 32]), 282 ) 283 .unwrap(), 284 ) 285 .await 286 .unwrap(); 287 let before = ProjectionStore::status(backend, legacy()).await.unwrap(); 288 assert_eq!( 289 backend.retire_projection_generation(exact(source)).await, 290 Err(Error::ProjectionRevisionConflict) 291 ); 292 assert_eq!( 293 ProjectionStore::status(backend, legacy()).await.unwrap(), 294 before 295 ); 296 backend 297 .transition_rebuild(RebuildTransition::start( 298 requested.ticket_id(), 299 requested.revision(), 300 2, 301 )) 302 .await 303 .unwrap(); 304 let before = ProjectionStore::status(backend, legacy()).await.unwrap(); 305 assert_eq!( 306 backend.retire_projection_generation(exact(source)).await, 307 Err(Error::ProjectionRevisionConflict) 308 ); 309 assert_eq!( 310 ProjectionStore::status(backend, legacy()).await.unwrap(), 311 before 312 ); 313 } 314 } 315 316 #[tokio::test] 317 async fn projection_cache_legacy_retirement_refuses_live_unconfigured_batch_registration() { 318 let temp = TempDir::new().unwrap(); 319 let store = open(&temp).await; 320 let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap()); 321 for backend in [&memory as &dyn Storage, &store as &dyn Storage] { 322 let source = EventStore::source_token(backend).await.unwrap(); 323 backend 324 .promote_projection_batch( 325 batch(source, 1, None), 326 ProjectionCheckpoint::new(rows(), generation(1), None, 1, 2).unwrap(), 327 ) 328 .await 329 .unwrap(); 330 backend 331 .invalidate( 332 ProjectionInvalidation::new( 333 rows(), 334 generation(1), 335 generation(2), 336 InvalidationReason::OperatorRequested, 337 3, 338 ) 339 .unwrap(), 340 ) 341 .await 342 .unwrap(); 343 let scope = 344 ProjectionCacheScope::new(rows(), generation(1), source, Some(generation(1))).unwrap(); 345 assert_eq!( 346 backend.retire_projection_generation(scope).await, 347 Err(Error::ProjectionRevisionConflict) 348 ); 349 assert_eq!( 350 backend 351 .projection_document(rows(), generation(1), "row".into()) 352 .await 353 .unwrap() 354 .unwrap() 355 .value(), 356 &[7] 357 ); 358 } 359 }