Skip to main content

runnerd_core/
store.rs

1//! The job history: a SQLite database of jobs, their runners, steps, samples and logs, the events clients resume
2//! from, cache sizes, the paused repositories and where the notifier got to. Owns the schema and its migrations, the one writer and the event
3//! broadcast. Writes are messages to a writer thread, so the engine never waits on the disk; reads and pruning run
4//! on Tokio's blocking pool, pruning on its own connection, a batch at a time. It decides nothing about scheduling
5//! and stores no secret: no token, no JIT config, no runner `_diag` log.
6
7use std::collections::BTreeMap;
8use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt};
9use std::path::{Path, PathBuf};
10use std::sync::atomic::{AtomicBool, Ordering};
11use std::sync::{Arc, Mutex, TryLockError};
12use std::time::Duration;
13
14use rusqlite::{Connection, OpenFlags};
15use tokio::sync::{OwnedSemaphorePermit, Semaphore, broadcast, mpsc, oneshot};
16use tracing::warn;
17
18use crate::api::{self, Event, EventEnvelope};
19use crate::config::History;
20use crate::github::{JobDetails, QueuedJob};
21use crate::observe::metrics::Events;
22use crate::scheduler::warm;
23use crate::sizing;
24use crate::sync::lock;
25
26mod prune;
27mod read;
28mod write;
29
30/// The schema's migrations, in order; the database's `user_version` is how many ran.
31const MIGRATIONS: &[&str] = &[
32    include_str!("store/migrations/0001_history.sql"),
33    include_str!("store/migrations/0002_runner_caps.sql"),
34    include_str!("store/migrations/0003_paused.sql"),
35    include_str!("store/migrations/0004_prune.sql"),
36    include_str!("store/migrations/0005_announced_profiles.sql"),
37    include_str!("store/migrations/0006_records.sql"),
38    include_str!("store/migrations/0007_admission.sql"),
39    include_str!("store/migrations/0008_memory_without_page_cache.sql"),
40    include_str!("store/migrations/0009_auto_sizing.sql"),
41    include_str!("store/migrations/0010_auto_sizing_cpus.sql"),
42    include_str!("store/migrations/0011_notify.sql"),
43    include_str!("store/migrations/0012_build_cache.sql"),
44    include_str!("store/migrations/0013_host_memory.sql"),
45];
46
47/// Bytes of log lines sent and not yet stored, at most: a job's capture waits beyond it, so a noisy job or a slow
48/// disk holds back that job's log, not the daemon's memory.
49pub(crate) const LOG_BACKLOG: u64 = 8 << 20;
50
51/// Host samples the writer holds before it stores them in one transaction: a minute's worth. One transaction per
52/// sample wrote about 300 MB a day to an idle daemon's WAL. A crash loses the samples held.
53const HOST_SAMPLE_BATCH: usize = 30;
54
55/// How many events a slow client may fall behind before its stream ends; it reconnects with `Last-Event-ID`.
56const EVENT_BUFFER: usize = 4096;
57
58/// Why the database cannot be used.
59#[derive(Debug, thiserror::Error)]
60pub enum StoreError {
61    /// SQLite failed.
62    #[error("{0}")]
63    Sqlite(#[from] rusqlite::Error),
64    /// A newer runnerd wrote the database.
65    #[error(
66        "the job history database is version {found}, newer than the {supported} this runnerd reads; \
67         run a newer runnerd or move the database aside"
68    )]
69    Newer {
70        /// The database's version.
71        found: u32,
72        /// The newest version this build knows.
73        supported: u32,
74    },
75    /// The directory or file cannot be created.
76    #[error("{0}")]
77    Io(#[from] std::io::Error),
78    /// A blocking read did not finish.
79    #[error("a database read stopped: {0}")]
80    Join(#[from] tokio::task::JoinError),
81    /// The database's `-wal` file holds changes not yet in it, and this user cannot open the database the way that
82    /// reads them.
83    #[error(
84        "{} holds changes not yet in the job history, which this user cannot read; run runnerd doctor as the user \
85         the daemon runs as",
86        .0.display()
87    )]
88    Unread(PathBuf),
89}
90
91/// A runner container as the history records it.
92#[derive(Debug, Clone, PartialEq, Eq)]
93pub struct RunnerRecord {
94    /// The runner's name, also its container's.
95    pub name: String,
96    /// `owner/name`.
97    pub repository: String,
98    /// The profile it runs in.
99    pub profile: String,
100    /// 1-based.
101    pub slot: usize,
102    /// The Docker container ID.
103    pub container: String,
104    /// When the container started, in milliseconds since the epoch.
105    pub started_at: u64,
106    /// The container's memory cap in bytes.
107    pub memory_cap: Option<u64>,
108    /// The container's CPU limit in millicores.
109    pub cpu_cap: Option<u64>,
110    /// How it was admitted; `None` for one a restart adopted without a record of it.
111    pub admission: Option<api::Admission>,
112}
113
114/// One sample of one runner's container.
115#[derive(Debug, Clone, PartialEq, Eq)]
116pub struct SampleRecord {
117    /// The runner.
118    pub runner: String,
119    /// When, in milliseconds since the epoch.
120    pub at: u64,
121    /// Millicores over the interval before it.
122    pub cpu: u64,
123    /// CPU time so far, in microseconds.
124    pub cpu_usage: u64,
125    /// Bytes without page cache (`observe::metrics::Usage::memory`).
126    pub memory: u64,
127    /// Bytes received since the container started.
128    pub network_received: u64,
129    /// Bytes sent since the container started.
130    pub network_sent: u64,
131    /// The container's `memory.events` so far.
132    pub events: Events,
133}
134
135/// The last measurement of the cache volumes.
136#[derive(Debug, Clone, Default, PartialEq, Eq)]
137pub struct Measured {
138    /// When it was taken (seconds); none before the first measurement.
139    pub at: Option<u64>,
140    /// Each volume's size in bytes, by name.
141    pub bytes: BTreeMap<String, u64>,
142}
143
144/// The profiles and repositories as the stored events last announced them: what clients resuming after those
145/// events have.
146#[derive(Debug, Clone, Default, PartialEq, Eq)]
147pub struct Announced {
148    /// The last `profile.changed` of each profile not removed since, by name.
149    pub profiles: Vec<api::ProfileStatus>,
150    /// The last `repository.changed` of each repository not removed since, by name.
151    pub repositories: Vec<api::RepositoryStatus>,
152}
153
154/// A job the history has not closed, as the engine needs it after a restart.
155#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct OpenJob {
157    /// GitHub's job ID.
158    pub id: u64,
159    /// `owner/name`.
160    pub repository: String,
161    /// `Queued` or `Running`.
162    pub state: api::JobState,
163    /// Its runner, once one took it.
164    pub runner: Option<String>,
165    /// When its runner was removed, in milliseconds since the epoch.
166    pub runner_removed_at: Option<u64>,
167    /// The workflow run it belongs to.
168    pub run_id: u64,
169    /// 1 for the run's first attempt.
170    pub run_attempt: u32,
171    /// The workflow's name, when known.
172    pub workflow: Option<String>,
173    /// When GitHub queued it, in milliseconds since the epoch.
174    pub queued_at: u64,
175}
176
177/// Where a job's log capture continues: the runner's page file and the byte offset in it.
178#[derive(Debug, Clone, Copy, PartialEq, Eq)]
179pub struct LogPosition {
180    /// The page number, from 1.
181    pub page: u64,
182    /// Bytes of that page already stored as lines.
183    pub offset: u64,
184}
185
186/// How far the mirror's log was counted: the time of the last response counted, in nanoseconds since the epoch as
187/// Docker logs it, and how many responses logged at exactly that time were counted, so the next read counts the
188/// ones it was too early to see.
189#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
190pub struct MirrorPosition {
191    /// Nanoseconds since the epoch.
192    pub at: u64,
193    /// Responses logged at `at` already counted.
194    pub seen: u64,
195}
196
197/// What the writer thread is asked to do, in order.
198#[derive(Debug)]
199enum Write {
200    Queued {
201        repository: String,
202        job: QueuedJob,
203    },
204    RunnerStarted(RunnerRecord),
205    RunnerAdmission {
206        runner: String,
207        admission: api::Admission,
208    },
209    RunnerOomKilled(String),
210    HostWaited {
211        jobs: Vec<u64>,
212        ms: u64,
213    },
214    BuildCache {
215        runner: String,
216        counts: api::BuildCacheCounts,
217    },
218    RunnerRemoved {
219        runner: String,
220        at: u64,
221    },
222    RunnersGone {
223        running: Vec<String>,
224        at: u64,
225    },
226    Claimed {
227        job: u64,
228        run_id: u64,
229        runner: String,
230        at: u64,
231    },
232    Details {
233        details: JobDetails,
234    },
235    Elsewhere {
236        details: JobDetails,
237    },
238    Lost {
239        job: u64,
240        at: u64,
241    },
242    Log {
243        job: u64,
244        lines: Vec<String>,
245        /// Where the capture resumes once these lines are stored; `None` for all but the last part of a batch.
246        position: Option<LogPosition>,
247        max_bytes: u64,
248        /// The lines' share of `LOG_BACKLOG`, given back once they are stored.
249        _backlog: OwnedSemaphorePermit,
250    },
251    Samples(Vec<SampleRecord>),
252    HostSample(api::HostSample),
253    MirrorBytes {
254        bytes: Vec<(String, u64)>,
255        until: MirrorPosition,
256        /// Whether the count committed.
257        done: oneshot::Sender<bool>,
258    },
259    CacheSizes {
260        at: u64,
261        sizes: BTreeMap<String, u64>,
262    },
263    /// Boxed: an event with a job's summary is many times the other writes' size.
264    Event(Box<Event>),
265    RecordMade(api::Record),
266    RecordSkipped {
267        job: u64,
268        why: String,
269    },
270    RecordRemoved {
271        job: u64,
272        at: u64,
273        by: api::RecordRemoval,
274    },
275    Paused {
276        repositories: Vec<String>,
277        /// Whether it committed.
278        done: oneshot::Sender<bool>,
279    },
280    SizingIntent {
281        change: api::SizingChange,
282        /// Its row, once it committed.
283        done: oneshot::Sender<Option<i64>>,
284    },
285    SizingResolved {
286        id: i64,
287        resolution: Resolution,
288        /// Whether it committed.
289        done: oneshot::Sender<bool>,
290    },
291    Notify {
292        change: NotifyChange,
293        /// Whether it committed.
294        done: oneshot::Sender<bool>,
295    },
296    Flush(oneshot::Sender<()>),
297}
298
299/// What the notifier records.
300#[derive(Debug)]
301enum NotifyChange {
302    /// Sets a target's keys and removes others.
303    Record {
304        target: String,
305        set: Vec<(String, u64)>,
306        remove: Vec<String>,
307    },
308    /// Forgets every target but these.
309    Keep(Vec<String>),
310}
311
312/// What became of a pending auto sizing change.
313#[derive(Debug, Clone, Copy, PartialEq, Eq)]
314pub enum Resolution {
315    /// The config file has it: announced as `sizing.changed`.
316    Done,
317    /// The file was not written.
318    Failed,
319    /// Left pending, and the config no longer says whether it was written: it keeps its interval and stays listed,
320    /// with what it replaced.
321    Uncertain,
322}
323
324/// Auto sizing as the history keeps it: when each profile last changed, and changes not yet known to be written.
325#[derive(Debug, Clone, Default, PartialEq, Eq)]
326pub struct AutoSizing {
327    /// The latest pending or done change of each profile, in seconds, however old.
328    pub last: BTreeMap<String, u64>,
329    /// Changes recorded before their file was written and not resolved since, by row.
330    pub pending: Vec<(i64, api::SizingChange)>,
331}
332
333/// The job history. Cloning shares it.
334#[derive(Debug, Clone)]
335pub struct Store {
336    writes: mpsc::UnboundedSender<Write>,
337    reader: Arc<Mutex<Connection>>,
338    /// Taken with `try_lock`, so one prune runs at a time; one that panicked midway left rows the next one deletes.
339    pruner: Arc<Mutex<Connection>>,
340    /// Set by `close`: a running prune stops after its batch, and none starts.
341    closed: Arc<AtomicBool>,
342    /// Host samples sent and not yet stored, oldest first; reads include them. The writer removes them once stored.
343    // Locked with `sync::lock`: every critical section is a push, a copy or a drain.
344    held: Arc<Mutex<Vec<api::HostSample>>>,
345    /// The last host sample sent, for admission to read without a query.
346    latest_host: Arc<Mutex<Option<api::HostSample>>>,
347    /// The last reading of the host's memory, which is not stored.
348    latest_host_memory: Arc<Mutex<Option<api::HostMemory>>>,
349    /// Each running runner's last sample, for admission to read without a query.
350    latest_samples: Arc<Mutex<BTreeMap<String, SampleRecord>>>,
351    events: broadcast::Sender<Arc<EventEnvelope>>,
352    backlog: Arc<Semaphore>,
353}
354
355impl Store {
356    /// Opens the database at `path`, creating it and its directory (mode 0700) if missing, and migrates it to the
357    /// latest version. Refuses a database a newer runnerd wrote.
358    pub fn open(path: &Path) -> Result<Store, StoreError> {
359        if let Some(dir) = path.parent().filter(|dir| !dir.as_os_str().is_empty()) {
360            std::fs::DirBuilder::new()
361                .recursive(true)
362                .mode(0o700)
363                .create(dir)?;
364        }
365        // Owner-only, as SQLite then makes its `-wal` and `-shm` files: logs may say more than the owner wants shared.
366        std::fs::OpenOptions::new()
367            .create(true)
368            .append(true)
369            .mode(0o600)
370            .open(path)?;
371        let mut db = Connection::open(path)?;
372        configure(&db)?;
373        migrate(&mut db)?;
374        let reader = Connection::open_with_flags(
375            path,
376            OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
377        )?;
378        configure(&reader)?;
379        let pruner = Connection::open(path)?;
380        configure(&pruner)?;
381        let (events, _) = broadcast::channel(EVENT_BUFFER);
382        let (writes, receiver) = mpsc::unbounded_channel();
383        let thread_events = events.clone();
384        let held = Arc::new(Mutex::new(Vec::new()));
385        let thread_held = held.clone();
386        std::thread::Builder::new()
387            .name("runnerd-store".to_owned())
388            .spawn(move || write::run(db, receiver, &thread_events, &thread_held))?;
389        Ok(Store {
390            writes,
391            reader: Arc::new(Mutex::new(reader)),
392            pruner: Arc::new(Mutex::new(pruner)),
393            closed: Arc::new(AtomicBool::new(false)),
394            held,
395            latest_host: Arc::new(Mutex::new(None)),
396            latest_host_memory: Arc::new(Mutex::new(None)),
397            latest_samples: Arc::new(Mutex::new(BTreeMap::new())),
398            events,
399            backlog: Arc::new(Semaphore::new(
400                usize::try_from(LOG_BACKLOG).unwrap_or(usize::MAX),
401            )),
402        })
403    }
404
405    fn send(&self, write: Write) {
406        // Fails only once the writer thread is gone, after a SQLite error it logged.
407        let _ = self.writes.send(write);
408    }
409
410    /// Records a queued job the repository's runners may run; announces it the first time.
411    pub fn queued(&self, repository: &str, job: &QueuedJob) {
412        self.send(Write::Queued {
413            repository: repository.to_owned(),
414            job: job.clone(),
415        });
416    }
417
418    /// Records a runner container that started, or one an earlier daemon started and this one adopted.
419    pub fn runner_started(&self, runner: RunnerRecord) {
420        self.send(Write::RunnerStarted(runner));
421    }
422
423    /// Records that a runner's container and registration were removed at `at` (ms).
424    pub fn runner_removed(&self, runner: &str, at: u64) {
425        lock(&self.latest_samples).remove(runner);
426        self.send(Write::RunnerRemoved {
427            runner: runner.to_owned(),
428            at,
429        });
430    }
431
432    /// Records every runner the history holds open, except the `running` ones, removed at `at` (ms): what recovery
433    /// found gone, so it stops counting as holding a slot.
434    pub fn runners_gone(&self, running: Vec<String>, at: u64) {
435        lock(&self.latest_samples).retain(|runner, _| running.contains(runner));
436        self.send(Write::RunnersGone { running, at });
437    }
438
439    /// Records that a runner took a job at `at` (ms).
440    pub fn claimed(&self, job: u64, run_id: u64, runner: &str, at: u64) {
441        self.send(Write::Claimed {
442            job,
443            run_id,
444            runner: runner.to_owned(),
445            at,
446        });
447    }
448
449    /// Records a running job's progress and steps as GitHub reports them; announces changed steps.
450    pub fn details(&self, details: JobDetails) {
451        self.send(Write::Details { details });
452    }
453
454    /// Closes a queued job that left the queue without a runner of this daemon.
455    pub fn elsewhere(&self, details: JobDetails) {
456        self.send(Write::Elsewhere { details });
457    }
458
459    /// Closes a job whose container is gone and that GitHub did not report completed in time.
460    pub fn lost(&self, job: u64, at: u64) {
461        self.send(Write::Lost { job, at });
462    }
463
464    /// Appends log lines to a job, up to `max_bytes` per job, and records where the capture continues. Waits while
465    /// `LOG_BACKLOG` bytes of lines are waiting to be stored; a larger batch goes in parts, each waiting its turn.
466    pub async fn log(&self, job: u64, lines: Vec<String>, position: LogPosition, max_bytes: u64) {
467        let size = |line: &String| line.len() as u64 + 1;
468        let mut lines = lines.into_iter().map(|mut line| {
469            // No line takes the whole backlog; the capture already cuts lines far shorter.
470            if size(&line) > LOG_BACKLOG {
471                let mut end = usize::try_from(LOG_BACKLOG / 2).unwrap_or(usize::MAX);
472                while !line.is_char_boundary(end) {
473                    end -= 1;
474                }
475                line.truncate(end);
476            }
477            line
478        });
479        let mut next = lines.next();
480        loop {
481            let (mut part, mut bytes) = (Vec::new(), 0);
482            while let Some(line) = next.take_if(|line| bytes + size(line) <= LOG_BACKLOG) {
483                bytes += size(&line);
484                part.push(line);
485                next = lines.next();
486            }
487            let last = next.is_none();
488            let share = u32::try_from(bytes.max(1)).unwrap_or(u32::MAX);
489            // The semaphore is never closed.
490            let Ok(backlog) = self.backlog.clone().acquire_many_owned(share).await else {
491                return;
492            };
493            self.send(Write::Log {
494                job,
495                lines: part,
496                position: last.then_some(position),
497                max_bytes,
498                _backlog: backlog,
499            });
500            if last {
501                return;
502            }
503        }
504    }
505
506    /// Records samples of runner containers.
507    pub fn samples(&self, samples: Vec<SampleRecord>) {
508        let mut latest = lock(&self.latest_samples);
509        for sample in &samples {
510            latest.insert(sample.runner.clone(), sample.clone());
511        }
512        drop(latest);
513        self.send(Write::Samples(samples));
514    }
515
516    /// Records a sample of the slice and sends it to every subscriber at once; it is stored with the next ones, a
517    /// minute's worth in one transaction, or at `flush`.
518    pub fn host_sample(&self, sample: api::HostSample) {
519        *lock(&self.latest_host) = Some(sample.clone());
520        lock(&self.held).push(sample.clone());
521        self.send(Write::HostSample(sample));
522    }
523
524    /// A runner's last sample, stored or not; `None` before its first sample or once it is removed.
525    pub fn latest_sample(&self, runner: &str) -> Option<SampleRecord> {
526        lock(&self.latest_samples).get(runner).cloned()
527    }
528
529    /// Records what a runner reserves now, when measured admission changed it after the start.
530    pub fn runner_admission(&self, runner: &str, admission: api::Admission) {
531        self.send(Write::RunnerAdmission {
532            runner: runner.to_owned(),
533            admission,
534        });
535    }
536
537    /// Records that Docker saw the kernel kill a process in a runner's container for memory, which its samples may
538    /// have missed: one in the last interval before the container stopped.
539    pub fn runner_oom_killed(&self, runner: &str) {
540        self.send(Write::RunnerOomKilled(runner.to_owned()));
541    }
542
543    /// Adds what the shared build cache served a runner and took from it, and records whether its job could write.
544    pub fn build_cache(&self, runner: &str, counts: api::BuildCacheCounts) {
545        self.send(Write::BuildCache {
546            runner: runner.to_owned(),
547            counts,
548        });
549    }
550
551    /// Keeps the latest reading of the host's memory for admission and the status; it is not stored.
552    pub fn host_memory(&self, memory: api::HostMemory) {
553        *lock(&self.latest_host_memory) = Some(memory);
554    }
555
556    /// The latest reading of the host's memory; `None` before the first.
557    pub fn latest_host_memory(&self) -> Option<api::HostMemory> {
558        *lock(&self.latest_host_memory)
559    }
560
561    /// Adds `ms` to how long each of `jobs` waited because the host's memory could not take its runner, which
562    /// sizing does not read as a wait for a slot.
563    pub fn host_waited(&self, jobs: Vec<u64>, ms: u64) {
564        self.send(Write::HostWaited { jobs, ms });
565    }
566
567    /// The last host sample sent, stored or not; `None` before the first.
568    pub fn latest_host_sample(&self) -> Option<api::HostSample> {
569        lock(&self.latest_host).clone()
570    }
571
572    /// Adds bytes the mirror served to runners, counted from its log up to `until`, and says whether that was
573    /// stored: if not, the caller counts the same lines again.
574    pub async fn mirror_bytes(&self, bytes: Vec<(String, u64)>, until: MirrorPosition) -> bool {
575        let (done, committed) = oneshot::channel();
576        self.send(Write::MirrorBytes { bytes, until, done });
577        committed.await.unwrap_or(false)
578    }
579
580    /// Records volume sizes in bytes, by name, measured at `at` (ms).
581    pub fn cache_sizes(&self, at: u64, sizes: BTreeMap<String, u64>) {
582        self.send(Write::CacheSizes { at, sizes });
583    }
584
585    /// Stores a durable event, or sends a live one, to every subscriber.
586    pub fn event(&self, event: Event) {
587        self.send(Write::Event(Box::new(event)));
588    }
589
590    /// Records that a failed job's runner was kept as `record`, and announces it.
591    pub fn record_made(&self, record: api::Record) {
592        self.send(Write::RecordMade(record));
593    }
594
595    /// Records why a failed job's runner was not kept.
596    pub fn record_skipped(&self, job: u64, why: String) {
597        self.send(Write::RecordSkipped { job, why });
598    }
599
600    /// Records that a job's record was removed at `at` (ms), and why, and announces it.
601    pub fn record_removed(&self, job: u64, at: u64, by: api::RecordRemoval) {
602        self.send(Write::RecordRemoved { job, at, by });
603    }
604
605    /// Records the paused repositories, all of them, in place of the ones recorded before. The write is sent at
606    /// once, in order with every other; the future says whether it committed.
607    pub fn paused(&self, repositories: Vec<String>) -> impl Future<Output = bool> + use<> {
608        let (done, committed) = oneshot::channel();
609        self.send(Write::Paused { repositories, done });
610        async move { committed.await.unwrap_or(false) }
611    }
612
613    /// Records a change auto sizing is about to write, as pending, before the file is, synced to disk. The future
614    /// has its row once it committed, `None` if it did not: then nothing may be written.
615    pub fn sizing_intent(
616        &self,
617        change: api::SizingChange,
618    ) -> impl Future<Output = Option<i64>> + use<> {
619        let (done, committed) = oneshot::channel();
620        self.send(Write::SizingIntent { change, done });
621        async move { committed.await.ok().flatten() }
622    }
623
624    /// Records what became of a pending change, synced to disk; a done one is announced as `sizing.changed` in the
625    /// same transaction. The future says whether it committed.
626    pub fn sizing_resolved(
627        &self,
628        id: i64,
629        resolution: Resolution,
630    ) -> impl Future<Output = bool> + use<> {
631        let (done, committed) = oneshot::channel();
632        self.send(Write::SizingResolved {
633            id,
634            resolution,
635            done,
636        });
637        async move { committed.await.unwrap_or(false) }
638    }
639
640    /// Records what the notifier delivered to a target: sets `set`'s keys and removes `remove`'s, in one
641    /// transaction. The future says whether it committed.
642    pub fn notify_record(
643        &self,
644        target: &str,
645        set: Vec<(String, u64)>,
646        remove: Vec<String>,
647    ) -> impl Future<Output = bool> + use<> {
648        let (done, committed) = oneshot::channel();
649        self.send(Write::Notify {
650            change: NotifyChange::Record {
651                target: target.to_owned(),
652                set,
653                remove,
654            },
655            done,
656        });
657        async move { committed.await.unwrap_or(false) }
658    }
659
660    /// Forgets what the notifier kept for every target but `targets`. The future says whether it committed.
661    pub fn notify_keep(&self, targets: Vec<String>) -> impl Future<Output = bool> + use<> {
662        let (done, committed) = oneshot::channel();
663        self.send(Write::Notify {
664            change: NotifyChange::Keep(targets),
665            done,
666        });
667        async move { committed.await.unwrap_or(false) }
668    }
669
670    /// Deletes what is older than `history` keeps, at `now` (ms), off the writer, in batches the writer's
671    /// transactions fit between. A prune while the last one runs does nothing. The handle finishes with it; dropping
672    /// it lets it run.
673    pub fn prune(&self, now: u64, history: &History) -> tokio::task::JoinHandle<()> {
674        let pruner = self.pruner.clone();
675        let closed = self.closed.clone();
676        let history = history.clone();
677        tokio::task::spawn_blocking(move || {
678            let db = match pruner.try_lock() {
679                Ok(db) => db,
680                Err(TryLockError::Poisoned(poisoned)) => poisoned.into_inner(),
681                Err(TryLockError::WouldBlock) => return,
682            };
683            let db = prune::Db {
684                connection: &db,
685                stop: &closed,
686            };
687            if !db.stop.load(Ordering::Relaxed) {
688                prune::run(&db, now, &history);
689            }
690        })
691    }
692
693    /// Stops a running prune after its batch, and any later one before it starts, so a shutdown does not wait for
694    /// one. What a stopped prune left, the next daemon's deletes. Writes go on as before.
695    pub fn close(&self) {
696        self.closed.store(true, Ordering::Relaxed);
697    }
698
699    /// Waits until every write sent before is done.
700    pub async fn flush(&self) {
701        let (done, wait) = oneshot::channel();
702        self.send(Write::Flush(done));
703        let _ = wait.await;
704    }
705
706    /// Every event from now on, durable or live. Subscribe before reading what was stored, then skip what the read
707    /// returned, and nothing is missed or sent twice.
708    pub fn subscribe(&self) -> broadcast::Receiver<Arc<EventEnvelope>> {
709        self.events.subscribe()
710    }
711
712    async fn read<T: Send + 'static>(
713        &self,
714        query: impl FnOnce(&Connection) -> rusqlite::Result<T> + Send + 'static,
715    ) -> Result<T, StoreError> {
716        let reader = self.reader.clone();
717        // A panic during a read leaves the connection usable: reads change nothing.
718        Ok(tokio::task::spawn_blocking(move || query(&lock(&reader))).await??)
719    }
720
721    /// One page of jobs, newest first.
722    pub async fn jobs(&self, query: api::JobQuery) -> Result<api::JobPage, StoreError> {
723        self.read(move |db| read::jobs(db, &query)).await
724    }
725
726    /// A job with its steps.
727    pub async fn job(&self, id: u64) -> Result<Option<api::Job>, StoreError> {
728        self.read(move |db| read::job(db, id)).await
729    }
730
731    /// A job's samples, oldest first; `None` if there is no such job.
732    pub async fn job_samples(
733        &self,
734        id: u64,
735        interval: Duration,
736    ) -> Result<Option<api::SampleSeries>, StoreError> {
737        self.read(move |db| read::job_samples(db, id, interval))
738            .await
739    }
740
741    /// Up to `limit` log lines of a job from line `from`, with their numbers.
742    pub async fn log_lines(
743        &self,
744        job: u64,
745        from: u64,
746        limit: u64,
747    ) -> Result<Vec<(u64, String)>, StoreError> {
748        self.read(move |db| read::log_lines(db, job, from, limit))
749            .await
750    }
751
752    /// Up to `limit` durable events after `after`, oldest first.
753    pub async fn events_after(
754        &self,
755        after: u64,
756        limit: u64,
757    ) -> Result<Vec<EventEnvelope>, StoreError> {
758        self.read(move |db| read::events_after(db, after, limit))
759            .await
760    }
761
762    /// The changes auto sizing wrote since `since` (ms), oldest first.
763    pub async fn sizing_changes(&self, since: u64) -> Result<Vec<api::SizingChange>, StoreError> {
764        self.read(move |db| read::sizing_changes(db, since)).await
765    }
766
767    /// When each profile last changed, and the changes still pending.
768    pub async fn auto_sizing(&self) -> Result<AutoSizing, StoreError> {
769        self.read(read::auto_sizing).await
770    }
771
772    /// The profiles and repositories as the stored events last announced them, so a restarted daemon announces
773    /// only what changed since, including what its config no longer has. Blocks on the database: for startup.
774    pub fn announced(&self) -> Result<Announced, StoreError> {
775        Ok(read::announced(&lock(&self.reader))?)
776    }
777
778    /// The repositories recorded paused, for the engine to start with. Blocks on the database: for startup.
779    pub fn paused_repositories(&self) -> Result<Vec<String>, StoreError> {
780        Ok(read::paused(&lock(&self.reader))?)
781    }
782
783    /// The slice's samples since `since` (seconds), oldest first, the ones not yet stored included.
784    pub async fn host_samples(&self, since: u64) -> Result<Vec<api::HostSample>, StoreError> {
785        // Copied before the read: the writer removes a sample only once it is stored, so each one is in the copy,
786        // the read, or both.
787        let held = lock(&self.held).clone();
788        let mut samples = self.read(move |db| read::host_samples(db, since)).await?;
789        let stored = samples.last().map_or(0, |sample| sample.at);
790        samples.extend(
791            held.into_iter()
792                .filter(|sample| sample.at >= since && sample.at > stored),
793        );
794        Ok(samples)
795    }
796
797    /// The jobs runnerd's runners started and finished from `since` until `now` (seconds).
798    pub async fn activity(&self, since: u64, now: u64) -> Result<api::Activity, StoreError> {
799        self.read(move |db| read::activity(db, since, now)).await
800    }
801
802    /// Finished jobs per repository and bucket.
803    pub async fn stats(&self, query: api::StatsQuery, now: u64) -> Result<api::Stats, StoreError> {
804        self.read(move |db| read::stats(db, &query, now)).await
805    }
806
807    /// What sizing reads: the finished jobs queued at or after `since` (ms) and the runners since.
808    pub async fn sizing(&self, since: u64) -> Result<sizing::History, StoreError> {
809        self.read(move |db| read::sizing(db, since)).await
810    }
811
812    /// The jobs completed or lost that were queued at or after `since` (ms), as insights read them.
813    pub async fn insights(&self, since: u64) -> Result<Vec<crate::insights::Run>, StoreError> {
814        self.read(move |db| read::insights(db, since)).await
815    }
816
817    /// The newest durable event's ID; 0 before the first.
818    pub async fn last_event(&self) -> Result<u64, StoreError> {
819        self.read(read::last_event).await
820    }
821
822    /// What the notifier kept for each target, by target, then key.
823    pub async fn notify_state(
824        &self,
825    ) -> Result<BTreeMap<String, BTreeMap<String, u64>>, StoreError> {
826        self.read(read::notify_state).await
827    }
828
829    /// The jobs queued at or after `since` (ms), whatever became of them, as warm runners learn from them.
830    pub async fn warm(&self, since: u64) -> Result<Vec<warm::Seen>, StoreError> {
831        self.read(move |db| read::warm(db, since)).await
832    }
833
834    /// The last measurement of every volume, with its time (seconds), by name.
835    pub async fn cache_sizes_now(&self) -> Result<Measured, StoreError> {
836        self.read(read::latest_cache_sizes).await
837    }
838
839    /// What the build cache served and took from the runners started since `since` (ms), summed.
840    pub async fn build_cache_since(&self, since: u64) -> Result<api::BuildCacheCounts, StoreError> {
841        self.read(move |db| read::build_cache_since(db, since))
842            .await
843    }
844
845    /// Every volume measurement since `since` (seconds).
846    pub async fn cache_history(&self, since: u64) -> Result<Vec<api::CacheSize>, StoreError> {
847        self.read(move |db| read::cache_history(db, since)).await
848    }
849
850    /// The jobs not yet closed, for the engine after a restart.
851    pub async fn open_jobs(&self) -> Result<Vec<OpenJob>, StoreError> {
852        self.read(read::open_jobs).await
853    }
854
855    /// The job a runner took, if the history knows it.
856    pub async fn runner_job(&self, runner: &str) -> Result<Option<u64>, StoreError> {
857        let runner = runner.to_owned();
858        self.read(move |db| read::runner_job(db, &runner)).await
859    }
860
861    /// How a runner was admitted, with what it reserves since, if the history knows it.
862    pub async fn runner_admitted(
863        &self,
864        runner: &str,
865    ) -> Result<Option<api::Admission>, StoreError> {
866        let runner = runner.to_owned();
867        self.read(move |db| read::runner_admission(db, &runner))
868            .await
869    }
870
871    /// Where a job's log capture continues.
872    pub async fn log_position(&self, job: u64) -> Result<LogPosition, StoreError> {
873        self.read(move |db| read::log_position(db, job)).await
874    }
875
876    /// How far the mirror's log was counted.
877    pub async fn mirror_log_until(&self) -> Result<MirrorPosition, StoreError> {
878        self.read(read::mirror_log_until).await
879    }
880}
881
882/// What sizing reads from the database at `path`, opened read-only and not migrated, as `runnerd doctor` reads it
883/// while no daemon runs; `None` when there is no database yet. In a directory it may write, SQLite keeps the
884/// `-wal` and `-shm` files a read of a WAL database needs next to it; in one it may not, it reads the file as it is,
885/// or refuses when the `-wal` holds changes the file does not have yet.
886pub fn sizing_at(path: &Path, since: u64) -> Result<Option<sizing::History>, StoreError> {
887    if !path.exists() {
888        return Ok(None);
889    }
890    let read = |path: &str, flags| -> Result<sizing::History, StoreError> {
891        let db = Connection::open_with_flags(path, flags)?;
892        db.busy_timeout(Duration::from_secs(5))?;
893        sizing_of(&db, since)
894    };
895    let name = path.to_string_lossy();
896    match read(&name, OpenFlags::SQLITE_OPEN_READ_ONLY) {
897        Err(StoreError::Sqlite(error))
898            if matches!(
899                error.sqlite_error_code(),
900                Some(rusqlite::ErrorCode::ReadOnly | rusqlite::ErrorCode::CannotOpen)
901            ) =>
902        {
903            // No `-shm` can be made: read the file alone, unless the `-wal` holds what it lacks.
904            let mut wal = path.as_os_str().to_owned();
905            wal.push("-wal");
906            let wal = PathBuf::from(wal);
907            if std::fs::metadata(&wal).is_ok_and(|wal| wal.len() > 0) {
908                return Err(StoreError::Unread(wal));
909            }
910            let uri = format!(
911                "file:{}?mode=ro&immutable=1",
912                name.replace('%', "%25")
913                    .replace('?', "%3f")
914                    .replace('#', "%23")
915            );
916            read(
917                &uri,
918                OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_URI,
919            )
920        }
921        history => history,
922    }
923    .map(Some)
924}
925
926/// What sizing reads from a database of any version this build knows, without migrating it.
927fn sizing_of(db: &Connection, since: u64) -> Result<sizing::History, StoreError> {
928    let found: u32 = db.pragma_query_value(None, "user_version", |row| row.get(0))?;
929    let supported = u32::try_from(MIGRATIONS.len()).unwrap_or(u32::MAX);
930    if found > supported {
931        return Err(StoreError::Newer { found, supported });
932    }
933    if found == 0 {
934        return Ok(sizing::History::default());
935    }
936    // Columns sizing reads that later versions added: before them, a temporary view gives each runner none.
937    let added = match found {
938        1 => {
939            "NULL AS memory_cap, NULL AS cpu_cap, NULL AS admission, NULL AS oom_kills, NULL AS ooms, \
940             NULL AS memory_measure"
941        }
942        2..=6 => "NULL AS admission, NULL AS oom_kills, NULL AS ooms, NULL AS memory_measure",
943        7 => "NULL AS ooms, NULL AS memory_measure",
944        _ => "",
945    };
946    if found < 13 {
947        db.execute_batch("CREATE TEMP VIEW jobs AS SELECT *, 0 AS host_waited FROM main.jobs")?;
948    }
949    if !added.is_empty() {
950        db.execute_batch(&format!(
951            "CREATE TEMP VIEW runners AS SELECT *, {added} FROM main.runners"
952        ))?;
953    }
954    Ok(read::sizing(db, since)?)
955}
956
957/// WAL, so the API reads while the writer writes; a busy timeout instead of an immediate error.
958fn configure(db: &Connection) -> rusqlite::Result<()> {
959    db.pragma_update(None, "journal_mode", "wal")?;
960    db.pragma_update(None, "synchronous", "normal")?;
961    db.pragma_update(None, "foreign_keys", "on")?;
962    db.busy_timeout(Duration::from_secs(5))
963}
964
965/// Runs the migrations the database lacks, each in its own transaction with its new version.
966fn migrate(db: &mut Connection) -> Result<(), StoreError> {
967    let found: u32 = db.pragma_query_value(None, "user_version", |row| row.get(0))?;
968    let supported = u32::try_from(MIGRATIONS.len()).unwrap_or(u32::MAX);
969    if found > supported {
970        return Err(StoreError::Newer { found, supported });
971    }
972    for (version, migration) in (1..).zip(MIGRATIONS).skip(found as usize) {
973        let transaction = db.transaction()?;
974        transaction.execute_batch(migration)?;
975        transaction.pragma_update(None, "user_version", version)?;
976        transaction.commit()?;
977    }
978    Ok(())
979}
980
981/// Logs a failed write; the writer goes on with the next one.
982fn logged<T>(what: &str, result: rusqlite::Result<T>) -> Option<T> {
983    result
984        .map_err(|error| warn!("job history: cannot {what}: {error}"))
985        .ok()
986}
987
988#[cfg(test)]
989mod tests;