oxedyne/fe2o3/fe2o3_o3db_sync/tests/gc_abort.rs
5.1 KiB, 1 run
created by r1870400018:62329, 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 collection that fails before it has replaced anything must leave the file's pair as it was, |
| 2 | //! must not leave the file held, and must leave nothing the next collection of the file would be |
| 3 | //! hurt by (D-C, 2026-10-01). Until then a failed collection returned an error that was only |
| 4 | //! logged: the file bot went on holding every read and write of the file for a collection that was |
| 5 | //! not coming, so the file could never be collected again, and a temporary file the collection had |
| 6 | //! begun was left to be appended to by the next one. `test::hooks::set_collect_failure` fails |
| 7 | //! each collection once it has written its temporary files, and is process-wide, which is why this |
| 8 | //! is a test binary of its own with a single test. |
| 9 | |
| 10 | mod gc_pair; |
| 11 | |
| 12 | use gc_pair::*; |
| 13 | |
| 14 | use oxedyne_fe2o3_core::prelude::*; |
| 15 | use oxedyne_fe2o3_iop_db::api::Database; |
| 16 | use oxedyne_fe2o3_o3db_sync::{ |
| 17 | file::{ |
| 18 | core::FileType, |
| 19 | zdir::ZoneDir, |
| 20 | }, |
| 21 | test::{ |
| 22 | hooks, |
| 23 | setup::{ |
| 24 | self, |
| 25 | Uid, |
| 26 | }, |
| 27 | }, |
| 28 | }; |
| 29 | |
| 30 | use std::{ |
| 31 | path::Path, |
| 32 | time::Duration, |
| 33 | }; |
| 34 | |
| 35 | #[test] |
| 36 | fn main() -> Outcome<()> { |
| 37 | log_set_level!("error"); |
| 38 | let dir = "./test_db_gc_abort"; |
| 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_abort_probe")); |
| 44 | let len = probe.len; |
| 45 | let cfg = res!(config(5 * len + len / 2)); |
| 46 | let zone = zone_dir(&root, &cfg); |
| 47 | let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1)); |
| 48 | |
| 49 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, true, true)); |
| 50 | let mut want = vec![1u8; NKEYS]; |
| 51 | res!(fill(&db)); |
| 52 | let (d_old, i_old) = (size(&d1), size(&i1)); |
| 53 | assert_eq!(d_old, 5 * len, "file 1 does not hold the five records it was sized for"); |
| 54 | |
| 55 | // 1. The collection that fails. Superseding two of file 1's records starts it, which then |
| 56 | // waits a second and a half before it reads the file, and a third record is superseded |
| 57 | // while it waits, so that the file bot holds that supersession in the collection's buffer. |
| 58 | hooks::set_collect_delay(Duration::from_millis(1_500)); |
| 59 | hooks::set_collect_failure(true); |
| 60 | for i in 0..3 { |
| 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), || hooks::collections_failed() >= 1), |
| 65 | "the collection of file 1 never started"); |
| 66 | |
| 67 | // 2. The file is not held. When the collection is abandoned the file bot replays what it |
| 68 | // held, and the third supersession finds the file eligible again, so a second collection |
| 69 | // starts and fails. A file bot still waiting on the first would never start another. |
| 70 | assert!(wait_until(Duration::from_secs(30), || hooks::collections_failed() >= 2), |
| 71 | "file 1 was never collected again after a collection of it failed"); |
| 72 | // The abandoned collections take their temporaries with them. |
| 73 | assert!(wait_until(Duration::from_secs(10), || temporaries(&zone).is_empty()), |
| 74 | "an abandoned collection left {:?} behind", temporaries(&zone)); |
| 75 | hooks::set_collect_delay(Duration::ZERO); |
| 76 | assert_eq!((size(&d1), size(&i1)), (d_old, i_old), |
| 77 | "file 1 is not the pair it was after collections that replaced nothing"); |
| 78 | let held = judge(&db, &want, "After two failed collections"); |
| 79 | |
| 80 | // 3. A temporary left in the directory, as a crash or a failed removal would leave it, must |
| 81 | // not become the head of the next collected file. |
| 82 | let mut stale = zone.clone(); |
| 83 | stale.push(ZoneDir::relative_gc_temp_path(&FileType::Data, 1)); |
| 84 | res!(std::fs::write(&stale, vec![0xabu8; 777])); |
| 85 | hooks::set_collect_failure(false); |
| 86 | res!(db.insert(key(3), value(3, 2), Uid::default(), None)); |
| 87 | want[3] = 2; |
| 88 | // Keys 0 to 3 are superseded, so the collection carries key 4's record alone. |
| 89 | assert!(wait_until(Duration::from_secs(30), || size(&d1) != d_old), |
| 90 | "file 1 was not collected once collection could succeed"); |
| 91 | assert!(wait_until(Duration::from_secs(10), || size(&i1) == probe.ind[1]), |
| 92 | "the index of file 1 was not rebuilt to one record"); |
| 93 | let collected = size(&d1); |
| 94 | let after = judge(&db, &want, "After the collection that worked"); |
| 95 | res!(db.close()); |
| 96 | |
| 97 | let index = res!(std::fs::read(&i1)); |
| 98 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, true, false)); |
| 99 | let reopened = judge(&db, &want, "After reopening"); |
| 100 | res!(db.close()); |
| 101 | log_finish_wait!(); |
| 102 | let unchanged = res!(std::fs::read(&i1)) == index; |
| 103 | |
| 104 | assert_eq!(held, 0, "{} keys read wrong after the failed collections", held); |
| 105 | assert_eq!(collected, len, |
| 106 | "file 1 is {} bytes after its collection, where its one current record is {}", collected, len); |
| 107 | assert_eq!(after, 0, "{} keys read wrong after the collection that worked", after); |
| 108 | assert_eq!(reopened, 0, "{} keys read wrong after reopening", reopened); |
| 109 | assert!(unchanged, "the start rebuilt an index that agreed with its data file"); |
| 110 | let _ = std::fs::remove_dir_all(dir); |
| 111 | Ok(()) |
| 112 | } |