Skip to content

Commit e5baed5

Browse files
committed
refactor(hm-vm): ImageRegistry capacity is a bare u64 where 0 silently makes the cache evict every entry
1 parent 904721f commit e5baed5

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

crates/hm-exec/src/local/backend.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! drives the [`hm_vm`] subsystem. The VM backend (Docker, etc.) is injected;
55
//! snapshot caching is owned by `hm-vm`'s [`hm_vm::ImageRegistry`].
66
7+
use std::num::NonZeroU64;
78
use std::sync::Arc;
89

910
use tokio::sync::mpsc;
@@ -16,7 +17,7 @@ use crate::{BackendError, BackendHandle, Capabilities, ExecutionBackend, Result,
1617

1718
/// Number of cached snapshots the image registry retains before evicting
1819
/// least-recently-used entries.
19-
const REGISTRY_CAPACITY: u64 = 64;
20+
const REGISTRY_CAPACITY: NonZeroU64 = NonZeroU64::new(64).expect("64 is non-zero");
2021

2122
/// Runs the build locally via the in-process DAG scheduler, executing each
2223
/// step inside a VM provided by the injected [`hm_vm::VmBackend`].

crates/hm-vm/src/registry.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! exceeded, returning the evicted snapshot IDs so the caller can clean up
66
//! backend resources.
77
8+
use std::num::NonZeroU64;
89
use std::path::Path;
910
use std::time::{SystemTime, UNIX_EPOCH};
1011

@@ -28,7 +29,7 @@ use crate::types::SnapshotId;
2829
pub struct ImageRegistry {
2930
#[debug(skip)]
3031
conn: Mutex<Connection>,
31-
capacity: u64,
32+
capacity: NonZeroU64,
3233
}
3334

3435
/// Returns the current Unix epoch in seconds.
@@ -52,7 +53,7 @@ impl ImageRegistry {
5253
///
5354
/// Returns an error if the database cannot be opened or the schema cannot
5455
/// be applied.
55-
pub fn open(path: &Path, capacity: u64) -> Result<Self> {
56+
pub fn open(path: &Path, capacity: NonZeroU64) -> Result<Self> {
5657
if let Some(parent) = path.parent() {
5758
std::fs::create_dir_all(parent)?;
5859
}
@@ -176,11 +177,12 @@ impl ImageRegistry {
176177
/// its capacity. Returns the snapshot IDs of evicted entries.
177178
fn evict_overflow(&self) -> Vec<SnapshotId> {
178179
let count = self.len();
179-
if count <= self.capacity {
180+
let capacity = self.capacity.get();
181+
if count <= capacity {
180182
return Vec::new();
181183
}
182184

183-
let overflow = count - self.capacity;
185+
let overflow = count - capacity;
184186

185187
let Ok(conn) = self.conn.lock() else {
186188
return Vec::new();
@@ -221,6 +223,7 @@ mod tests {
221223
fn open_temp(capacity: u64) -> (ImageRegistry, tempfile::TempDir) {
222224
let dir = tempfile::tempdir().expect("failed to create temp dir");
223225
let db_path = dir.path().join("registry.db");
226+
let capacity = NonZeroU64::new(capacity).expect("capacity must be non-zero");
224227
let registry = ImageRegistry::open(&db_path, capacity).expect("failed to open registry");
225228
(registry, dir)
226229
}
@@ -294,14 +297,16 @@ mod tests {
294297
let dir = tempfile::tempdir().expect("failed to create temp dir");
295298
let db_path = dir.path().join("registry.db");
296299

300+
let capacity = NonZeroU64::new(10).expect("capacity must be non-zero");
301+
297302
{
298-
let reg = ImageRegistry::open(&db_path, 10).expect("open");
303+
let reg = ImageRegistry::open(&db_path, capacity).expect("open");
299304
reg.put("persistent", &SnapshotId("snap-persist".into()));
300305
assert_eq!(reg.len(), 1);
301306
// reg is dropped here, closing the connection.
302307
}
303308

304-
let reg2 = ImageRegistry::open(&db_path, 10).expect("reopen");
309+
let reg2 = ImageRegistry::open(&db_path, capacity).expect("reopen");
305310
assert_eq!(reg2.len(), 1);
306311
let got = reg2.get("persistent");
307312
assert_eq!(got, Some(SnapshotId("snap-persist".into())));

crates/hm-vm/src/vm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ mod tests {
258258
fn open_temp_registry(capacity: u64) -> (ImageRegistry, tempfile::TempDir) {
259259
let dir = tempfile::tempdir().expect("failed to create temp dir");
260260
let db = dir.path().join("registry.db");
261+
let capacity =
262+
std::num::NonZeroU64::new(capacity).expect("capacity must be non-zero");
261263
let reg = ImageRegistry::open(&db, capacity).expect("failed to open registry");
262264
(reg, dir)
263265
}

0 commit comments

Comments
 (0)