Skip to content

Commit

Permalink
Sync file after allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Oct 25, 2024
1 parent 362c1f5 commit 00ce8b4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/subspace-farmer-components/src/file_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub trait FileExt {
/// Make sure file has specified number of bytes allocated for it
fn preallocate(&self, len: u64) -> Result<()>;

/// Attempts to sync all OS-internal file content and metadata to disk
fn sync(&self) -> Result<()>;

/// Advise OS/file system that file will use random access and read-ahead behavior is
/// undesirable, on Windows this can only be set when file is opened, see [`OpenOptionsExt`]
fn advise_random_access(&self) -> Result<()>;
Expand Down Expand Up @@ -111,6 +114,10 @@ impl FileExt for File {
fs2::FileExt::allocate(self, len)
}

fn sync(&self) -> Result<()> {
self.sync_all()
}

#[cfg(target_os = "linux")]
fn advise_random_access(&self) -> Result<()> {
use std::os::unix::io::AsRawFd;
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-farmer/src/disk_piece_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ impl DiskPieceCache {
.map_err(DiskPieceCacheError::CantPreallocateCacheFile)?;
// Truncating file (if necessary)
file.set_len(expected_size)?;
file.sync()?;
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/subspace-farmer/src/single_disk_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,7 @@ impl SingleDiskFarm {
metadata_file
.preallocate(expected_metadata_size)
.map_err(SingleDiskFarmError::CantPreallocateMetadataFile)?;
metadata_file.sync()?;
metadata_file.write_all_at(metadata_header.encode().as_slice(), 0)?;

metadata_header
Expand All @@ -1404,6 +1405,7 @@ impl SingleDiskFarm {
.map_err(SingleDiskFarmError::CantPreallocateMetadataFile)?;
// Truncating file (if necessary)
metadata_file.set_len(expected_metadata_size)?;
metadata_file.sync()?;
}

let mut metadata_header_bytes = vec![0; PlotMetadataHeader::encoded_size()];
Expand Down Expand Up @@ -1476,6 +1478,7 @@ impl SingleDiskFarm {
.map_err(SingleDiskFarmError::CantPreallocatePlotFile)?;
// Truncating file (if necessary)
plot_file.set_len(allocated_space_distribution.plot_file_size)?;
plot_file.sync()?;
}

let plot_file = Arc::new(plot_file);
Expand Down
4 changes: 4 additions & 0 deletions crates/subspace-farmer/src/single_disk_farm/direct_io_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl FileExt for DirectIoFile {
self.file.preallocate(len)
}

fn sync(&self) -> io::Result<()> {
self.file.sync_all()
}

fn advise_random_access(&self) -> io::Result<()> {
// Ignore, already set
Ok(())
Expand Down

0 comments on commit 00ce8b4

Please sign in to comment.