Skip to content
Merged
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
49 changes: 42 additions & 7 deletions crates/analysis/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct Control {
cancel_at_start: bool,
/// How often the work looked at the flag ([`Control::looked`]).
looked: AtomicU64,
/// How many [`uncounted`] parts of the work are running: their steps don't count.
quiet: AtomicU64,
/// Cancelled at its `n`th look ([`Control::cancelled_at_look`]); 0 for never.
#[cfg(any(test, feature = "testing"))]
cancel_at_look: u64,
Expand Down Expand Up @@ -112,12 +114,45 @@ impl rustgis_data::progress::Probe for Control {
self.looked_at()
}
fn begin(&self, total: u64) {
self.done.store(0, Ordering::Relaxed);
self.total.store(total, Ordering::Relaxed);
if self.counting() {
self.done.store(0, Ordering::Relaxed);
self.total.store(total, Ordering::Relaxed);
}
}
fn advance(&self, n: u64) {
self.done.fetch_add(n, Ordering::Relaxed);
if self.counting() {
self.done.fetch_add(n, Ordering::Relaxed);
}
}
}

impl Control {
/// Whether the work's steps count: none of it runs [`uncounted`].
fn counting(&self) -> bool {
self.quiet.load(Ordering::Relaxed) == 0
}
}

/// Runs `work` without counting its steps in the progress of the run on this thread, whose looks
/// at Cancel still stop it: for a piece of a step its caller counts itself (a block of a raster
/// made a block at a time), whose own steps would start the count again at each piece. Its
/// threads ([`crate::parallel`]) share the run's control, so they don't count either.
pub fn uncounted<T>(work: impl FnOnce() -> T) -> T {
/// Counts the part as running while it lives, also as it unwinds.
struct Quiet(Option<Arc<Control>>);
impl Drop for Quiet {
fn drop(&mut self) {
if let Some(c) = &self.0 {
c.quiet.fetch_sub(1, Ordering::Relaxed);
}
}
}
let control = current();
if let Some(c) = &control {
c.quiet.fetch_add(1, Ordering::Relaxed);
}
let _quiet = Quiet(control);
work()
}

