Skip to main content

runnerd_core/
cache.rs

1//! The warm caches runner containers share and keep between jobs: each profile's read-only snapshot, each slot's
2//! volume (its layers' changes and its Docker Engine's data), the shared pnpm store and Maven repository, and the
3//! Docker Hub mirror on the runners' network. Owns their names, mounts and lifecycle; it never decides when a
4//! runner starts, and it never touches a volume a running container uses.
5
6use std::collections::{BTreeMap, HashMap};
7
8use bollard::models::{
9    ContainerCreateBody, EndpointSettings, HostConfig, Mount, MountType, NetworkingConfig,
10};
11
12use crate::api;
13use crate::config::{self, ByteSize};
14use crate::docker::{DockerApi, Error, RunnerSpec, is_not_found};
15use crate::isolation::Isolation;
16
17/// The Docker Hub pull-through cache (`registry` 3.1.2, multi-platform index).
18pub const MIRROR_IMAGE: &str =
19    "registry:3.1.2@sha256:01cd312e67026f06f5d89c3e133a4a9adc7cb9eeb9b1194e0b2954dfd770efd2";
20
21/// The host name runner containers reach the mirror at (`image/daemon.json`).
22const MIRROR_ALIAS: &str = "mirror";
23
24/// The mirror's `oom_score_adj`: the kernel picks it after the runners' processes, which keep the default 0.
25const MIRROR_OOM_SCORE_ADJ: i64 = -500;
26
27/// The names of everything runnerd creates in Docker, under one prefix: `runnerd`, or another for tests.
28#[derive(Debug, Clone)]
29pub struct Names {
30    prefix: String,
31}
32
33impl Names {
34    /// Names under `prefix`.
35    pub fn new(prefix: &str) -> Names {
36        Names {
37            prefix: prefix.to_owned(),
38        }
39    }
40
41    /// The network runners and the mirror share.
42    pub fn network(&self) -> String {
43        self.prefix.clone()
44    }
45
46    /// The profile's read-only snapshot.
47    pub fn base(&self, profile: &str) -> String {
48        format!("{}-{profile}-base", self.prefix)
49    }
50
51    /// Slot `slot` (1-based) of the profile: its layers' changes and its Docker Engine's data.
52    pub fn slot(&self, profile: &str, slot: usize) -> String {
53        format!("{}-{profile}-slot-{slot}", self.prefix)
54    }
55
56    /// The pnpm store all slots share.
57    pub fn pnpm(&self) -> String {
58        format!("{}-pnpm", self.prefix)
59    }
60
61    /// The Maven repository all slots share.
62    pub fn maven(&self) -> String {
63        format!("{}-maven", self.prefix)
64    }
65
66    /// The slot (1-based) of the profile whose volume `volume` is, if it is one of its slots'.
67    pub fn slot_of(&self, profile: &str, volume: &str) -> Option<usize> {
68        volume
69            .strip_prefix(&format!("{}-{profile}-slot-", self.prefix))?
70            .parse()
71            .ok()
72    }
73
74    /// The container that rebuilds the profile's snapshot.
75    pub fn seed(&self, profile: &str) -> String {
76        format!("{}-{profile}-seed", self.prefix)
77    }
78
79    /// The mirror's container and its storage volume.
80    pub fn mirror(&self) -> String {
81        format!("{}-mirror", self.prefix)
82    }
83
84    /// What `GET /v1/caches` and `runnerctl caches clean` call the shared build cache: a directory of the daemon's,
85    /// not a Docker volume.
86    pub fn build_cache(&self) -> String {
87        format!("{}-build-cache", self.prefix)
88    }
89}
90
91fn volume(source: String, target: &str, read_only: bool) -> Mount {
92    Mount {
93        target: Some(target.to_owned()),
94        source: Some(source),
95        typ: Some(MountType::VOLUME),
96        read_only: Some(read_only),
97        ..Default::default()
98    }
99}
100
101/// What a runner container in slot `slot` (1-based) of the profile mounts (paths from `image/Dockerfile`).
102pub fn runner_mounts(names: &Names, profile: &str, slot: usize) -> Vec<Mount> {
103    vec![
104        volume(names.base(profile), "/base", true),
105        volume(names.slot(profile, slot), "/state", false),
106        volume(names.pnpm(), "/shared/pnpm", false),
107        volume(names.maven(), "/shared/maven", false),
108    ]
109}
110
111/// The mirror container: Docker Hub through a proxy registry, which refuses pushes, so a job can only make it
112/// fetch what Docker Hub serves. Content not pulled again within the TTL expires. No published port.
113pub fn mirror_body(names: &Names, mirror: &config::Mirror, slice: &str) -> ContainerCreateBody {
114    let memory = i64::try_from(mirror.memory.0).unwrap_or(i64::MAX);
115    ContainerCreateBody {
116        image: Some(MIRROR_IMAGE.to_owned()),
117        env: Some(vec![
118            "REGISTRY_PROXY_REMOTEURL=https://registry-1.docker.io".to_owned(),
119            format!("REGISTRY_PROXY_TTL={}s", mirror.ttl.as_secs()),
120            // Expiry deletes content.
121            "REGISTRY_STORAGE_DELETE_ENABLED=true".to_owned(),
122        ]),
123        labels: Some(HashMap::from([(
124            "runnerd.mirror".to_owned(),
125            String::new(),
126        )])),
127        host_config: Some(HostConfig {
128            cgroup_parent: Some(slice.to_owned()),
129            memory: Some(memory),
130            memory_swap: Some(memory),
131            nano_cpus: Some(i64::from(mirror.cpus) * 1_000_000_000),
132            // When the slice runs out of memory, which measured admission allows, the kernel kills a job's process
133            // before the mirror every runner pulls through.
134            oom_score_adj: Some(MIRROR_OOM_SCORE_ADJ),
135            mounts: Some(vec![volume(names.mirror(), "/var/lib/registry", false)]),
136            network_mode: Some(names.network()),
137            ..Default::default()
138        }),
139        networking_config: Some(NetworkingConfig {
140            endpoints_config: Some(HashMap::from([(
141                names.network(),
142                EndpointSettings {
143                    aliases: Some(vec![MIRROR_ALIAS.to_owned()]),
144                    ..Default::default()
145                },
146            )])),
147        }),
148        ..Default::default()
149    }
150}
151
152/// Creates what a profile's runner in `slot` (1-based) mounts and joins, if missing.
153pub async fn prepare(
154    docker: &impl DockerApi,
155    names: &Names,
156    profile: &str,
157    slot: usize,
158) -> Result<(), Error> {
159    docker.ensure_network(&names.network()).await?;
160    for name in [
161        names.base(profile),
162        names.slot(profile, slot),
163        names.pnpm(),
164        names.maven(),
165    ] {
166        docker.ensure_volume(&name).await?;
167    }
168    Ok(())
169}
170
171/// Starts the mirror unless it runs.
172pub async fn mirror_up(
173    docker: &impl DockerApi,
174    names: &Names,
175    mirror: &config::Mirror,
176    slice: &str,
177) -> Result<(), Error> {
178    match docker.inspect(&names.mirror()).await {
179        Ok(state) if state.running => return Ok(()),
180        // Stopped on its own: started again from scratch, its storage is the volume.
181        Ok(_) => docker.remove(&names.mirror()).await?,
182        Err(error) if is_not_found(&error) => {}
183        Err(error) => return Err(error),
184    }
185    docker.ensure_network(&names.network()).await?;
186    if !docker.image_present(MIRROR_IMAGE).await? {
187        docker.pull(MIRROR_IMAGE).await?;
188    }
189    docker
190        .run(&names.mirror(), mirror_body(names, mirror, slice))
191        .await?;
192    Ok(())
193}
194
195/// Stops the mirror; its storage stays.
196pub async fn mirror_down(docker: &impl DockerApi, names: &Names) -> Result<(), Error> {
197    match docker.remove(&names.mirror()).await {
198        Err(error) if is_not_found(&error) => Ok(()),
199        other => other,
200    }
201}
202
203/// Whether the mirror's container is down, so its storage can be removed.
204#[derive(Debug, Clone, Copy, PartialEq, Eq)]
205pub enum Mirror {
206    /// It runs, or may: an earlier daemon may have left it up.
207    Up,
208    /// It is stopped.
209    Down,
210}
211
212/// What `trim_shared` measured and emptied.
213#[derive(Debug, Clone, PartialEq, Eq)]
214pub struct Trimmed {
215    /// Every volume's size in bytes, by name, as measured before emptying any.
216    pub sizes: BTreeMap<String, u64>,
217    /// The shared caches it emptied.
218    pub emptied: Vec<Emptied>,
219    /// The slot volumes past their profile's slot count it removed.
220    pub closed: Vec<Emptied>,
221    /// Docker's free disk in bytes, as measured before emptying any; `None` when it could not be measured.
222    pub free: Option<u64>,
223}
224
225/// A shared cache `trim_shared` emptied.
226#[derive(Debug, Clone, PartialEq, Eq)]
227pub struct Emptied {
228    /// The volume.
229    pub name: String,
230    /// How large it had grown.
231    pub size: ByteSize,
232}
233
234/// Empties the shared caches that outgrew their limit, the mirror's storage among them once the mirror is down
235/// (`Mirror::Down`), and removes the volumes of slots past the count `slots` gives their profile, as a lowered count
236/// leaves them: each holds a slot's layers and Docker store, which no job prunes again (docs/adr/0054). Call only
237/// while no runner runs, and leave out of `slots` a profile whose reseed runs, which mounts its slots' volumes.
238pub async fn trim_shared(
239    docker: &impl DockerApi,
240    names: &Names,
241    caches: &config::Caches,
242    mirror: Mirror,
243    slots: &BTreeMap<String, usize>,
244) -> Result<Trimmed, Error> {
245    let sizes = docker.volume_sizes().await?;
246    let free = docker.free_space().await.ok();
247    let mut emptied = Vec::new();
248    let mut limits = vec![
249        (names.pnpm(), caches.pnpm.max_size),
250        (names.maven(), caches.maven.max_size),
251    ];
252    if mirror == Mirror::Down {
253        limits.push((names.mirror(), caches.mirror.max_size));
254    }
255    for (name, limit) in limits {
256        if let Some(&size) = sizes.get(&name)
257            && size > limit.0
258        {
259            docker.remove_volume(&name).await?;
260            emptied.push(Emptied {
261                name,
262                size: ByteSize(size),
263            });
264        }
265    }
266    let mut closed = Vec::new();
267    for (name, &size) in &sizes {
268        let past = slots.iter().any(|(profile, &count)| {
269            names
270                .slot_of(profile, name)
271                .is_some_and(|slot| slot > count)
272        });
273        if past {
274            docker.remove_volume(name).await?;
275            closed.push(Emptied {
276                name: name.clone(),
277                size: ByteSize(size),
278            });
279        }
280    }
281    Ok(Trimmed {
282        sizes,
283        emptied,
284        closed,
285        free,
286    })
287}
288
289/// What the slots' Docker Engines may keep together, in bytes: a quarter of Docker's disk not otherwise used, which
290/// is what is free plus what the slot volumes already hold, measured together (docs/adr/0054).
291pub fn docker_disk(
292    names: &Names,
293    config: &config::Config,
294    free: u64,
295    sizes: &BTreeMap<String, u64>,
296) -> u64 {
297    let held: u64 = config
298        .profiles
299        .iter()
300        .flat_map(|(profile, settings)| {
301            (1..=settings.slots as usize).map(move |slot| names.slot(profile, slot))
302        })
303        .filter_map(|name| sizes.get(&name))
304        .sum();
305    free.saturating_add(held) / DOCKER_DISK_SHARE
306}
307
308/// One slot's share of `disk` among `slots`, in whole GiB as `image/layers.sh` takes it: at least 1, at most
309/// `DOCKER_SLOT_MAX_GIB`.
310pub fn docker_budget_gib(disk: u64, slots: u64) -> u64 {
311    ((disk / slots.max(1)) >> 30).clamp(1, DOCKER_SLOT_MAX_GIB)
312}
313
314/// The slots' Docker Engines together keep at most one part in this of Docker's disk not otherwise used.
315const DOCKER_DISK_SHARE: u64 = 4;
316
317/// The most one slot's Docker Engine keeps, in GiB: the image's own default, for a daemon that sets none.
318const DOCKER_SLOT_MAX_GIB: u64 = 20;
319
320/// Every cache volume the config's profiles use, what it holds and its limit, with size 0.
321pub fn volumes(names: &Names, config: &config::Config) -> Vec<api::CacheVolume> {
322    let volume = |name: String, kind: api::CacheKind, limit: Option<u64>| api::CacheVolume {
323        name,
324        kind,
325        bytes: 0,
326        limit,
327    };
328    let mut volumes = Vec::new();
329    for (profile, settings) in &config.profiles {
330        volumes.push(volume(
331            names.base(profile),
332            api::CacheKind::Snapshot {
333                profile: profile.clone(),
334            },
335            Some(config.caches.snapshot_max.0),
336        ));
337        for slot in 1..=settings.slots as usize {
338            volumes.push(volume(
339                names.slot(profile, slot),
340                api::CacheKind::Slot {
341                    profile: profile.clone(),
342                    slot,
343                },
344                None,
345            ));
346        }
347    }
348    volumes.push(volume(
349        names.pnpm(),
350        api::CacheKind::Pnpm,
351        Some(config.caches.pnpm.max_size.0),
352    ));
353    volumes.push(volume(
354        names.maven(),
355        api::CacheKind::Maven,
356        Some(config.caches.maven.max_size.0),
357    ));
358    if config.caches.mirror.enabled {
359        volumes.push(volume(
360            names.mirror(),
361            api::CacheKind::Mirror,
362            Some(config.caches.mirror.max_size.0),
363        ));
364    }
365    if config.caches.build.enabled {
366        volumes.push(volume(
367            names.build_cache(),
368            api::CacheKind::Build,
369            Some(config.caches.build.max_size.0),
370        ));
371    }
372    volumes.sort_by(|a, b| a.name.cmp(&b.name));
373    volumes
374}
375
376/// What `reseed` runs on: the profile's image and limits, its slots and the isolation backend.
377#[derive(Debug)]
378pub struct Reseed<'profile> {
379    /// Where the profile's volumes are.
380    pub names: &'profile Names,
381    /// The profile to reseed.
382    pub profile: &'profile str,
383    /// The profile's runner image, which holds the seed script.
384    pub image: &'profile str,
385    /// How many slots the profile has; each slot's layer changes are dropped.
386    pub slots: usize,
387    /// The systemd slice the seed container runs in.
388    pub slice: &'profile str,
389    /// The profile's CPUs.
390    pub cpus: u32,
391    /// The profile's memory.
392    pub memory: ByteSize,
393    /// Slot 1's layers must have changed at least this much (`caches.reseed_at`).
394    pub min_changes: ByteSize,
395    /// A snapshot that would outgrow this starts over (`caches.snapshot_max`).
396    pub max_size: ByteSize,
397    /// How the seed container is isolated, as runners are.
398    pub isolation: &'profile dyn Isolation,
399}
400
401/// Makes slot 1's layers the profile's new snapshot and drops every slot's own layer changes (`image/seed.sh`),
402/// if slot 1 changed at least `min_changes`. A snapshot that would outgrow `max_size` starts over from slot 1's
403/// changes alone, or from the image when those outgrow it too. Call only while none of the profile's slots runs. Returns the seed
404/// container's output.
405pub async fn reseed(docker: &impl DockerApi, reseed: &Reseed<'_>) -> Result<String, Error> {
406    let names = reseed.names;
407    let mut mounts = vec![volume(names.base(reseed.profile), "/base", false)];
408    mounts.extend((1..=reseed.slots).map(|slot| {
409        volume(
410            names.slot(reseed.profile, slot),
411            &format!("/workers/{slot}"),
412            false,
413        )
414    }));
415    for mount in &mounts {
416        if let Some(source) = &mount.source {
417            docker.ensure_volume(source).await?;
418        }
419    }
420    let name = names.seed(reseed.profile);
421    let spec = RunnerSpec {
422        name: name.clone(),
423        image: reseed.image.to_owned(),
424        profile: reseed.profile.to_owned(),
425        slice: reseed.slice.to_owned(),
426        cpus: reseed.cpus,
427        memory: reseed.memory,
428        env: Vec::new(),
429        mounts,
430        network: Some("none".to_owned()),
431        labels: BTreeMap::from([("runnerd.seed".to_owned(), String::new())]),
432        entrypoint: Some(vec![
433            "/usr/local/sbin/runnerd-seed".to_owned(),
434            reseed.min_changes.0.to_string(),
435            reseed.max_size.0.to_string(),
436        ]),
437        jit_config: None,
438    };
439    // A seed container left by a daemon that died while seeding.
440    match docker.remove(&name).await {
441        Err(error) if !is_not_found(&error) => return Err(error),
442        _ => {}
443    }
444    let id = docker.start(&spec, reseed.isolation).await?;
445    finish_reseed(docker, &id).await
446}
447
448/// Waits for a seed container to exit, removes it, and returns its output. Also takes over a seed container an
449/// earlier daemon left running: stopping it between its `rm` and `mv` would lose that snapshot layer.
450pub async fn finish_reseed(docker: &impl DockerApi, container: &str) -> Result<String, Error> {
451    let result = async {
452        let code = docker.wait(container).await?;
453        let output = docker.logs(container).await?;
454        if code == 0 {
455            Ok(output)
456        } else {
457            Err(Error::DockerContainerWaitError {
458                error: format!("seeding exited {code}: {}", output.trim()),
459                code,
460            })
461        }
462    }
463    .await;
464    let removed = docker.remove(container).await;
465    let output = result?;
466    removed?;
467    Ok(output)
468}
469
470#[cfg(test)]
471mod tests {
472    use super::docker_budget_gib;
473
474    #[test]
475    fn a_slots_docker_budget_is_its_share_between_1_and_20_gib() {
476        assert_eq!(docker_budget_gib(100 << 30, 10), 10);
477        assert_eq!(
478            docker_budget_gib(1 << 40, 4),
479            20,
480            "a large disk still stops at 20 GiB"
481        );
482        assert_eq!(
483            docker_budget_gib(1 << 30, 13),
484            1,
485            "a full disk still keeps 1 GiB"
486        );
487        assert_eq!(docker_budget_gib(1 << 30, 0), 1);
488    }
489}