runnerd-core 0.1.2

Config, domain logic and API types shared by runnerd and its clients
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Startup recovery: adopts the runner and seed containers a previous daemon left, removes the rest, and reads what
//! the history left open. It changes nothing unless Docker lists both kinds of container and tells the caps of the
//! runners of removed profiles, so it can be tried again; it must not delete GitHub registrations: the first poll
//! does.

use std::collections::{BTreeMap, BTreeSet};
use std::time::{Duration, SystemTime};

use tracing::{info, warn};

use super::leftovers::Unread;
use super::records::Undecided;
use super::unsynced::Recovered;
use super::{Engagement, Engine, Occupant, Slot};
use crate::api;
use crate::cache;
use crate::docker::{self, DockerApi, is_being_removed, is_not_found};
use crate::github::GithubApi;
use crate::record;
use crate::scheduler::warm::Attempt;
use crate::scheduler::{Pool, SlotRef};
use crate::time;

/// The limits an adopted container runs with, and when it started.
struct Caps {
    /// Bytes.
    memory: u64,
    /// Millicores.
    cpus: u64,
    /// When Docker started it; `None` when Docker cannot tell.
    started_at: Option<SystemTime>,
}

/// The second a runner's name carries: the engine names the next runner later, even within that second or after the
/// clock stepped back.
fn name_second(name: &str) -> Option<u64> {
    name.rsplit_once('-')?.1.parse().ok()
}

impl<G: GithubApi + 'static, D: DockerApi + 'static> Engine<G, D> {
    /// Takes over the seed containers a previous daemon left: a running one counts as its profile's reseed, so no
    /// runner of the profile starts while it rewrites the snapshot and the slots' layers. A measure container it left
    /// changed nothing and is removed; slot 1 is measured again after its next job.
    async fn recover_reseeds(&mut self, containers: Vec<docker::Container>) {
        for container in containers {
            let Some(profile) = container.labels.get("runnerd.profile").cloned() else {
                continue;
            };
            if container.name == self.names.measure(&profile) {
                match self.docker.remove(&container.id).await {
                    Err(error) if !is_not_found(&error) => {
                        warn!("cannot remove {}: {error}", container.name);
                        self.leftovers.removing(container.id, container.name);
                    }
                    _ => info!("removed {}, left by an earlier run", container.name),
                }
                continue;
            }
            // Only this daemon's own names: tests seed under other prefixes on the same Engine.
            if container.name != self.names.seed(&profile) || !self.slots.contains_key(&profile) {
                continue;
            }
            info!("{profile}: waiting for the reseed an earlier run started");
            let docker = self.docker.clone();
            let id = container.id.clone();
            let memory = self.running_caps(&container, &profile).await.memory;
            self.upkeep.reseed_started(
                &profile,
                tokio::spawn(async move { cache::finish_reseed(&*docker, &id).await }),
                memory,
            );
        }
    }

    /// The memory and CPU caps a container left by an earlier daemon runs with, which a reload since may have
    /// changed in the config; when Docker cannot tell, the larger of the memory cap its record kept and the
    /// configured one, as a reload may have lowered that past what it runs with, and the configured CPUs. Also when it
    /// started.
    async fn running_caps(&self, container: &docker::Container, profile: &str) -> Caps {
        let state = self.docker.inspect(&container.id).await.ok();
        let configured = self.config.profiles.get(profile);
        let memory = if let Some(memory) = state.as_ref().and_then(|state| state.memory) {
            memory
        } else {
            let stored = self.store.runner_memory_cap(&container.name).await;
            let stored = stored.ok().flatten().unwrap_or(0);
            stored.max(configured.map_or(0, |settings| settings.memory.0))
        };
        Caps {
            memory,
            cpus: state
                .as_ref()
                .and_then(|state| state.cpus)
                .or_else(|| configured.map(|settings| u64::from(settings.cpus) * 1000))
                .unwrap_or(0),
            started_at: state
                .and_then(|state| state.started_at)
                .map(|ms| SystemTime::UNIX_EPOCH + Duration::from_millis(ms)),
        }
    }

    /// The caps of the running runner containers of profiles the config no longer has, which only Docker can tell,
    /// by container ID. An error unless Docker answered for each; one that it says nothing of is left out.
    async fn retired_caps(
        &self,
        containers: &[docker::Container],
    ) -> Result<BTreeMap<String, Caps>, docker::Error> {
        let mut caps = BTreeMap::new();
        for container in containers {
            let profile = container.labels.get("runnerd.profile");
            if !container.running
                || container.labels.contains_key(record::SHELL_LABEL)
                || profile.is_none_or(|profile| {
                    profile.is_empty() || self.config.profiles.contains_key(profile)
                })
            {
                continue;
            }
            let state = match self.docker.inspect(&container.id).await {
                Ok(state) => state,
                Err(error) if is_not_found(&error) => continue,
                Err(error) => return Err(error),
            };
            if let (Some(memory), Some(cpus)) = (state.memory, state.cpus) {
                caps.insert(
                    container.id.clone(),
                    Caps {
                        memory,
                        cpus,
                        started_at: state
                            .started_at
                            .map(|ms| SystemTime::UNIX_EPOCH + Duration::from_millis(ms)),
                    },
                );
            }
        }
        Ok(caps)
    }

