Skip to main content

artifactize_core/store/
history.rs

1//! The key history: every completed GREEN/RED execution of a reuse key whose history columns
2//! (`completed_at`, `bytes`, `last_used`) are set. The latest by completion time is the one
3//! reused; the cache GC and `cache rm` clear the columns and keep the execution.
4
5use std::path::Path;
6
7use rusqlite::{OpenFlags, params};
8use serde::Serialize;
9use tokio_rusqlite::Connection;
10
11use super::{
12    DATABASE, Execution,
13    receipts::{Error, NotifyingConnection, schema_initialized},
14};
15
16/// Bound the history no checkout reuses now: records a newer one of the same eval and checkout
17/// replaced, and the records of checkouts that are gone. Eviction preserves execution audit.
18pub const MAX_ENTRIES: i64 = 10_000;
19/// Bound those records' JSON payload bytes independently of the record-count ceiling.
20pub const MAX_BYTES: i64 = 1024 * 1024 * 1024;
21/// Refuse one oversized reusable record rather than letting it monopolize cache storage.
22pub const MAX_ENTRY_BYTES: usize = 16 * 1024 * 1024;
23
24/// A record of the key history, of executions `e`.
25pub(super) const RECORD: &str =
26    "e.completed_at IS NOT NULL AND e.status IN ('GREEN','RED') AND e.key IS NOT NULL";
27
28/// The latest record of a key first: the newest completion, then the most recently stored.
29pub(super) const LATEST: &str = "e.completed_at DESC, e.rowid DESC";
30
31/// One record of a key's history, or with `cache list` the latest record of each key.
32#[derive(Debug, Serialize)]
33#[serde(rename_all = "camelCase")]
34pub struct Entry {
35    pub key: crate::types::ReuseKey,
36    pub eval_def_hash: crate::types::DefinitionHash,
37    pub execution_id: crate::types::ExecutionId,
38    pub verdict: crate::types::ExecutionStatus,
39    #[serde(with = "crate::platform::path_serde")]
40    pub repo_path: std::path::PathBuf,
41    pub eval_id: crate::types::EvalId,
42    /// The target Artifact's fingerprint.
43    pub fingerprint: Option<crate::types::Fingerprint>,
44    pub completed_at: Option<crate::types::Timestamp>,
45    /// `user@host` of the machine that produced the result.
46    pub producer: Option<crate::types::ProducerId>,
47    /// The selected profile variant, when the result came from one.
48    pub variant: Option<crate::config::ProfileVariantName>,
49    /// The remote store URL of a mirrored record.
50    pub origin: Option<crate::types::StoreUrl>,
51    /// How many records the key holds.
52    pub records: i64,
53    pub bytes: i64,
54    pub last_used: crate::types::Timestamp,
55}
56
57async fn open(state: &Path, writable: bool) -> Result<Option<NotifyingConnection>, String> {
58    let state = crate::workspace::canonical_target(state).map_err(|e| e.to_string())?;
59    super::receipts::check_files(&state)?;
60    let path = state.join(DATABASE);
61    if !path.try_exists().map_err(|e| e.to_string())? {
62        return Ok(None);
63    }
64    let connection = Connection::open_with_flags(
65        path,
66        if writable {
67            OpenFlags::SQLITE_OPEN_READ_WRITE
68        } else {
69            OpenFlags::SQLITE_OPEN_READ_ONLY
70        },
71    )
72    .await
73    .map_err(|e| e.to_string())?;
74    let connection = NotifyingConnection::new(connection, &state);
75    let initialized = connection
76        .call(|db| -> Result<bool, Error> {
77            super::wait_when_busy(db)?;
78            let transaction = db.transaction()?;
79            schema_initialized(&transaction)
80        })
81        .await
82        .map_err(|e| e.to_string());
83    match initialized {
84        Ok(true) => Ok(Some(connection)),
85        result => connection.finish(result.map(|_| None)).await,
86    }
87}
88
89/// The latest record of every key, or with `history` every record, latest first per key.
90pub async fn list(state: &Path, history: bool) -> Result<Vec<Entry>, String> {
91    let Some(connection) = open(state, false).await? else {
92        return Ok(Vec::new());
93    };
94    let result = connection
95        .call(move |db| -> Result<_, Error> {
96            let mut statement = db.prepare(&format!(
97                "SELECT * FROM (SELECT e.key,e.eval_def_hash,e.id,e.status,json_extract(e.data,'$.provenance.repoPath'),json_extract(e.data,'$.provenance.evalId'),json_extract(e.data,'$.fingerprint'),json_extract(e.data,'$.completedAt'),json_extract(e.data,'$.origin.store'),count(*) OVER (PARTITION BY e.key),e.bytes,e.last_used,row_number() OVER (PARTITION BY e.key ORDER BY {LATEST}) AS rank,e.data FROM executions e WHERE {RECORD}) WHERE ?1 OR rank=1 ORDER BY 1,rank"
98            ))?;
99            Ok(statement
100                .query_map([history], |row| {
101                    let id: crate::types::ExecutionId = row.get(2)?;
102                    let data: String = row.get(13)?;
103                    let Some(execution) =
104                        super::unreadable::evidence::<Execution>("execution", &data)
105                    else {
106                        return Ok(None);
107                    };
108                    Ok(Some(Entry {
109                        key: row.get(0)?,
110                        eval_def_hash: row.get(1)?,
111                        execution_id: id,
112                        verdict: row.get(3)?,
113                        repo_path: row.get::<_, String>(4)?.into(),
114                        eval_id: row.get(5)?,
115                        fingerprint: row.get(6)?,
116                        completed_at: row.get(7)?,
117                        producer: execution.producer.map(|producer| producer.name),
118                        variant: execution.options.variant,
119                        origin: row.get(8)?,
120                        records: row.get(9)?,
121                        bytes: row.get(10)?,
122                        last_used: row.get(11)?,
123                    }))
124                })?
125                .filter_map(|row| row.transpose())
126                .collect::<Result<_, _>>()?)
127        })
128        .await
129        .map_err(|e| e.to_string());
130    connection.finish(result).await
131}
132
133/// A key's records, latest first; without `history` only the latest.
134pub async fn show(state: &Path, key: &str, history: bool) -> Result<Vec<Execution>, String> {
135    let Some(connection) = open(state, false).await? else {
136        return Ok(Vec::new());
137    };
138    let Ok(key) = key.parse::<crate::types::ReuseKey>() else {
139        return connection.finish(Ok(Vec::new())).await;
140    };
141    let result = connection
142        .call(move |db| -> Result<_, Error> {
143            let mut statement = db.prepare(&format!(
144                "SELECT e.data FROM executions e WHERE e.key=? AND {RECORD} ORDER BY {LATEST} LIMIT ?"
145            ))?;
146            statement
147                .query_map(params![key, if history { -1 } else { 1 }], |row| {
148                    row.get::<_, String>(0)
149                })?
150                .filter_map(|row| match row {
151                    Ok(data) => super::unreadable::evidence("execution", &data).map(Ok),
152                    Err(error) => Some(Err(Error::Sql(error))),
153                })
154                .collect()
155        })
156        .await
157        .map_err(|e| e.to_string());
158    connection.finish(result).await
159}
160
161/// The latest locally produced record (never a mirror) of each key after `after`, in key order.
162pub(crate) async fn local(
163    state: &Path,
164    after: Option<crate::types::ReuseKey>,
165    limit: usize,
166) -> Result<Vec<(crate::types::ReuseKey, Execution)>, String> {
167    let Some(connection) = open(state, false).await? else {
168        return Ok(Vec::new());
169    };
170    let result = connection
171        .call(move |db| -> Result<_, Error> {
172            let mut statement = db.prepare(&format!(
173                "SELECT key,data FROM (SELECT e.key,e.data,row_number() OVER (PARTITION BY e.key ORDER BY {LATEST}) AS rank FROM executions e WHERE {RECORD} AND json_extract(e.data,'$.origin') IS NULL AND e.key>?) WHERE rank=1 ORDER BY key LIMIT ?"
174            ))?;
175            let mut rows = statement.query(params![
176                after.as_ref().map_or("", crate::types::ReuseKey::as_str),
177                limit as i64
178            ])?;
179            let mut page = Vec::new();
180            while let Some(row) = rows.next()? {
181                let key: crate::types::ReuseKey = row.get(0)?;
182                let data: String = row.get(1)?;
183                let execution: Execution = super::unreadable::decode("execution", key.as_str(), &data)
184                    .map_err(Error::Unreadable)?;
185                if execution.key.as_ref() != Some(&key) {
186                    return Err(Error::Invalid(format!("Cached execution {} does not have its indexed reuse key {key}.", execution.id)));
187                }
188                page.push((key, execution));
189            }
190            Ok(page)
191        })
192        .await
193        .map_err(|e| e.to_string());
194    connection.finish(result).await
195}
196
197/// Remove every record of an unused key from its history, preserving executions and Runs.
198pub async fn remove(state: &Path, key: &str) -> Result<bool, String> {
199    let Some(connection) = open(state, true).await? else {
200        return Ok(false);
201    };
202    let Ok(key) = key.parse::<crate::types::ReuseKey>() else {
203        return connection.finish(Ok(false)).await;
204    };
205    let result = connection
206        .call(move |db| -> Result<bool, Error> {
207            let transaction =
208                db.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
209            let in_use: bool = transaction.query_row(
210                "SELECT EXISTS(SELECT 1 FROM executions WHERE key=?1 AND status IN ('RUNNING','WAITING_HUMAN')) OR EXISTS(SELECT 1 FROM requests q JOIN executions e ON e.id=q.execution_id WHERE e.key=?1 AND e.completed_at IS NOT NULL AND q.status IN ('QUEUED','RUNNING','WAITING_HUMAN'))",
211                [&key],
212                |row| row.get(0),
213            )?;
214            if in_use {
215                return Err(Error::Invalid(format!(
216                    "Key {key} is in use by an active execution or waiter."
217                )));
218            }
219            let removed = transaction.execute(
220                "UPDATE executions SET completed_at=NULL,bytes=NULL,last_used=NULL WHERE key=? AND completed_at IS NOT NULL",
221                [&key],
222            )?;
223            transaction.commit()?;
224            Ok(removed != 0)
225        })
226        .await
227        .map_err(|e| e.to_string());
228    connection.finish(result).await
229}
230
231/// The history columns of a completed record: its sortable completion time and size, or
232/// `None` when it does not join its key's history (no key, no verdict, or over the entry
233/// limit).
234pub(super) fn columns(
235    execution: &Execution,
236    bytes: usize,
237) -> Result<Option<(crate::types::Timestamp, i64)>, Error> {
238    let (Some(_), Some(_), Some(completed_at)) = (
239        &execution.key,
240        execution.verdict(),
241        &execution.completed_at(),
242    ) else {
243        return Ok(None);
244    };
245    if bytes > MAX_ENTRY_BYTES {
246        return Ok(None);
247    }
248    Ok(Some((*completed_at, bytes as i64)))
249}
250
251/// Mark a reused record as used now.
252pub(super) fn touch(
253    db: &rusqlite::Connection,
254    execution_id: &crate::types::ExecutionId,
255    at: crate::types::Timestamp,
256) -> Result<(), Error> {
257    db.execute(
258        "UPDATE executions SET last_used=? WHERE id=? AND completed_at IS NOT NULL",
259        params![at, execution_id],
260    )?;
261    Ok(())
262}
263
264/// The caps [`collect`] keeps the history no checkout reuses now within.
265#[derive(Debug, Clone, Copy)]
266struct Caps {
267    entries: i64,
268    bytes: i64,
269}
270
271const CAPS: Caps = Caps {
272    entries: MAX_ENTRIES,
273    bytes: MAX_BYTES,
274};
275
276/// Collect the key history once a Run has finished. The newest record of each eval in each
277/// checkout that still exists is what that checkout's next verify reuses, so it stays whatever
278/// the caps: one large project evicts neither another project's results nor its own. The
279/// other records stay within [`MAX_ENTRIES`] and [`MAX_BYTES`], the least recently used
280/// leaving first and Human sign-offs last. Records of a key with an active execution, and
281/// records an in-flight request waits for, stay as well.
282pub(crate) async fn collect(state: &Path) -> Result<(), String> {
283    let Some(connection) = open(state, true).await? else {
284        return Ok(());
285    };
286    let result = connection
287        .call(|db| collect_within(db, CAPS))
288        .await
289        .map_err(|e| e.to_string());
290    connection.finish(result).await
291}
292
293/// A record as collection weighs it.
294struct Record {
295    id: crate::types::ExecutionId,
296    checkout: Option<std::path::PathBuf>,
297    /// The eval it reviewed; a saved id that no longer reads is no eval's latest record.
298    eval: Option<crate::types::EvalId>,
299    human: bool,
300    bytes: i64,
301    last_used: crate::types::Timestamp,
302    held: bool,
303}
304
305fn collect_within(db: &mut rusqlite::Connection, caps: Caps) -> Result<(), Error> {
306    let transaction = db.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
307    // Latest first: the first record seen of an eval in a checkout is the one it reuses.
308    let records = transaction
309        .prepare(&format!(
310            "SELECT e.id,json_extract(e.data,'$.provenance.repoPath'),json_extract(e.data,'$.provenance.evalId'),
311             coalesce(json_extract(e.data,'$.profile.kind')='human',0),e.bytes,e.last_used,
312             EXISTS(SELECT 1 FROM executions a WHERE a.key=e.key AND a.status IN ('RUNNING','WAITING_HUMAN'))
313             OR EXISTS(SELECT 1 FROM requests q WHERE q.execution_id=e.id AND q.status IN ('QUEUED','RUNNING','WAITING_HUMAN'))
314             FROM executions e WHERE {RECORD} ORDER BY {LATEST}"
315        ))?
316        .query_map([], |row| {
317            Ok(Record {
318                id: row.get(0)?,
319                checkout: row.get::<_, Option<String>>(1)?.map(Into::into),
320                eval: row
321                    .get::<_, Option<String>>(2)?
322                    .and_then(|eval| eval.parse().ok()),
323                human: row.get(3)?,
324                bytes: row.get(4)?,
325                last_used: row.get(5)?,
326                held: row.get(6)?,
327            })
328        })?
329        .collect::<Result<Vec<_>, _>>()?;
330    let present: std::collections::BTreeSet<_> = records
331        .iter()
332        .filter_map(|record| record.checkout.as_deref())
333        .collect::<std::collections::BTreeSet<_>>()
334        .into_iter()
335        .filter(|checkout| checkout.is_dir())
336        .collect();
337    {
338        // Execution and receipt rows stay; only their place in the history goes.
339        let mut evict = transaction.prepare(
340            "UPDATE executions SET completed_at=NULL,bytes=NULL,last_used=NULL WHERE id=?",
341        )?;
342        for id in eviction_candidates(&records, &present, caps) {
343            evict.execute([id])?;
344        }
345    }
346    Ok(transaction.commit()?)
347}
348
349fn eviction_candidates<'a>(
350    records: &'a [Record],
351    present: &std::collections::BTreeSet<&Path>,
352    caps: Caps,
353) -> Vec<&'a crate::types::ExecutionId> {
354    let mut reused = std::collections::BTreeSet::new();
355    let mut history = Vec::new();
356    let mut evicted = Vec::new();
357    for record in records {
358        let newest = match (&record.checkout, &record.eval) {
359            (Some(checkout), Some(eval)) => {
360                reused.insert((checkout, eval)) && present.contains(checkout.as_path())
361            }
362            _ => false,
363        };
364        if record.held {
365            continue;
366        }
367        if record.bytes > MAX_ENTRY_BYTES as i64 {
368            evicted.push(&record.id);
369        } else if !newest {
370            history.push(record);
371        }
372    }
373    // Human sign-offs leave last; among the rest the least recently used leave first.
374    history.sort_by(|a, b| {
375        b.human
376            .cmp(&a.human)
377            .then_with(|| b.last_used.cmp(&a.last_used))
378    });
379    let (mut entries, mut bytes) = (0, 0);
380    for record in history {
381        if entries < caps.entries && bytes + record.bytes <= caps.bytes {
382            entries += 1;
383            bytes += record.bytes;
384        } else {
385            evicted.push(&record.id);
386        }
387    }
388    evicted
389}
390
391#[cfg(test)]
392mod tests;