oxedyne/fe2o3/fe2o3_o3db_sync/tests/gc_mixed_pairs.rs
5.3 KiB, 1 run
created by r1870400018:62335, 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 | //! The start must not believe an index that is not its data file's own, whichever file of the pair |
| 2 | //! is the newer, and must leave alone an index that is (D-C, 2026-10-01). A collection replaces a |
| 3 | //! file's data and index with shorter ones, so an interruption between the two renames leaves one |
| 4 | //! generation's file beside the other's. Stores written before the collection committed its data |
| 5 | //! file first hold the other mixture, the index of the new generation beside the old data. Both |
| 6 | //! used to be believed: the cache was filled from the index, the data file's own entries were |
| 7 | //! refused as no newer, and records were read from the offsets of the file beside them. |
| 8 | //! |
| 9 | //! The two generations of file 1 are made by really collecting it, and each mixture is made by |
| 10 | //! putting one generation's file beside the other's in the store. Nothing here needs a hook. |
| 11 | |
| 12 | mod gc_pair; |
| 13 | |
| 14 | use gc_pair::*; |
| 15 | |
| 16 | use oxedyne_fe2o3_core::prelude::*; |
| 17 | use oxedyne_fe2o3_iop_db::api::Database; |
| 18 | use oxedyne_fe2o3_o3db_sync::{ |
| 19 | file::core::FileType, |
| 20 | test::setup::{ |
| 21 | self, |
| 22 | Uid, |
| 23 | }, |
| 24 | }; |
| 25 | |
| 26 | use std::{ |
| 27 | path::Path, |
| 28 | time::Duration, |
| 29 | }; |
| 30 | |
| 31 | #[test] |
| 32 | fn main() -> Outcome<()> { |
| 33 | log_set_level!("error"); |
| 34 | let dir = "./test_db_gc_mixed_pairs"; |
| 35 | let _ = std::fs::remove_dir_all(dir); |
| 36 | res!(std::fs::create_dir_all(dir)); |
| 37 | let root = res!(Path::new(dir).canonicalize()); |
| 38 | let probe = res!(probe("./test_db_gc_mixed_pairs_probe")); |
| 39 | let len = probe.len; |
| 40 | let cfg = res!(config(5 * len + len / 2)); |
| 41 | let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1)); |
| 42 | // The generations are kept outside the store, where nothing will look for them. |
| 43 | let sdir = "./test_db_gc_mixed_pairs_stash"; |
| 44 | let _ = std::fs::remove_dir_all(sdir); |
| 45 | res!(std::fs::create_dir_all(sdir)); |
| 46 | let sdir = res!(Path::new(sdir).canonicalize()); |
| 47 | let stash = |name: &str| sdir.join(name); |
| 48 | |
| 49 | // The old generation of file 1: five records. |
| 50 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, true, true)); |
| 51 | res!(fill(&db)); |
| 52 | res!(db.close()); |
| 53 | assert_eq!((size(&d1), size(&i1)), (5 * len, probe.ind[5]), "file 1 is not five records"); |
| 54 | res!(std::fs::copy(&d1, stash("old.dat"))); |
| 55 | res!(std::fs::copy(&i1, stash("old.ind"))); |
| 56 | |
| 57 | // The new generation: superseding keys 0 and 1 collects file 1 down to keys 2 to 4. |
| 58 | let mut want = vec![1u8; NKEYS]; |
| 59 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, true, false)); |
| 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(60), || size(&d1) == 3 * len && size(&i1) == probe.ind[3]), |
| 65 | "file 1 was not collected to three records"); |
| 66 | res!(db.close()); |
| 67 | res!(std::fs::copy(&d1, stash("new.dat"))); |
| 68 | res!(std::fs::copy(&i1, stash("new.ind"))); |
| 69 | |
| 70 | let mut bad = Vec::new(); |
| 71 | |
| 72 | // The new index beside the old data file: what a collection used to leave. |
| 73 | res!(std::fs::copy(stash("old.dat"), &d1)); |
| 74 | res!(std::fs::copy(stash("new.ind"), &i1)); |
| 75 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, false, false)); |
| 76 | let n = judge(&db, &want, "New index beside old data"); |
| 77 | if n != 0 { bad.push(fmt!("{} keys read wrong with the new index beside the old data", n)); } |
| 78 | res!(db.close()); |
| 79 | if size(&i1) != probe.ind[5] { |
| 80 | bad.push(fmt!("the index beside the old data was {} bytes, not rebuilt to {}", |
| 81 | size(&i1), probe.ind[5])); |
| 82 | } |
| 83 | |
| 84 | // The old index beside the new data file: what an interruption leaves. |
| 85 | res!(std::fs::copy(stash("new.dat"), &d1)); |
| 86 | res!(std::fs::copy(stash("old.ind"), &i1)); |
| 87 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, false, false)); |
| 88 | let n = judge(&db, &want, "Old index beside new data"); |
| 89 | if n != 0 { bad.push(fmt!("{} keys read wrong with the old index beside the new data", n)); } |
| 90 | res!(db.close()); |
| 91 | if size(&i1) != probe.ind[3] { |
| 92 | bad.push(fmt!("the index beside the new data was {} bytes, not rebuilt to {}", |
| 93 | size(&i1), probe.ind[3])); |
| 94 | } |
| 95 | |
| 96 | // A pair that is whole is read as it is: the start must not rewrite an index it can believe. |
| 97 | res!(std::fs::copy(stash("new.dat"), &d1)); |
| 98 | res!(std::fs::copy(stash("new.ind"), &i1)); |
| 99 | let before = res!(res!(std::fs::metadata(&i1)).modified()); |
| 100 | let bytes = res!(std::fs::read(&i1)); |
| 101 | thread_pause(); |
| 102 | let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, false, false)); |
| 103 | let n = judge(&db, &want, "Whole pair"); |
| 104 | if n != 0 { bad.push(fmt!("{} keys read wrong with a whole pair", n)); } |
| 105 | res!(db.close()); |
| 106 | log_finish_wait!(); |
| 107 | if res!(std::fs::read(&i1)) != bytes { |
| 108 | bad.push(fmt!("the index of a whole pair was changed by the start")); |
| 109 | } |
| 110 | if res!(res!(std::fs::metadata(&i1)).modified()) != before { |
| 111 | bad.push(fmt!("the index of a whole pair was rewritten by the start")); |
| 112 | } |
| 113 | |
| 114 | assert!(bad.is_empty(), "{}", bad.join("; ")); |
| 115 | let _ = std::fs::remove_dir_all(dir); |
| 116 | let _ = std::fs::remove_dir_all(&sdir); |
| 117 | Ok(()) |
| 118 | } |
| 119 | |
| 120 | /// Long enough for a file written now to have a modification time of its own. |
| 121 | fn thread_pause() { |
| 122 | std::thread::sleep(Duration::from_millis(50)); |
| 123 | } |