Skip to content

Commit

Permalink
switch to statvfs
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chi Z <chi@neon.tech>
  • Loading branch information
skyzh committed Oct 15, 2024
1 parent 5c2284c commit ccda7d7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ enumset = "1.0.12"
fail = "0.5.0"
fallible-iterator = "0.2"
framed-websockets = { version = "0.1.0", git = "https://github.com/neondatabase/framed-websockets" }
fs2 = "0.4"
futures = "0.3"
futures-core = "0.3"
futures-util = "0.3"
Expand Down
1 change: 0 additions & 1 deletion pageserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ consumption_metrics.workspace = true
crc32c.workspace = true
either.workspace = true
fail.workspace = true
fs2.workspace = true
futures.workspace = true
hex.workspace = true
humantime.workspace = true
Expand Down
11 changes: 1 addition & 10 deletions pageserver/src/disk_usage_eviction_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,16 +1218,7 @@ mod filesystem_level_usage {
let stat = Statvfs::get(tenants_dir, mock_config)
.context("statvfs failed, presumably directory got unlinked")?;

// https://unix.stackexchange.com/a/703650
let blocksize = if stat.fragment_size() > 0 {
stat.fragment_size()
} else {
stat.block_size()
};

// use blocks_available (b_avail) since, pageserver runs as unprivileged user
let avail_bytes = stat.blocks_available() * blocksize;
let total_bytes = stat.blocks() * blocksize;
let (avail_bytes, total_bytes) = stat.get_avail_total_bytes();

Ok(Usage {
config,
Expand Down
16 changes: 16 additions & 0 deletions pageserver/src/statvfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ impl Statvfs {
Statvfs::Mock(stat) => stat.block_size,
}
}

/// Get the available and total bytes on the filesystem.
pub fn get_avail_total_bytes(&self) -> (u64, u64) {
// https://unix.stackexchange.com/a/703650
let blocksize = if self.fragment_size() > 0 {
self.fragment_size()
} else {
self.block_size()
};

// use blocks_available (b_avail) since, pageserver runs as unprivileged user
let avail_bytes = self.blocks_available() * blocksize;
let total_bytes = self.blocks() * blocksize;

(avail_bytes, total_bytes)
}
}

pub mod mock {
Expand Down
11 changes: 9 additions & 2 deletions pageserver/src/tenant/timeline/compaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use utils::id::TimelineId;

use crate::context::{AccessStatsBehavior, RequestContext, RequestContextBuilder};
use crate::page_cache;
use crate::statvfs::Statvfs;
use crate::tenant::checks::check_valid_layermap;
use crate::tenant::remote_timeline_client::WaitCompletionError;
use crate::tenant::storage_layer::filter_iterator::FilterIterator;
Expand Down Expand Up @@ -1693,8 +1694,14 @@ impl Timeline {

/// Check how much space is left on the disk
async fn check_available_space(self: &Arc<Self>) -> anyhow::Result<u64> {
let base_path = self.conf.tenants_path();
fs2::free_space(base_path).context("fs2::free_space")
let tenants_dir = self.conf.tenants_path();

let stat = Statvfs::get(&tenants_dir, None)
.context("statvfs failed, presumably directory got unlinked")?;

let (avail_bytes, _) = stat.get_avail_total_bytes();

Ok(avail_bytes)
}

/// Check if the compaction can proceed safely without running out of space. We assume the size
Expand Down

0 comments on commit ccda7d7

Please sign in to comment.