Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions rust/src/articledl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5505,11 +5505,14 @@ mod tests {
let context = crate::parlive::source_write_context(-1_961_502, 40, &dir, b"part40.rar").unwrap();
let limit = crate::parchecker::effective_live_pool_bytes();
assert!(limit > 0);
// The pool is process-global; hold whatever other tests left free.
// Fill a pool of this test's own (the shared one is process-global, and holding all of
// it starved every other live test running in parallel).
let pool = std::sync::Arc::new(crate::parlive::BudgetPool::with_size(limit));
crate::parchecker::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = Some(pool.clone()));
let mut reservation = Vec::new();
let mut chunk = limit;
while chunk != 0 {
while let Some(lease) = crate::parlive::LIVE_BUDGET.acquire(chunk, limit) {
while let Some(lease) = pool.acquire(chunk, limit) {
reservation.push(lease);
}
chunk /= 2;
Expand All @@ -5519,6 +5522,7 @@ mod tests {
assert!(file.flush_and_close());
assert_eq!(std::fs::read(path).unwrap(), b"ordinary");
drop(reservation);
crate::parchecker::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = None);
crate::parlive::forget_download_writes(-1_961_502);
std::fs::remove_dir_all(dir).unwrap();
}
Expand Down
13 changes: 11 additions & 2 deletions rust/src/parchecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use crate::parlive::{
download_writes, reserve_control, resources, ControlLease, SessionBudgetLease,
SessionGeneration, WriteCoordinator, LIVE_BUDGET,
SessionGeneration, WriteCoordinator,
};
use std::collections::{HashMap, HashSet, VecDeque};
use std::io::{Read, Seek, SeekFrom};
Expand Down Expand Up @@ -401,8 +401,17 @@ fn lease_pool_bytes(lease: &SessionBudgetLease) -> u64 {
lease.pool().size().unwrap_or_else(effective_live_pool_bytes)
}

#[cfg(test)]
thread_local! {
/// A test's own budget pool for sessions started on its thread (see `live_lifecycle_tests`):
/// the daemon's shared pool is one process-wide 500 MB that parallel tests would exhaust.
pub(crate) static TEST_BUDGET_POOL: std::cell::RefCell<Option<Arc<crate::parlive::BudgetPool>>> =
const { std::cell::RefCell::new(None) };
}

fn acquire_session_budget(bytes: u64, limit: u64) -> Result<SessionBudgetLease, RepairError> {
LIVE_BUDGET
let pool = crate::parlive::live_pool();
pool
.acquire(bytes, limit)
.ok_or(RepairError::StreamingMemoryLimit {
context: "shared live PAR reservation",
Expand Down
5 changes: 5 additions & 0 deletions rust/src/parchecker/live_lifecycle_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ impl Fixture {
NEXT_FIXTURE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
));
std::fs::create_dir(&dir).unwrap();
// Sessions this test starts draw on a pool of its own, not the shared 500 MB one.
super::TEST_BUDGET_POOL.with(|p| {
*p.borrow_mut() = Some(Arc::new(BudgetPool::with_size(500 << 20)));
});
for f in std::fs::read_dir(
Path::new(env!("CARGO_MANIFEST_DIR")).join("../tests/testdata/parchecker"),
)
Expand All @@ -33,6 +37,7 @@ impl Fixture {
}
impl Drop for Fixture {
fn drop(&mut self) {
super::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = None);
if !std::thread::panicking() {
let _ = std::fs::remove_dir_all(&self.0);
}
Expand Down
15 changes: 13 additions & 2 deletions rust/src/parlive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ impl SessionBudgetLease {
pub(crate) static LIVE_BUDGET: LazyLock<Arc<BudgetPool>> =
LazyLock::new(|| Arc::new(BudgetPool::default()));

/// The pool live reservations draw on: the daemon's shared one, or in a test the pool the
/// test installed for its thread (`parchecker::TEST_BUDGET_POOL`), so tests that fill or
/// share it cannot starve each other.
pub(crate) fn live_pool() -> Arc<BudgetPool> {
#[cfg(test)]
if let Some(pool) = crate::parchecker::TEST_BUDGET_POOL.with(|p| p.borrow().clone()) {
return pool;
}
Arc::clone(&LIVE_BUDGET)
}

#[derive(Default)]
struct ControlAccounting {
bytes: u64,
Expand Down Expand Up @@ -294,7 +305,7 @@ fn reserve_control_inner(
// Ordinary-write root control is accounted (so live reservations yield to
// it) but never refused by the live caps.
if pool_optional {
let budget = LIVE_BUDGET.acquire(bytes, pool_limit);
let budget = live_pool().acquire(bytes, pool_limit);
used.bytes = used.bytes.saturating_add(bytes);
*used.jobs.entry(nzb_id).or_insert(0) += bytes;
return Ok(ControlLease {
Expand All @@ -319,7 +330,7 @@ fn reserve_control_inner(
MIB,
));
}
let budget = LIVE_BUDGET.acquire(bytes, pool_limit);
let budget = live_pool().acquire(bytes, pool_limit);
if budget.is_none() {
return Err(limit_error("shared live PAR reservation", bytes, pool_limit));
}
Expand Down
6 changes: 5 additions & 1 deletion rust/src/parlive/physical_ownership_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,13 @@ fn exhausted_live_budget_does_not_bypass_a_foreign_certified_source() {
let gate = f.seal(&path);
let limit = crate::parchecker::effective_live_pool_bytes();
assert!(limit > 0);
// Drain a pool of this test's own: holding all of the shared one starves parallel tests.
let pool = Arc::new(BudgetPool::with_size(limit));
crate::parchecker::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = Some(Arc::clone(&pool)));
let mut held = Vec::new();
let mut chunk = 1024 * 1024u64;
while chunk != 0 {
while let Some(lease) = LIVE_BUDGET.acquire(chunk, limit) { held.push(lease); }
while let Some(lease) = pool.acquire(chunk, limit) { held.push(lease); }
chunk /= 2;
}
let other_job = NEXT_JOB.fetch_sub(1, Ordering::Relaxed);
Expand All @@ -380,6 +383,7 @@ fn exhausted_live_budget_does_not_bypass_a_foreign_certified_source() {
assert_eq!(std::fs::read(path).unwrap(), b"certified");
assert!(gate.is_current_generation(SessionGeneration(1)));
drop(held);
crate::parchecker::TEST_BUDGET_POOL.with(|p| *p.borrow_mut() = None);
forget_download_writes(other_job);
f.finish();
}
Expand Down
Loading