qk-cache 0.6.1

Task input hashing and caching for the qk task runner
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Keeps the cache under its size limit by evicting the least recently used
//! entries, as Nx's `maxCacheSize` does.
//!
//! An entry's last use is its manifest's modification time, refreshed on every
//! hit. Blobs are shared between entries, so a blob is deleted only once no
//! remaining manifest cites it. A blob no manifest cites may belong to an entry
//! another run is still publishing, so it is kept until it is an hour old.

use std::collections::{BTreeMap, BTreeSet};
use std::fs::{self, File};
use std::path::Path;
use std::time::{Duration, SystemTime};

use anyhow::{Context, Result, bail};
use fs2::FileExt;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// How long an unreferenced blob or scratch file may be part of a publish.
const IN_FLIGHT: Duration = Duration::from_secs(60 * 60);

#[derive(Debug, Default, PartialEq, Eq)]
pub struct Pruned {
    /// Bytes the cache holds afterwards.
    pub size: u64,
    pub entries: usize,
    pub blobs: usize,
    pub freed: u64,
}

/// Nx's `parseMaxCacheSize`: a number of bytes with an optional `KB`, `MB` or
/// `GB` unit, in powers of 1024.
pub fn parse_size(text: &str) -> Result<u64> {
    let text = text.trim();
    let (number, unit) = match text.find(|c: char| !(c.is_ascii_digit() || c == '.')) {
        Some(at) => (&text[..at], text[at..].trim_start()),
        None => (text, ""),
    };
    let factor: u64 = match unit {
        "" | "B" => 1,
        "KB" => 1 << 10,
        "MB" => 1 << 20,
        "GB" => 1 << 30,
        _ => bail!("invalid cache size {text:?}: use a number with an optional KB, MB or GB"),
    };
    if number.matches('.').count() > 1 || number.is_empty() {
        bail!("invalid cache size {text:?}");
    }
    let number: f64 = number
        .parse()
        .with_context(|| format!("invalid cache size {text:?}"))?;
    Ok((number * factor as f64) as u64)
}

/// The size limit: `NX_MAX_CACHE_SIZE`, then nx.json `maxCacheSize`, then, as
/// in Nx, a tenth of the disk holding the cache. `None` when the limit is 0,
/// which means unlimited.
pub fn max_size(configured: Option<&Value>, cache: &Path) -> Result<Option<u64>> {
    let configured = match std::env::var("NX_MAX_CACHE_SIZE") {
        Ok(value) => Some(value),
        Err(_) => configured.map(|value| match value {
            Value::String(text) => text.clone(),
            other => other.to_string(),
        }),
    };
    let limit = match configured {
        Some(text) => parse_size(&text)?,
        None => {
            let existing = cache
                .ancestors()
                .find(|path| path.exists())
                .context("cache path has no existing ancestor")?;
            fs2::total_space(existing)? / 10
        }
    };
    Ok((limit > 0).then_some(limit))
}

/// Refreshes an entry's last use.
pub(crate) fn touch(manifest: &Path) {
    if let Ok(file) = File::options().write(true).open(manifest) {
        let _ = file.set_modified(SystemTime::now());
    }
}

struct Entry {
    path: std::path::PathBuf,
    used: SystemTime,
    size: u64,
    blobs: BTreeSet<String>,
}

/// Evicts least recently used entries until the cache holds at most `limit`
/// bytes, and removes what no entry needs. Another prune in progress makes
/// this one return without doing anything.
///
/// Explicit pruning always remeasures the cache, including files changed in
/// place by external tools. Normal runs use [`prune_automatic`].
pub fn prune(root: &Path, limit: u64) -> Result<Pruned> {
    prune_inner(root, limit, false)
}

/// Enforces the limit after a run, reusing size accounting while the cache's
/// directories are unchanged. Normal publication replaces records atomically
/// and blobs are immutable, so directory changes invalidate the memo. A full
/// reconciliation still runs at least once an hour.
pub fn prune_automatic(root: &Path, limit: u64) -> Result<Pruned> {
    prune_inner(root, limit, true)
}

