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
//! A slot's runner from start to removal: registers it with a JIT config and starts its container, stops idle or
//! forced ones, frees the slots of ended containers and holds back a repository whose runners end without taking
//! a job. It must not decide which slots start or stop: `tick` does, through `plan`.
use std::collections::BTreeMap;
use std::time::{Duration, SystemTime};
use tracing::{info, warn};
use super::{Engine, Occupant, Purpose, RunningJobs, unix_secs};
use crate::api::{Admission, Event, HoldCause, RunnerFailure};
use crate::cache;
use crate::docker::{DockerApi, RunnerSpec, is_not_found};
use crate::github::{Deleted, GithubApi};
use crate::record;
use crate::scheduler::{Hold, IDLE_TIMEOUT, SlotRef};
/// The runner container's label with the labels its runner registered with, comma-separated as GitHub's runner
/// takes them, so a restart knows them.
pub(super) const LABELS: &str = "runnerd.labels";
/// The runner container's label with the image it runs, as the config named it, so a restart knows it.
pub(super) const IMAGE: &str = "runnerd.image";
/// Why a slot's runner is removed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Release {
/// Its container stopped on its own.
Ended,
/// It was idle and is no longer needed.
Idle,
}
/// The labels of a runner container: what a restart adopts it by.
fn container_labels(
name: &str,
repository: &str,
slot: usize,
runner_id: u64,
labels: &[String],
image: &str,
) -> BTreeMap<String, String> {
BTreeMap::from([
("runnerd.job".to_owned(), name.to_owned()),
("runnerd.repository".to_owned(), repository.to_owned()),
("runnerd.slot".to_owned(), slot.to_string()),
("runnerd.runner-id".to_owned(), runner_id.to_string()),
(LABELS.to_owned(), labels.join(",")),
(IMAGE.to_owned(), image.to_owned()),
])
}
/// Where the next backoff of a repository whose runners keep failing ends.
pub(super) fn backoff(failures: u32) -> Duration {
Duration::from_secs(30 << failures.saturating_sub(1).min(5)).min(Duration::from_secs(600))
}
impl<G: GithubApi + 'static, D: DockerApi + 'static> Engine<G, D> {
/// Frees the slots whose container stopped: the runner took its job and exited, or it never started.
pub(super) async fn collect_finished(&mut self) {
let occupied: Vec<(SlotRef, Occupant)> = self
.occupied()
.map(|(slot, occupant)| (slot, occupant.clone()))
.collect();
for (slot, occupant) in occupied {
let running = match self.docker.inspect(&occupant.container).await {
// A kill in the last interval before it stopped, which no sample of its cgroup saw.
Ok(state) if state.oom_killed => {
self.store.runner_oom_killed(&occupant.runner_name);
state.running
}
Ok(state) => state.running,
Err(error) if is_not_found(&error) => false,
Err(error) => {
warn!("cannot inspect {}: {error}", occupant.runner_name);
continue;
}
};
if !running {
info!("{}: {} ended", occupant.repository, occupant.runner_name);
self.release(&slot, &occupant, Release::Ended).await;
}
}
}
/// Removes a slot's container and its runner, then frees the slot. If the container `Ended` and its runner took
/// no job, the repository's next runners are held back for a while.
async fn release(&mut self, slot: &SlotRef, occupant: &Occupant, why: Release) {
// The runner logs each job it takes and how it ended; its exit code says nothing (run.sh exits 0 when it
// fails to start).
let logs = if why == Release::Ended {
match self.docker.logs(&occupant.container).await {
Ok(logs) => Some(logs),
Err(error) => {
warn!("cannot read the log of {}: {error}", occupant.runner_name);
None
}
}
} else {
None
};
let took_job = logs.as_ref().map(|logs| logs.contains("Running job: "));
// The capture ends with the container; its last lines must be stored before the job's record closes.
self.end_capture(&occupant.runner_name).await;
self.count_mirror_bytes().await;
// A failed job's container is kept as its record, then removed, in the background.
let recorded =
logs.as_deref().is_some_and(record::failed) && self.begin_record(slot, occupant).await;
if !recorded {
match self.docker.remove(&occupant.container).await {
Err(error) if !is_not_found(&error) => {
warn!("cannot remove {}: {error}", occupant.runner_name);
return;
}
_ => {}
}
}
match self
.github
.delete_runner(&occupant.repository, occupant.runner_id)
.await
{
// Busy: left to the next poll's cleanup of runners without a container.
Ok(_) => {}
Err(error) => warn!("cannot delete {}: {error}", occupant.runner_name),
}
self.forget(occupant);
let ran = occupant.busy
|| occupant.job.is_some()
|| match why {
// Unread logs: it may have run one.
Release::Ended => took_job != Some(false),
Release::Idle => false,
};
if let Some(state) = self.repositories.get_mut(&occupant.repository) {
match took_job {
Some(true) => {
state.failures = 0;
state.idle_exits = 0;
state.held = None;
}
// One that waited `IDLE_TIMEOUT` had started and connected: a warm runner whose container went
// once (a restart of Docker, the runner removed on GitHub) says nothing of the next one's start. The
// second in a row is a fault that would restart one every few minutes.
Some(false)
if occupant
.started_at
.elapsed()
.is_ok_and(|waited| waited >= IDLE_TIMEOUT) =>
{
state.idle_exits += 1;
// It started and connected: a failure before it is over, and the next one is a new streak's
// first, announced. Not so for the second in a row, a fault of its own.
if state.idle_exits == 1 {
state.failures = 0;
}
warn!(
"{}: {} ended without a job after waiting for one",
occupant.repository, occupant.runner_name
);
if state.idle_exits >= 2 {
self.hold(
&occupant.repository,
&slot.profile,
HoldCause::IdleExits,
"its runners end without a job after waiting for one".to_owned(),
);
}
}
Some(false) => {
warn!(
"{}: {} ended without taking a job",
occupant.repository, occupant.runner_name
);
self.hold(
&occupant.repository,
&slot.profile,
HoldCause::NoJob,
format!("{} ended without taking a job", occupant.runner_name),
);
}
None => {}
}
}
self.vacate(slot, ran);
}
/// Frees a slot whose runner is gone. Its runner may have grown the shared caches, and if it `ran` a job in slot
/// 1, its profile's layers: both are checked once idle. A killed job's partial writes count too: they stay in
/// slot 1 and would reach the snapshot on any later reseed, like those of a job cancelled or out of memory. A
/// warm runner that leaves slot 1 without a job changed nothing there.
fn vacate(&mut self, slot: &SlotRef, ran: bool) {
if let Some(entry) = self
.slots
.get_mut(&slot.profile)
.and_then(|slots| slots.get_mut(slot.index))
{
*entry = None;
}
if slot.index == 0 && ran {
self.reseed_due.insert(slot.profile.clone());
}
self.trim_due = true;
self.drop_closed_slots();
}
/// A runner container's environment: its slot's share of what the slots' Docker Engines may keep, which its
/// image's job cleanup holds it to, once the caches' check has measured Docker's disk.
fn runner_env(&self) -> Vec<String> {
self.docker_disk
.map(|disk| {
let slots = self
.config
.profiles
.values()
.map(|p| u64::from(p.slots))
.sum();
vec![format!(
"RUNNERD_DOCKER_BUDGET_GB={}",
cache::docker_budget_gib(disk, slots)
)]
})
.unwrap_or_default()
}
/// Removes an idle runner, or with `RunningJobs::Kill` a busy one and its job.
pub(super) async fn stop(&mut self, slot: &SlotRef, jobs: RunningJobs) {
let Some(occupant) = self
.slots
.get(&slot.profile)
.and_then(|slots| slots.get(slot.index))
.cloned()
.flatten()
else {
return;
};
if jobs == RunningJobs::Kill {
info!("{}: killing {}", occupant.repository, occupant.runner_name);
if let Err(error) = self.docker.remove(&occupant.container).await
&& !is_not_found(&error)
{
warn!("cannot remove {}: {error}", occupant.runner_name);
return;
}
self.end_capture(&occupant.runner_name).await;
// Responses the mirror logged since this tick's count: on a shutdown, the mirror and its log go next.
self.count_mirror_bytes().await;
self.forget(&occupant);
match self
.github
.delete_runner(&occupant.repository, occupant.runner_id)
.await
{
// GitHub refuses until it notices the job is gone, minutes later, even if the run is cancelled.
Ok(Deleted::Busy) => warn!(
"{}: GitHub keeps {} until it notices its job is gone; the next runnerd start deletes it",
occupant.repository, occupant.runner_name
),
Ok(_) => {}
Err(error) => warn!("cannot delete {}: {error}", occupant.runner_name),
}
self.vacate(slot, occupant.busy || occupant.job.is_some());
return;
}
match self
.github
.delete_runner(&occupant.repository, occupant.runner_id)
.await
{
Ok(Deleted::Busy) => {
if let Some(Some(entry)) = self
.slots
.get_mut(&slot.profile)
.and_then(|slots| slots.get_mut(slot.index))
{
entry.busy = true;
}
}
Ok(Deleted::Deleted | Deleted::NotFound) => {
info!(
"{}: removed idle {}",
occupant.repository, occupant.runner_name
);
self.release(slot, &occupant, Release::Idle).await;
}
Err(error) => warn!("cannot delete {}: {error}", occupant.runner_name),
}
}
/// Holds back a repository's next runners after one failed, longer each time in a row, and announces the first
/// hold of a streak as `runner.failed`: a repository that stays broken says so once, not every 10 minutes, until
/// a runner takes a job. Runners that fail while it is held started before the hold, so they do not lengthen it.
fn hold(&mut self, repository: &str, profile: &str, cause: HoldCause, error: String) {
if let Some(state) = self.repositories.get_mut(repository)
&& state
.held
.is_none_or(|held| held.until <= SystemTime::now())
{
state.failures += 1;
let wait = backoff(state.failures);
state.held = Some(Hold {
until: SystemTime::now() + wait,
cause,
});
warn!("{repository}: no runner starts for {}s", wait.as_secs());
if state.failures > 1 {
return;
}
self.store.event(Event::RunnerFailed {
failure: RunnerFailure {
repository: repository.to_owned(),
profile: profile.to_owned(),
error,
held_for: wait.as_secs(),
},
});
}
}
/// Starts the registry mirror, when enabled and down, for a runner about to start.
async fn start_mirror(&mut self) {
let mirror = &self.config.caches.mirror;
if mirror.enabled && !self.mirror_up {
match cache::mirror_up(&*self.docker, &self.names, mirror, &self.config.host.slice)
.await
{
Ok(()) => {
self.mirror_up = true;
self.mirror_memory = mirror.memory.0;
}
// Runners pull from Docker Hub directly when the mirror does not answer.
Err(error) => warn!("cannot start the mirror: {error}"),
}
}
}
pub(super) async fn start(
&mut self,
slot: &SlotRef,
repository: &str,
admission: Admission,
purpose: Purpose,
) {
let Some(labels) = self
.config
.repositories
.iter()
.find(|candidate| candidate.name == repository)
.map(|candidate| candidate.labels.clone())
else {
return;
};
let number = slot.index + 1;
let now = SystemTime::now();
let name = self
.identity
.runner_name(&slot.profile, number, unix_secs(now));
if let Err(error) = cache::prepare(&*self.docker, &self.names, &slot.profile, number).await
{
warn!(
"cannot prepare the caches of {} slot {number}: {error}",
slot.profile
);
return;
}
self.start_mirror().await;
let Some(profile) = self.config.profiles.get(&slot.profile) else {
return;
};
let (runner, jit_config) = match self.github.jit_config(repository, &name, &labels).await {
Ok(registered) => registered,
Err(error) => {
let failure = format!("cannot register a runner: {error}");
warn!("{repository}: {failure}");
self.hold(repository, &slot.profile, HoldCause::Register, failure);
return;
}
};
let image = profile.image.reference();
let spec = RunnerSpec {
name: name.clone(),
image: image.clone(),
profile: slot.profile.clone(),
slice: self.config.host.slice.clone(),
cpus: profile.cpus,
memory: profile.memory,
env: self.runner_env(),
mounts: self.runner_mounts(&slot.profile, number, &name, repository),
network: Some(self.names.network()),
labels: container_labels(&name, repository, number, runner.id, &labels, &image),
entrypoint: None,
jit_config: Some(jit_config),
};
match self.docker.start(&spec, self.isolation).await {
Ok(container) => {
let warm = match purpose {
Purpose::Job => "",
Purpose::Warm => " warm, for jobs not queued yet",
};
info!(
"{repository}: started {name} in {} slot {number}{warm}",
slot.profile
);
let occupant = Occupant {
repository: repository.to_owned(),
runner_name: name,
runner_id: runner.id,
container,
busy: false,
started_at: now,
memory: profile.memory.0,
cpus: u64::from(profile.cpus) * 1000,
admission: Some(admission),
job: None,
key: None,
labels: Some(labels),
image: Some(image),
};
if let Some(entry) = self
.slots
.get_mut(&slot.profile)
.and_then(|slots| slots.get_mut(slot.index))
{
*entry = Some(occupant.clone());
}
self.observe(slot, &occupant).await;
}
Err(error) => {
self.close_build_cache(&name);
warn!("{repository}: cannot start {name}: {error}");
self.recheck_image(&spec.image);
let failure = format!("cannot start {name}: {error}");
if let Err(error) = self.github.delete_runner(repository, runner.id).await {
warn!("{repository}: cannot delete {name}: {error}");
}
self.hold(repository, &slot.profile, HoldCause::Start, failure);
}
}
}
}