    /// Removes the shell containers an earlier run left; each tick tries again those Docker failed to remove.
    async fn remove_shells(&mut self, shells: Vec<docker::Container>) {
        for shell in shells {
            info!("removing {}, a shell an earlier run left", shell.name);
            if let Err(error) = self.docker.remove(&shell.id).await {
                warn!("cannot remove {}: {error}", shell.name);
                self.leftovers.removing(shell.id, shell.name);
            }
        }
    }

    /// What a runner container left running by an earlier daemon runs as, from its labels, `caps` and its record.
    async fn adopted(&self, container: &docker::Container, caps: Caps) -> Occupant {
        let label = |name: &str| container.labels.get(name).cloned().unwrap_or_default();
        Occupant {
            repository: label("runnerd.repository"),
            runner_name: container.name.clone(),
            runner_id: label("runnerd.runner-id").parse().unwrap_or(0),
            container: container.id.clone(),
            // Its uptime goes on from its container's start, not from this restart.
            started_at: caps.started_at.unwrap_or_else(SystemTime::now),
            memory: caps.memory,
            cpus: caps.cpus,
            // What it reserved before the restart, as its record kept it; its cap if the record has none.
            admission: self
                .store
                .runner_admitted(&container.name)
                .await
                .ok()
                .flatten(),
            labels: container
                .labels
                .get(super::runners::LABELS)
                .map(|labels| labels.split(',').map(str::to_owned).collect()),
            image: container.labels.get(super::runners::IMAGE).cloned(),
            spec: container.labels.get(super::runners::SPEC).cloned(),
            // Its capture reports its job again; until then, or if it cannot, the history knows it.
            engagement: Engagement::adopted(
                self.store.runner_job(&container.name).await.ok().flatten(),
                self.store
                    .runner_cancel_deadline(&container.name)
                    .await
                    .ok()
                    .flatten()
                    .map(cancel_deadline),
            ),
        }
    }

    /// Removes a runner container an earlier run left that is not adopted, unless it stopped after a failed job whose
    /// runner its profile keeps: `decide_left` reads that.
    async fn remove_left(
        &mut self,
        container: docker::Container,
        slot: SlotRef,
        occupant: Occupant,
    ) {
        if container.running {
            self.remove_unkept(container.id, container.name).await;
        } else {
            self.decide_left(Unread::new(slot, occupant)).await;
        }
    }

    /// Keeps a stopped runner container an earlier run left as its failed job's record, or removes it. One Docker could
    /// not tell that of stays, never removed, and is asked about again after a backoff, as an unreadable one would
    /// otherwise be read again at every tick.
    pub(super) async fn decide_left(&mut self, unread: Unread) {
        match self.keep_left_record(&unread.slot, &unread.occupant).await {
            // Its capture ended with the last daemon, which may have left the job unnamed. Read once it is
            // decided, as one Docker cannot read comes back here until it is.
            Ok(kept) => {
                self.claim_from_worker_log(&unread.occupant).await;
                if kept {
                    return;
                }
            }
            Err(error) => {
                let name = unread.occupant.runner_name.clone();
                let wait = self
                    .leftovers
                    .undecided(unread, self.config.host.poll_interval);
                warn!(
                    "cannot tell whether {name} ran a failed job to keep; trying again in {}s: {error}",
                    wait.as_secs()
                );
                if let Undecided::Docker(error) = &error {
                    self.docker_failed(error);
                }
                return;
            }
        }
        let Occupant {
            container,
            runner_name,
            ..
        } = unread.occupant;
        self.remove_unkept(container, runner_name).await;
    }

    /// Removes a container an earlier run left that nothing keeps; each tick tries again if Docker does not.
    async fn remove_unkept(&mut self, id: String, name: String) {
        info!("removing {name}, left by an earlier run");
        match self.docker.remove(&id).await {
            Err(error) if is_being_removed(&error) => self.leftovers.removing(id, name),
            Err(error) if !is_not_found(&error) => {
                warn!("cannot remove {name}: {error}");
                self.leftovers.removing(id, name);
            }
            _ => {}
        }
    }

