Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_o3db_sync/tests/gc_pair/mod.rs

13.4 KiB, 20 runs

created by r1870400018:62337, 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//! What the collection tests share (D-C, 2026-10-01): a one-zone store whose data files hold five
2//! records of one length each. Superseding two of file 1's records then crosses the collection
3//! trigger, and the collected file is exactly three records long, so the two generations of its
4//! pair differ in size and can be told apart from outside.
5
6#![allow(dead_code)]
7
8use oxedyne_fe2o3_core::prelude::*;
9use oxedyne_fe2o3_hash::{
10 csum::ChecksumScheme,
11 hash::HashScheme,
12};
13use oxedyne_fe2o3_iop_db::api::Database;
14use oxedyne_fe2o3_jdat::prelude::*;
15use oxedyne_fe2o3_o3db_sync::{
16 O3db,
17 base::{
18 cfg::OzoneConfig,
19 constant,
20 },
21 data::core::RestSchemesInput,
22 file::{
23 core::FileType,
24 zdir::ZoneDir,
25 },
26 test::setup::{
27 self,
28 Uid,
29 UID_LEN,
30 },
31};
32
33use std::{
34 collections::{
35 BTreeMap,
36 BTreeSet,
37 },
38 fs,
39 path::{
40 Path,
41 PathBuf,
42 },
43 thread,
44 time::{
45 Duration,
46 Instant,
47 },
48};
49
50pub type TestDb = O3db<
51 { UID_LEN },
52 Uid,
53 (),
54 HashScheme,
55 HashScheme,
56 ChecksumScheme,
57>;
58
59pub const NKEYS: usize = 12;
60
61pub fn config(max: u64) -> Outcome<OzoneConfig> {
62 let mut cfg = res!(setup::default_cfg());
63 cfg.num_zones = 1;
64 cfg.num_cbots_per_zone = 1;
65 cfg.num_fbots_per_zone = 1;
66 cfg.num_igbots_per_zone = 1;
67 cfg.num_rbots_per_zone = 1;
68 cfg.num_wbots_per_zone = 1;
69 cfg.data_file_max_bytes = max;
70 cfg.rest_chunk_threshold = max * 7 / 10;
71 cfg.zone_overrides = BTreeMap::new();
72 cfg.sync_on_write = true;
73 Ok(cfg)
74}
75
76pub fn schemes() -> RestSchemesInput<(), HashScheme, HashScheme, ChecksumScheme> {
77 RestSchemesInput::new(
78 None::<()>,
79 None::<HashScheme>,
80 None::<HashScheme>,
81 Some(ChecksumScheme::new_crc32()),
82 )
83}
84
85pub fn key(i: usize) -> Dat { dat!(fmt!("gc pair key {:03}", i)) }
86
87/// Every value is 100 bytes and carries its key's number and version, so a value read back names
88/// the record it came from.
89pub fn value(i: usize, ver: u8) -> Dat {
90 let mut v = vec![0u8; 100];
91 v[0] = i as u8;
92 v[1] = ver;
93 for j in 2..100 {
94 v[j] = (i as u8) ^ ver ^ (j as u8);
95 }
96 Dat::BU32(v)
97}
98
99pub fn size(p: &Path) -> u64 {
100 match fs::metadata(p) { Ok(m) => m.len(), Err(_) => 0 }
101}
102
103pub fn file(root: &Path, cfg: &OzoneConfig, typ: FileType, n: u32) -> PathBuf {
104 cfg.zone_root(root).join("zone_001").join(ZoneDir::relative_file_path(&typ, n))
105}
106
107pub fn zone_dir(root: &Path, cfg: &OzoneConfig) -> PathBuf {
108 cfg.zone_root(root).join("zone_001")
109}
110
111/// What the files of a store hold records of this test's size to. Index entries differ in length
112/// with the offset they name, so the length of an index is not its record count times one number.
113pub struct Probe {
114 pub len: u64, // data bytes in one record
115 pub ind: Vec<u64>, // index bytes for 0 to 5 records
116}
117
118/// Measures the records by writing five to a store of their own.
119pub fn probe(probe_dir: &str) -> Outcome<Probe> {
120 let _ = fs::remove_dir_all(probe_dir);
121 res!(fs::create_dir_all(probe_dir));
122 let root = res!(Path::new(probe_dir).canonicalize());
123 let cfg = res!(config(4_000));
124 let db: TestDb = res!(setup::start_db(root.clone(), Some(cfg.clone()), schemes(), None, false, true));
125 let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1));
126 let mut ind = vec![0u64];
127 for i in 0..5 {
128 res!(db.insert(key(i), value(i, 1), Uid::default(), None));
129 ind.push(size(&i1));
130 }
131 res!(db.close());
132 let len = size(&d1) / 5;
133 let _ = fs::remove_dir_all(probe_dir);
134 assert!(len > 100, "the probe record is {} bytes, shorter than its own value", len);
135 assert_eq!(size(&d1) % 5, 0, "the five probe records are not of one length");
136 Ok(Probe { len, ind })
137}
138
139/// Writes every key at version 1. File 1 ends up holding keys 0 to 4.
140pub fn fill(db: &TestDb) -> Outcome<()> {
141 for i in 0..NKEYS {
142 res!(db.insert(key(i), value(i, 1), Uid::default(), None));
143 }
144 Ok(())
145}
146
147/// Reads every key and counts the answers that are not the version last written.
148pub fn judge(db: &TestDb, want: &[u8], label: &str) -> usize {
149 let mut bad = 0;
150 for i in 0..NKEYS {
151 match db.get(&key(i), None) {
152 Ok(Some((v, _))) if v == value(i, want[i]) => (),
153 Ok(Some((v, _))) => {
154 bad += 1;
155 let head = match v.bytes_ref() {
156 Some(b) if b.len() >= 2 => fmt!("key {} version {}", b[0], b[1]),
157 _ => fmt!("{:?}", v),
158 };
159 msg!("{}: key {} read back {}, not version {}.", label, i, head, want[i]);
160 },
161 Ok(None) => {
162 bad += 1;
163 msg!("{}: key {} is missing.", label, i);
164 },
165 Err(e) => {
166 bad += 1;
167 let s: String = fmt!("{}", e).chars().take(300).collect();
168 msg!("{}: key {} could not be read: {}", label, i, s);
169 },
170 }
171 }
172 msg!("{}: {} of {} keys wrong.", label, bad, NKEYS);
173 bad
174}
175
176/// Polls until the condition holds, for at most the limit.
177pub fn wait_until<F: FnMut() -> bool>(limit: Duration, mut f: F) -> bool {
178 let start = Instant::now();
179 while start.elapsed() < limit {
180 if f() {
181 return true;
182 }
183 thread::sleep(Duration::from_millis(20));
184 }
185 f()
186}
187
188/// The temporary files of a collection that are in the directory.
189pub fn temporaries(dir: &Path) -> Vec<String> {
190 let mut found = Vec::new();
191 if let Ok(list) = fs::read_dir(dir) {
192 for entry in list.flatten() {
193 if ZoneDir::is_gc_temp_file(&entry.path()) {
194 found.push(entry.file_name().to_string_lossy().to_string());
195 }
196 }
197 }
198 found
199}
200
201/// The bytes in every regular file under the zone root, counted by walking the directories with
202/// nothing of the store's own, so that the store's answer has something outside it to be checked
203/// against.
204pub fn zone_files_len(root: &Path, cfg: &OzoneConfig) -> u64 {
205 dir_len(&cfg.zone_root(root))
206}
207
208/// The bytes in every regular file under a directory, subdirectories included.
209pub fn dir_len(dir: &Path) -> u64 {
210 let mut bytes = 0u64;
211 let mut stack = vec![dir.to_path_buf()];
212 while let Some(d) = stack.pop() {
213 if let Ok(list) = fs::read_dir(&d) {
214 for entry in list.flatten() {
215 match entry.metadata() {
216 Ok(m) if m.is_dir() => stack.push(entry.path()),
217 Ok(m) if m.is_file() => bytes += m.len(),
218 _ => (),
219 }
220 }
221 }
222 }
223 bytes
224}
225
226/// Waits until the zone files have stopped changing, and gives their length then.
227pub fn settled_len(root: &Path, cfg: &OzoneConfig) -> u64 {
228 let start = Instant::now();
229 let mut last = zone_files_len(root, cfg);
230 let mut quiet = 0;
231 while start.elapsed() < Duration::from_secs(30) {
232 thread::sleep(Duration::from_millis(150));
233 let now = zone_files_len(root, cfg);
234 if now == last {
235 quiet += 1;
236 if quiet >= 4 {
237 break;
238 }
239 } else {
240 quiet = 0;
241 last = now;
242 }
243 }
244 last
245}
246
247/// The directories a store keeps files in: its root, which holds the zone root and everything
248/// else the store writes, and the directory of each zone placed elsewhere by an override.
249pub fn store_dirs(root: &Path, cfg: &OzoneConfig) -> Vec<PathBuf> {
250 let mut dirs = vec![root.to_path_buf()];
251 for zmap in cfg.zone_overrides().values() {
252 let dir = match zmap {
253 Dat::Map(m) => match m.get(&dat!("dir")) {
254 Some(Dat::Str(s)) => s.clone(),
255 _ => String::new(),
256 },
257 _ => String::new(),
258 };
259 let base = if dir.is_empty() { root.to_path_buf() } else { root.join(dir) };
260 dirs.push(cfg.zone_root(&base));
261 }
262 dirs
263}
264
265/// The files of the store, of whatever extension, that hold the needle anywhere in their bytes,
266/// found by reading each whole file under the store root and every overridden zone directory with
267/// nothing of the store's own. Index files, configuration, temporaries and a zone placed outside
268/// the root are all searched, so that a copy of a deleted value cannot hide in any of them.
269pub fn files_holding(root: &Path, cfg: &OzoneConfig, needle: &[u8]) -> Vec<PathBuf> {
270 let mut found = BTreeSet::new();
271 let mut stack = store_dirs(root, cfg);
272 let mut seen = BTreeSet::new();
273 while let Some(d) = stack.pop() {
274 if !seen.insert(d.clone()) {
275 continue;
276 }
277 if let Ok(list) = fs::read_dir(&d) {
278 for entry in list.flatten() {
279 let p = entry.path();
280 match entry.metadata() {
281 Ok(m) if m.is_dir() => stack.push(p),
282 Ok(m) if m.is_file() => {
283 if let Ok(bytes) = fs::read(&p) {
284 if needle.len() <= bytes.len() && bytes.windows(needle.len()).any(|w| w == needle) {
285 found.insert(p);
286 }
287 }
288 },
289 _ => (),
290 }
291 }
292 }
293 }
294 found.into_iter().collect()
295}
296
297// The marks of every zone, in zone order.
298fn all_marks(db: &TestDb, c: &OzoneConfig) -> Outcome<Vec<(usize, u32, (u64, u64))>> {
299 let mut marks = Vec::new();
300 for z in 0..c.num_zones as usize {
301 for (f, m) in res!(db.api().chans().marks().files(z)) {
302 marks.push((z, f, m));
303 }
304 }
305 Ok(marks)
306}
307
308// Every file under `dir` with its inode, so that a file replaced by a rename is told from itself.
309fn inodes(dir: &Path, out: &mut BTreeMap<PathBuf, u64>) -> Outcome<()> {
310 use std::os::unix::fs::MetadataExt;
311 let list = match fs::read_dir(dir) {
312 Ok(list) => list,
313 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()), // removed since listed
314 Err(e) => return Err(err!(e, "Listing {:?} for a power cut.", dir; IO, Test)),
315 };
316 for entry in list {
317 let p = res!(entry).path();
318 match fs::symlink_metadata(&p) {
319 Ok(m) if m.is_dir() => res!(inodes(&p, out)),
320 Ok(m) => { out.insert(p, m.ino()); },
321 Err(e) if e.kind() == std::io::ErrorKind::NotFound => (), // removed since listed
322 Err(e) => return Err(err!(e, "Reading {:?} for a power cut.", p; IO, Test)),
323 }
324 }
325 Ok(())
326}
327
328/// A power cut at one instant of the live store `root`, imaged into the empty directory `dst`:
329/// each live file of the image is cut to the mark its last barrier published. The copy takes
330/// time while collection deletes, renames and rolls files over, so the marks and the directory
331/// (names and inodes) are read before and after it, and the copy is taken again until neither
332/// moved. A file that vanishes mid-copy is such a move. An image mixing a directory from after
333/// a collection with marks from before it is a state no real crash leaves (2026-10-11).
334/// `RACES_COPY_DELAY_MS` widens each copy, to show the image stays single-instant under a slow disk.
335pub fn instant_cut(db: &TestDb, root: &Path, c: &OzoneConfig, dst: &Path) -> Outcome<()> {
336 const TRIES: usize = 200;
337 let delay = std::env::var("RACES_COPY_DELAY_MS").ok().and_then(|s| s.parse::<u64>().ok());
338 for _ in 0..TRIES {
339 let marks = res!(all_marks(db, c));
340 let mut before = BTreeMap::new();
341 res!(inodes(root, &mut before));
342 let _ = fs::remove_dir_all(dst);
343 res!(fs::create_dir_all(dst));
344 let mut moved = false;
345 for f in before.keys() {
346 let rel = res!(f.strip_prefix(root).map_err(|e| err!(e, "{:?} is not under the root.", f; Test)));
347 let out = dst.join(rel);
348 if let Some(p) = out.parent() {
349 res!(fs::create_dir_all(p));
350 }
351 if let Some(ms) = delay {
352 thread::sleep(Duration::from_millis(ms));
353 }
354 if fs::copy(f, &out).is_err() {
355 moved = true; // gone or replaced mid-copy: take the image again
356 break;
357 }
358 }
359 if !moved {
360 let mut after = BTreeMap::new();
361 res!(inodes(root, &mut after));
362 moved = after != before || res!(all_marks(db, c)) != marks;
363 }
364 if moved {
365 thread::sleep(Duration::from_millis(5));
366 continue;
367 }
368 let _ = fs::remove_file(dst.join(constant::FAULT_FILENAME));
369 let _ = fs::remove_file(dst.join(constant::REFUSAL_FILENAME));
370 for (z, f, (d, i)) in marks {
371 let zd = c.zone_root(dst).join(fmt!("zone_{:03}", z + 1));
372 for (typ, upto) in [(FileType::Data, d), (FileType::Index, i)] {
373 let p = zd.join(ZoneDir::relative_file_path(&typ, f));
374 if let Ok(file) = fs::OpenOptions::new().write(true).open(&p) {
375 if res!(file.metadata()).len() > upto {
376 res!(file.set_len(upto));
377 }
378 }
379 }
380 }
381 return Ok(());
382 }
383 Err(err!("No instant image of {:?} was taken in {} tries: the store kept moving under \
384 every copy.", root, TRIES; Test, Timeout))
385}