aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-kv-store / tests / host_fs_tests.rs
| 1 | #![cfg(feature = "std")] |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | // The publication protocol, fault hooks and cache behaviour are written from |
| 18 | // ADR 0003 sections 2.4, 3.4 and 7.4. LMCache fs_l2_adapter.py (4d69cdc4, |
| 19 | // Apache-2.0, Copyright LMCache contributors) informed two ideas only: a |
| 20 | // completed object is published by writing a temporary file and renaming it, |
| 21 | // and a store of a key that already exists on disk is skipped. No code or |
| 22 | // test text was copied. See THIRD_PARTY.md and ADR 0003 context item 24. |
| 23 | |
| 24 | //! The host filesystem `KvStore` adapter (cut C4, ADR 0003 sections 2.4, 3.4, |
| 25 | //! 7.4): real temporary directories, no mocks of the filesystem. |
| 26 | |
| 27 | mod common; |
| 28 | #[path = "common/host_fs_rig.rs"] |
| 29 | mod rig; |
| 30 | |
| 31 | use aien_kv_store::host_fs::{Boundary, FaultAction, HostFsStore, WorkerMode, EIO, ENOSPC}; |
| 32 | use aien_kv_store::index::{Index, IndexEntry, FLAG_FOREIGN, INDEX_ENTRY_BYTES}; |
| 33 | use aien_kv_store::{ |
| 34 | CancelOutcome, Incarnation, JobStage, KvStore, PublishOutcome, StoreState, Terminal, |
| 35 | }; |
| 36 | use aien_platform::PlatformError; |
| 37 | use rig::*; |
| 38 | use std::sync::{Arc, OnceLock}; |
| 39 | use std::time::{Duration, Instant}; |
| 40 | |
| 41 | fn failed(t: &Terminal) -> &PlatformError { |
| 42 | match t { |
| 43 | Terminal::Failed(e) => e, |
| 44 | other => panic!("expected a failed terminal, got {other:?}"), |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn is_hw(e: &PlatformError) -> bool { |
| 49 | matches!(e, PlatformError::HardwareFault(_)) |
| 50 | } |
| 51 | |
| 52 | /// Load `key` and assert it returns exactly `expected`. |
| 53 | fn assert_loads(fx: &Fx, s: &HostFsStore, key: aien_kv_store::ChunkKey, n: u32, tokens: u32) { |
| 54 | let (_, header) = fx.chunk(n, tokens); |
| 55 | let (t, mem) = fx.load(s, key, &header); |
| 56 | assert_eq!(t.unwrap(), Terminal::Done(JobStage::Completed)); |
| 57 | assert_eq!(mem.bytes(), expected_payload(n, tokens)); |
| 58 | } |
| 59 | |
| 60 | // ---- first open ------------------------------------------------------------ |
| 61 | |
| 62 | #[test] |
| 63 | fn first_open_precreates_256_shards_and_store_id_with_parent_fsync() { |
| 64 | let fx = Fx::new(); |
| 65 | let s = fx.open_raw(|_| {}); |
| 66 | let st = s.stats(); |
| 67 | for b in 0..=255u8 { |
| 68 | assert!( |
| 69 | fx.root().join("objects").join(format!("{b:02x}")).is_dir(), |
| 70 | "shard {b:02x}" |
| 71 | ); |
| 72 | } |
| 73 | assert!(fx.root().join("tmp").is_dir()); |
| 74 | let id = std::fs::read(fx.root().join("STORE_ID")).unwrap(); |
| 75 | assert_eq!(id.len(), 16); |
| 76 | assert_eq!(s.store_id(), Some(id.clone().try_into().unwrap())); |
| 77 | assert!(st.mkdirs >= 256, "mkdir each shard: {}", st.mkdirs); |
| 78 | assert!(st.dir_fsyncs >= 2, "fsync(objects) and fsync(root)"); |
| 79 | assert!( |
| 80 | tmp_files(fx.root()).is_empty(), |
| 81 | "the STORE_ID temp was renamed" |
| 82 | ); |
| 83 | recover_all(&s); |
| 84 | |
| 85 | // A publication never creates a directory. |
| 86 | let before = s.stats(); |
| 87 | fx.put_full(&s, 1); |
| 88 | let after = s.stats(); |
| 89 | assert_eq!(after.mkdirs, before.mkdirs); |
| 90 | drop(s); |
| 91 | |
| 92 | // Reopen: same STORE_ID, nothing recreated. |
| 93 | let s2 = fx.open_raw(|_| {}); |
| 94 | assert_eq!(s2.store_id(), Some(id.try_into().unwrap())); |
| 95 | assert_eq!(s2.stats().mkdirs, 0); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn missing_shard_dir_is_recreated_at_open_with_a_parent_fsync() { |
| 100 | let fx = Fx::new(); |
| 101 | fx.prime(); |
| 102 | std::fs::remove_dir(fx.root().join("objects").join("ab")).unwrap(); |
| 103 | let s = fx.open_raw(|_| {}); |
| 104 | assert!(fx.root().join("objects").join("ab").is_dir()); |
| 105 | let st = s.stats(); |
| 106 | assert_eq!(st.mkdirs, 1); |
| 107 | assert!(st.dir_fsyncs >= 1); |
| 108 | assert_ne!(s.capacity().state, StoreState::Failed); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn malformed_store_id_opens_failed_and_serves_nothing() { |
| 113 | let fx = Fx::new(); |
| 114 | fx.prime(); |
| 115 | std::fs::write(fx.root().join("STORE_ID"), b"abc").unwrap(); |
| 116 | let s = fx.open_raw(|_| {}); |
| 117 | assert_eq!(s.capacity().state, StoreState::Failed); |
| 118 | let (_, h) = fx.chunk(1, 8); |
| 119 | assert_eq!(s.lookup(&[h.chunk_key]), vec![None]); |
| 120 | let (job, _) = fx.submit(&s, 1, 8); |
| 121 | assert_eq!(job.unwrap_err(), PlatformError::DeviceUnavailable); |
| 122 | // A caller looping on recovery must stop: Failed is reported, never done. |
| 123 | let p = s.recover_step(1); |
| 124 | assert!(p.failed && !p.done); |
| 125 | assert_eq!(s.capacity().state, StoreState::Failed); |
| 126 | } |
| 127 | |
| 128 | // ---- A1 / A7: adapter recreation ----------------------------------------- |
| 129 | |
| 130 | #[test] |
| 131 | fn store_then_recreate_adapter_serves_same_bytes() { |
| 132 | // A7 row: ADAPTER RECREATION (same process, new adapter on the same directory). |
| 133 | // It is not a process restart, a clean reboot or a power loss. |
| 134 | let fx = Fx::new(); |
| 135 | let s = fx.open(); |
| 136 | let id = s.store_id().unwrap(); |
| 137 | let a = fx.put(&s, 1, 8); |
| 138 | let b = fx.put(&s, 2, 6); // 2 blocks, 2 real rows in the last |
| 139 | let c = fx.put(&s, 3, 3); // 1 block, 3 real rows |
| 140 | for p in [&a, &b, &c] { |
| 141 | assert!(p.job.is_ok()); |
| 142 | } |
| 143 | let cap = s.capacity(); |
| 144 | assert_eq!(cap.objects, 3); |
| 145 | assert_eq!(cap.used_bytes, OBJ + OBJ + OBJ1); |
| 146 | let report = s.shutdown(100); |
| 147 | assert!(report.unfinished.is_empty()); |
| 148 | drop(s); |
| 149 | |
| 150 | let s2 = fx.open(); |
| 151 | assert_eq!(s2.store_id(), Some(id)); |
| 152 | let cap2 = s2.capacity(); |
| 153 | assert_eq!(cap2.state, StoreState::Open); |
| 154 | assert_eq!(cap2.used_bytes, cap.used_bytes); |
| 155 | assert_eq!(cap2.objects, 3); |
| 156 | assert_eq!( |
| 157 | s2.lookup(&[a.key, b.key, c.key]), |
| 158 | vec![Some(FULL_PAYLOAD), Some(FULL_PAYLOAD), Some(BS as u64)] |
| 159 | ); |
| 160 | assert_loads(&fx, &s2, a.key, 1, 8); |
| 161 | assert_loads(&fx, &s2, b.key, 2, 6); |
| 162 | assert_loads(&fx, &s2, c.key, 3, 3); |
| 163 | s2.audit().unwrap(); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn publication_order_is_write_fsync_rename_dir_fsync_then_index() { |
| 168 | let fx = Fx::new(); |
| 169 | let s = fx.open(); |
| 170 | let before = s.stats(); |
| 171 | let p = fx.put_full(&s, 1); |
| 172 | assert_eq!( |
| 173 | fx.faults.seen(), |
| 174 | vec![ |
| 175 | Boundary::P1Write, |
| 176 | Boundary::P2FsyncFile, |
| 177 | Boundary::P3Rename, |
| 178 | Boundary::P4FsyncDir, |
| 179 | Boundary::P5IndexInsert |
| 180 | ] |
| 181 | ); |
| 182 | let st = s.stats(); |
| 183 | assert_eq!(st.file_fsyncs - before.file_fsyncs, 1); |
| 184 | assert_eq!(st.dir_fsyncs - before.dir_fsyncs, 1); |
| 185 | assert_eq!(st.renames - before.renames, 1); |
| 186 | assert!(st.write_bytes - before.write_bytes >= OBJ); |
| 187 | assert!(tmp_files(fx.root()).is_empty()); |
| 188 | assert!(fx.object_file(&p.key).is_file()); |
| 189 | let job = p.job.unwrap(); |
| 190 | // Completed (progress), then the one terminal: Durable, published and durable together. |
| 191 | let mine: Vec<_> = p.completions.iter().filter(|c| c.job == job).collect(); |
| 192 | assert_eq!(mine.len(), 2, "{mine:?}"); |
| 193 | assert_eq!(mine[0].stage, JobStage::Completed); |
| 194 | assert!(mine[0].terminal.is_none()); |
| 195 | assert_eq!(mine[1].terminal, Some(Terminal::Done(JobStage::Durable))); |
| 196 | assert_eq!(mine[1].publish, PublishOutcome::Durable); |
| 197 | assert_eq!(mine[1].bytes, FULL_PAYLOAD); |
| 198 | } |
| 199 | |
| 200 | // ---- lookup and submit do no I/O ------------------------------------------- |
| 201 | |
| 202 | #[test] |
| 203 | fn lookup_is_index_only_no_io() { |
| 204 | let fx = Fx::new(); |
| 205 | let s = fx.open(); |
| 206 | let a = fx.put_full(&s, 1); |
| 207 | let (miss, _) = fx.chunk(99, 8); |
| 208 | let before = s.stats(); |
| 209 | // Even with the files gone from under it, the index answers. |
| 210 | std::fs::remove_file(fx.object_file(&a.key)).unwrap(); |
| 211 | for _ in 0..1000 { |
| 212 | assert_eq!(s.lookup(&[a.key, miss]), vec![Some(FULL_PAYLOAD), None]); |
| 213 | } |
| 214 | let _ = s.capacity(); |
| 215 | let _ = s.poll(10); |
| 216 | assert_eq!(s.stats().io_ops, before.io_ops); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn submit_store_performs_no_io() { |
| 221 | let fx = Fx::new(); |
| 222 | let s = fx.open(); |
| 223 | let before = s.stats(); |
| 224 | let (job, _) = fx.submit(&s, 1, 8); |
| 225 | job.unwrap(); |
| 226 | assert_eq!(s.stats().io_ops, before.io_ops, "submit touches no file"); |
| 227 | assert!(tmp_files(fx.root()).is_empty()); |
| 228 | s.run_until_idle(); |
| 229 | assert!(s.stats().io_ops > before.io_ops); |
| 230 | } |
| 231 | |
| 232 | // ---- reservation (A6) ------------------------------------------------------ |
| 233 | |
| 234 | #[test] |
| 235 | fn inflight_temp_bytes_are_reserved() { |
| 236 | let fx = Fx::new(); |
| 237 | let cell: StoreCell = Arc::new(OnceLock::new()); |
| 238 | let seen = log(); |
| 239 | let (c2, s2) = (cell.clone(), seen.clone()); |
| 240 | let faults = fx.faults.clone(); |
| 241 | let mut cfg = fx.config(); |
| 242 | cfg.quota_bytes = OBJ + OBJ / 2; |
| 243 | cfg.faults = Some(Arc::new(CallbackHook { |
| 244 | scripted: faults, |
| 245 | f: Box::new(move |b, _| { |
| 246 | if b == Boundary::P2FsyncFile { |
| 247 | // The temp file is fully written, the object is not published yet. |
| 248 | let store = c2.get().expect("store"); |
| 249 | let cap = store.capacity(); |
| 250 | let tmp = tmp_files(store.root()); |
| 251 | s2.lock().unwrap().push(format!( |
| 252 | "reserved={} used={} tmp={} tmp_len={}", |
| 253 | cap.reserved_bytes, |
| 254 | cap.used_bytes, |
| 255 | tmp.len(), |
| 256 | tmp.first() |
| 257 | .map_or(0, |p| std::fs::metadata(p).unwrap().len()) |
| 258 | )); |
| 259 | } |
| 260 | None |
| 261 | }), |
| 262 | })); |
| 263 | let store = Arc::new(HostFsStore::open(cfg).unwrap()); |
| 264 | cell.set(store.clone()).ok(); |
| 265 | recover_all(&store); |
| 266 | |
| 267 | let (j1, _) = fx.submit(&store, 1, 8); |
| 268 | j1.unwrap(); |
| 269 | assert_eq!(store.capacity().reserved_bytes, OBJ, "reserved at submit"); |
| 270 | assert_eq!(store.capacity().inflight_bytes, OBJ); |
| 271 | assert_eq!(store.capacity().park_ops, 1); |
| 272 | // No overcommit: a second store beside the first does not fit the quota. |
| 273 | let (j2, _) = fx.submit(&store, 2, 8); |
| 274 | assert_eq!(j2.unwrap_err(), PlatformError::OutOfMemory); |
| 275 | assert_eq!( |
| 276 | store.capacity().reserved_bytes, |
| 277 | OBJ, |
| 278 | "refusal reserves nothing" |
| 279 | ); |
| 280 | assert!(tmp_files(store.root()).is_empty(), "submit wrote no temp"); |
| 281 | |
| 282 | store.run_until_idle(); |
| 283 | let log = seen.lock().unwrap().clone(); |
| 284 | assert_eq!(log.len(), 1, "{log:?}"); |
| 285 | assert_eq!( |
| 286 | log[0], |
| 287 | format!("reserved={OBJ} used=0 tmp=1 tmp_len={OBJ}"), |
| 288 | "while the temp exists, its bytes are reserved and not yet used" |
| 289 | ); |
| 290 | let cap = store.capacity(); |
| 291 | assert_eq!(cap.reserved_bytes, 0); |
| 292 | assert_eq!(cap.used_bytes, OBJ); |
| 293 | assert_eq!(cap.inflight_bytes, 0); |
| 294 | store.audit().unwrap(); |
| 295 | } |
| 296 | |
| 297 | // ---- failed writes keep the last usable object (A4) ------------------------- |
| 298 | |
| 299 | #[test] |
| 300 | fn enospc_mid_write_leaves_last_object_intact() { |
| 301 | let fx = Fx::new(); |
| 302 | let s = fx.open(); |
| 303 | let a = fx.put_full(&s, 1); |
| 304 | let used = s.capacity().used_bytes; |
| 305 | fx.faults.arm(Boundary::P1Write, FaultAction::Fail(ENOSPC)); |
| 306 | let b = fx.put_full(&s, 2); |
| 307 | let t = terminal_of(&b.completions, b.job.unwrap()); |
| 308 | assert_eq!(*failed(&t), PlatformError::OutOfMemory); |
| 309 | assert_eq!(s.lookup(&[b.key]), vec![None]); |
| 310 | assert!(!fx.object_file(&b.key).exists()); |
| 311 | assert!(tmp_files(fx.root()).is_empty(), "partial temp unlinked"); |
| 312 | let cap = s.capacity(); |
| 313 | assert_eq!(cap.used_bytes, used); |
| 314 | assert_eq!(cap.reserved_bytes, 0); |
| 315 | assert_eq!(cap.state, StoreState::Open, "ENOSPC alone does not degrade"); |
| 316 | assert_eq!(s.stats().disk_full_total, 1); |
| 317 | assert_eq!(s.lookup(&[a.key]), vec![Some(FULL_PAYLOAD)]); |
| 318 | assert_loads(&fx, &s, a.key, 1, 8); |
| 319 | // The fault was one-shot: the same store now succeeds. |
| 320 | let b2 = fx.put_full(&s, 2); |
| 321 | assert_eq!( |
| 322 | terminal_of(&b2.completions, b2.job.unwrap()), |
| 323 | Terminal::Done(JobStage::Durable) |
| 324 | ); |
| 325 | s.audit().unwrap(); |
| 326 | } |
| 327 | |
| 328 | #[test] |
| 329 | fn torn_write_leaves_no_partial_object_and_keeps_last() { |
| 330 | let fx = Fx::new(); |
| 331 | let s = fx.open(); |
| 332 | let a = fx.put_full(&s, 1); |
| 333 | fx.faults.arm( |
| 334 | Boundary::P1Write, |
| 335 | FaultAction::TornWrite { |
| 336 | after_bytes: BS as u64 + 10, |
| 337 | crash: false, |
| 338 | }, |
| 339 | ); |
| 340 | let b = fx.put_full(&s, 2); |
| 341 | let t = terminal_of(&b.completions, b.job.unwrap()); |
| 342 | assert!(is_hw(failed(&t)), "{t:?}"); |
| 343 | assert!(tmp_files(fx.root()).is_empty()); |
| 344 | assert_eq!(object_files(fx.root()).len(), 1); |
| 345 | assert_loads(&fx, &s, a.key, 1, 8); |
| 346 | } |
| 347 | |
| 348 | #[test] |
| 349 | fn fsync_failure_fails_job_and_publishes_nothing() { |
| 350 | let fx = Fx::new(); |
| 351 | let s = fx.open(); |
| 352 | let a = fx.put_full(&s, 1); |
| 353 | let renames = s.stats().renames; |
| 354 | fx.faults.arm(Boundary::P2FsyncFile, FaultAction::Fail(EIO)); |
| 355 | let b = fx.put_full(&s, 2); |
| 356 | let c = completion_of(&b.completions, b.job.unwrap()); |
| 357 | assert!(is_hw(failed(c.terminal.as_ref().unwrap()))); |
| 358 | assert_eq!(c.publish, PublishOutcome::NotApplicable); |
| 359 | assert_eq!(s.lookup(&[b.key]), vec![None]); |
| 360 | assert!(!fx.object_file(&b.key).exists()); |
| 361 | assert!( |
| 362 | tmp_files(fx.root()).is_empty(), |
| 363 | "temp unlinked, fd never retried" |
| 364 | ); |
| 365 | assert_eq!(s.stats().renames, renames, "no rename after a failed fsync"); |
| 366 | // Degraded: loads are served, no new stores. |
| 367 | assert_eq!(s.capacity().state, StoreState::Degraded); |
| 368 | assert_loads(&fx, &s, a.key, 1, 8); |
| 369 | let (job, _) = fx.submit(&s, 3, 8); |
| 370 | assert_eq!(job.unwrap_err(), PlatformError::DeviceUnavailable); |
| 371 | assert_eq!(s.capacity().reserved_bytes, 0); |
| 372 | } |
| 373 | |
| 374 | #[test] |
| 375 | fn rename_failure_unlinks_temp_and_publishes_nothing() { |
| 376 | let fx = Fx::new(); |
| 377 | let s = fx.open(); |
| 378 | let a = fx.put_full(&s, 1); |
| 379 | fx.faults.arm(Boundary::P3Rename, FaultAction::Fail(EIO)); |
| 380 | let b = fx.put_full(&s, 2); |
| 381 | let t = terminal_of(&b.completions, b.job.unwrap()); |
| 382 | assert!(is_hw(failed(&t))); |
| 383 | assert_eq!(s.lookup(&[b.key]), vec![None]); |
| 384 | assert!(tmp_files(fx.root()).is_empty()); |
| 385 | assert!(!fx.object_file(&b.key).exists()); |
| 386 | assert_eq!(s.capacity().state, StoreState::Open); |
| 387 | assert_eq!(s.capacity().reserved_bytes, 0); |
| 388 | assert_loads(&fx, &s, a.key, 1, 8); |
| 389 | } |
| 390 | |
| 391 | #[test] |
| 392 | fn dir_fsync_failure_after_rename_is_published_indeterminate() { |
| 393 | let fx = Fx::new(); |
| 394 | let s = fx.open(); |
| 395 | fx.faults.arm(Boundary::P4FsyncDir, FaultAction::Fail(EIO)); |
| 396 | let b = fx.put_full(&s, 2); |
| 397 | let c = completion_of(&b.completions, b.job.unwrap()); |
| 398 | assert!(is_hw(failed(c.terminal.as_ref().unwrap()))); |
| 399 | assert_eq!(c.publish, PublishOutcome::Indeterminate); |
| 400 | assert_eq!(c.stage, JobStage::Published); |
| 401 | // The object is whole, indexed and served; only its power-loss durability is unknown. |
| 402 | assert_eq!(s.lookup(&[b.key]), vec![Some(FULL_PAYLOAD)]); |
| 403 | assert!(fx.object_file(&b.key).is_file()); |
| 404 | assert_loads(&fx, &s, b.key, 2, 8); |
| 405 | assert_eq!(s.capacity().state, StoreState::Degraded); |
| 406 | assert_eq!(s.capacity().used_bytes, OBJ); |
| 407 | assert_eq!(s.stats().indeterminate_total, 1); |
| 408 | let (job, _) = fx.submit(&s, 3, 8); |
| 409 | assert_eq!(job.unwrap_err(), PlatformError::DeviceUnavailable); |
| 410 | drop(s); |
| 411 | // Recovery after a restart indexes it like any other (it passed P1 to P3). |
| 412 | let s2 = fx.open(); |
| 413 | assert_eq!(s2.lookup(&[b.key]), vec![Some(FULL_PAYLOAD)]); |
| 414 | assert_loads(&fx, &s2, b.key, 2, 8); |
| 415 | } |
| 416 | |
| 417 | #[test] |
| 418 | fn fault_at_each_publication_boundary_leaves_consistent_outcome() { |
| 419 | // ADR 2.4 table: (boundary, object file exists, indexed, temp left, publish outcome, state). |
| 420 | struct Case { |
| 421 | at: Boundary, |
| 422 | object: bool, |
| 423 | publish: PublishOutcome, |
| 424 | state: StoreState, |
| 425 | } |
| 426 | let cases = [ |
| 427 | Case { |
| 428 | at: Boundary::P1Write, |
| 429 | object: false, |
| 430 | publish: PublishOutcome::NotApplicable, |
| 431 | state: StoreState::Open, |
| 432 | }, |
| 433 | Case { |
| 434 | at: Boundary::P2FsyncFile, |
| 435 | object: false, |
| 436 | publish: PublishOutcome::NotApplicable, |
| 437 | state: StoreState::Degraded, |
| 438 | }, |
| 439 | Case { |
| 440 | at: Boundary::P3Rename, |
| 441 | object: false, |
| 442 | publish: PublishOutcome::NotApplicable, |
| 443 | state: StoreState::Open, |
| 444 | }, |
| 445 | Case { |
| 446 | at: Boundary::P4FsyncDir, |
| 447 | object: true, |
| 448 | publish: PublishOutcome::Indeterminate, |
| 449 | state: StoreState::Degraded, |
| 450 | }, |
| 451 | ]; |
| 452 | for case in cases { |
| 453 | let name = format!("{:?}", case.at); |
| 454 | let fx = Fx::new(); |
| 455 | let s = fx.open(); |
| 456 | let a = fx.put_full(&s, 1); // the last usable object |
| 457 | let used = s.capacity().used_bytes; |
| 458 | fx.faults.arm(case.at, FaultAction::Fail(EIO)); |
| 459 | let b = fx.put_full(&s, 2); |
| 460 | let job = b.job.unwrap(); |
| 461 | let c = completion_of(&b.completions, job); |
| 462 | assert!(is_hw(failed(c.terminal.as_ref().unwrap())), "{name}"); |
| 463 | assert_eq!(c.publish, case.publish, "{name}"); |
| 464 | assert_eq!(fx.object_file(&b.key).exists(), case.object, "{name}"); |
| 465 | assert_eq!( |
| 466 | s.lookup(&[b.key]), |
| 467 | vec![case.object.then_some(FULL_PAYLOAD)], |
| 468 | "{name}" |
| 469 | ); |
| 470 | assert!(tmp_files(fx.root()).is_empty(), "{name}: temp unlinked"); |
| 471 | let cap = s.capacity(); |
| 472 | assert_eq!(cap.state, case.state, "{name}"); |
| 473 | assert_eq!(cap.reserved_bytes, 0, "{name}"); |
| 474 | assert_eq!( |
| 475 | cap.used_bytes, |
| 476 | used + if case.object { OBJ } else { 0 }, |
| 477 | "{name}" |
| 478 | ); |
| 479 | assert_eq!(fx.faults.armed(case.at), 0, "{name}: the fault fired"); |
| 480 | assert_loads(&fx, &s, a.key, 1, 8); |
| 481 | s.audit().unwrap(); |
| 482 | } |
| 483 | |
| 484 | // P5 (index insert) cannot fail: capacity was reserved at submit. A fault armed |
| 485 | // there is not an I/O failure and changes nothing. |
| 486 | let fx = Fx::new(); |
| 487 | let s = fx.open(); |
| 488 | fx.faults |
| 489 | .arm(Boundary::P5IndexInsert, FaultAction::Fail(EIO)); |
| 490 | let b = fx.put_full(&s, 2); |
| 491 | assert_eq!( |
| 492 | terminal_of(&b.completions, b.job.unwrap()), |
| 493 | Terminal::Done(JobStage::Durable) |
| 494 | ); |
| 495 | assert_eq!(s.lookup(&[b.key]), vec![Some(FULL_PAYLOAD)]); |
| 496 | } |
| 497 | |
| 498 | #[test] |
| 499 | fn crash_at_each_publication_boundary_recovers_consistently() { |
| 500 | // The process dies at the boundary: no cleanup, no completion. A new adapter |
| 501 | // on the same directory must find the last usable object, never a partial |
| 502 | // one, and no temp. |
| 503 | struct Case { |
| 504 | at: Boundary, |
| 505 | action: FaultAction, |
| 506 | survives: bool, |
| 507 | temp_left: bool, |
| 508 | } |
| 509 | let cases = [ |
| 510 | Case { |
| 511 | at: Boundary::P1Write, |
| 512 | action: FaultAction::Crash, |
| 513 | survives: false, |
| 514 | temp_left: true, |
| 515 | }, |
| 516 | Case { |
| 517 | at: Boundary::P1Write, |
| 518 | action: FaultAction::TornWrite { |
| 519 | after_bytes: BS as u64 + 1, |
| 520 | crash: true, |
| 521 | }, |
| 522 | survives: false, |
| 523 | temp_left: true, |
| 524 | }, |
| 525 | Case { |
| 526 | at: Boundary::P2FsyncFile, |
| 527 | action: FaultAction::Crash, |
| 528 | survives: false, |
| 529 | temp_left: true, |
| 530 | }, |
| 531 | Case { |
| 532 | at: Boundary::P3Rename, |
| 533 | action: FaultAction::Crash, |
| 534 | survives: false, |
| 535 | temp_left: true, |
| 536 | }, |
| 537 | Case { |
| 538 | at: Boundary::P4FsyncDir, |
| 539 | action: FaultAction::Crash, |
| 540 | survives: true, |
| 541 | temp_left: false, |
| 542 | }, |
| 543 | Case { |
| 544 | at: Boundary::P5IndexInsert, |
| 545 | action: FaultAction::Crash, |
| 546 | survives: true, |
| 547 | temp_left: false, |
| 548 | }, |
| 549 | ]; |
| 550 | for case in cases { |
| 551 | let name = format!("{:?}/{:?}", case.at, case.action); |
| 552 | let fx = Fx::new(); |
| 553 | let s = fx.open(); |
| 554 | let a = fx.put_full(&s, 1); |
| 555 | fx.faults.arm(case.at, case.action); |
| 556 | let (key_b, _) = fx.chunk(2, 8); |
| 557 | let (job, _) = fx.submit(&s, 2, 8); |
| 558 | let job = job.unwrap(); |
| 559 | s.run_until_idle(); |
| 560 | let polled = drain(&s); |
| 561 | assert!( |
| 562 | polled.iter().all(|c| c.job != job || c.terminal.is_none()), |
| 563 | "{name}: a dead process reports no terminal" |
| 564 | ); |
| 565 | assert_eq!( |
| 566 | tmp_files(fx.root()).len(), |
| 567 | usize::from(case.temp_left), |
| 568 | "{name}" |
| 569 | ); |
| 570 | drop(s); |
| 571 | |
| 572 | let s2 = fx.open_raw(|_| {}); |
| 573 | let p = recover_all(&s2); |
| 574 | assert_eq!(p.tmp_deleted, u64::from(case.temp_left), "{name}"); |
| 575 | assert_eq!(p.corrupt_deleted, 0, "{name}"); |
| 576 | assert!(tmp_files(fx.root()).is_empty(), "{name}"); |
| 577 | assert_loads(&fx, &s2, a.key, 1, 8); |
| 578 | assert_eq!( |
| 579 | s2.lookup(&[key_b]), |
| 580 | vec![case.survives.then_some(FULL_PAYLOAD)], |
| 581 | "{name}" |
| 582 | ); |
| 583 | if case.survives { |
| 584 | assert_loads(&fx, &s2, key_b, 2, 8); |
| 585 | } |
| 586 | assert_eq!( |
| 587 | s2.capacity().used_bytes, |
| 588 | OBJ + if case.survives { OBJ } else { 0 }, |
| 589 | "{name}" |
| 590 | ); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // ---- A3: truncated, corrupt, wrong identity -------------------------------- |
| 595 | |
| 596 | #[test] |
| 597 | fn truncated_payload_fails_on_load_and_is_deleted() { |
| 598 | for cut in [1u64, 4096 + BS as u64, 4096, 100, 0] { |
| 599 | let fx = Fx::new(); |
| 600 | let s = fx.open(); |
| 601 | let a = fx.put_full(&s, 1); |
| 602 | let keep = fx.put_full(&s, 2); |
| 603 | let path = fx.object_file(&a.key); |
| 604 | let len = std::fs::metadata(&path).unwrap().len(); |
| 605 | let new_len = if cut == 1 { len - 1 } else { cut }; |
| 606 | std::fs::OpenOptions::new() |
| 607 | .write(true) |
| 608 | .open(&path) |
| 609 | .unwrap() |
| 610 | .set_len(new_len) |
| 611 | .unwrap(); |
| 612 | let used = s.capacity().used_bytes; |
| 613 | let (t, mem) = fx.load(&s, a.key, &a.header); |
| 614 | let t = t.unwrap(); |
| 615 | assert_eq!(*failed(&t), PlatformError::InvalidLayout, "cut {cut}"); |
| 616 | assert!( |
| 617 | mem.bytes().iter().all(|b| *b == 0xEE), |
| 618 | "cut {cut}: destination untouched" |
| 619 | ); |
| 620 | assert!(!path.exists(), "cut {cut}: deleted"); |
| 621 | assert_eq!(s.lookup(&[a.key]), vec![None]); |
| 622 | assert_eq!(s.capacity().used_bytes, used - OBJ, "cut {cut}"); |
| 623 | assert_eq!(s.stats().corrupt_deleted_total, 1); |
| 624 | assert_loads(&fx, &s, keep.key, 2, 8); |
| 625 | s.audit().unwrap(); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | #[test] |
| 630 | fn corrupt_payload_byte_fails_segment_hash_and_deletes() { |
| 631 | use std::os::unix::fs::FileExt; |
| 632 | let fx = Fx::new(); |
| 633 | let s = fx.open(); |
| 634 | let a = fx.put_full(&s, 1); |
| 635 | let path = fx.object_file(&a.key); |
| 636 | let f = std::fs::OpenOptions::new() |
| 637 | .read(true) |
| 638 | .write(true) |
| 639 | .open(&path) |
| 640 | .unwrap(); |
| 641 | // One byte in the second segment (second pool block). The payload on disk |
| 642 | // is AES-GCM ciphertext under a fresh random nonce, so a fixed value would |
| 643 | // already be there one run in 256 and the object would load intact; flip |
| 644 | // the byte instead so it always changes. |
| 645 | let off = 4096 + BS as u64 + 17; |
| 646 | let mut b = [0u8; 1]; |
| 647 | f.read_exact_at(&mut b, off).unwrap(); |
| 648 | f.write_all_at(&[b[0] ^ 0xFF], off).unwrap(); |
| 649 | drop(f); |
| 650 | let (t, mem) = fx.load(&s, a.key, &a.header); |
| 651 | assert_eq!(*failed(&t.unwrap()), PlatformError::InvalidLayout); |
| 652 | // The whole object is authenticated before any segment lands (ADR 0003 |
| 653 | // section 20), so not even the good first segment reaches the destination. |
| 654 | assert!( |
| 655 | mem.bytes().iter().all(|x| *x == 0xEE), |
| 656 | "nothing reaches the destination from a refused object" |
| 657 | ); |
| 658 | assert!(!path.exists()); |
| 659 | assert_eq!(s.lookup(&[a.key]), vec![None]); |
| 660 | } |
| 661 | |
| 662 | #[test] |
| 663 | fn corrupt_header_region_on_load_is_refused_and_deleted() { |
| 664 | use std::os::unix::fs::FileExt; |
| 665 | // (offset, why): magic, a struct byte (header checksum), a segment digest |
| 666 | // (root mismatch), padding. |
| 667 | for (off, why) in [ |
| 668 | (0u64, "magic"), |
| 669 | (150, "struct byte"), |
| 670 | (256 + 40, "digest table"), |
| 671 | (4000, "padding"), |
| 672 | ] { |
| 673 | let fx = Fx::new(); |
| 674 | let s = fx.open(); |
| 675 | let a = fx.put_full(&s, 1); |
| 676 | let path = fx.object_file(&a.key); |
| 677 | let f = std::fs::OpenOptions::new() |
| 678 | .read(true) |
| 679 | .write(true) |
| 680 | .open(&path) |
| 681 | .unwrap(); |
| 682 | let mut b = [0u8; 1]; |
| 683 | std::os::unix::fs::FileExt::read_exact_at(&f, &mut b, off).unwrap(); |
| 684 | f.write_all_at(&[b[0] ^ 0xFF], off).unwrap(); |
| 685 | drop(f); |
| 686 | let (t, mem) = fx.load(&s, a.key, &a.header); |
| 687 | assert_eq!(*failed(&t.unwrap()), PlatformError::InvalidLayout, "{why}"); |
| 688 | assert!(mem.bytes().iter().all(|x| *x == 0xEE), "{why}"); |
| 689 | assert!( |
| 690 | !path.exists(), |
| 691 | "{why}: a header that fails validation deletes the object" |
| 692 | ); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | #[test] |
| 697 | fn load_with_wrong_expectation_is_refused_without_io() { |
| 698 | let fx = Fx::new(); |
| 699 | let s = fx.open(); |
| 700 | let a = fx.put_full(&s, 1); |
| 701 | let before = s.stats(); |
| 702 | let mut cases: Vec<(&str, aien_kv_store::ChunkHeader)> = Vec::new(); |
| 703 | let mut h = a.header; |
| 704 | h.model_sha256[0] ^= 1; |
| 705 | cases.push(("model", h)); |
| 706 | let mut h = a.header; |
| 707 | h.namespace_id.0[0] ^= 1; |
| 708 | cases.push(("namespace", h)); |
| 709 | let mut h = a.header; |
| 710 | h.num_layers += 1; |
| 711 | cases.push(("layers", h)); |
| 712 | let mut h = a.header; |
| 713 | h.block_size = 8; |
| 714 | cases.push(("block size", h)); |
| 715 | let mut h = a.header; |
| 716 | h.dtype = 4; |
| 717 | cases.push(("dtype", h)); |
| 718 | let mut h = a.header; |
| 719 | h.payload_len = BS as u64; |
| 720 | cases.push(("payload len", h)); |
| 721 | let mut h = a.header; |
| 722 | h.chunk_key.0 .0[0] ^= 1; |
| 723 | cases.push(("chunk key", h)); |
| 724 | for (why, h) in cases { |
| 725 | let (job, mem) = fx.submit_load(&s, a.key, &h); |
| 726 | assert_eq!(job.unwrap_err(), PlatformError::InvalidLayout, "{why}"); |
| 727 | assert!(mem.bytes().iter().all(|x| *x == 0xEE), "{why}"); |
| 728 | } |
| 729 | assert_eq!(s.stats().io_ops, before.io_ops, "refused at submit, no I/O"); |
| 730 | assert!( |
| 731 | fx.object_file(&a.key).is_file(), |
| 732 | "a caller's wrong expectation deletes nothing" |
| 733 | ); |
| 734 | // An absent key. |
| 735 | let (k, h) = fx.chunk(77, 8); |
| 736 | let (job, _) = fx.submit_load(&s, k, &h); |
| 737 | assert_eq!(job.unwrap_err(), PlatformError::ObjectNotFound(k.0)); |
| 738 | // Wrong destination size. |
| 739 | let mem = Mem::new(1, 0xEE); |
| 740 | let (dst, _) = mem.dest(1); |
| 741 | assert_eq!( |
| 742 | s.submit_load(a.key, &a.header, dst, Incarnation::seq(1)) |
| 743 | .unwrap_err(), |
| 744 | PlatformError::InvalidLayout |
| 745 | ); |
| 746 | assert_loads(&fx, &s, a.key, 1, 8); |
| 747 | } |
| 748 | |
| 749 | #[test] |
| 750 | fn file_header_disagreeing_with_the_requested_chunk_is_deleted_on_load() { |
| 751 | // A self-consistent file under the right name whose position, parent or |
| 752 | // token count differs from what the key's chain says: recovery cannot |
| 753 | // know, the load's validation does. |
| 754 | #[allow(clippy::type_complexity)] |
| 755 | let cases: Vec<(&str, Box<dyn Fn(&mut aien_kv_store::ChunkHeader)>)> = vec![ |
| 756 | ("position", Box::new(|h| h.first_position = 8)), |
| 757 | ("parent", Box::new(|h| h.parent_key = [0x42; 32])), |
| 758 | ( |
| 759 | "token count", |
| 760 | Box::new(|h| { |
| 761 | h.token_count = 7; |
| 762 | h.flags = 1; |
| 763 | }), |
| 764 | ), |
| 765 | ]; |
| 766 | for (why, mutate) in cases { |
| 767 | let fx = Fx::new(); |
| 768 | fx.prime(); |
| 769 | let (key, header) = fx.chunk(1, 8); |
| 770 | let mut planted = header; |
| 771 | mutate(&mut planted); |
| 772 | fx.plant(&key, &planted, &pattern(1, 2 * BS), BS); |
| 773 | let s = fx.open(); |
| 774 | assert_eq!( |
| 775 | s.lookup(&[key]), |
| 776 | vec![Some(FULL_PAYLOAD)], |
| 777 | "{why}: indexed at recovery" |
| 778 | ); |
| 779 | let (t, mem) = fx.load(&s, key, &header); |
| 780 | assert_eq!(*failed(&t.unwrap()), PlatformError::InvalidLayout, "{why}"); |
| 781 | assert!(mem.bytes().iter().all(|x| *x == 0xEE), "{why}"); |
| 782 | assert!(!fx.object_file(&key).exists(), "{why}"); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | #[test] |
| 787 | fn released_source_fails_the_job_and_writes_nothing() { |
| 788 | let fx = Fx::new(); |
| 789 | let s = fx.open(); |
| 790 | let (key, _) = fx.chunk(1, 8); |
| 791 | let (job, handle) = fx.submit(&s, 1, 8); |
| 792 | let job = job.unwrap(); |
| 793 | handle.revoke(); // the owner freed the blocks while the job was queued |
| 794 | s.run_until_idle(); |
| 795 | let t = terminal_of(&drain(&s), job); |
| 796 | assert_eq!(*failed(&t), PlatformError::InvalidLayout); |
| 797 | assert_eq!(s.stats().released_source_violations, 1); |
| 798 | assert_eq!(s.lookup(&[key]), vec![None]); |
| 799 | assert!(tmp_files(fx.root()).is_empty()); |
| 800 | assert_eq!(s.capacity().reserved_bytes, 0); |
| 801 | } |
| 802 | |
| 803 | // ---- tail rows (finding 3) --------------------------------------------------- |
| 804 | |
| 805 | #[test] |
| 806 | fn tail_rows_above_token_count_are_zero_in_object() { |
| 807 | for tokens in [6u32, 1, 3, 5, 8, 4] { |
| 808 | let fx = Fx::new(); |
| 809 | let s = fx.open(); |
| 810 | let (key, header) = fx.chunk(1, tokens); |
| 811 | let blocks = blocks_for(tokens); |
| 812 | // The pool holds garbage everywhere, including rows another conversation left. |
| 813 | let mem = Mem::new(blocks, 0xFF); |
| 814 | let (src, _) = mem.source(blocks, tail_valid(tokens)); |
| 815 | let job = s.submit_store(header, src, Incarnation::seq(1)).unwrap(); |
| 816 | s.run_until_idle(); |
| 817 | assert_eq!( |
| 818 | terminal_of(&drain(&s), job), |
| 819 | Terminal::Done(JobStage::Durable), |
| 820 | "{tokens}" |
| 821 | ); |
| 822 | assert!( |
| 823 | mem.bytes().iter().all(|b| *b == 0xFF), |
| 824 | "the pool block is never modified" |
| 825 | ); |
| 826 | |
| 827 | let path = fx.object_file(&key); |
| 828 | let plain = read_object_plain(&path); |
| 829 | let payload = plain[4096..].to_vec(); |
| 830 | assert_eq!(payload.len(), blocks * BS); |
| 831 | let mut expect = vec![0xFFu8; blocks * BS]; |
| 832 | let tail = tail_valid(tokens); |
| 833 | if tail < BLOCK_TOKENS { |
| 834 | let last = (blocks - 1) * BS; |
| 835 | zero_tail_rows(&mut expect[last..last + BS], tail); |
| 836 | } |
| 837 | assert_eq!(payload, expect, "{tokens} tokens"); |
| 838 | if tail < BLOCK_TOKENS { |
| 839 | // Spot check: layer 1, V plane, first row at or above the tail. |
| 840 | let off = (blocks - 1) * BS + LAYER_STRIDE + PLANE_STRIDE + tail as usize * ROW; |
| 841 | assert!( |
| 842 | payload[off..off + ROW].iter().all(|b| *b == 0), |
| 843 | "{tokens} tokens" |
| 844 | ); |
| 845 | assert_eq!(payload[off - 1], 0xFF); |
| 846 | } |
| 847 | // The segment digests cover the zeroed bytes. |
| 848 | let region = &plain[..4096]; |
| 849 | for (i, seg) in payload.chunks(BS).enumerate() { |
| 850 | assert_eq!( |
| 851 | ®ion[256 + 32 * i..256 + 32 * i + 32], |
| 852 | &sha(seg), |
| 853 | "segment {i}" |
| 854 | ); |
| 855 | } |
| 856 | // And a load returns the zeroed rows. |
| 857 | let (t, m) = fx.load(&s, key, &header); |
| 858 | assert_eq!(t.unwrap(), Terminal::Done(JobStage::Completed)); |
| 859 | assert_eq!(m.bytes(), expect, "{tokens} tokens"); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | #[test] |
| 864 | fn tail_scratch_is_one_block() { |
| 865 | let fx = Fx::new(); |
| 866 | let s = fx.open(); |
| 867 | assert_eq!(s.stats().tail_scratch_bytes, BS as u64); |
| 868 | } |
| 869 | |
| 870 | // ---- accounting and eviction ------------------------------------------------- |
| 871 | |
| 872 | #[test] |
| 873 | fn used_bytes_counts_header_region_and_physical_blocks_after_publish() { |
| 874 | let fx = Fx::new(); |
| 875 | let s = fx.open(); |
| 876 | let a = fx.put_full(&s, 1); |
| 877 | let b = fx.put(&s, 2, 2); |
| 878 | let want = phys_of(&fx.object_file(&a.key)) + phys_of(&fx.object_file(&b.key)); |
| 879 | assert_eq!(want, OBJ + OBJ1); |
| 880 | assert_eq!(s.capacity().used_bytes, want); |
| 881 | assert_eq!(s.capacity().objects, 2); |
| 882 | } |
| 883 | |
| 884 | #[test] |
| 885 | fn eviction_runs_on_worker_not_in_submit() { |
| 886 | let fx = Fx::new(); |
| 887 | let s = fx.open_with(|c| c.quota_bytes = 2 * OBJ); |
| 888 | let a = fx.put_full(&s, 1); |
| 889 | let b = fx.put_full(&s, 2); |
| 890 | assert_eq!(s.capacity().used_bytes, 2 * OBJ); |
| 891 | let before = s.stats(); |
| 892 | let (job, _) = fx.submit(&s, 3, 8); |
| 893 | let job = job.unwrap(); |
| 894 | // Submit reserved and nothing else: no unlink, no file touched, no lookup change. |
| 895 | let mid = s.stats(); |
| 896 | assert_eq!(mid.io_ops, before.io_ops); |
| 897 | assert_eq!(mid.unlinks, before.unlinks); |
| 898 | assert!(fx.object_file(&a.key).is_file()); |
| 899 | assert_eq!( |
| 900 | s.lookup(&[a.key, b.key]), |
| 901 | vec![Some(FULL_PAYLOAD), Some(FULL_PAYLOAD)] |
| 902 | ); |
| 903 | assert_eq!(s.capacity().reserved_bytes, OBJ); |
| 904 | |
| 905 | s.run_until_idle(); |
| 906 | assert_eq!( |
| 907 | terminal_of(&drain(&s), job), |
| 908 | Terminal::Done(JobStage::Durable) |
| 909 | ); |
| 910 | let after = s.stats(); |
| 911 | assert_eq!(after.unlinks - before.unlinks, 1, "one victim"); |
| 912 | assert_eq!(after.evicted_bytes_total, OBJ); |
| 913 | // The least recently used (a) went; b and the new object stay. |
| 914 | assert!(!fx.object_file(&a.key).exists()); |
| 915 | assert!(fx.object_file(&b.key).is_file()); |
| 916 | let (k3, _) = fx.chunk(3, 8); |
| 917 | assert_eq!( |
| 918 | s.lookup(&[a.key, b.key, k3]), |
| 919 | vec![None, Some(FULL_PAYLOAD), Some(FULL_PAYLOAD)] |
| 920 | ); |
| 921 | let cap = s.capacity(); |
| 922 | assert!(cap.used_bytes <= cap.quota_bytes); |
| 923 | assert_eq!(cap.used_bytes, 2 * OBJ); |
| 924 | s.audit().unwrap(); |
| 925 | } |
| 926 | |
| 927 | #[test] |
| 928 | fn eviction_is_least_recently_used_by_load_time_and_skips_pinned() { |
| 929 | let fx = Fx::new(); |
| 930 | let s = fx.open_with(|c| c.quota_bytes = 3 * OBJ); |
| 931 | let a = fx.put_full(&s, 1); |
| 932 | let b = fx.put_full(&s, 2); |
| 933 | let c = fx.put_full(&s, 3); |
| 934 | // Loading `a` makes it the most recently used. |
| 935 | assert_loads(&fx, &s, a.key, 1, 8); |
| 936 | let token = s.pin(&[b.key]).unwrap(); |
| 937 | let d = fx.put_full(&s, 4); |
| 938 | assert_eq!( |
| 939 | terminal_of(&d.completions, d.job.unwrap()), |
| 940 | Terminal::Done(JobStage::Durable) |
| 941 | ); |
| 942 | // b is pinned (skipped); c is the least recently used unpinned object. |
| 943 | assert_eq!( |
| 944 | s.lookup(&[a.key, b.key, c.key, d.key]), |
| 945 | vec![ |
| 946 | Some(FULL_PAYLOAD), |
| 947 | Some(FULL_PAYLOAD), |
| 948 | None, |
| 949 | Some(FULL_PAYLOAD) |
| 950 | ] |
| 951 | ); |
| 952 | // Everything else pinned: nothing is evictable, so admission refuses at submit. |
| 953 | let t2 = s.pin(&[a.key, d.key]).unwrap(); |
| 954 | let (job, _) = fx.submit(&s, 5, 8); |
| 955 | assert_eq!(job.unwrap_err(), PlatformError::OutOfMemory); |
| 956 | assert_eq!(s.capacity().reserved_bytes, 0); |
| 957 | s.unpin(token); |
| 958 | s.unpin(t2); |
| 959 | let (job, _) = fx.submit(&s, 5, 8); |
| 960 | job.unwrap(); |
| 961 | s.run_until_idle(); |
| 962 | s.audit().unwrap(); |
| 963 | } |
| 964 | |
| 965 | #[test] |
| 966 | fn delete_marks_at_once_and_unlinks_on_the_worker() { |
| 967 | let fx = Fx::new(); |
| 968 | let s = fx.open(); |
| 969 | let a = fx.put_full(&s, 1); |
| 970 | let b = fx.put_full(&s, 2); |
| 971 | let token = s.pin(&[b.key]).unwrap(); |
| 972 | let used = s.capacity().used_bytes; |
| 973 | let before = s.stats(); |
| 974 | assert_eq!(s.delete(&[a.key, b.key]), 1, "the pinned key is skipped"); |
| 975 | assert_eq!( |
| 976 | s.lookup(&[a.key, b.key]), |
| 977 | vec![None, Some(FULL_PAYLOAD)], |
| 978 | "a misses at once" |
| 979 | ); |
| 980 | assert_eq!( |
| 981 | s.stats().unlinks, |
| 982 | before.unlinks, |
| 983 | "delete itself unlinks nothing" |
| 984 | ); |
| 985 | assert!(fx.object_file(&a.key).is_file()); |
| 986 | let cap = s.capacity(); |
| 987 | assert_eq!( |
| 988 | cap.used_bytes, used, |
| 989 | "bytes are subtracted only after the unlink returns" |
| 990 | ); |
| 991 | assert_eq!(cap.evicting_bytes, OBJ); |
| 992 | s.run_until_idle(); |
| 993 | assert!(!fx.object_file(&a.key).exists()); |
| 994 | let cap = s.capacity(); |
| 995 | assert_eq!(cap.used_bytes, used - OBJ); |
| 996 | assert_eq!(cap.evicting_bytes, 0); |
| 997 | s.unpin(token); |
| 998 | s.audit().unwrap(); |
| 999 | } |
| 1000 | |
| 1001 | #[test] |
| 1002 | fn unlink_failure_returns_the_victim_to_service_and_the_new_object_stays_published() { |
| 1003 | let fx = Fx::new(); |
| 1004 | let s = fx.open_with(|c| c.quota_bytes = OBJ); |
| 1005 | let a = fx.put_full(&s, 1); |
| 1006 | fx.faults.arm(Boundary::Unlink, FaultAction::Fail(EIO)); |
| 1007 | let b = fx.put_full(&s, 2); |
| 1008 | let t = terminal_of(&b.completions, b.job.unwrap()); |
| 1009 | assert_eq!( |
| 1010 | t, |
| 1011 | Terminal::Done(JobStage::Durable), |
| 1012 | "the write already succeeded" |
| 1013 | ); |
| 1014 | assert_eq!( |
| 1015 | s.lookup(&[a.key, b.key]), |
| 1016 | vec![Some(FULL_PAYLOAD), Some(FULL_PAYLOAD)] |
| 1017 | ); |
| 1018 | assert!(fx.object_file(&a.key).is_file()); |
| 1019 | assert_eq!( |
| 1020 | s.capacity().used_bytes, |
| 1021 | 2 * OBJ, |
| 1022 | "over quota until a later purge" |
| 1023 | ); |
| 1024 | assert_eq!(s.capacity().evicting_bytes, 0); |
| 1025 | assert_eq!(s.capacity().reserved_bytes, 0); |
| 1026 | assert!(tmp_files(fx.root()).is_empty()); |
| 1027 | s.audit().unwrap(); |
| 1028 | } |
| 1029 | |
| 1030 | // ---- jobs ---------------------------------------------------------------------- |
| 1031 | |
| 1032 | #[test] |
| 1033 | fn store_of_a_published_key_is_a_zero_byte_job_and_in_flight_duplicates_share_one() { |
| 1034 | let fx = Fx::new(); |
| 1035 | let s = fx.open(); |
| 1036 | let a = fx.put_full(&s, 1); |
| 1037 | let writes = s.stats().writes; |
| 1038 | let (j, _) = fx.submit(&s, 1, 8); |
| 1039 | let j = j.unwrap(); |
| 1040 | assert_ne!(Some(j), a.job.ok()); |
| 1041 | s.run_until_idle(); |
| 1042 | let c = completion_of(&drain(&s), j); |
| 1043 | assert_eq!(c.terminal, Some(Terminal::Done(JobStage::Durable))); |
| 1044 | assert_eq!(c.bytes, 0); |
| 1045 | assert_eq!(s.stats().writes, writes, "nothing is rewritten"); |
| 1046 | assert_eq!(s.stats().dedup_total, 1); |
| 1047 | |
| 1048 | let (j1, _) = fx.submit(&s, 2, 8); |
| 1049 | let (j2, _) = fx.submit(&s, 2, 8); |
| 1050 | assert_eq!( |
| 1051 | j1.unwrap(), |
| 1052 | j2.unwrap(), |
| 1053 | "same key in flight: the existing job" |
| 1054 | ); |
| 1055 | assert_eq!(s.capacity().reserved_bytes, OBJ, "reserved once"); |
| 1056 | s.run_until_idle(); |
| 1057 | s.audit().unwrap(); |
| 1058 | } |
| 1059 | |
| 1060 | #[test] |
| 1061 | fn cancel_before_start_yields_one_cancelled_terminal_and_touches_nothing() { |
| 1062 | let fx = Fx::new(); |
| 1063 | let s = fx.open(); |
| 1064 | let a = fx.put_full(&s, 1); |
| 1065 | let (job, _) = fx.submit(&s, 2, 8); |
| 1066 | let job = job.unwrap(); |
| 1067 | let before = s.stats(); |
| 1068 | assert_eq!(s.cancel(job), CancelOutcome::Cancelled); |
| 1069 | assert_eq!(s.stats().io_ops, before.io_ops); |
| 1070 | s.run_until_idle(); |
| 1071 | let v = drain(&s); |
| 1072 | assert_eq!(terminal_of(&v, job), Terminal::Cancelled); |
| 1073 | assert_eq!(s.capacity().reserved_bytes, 0); |
| 1074 | assert_eq!(s.capacity().park_ops, 0); |
| 1075 | assert_eq!(s.cancel(job), CancelOutcome::TooLate, "already terminal"); |
| 1076 | |
| 1077 | // A queued load: cancelled before it starts, destination never written. |
| 1078 | let (lj, mem) = fx.submit_load(&s, a.key, &a.header); |
| 1079 | let lj = lj.unwrap(); |
| 1080 | assert_eq!(s.cancel(lj), CancelOutcome::Cancelled); |
| 1081 | s.run_until_idle(); |
| 1082 | assert_eq!(terminal_of(&drain(&s), lj), Terminal::Cancelled); |
| 1083 | assert!(mem.bytes().iter().all(|b| *b == 0xEE)); |
| 1084 | assert!(!s.pump(), "nothing left to do"); |
| 1085 | s.audit().unwrap(); |
| 1086 | } |
| 1087 | |
| 1088 | #[test] |
| 1089 | fn cancel_during_write_unlinks_the_temp_and_publishes_nothing() { |
| 1090 | let fx = Fx::new(); |
| 1091 | let cell: StoreCell = Arc::new(OnceLock::new()); |
| 1092 | let c2 = cell.clone(); |
| 1093 | let outcome = log(); |
| 1094 | let o2 = outcome.clone(); |
| 1095 | let mut cfg = fx.config(); |
| 1096 | cfg.faults = Some(Arc::new(CallbackHook { |
| 1097 | scripted: fx.faults.clone(), |
| 1098 | f: Box::new(move |b, key| { |
| 1099 | if b == Boundary::P1Write { |
| 1100 | let store = c2.get().unwrap(); |
| 1101 | // The job id is 1: the first job of a fresh store. |
| 1102 | let _ = key; |
| 1103 | o2.lock() |
| 1104 | .unwrap() |
| 1105 | .push(format!("{:?}", store.cancel(aien_kv_store::JobId(1)))); |
| 1106 | } |
| 1107 | None |
| 1108 | }), |
| 1109 | })); |
| 1110 | let store = Arc::new(HostFsStore::open(cfg).unwrap()); |
| 1111 | cell.set(store.clone()).ok(); |
| 1112 | recover_all(&store); |
| 1113 | let (key, _) = fx.chunk(1, 8); |
| 1114 | let (job, _) = fx.submit(&store, 1, 8); |
| 1115 | let job = job.unwrap(); |
| 1116 | assert_eq!(job.0, 1); |
| 1117 | store.run_until_idle(); |
| 1118 | assert_eq!( |
| 1119 | outcome.lock().unwrap().clone(), |
| 1120 | vec!["Cancelled".to_string()] |
| 1121 | ); |
| 1122 | assert_eq!(terminal_of(&drain(&store), job), Terminal::Cancelled); |
| 1123 | assert!(tmp_files(fx.root()).is_empty()); |
| 1124 | assert_eq!(store.lookup(&[key]), vec![None]); |
| 1125 | assert_eq!(store.capacity().reserved_bytes, 0); |
| 1126 | } |
| 1127 | |
| 1128 | #[test] |
| 1129 | fn cancel_after_rename_is_too_late_and_the_object_publishes() { |
| 1130 | let fx = Fx::new(); |
| 1131 | let cell: StoreCell = Arc::new(OnceLock::new()); |
| 1132 | let c2 = cell.clone(); |
| 1133 | let outcome = log(); |
| 1134 | let o2 = outcome.clone(); |
| 1135 | let mut cfg = fx.config(); |
| 1136 | cfg.faults = Some(Arc::new(CallbackHook { |
| 1137 | scripted: fx.faults.clone(), |
| 1138 | f: Box::new(move |b, _| { |
| 1139 | if b == Boundary::P4FsyncDir { |
| 1140 | let store = c2.get().unwrap(); |
| 1141 | o2.lock() |
| 1142 | .unwrap() |
| 1143 | .push(format!("{:?}", store.cancel(aien_kv_store::JobId(1)))); |
| 1144 | } |
| 1145 | None |
| 1146 | }), |
| 1147 | })); |
| 1148 | let store = Arc::new(HostFsStore::open(cfg).unwrap()); |
| 1149 | cell.set(store.clone()).ok(); |
| 1150 | recover_all(&store); |
| 1151 | let (key, _) = fx.chunk(1, 8); |
| 1152 | let (job, _) = fx.submit(&store, 1, 8); |
| 1153 | let job = job.unwrap(); |
| 1154 | store.run_until_idle(); |
| 1155 | assert_eq!(outcome.lock().unwrap().clone(), vec!["TooLate".to_string()]); |
| 1156 | assert_eq!( |
| 1157 | terminal_of(&drain(&store), job), |
| 1158 | Terminal::Done(JobStage::Durable) |
| 1159 | ); |
| 1160 | assert_eq!(store.lookup(&[key]), vec![Some(FULL_PAYLOAD)]); |
| 1161 | } |
| 1162 | |
| 1163 | #[test] |
| 1164 | fn every_job_yields_exactly_one_terminal_completion() { |
| 1165 | let fx = Fx::new(); |
| 1166 | let s = fx.open(); |
| 1167 | let a = fx.put_full(&s, 1); |
| 1168 | fx.faults.arm(Boundary::P1Write, FaultAction::Fail(EIO)); |
| 1169 | let mut jobs = Vec::new(); |
| 1170 | for n in 2..5 { |
| 1171 | jobs.push(fx.submit(&s, n, 8).0.unwrap()); |
| 1172 | } |
| 1173 | jobs.push(fx.submit_load(&s, a.key, &a.header).0.unwrap()); |
| 1174 | jobs.push(s.submit_flush(Incarnation::seq(5)).unwrap()); |
| 1175 | let cancelled = fx.submit(&s, 9, 8).0.unwrap(); |
| 1176 | s.cancel(cancelled); |
| 1177 | jobs.push(cancelled); |
| 1178 | s.run_until_idle(); |
| 1179 | let v = drain(&s); |
| 1180 | for j in jobs { |
| 1181 | let n = v |
| 1182 | .iter() |
| 1183 | .filter(|c| c.job == j && c.terminal.is_some()) |
| 1184 | .count(); |
| 1185 | assert_eq!(n, 1, "{j:?}"); |
| 1186 | } |
| 1187 | assert!(!s.pump()); |
| 1188 | } |
| 1189 | |
| 1190 | #[test] |
| 1191 | fn per_class_bounds_refuse_with_queue_full_and_release_at_terminal() { |
| 1192 | let fx = Fx::new(); |
| 1193 | let s = fx.open_with(|c| { |
| 1194 | c.max_park_ops = 2; |
| 1195 | c.max_restore_ops = 1; |
| 1196 | }); |
| 1197 | let a = fx.put_full(&s, 1); |
| 1198 | let j1 = fx.submit(&s, 2, 8).0.unwrap(); |
| 1199 | let _j2 = fx.submit(&s, 3, 8).0.unwrap(); |
| 1200 | assert_eq!(fx.submit(&s, 4, 8).0.unwrap_err(), PlatformError::QueueFull); |
| 1201 | // The restore class has its own reservation. |
| 1202 | let (l1, _) = fx.submit_load(&s, a.key, &a.header); |
| 1203 | let l1 = l1.unwrap(); |
| 1204 | let (l2, _) = fx.submit_load(&s, a.key, &a.header); |
| 1205 | assert_eq!(l2.unwrap_err(), PlatformError::QueueFull); |
| 1206 | let cap = s.capacity(); |
| 1207 | assert_eq!((cap.park_ops, cap.restore_ops), (2, 1)); |
| 1208 | s.run_until_idle(); |
| 1209 | let v = drain(&s); |
| 1210 | assert!(v.iter().any(|c| c.job == j1 && c.terminal.is_some())); |
| 1211 | assert!(v.iter().any(|c| c.job == l1 && c.terminal.is_some())); |
| 1212 | let cap = s.capacity(); |
| 1213 | assert_eq!( |
| 1214 | (cap.park_ops, cap.restore_ops, cap.inflight_bytes), |
| 1215 | (0, 0, 0) |
| 1216 | ); |
| 1217 | fx.submit(&s, 4, 8).0.unwrap(); |
| 1218 | s.run_until_idle(); |
| 1219 | } |
| 1220 | |
| 1221 | #[test] |
| 1222 | fn attach_waiter_is_bounded_per_job() { |
| 1223 | let fx = Fx::new(); |
| 1224 | let s = fx.open_with(|c| c.max_waiters_per_job = 3); |
| 1225 | let a = fx.put_full(&s, 1); |
| 1226 | let (l, _) = fx.submit_load(&s, a.key, &a.header); |
| 1227 | let l = l.unwrap(); |
| 1228 | s.attach_waiter(l, Incarnation::seq(77)).unwrap(); |
| 1229 | s.attach_waiter(l, Incarnation::seq(77)).unwrap(); // requester + 2 attached = 3 |
| 1230 | assert_eq!( |
| 1231 | s.attach_waiter(l, Incarnation::seq(77)).unwrap_err(), |
| 1232 | PlatformError::QueueFull |
| 1233 | ); |
| 1234 | let st = fx.submit(&s, 2, 8).0.unwrap(); |
| 1235 | assert!(matches!( |
| 1236 | s.attach_waiter(st, Incarnation::seq(77)), |
| 1237 | Err(PlatformError::TaskRejected(_)) |
| 1238 | )); |
| 1239 | s.run_until_idle(); |
| 1240 | } |
| 1241 | |
| 1242 | #[test] |
| 1243 | fn pin_is_all_or_nothing_and_unpin_releases() { |
| 1244 | let fx = Fx::new(); |
| 1245 | let s = fx.open(); |
| 1246 | let a = fx.put_full(&s, 1); |
| 1247 | let (miss, _) = fx.chunk(50, 8); |
| 1248 | assert_eq!( |
| 1249 | s.pin(&[a.key, miss]).unwrap_err(), |
| 1250 | PlatformError::ObjectNotFound(miss.0) |
| 1251 | ); |
| 1252 | let t = s.pin(&[a.key]).unwrap(); |
| 1253 | assert_eq!(s.delete(&[a.key]), 0); |
| 1254 | s.unpin(t); |
| 1255 | assert_eq!(s.delete(&[a.key]), 1); |
| 1256 | s.run_until_idle(); |
| 1257 | s.audit().unwrap(); |
| 1258 | } |
| 1259 | |
| 1260 | #[test] |
| 1261 | fn flush_job_syncs_directories_and_reports_durable() { |
| 1262 | let fx = Fx::new(); |
| 1263 | let s = fx.open(); |
| 1264 | fx.put_full(&s, 1); |
| 1265 | let before = s.stats(); |
| 1266 | let j = s.submit_flush(Incarnation::seq(7)).unwrap(); |
| 1267 | s.run_until_idle(); |
| 1268 | let c = completion_of(&drain(&s), j); |
| 1269 | assert_eq!(c.terminal, Some(Terminal::Done(JobStage::Durable))); |
| 1270 | assert_eq!(c.publish, PublishOutcome::Durable); |
| 1271 | assert!(s.stats().dir_fsyncs > before.dir_fsyncs); |
| 1272 | |
| 1273 | fx.faults.arm(Boundary::FlushSync, FaultAction::Fail(EIO)); |
| 1274 | let j = s.submit_flush(Incarnation::seq(8)).unwrap(); |
| 1275 | s.run_until_idle(); |
| 1276 | let t = terminal_of(&drain(&s), j); |
| 1277 | assert!(is_hw(failed(&t))); |
| 1278 | assert_eq!(s.capacity().state, StoreState::Degraded); |
| 1279 | // A degraded store may still run a flush. |
| 1280 | s.submit_flush(Incarnation::seq(9)).unwrap(); |
| 1281 | s.run_until_idle(); |
| 1282 | } |
| 1283 | |
| 1284 | // ---- the worker thread and shutdown ----------------------------------------------- |
| 1285 | |
| 1286 | #[test] |
| 1287 | fn thread_worker_recovers_and_publishes_without_a_manual_pump() { |
| 1288 | let fx = Fx::new(); |
| 1289 | // Something to recover. |
| 1290 | { |
| 1291 | let s = fx.open(); |
| 1292 | fx.put_full(&s, 1); |
| 1293 | } |
| 1294 | let s = fx.open_raw(|c| c.worker = WorkerMode::Thread); |
| 1295 | wait_for("recovery", || s.capacity().state == StoreState::Open); |
| 1296 | let (k1, _) = fx.chunk(1, 8); |
| 1297 | assert_eq!(s.lookup(&[k1]), vec![Some(FULL_PAYLOAD)]); |
| 1298 | let (job, _) = fx.submit(&s, 2, 8); |
| 1299 | let job = job.unwrap(); |
| 1300 | let mut got = Vec::new(); |
| 1301 | wait_for("terminal", || { |
| 1302 | got.extend(s.poll(10)); |
| 1303 | got.iter().any(|c| c.job == job && c.terminal.is_some()) |
| 1304 | }); |
| 1305 | assert_eq!(terminal_of(&got, job), Terminal::Done(JobStage::Durable)); |
| 1306 | let (k2, _) = fx.chunk(2, 8); |
| 1307 | assert_eq!(s.lookup(&[k2]), vec![Some(FULL_PAYLOAD)]); |
| 1308 | let r = s.shutdown(1000); |
| 1309 | assert!(r.unfinished.is_empty()); |
| 1310 | } |
| 1311 | |
| 1312 | #[test] |
| 1313 | fn shutdown_with_stalled_fsync_returns_by_deadline_and_reports_unfinished() { |
| 1314 | let fx = Fx::new(); |
| 1315 | let s = fx.open_with(|c| c.worker = WorkerMode::Thread); |
| 1316 | fx.faults.arm( |
| 1317 | Boundary::P2FsyncFile, |
| 1318 | FaultAction::Stall(Duration::from_millis(1500)), |
| 1319 | ); |
| 1320 | let (job, _) = fx.submit(&s, 1, 8); |
| 1321 | let job = job.unwrap(); |
| 1322 | // Wait until the job is inside the stalled fsync. |
| 1323 | wait_for("stall", || fx.faults.fired(Boundary::P2FsyncFile) == 1); |
| 1324 | let queued = fx.submit(&s, 2, 8).0.unwrap(); |
| 1325 | let t0 = Instant::now(); |
| 1326 | let report = s.shutdown(100); |
| 1327 | assert!( |
| 1328 | t0.elapsed() < Duration::from_millis(1200), |
| 1329 | "returned by the deadline" |
| 1330 | ); |
| 1331 | assert_eq!(report.unfinished.len(), 1, "{report:?}"); |
| 1332 | assert_eq!(report.unfinished[0].job, job); |
| 1333 | assert_eq!(report.unfinished[0].bytes, OBJ); |
| 1334 | assert_eq!(s.stats().shutdown_unfinished_jobs, 1); |
| 1335 | // Intake is closed. |
| 1336 | assert_eq!( |
| 1337 | fx.submit(&s, 3, 8).0.unwrap_err(), |
| 1338 | PlatformError::DeviceUnavailable |
| 1339 | ); |
| 1340 | // The stalled job is past nothing cancellable: it finishes its publication later; |
| 1341 | // the queued one was cancelled by the drain. |
| 1342 | let mut got = Vec::new(); |
| 1343 | wait_for("both terminals", || { |
| 1344 | got.extend(s.poll(10)); |
| 1345 | [job, queued] |
| 1346 | .iter() |
| 1347 | .all(|j| got.iter().any(|c| c.job == *j && c.terminal.is_some())) |
| 1348 | }); |
| 1349 | assert_eq!(terminal_of(&got, job), Terminal::Done(JobStage::Durable)); |
| 1350 | assert_eq!(terminal_of(&got, queued), Terminal::Cancelled); |
| 1351 | } |
| 1352 | |
| 1353 | #[test] |
| 1354 | fn manual_shutdown_cancels_queued_jobs_and_finishes_nothing_half_done() { |
| 1355 | let fx = Fx::new(); |
| 1356 | let s = fx.open(); |
| 1357 | let a = fx.put_full(&s, 1); |
| 1358 | let j1 = fx.submit(&s, 2, 8).0.unwrap(); |
| 1359 | let l1 = fx.submit_load(&s, a.key, &a.header).0.unwrap(); |
| 1360 | let report = s.shutdown(500); |
| 1361 | assert!(report.unfinished.is_empty()); |
| 1362 | assert_eq!(report.cancelled, 2); |
| 1363 | let v = drain(&s); |
| 1364 | assert_eq!(terminal_of(&v, j1), Terminal::Cancelled); |
| 1365 | assert_eq!(terminal_of(&v, l1), Terminal::Cancelled); |
| 1366 | assert!(tmp_files(fx.root()).is_empty()); |
| 1367 | assert_eq!( |
| 1368 | fx.submit(&s, 3, 8).0.unwrap_err(), |
| 1369 | PlatformError::DeviceUnavailable |
| 1370 | ); |
| 1371 | } |
| 1372 | |
| 1373 | // ---- settings ------------------------------------------------------------------------- |
| 1374 | |
| 1375 | #[test] |
| 1376 | fn apply_env_parses_quota_max_objects_and_slice_and_rejects_garbage() { |
| 1377 | let fx = Fx::new(); |
| 1378 | let mut c = fx.config(); |
| 1379 | let env = |k: &str| match k { |
| 1380 | "AIEN_KV_STORE_QUOTA_BYTES" => Some("123456".to_string()), |
| 1381 | "AIEN_KV_STORE_MAX_OBJECTS" => Some("77".to_string()), |
| 1382 | "AIEN_KV_STORE_RECOVERY_SLICE_MS" => Some("9".to_string()), |
| 1383 | _ => None, |
| 1384 | }; |
| 1385 | c.apply_env(env).unwrap(); |
| 1386 | assert_eq!( |
| 1387 | (c.quota_bytes, c.max_objects, c.recovery_slice_ms), |
| 1388 | (123456, 77, 9) |
| 1389 | ); |
| 1390 | // Unset keeps the config (defaults: 8 GiB, 65536, 50 ms). |
| 1391 | let mut d = fx.config(); |
| 1392 | let (q, m, sl) = (d.quota_bytes, d.max_objects, d.recovery_slice_ms); |
| 1393 | d.apply_env(|_| None).unwrap(); |
| 1394 | assert_eq!( |
| 1395 | (d.quota_bytes, d.max_objects, d.recovery_slice_ms), |
| 1396 | (q, m, sl) |
| 1397 | ); |
| 1398 | let fresh = |
| 1399 | aien_kv_store::host_fs::HostFsConfig::from_fields(fx.root().to_path_buf(), &fx.fields) |
| 1400 | .unwrap(); |
| 1401 | assert_eq!(fresh.quota_bytes, 8 << 30); |
| 1402 | assert_eq!(fresh.max_objects, 65536); |
| 1403 | assert_eq!(fresh.recovery_slice_ms, 50); |
| 1404 | assert_eq!((fresh.max_restore_ops, fresh.max_park_ops), (48, 64)); |
| 1405 | assert_eq!(fresh.max_inflight_bytes, 256 << 20); |
| 1406 | // Garbage and zero are refused, and leave the config unchanged. |
| 1407 | for bad in ["abc", "", "-5", "0", "1.5"] { |
| 1408 | let mut e = fx.config(); |
| 1409 | let (q0, m0) = (e.quota_bytes, e.max_objects); |
| 1410 | let r = e.apply_env(|k| (k == "AIEN_KV_STORE_QUOTA_BYTES").then(|| bad.to_string())); |
| 1411 | assert!(r.is_err(), "{bad:?}"); |
| 1412 | assert_eq!((e.quota_bytes, e.max_objects), (q0, m0)); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | #[test] |
| 1417 | fn open_refuses_a_layout_whose_rows_do_not_fit_the_block() { |
| 1418 | let fx = Fx::new(); |
| 1419 | let mut c = fx.config(); |
| 1420 | c.layout.block_stride = 4096; // the last rows would fall outside the block |
| 1421 | assert!(HostFsStore::open(c).is_err()); |
| 1422 | let mut c = fx.config(); |
| 1423 | c.quota_bytes = 0; |
| 1424 | assert!(HostFsStore::open(c).is_err()); |
| 1425 | } |
| 1426 | |
| 1427 | // ---- the index --------------------------------------------------------------------------- |
| 1428 | |
| 1429 | fn entry(k: u8, phys: u64, mtime: u64, flags: u32) -> IndexEntry { |
| 1430 | IndexEntry { |
| 1431 | key: [k; 32], |
| 1432 | phys_bytes: phys, |
| 1433 | mtime_ns: mtime, |
| 1434 | payload_len: 1, |
| 1435 | flags, |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | #[test] |
| 1440 | fn index_entry_is_64_bytes_and_the_cap_is_4_mib() { |
| 1441 | assert_eq!(std::mem::size_of::<IndexEntry>(), INDEX_ENTRY_BYTES); |
| 1442 | assert_eq!(INDEX_ENTRY_BYTES * 65536, 4_194_304); |
| 1443 | } |
| 1444 | |
| 1445 | #[test] |
| 1446 | fn index_aggregates_track_busy_and_evicting_entries() { |
| 1447 | let mut i = Index::new(); |
| 1448 | for k in 1..=4u8 { |
| 1449 | i.insert(entry(k, 100 * k as u64, k as u64, 0)); |
| 1450 | } |
| 1451 | assert_eq!(i.total_bytes(), 1000); |
| 1452 | assert_eq!(i.evictable(), (1000, 4)); |
| 1453 | assert!(i.busy_inc(&[2; 32])); |
| 1454 | assert!(i.busy_inc(&[2; 32])); |
| 1455 | assert_eq!(i.evictable(), (800, 3)); |
| 1456 | assert!(!i.mark_evicting(&[2; 32]), "busy entries are not marked"); |
| 1457 | assert!(i.mark_evicting(&[3; 32])); |
| 1458 | assert_eq!(i.evicting(), (300, 1)); |
| 1459 | assert_eq!(i.evictable(), (500, 2)); |
| 1460 | assert!(!i.busy_inc(&[3; 32]), "a marked entry cannot be pinned"); |
| 1461 | i.busy_dec(&[2; 32]); |
| 1462 | assert_eq!(i.evictable(), (500, 2), "still one reference"); |
| 1463 | i.busy_dec(&[2; 32]); |
| 1464 | assert_eq!(i.evictable(), (700, 3)); |
| 1465 | i.clear_evicting(&[3; 32]); |
| 1466 | assert_eq!(i.evictable(), (1000, 4)); |
| 1467 | assert!(i.mark_evicting(&[3; 32])); |
| 1468 | let gone = i.remove(&[3; 32]).unwrap(); |
| 1469 | assert_eq!(gone.phys_bytes, 300); |
| 1470 | assert_eq!(i.total_bytes(), 700); |
| 1471 | assert_eq!(i.evicting(), (0, 0)); |
| 1472 | assert_eq!(i.len(), 3); |
| 1473 | i.audit().unwrap(); |
| 1474 | // Replacing an entry keeps the aggregates right. |
| 1475 | assert!(i.insert(entry(1, 50, 9, 0)).is_some()); |
| 1476 | assert_eq!(i.total_bytes(), 650); |
| 1477 | i.audit().unwrap(); |
| 1478 | } |
| 1479 | |
| 1480 | #[test] |
| 1481 | fn index_victim_prefers_foreign_then_oldest_and_never_busy_or_marked() { |
| 1482 | let mut i = Index::new(); |
| 1483 | i.insert(entry(1, 10, 5, 0)); |
| 1484 | i.insert(entry(2, 10, 3, 0)); |
| 1485 | i.insert(entry(3, 10, 9, FLAG_FOREIGN)); |
| 1486 | i.insert(entry(4, 10, 1, 0)); |
| 1487 | assert_eq!( |
| 1488 | i.pick_victim(), |
| 1489 | Some([3; 32]), |
| 1490 | "foreign first, whatever its age" |
| 1491 | ); |
| 1492 | assert!(i.mark_evicting(&[3; 32])); |
| 1493 | assert_eq!(i.pick_victim(), Some([4; 32]), "then the oldest"); |
| 1494 | assert!(i.busy_inc(&[4; 32])); |
| 1495 | assert_eq!(i.pick_victim(), Some([2; 32])); |
| 1496 | i.touch(&[2; 32], 100); |
| 1497 | assert_eq!(i.pick_victim(), Some([1; 32]), "a load raises recency"); |
| 1498 | assert!(i.busy_inc(&[1; 32])); |
| 1499 | assert!(i.busy_inc(&[2; 32])); |
| 1500 | assert_eq!(i.pick_victim(), None); |
| 1501 | } |
| 1502 | |
| 1503 | // ---- victims are removed only after the new object is durable (C4 open item) ----- |
| 1504 | |
| 1505 | #[test] |
| 1506 | fn failed_write_keeps_the_victim_it_would_have_evicted() { |
| 1507 | for boundary in [Boundary::P1Write, Boundary::P2FsyncFile] { |
| 1508 | let fx = Fx::new(); |
| 1509 | let s = fx.open_with(|c| c.quota_bytes = OBJ); |
| 1510 | let a = fx.put_full(&s, 1); |
| 1511 | fx.faults.arm(boundary, FaultAction::Fail(EIO)); |
| 1512 | let b = fx.put_full(&s, 2); |
| 1513 | let t = terminal_of(&b.completions, b.job.unwrap()); |
| 1514 | assert!(matches!(t, Terminal::Failed(_)), "{boundary:?}: {t:?}"); |
| 1515 | assert_eq!( |
| 1516 | s.lookup(&[a.key, b.key]), |
| 1517 | vec![Some(FULL_PAYLOAD), None], |
| 1518 | "{boundary:?}: the old object is still served" |
| 1519 | ); |
| 1520 | assert!(fx.object_file(&a.key).is_file(), "{boundary:?}"); |
| 1521 | let cap = s.capacity(); |
| 1522 | assert_eq!(cap.used_bytes, OBJ, "{boundary:?}"); |
| 1523 | assert_eq!(cap.evicting_bytes, 0, "{boundary:?}"); |
| 1524 | assert_eq!(cap.reserved_bytes, 0, "{boundary:?}"); |
| 1525 | assert_loads(&fx, &s, a.key, 1, 8); |
| 1526 | s.audit().unwrap(); |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | #[test] |
| 1531 | fn indeterminate_publish_keeps_the_victim_until_the_new_object_is_known_durable() { |
| 1532 | let fx = Fx::new(); |
| 1533 | let s = fx.open_with(|c| c.quota_bytes = OBJ); |
| 1534 | let a = fx.put_full(&s, 1); |
| 1535 | fx.faults.arm(Boundary::P4FsyncDir, FaultAction::Fail(EIO)); |
| 1536 | let b = fx.put_full(&s, 2); |
| 1537 | let c = completion_of(&b.completions, b.job.unwrap()); |
| 1538 | assert_eq!(c.publish, PublishOutcome::Indeterminate); |
| 1539 | assert_eq!( |
| 1540 | s.lookup(&[a.key, b.key]), |
| 1541 | vec![Some(FULL_PAYLOAD), Some(FULL_PAYLOAD)], |
| 1542 | "both survive: the new one may not outlive a power cut" |
| 1543 | ); |
| 1544 | assert!(fx.object_file(&a.key).is_file()); |
| 1545 | assert_eq!(s.capacity().evicting_bytes, 0); |
| 1546 | s.audit().unwrap(); |
| 1547 | } |
| 1548 | |
| 1549 | #[test] |
| 1550 | fn cancel_before_the_write_ends_leaves_every_victim_in_service() { |
| 1551 | let fx = Fx::new(); |
| 1552 | let s = fx.open_with(|c| c.quota_bytes = OBJ); |
| 1553 | let a = fx.put_full(&s, 1); |
| 1554 | let (job, _) = fx.submit(&s, 2, 8); |
| 1555 | let job = job.unwrap(); |
| 1556 | assert_eq!(s.cancel(job), CancelOutcome::Cancelled); |
| 1557 | s.run_until_idle(); |
| 1558 | assert_eq!(terminal_of(&drain(&s), job), Terminal::Cancelled); |
| 1559 | assert_eq!(s.lookup(&[a.key]), vec![Some(FULL_PAYLOAD)]); |
| 1560 | assert!(fx.object_file(&a.key).is_file()); |
| 1561 | assert_eq!(s.capacity().evicting_bytes, 0); |
| 1562 | s.audit().unwrap(); |
| 1563 | } |