Skip to content

Commit

Permalink
storage: remove Status::garbage_disk_size
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Feb 4, 2025
1 parent 10ee125 commit 61783ba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
16 changes: 8 additions & 8 deletions src/bin/toysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,18 @@ Transactions: {active_txns} active, {versions} total
nodes = status.raft.match_index.len(),
committed = status.raft.commit_index,
applied = status.raft.applied_index,
raft_size = format_args!("{:.3}", status.raft.storage.size as f64 / 1000000.0),
raft_garbage = format_args!("{:.0}", status.raft.storage.garbage_percent()),
raft_size =
format_args!("{:.3}", status.raft.storage.size as f64 / 1_000_000.0),
raft_garbage =
format_args!("{:.0}", status.raft.storage.garbage_disk_percent()),
raft_storage = status.raft.storage.name,
raft_match =
status.raft.match_index.iter().map(|(n, m)| format!("n{n}:{m}")).join(" "),
sql_keys = status.mvcc.storage.keys,
sql_size = format_args!("{:.3}", status.mvcc.storage.size as f64 / 1000000.0),
sql_disk_size = format_args!(
"{:.3}",
status.mvcc.storage.total_disk_size as f64 / 1000000.0
),
sql_garbage = format_args!("{:.0}", status.mvcc.storage.garbage_percent()),
sql_size = format_args!("{:.3}", status.mvcc.storage.size as f64 / 1_000_000.0),
sql_disk_size =
format_args!("{:.3}", status.mvcc.storage.disk_size as f64 / 1_000_000.0),
sql_garbage = format_args!("{:.0}", status.mvcc.storage.garbage_disk_percent()),
sql_storage = status.mvcc.storage.name,
active_txns = status.mvcc.active_txns,
versions = status.mvcc.versions,
Expand Down
27 changes: 10 additions & 17 deletions src/storage/bitcask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ impl BitCask {

let status = s.status()?;
if Self::should_compact(
status.garbage_disk_size,
status.total_disk_size,
status.garbage_disk_size(),
status.disk_size,
garbage_min_fraction,
garbage_min_bytes,
) {
info!(
"Compacting {} to remove {:.0}% garbage ({} MB out of {} MB)",
s.log.path.display(),
status.garbage_percent(),
status.garbage_disk_size / 1024 / 1024,
status.total_disk_size / 1024 / 1024
status.garbage_disk_percent(),
status.garbage_disk_size() / 1024 / 1024,
status.disk_size / 1024 / 1024
);
s.compact()?;
info!(
"Compacted {} to size {} MB",
s.log.path.display(),
(status.total_disk_size - status.garbage_disk_size) / 1024 / 1024
(status.disk_size - status.garbage_disk_size()) / 1024 / 1024
);
}

Expand Down Expand Up @@ -161,18 +161,11 @@ impl Engine for BitCask {
let size = self
.keydir
.iter()
.fold(0, |size, (key, (_, value_len))| size + key.len() as u64 + *value_len as u64);
let total_disk_size = self.log.file.metadata()?.len();
.map(|(key, (_, value_len))| key.len() as u64 + *value_len as u64)
.sum();
let disk_size = self.log.file.metadata()?.len();
let live_disk_size = size + 8 * keys; // account for length prefixes
let garbage_disk_size = total_disk_size - live_disk_size;
Ok(Status {
name: "bitcask".to_string(),
keys,
size,
total_disk_size,
live_disk_size,
garbage_disk_size,
})
Ok(Status { name: "bitcask".to_string(), keys, size, disk_size, live_disk_size })
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/storage/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,23 @@ pub struct Status {
/// The logical size of live key/value pairs.
pub size: u64,
/// The on-disk size of all data, live and garbage.
pub total_disk_size: u64,
pub disk_size: u64,
/// The on-disk size of live data.
pub live_disk_size: u64,
/// The on-disk size of garbage data.
pub garbage_disk_size: u64,
}

impl Status {
pub fn garbage_percent(&self) -> f64 {
if self.total_disk_size == 0 {
/// The on-disk size of garbage data.
pub fn garbage_disk_size(&self) -> u64 {
self.disk_size - self.live_disk_size
}

/// The ratio of on-disk garbage to total size.
pub fn garbage_disk_percent(&self) -> f64 {
if self.disk_size == 0 {
return 0.0;
}
self.garbage_disk_size as f64 / self.total_disk_size as f64 * 100.0
self.garbage_disk_size() as f64 / self.disk_size as f64 * 100.0
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/storage/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ impl Engine for Memory {
name: "memory".to_string(),
keys: self.data.len() as u64,
size: self.data.iter().fold(0, |size, (k, v)| size + k.len() as u64 + v.len() as u64),
total_disk_size: 0,
disk_size: 0,
live_disk_size: 0,
garbage_disk_size: 0,
})
}
}
Expand Down

0 comments on commit 61783ba

Please sign in to comment.