oxedyne/fe2o3/fe2o3_o3db_sync/tests/gc_close.rs
3.1 KiB, 1 run
created by r1870400018:62331, 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 close that arrives while a collection is between its two renames must leave a store that |
| 2 | //! reads every key right (D-C, 2026-10-01). The collection is held between the renames for longer |
| 3 | //! than a close waits for the collectors, so the close finishes the cache bots and the file bots |
| 4 | //! with the collection still to replace its index and to ask the cache it has no one left to |
| 5 | //! answer. Before the data file was committed first, that left a new index beside the old data |
| 6 | //! file, which the next start believed. `test::hooks::set_commit_delay` is process-wide, which is |
| 7 | //! why this is a test 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 | time::{ |
| 28 | Duration, |
| 29 | Instant, |
| 30 | }, |
| 31 | }; |
| 32 | |
| 33 | #[test] |
| 34 | fn main() -> Outcome<()> { |
| 35 | log_set_level!("error"); |
| 36 | let dir = "./test_db_gc_close"; |
| 37 | let _ = std::fs::remove_dir_all(dir); |
| 38 | res!(std::fs::create_dir_all(dir)); |
| 39 | let root = res!(std::path::Path::new(dir).canonicalize()); |
| 40 | let probe = res!(probe("./test_db_gc_close_probe")); |
| 41 | let len = probe.len; |
| 42 | let cfg = res!(config(5 * len + len / 2)); |
| 43 | let zone = zone_dir(&root, &cfg); |
| 44 | let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1)); |
| 45 | |
| 46 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, true, true)); |
| 47 | let mut want = vec![1u8; NKEYS]; |
| 48 | res!(fill(&db)); |
| 49 | |
| 50 | // Held for longer than a close gives the collectors, and longer than the cache update waits. |
| 51 | hooks::set_commit_delay(Duration::from_secs(8)); |
| 52 | for i in 0..2 { |
| 53 | res!(db.insert(key(i), value(i, 2), Uid::default(), None)); |
| 54 | want[i] = 2; |
| 55 | } |
| 56 | assert!(wait_until(Duration::from_secs(30), || size(&d1) == 3 * len), |
| 57 | "the collection of file 1 never reached its renames"); |
| 58 | assert_eq!(size(&i1), probe.ind[5], "the index was replaced before the delay"); |
| 59 | let start = Instant::now(); |
| 60 | res!(db.close()); |
| 61 | msg!("The close took {:?}.", start.elapsed()); |
| 62 | hooks::set_commit_delay(Duration::ZERO); |
| 63 | |
| 64 | // The collection ran to its end after the close began. |
| 65 | let finished = wait_until(Duration::from_secs(30), || size(&i1) == probe.ind[3]); |
| 66 | let temps = temporaries(&zone); |
| 67 | let index = res!(std::fs::read(&i1)); |
| 68 | |
| 69 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, true, false)); |
| 70 | let reopened = judge(&db, &want, "After the close"); |
| 71 | res!(db.close()); |
| 72 | log_finish_wait!(); |
| 73 | let unchanged = res!(std::fs::read(&i1)) == index; |
| 74 | |
| 75 | assert!(finished, "the index of file 1 is {} bytes, not the {} of the collected file", |
| 76 | size(&i1), probe.ind[3]); |
| 77 | assert!(temps.is_empty(), "a collection left {:?} behind", temps); |
| 78 | assert_eq!(reopened, 0, "{} keys read wrong after the close", reopened); |
| 79 | assert!(unchanged, "the start rebuilt an index that agreed with its data file"); |
| 80 | let _ = std::fs::remove_dir_all(dir); |
| 81 | Ok(()) |
| 82 | } |