diff --git a/rust/Cargo.lock b/rust/Cargo.lock index fec5d14ac..7a57ba1b6 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -644,7 +644,7 @@ dependencies = [ [[package]] name = "par-rs" version = "0.2.0" -source = "git+https://github.com/Appz4Fun/par-rs?rev=2524277d7c74db8a256617e75c28bf0872a04462#2524277d7c74db8a256617e75c28bf0872a04462" +source = "git+https://github.com/Appz4Fun/par-rs?rev=985b37371ed229e0f27e1d4ec9e542fc7e989284#985b37371ed229e0f27e1d4ec9e542fc7e989284" dependencies = [ "crc32fast", "libc", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index ef5b414d8..e5d39a2a3 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -20,7 +20,7 @@ bench = false doctest = false [dependencies] -par-rs = { git = "https://github.com/Appz4Fun/par-rs", rev = "2524277d7c74db8a256617e75c28bf0872a04462" } +par-rs = { git = "https://github.com/Appz4Fun/par-rs", rev = "985b37371ed229e0f27e1d4ec9e542fc7e989284" } zipfast = { git = "https://github.com/Appz4Fun/zipfast", rev = "ed2ee346a5b18724c55aec6a90c73e86d5909614", features = ["persistent-partials"] } vidmatch = { git = "https://github.com/Appz4Fun/vidmatch", rev = "636facc1223d2a87e5ca7a43bc1d2e169c39deb6" } zipview = { git = "https://github.com/xbmc4lyfe/zipview", rev = "2c3d73a06b29987cea1510e903e437a482076745" } diff --git a/rust/src/parchecker.rs b/rust/src/parchecker.rs index 9d9195915..076b9ff5b 100644 --- a/rust/src/parchecker.rs +++ b/rust/src/parchecker.rs @@ -4489,7 +4489,11 @@ pub(crate) fn streaming_session_kick_backfill(nzb_id: i32) { // Bytes a flush or a completion held in RAM go first: never read back. worker.apply_held_backfill(); let reread = worker.session.io_stats().backfill_bytes_read; - let backfilled = worker.session.backfill_pending().is_ok(); + // Since par-rs 985b373 (#147) a slice not on disk yet leaves the back-fill + // pending and returns Ok: only an empty queue counts as done, so the retry + // delay stays in force while it waits for the cache to write those slices. + let backfilled = + worker.session.backfill_pending().is_ok() && !worker.session.needs_backfill(); let reread = worker.session.io_stats().backfill_bytes_read.saturating_sub(reread); if reread > 0 { let message = format!( @@ -7054,9 +7058,14 @@ mod tests { streaming_session_on_recovery_volume_arrived(nzb_id, &path, Some(9)) .unwrap() .unwrap(); - while entry.session.needs_backfill() { + // Bounded: since par-rs 985b373 a slice not on disk leaves it pending with Ok. + for _ in 0..8 { + if !entry.session.needs_backfill() { + break; + } entry.session.backfill_pending().unwrap(); } + assert!(!entry.session.needs_backfill(), "back-fill still pending"); let report = streaming_session_repair_available_targets_now(nzb_id, &[id0]) .unwrap() .unwrap(); @@ -7234,9 +7243,14 @@ mod tests { let _ = streaming_session_poll(nzb_id, true); let more = streaming_session_take_unpause_ids(nzb_id); assert!(more.is_empty(), "nothing more fetched: {more:?}"); - while entry.session.needs_backfill() { + // Bounded: since par-rs 985b373 a slice not on disk leaves it pending with Ok. + for _ in 0..8 { + if !entry.session.needs_backfill() { + break; + } entry.session.backfill_pending().unwrap(); } + assert!(!entry.session.needs_backfill(), "back-fill still pending"); let id0 = entry.session.file_id_by_name(b"part01.rar").unwrap(); let report = streaming_session_repair_available_targets_now(nzb_id, &[id0]) .unwrap() diff --git a/rust/src/parchecker/live_trust_tests.rs b/rust/src/parchecker/live_trust_tests.rs index c90b4dd48..f68caf064 100644 --- a/rust/src/parchecker/live_trust_tests.rs +++ b/rust/src/parchecker/live_trust_tests.rs @@ -35,6 +35,20 @@ impl Drop for Fixture { /// A one-file set (`part01.rar`, `slices` slices plus a short last one), its file on disk, /// and a started live session sized for `slots` recovery slots. fn fixture(nzb_id: i32, slices: usize, slots: usize, recovery: u32) -> Fixture { + fixture_with(nzb_id, slices, slots, slots, recovery, VolumeScheme::Single) +} + +/// [`fixture`] with `preallocated` of `slots` recovery slots allocated up front (as +/// `parlive::resources` sizes an unplanned session: a few preallocated, the cap and the +/// syndrome bytes for all of them) and the recovery blocks spread by `volumes`. +fn fixture_with( + nzb_id: i32, + slices: usize, + preallocated: usize, + slots: usize, + recovery: u32, + volumes: VolumeScheme, +) -> Fixture { let dir = std::env::temp_dir().join(format!( "rustyget-live-trust-{}-{nzb_id}", std::process::id() @@ -50,7 +64,7 @@ fn fixture(nzb_id: i32, slices: usize, slots: usize, recovery: u32) -> Fixture { slice_size: SLICE, recovery_blocks: recovery, first_exponent: 0, - volumes: VolumeScheme::Single, + volumes, creator: "rustyget-test".to_string(), unicode_names: false, kernel: Kernel::detect(), @@ -69,7 +83,7 @@ fn fixture(nzb_id: i32, slices: usize, slots: usize, recovery: u32) -> Fixture { &index, &dir, SessionOptions { - preallocated_syndrome_slots: slots, + preallocated_syndrome_slots: preallocated, max_syndrome_slots: slots, max_syndrome_bytes: (slots.max(1) * SLICE) as u64, max_working_bytes: 8 << 20, @@ -949,3 +963,138 @@ fn a_live_repair_reads_nothing_back() { ); } } + +/// The write-back case at RustyGet's capture: the article cache dropped an article's bytes +/// before its write landed (a write queued behind a flush), so the capture takes it as +/// written and par-rs reads the preallocated zeros. Since par-rs 2524277 (#145) the slice +/// waits instead of counting as damaged, because the zeros do not match the CRC of the +/// bytes the article will be written with; the cache's hand-over after the write +/// (`articles_written`) makes the next drain run `complete_staged_slices`, which verifies +/// it. No damage is ever reported and no recovery volume is asked for. +#[test] +fn an_article_taken_as_written_before_its_write_lands_is_not_damaged() { + let f = fixture(0x7a75_0030, 6, 2, 2); + let path = f.dir.join("part01.rar"); + std::fs::write(&path, vec![0u8; f.data.len()]).unwrap(); + let entry = active_streaming_session(f.nzb_id).unwrap(); + let half = SLICE / 2; + let identity = (None, &b"part01.rar"[..], &b""[..]); + let pieces: Vec<(u64, &[u8])> = f.data[..2 * SLICE] + .chunks(half) + .enumerate() + .map(|(i, c)| ((i * half) as u64, c)) + .collect(); + for &(offset, piece) in &pieces { + streaming_session_defer_committed_article( + f.nzb_id, + identity, + offset, + piece.len() as u64, + par_rs::crc::crc32(piece), + Some(&path), + || None, + ) + .unwrap() + .unwrap(); + } + streaming_session_drain_articles(f.nzb_id).unwrap().unwrap(); + let status = entry.session.status(); + assert_eq!(status.damaged, 0, "slices read before their write wait"); + assert_eq!(status.verified, 0); + assert!(entry.pending_unpause_ids.lock().unwrap().is_empty()); + // The writes land, then the cache hands the written articles over. + let mut disk = std::fs::read(&path).unwrap(); + disk[..2 * SLICE].copy_from_slice(&f.data[..2 * SLICE]); + std::fs::write(&path, &disk).unwrap(); + streaming_session_articles_written(f.nzb_id, identity, true, &pieces); + assert!( + entry.staged_due.load(Ordering::Acquire), + "the flush asks for a staged check" + ); + streaming_session_drain_articles(f.nzb_id).unwrap().unwrap(); + let status = entry.session.status(); + assert_eq!(status.damaged, 0, "never damaged"); + assert_eq!(status.verified, 2, "both slices verify once their bytes are on disk"); + assert!( + entry.pending_unpause_ids.lock().unwrap().is_empty(), + "no recovery asked for" + ); +} + +/// par-rs 985b373 (#147) at RustyGet's arrival and back-fill thread: a recovery volume +/// whose exponent no slot was allocated for, arriving after slices verified, costs one +/// back-fill pass (every verified slice read once), and that pass also allocates spare +/// slots, so the next volumes of the set land in them and read nothing back. +#[test] +fn an_unplanned_volume_mid_download_costs_one_back_fill_pass() { + // 25 slices (the last short), 2 slots preallocated (exponents 0 and 1) of a cap of 40 + // (as `parlive::resources` sizes an unplanned session), one recovery block per volume. + let f = fixture_with(0x7a75_0031, 24, 2, 40, 8, VolumeScheme::Uniform(1)); + let entry = active_streaming_session(f.nzb_id).unwrap(); + let id = file_id(f.nzb_id); + let lost = [1usize, 3, 5, 7, 9, 11]; + for (i, chunk) in f.data.chunks(SLICE).enumerate() { + let offset = (i * SLICE) as u64; + if lost.contains(&i) { + streaming_session_on_article_failed_permanently( + f.nzb_id, + &id, + offset, + chunk.len() as u64, + ) + .unwrap() + .unwrap(); + } else { + streaming_session_on_article_finished_by_name( + f.nzb_id, + b"part01.rar", + offset, + chunk.len() as u64, + par_rs::crc::crc32(chunk), + Some(chunk), + ) + .unwrap() + .unwrap(); + } + } + let pass = (f.data.len() - lost.len() * SLICE) as u64; + let backfill_read = || entry.session.io_stats().backfill_bytes_read; + let wait_backfill = || { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + while entry.session.needs_backfill() || entry.backfill_running.load(Ordering::Acquire) { + assert!(std::time::Instant::now() < deadline, "back-fill never finished"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + }; + let arrive = |exponent: usize| { + let (name, volume) = &f.volumes[exponent]; + let path = f.dir.join(name); + std::fs::write(&path, volume).unwrap(); + streaming_session_on_recovery_volume_arrived( + f.nzb_id, + &path, + Some(500 + exponent as u64), + ) + .unwrap() + .unwrap(); + }; + // Volumes for the preallocated slots: nothing to back-fill. + arrive(0); + arrive(1); + assert!(!entry.session.needs_backfill()); + assert_eq!(backfill_read(), 0); + // An unplanned exponent: one pass, which also fills the spare slots. + arrive(2); + wait_backfill(); + assert_eq!(backfill_read(), pass, "one pass over the verified slices"); + let slots = entry.session.status().syndrome_slots; + assert!(slots >= 32, "the pass allocated spares: {slots} slots"); + // The next volumes land in spares: no further pass. + for exponent in 3..6 { + arrive(exponent); + wait_backfill(); + } + assert_eq!(backfill_read(), pass, "later volumes landed in spares"); + // Six recovery blocks for six damaged slices: decode-ahead may already have rebuilt them. + assert_eq!(entry.session.status().ready_recovery, 6); +}