/// Carries the initiating request predicate to the data writer's last commit check as well as
Expand Down Expand Up @@ -238,7 +273,7 @@ pub(crate) fn check() -> Result<(), Cancelled> {
/// Starts counting a step of `total` items (rows, features) for the progress, from none done.
pub(crate) fn begin(total: usize) {
CURRENT.with(|c| {
if let Some(c) = c.borrow().as_ref() {
if let Some(c) = c.borrow().as_ref().filter(|c| c.counting()) {
c.done.store(0, Ordering::Relaxed);
c.total.store(total as u64, Ordering::Relaxed);
}
Expand All @@ -249,7 +284,7 @@ pub(crate) fn begin(total: usize) {
/// (its index).
pub(crate) fn reached(done: usize) {
CURRENT.with(|c| {
if let Some(c) = c.borrow().as_ref() {
if let Some(c) = c.borrow().as_ref().filter(|c| c.counting()) {
c.done.store(done as u64, Ordering::Relaxed);
}
});
Expand All @@ -259,7 +294,7 @@ pub(crate) fn reached(done: usize) {
/// otherwise ends one short).
pub(crate) fn finish() {
CURRENT.with(|c| {
if let Some(c) = c.borrow().as_ref() {
if let Some(c) = c.borrow().as_ref().filter(|c| c.counting()) {
c.done.store(c.total.load(Ordering::Relaxed), Ordering::Relaxed);
}
});
Expand All @@ -268,7 +303,7 @@ pub(crate) fn finish() {
/// Counts `n` more items of the step done.
pub(crate) fn advance(n: usize) {
CURRENT.with(|c| {
if let Some(c) = c.borrow().as_ref() {
if let Some(c) = c.borrow().as_ref().filter(|c| c.counting()) {
c.done.fetch_add(n as u64, Ordering::Relaxed);
}
});
Expand Down
75 changes: 73 additions & 2 deletions crates/data/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,49 @@ pub fn stage_streamed<T, E: From<std::io::Error>>(
stage_at(link_target(path)?, 1 << 20, write)
}

/// [`stage_streamed`] to a destination nothing holds yet: it is reserved first, an empty file
/// made in its place only if nothing is there (an `AlreadyExists` error otherwise, before
/// anything is written), so two writes never pick the same file. [`StagedWrite::commit`] puts
/// the file there; dropping the result, or an error, removes the reservation, and `folder` (one
/// the caller just made for it) when nothing else is in it.
pub fn stage_streamed_new<T, E: From<std::io::Error>>(
path: &Path,
folder: Option<&Path>,
write: impl FnOnce(&mut std::io::BufWriter<std::fs::File>) -> std::result::Result<T, E>,
) -> std::result::Result<(StagedWrite, T), E> {
std::fs::OpenOptions::new().write(true).create_new(true).open(path)?;
let mut held = Reserved { path: Some(path.to_path_buf()), folder: folder.map(Path::to_path_buf) };
let (mut staged, value) = stage_at(path.to_path_buf(), 1 << 20, write)?;
(staged.reserved, staged.folder) = (held.path.take(), held.folder.take());
Ok((staged, value))
}

/// A destination reserved for a write ([`stage_streamed_new`]), and a folder made for it, removed
/// while the write isn't installed: the reservation while it is still empty, the folder while
/// nothing else is in it.
struct Reserved {
path: Option<std::path::PathBuf>,
folder: Option<std::path::PathBuf>,
}

impl Drop for Reserved {
fn drop(&mut self) {
unreserve(self.path.take(), self.folder.take());
}
}

/// Removes a reserved destination still empty, then `folder` if nothing else is in it.
fn unreserve(path: Option<std::path::PathBuf>, folder: Option<std::path::PathBuf>) {
if let Some(p) = path
&& std::fs::symlink_metadata(&p).is_ok_and(|m| m.is_file() && m.len() == 0)
{
let _ = std::fs::remove_file(p);
}
if let Some(f) = folder {
let _ = std::fs::remove_dir(f);
}
}

/// Stages and installs a write, retaining the old file on error or cancellation.
fn atomic_write_at<T, E: From<std::io::Error>>(
path: std::path::PathBuf,
Expand All @@ -1353,7 +1396,7 @@ fn stage_at<T, E: From<std::io::Error>>(
let tmp = temp_beside(&path)?;
// Opened before the guard, so a name already taken is never removed.
let file = std::fs::OpenOptions::new().write(true).create_new(true).open(&tmp)?;
let staged = StagedWrite { path, tmp: Some(tmp) };
let staged = StagedWrite { path, tmp: Some(tmp), reserved: None, folder: None };
// The new file takes the old one's mode before any content is written, so a private file
// (0600) never becomes readable by others. On Windows a new file inherits its folder's access
// rules, and copying a read-only flag would only make the rename fail.
Expand Down Expand Up @@ -1381,10 +1424,14 @@ fn stage_at<T, E: From<std::io::Error>>(
Ok((staged, value))
}

/// An atomic write awaiting installation. An error or drop removes its temporary file.
/// An atomic write awaiting installation. An error or drop removes its temporary file, and what
/// [`stage_streamed_new`] reserved for it.
pub struct StagedWrite {
path: std::path::PathBuf,
tmp: Option<std::path::PathBuf>,
/// The destination reserved, and the folder made for it ([`stage_streamed_new`]).
reserved: Option<std::path::PathBuf>,
folder: Option<std::path::PathBuf>,
}

impl StagedWrite {
Expand All @@ -1402,6 +1449,7 @@ impl StagedWrite {
if let Some(tmp) = &self.tmp {
std::fs::rename(tmp, &self.path)?;
self.tmp = None;
(self.reserved, self.folder) = (None, None);
sync_folder(&self.path);
}
Ok(())
Expand All @@ -1413,6 +1461,7 @@ impl Drop for StagedWrite {
if let Some(tmp) = &self.tmp {
let _ = std::fs::remove_file(tmp);
}
unreserve(self.reserved.take(), self.folder.take());
}
}

Expand Down Expand Up @@ -5804,6 +5853,28 @@ mod tests {
use super::*;
use std::io::Write;

/// A write to a name nothing holds reserves it first: a second write to it is refused before
/// anything is written, and dropping the write removes the reservation and the folder made for
/// it; once installed, both stay (#2865).
#[test]
fn a_new_staged_file_reserves_its_name_and_leaves_nothing_when_dropped() {
use std::io::Write;
let dir = tempfile::tempdir().unwrap();
let folder = dir.path().join("Survey.rgis.data");
std::fs::create_dir(&folder).unwrap();
let dest = folder.join("index.tif");
let (staged, ()) = stage_streamed_new::<_, std::io::Error>(&dest, Some(&folder), |w| w.write_all(b"pixels")).unwrap();
assert!(dest.is_file(), "reserved");
let taken = stage_streamed_new::<(), std::io::Error>(&dest, None, |_| panic!("written to a name another holds"));
assert_eq!(taken.err().map(|e| e.kind()), Some(std::io::ErrorKind::AlreadyExists));
drop(staged);
assert!(!dest.exists() && !folder.exists(), "the reservation and its folder go with it");
std::fs::create_dir(&folder).unwrap();
let (staged, ()) = stage_streamed_new::<_, std::io::Error>(&dest, Some(&folder), |w| w.write_all(b"pixels")).unwrap();
staged.commit().unwrap();
assert_eq!(std::fs::read(&dest).unwrap(), b"pixels");
}

/// Each extra cell is counted, including empty ones, and each affected row once, preserving named data.
#[test]
fn csv_extra_cells_are_counted() {
Expand Down
Loading
Loading