fn prune_inner(root: &Path, limit: u64, automatic: bool) -> Result<Pruned> {
    if !root.join("entries").is_dir() {
        return Ok(Pruned::default());
    }
    fs::create_dir_all(root.join("locks"))?;
    let marker = root.join("locks").join(".prune");
    let recent = fs::metadata(&marker)
        .ok()
        .and_then(|metadata| metadata.modified().ok())
        .and_then(|modified| SystemTime::now().duration_since(modified).ok())
        .is_some_and(|age| age < IN_FLIGHT);
    if automatic && recent {
        let size = cached_size(root)?;
        if size <= limit {
            return Ok(Pruned {
                size,
                ..Pruned::default()
            });
        }
    }
    let guard = File::options()
        .create(true)
        .truncate(false)
        .write(true)
        .open(&marker)?;
    if guard.try_lock_exclusive().is_err() {
        return Ok(Pruned::default());
    }
    // Never associate a computed post-eviction size with stamps taken only
    // after eviction: a concurrent publisher could have added uncounted bytes.
    let _ = fs::remove_file(root.join("locks").join(".size"));
    let _ = guard.set_modified(SystemTime::now());
    let now = SystemTime::now();
    let age = |metadata: &fs::Metadata| {
        metadata
            .modified()
            .ok()
            .and_then(|modified| now.duration_since(modified).ok())
            .unwrap_or_default()
    };
    let mut entries = Vec::new();
    let mut references: BTreeMap<String, usize> = BTreeMap::new();
    // Warm state records cite blobs as entries do, and age out the same way.
    let warm = fs::read_dir(root.join("warm")).into_iter().flatten();
    for item in fs::read_dir(root.join("entries"))?.chain(warm) {
        let item = item?;
        let metadata = item.metadata()?;
        // Unreadable manifests are misses already; they only take space.
        let blobs = fs::read(item.path())
            .ok()
            .and_then(|bytes| crate::record::StoredRecord::read(&bytes).ok())
            .map(|record| record.blobs())
            .unwrap_or_default();
        for blob in &blobs {
            *references.entry(blob.clone()).or_default() += 1;
        }
        entries.push(Entry {
            path: item.path(),
            used: metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH),
            size: metadata.len(),
            blobs,
        });
    }
    let mut blob_sizes = BTreeMap::new();
    let mut pruned = Pruned::default();
    for item in fs::read_dir(root.join("blobs"))? {
        let item = item?;
        let metadata = item.metadata()?;
        let name = item.file_name().to_string_lossy().into_owned();
        if !references.contains_key(&name) && age(&metadata) > IN_FLIGHT {
            if fs::remove_file(item.path()).is_ok() {
                pruned.blobs += 1;
                pruned.freed += metadata.len();
            }
            continue;
        }
        blob_sizes.insert(name, metadata.len());
    }
    let mut size: u64 =
        entries.iter().map(|entry| entry.size).sum::<u64>() + blob_sizes.values().sum::<u64>();
    entries.sort_by_key(|entry| entry.used);
    for entry in entries {
        if size <= limit {
            break;
        }
        if fs::remove_file(&entry.path).is_err() {
            continue;
        }
        pruned.entries += 1;
        pruned.freed += entry.size;
        size -= entry.size;
        for blob in entry.blobs {
            let count = references.get_mut(&blob).expect("counted above");
            *count -= 1;
            if *count == 0
                && let Some(blob_size) = blob_sizes.remove(&blob)
                && fs::remove_file(root.join("blobs").join(&blob)).is_ok()
            {
                pruned.blobs += 1;
                pruned.freed += blob_size;
                size -= blob_size;
            }
        }
    }
    // Scratch space of runs that ended without cleaning up.
    for item in fs::read_dir(root.join("tmp"))
        .into_iter()
        .flatten()
        .flatten()
    {
        if item
            .metadata()
            .is_ok_and(|metadata| age(&metadata) > IN_FLIGHT)
        {
            let _ = fs::remove_dir_all(item.path()).or_else(|_| fs::remove_file(item.path()));
        }
    }
    // Locks of keys without an entry, unless another run holds them.
    for item in fs::read_dir(root.join("locks"))?.flatten() {
        let name = item.file_name().to_string_lossy().into_owned();
        if name.starts_with('.') || root.join("entries").join(format!("{name}.json")).exists() {
            continue;
        }
        if item
            .metadata()
            .is_ok_and(|metadata| age(&metadata) > IN_FLIGHT)
            && let Ok(file) = File::open(item.path())
            && file.try_lock_exclusive().is_ok()
        {
            let _ = fs::remove_file(item.path());
        }
    }
    pruned.size = if automatic { cached_size(root)? } else { size };
    Ok(pruned)
}

