Volumes: persist measured volume sizes across VM sleep and restarts (#936)
This commit is contained in:
1 parent
ab6221abb0
commit
e8aa1c17da
2 files changed
+267
-76
No files matched your search
@@ -5,7 +5,7 @@
|
||||
//! ps -a` survives a daemon restart. Every state change notifies one
|
||||
//! condition variable, which is what `wait` blocks on.
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::mpsc::{self, Receiver, Sender};
|
||||
@@ -1751,17 +1751,52 @@ pub struct Engine {
|
||||
image_labels: Mutex<HashMap<String, serde_json::Value>>,
|
||||
/// The running VM's uplink status for `/info` and `kiri diag report` (#435).
|
||||
uplink: Mutex<serde_json::Value>,
|
||||
/// Short-lived cache of guest-measured volume sizes (`GET /kiri/volumes/sizes`),
|
||||
/// invalidated on volume create/remove and container remove while leaving
|
||||
/// `GET /system/df` live.
|
||||
/// Persisted and short-TTL cached volume byte sizes (`GET /kiri/volumes/sizes`
|
||||
/// and `<state_dir>/volume-sizes.json`), tracking volumes dirtied since the
|
||||
/// last guest disk measurement while leaving `GET /system/df` live.
|
||||
volume_sizes_cache: Mutex<VolumeSizesCache>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct VolumeSizesCache {
|
||||
path: PathBuf,
|
||||
generation: u64,
|
||||
measured_at: Option<std::time::Instant>,
|
||||
sizes: HashMap<String, u64>,
|
||||
dirty: HashSet<String>,
|
||||
}
|
||||
|
||||
pub(super) struct VolumeSizesSnapshot {
|
||||
pub generation: u64,
|
||||
pub fresh_ttl: bool,
|
||||
pub sizes: HashMap<String, u64>,
|
||||
pub dirty: HashSet<String>,
|
||||
}
|
||||
|
||||
impl VolumeSizesCache {
|
||||
fn load(root: &std::path::Path, current: &[super::volumes::Volume]) -> VolumeSizesCache {
|
||||
let path = root.join("volume-sizes.json");
|
||||
let mut sizes: HashMap<String, u64> = match std::fs::read(&path) {
|
||||
Ok(b) => serde_json::from_slice(&b).unwrap_or_default(),
|
||||
Err(_) => HashMap::new(),
|
||||
};
|
||||
sizes.retain(|k, _| current.iter().any(|v| v.name == *k && v.options.is_empty()));
|
||||
VolumeSizesCache {
|
||||
path,
|
||||
generation: 0,
|
||||
measured_at: None,
|
||||
sizes,
|
||||
dirty: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn save(&self) {
|
||||
if let Ok(json) = serde_json::to_vec(&self.sizes) {
|
||||
let tmp = self.path.with_extension("json.tmp");
|
||||
if let Err(e) = std::fs::write(&tmp, json).and_then(|()| std::fs::rename(&tmp, &self.path)) {
|
||||
crate::diag::error!("saving volume sizes failed", error = e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// k3s's pod and service ranges, which `kiri k8s` leaves at their defaults.
|
||||
@@ -1850,7 +1885,9 @@ impl Engine {
|
||||
let records = store.root().join("containers.json");
|
||||
let ceiling = memory_ceiling();
|
||||
let networks = Mutex::new(Networks::load(store.root())?);
|
||||
let volumes = Mutex::new(super::volumes::Volumes::load(store.root())?);
|
||||
let loaded_volumes = super::volumes::Volumes::load(store.root())?;
|
||||
let volume_sizes_cache = Mutex::new(VolumeSizesCache::load(store.root(), loaded_volumes.all()));
|
||||
let volumes = Mutex::new(loaded_volumes);
|
||||
let engine = Arc::new(Engine {
|
||||
volumes,
|
||||
link: Mutex::new(None),
|
||||
@@ -1897,7 +1934,7 @@ impl Engine {
|
||||
k8s_fwd: Mutex::new(HashMap::new()),
|
||||
image_labels: Mutex::new(HashMap::new()),
|
||||
uplink: Mutex::new(serde_json::Value::Null),
|
||||
volume_sizes_cache: Mutex::default(),
|
||||
volume_sizes_cache,
|
||||
});
|
||||
let _ = engine.me.set(Arc::downgrade(&engine));
|
||||
engine.start_syncer();
|
||||
@@ -2463,6 +2500,11 @@ impl Engine {
|
||||
return false;
|
||||
}
|
||||
let Some(vm) = self.link.lock().unwrap_or_else(|e| e.into_inner()).clone() else { return false };
|
||||
if self.volume_list().iter().any(|v| v.options.is_empty())
|
||||
&& let Some(lines) = vm.usage(Duration::from_secs(15))
|
||||
{
|
||||
self.cache_volume_sizes(None, super::ops::parse_volume_usage(&lines));
|
||||
}
|
||||
self.sleeping.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
self.memory_used.store(0, std::sync::atomic::Ordering::Relaxed);
|
||||
self.mem_available_kib.store(0, std::sync::atomic::Ordering::Relaxed);
|
||||
@@ -2774,38 +2816,104 @@ impl Engine {
|
||||
self.awake()
|
||||
}
|
||||
|
||||
/// Cached guest volume disk usage if younger than `ttl`, plus the cache
|
||||
/// generation so an in-flight measurement does not overwrite an invalidation.
|
||||
pub(super) fn volume_sizes_cached(&self, ttl: Duration) -> (u64, Option<HashMap<String, u64>>) {
|
||||
/// Snapshot of persisted + cached volume disk usage, TTL freshness, dirty
|
||||
/// set, and generation counter.
|
||||
pub(super) fn volume_sizes_snapshot(&self, ttl: Duration) -> VolumeSizesSnapshot {
|
||||
let c = self.volume_sizes_cache.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let hit = c.measured_at.is_some_and(|at| at.elapsed() < ttl).then(|| c.sizes.clone());
|
||||
(c.generation, hit)
|
||||
VolumeSizesSnapshot {
|
||||
generation: c.generation,
|
||||
fresh_ttl: c.measured_at.is_some_and(|at| at.elapsed() < ttl),
|
||||
sizes: c.sizes.clone(),
|
||||
dirty: c.dirty.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Stores freshly measured volume sizes. When `expected_gen` is `Some(g)`,
|
||||
/// skips writing if the cache was invalidated since generation `g`.
|
||||
pub(super) fn cache_volume_sizes(&self, expected_gen: Option<u64>, sizes: HashMap<String, u64>) {
|
||||
/// Stores freshly measured volume sizes and persists `<state_dir>/volume-sizes.json`.
|
||||
/// When `expected_gen` is `Some(g)`, skips writing if invalidated since `g`.
|
||||
pub(super) fn cache_volume_sizes(&self, expected_gen: Option<u64>, du_volumes: HashMap<String, u64>) {
|
||||
let volumes = self.volume_list();
|
||||
let mut c = self.volume_sizes_cache.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if expected_gen.is_some_and(|g| g != c.generation) {
|
||||
return;
|
||||
}
|
||||
let mut next = HashMap::with_capacity(volumes.len());
|
||||
for v in &volumes {
|
||||
if v.options.is_empty() {
|
||||
next.insert(v.name.clone(), du_volumes.get(&v.name).copied().unwrap_or(0));
|
||||
}
|
||||
}
|
||||
c.measured_at = Some(std::time::Instant::now());
|
||||
c.sizes = sizes;
|
||||
c.sizes = next;
|
||||
c.dirty.clear();
|
||||
c.save();
|
||||
}
|
||||
|
||||
/// Drops cached volume sizes and advances the generation counter.
|
||||
pub(super) fn invalidate_volume_sizes(&self) {
|
||||
/// Initializes a newly created local volume's persisted size to `0` (unless
|
||||
/// already measured) while bumping generation so an earlier in-flight scan
|
||||
/// cannot clobber it.
|
||||
fn record_new_volume(&self, v: &super::volumes::Volume) {
|
||||
let mut c = self.volume_sizes_cache.lock().unwrap_or_else(|e| e.into_inner());
|
||||
c.generation = c.generation.wrapping_add(1);
|
||||
c.measured_at = None;
|
||||
c.sizes.clear();
|
||||
c.dirty.remove(&v.name);
|
||||
if v.options.is_empty() {
|
||||
c.sizes.entry(v.name.clone()).or_insert(0);
|
||||
c.save();
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes a deleted volume from the persisted size map and bumps generation.
|
||||
fn forget_volume_size(&self, name: &str) {
|
||||
let mut c = self.volume_sizes_cache.lock().unwrap_or_else(|e| e.into_inner());
|
||||
c.generation = c.generation.wrapping_add(1);
|
||||
c.measured_at = None;
|
||||
c.dirty.remove(name);
|
||||
if c.sizes.remove(name).is_some() {
|
||||
c.save();
|
||||
}
|
||||
}
|
||||
|
||||
/// Marks mounted local volumes dirty when a container using them starts,
|
||||
/// exits, is removed, or receives `docker cp` writes.
|
||||
pub(super) fn mark_volumes_dirty<'a>(&self, names: impl IntoIterator<Item = &'a String>) {
|
||||
let names: Vec<String> = names.into_iter().cloned().collect();
|
||||
if names.is_empty() {
|
||||
return;
|
||||
}
|
||||
let local_set: HashSet<String> = self
|
||||
.volume_list()
|
||||
.into_iter()
|
||||
.filter(|v| v.options.is_empty())
|
||||
.map(|v| v.name)
|
||||
.collect();
|
||||
let mut c = self.volume_sizes_cache.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut any = false;
|
||||
for name in names {
|
||||
if local_set.contains(&name) {
|
||||
c.dirty.insert(name);
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
if any {
|
||||
c.generation = c.generation.wrapping_add(1);
|
||||
c.measured_at = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(super) fn set_volume_sizes_cache_at(&self, at: std::time::Instant, sizes: HashMap<String, u64>) {
|
||||
pub(super) fn set_volume_sizes_cache_at(&self, at: std::time::Instant, du_volumes: HashMap<String, u64>) {
|
||||
let volumes = self.volume_list();
|
||||
let mut c = self.volume_sizes_cache.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut next = HashMap::with_capacity(volumes.len());
|
||||
for v in &volumes {
|
||||
if v.options.is_empty() {
|
||||
next.insert(v.name.clone(), du_volumes.get(&v.name).copied().unwrap_or(0));
|
||||
}
|
||||
}
|
||||
c.measured_at = Some(at);
|
||||
c.sizes = sizes;
|
||||
c.sizes = next;
|
||||
c.dirty.clear();
|
||||
c.save();
|
||||
}
|
||||
|
||||
/// Queries `(in_use, capacity)` of the guest data disk while the VM is
|
||||
@@ -3092,7 +3200,7 @@ impl Engine {
|
||||
// rules wiped by a delayed `Forwards::drop` -> `Unpublish`
|
||||
// (#649). Loop in case a concurrent `start`/`connect`/
|
||||
// `set_ports` installed forwards while the lock was released.
|
||||
let (restart_in, persist, exited_name, actor, start_owed, remove, anon, removed_log, removed_layers) = loop {
|
||||
let (restart_in, persist, exited_name, actor, start_owed, remove, anon, removed_log, removed_layers, touched_volumes) = loop {
|
||||
let mut st = self.lock();
|
||||
if let Some(e) = st.execs.get_mut(id) {
|
||||
e.running = false;
|
||||
@@ -3123,8 +3231,10 @@ impl Engine {
|
||||
if let Some(h) = st.health.get_mut(id) {
|
||||
close_monitor(h);
|
||||
}
|
||||
let mut touched_volumes = Vec::new();
|
||||
let remove = match st.containers.get_mut(id) {
|
||||
Some(c) => {
|
||||
touched_volumes = c.spec.volumes.iter().chain(&c.spec.anon_volumes).cloned().collect();
|
||||
c.status = Status::Exited;
|
||||
c.exit_code = code;
|
||||
c.pid = 0;
|
||||
@@ -3180,7 +3290,7 @@ impl Engine {
|
||||
note_removed(&mut st, id, code);
|
||||
}
|
||||
drop(st);
|
||||
break (restart_in, persist, exited_name, actor, start_owed, remove, anon, removed_log, removed_layers);
|
||||
break (restart_in, persist, exited_name, actor, start_owed, remove, anon, removed_log, removed_layers, touched_volumes);
|
||||
};
|
||||
// Relieve the balloon and log the exit outside `self.state` so
|
||||
// neither `memory_target`, `link` nor `stopping_ops` is ever
|
||||
@@ -3207,6 +3317,7 @@ impl Engine {
|
||||
// reader), since that asks the VM, and vm() waits for this
|
||||
// reader when the VM was just lost.
|
||||
let forgotten: Vec<String> = self.unshared(anon).into_iter().filter(|v| self.forget_volume(v)).collect();
|
||||
self.mark_volumes_dirty(&touched_volumes);
|
||||
if !forgotten.is_empty() && let Some(me) = self.me.get().and_then(Weak::upgrade) {
|
||||
let _ = std::thread::Builder::new().name("kiri-volume-rm".into()).spawn(move || {
|
||||
if let Ok(vm) = me.vm() {
|
||||
@@ -3998,6 +4109,7 @@ impl Engine {
|
||||
(out.0, out.1, out.2, out.3, out.4, (out.5.0, out.5.1, earlier, halted))
|
||||
};
|
||||
self.clear_stopping_op(&id);
|
||||
self.mark_volumes_dirty(spec.volumes.iter().chain(&spec.anon_volumes));
|
||||
let (gate, exits, earlier, halted_at_claim) = gate;
|
||||
// A bind source that is a link is followed again at every start, as
|
||||
// Docker does: re-pointing it between runs (a `current` release
|
||||
@@ -4856,6 +4968,9 @@ impl Engine {
|
||||
let mut out = Vec::new();
|
||||
kiri_proto::encode_copy(&mut out, op, &id, path, flags, &layers, &mounts);
|
||||
std::io::Write::write_all(&mut std::fs::File::from(stream.fd.try_clone()?), &out)?;
|
||||
if op == kiri_proto::copy_op::IN {
|
||||
self.mark_volumes_dirty(spec.volumes.iter().chain(&spec.anon_volumes));
|
||||
}
|
||||
Ok(stream)
|
||||
}
|
||||
|
||||
@@ -5854,7 +5969,9 @@ impl Engine {
|
||||
self.delete_volume_data(&v);
|
||||
}
|
||||
}
|
||||
self.invalidate_volume_sizes();
|
||||
if let Some(c) = &removed {
|
||||
self.mark_volumes_dirty(c.spec.volumes.iter().chain(&c.spec.anon_volumes));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -5868,7 +5985,7 @@ impl Engine {
|
||||
let new = self.volume_get(name).is_none();
|
||||
let v = self.lock_volumes().ensure(name, labels, anonymous).map_err(Error::BadRequest)?;
|
||||
if new {
|
||||
self.invalidate_volume_sizes();
|
||||
self.record_new_volume(&v);
|
||||
self.emit_volume("create", name);
|
||||
}
|
||||
Ok(v)
|
||||
@@ -5880,7 +5997,7 @@ impl Engine {
|
||||
let new = self.volume_get(name).is_none();
|
||||
let v = self.lock_volumes().ensure_with(name, labels, false, options).map_err(Error::BadRequest)?;
|
||||
if new {
|
||||
self.invalidate_volume_sizes();
|
||||
self.record_new_volume(&v);
|
||||
self.emit_volume("create", name);
|
||||
}
|
||||
Ok(v)
|
||||
@@ -5945,7 +6062,7 @@ impl Engine {
|
||||
fn forget_volume(&self, name: &str) -> bool {
|
||||
let forgot = self.lock_volumes().forget(name);
|
||||
if forgot {
|
||||
self.invalidate_volume_sizes();
|
||||
self.forget_volume_size(name);
|
||||
self.emit_volume("destroy", name);
|
||||
}
|
||||
forgot
|
||||
@@ -6047,6 +6164,25 @@ impl Engine {
|
||||
flags |= exec_security.0;
|
||||
let console = config.console_size;
|
||||
self.wait_for_start(&container_id);
|
||||
let conflict = {
|
||||
let st = self.lock();
|
||||
match st.containers.get(&container_id) {
|
||||
None => Some(format!("container {} is not running", &container_id[..12])),
|
||||
Some(c) if c.status != Status::Running => Some(format!("container {} is not running", &container_id[..12])),
|
||||
Some(c) if c.paused && !is_builder(c) => Some(format!("Container {} is paused, unpause the container before exec", &container_id[..12])),
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
if let Some(msg) = conflict {
|
||||
let mut st = self.lock();
|
||||
if let Some(x) = st.execs.get_mut(exec_id) {
|
||||
x.running = false;
|
||||
x.attached.clear();
|
||||
}
|
||||
drop(st);
|
||||
self.changed.notify_all();
|
||||
return Err(Error::Conflict(msg));
|
||||
}
|
||||
let started = self.vm()?.exec(&container_id, exec_id, &config.argv, &env, &config.cwd, &config.user, flags, exec_security.1, &exec_security.2);
|
||||
if let Ok(pid) = started {
|
||||
if let Some(x) = self.lock().execs.get_mut(exec_id).filter(|x| x.exit_code.is_none()) {
|
||||
|
||||
+104
-49
@@ -556,29 +556,50 @@ fn system_df(engine: &Engine) -> Result<Value, Error> {
|
||||
Ok(json!({"LayersSize": layers_size, "Images": image_rows, "Containers": containers, "Volumes": volumes, "BuildCache": super::builder::cache(engine)}))
|
||||
}
|
||||
|
||||
/// How long `GET /kiri/volumes/sizes` reuses guest-measured volume sizes so
|
||||
/// tab switches do not re-walk the guest disk (`GET /system/df` stays live).
|
||||
/// How long `GET /kiri/volumes/sizes` reuses guest-measured volume sizes while
|
||||
/// the VM is awake so tab switches do not re-walk the guest disk (`GET /system/df`
|
||||
/// stays live).
|
||||
const VOLUME_SIZES_TTL: Duration = Duration::from_secs(30);
|
||||
|
||||
fn volume_sizes_map(volumes: &[super::volumes::Volume], du: &HashMap<String, u64>) -> serde_json::Map<String, Value> {
|
||||
pub(super) fn parse_volume_usage(lines: &[String]) -> HashMap<String, u64> {
|
||||
DiskUsage::parse(lines).volumes
|
||||
}
|
||||
|
||||
fn volume_sizes_map(
|
||||
volumes: &[super::volumes::Volume],
|
||||
du: &HashMap<String, u64>,
|
||||
dirty: &HashSet<String>,
|
||||
complete_scan: bool,
|
||||
) -> serde_json::Map<String, Value> {
|
||||
let mut out = serde_json::Map::with_capacity(volumes.len());
|
||||
for v in volumes {
|
||||
let size = match du.get(&v.name) {
|
||||
Some(&b) => i64::try_from(b).unwrap_or(i64::MAX),
|
||||
if !v.options.is_empty() {
|
||||
// Backed by driver options (NFS/bind/tmpfs): not on the guest data disk.
|
||||
out.insert(v.name.clone(), json!(-1));
|
||||
continue;
|
||||
}
|
||||
if !complete_scan && dirty.contains(&v.name) {
|
||||
continue;
|
||||
}
|
||||
match du.get(&v.name) {
|
||||
Some(&b) => {
|
||||
out.insert(v.name.clone(), json!(i64::try_from(b).unwrap_or(i64::MAX)));
|
||||
}
|
||||
// A plain local volume never mounted yet has no directory in the
|
||||
// guest and takes 0 bytes; one backed by driver options (NFS/bind/
|
||||
// tmpfs) is not measured on the guest data disk.
|
||||
None if v.options.is_empty() => 0,
|
||||
None => -1,
|
||||
};
|
||||
out.insert(v.name.clone(), json!(size));
|
||||
// guest and takes 0 bytes.
|
||||
None if complete_scan => {
|
||||
out.insert(v.name.clone(), json!(0));
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Each volume's size in bytes for the Volumes view (`GET /kiri/volumes/sizes`).
|
||||
/// Uses a short cache invalidated on volume/container changes and never wakes
|
||||
/// a sleeping or stopped VM.
|
||||
/// Uses persisted `<state_dir>/volume-sizes.json` across VM sleep and daemon
|
||||
/// restarts without waking the VM unless an active (`!is_stopped()`) engine has
|
||||
/// dirty or unmeasured local volumes.
|
||||
fn volume_sizes(engine: &Engine) -> Result<Value, Error> {
|
||||
volume_sizes_with_ttl(engine, VOLUME_SIZES_TTL)
|
||||
}
|
||||
@@ -588,20 +609,43 @@ fn volume_sizes_with_ttl(engine: &Engine, ttl: Duration) -> Result<Value, Error>
|
||||
if volumes.is_empty() {
|
||||
return Ok(json!({"sizes": {}}));
|
||||
}
|
||||
let (generation, cached) = engine.volume_sizes_cached(ttl);
|
||||
if let Some(du) = cached {
|
||||
return Ok(json!({"sizes": volume_sizes_map(&volumes, &du)}));
|
||||
let snap = engine.volume_sizes_snapshot(ttl);
|
||||
let any_missing_or_dirty = volumes
|
||||
.iter()
|
||||
.any(|v| v.options.is_empty() && (snap.dirty.contains(&v.name) || !snap.sizes.contains_key(&v.name)));
|
||||
|
||||
if let Some(vm) = engine.awake_link() {
|
||||
if snap.fresh_ttl && !any_missing_or_dirty {
|
||||
return Ok(json!({"sizes": volume_sizes_map(&volumes, &snap.sizes, &snap.dirty, false)}));
|
||||
}
|
||||
let Some(measured) = vm.usage(Duration::from_secs(120)) else {
|
||||
return Ok(json!({"sizes": {}}));
|
||||
};
|
||||
let du = DiskUsage::parse(&measured);
|
||||
engine.cache_volume_sizes(Some(snap.generation), du.volumes.clone());
|
||||
let current = engine.volume_list();
|
||||
return Ok(json!({"sizes": volume_sizes_map(¤t, &du.volumes, &HashSet::new(), true)}));
|
||||
}
|
||||
let Some(vm) = engine.awake_link() else {
|
||||
return Ok(json!({"sizes": {}}));
|
||||
};
|
||||
let Some(measured) = vm.usage(Duration::from_secs(120)) else {
|
||||
return Ok(json!({"sizes": {}}));
|
||||
};
|
||||
let du = DiskUsage::parse(&measured);
|
||||
engine.cache_volume_sizes(Some(generation), du.volumes.clone());
|
||||
let current = engine.volume_list();
|
||||
Ok(json!({"sizes": volume_sizes_map(¤t, &du.volumes)}))
|
||||
|
||||
if !any_missing_or_dirty {
|
||||
return Ok(json!({"sizes": volume_sizes_map(&volumes, &snap.sizes, &snap.dirty, false)}));
|
||||
}
|
||||
if !engine.is_stopped()
|
||||
&& let Some(measured) = engine.vm().ok().and_then(|vm| vm.usage(Duration::from_secs(120)))
|
||||
{
|
||||
let du = DiskUsage::parse(&measured);
|
||||
engine.cache_volume_sizes(Some(snap.generation), du.volumes.clone());
|
||||
let current = engine.volume_list();
|
||||
return Ok(json!({"sizes": volume_sizes_map(¤t, &du.volumes, &HashSet::new(), true)}));
|
||||
}
|
||||
|
||||
let has_any_clean_local = volumes
|
||||
.iter()
|
||||
.any(|v| v.options.is_empty() && !snap.dirty.contains(&v.name) && snap.sizes.contains_key(&v.name));
|
||||
if has_any_clean_local {
|
||||
return Ok(json!({"sizes": volume_sizes_map(&volumes, &snap.sizes, &snap.dirty, false)}));
|
||||
}
|
||||
Ok(json!({"sizes": {}}))
|
||||
}
|
||||
|
||||
/// `docker history`: the config's history, newest first, with each
|
||||
@@ -950,7 +994,7 @@ mod disk_usage_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn volume_sizes_never_wakes_asleep_vm_and_honors_cache_and_invalidation() {
|
||||
fn volume_sizes_persists_across_sleep_and_restart_without_waking_vm() {
|
||||
use serde_json::json;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::time::{Duration, Instant};
|
||||
@@ -967,42 +1011,53 @@ mod disk_usage_tests {
|
||||
nfs_opts.insert("device".into(), ":/export".into());
|
||||
engine.volume_ensure_with("nfs_vol", json!({}), nfs_opts).unwrap();
|
||||
|
||||
// With no awake VM and an empty cache, returns {} without attempting to boot a VM.
|
||||
let unmeasured = super::volume_sizes(&engine).unwrap();
|
||||
assert_eq!(unmeasured, json!({"sizes": {}}));
|
||||
let initial = super::volume_sizes(&engine).unwrap();
|
||||
assert_eq!(initial, json!({"sizes": {"pg_data": 0, "empty_vol": 0, "nfs_vol": -1}}));
|
||||
assert!(!engine.is_awake());
|
||||
|
||||
// Populate cache as if an awake measurement just completed.
|
||||
let mut measured = HashMap::new();
|
||||
measured.insert("pg_data".into(), 807_700_000u64);
|
||||
let (gen_before, _) = engine.volume_sizes_cached(Duration::from_secs(30));
|
||||
let gen_before = engine.volume_sizes_snapshot(Duration::from_secs(30)).generation;
|
||||
engine.cache_volume_sizes(Some(gen_before), measured.clone());
|
||||
assert!(root.join("volume-sizes.json").is_file(), "volume-sizes.json persisted");
|
||||
|
||||
let hit = super::volume_sizes_with_ttl(&engine, Duration::from_secs(30)).unwrap();
|
||||
assert_eq!(hit, json!({"sizes": {"pg_data": 807_700_000, "empty_vol": 0, "nfs_vol": -1}}));
|
||||
|
||||
// Stale cache entry (> TTL) misses and still does not wake the VM.
|
||||
engine.set_volume_sizes_cache_at(Instant::now() - Duration::from_secs(31), measured.clone());
|
||||
let expired = super::volume_sizes_with_ttl(&engine, Duration::from_secs(30)).unwrap();
|
||||
assert_eq!(expired, json!({"sizes": {}}));
|
||||
let after_ttl = super::volume_sizes_with_ttl(&engine, Duration::from_secs(30)).unwrap();
|
||||
assert_eq!(after_ttl, json!({"sizes": {"pg_data": 807_700_000, "empty_vol": 0, "nfs_vol": -1}}));
|
||||
|
||||
// Creating a new volume invalidates the cache and bumps generation so
|
||||
// an in-flight measurement from before creation cannot repopulate it.
|
||||
engine.cache_volume_sizes(None, measured.clone());
|
||||
let (old_gen, cached) = engine.volume_sizes_cached(Duration::from_secs(30));
|
||||
assert!(cached.is_some());
|
||||
let old_gen = engine.volume_sizes_snapshot(Duration::from_secs(30)).generation;
|
||||
engine.volume_ensure("new_vol", json!({}), false).unwrap();
|
||||
assert!(engine.volume_sizes_cached(Duration::from_secs(30)).1.is_none());
|
||||
engine.cache_volume_sizes(Some(old_gen), measured.clone());
|
||||
assert!(engine.volume_sizes_cached(Duration::from_secs(30)).1.is_none(), "stale generation write ignored");
|
||||
assert!(!engine.volume_sizes_snapshot(Duration::from_secs(30)).fresh_ttl);
|
||||
let mut stale = HashMap::new();
|
||||
stale.insert("pg_data".into(), 1u64);
|
||||
engine.cache_volume_sizes(Some(old_gen), stale);
|
||||
assert_eq!(
|
||||
super::volume_sizes(&engine).unwrap(),
|
||||
json!({"sizes": {"pg_data": 807_700_000, "empty_vol": 0, "new_vol": 0, "nfs_vol": -1}}),
|
||||
"stale generation write ignored and new_vol initialized to 0"
|
||||
);
|
||||
|
||||
// Removing a volume also invalidates the cache.
|
||||
engine.cache_volume_sizes(None, measured);
|
||||
assert!(engine.volume_sizes_cached(Duration::from_secs(30)).1.is_some());
|
||||
engine.volume_remove("new_vol", false).unwrap();
|
||||
assert!(engine.volume_sizes_cached(Duration::from_secs(30)).1.is_none());
|
||||
let saved_raw: HashMap<String, u64> =
|
||||
serde_json::from_slice(&std::fs::read(root.join("volume-sizes.json")).unwrap()).unwrap();
|
||||
assert!(!saved_raw.contains_key("new_vol"));
|
||||
assert_eq!(saved_raw.get("pg_data"), Some(&807_700_000));
|
||||
|
||||
engine.mark_volumes_dirty([&"pg_data".to_owned()]);
|
||||
assert_eq!(super::volume_sizes(&engine).unwrap(), json!({"sizes": {"empty_vol": 0, "nfs_vol": -1}}));
|
||||
|
||||
engine.cache_volume_sizes(None, measured);
|
||||
drop(engine);
|
||||
|
||||
let asleep_engine = super::super::engine::Engine::boot(kiri_image::store::Store::open(&root).unwrap()).unwrap();
|
||||
assert!(!asleep_engine.is_stopped(), "engine is asleep, not stopped");
|
||||
assert!(!asleep_engine.is_awake(), "VM starts asleep");
|
||||
let asleep_sizes = super::volume_sizes(&asleep_engine).unwrap();
|
||||
assert_eq!(asleep_sizes, json!({"sizes": {"pg_data": 807_700_000, "empty_vol": 0, "nfs_vol": -1}}));
|
||||
assert!(!asleep_engine.is_awake(), "persisted volume sizes returned without waking asleep VM");
|
||||
|
||||
drop(asleep_engine);
|
||||
let _ = std::fs::remove_dir_all(root);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user