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-expert-store / tests / adapter_tests.rs
| 1 | //! The bounded read adapter over the deterministic fake source: verification |
| 2 | //! before ready, scrubbing on failure, every bound, deadlines, cancellation |
| 3 | //! and concurrent readers. |
| 4 | |
| 5 | mod common; |
| 6 | |
| 7 | use aien_expert_store::fake::{FakeSource, Fault}; |
| 8 | use aien_expert_store::{ |
| 9 | CancelOutcome, IndexEntry, Outcome, Projection, ReadAdapter, ReadCompletion, ReadError, |
| 10 | ReadLimits, StagingBuffer, |
| 11 | }; |
| 12 | use common::*; |
| 13 | use std::io::ErrorKind; |
| 14 | use std::sync::Arc; |
| 15 | use std::time::{Duration, Instant}; |
| 16 | |
| 17 | const WAIT: Duration = Duration::from_secs(10); |
| 18 | |
| 19 | fn limits() -> ReadLimits { |
| 20 | ReadLimits { |
| 21 | max_in_flight: 2, |
| 22 | max_inflight_bytes: 1 << 20, |
| 23 | max_object_bytes: 512 << 10, |
| 24 | chunk_bytes: 4096, |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn setup(lim: ReadLimits) -> (FakeSource, ReadAdapter, IndexEntry, Vec<u8>, Vec<u8>) { |
| 29 | let fake = FakeSource::new(); |
| 30 | let (e, bytes, w, s) = artifact(key(1, 2, Projection::Up), 192, 256, 3); |
| 31 | fake.insert(e.data_digest, bytes); |
| 32 | let a = ReadAdapter::new(Arc::new(fake.clone()), lim).unwrap(); |
| 33 | (fake, a, e, w, s) |
| 34 | } |
| 35 | |
| 36 | fn far() -> Instant { |
| 37 | Instant::now() + Duration::from_secs(30) |
| 38 | } |
| 39 | |
| 40 | fn buf(e: &IndexEntry) -> StagingBuffer { |
| 41 | StagingBuffer::new(e.object_len as usize).unwrap() |
| 42 | } |
| 43 | |
| 44 | fn one(a: &ReadAdapter, e: IndexEntry) -> ReadCompletion { |
| 45 | let t = a.submit(e, buf(&e), far()).unwrap(); |
| 46 | let c = a.wait(WAIT).expect("terminal"); |
| 47 | assert_eq!(c.ticket, t); |
| 48 | c |
| 49 | } |
| 50 | |
| 51 | fn expect_failed(c: ReadCompletion) -> ReadError { |
| 52 | match c.outcome { |
| 53 | Outcome::Failed { error, buf } => { |
| 54 | assert!( |
| 55 | buf.as_slice().iter().all(|&b| b == 0), |
| 56 | "buffer not scrubbed" |
| 57 | ); |
| 58 | error |
| 59 | } |
| 60 | o => panic!("expected Failed, got {o:?}"), |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn ready_only_after_verification_and_bytes_round_trip() { |
| 66 | let (_f, a, e, w, s) = setup(limits()); |
| 67 | match one(&a, e).outcome { |
| 68 | Outcome::Ready(r) => { |
| 69 | assert_eq!(r.weights(), &w[..]); |
| 70 | assert_eq!(r.scales(), &s[..]); |
| 71 | assert_eq!(r.header.key, e.key); |
| 72 | assert_eq!(r.artifact().len() as u64, e.object_len); |
| 73 | } |
| 74 | o => panic!("{o:?}"), |
| 75 | } |
| 76 | assert_eq!(a.in_flight(), (0, 0)); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn corruption_is_a_digest_mismatch_and_scrubs() { |
| 81 | let (f, a, e, _, _) = setup(limits()); |
| 82 | for at in [0u64, 5000, e.object_len - 1] { |
| 83 | f.set_fault(e.data_digest, Fault::CorruptByte(at)); |
| 84 | assert_eq!( |
| 85 | expect_failed(one(&a, e)), |
| 86 | ReadError::DigestMismatch, |
| 87 | "at {at}" |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn truncation_eof_and_io_errors_are_explicit() { |
| 94 | let (f, a, e, _, _) = setup(limits()); |
| 95 | f.set_fault(e.data_digest, Fault::Truncate(e.object_len - 4096)); |
| 96 | assert_eq!( |
| 97 | expect_failed(one(&a, e)), |
| 98 | ReadError::LengthMismatch { |
| 99 | expected: e.object_len, |
| 100 | found: e.object_len - 4096 |
| 101 | } |
| 102 | ); |
| 103 | f.set_fault(e.data_digest, Fault::EofAt(8192 + 100)); |
| 104 | assert_eq!( |
| 105 | expect_failed(one(&a, e)), |
| 106 | ReadError::UnexpectedEof { |
| 107 | read: 8192 + 100, |
| 108 | expected: e.object_len |
| 109 | } |
| 110 | ); |
| 111 | f.set_fault(e.data_digest, Fault::IoError(ErrorKind::Other)); |
| 112 | assert_eq!(expect_failed(one(&a, e)), ReadError::Io(ErrorKind::Other)); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn short_reads_are_resumed_not_trusted() { |
| 117 | let (f, a, e, w, _) = setup(limits()); |
| 118 | f.set_fault(e.data_digest, Fault::ShortReads(1000)); |
| 119 | match one(&a, e).outcome { |
| 120 | Outcome::Ready(r) => assert_eq!(r.weights(), &w[..]), |
| 121 | o => panic!("{o:?}"), |
| 122 | } |
| 123 | assert!(f.reads() as u64 >= e.object_len / 1000); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn wrong_hash_wrong_object_and_wrong_key_are_refused() { |
| 128 | let (f, a, e, _, _) = setup(limits()); |
| 129 | // Index digest names no object. |
| 130 | let mut missing = e; |
| 131 | missing.data_digest[0] ^= 1; |
| 132 | assert_eq!(expect_failed(one(&a, missing)), ReadError::NotFound); |
| 133 | // The object stored under the digest is a different (valid) artifact. |
| 134 | let (other, other_bytes, _, _) = artifact(key(1, 2, Projection::Up), 192, 256, 77); |
| 135 | assert_ne!(other.data_digest, e.data_digest); |
| 136 | f.insert(e.data_digest, other_bytes.clone()); |
| 137 | assert_eq!(expect_failed(one(&a, e)), ReadError::DigestMismatch); |
| 138 | // The digest matches, but the index entry claims another key. |
| 139 | f.insert(other.data_digest, other_bytes); |
| 140 | let mut lying = other; |
| 141 | lying.key = key(9, 9, Projection::Down); |
| 142 | assert_eq!(expect_failed(one(&a, lying)), ReadError::HeaderMismatch); |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn submit_bounds_refuse_and_return_the_buffer() { |
| 147 | let (f, a, e, _, _) = setup(limits()); |
| 148 | // Insufficient capacity. |
| 149 | let small = StagingBuffer::new(4096).unwrap(); |
| 150 | let r = a.submit(e, small, far()).unwrap_err(); |
| 151 | assert_eq!( |
| 152 | r.error, |
| 153 | ReadError::InsufficientCapacity { |
| 154 | need: e.object_len, |
| 155 | have: 4096 |
| 156 | } |
| 157 | ); |
| 158 | assert_eq!(r.buf.capacity(), 4096); |
| 159 | // Object larger than max_object_bytes. |
| 160 | let mut huge = e; |
| 161 | huge.object_len = 1 << 20; |
| 162 | let r = a |
| 163 | .submit(huge, StagingBuffer::new(1 << 20).unwrap(), far()) |
| 164 | .unwrap_err(); |
| 165 | assert_eq!(r.error, ReadError::ObjectTooLarge); |
| 166 | // Malformed entry length. |
| 167 | let mut odd = e; |
| 168 | odd.object_len = 4097; |
| 169 | assert_eq!( |
| 170 | a.submit(odd, buf(&e), far()).unwrap_err().error, |
| 171 | ReadError::InvalidEntry |
| 172 | ); |
| 173 | // Deadline already passed. |
| 174 | let r = a.submit(e, buf(&e), Instant::now()).unwrap_err(); |
| 175 | assert_eq!(r.error, ReadError::DeadlineExceeded); |
| 176 | // Queue depth: hold the device, fill both slots, the third is refused. |
| 177 | f.hold(); |
| 178 | let t1 = a.submit(e, buf(&e), far()).unwrap(); |
| 179 | let t2 = a.submit(e, buf(&e), far()).unwrap(); |
| 180 | assert_eq!( |
| 181 | a.submit(e, buf(&e), far()).unwrap_err().error, |
| 182 | ReadError::QueueFull |
| 183 | ); |
| 184 | assert_eq!(a.in_flight(), (2, 2 * e.object_len)); |
| 185 | f.release(); |
| 186 | let mut got = vec![a.wait(WAIT).unwrap().ticket, a.wait(WAIT).unwrap().ticket]; |
| 187 | got.sort(); |
| 188 | assert_eq!(got, vec![t1, t2]); |
| 189 | assert_eq!(a.in_flight(), (0, 0)); |
| 190 | } |
| 191 | |
| 192 | #[test] |
| 193 | fn inflight_byte_budget_is_enforced() { |
| 194 | let fake = FakeSource::new(); |
| 195 | let (e, bytes, _, _) = artifact(key(0, 0, Projection::Gate), 192, 256, 1); |
| 196 | fake.insert(e.data_digest, bytes); |
| 197 | let lim = ReadLimits { |
| 198 | max_in_flight: 8, |
| 199 | max_inflight_bytes: e.object_len * 2 + 1, |
| 200 | ..limits() |
| 201 | }; |
| 202 | let a = ReadAdapter::new(Arc::new(fake.clone()), lim).unwrap(); |
| 203 | fake.hold(); |
| 204 | a.submit(e, buf(&e), far()).unwrap(); |
| 205 | a.submit(e, buf(&e), far()).unwrap(); |
| 206 | assert_eq!( |
| 207 | a.submit(e, buf(&e), far()).unwrap_err().error, |
| 208 | ReadError::InflightBytesExceeded |
| 209 | ); |
| 210 | fake.release(); |
| 211 | for _ in 0..2 { |
| 212 | assert!(matches!(a.wait(WAIT).unwrap().outcome, Outcome::Ready(_))); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | #[test] |
| 217 | fn invalid_limits_are_refused() { |
| 218 | let src = Arc::new(FakeSource::new()); |
| 219 | for lim in [ |
| 220 | ReadLimits { |
| 221 | max_in_flight: 0, |
| 222 | ..limits() |
| 223 | }, |
| 224 | ReadLimits { |
| 225 | max_inflight_bytes: 0, |
| 226 | ..limits() |
| 227 | }, |
| 228 | ReadLimits { |
| 229 | max_object_bytes: 0, |
| 230 | ..limits() |
| 231 | }, |
| 232 | ReadLimits { |
| 233 | chunk_bytes: 1000, |
| 234 | ..limits() |
| 235 | }, |
| 236 | ] { |
| 237 | assert_eq!( |
| 238 | ReadAdapter::new(src.clone(), lim).err(), |
| 239 | Some(ReadError::InvalidLimits) |
| 240 | ); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | fn a_stalled_read_hits_its_deadline_with_no_ready_state() { |
| 246 | let (f, a, e, _, _) = setup(limits()); |
| 247 | f.set_fault(e.data_digest, Fault::Delay(Duration::from_millis(20))); |
| 248 | let t0 = Instant::now(); |
| 249 | a.submit(e, buf(&e), Instant::now() + Duration::from_millis(60)) |
| 250 | .unwrap(); |
| 251 | let c = a.wait(WAIT).unwrap(); |
| 252 | assert_eq!(expect_failed(c), ReadError::DeadlineExceeded); |
| 253 | // Detected within a few chunk delays, not after the whole object. |
| 254 | assert!(t0.elapsed() < Duration::from_secs(2)); |
| 255 | assert_eq!(a.in_flight(), (0, 0)); |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn cancellation_yields_one_scrubbed_terminal() { |
| 260 | let (f, a, e, _, _) = setup(limits()); |
| 261 | f.hold(); |
| 262 | let t = a.submit(e, buf(&e), far()).unwrap(); |
| 263 | assert_eq!(a.cancel(t), CancelOutcome::Requested); |
| 264 | f.release(); |
| 265 | let c = a.wait(WAIT).unwrap(); |
| 266 | assert_eq!(c.ticket, t); |
| 267 | match c.outcome { |
| 268 | Outcome::Cancelled { buf } => assert!(buf.as_slice().iter().all(|&b| b == 0)), |
| 269 | o => panic!("{o:?}"), |
| 270 | } |
| 271 | assert!( |
| 272 | a.wait(Duration::from_millis(50)).is_none(), |
| 273 | "exactly one terminal" |
| 274 | ); |
| 275 | assert_eq!(a.cancel(t), CancelOutcome::Unknown); |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn concurrent_readers_respect_queue_depth_and_all_verify() { |
| 280 | let fake = FakeSource::new(); |
| 281 | let mut entries = Vec::new(); |
| 282 | for ex in 0..8u32 { |
| 283 | let (e, bytes, w, _) = artifact(key(0, ex, Projection::Gate), 192, 256, ex); |
| 284 | fake.insert(e.data_digest, bytes); |
| 285 | entries.push((e, w)); |
| 286 | } |
| 287 | fake.set_fault(entries[3].0.data_digest, Fault::CorruptByte(9000)); |
| 288 | let lim = ReadLimits { |
| 289 | max_in_flight: 4, |
| 290 | max_inflight_bytes: 64 << 20, |
| 291 | ..limits() |
| 292 | }; |
| 293 | let a = Arc::new(ReadAdapter::new(Arc::new(fake.clone()), lim).unwrap()); |
| 294 | let readers: Vec<_> = (0..6) |
| 295 | .map(|t| { |
| 296 | let a = Arc::clone(&a); |
| 297 | let entries = entries.clone(); |
| 298 | std::thread::spawn(move || { |
| 299 | let mut submitted = 0; |
| 300 | for i in 0..16 { |
| 301 | let (e, _) = entries[(t + i) % entries.len()]; |
| 302 | let mut b = buf(&e); |
| 303 | loop { |
| 304 | match a.submit(e, b, far()) { |
| 305 | Ok(_) => break, |
| 306 | Err(r) => { |
| 307 | assert_eq!(r.error, ReadError::QueueFull); |
| 308 | b = r.buf; |
| 309 | std::thread::yield_now(); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | submitted += 1; |
| 314 | } |
| 315 | submitted |
| 316 | }) |
| 317 | }) |
| 318 | .collect(); |
| 319 | let total: usize = readers.into_iter().map(|h| h.join().unwrap()).sum(); |
| 320 | let (mut ok, mut bad) = (0, 0); |
| 321 | for _ in 0..total { |
| 322 | match a.wait(WAIT).expect("terminal").outcome { |
| 323 | Outcome::Ready(r) => { |
| 324 | let (_, w) = entries.iter().find(|(e, _)| e.key == r.header.key).unwrap(); |
| 325 | assert_eq!(r.weights(), &w[..]); |
| 326 | ok += 1; |
| 327 | } |
| 328 | Outcome::Failed { error, buf } => { |
| 329 | assert_eq!(error, ReadError::DigestMismatch); |
| 330 | assert!(buf.as_slice().iter().all(|&b| b == 0)); |
| 331 | bad += 1; |
| 332 | } |
| 333 | o => panic!("{o:?}"), |
| 334 | } |
| 335 | } |
| 336 | assert_eq!(ok + bad, 96); |
| 337 | assert_eq!( |
| 338 | bad, 12, |
| 339 | "every read of the corrupt object fails, no other does" |
| 340 | ); |
| 341 | assert!(fake.peak() <= 4, "peak {} exceeds queue depth", fake.peak()); |
| 342 | assert_eq!(a.in_flight(), (0, 0)); |
| 343 | } |
| 344 | |
| 345 | #[test] |
| 346 | fn drop_cancels_queued_reads_and_joins() { |
| 347 | let (f, a, e, _, _) = setup(limits()); |
| 348 | f.hold(); |
| 349 | a.submit(e, buf(&e), far()).unwrap(); |
| 350 | let releaser = { |
| 351 | let f = f.clone(); |
| 352 | std::thread::spawn(move || { |
| 353 | std::thread::sleep(Duration::from_millis(50)); |
| 354 | f.release(); |
| 355 | }) |
| 356 | }; |
| 357 | drop(a); |
| 358 | releaser.join().unwrap(); |
| 359 | } |