Skip to main content

runnerd_core/api/
storage.rs

1//! What the disk runnerd keeps means as every client shows it: what each cache volume holds and what its limit does,
2//! what the build cache did for a job and over a week, where GitHub Actions' cache is served, how full the records are and what became of a job's record,
3//! and how much of a job's log is stored. Plain derivations of the API's types; clients only lay them out.
4
5use super::{
6    BuildCacheCounts, BuildCacheStats, CacheKind, CacheVolume, LogSummary, RecordLimits,
7    RecordNote, RecordRemoval, Records, Tone, period,
8};
9use crate::config::ByteSize;
10
11/// The share of `[records] max_total` past which the records are near it, in percent: new ones then remove the
12/// oldest to fit.
13pub const RECORDS_NEAR_FULL: u64 = 80;
14
15/// Whether records taking `bytes` are near `max_total` (`RECORDS_NEAR_FULL`).
16pub fn records_near_full(bytes: u64, max_total: u64) -> bool {
17    u128::from(bytes) * 100 >= u128::from(max_total) * u128::from(RECORDS_NEAR_FULL)
18}
19
20impl CacheKind {
21    /// What it holds, as every client names it: `jvm snapshot`, `jvm slot 2 layer`, `pnpm store, shared`, `Maven
22    /// repository, shared`, `Docker Hub mirror` or `build cache, shared`.
23    pub fn description(&self) -> String {
24        match self {
25            CacheKind::Snapshot { profile } => format!("{profile} snapshot"),
26            CacheKind::Slot { profile, slot } => format!("{profile} slot {slot} layer"),
27            CacheKind::Pnpm => "pnpm store, shared".to_owned(),
28            CacheKind::Maven => "Maven repository, shared".to_owned(),
29            CacheKind::Mirror => "Docker Hub mirror".to_owned(),
30            CacheKind::Build => "build cache, shared".to_owned(),
31        }
32    }
33
34    /// The profile it belongs to; `None` for a shared one.
35    pub fn profile(&self) -> Option<&str> {
36        match self {
37            CacheKind::Snapshot { profile } | CacheKind::Slot { profile, .. } => Some(profile),
38            CacheKind::Pnpm | CacheKind::Maven | CacheKind::Mirror | CacheKind::Build => None,
39        }
40    }
41}
42
43/// A cache volume against its limit, from `CacheVolume::limit_status`.
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct CacheLimit {
46    /// In bytes.
47    pub limit: u64,
48    /// Whether the volume is past it, so runnerd acts on it.
49    pub over: bool,
50    /// What runnerd does at it: `emptied above 5G`, `trimmed to 20G`, or for a snapshot `started over at its next
51    /// reseed above 30G`.
52    pub label: String,
53}
54
55impl CacheLimit {
56    /// What its size means: past the limit is worth a look.
57    pub fn tone(&self) -> Tone {
58        if self.over { Tone::Warn } else { Tone::Neutral }
59    }
60}
61
62impl CacheVolume {
63    /// Its size against its limit, when it has one.
64    pub fn limit_status(&self) -> Option<CacheLimit> {
65        let limit = self.limit?;
66        let size = ByteSize(limit);
67        let label = match self.kind {
68            CacheKind::Build => format!("trimmed to {size}"),
69            CacheKind::Snapshot { .. } => format!("started over at its next reseed above {size}"),
70            CacheKind::Slot { .. } | CacheKind::Pnpm | CacheKind::Maven | CacheKind::Mirror => {
71                format!("emptied above {size}")
72            }
73        };
74        Some(CacheLimit {
75            limit,
76            over: self.bytes > limit,
77            label,
78        })
79    }
80}
81
82/// What the build cache did for one job, from `BuildCacheCounts::activity`.
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub struct BuildCacheActivity {
85    /// The counts.
86    pub counts: BuildCacheCounts,
87}
88
89impl BuildCacheCounts {
90    /// What it did, for a job that used it at all: looked an entry up, stored one, or was refused one. `None` for
91    /// a job that never used it.
92    pub fn activity(&self) -> Option<BuildCacheActivity> {
93        (self.lookups() > 0 || self.written_bytes > 0 || self.refused > 0)
94            .then_some(BuildCacheActivity { counts: *self })
95    }
96}
97
98impl BuildCacheActivity {
99    /// Its lookups: `3 of 4 lookups hit (75%)`, or `no lookups`.
100    pub fn lookups_line(&self) -> String {
101        let counts = &self.counts;
102        match counts.hit_percent() {
103            Some(percent) => format!(
104                "{} of {} lookups hit ({percent}%)",
105                counts.hits,
106                counts.lookups()
107            ),
108            None => "no lookups".to_owned(),
109        }
110    }
111
112    /// What it served: `1.5G served`.
113    pub fn served_line(&self) -> String {
114        format!("{} served", ByteSize(self.counts.read_bytes))
115    }
116
117    /// What it stored: `2G stored`, or `read only` for a job that may not write.
118    pub fn stored_line(&self) -> String {
119        if self.counts.writer {
120            format!("{} stored", ByteSize(self.counts.written_bytes))
121        } else {
122            "read only".to_owned()
123        }
124    }
125
126    /// The uploads it refused, when it refused any: `3 uploads refused`.
127    pub fn refused_line(&self) -> Option<String> {
128        (self.counts.refused > 0).then(|| format!("{} uploads refused", self.counts.refused))
129    }
130
131    /// All of it in a line: `3 of 4 lookups hit (75%), 1.5G served, 2G stored, 3 uploads refused`.
132    pub fn line(&self) -> String {
133        let mut parts = vec![self.lookups_line(), self.served_line(), self.stored_line()];
134        parts.extend(self.refused_line());
135        parts.join(", ")
136    }
137}
138
139/// The tools whose caches the build cache serves, as clients name them over its counts (ADR 0052, 0083). The image
140/// points each at it but Nx, which fails its run when the cache does not answer.
141pub const BUILD_CACHE_TOOLS: &str =
142    "Gradle, sccache, Bazel and Turborepo, and Nx where a workflow opts in";
143
144/// What doctor and the Caches page name GitHub Actions' cache.
145pub const ACTIONS_CACHE: &str = "GitHub Actions' cache";
146
147/// Where jobs' GitHub Actions cache is (ADR 0082), with `[caches.build] actions_cache` at `served`.
148pub fn actions_cache_place(served: bool) -> &'static str {
149    if served {
150        "served from the build cache on this host, in place of GitHub's"
151    } else {
152        "GitHub's, as [caches.build] actions_cache = false sets"
153    }
154}
155
156impl BuildCacheStats {
157    /// Where jobs' GitHub Actions cache is, and while it is here, where its hits are counted and how to turn it off:
158    /// `GitHub Actions' cache: served from the build cache on this host, in place of GitHub's, and counted with it;
159    /// [caches.build] actions_cache = false turns it off`.
160    pub fn actions_cache_line(&self) -> String {
161        let place = actions_cache_place(self.actions_cache);
162        if self.actions_cache {
163            format!(
164                "{ACTIONS_CACHE}: {place}, and counted with it; [caches.build] actions_cache = false turns it \
165                 off"
166            )
167        } else {
168            format!("{ACTIONS_CACHE}: {place}")
169        }
170    }
171
172    /// Its entries and the last 7 days' jobs: `120 entries; 75% of 400 lookups hit in 7 days, 3G served, 1G
173    /// stored, 2 uploads refused`, `120 entries; no lookups in 7 days, 1G stored`, or `120 entries; no lookups in 7
174    /// days`.
175    pub fn week_line(&self) -> String {
176        let week = &self.week;
177        let mut parts = match week.hit_percent() {
178            Some(percent) => vec![
179                format!("{percent}% of {} lookups hit in 7 days", week.lookups()),
180                format!("{} served", ByteSize(week.read_bytes)),
181            ],
182            None => vec!["no lookups in 7 days".to_owned()],
183        };
184        if week.activity().is_some() {
185            parts.push(format!("{} stored", ByteSize(week.written_bytes)));
186        }
187        if week.refused > 0 {
188            parts.push(format!("{} uploads refused", week.refused));
189        }
190        format!("{} entries; {}", self.entries, parts.join(", "))
191    }
192}
193
194impl RecordLimits {
195    /// Whether records taking `bytes` are near `max_total` (`records_near_full`).
196    pub fn near_full(&self, bytes: u64) -> bool {
197        records_near_full(bytes, self.max_total)
198    }
199}
200
201impl Records {
202    /// How many there are and how much they take of their limits, and when near `max_total`, `Records::pressure`:
203    /// `3 of 20 records, 1.2G of 10G, each kept 7 days; near max_total, so new ones remove the oldest`.
204    pub fn summary(&self) -> String {
205        match self.pressure() {
206            Some(pressure) => format!("{}; {pressure}", self.usage()),
207            None => self.usage(),
208        }
209    }
210
211    /// How many there are and how much they take of their limits: `3 of 20 records, 1.2G of 10G, each kept 7 days`.
212    pub fn usage(&self) -> String {
213        let limits = &self.limits;
214        // `[records] max_age` is in days more often than not.
215        let age = match limits.max_age / 86_400 {
216            1 if limits.max_age == 86_400 => "1 day".to_owned(),
217            days if days > 1 && limits.max_age.is_multiple_of(86_400) => format!("{days} days"),
218            _ => period(limits.max_age),
219        };
220        format!(
221            "{} of {} records, {} of {}, each kept {age}",
222            self.records.len(),
223            limits.max_count,
224            ByteSize(self.bytes()),
225            ByteSize(limits.max_total),
226        )
227    }
228
229    /// What being near `max_total` means, when they are: `near max_total, so new ones remove the oldest`.
230    pub fn pressure(&self) -> Option<&'static str> {
231        self.limits
232            .near_full(self.bytes())
233            .then_some("near max_total, so new ones remove the oldest")
234    }
235
236    /// What the summary means: near `max_total` is worth a look.
237    pub fn summary_tone(&self) -> Tone {
238        if self.limits.near_full(self.bytes()) {
239            Tone::Warn
240        } else {
241            Tone::Neutral
242        }
243    }
244
245    /// Why there are none: `none kept: a profile with record_failures = true keeps each failed job's runner as a
246    /// record, to open a shell where it failed`.
247    pub fn empty_reason() -> &'static str {
248        "none kept: a profile with record_failures = true keeps each failed job's runner as a record, to open a \
249         shell where it failed"
250    }
251
252    /// Whether a shell is open in `job`'s record.
253    pub fn shell_open(&self, job: u64) -> bool {
254        self.shells.contains(&job)
255    }
256}
257
258/// What became of a failed job's record, from `RecordNote::state`.
259#[derive(Debug, Clone, Copy, PartialEq, Eq)]
260pub enum RecordState<'note> {
261    /// None was made.
262    Skipped {
263        /// Why, such as too little free disk.
264        why: &'note str,
265    },
266    /// It was made, then removed.
267    Removed {
268        /// When.
269        at: u64,
270        /// Why, when known.
271        by: Option<RecordRemoval>,
272    },
273    /// It is kept: a shell can open in it.
274    Kept {
275        /// Its size when made, when known.
276        bytes: Option<u64>,
277    },
278}
279
280impl RecordNote {
281    /// What became of the record: not made, made and removed, or kept, in that order of precedence.
282    pub fn state(&self) -> RecordState<'_> {
283        match (&self.skipped, self.removed_at) {
284            (Some(why), _) => RecordState::Skipped { why },
285            (None, Some(at)) => RecordState::Removed {
286                at,
287                by: self.removed_by,
288            },
289            (None, None) => RecordState::Kept { bytes: self.bytes },
290        }
291    }
292
293    /// The command that opens a shell in `job`'s record: `runnerctl shell 42`.
294    pub fn shell_command(job: u64) -> String {
295        format!("runnerctl shell {job}")
296    }
297}
298
299impl RecordState<'_> {
300    /// Its words, before when it was removed for a removed one: `not kept: too little free disk`, `removed past
301    /// max_age`, or `record kept, 2G`.
302    pub fn line(&self) -> String {
303        match self {
304            RecordState::Skipped { why } => format!("not kept: {why}"),
305            RecordState::Removed { by, .. } => {
306                by.map_or_else(|| "removed".to_owned(), |by| by.to_string())
307            }
308            RecordState::Kept { bytes: Some(bytes) } => {
309                format!("record kept, {}", ByteSize(*bytes))
310            }
311            RecordState::Kept { bytes: None } => "record kept".to_owned(),
312        }
313    }
314}
315
316impl LogSummary {
317    /// How much of the log is stored: `120 lines, 4M`, and `, cut at history.log_max` once later lines were
318    /// dropped.
319    pub fn description(&self) -> String {
320        let line = format!("{} lines, {}", self.lines, ByteSize(self.bytes));
321        if self.truncated {
322            format!("{line}, cut at history.log_max")
323        } else {
324            line
325        }
326    }
327}
328
329#[cfg(test)]
330mod tests;