    /// Makes room for a running runner the config no longer has room for, as a reload leaves it: its slot past the
    /// profile's count, its profile retired with the limits it runs with, its repository draining.
    fn keep_draining(&mut self, profile: &str, index: usize, occupant: &Occupant) {
        let slots = self.slots.entry(profile.to_owned()).or_default();
        if slots.len() <= index {
            slots.resize_with(index + 1, Slot::default);
        }
        if !self.config.profiles.contains_key(profile) {
            self.retired
                .entry(profile.to_owned())
                .or_insert_with(|| crate::config::Profile {
                    image: crate::config::Image::Pulled(occupant.image.clone().unwrap_or_default()),
                    cpus: u32::try_from(occupant.cpus / 1000).unwrap_or(u32::MAX),
                    memory: crate::config::ByteSize(occupant.memory),
                    slots: 0,
                    record_failures: false,
                    min_memory: None,
                    max_memory: None,
                    max_slots: None,
                    priced_as: None,
                });
        }
        self.repositories.found_running(&occupant.repository);
    }

    /// Adopts the runner and seed containers a previous daemon left running, also those the config no longer has
    /// room for, which drain as after a reload, and removes the runners that stopped, or that run under this
    /// machine's names without a slot to adopt them in. Their registrations, and any of this host's runners without a
    /// container, go at the first poll.
    /// Changes nothing unless Docker lists both kinds and tells the caps of the runners of removed profiles, so it can
    /// be tried again.
    pub(super) async fn recover(&mut self) -> Result<(), docker::Error> {
        let seeds = self.docker.list("runnerd.seed").await?;
        let containers = self.docker.list("runnerd.job").await?;
        // Before anything changes: a retired profile's runner counts at zero if its caps are not known.
        let mut retired_caps = self.retired_caps(&containers).await?;
        self.recover_reseeds(seeds).await;
        // A shell's terminal does not outlive the daemon that served it. Shells carry their record's runner labels.
        let (shells, containers): (Vec<_>, Vec<_>) = containers
            .into_iter()
            .partition(|container| container.labels.contains_key(record::SHELL_LABEL));
        self.remove_shells(shells).await;
        self.list_records().await;
        // A reload may have lowered the configured cap since the mirror started.
        match self.docker.inspect(&self.names.mirror()).await {
            Ok(state) if state.running => {
                self.mirror.found_running(state.memory);
            }
            Ok(_) => self.mirror.found_not_running(),
            Err(error) if is_not_found(&error) => self.mirror.found_not_running(),
            // Unknown: the configured cap.
            Err(_) => {}
        }
        // Runner containers still running once recovery is done.
        let mut running = BTreeSet::new();
        for container in containers {
            // Runner containers on this Engine are this machine's whatever their name, which older builds or
            // another machine ID may have chosen: a running one is adopted when its labels name a free slot.
            let ours = container.name.starts_with(&self.runner_prefix);
            let label = |name: &str| container.labels.get(name).cloned().unwrap_or_default();
            let profile = label("runnerd.profile");
            let index = label("runnerd.slot")
                .parse::<usize>()
                .ok()
                .and_then(|slot| slot.checked_sub(1));
            if ours && let Some(secs) = name_second(&container.name) {
                self.named = self.named.max(secs);
            }
            let retired = retired_caps.remove(&container.id);
            let known_caps = retired.is_some() || self.config.profiles.contains_key(&profile);
            let caps = match retired {
                Some(caps) => caps,
                None => self.running_caps(&container, &profile).await,
            };
            let occupant = self.adopted(&container, caps).await;
            // A runner the config no longer has room for drains as it would have without the restart: in a slot
            // past a lowered count, of a removed profile or for a removed repository. One of a removed profile
            // whose caps Docker does not tell is removed, as it would count at zero.
            if ours
                && container.running
                && known_caps
                && !profile.is_empty()
                && !occupant.repository.is_empty()
                && let Some(index) = index
            {
                self.keep_draining(&profile, index, &occupant);
            }
            let known_repository = self.repositories.is_known(&occupant.repository);
            let free = index.and_then(|index| {
                self.slots
                    .get_mut(&profile)?
                    .get_mut(index)
                    .filter(|slot| slot.is_free())
            });
            match free {
                Some(slot) if container.running && known_repository => {
                    info!(
                        "adopted {} in {profile} slot {}",
                        occupant.runner_name,
                        index.map_or(0, |index| index + 1)
                    );
                    slot.place(occupant.clone());
                    running.insert(occupant.runner_name.clone());
                    // Its socket back in the directory its container mounts, if it does; its job's writes are
                    // decided again.
                    drop(self.attach_build_cache(
                        &occupant.runner_name,
                        &occupant.repository,
                        &profile,
                    ));
                    let slot = SlotRef {
                        profile,
                        index: index.unwrap_or(0),
                    };
                    self.observe(&slot, &occupant).await;
                }
                _ if !ours && container.running => {
                    running.insert(container.name.clone());
                    warn!(
                        "leaving {}: a runner container not named by this machine, for no free slot",
                        container.name
                    );
                }
                _ => {
                    let slot = SlotRef {
                        profile,
                        index: index.unwrap_or(0),
                    };
                    self.remove_left(container, slot, occupant).await;
                }
            }
        }
        if let Some(cache) = &self.build_cache {
            let keep: Vec<&str> = running.iter().map(String::as_str).collect();
            cache.forget_others(&keep);
        }
        self.drop_closed_slots();
        self.recover_history(&running).await;
        self.measure_docker_disk();
        self.recover_unsynced();
        Ok(())
    }