// Recent timestamps may have coarse resolution on the backing filesystem.
const STAMP_SETTLE: Duration = Duration::from_secs(2);

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct DirectoryStamp {
    modified: SystemTime,
    len: u64,
    #[cfg(unix)]
    identity: (u64, u64, i64, i64),
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct SizeMemo {
    measured: SystemTime,
    stamps: Vec<Option<DirectoryStamp>>,
    size: u64,
}

fn directory_stamps(root: &Path) -> Result<Vec<Option<DirectoryStamp>>> {
    ["entries", "blobs", "warm"]
        .iter()
        .map(|name| {
            let metadata = match fs::metadata(root.join(name)) {
                Ok(metadata) => metadata,
                Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
                Err(error) => return Err(error.into()),
            };
            #[cfg(unix)]
            use std::os::unix::fs::MetadataExt;
            Ok(Some(DirectoryStamp {
                modified: metadata.modified()?,
                len: metadata.len(),
                #[cfg(unix)]
                identity: (
                    metadata.dev(),
                    metadata.ino(),
                    metadata.ctime(),
                    metadata.ctime_nsec(),
                ),
            }))
        })
        .collect()
}

fn settled(stamps: &[Option<DirectoryStamp>], now: SystemTime) -> bool {
    stamps.iter().flatten().all(|stamp| {
        let old_enough = |time| {
            now.duration_since(time)
                .is_ok_and(|age| age >= STAMP_SETTLE)
        };
        if !old_enough(stamp.modified) {
            return false;
        }
        #[cfg(unix)]
        {
            let (_, _, seconds, nanos) = stamp.identity;
            if seconds < 0 || !(0..1_000_000_000).contains(&nanos) {
                return false;
            }
            let changed = SystemTime::UNIX_EPOCH + Duration::new(seconds as u64, nanos as u32);
            if !old_enough(changed) {
                return false;
            }
        }
        true
    })
}

fn cached_size(root: &Path) -> Result<u64> {
    let memo_path = root.join("locks").join(".size");
    let now = SystemTime::now();
    let before = directory_stamps(root)?;
    if settled(&before, now)
        && let Some(memo) = fs::read(&memo_path)
            .ok()
            .and_then(|bytes| serde_json::from_slice::<SizeMemo>(&bytes).ok())
        && now
            .duration_since(memo.measured)
            .is_ok_and(|age| age < IN_FLIGHT)
        && memo.stamps == before
    {
        return Ok(memo.size);
    }
    let size = ["entries", "blobs", "warm"]
        .iter()
        .zip(&before)
        .filter(|(_, stamp)| stamp.is_some())
        .try_fold(0u64, |total, (name, _)| {
            directory_size(&root.join(name)).map(|size| total.saturating_add(size))
        })?;
    let after = directory_stamps(root)?;
    if before == after && settled(&after, now) {
        let memo = SizeMemo {
            measured: now,
            stamps: after,
            size,
        };
        // Concurrent writers can replace this memo safely: readers still compare
        // its stamps with the current directories. Partial writes are misses.
        let mut temporary = tempfile::NamedTempFile::new_in(root.join("locks"))?;
        serde_json::to_writer(temporary.as_file_mut(), &memo)?;
        temporary.persist(&memo_path).map_err(|error| error.error)?;
    } else {
        let _ = fs::remove_file(memo_path);
    }
    Ok(size)
}

fn directory_size(directory: &Path) -> Result<u64> {
    let mut size = 0;
    for item in fs::read_dir(directory)? {
        size += item?.metadata()?.len();
    }
    Ok(size)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn fixture() -> tempfile::TempDir {
        let root = tempfile::tempdir().unwrap();
        for name in ["entries", "blobs", "locks"] {
            fs::create_dir(root.path().join(name)).unwrap();
        }
        fs::write(root.path().join("entries/a.json"), b"bad record").unwrap();
        fs::write(root.path().join("blobs/a"), [0; 100]).unwrap();
        fs::write(root.path().join("locks/.prune"), []).unwrap();
        root
    }

    #[test]
    fn automatic_size_memo_reuses_and_reconciles() {
        let root = fixture();
        let root = root.path();
        // No memo can be trusted while directory timestamps are recent.
        assert_eq!(prune_automatic(root, 1_000).unwrap().size, 110);
        assert!(!root.join("locks/.size").exists());
        std::thread::sleep(STAMP_SETTLE + Duration::from_millis(100));
        assert_eq!(prune_automatic(root, 1_000).unwrap().size, 110);
        assert!(root.join("locks/.size").exists());

        // Unsupported external in-place changes intentionally demonstrate that
        // the automatic path reads only directory stamps, rather than each file.
        fs::write(root.join("blobs/a"), [0; 200]).unwrap();
        touch(&root.join("entries/a.json"));
        assert_eq!(prune_automatic(root, 1_000).unwrap().size, 110);
        assert_eq!(prune(root, 1_000).unwrap().size, 210);
        assert!(!root.join("locks/.size").exists());

        assert_eq!(prune_automatic(root, 1_000).unwrap().size, 210);
        fs::write(root.join("locks/.size"), b"corrupt").unwrap();
        assert_eq!(prune_automatic(root, 1_000).unwrap().size, 210);
        let mut memo: SizeMemo =
            serde_json::from_slice(&fs::read(root.join("locks/.size")).unwrap()).unwrap();
        memo.measured = SystemTime::UNIX_EPOCH;
        memo.size = 0;
        fs::write(root.join("locks/.size"), serde_json::to_vec(&memo).unwrap()).unwrap();
        assert_eq!(prune_automatic(root, 1_000).unwrap().size, 210);

        // An atomic publication invalidates accounting immediately.
        let temp = root.join("entries/new.tmp");
        fs::write(&temp, [0; 30]).unwrap();
        fs::rename(temp, root.join("entries/new.json")).unwrap();
        assert_eq!(prune_automatic(root, 1_000).unwrap().size, 240);
        fs::create_dir(root.join("warm")).unwrap();
        fs::write(root.join("warm/a.json"), [0; 40]).unwrap();
        assert_eq!(prune_automatic(root, 1_000).unwrap().size, 280);
        std::thread::sleep(STAMP_SETTLE + Duration::from_millis(100));
        assert_eq!(prune_automatic(root, 1_000).unwrap().size, 280);
        assert!(root.join("locks/.size").exists());
        // A lower limit must still trigger eviction with an unchanged memo.
        let result = prune_automatic(root, 250).unwrap();
        assert!(result.entries > 0);
        assert!(result.size <= 250);
    }

    #[test]
    fn old_prune_marker_forces_full_reconciliation() {
        let root = fixture();
        let root = root.path();
        let marker = File::options()
            .write(true)
            .open(root.join("locks/.prune"))
            .unwrap();
        marker.set_modified(SystemTime::UNIX_EPOCH).unwrap();
        let result = prune_automatic(root, 100).unwrap();
        assert_eq!(result.entries, 1);
        assert_eq!(result.size, 100);
    }

    #[test]
    fn parses_sizes_like_nx() {
        assert_eq!(parse_size("1024").unwrap(), 1024);
        assert_eq!(parse_size("1KB").unwrap(), 1024);
        assert_eq!(parse_size("1.5 MB").unwrap(), 1_572_864);
        assert_eq!(parse_size("2GB").unwrap(), 2 << 30);
        assert_eq!(parse_size("0").unwrap(), 0);
        for bad in ["", "1TB", "1.2.3MB", "MB", "-1"] {
            assert!(parse_size(bad).is_err(), "{bad}");
        }
    }
}