oxedyne/fe2o3/fe2o3_o3db_sync/tests/gc_read_hold.rs
4.1 KiB, 1 run
created by r1870400018:63099, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A read that waits behind a collection longer than it will wait for its location must give back |
| 2 | //! the reader pin the file bot takes for it when it finally grants the location (L-DC2, |
| 3 | //! 2026-10-01). The reader had gone, so it never sent `ReadFinished`, the file kept a reader for |
| 4 | //! the life of the process, and a file with a reader is neither collected nor deleted: a fully |
| 5 | //! superseded file stayed on the disk. `test::hooks::set_commit_delay` holds the collection of |
| 6 | //! file 1 for longer than the read deadline, and is process-wide, which is why this is a test |
| 7 | //! binary of its own with a single test. |
| 8 | |
| 9 | mod gc_pair; |
| 10 | |
| 11 | use gc_pair::*; |
| 12 | |
| 13 | use oxedyne_fe2o3_core::prelude::*; |
| 14 | use oxedyne_fe2o3_iop_db::api::Database; |
| 15 | use oxedyne_fe2o3_o3db_sync::{ |
| 16 | file::core::FileType, |
| 17 | test::{ |
| 18 | hooks, |
| 19 | setup::{ |
| 20 | self, |
| 21 | Uid, |
| 22 | }, |
| 23 | }, |
| 24 | }; |
| 25 | |
| 26 | use std::{ |
| 27 | path::Path, |
| 28 | thread, |
| 29 | time::{ |
| 30 | Duration, |
| 31 | Instant, |
| 32 | }, |
| 33 | }; |
| 34 | |
| 35 | #[test] |
| 36 | fn main() -> Outcome<()> { |
| 37 | log_set_level!("error"); |
| 38 | let dir = "./test_db_gc_read_hold"; |
| 39 | let _ = std::fs::remove_dir_all(dir); |
| 40 | res!(std::fs::create_dir_all(dir)); |
| 41 | let root = res!(Path::new(dir).canonicalize()); |
| 42 | |
| 43 | let probe = res!(probe("./test_db_gc_read_hold_probe")); |
| 44 | let len = probe.len; |
| 45 | let cfg = res!(config(5 * len + len / 2)); |
| 46 | let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1)); |
| 47 | |
| 48 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, true, true)); |
| 49 | let mut want = vec![1u8; NKEYS]; |
| 50 | res!(fill(&db)); |
| 51 | // Reopened, so that the cache holds locations and no values, and every read of a record in |
| 52 | // file 1 goes to its file bot. In the process that wrote the records their values are |
| 53 | // served from the cache and a read never reaches the file bot at all. |
| 54 | res!(db.close()); |
| 55 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, true, false)); |
| 56 | |
| 57 | // 1. The collection of file 1, held for seven seconds between its renames, which is longer |
| 58 | // than a read waits for its location. Superseding two of its records starts it. |
| 59 | hooks::set_commit_delay(Duration::from_secs(7)); |
| 60 | for i in 0..2 { |
| 61 | res!(db.insert(key(i), value(i, 2), Uid::default(), None)); |
| 62 | want[i] = 2; |
| 63 | } |
| 64 | assert!(wait_until(Duration::from_secs(30), || size(&d1) == 3 * len), |
| 65 | "the collection never committed its data file"); |
| 66 | |
| 67 | // 2. A read of a record the collection carried. It reaches the file bot while the file is |
| 68 | // held, is buffered there, and its reader stops waiting five seconds on. Whether that read |
| 69 | // is answered or refused is not what this test is about. |
| 70 | let t = Instant::now(); |
| 71 | let held = db.get(&key(2), None); |
| 72 | msg!("The read held behind the collection returned {} after {:?}.", |
| 73 | if held.is_ok() { "an answer" } else { "an error" }, t.elapsed()); |
| 74 | assert!(wait_until(Duration::from_secs(30), || size(&i1) == probe.ind[3]), |
| 75 | "the collection never finished"); |
| 76 | hooks::set_commit_delay(Duration::ZERO); |
| 77 | // Let the file bot replay the buffered read: it grants a location to a reader that is gone. |
| 78 | thread::sleep(Duration::from_millis(500)); |
| 79 | assert_eq!(judge(&db, &want, "After the collection"), 0); |
| 80 | |
| 81 | // 3. Every record left in file 1 superseded, so that the file is wholly old. A file bot that |
| 82 | // still counts a reader for the file keeps it, and its three records stay on the disk. |
| 83 | for i in 2..5 { |
| 84 | res!(db.insert(key(i), value(i, 3), Uid::default(), None)); |
| 85 | want[i] = 3; |
| 86 | } |
| 87 | assert!(wait_until(Duration::from_secs(10), || size(&d1) == 0), |
| 88 | "file 1 is wholly superseded and is still {} bytes: a reader pin was never returned", |
| 89 | size(&d1)); |
| 90 | assert_eq!(judge(&db, &want, "Settled"), 0); |
| 91 | |
| 92 | res!(db.close()); |
| 93 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, true, false)); |
| 94 | assert_eq!(judge(&db, &want, "After reopening"), 0); |
| 95 | res!(db.close()); |
| 96 | log_finish_wait!(); |
| 97 | |
| 98 | let _ = std::fs::remove_dir_all(dir); |
| 99 | Ok(()) |
| 100 | } |