    /// Notes whether the host booted again since runners wrote without syncing, with neither a clean stop nor an
    /// orderly shutdown between: the first tick then empties their caches before it starts any runner (ADR 0141).
    fn recover_unsynced(&mut self) {
        match self.unsynced.recovered() {
            Ok(Recovered::Torn) => warn!(
                "the host booted again since runners wrote without syncing, and the journal shows no orderly \
                 shutdown that wrote it: their caches are emptied before any runner starts"
            ),
            Ok(Recovered::ShutDownInOrder) => info!(
                "the host shut down in order since runners wrote without syncing, which wrote it all: their caches \
                 are kept"
            ),
            Ok(Recovered::Unchanged) => {}
            Err(error) => warn!(
                "cannot keep for the next start what the host's boot since runners wrote without syncing came to: \
                 {error}"
            ),
        }
    }

    /// Reads what the history left open: queued jobs to watch, and jobs to close. A job whose runner's container
    /// is still `running` is left to run: `forget` closes it once its container is gone. Any other lost its
    /// container while no daemon watched: its runner is recorded removed now, unless it was before, as is every
    /// other runner the history holds open whose container is not running, with a job or idle.
    async fn recover_history(&mut self, running: &BTreeSet<String>) {
        self.store
            .runners_gone(running.iter().cloned().collect(), time::now_ms());
        match self.store.open_jobs().await {
            Ok(open) => {
                for job in open {
                    // Its run is as old as the job, for the warm runners its remaining jobs get, not as new as
                    // this start.
                    let runs = job.state == api::JobState::Queued
                        || job
                            .runner
                            .as_ref()
                            .is_some_and(|runner| running.contains(runner));
                    let pool = self
                        .config
                        .pool(&job.repository, &job.labels)
                        .map(Pool::from);
                    if runs && let Some(pool) = pool {
                        self.note_job(
                            &pool,
                            Attempt {
                                run_id: job.run_id,
                                run_attempt: job.run_attempt,
                            },
                            job.id,
                            job.workflow.as_deref(),
                            job.queued_at,
                        );
                    }
                    if job.state == api::JobState::Queued {
                        self.ledger.follow_queued(job.id, &job.repository);
                        continue;
                    }
                    if let Some(runner) = &job.runner {
                        if running.contains(runner) {
                            continue;
                        }
                        self.store.runner_removed(runner, time::now_ms());
                    }
                    self.ledger.close(job.id, &job.repository);
                    self.consume(&job.repository, job.id);
                }
            }
            Err(error) => warn!("cannot read the job history: {error}"),
        }
        // The next runner is named later than every one the history names, whose containers may be gone.
        match self
            .store
            .last_runner_second(self.runner_prefix.clone())
            .await
        {
            Ok(secs) => self.named = self.named.max(secs.unwrap_or_default()),
            Err(error) => warn!("cannot read the job history: {error}"),
        }
        // The slots' layers outlive the daemon, and so does what ran last in each.
        match self.store.slot_jobs().await {
            Ok(jobs) => {
                for job in jobs {
                    if let Some(index) = job.slot.checked_sub(1) {
                        self.ran
                            .entry(SlotRef {
                                profile: job.profile,
                                index,
                            })
                            .or_insert(job.key);
                    }
                }
            }
            Err(error) => warn!("cannot read the job history: {error}"),
        }
        match self.store.mirror_log_until().await {
            Ok(until) => self.mirror_until = until,
            Err(error) => warn!("cannot read the job history: {error}"),
        }
    }
}

/// The deadline a forced pause stored as `at`, in ms since the epoch, on this daemon's clock: now if it has passed.
fn cancel_deadline(at: u64) -> tokio::time::Instant {
    tokio::time::Instant::now() + Duration::from_millis(at.saturating_sub(time::now_ms()))
}