fix: reserve disk for admitted exports so concurrent downloads cannot share the same free space
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,7 +10,10 @@ use axum::body::Bytes;
|
|||||||
use futures_core::Stream;
|
use futures_core::Stream;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::{
|
||||||
|
atomic::{AtomicU64, Ordering},
|
||||||
|
Arc,
|
||||||
|
};
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
|
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@@ -170,6 +173,26 @@ pub struct ExportGate {
|
|||||||
permits: Arc<Semaphore>,
|
permits: Arc<Semaphore>,
|
||||||
/// Free space that must remain after the export has been written.
|
/// Free space that must remain after the export has been written.
|
||||||
min_free: u64,
|
min_free: u64,
|
||||||
|
/// Bytes promised to exports that are admitted and not finished yet. Free space alone cannot say
|
||||||
|
/// that, because an export that has only just started has barely written anything.
|
||||||
|
reserved: Arc<AtomicU64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The most one export may reserve: 64 of them (the concurrency ceiling) still fit in a `u64`.
|
||||||
|
const MAX_RESERVATION: u64 = 1 << 57;
|
||||||
|
|
||||||
|
/// An admitted export: its place among the running ones and the disk it was promised. Both are given
|
||||||
|
/// back when this is dropped.
|
||||||
|
pub struct Admission {
|
||||||
|
_permit: OwnedSemaphorePermit,
|
||||||
|
reserved: Arc<AtomicU64>,
|
||||||
|
size: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Admission {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.reserved.fetch_sub(self.size, Ordering::SeqCst);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Refused {
|
pub enum Refused {
|
||||||
@@ -184,6 +207,7 @@ impl ExportGate {
|
|||||||
Self {
|
Self {
|
||||||
permits: Arc::new(Semaphore::new(concurrency)),
|
permits: Arc::new(Semaphore::new(concurrency)),
|
||||||
min_free,
|
min_free,
|
||||||
|
reserved: Arc::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,17 +224,31 @@ impl ExportGate {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Admit one export of about `size` bytes into `dir`. The permit lives as long as the response body.
|
/// Admit one export of about `size` bytes into `dir`. The slot and the bytes are held for as long as the
|
||||||
pub fn admit(&self, dir: &std::path::Path, size: u64) -> Result<OwnedSemaphorePermit, Refused> {
|
/// response body, so exports admitted together cannot each count on the same free space.
|
||||||
|
pub fn admit(&self, dir: &std::path::Path, size: u64) -> Result<Admission, Refused> {
|
||||||
let permit = self
|
let permit = self
|
||||||
.permits
|
.permits
|
||||||
.clone()
|
.clone()
|
||||||
.try_acquire_owned()
|
.try_acquire_owned()
|
||||||
.map_err(|_| Refused::Busy)?;
|
.map_err(|_| Refused::Busy)?;
|
||||||
|
// No disk is that large, and the cap keeps the running total from overflowing.
|
||||||
|
let size = size.min(MAX_RESERVATION);
|
||||||
|
// Reserve first, then check: concurrent callers can only over-refuse, never over-admit.
|
||||||
|
let earlier = self.reserved.fetch_add(size, Ordering::SeqCst);
|
||||||
|
let admission = Admission {
|
||||||
|
_permit: permit,
|
||||||
|
reserved: self.reserved.clone(),
|
||||||
|
size,
|
||||||
|
};
|
||||||
match free_bytes(dir) {
|
match free_bytes(dir) {
|
||||||
// `size` is the snapshot's recorded size; when a producer did not record one it is 0 and the margin alone applies.
|
// `size` is the snapshot's recorded size; when a producer did not record one it is 0 and the margin alone applies.
|
||||||
Some(free) if free < size.saturating_add(self.min_free) => Err(Refused::NoSpace),
|
// Bytes already written by running exports are no longer free and are counted again in `earlier`; being
|
||||||
_ => Ok(permit),
|
// that cautious is deliberate.
|
||||||
|
Some(free) if free < earlier.saturating_add(size).saturating_add(self.min_free) => {
|
||||||
|
Err(Refused::NoSpace)
|
||||||
|
}
|
||||||
|
_ => Ok(admission),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,7 +286,7 @@ pub fn sweep_stale(data_dir: &std::path::Path) -> usize {
|
|||||||
/// the client went away, or the response was discarded) releases the permit and deletes the export.
|
/// the client went away, or the response was discarded) releases the permit and deletes the export.
|
||||||
pub struct Guarded<S> {
|
pub struct Guarded<S> {
|
||||||
inner: Pin<Box<S>>,
|
inner: Pin<Box<S>>,
|
||||||
_permit: OwnedSemaphorePermit,
|
_permit: Admission,
|
||||||
_dir: tempfile::TempDir,
|
_dir: tempfile::TempDir,
|
||||||
db: Db,
|
db: Db,
|
||||||
principal: Principal,
|
principal: Principal,
|
||||||
@@ -261,7 +299,7 @@ pub struct Guarded<S> {
|
|||||||
impl<S> Guarded<S> {
|
impl<S> Guarded<S> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
inner: S,
|
inner: S,
|
||||||
permit: OwnedSemaphorePermit,
|
permit: Admission,
|
||||||
dir: tempfile::TempDir,
|
dir: tempfile::TempDir,
|
||||||
db: Db,
|
db: Db,
|
||||||
principal: Principal,
|
principal: Principal,
|
||||||
@@ -596,4 +634,45 @@ mod tests {
|
|||||||
"a snapshot bigger than the disk"
|
"a snapshot bigger than the disk"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn concurrent_exports_cannot_each_count_on_the_same_free_space() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let free = free_bytes(dir.path()).expect("statvfs works on the temp dir");
|
||||||
|
// each export needs a bit over a third of what is free, so two fit on paper but not together
|
||||||
|
// (the disk may move under us a little, which is why the margin is generous)
|
||||||
|
let size = free / 3 + free / 20;
|
||||||
|
let gate = ExportGate::new(8, 0);
|
||||||
|
let first = gate.admit(dir.path(), size).ok().expect("one fits");
|
||||||
|
let second = gate.admit(dir.path(), size).ok().expect("two fit");
|
||||||
|
assert!(
|
||||||
|
matches!(gate.admit(dir.path(), size), Err(Refused::NoSpace)),
|
||||||
|
"the third would need more than the disk has once the first two are counted"
|
||||||
|
);
|
||||||
|
drop(first);
|
||||||
|
assert!(
|
||||||
|
gate.admit(dir.path(), size).is_ok(),
|
||||||
|
"a finished export gives its bytes back"
|
||||||
|
);
|
||||||
|
drop(second);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_refused_export_leaves_nothing_reserved() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let gate = ExportGate::new(2, 0);
|
||||||
|
for _ in 0..3 {
|
||||||
|
assert!(matches!(
|
||||||
|
gate.admit(dir.path(), u64::MAX),
|
||||||
|
Err(Refused::NoSpace)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
assert_eq!(gate.reserved.load(Ordering::SeqCst), 0);
|
||||||
|
let busy = ExportGate::new(1, 0);
|
||||||
|
let held = busy.admit(dir.path(), 5).ok().unwrap();
|
||||||
|
assert!(matches!(busy.admit(dir.path(), 7), Err(Refused::Busy)));
|
||||||
|
assert_eq!(busy.reserved.load(Ordering::SeqCst), 5);
|
||||||
|
drop(held);
|
||||||
|
assert_eq!(busy.reserved.load(Ordering::SeqCst), 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user