buildl_core/ports/remote_cache.rs
1//! The port through which a build asks a remote cache for a result, stores one there, and asks
2//! it to run an action.
3//!
4//! Its own file because a remote cache is a different kind of store from the local one: it is
5//! keyed by a description of the work rather than by buildl's own key, it answers with a result
6//! that has not been checked, and it is reached over a network by several workers at once.
7//!
8//! Responsibilities: [`RemoteCache`] and [`BlobSource`].
9//!
10//! Non-responsibilities: implementing them, and judging what a remote answers. The first belongs
11//! to an adapter crate; the second to the phase that executes, which checks every answer before
12//! it uses one. An adapter only moves and encodes.
13//!
14//! This file holds only trait definitions, so it carries no logic to unit-test.
15
16use crate::error::Result;
17use crate::types::{
18 Blob, DecodedTree, Digest, Executed, RemoteAction, RemoteHit, RemoteUpload, Uploaded,
19};
20
21/// A cache of results shared between machines, asked and filled by the action that made them,
22/// and able to run an action itself.
23///
24/// Blocking, and called from several worker threads at once. Every failure is an
25/// [`Error::Adapter`](crate::Error::Adapter) naming
26/// [`PortName::RemoteCache`](crate::PortName::RemoteCache); a miss is not a failure.
27pub trait RemoteCache: Send + Sync {
28 /// The result the remote holds for `action`, as it was stored and not yet checked; `None`
29 /// when it holds none.
30 ///
31 /// # Errors
32 ///
33 /// Returns an error when the remote cannot be reached or answers in a way this port cannot
34 /// read.
35 fn lookup(&self, action: &RemoteAction) -> Result<Option<RemoteHit>>;
36
37 /// The bytes of each of `blobs`, in the same order.
38 ///
39 /// # Errors
40 ///
41 /// Returns an error when the remote cannot be reached, or does not hold one of them.
42 fn download(&self, blobs: &[Blob]) -> Result<Vec<Vec<u8>>>;
43
44 /// What a directory tree the remote stores holds: its files in path order, or the first
45 /// thing in it a build cannot place, a symlink or an empty directory, named by its path
46 /// below the tree's root.
47 ///
48 /// # Errors
49 ///
50 /// Returns an error when the port cannot decode at all; bytes it cannot read as a tree are
51 /// [`DecodedTree::Unreadable`].
52 fn decode_tree(&self, bytes: &[u8]) -> Result<DecodedTree>;
53
54 /// Stores what a run produced as the result for `action`, with the input files and the
55 /// output and log blobs it names. The output and log blobs are read from `blobs`.
56 ///
57 /// # Errors
58 ///
59 /// Returns an error when the remote cannot be reached or refuses what it is sent.
60 fn upload(
61 &self,
62 action: &RemoteAction,
63 produced: &RemoteUpload,
64 blobs: &dyn BlobSource,
65 ) -> Result<Uploaded>;
66
67 /// Asks the remote to run `action`, and returns how it ended: the result, as it was
68 /// answered and not yet checked, or the reason there is none.
69 ///
70 /// # Errors
71 ///
72 /// Returns an error when the remote cannot be reached, does not run actions, or answers in
73 /// a way this port cannot read.
74 fn execute(&self, action: &RemoteAction) -> Result<Executed>;
75}
76
77/// Where the bytes of the blobs an upload names are read from.
78///
79/// It lets a remote adapter read what the local content store holds without importing the store.
80pub trait BlobSource: Sync {
81 /// The bytes held under `digest`; `None` when none are held.
82 ///
83 /// # Errors
84 ///
85 /// Returns an error when the store cannot be read.
86 fn read(&self, digest: &Digest) -> Result<Option<Vec<u8>>>;
87}