OXYGENHUB
# af7902eab78a oxygenflow / crates / flow / src / images.rs
11.5 KiB · Rust · last changed Jetstream: runners share images peer to peer 24 days ago source blame history raw
1
//! Images by hash with a hub (oxygenflow#39, docs/jetstream.md §8):
2
//! `flow rootfs push` sends an image's missing chunks and its manifest;
3
//! `flow rootfs pull`, and a runner that lacks a job's image, fetch the
4
//! manifest and the chunks they lack and write the image back, checked
5
//! against its hash. Fetched chunks stay in the home's chunk cache
6
//! (`chunks/`), so a rebuilt image costs only the chunks that changed.
7
8
use crate::home::Home;
9
use anyhow::{bail, Context, Result};
10
use jet::images::{self, Chunk, Manifest};
11
use std::collections::HashSet;
12
use std::path::{Path, PathBuf};
13
use std::sync::Mutex;
14
use std::time::{Duration, Instant};
15
16
/// Requests in flight at once.
17
const PARALLEL: usize = 8;
18
19
/// A hub's image endpoints, with a token.
20
pub struct Client {
21
    root: String,
22
    base: String,
23
    token: String,
24
    agent: ureq::Agent,
25
}
26
27
/// What a push or a fetch moved.
28
pub struct Moved {
29
    pub chunks: usize,
30
    pub bytes: u64,
31
    /// Of `bytes`, those that came from peers rather than the hub.
32
    pub peer_bytes: u64,
33
    pub elapsed: Duration,
34
}
35
36
impl Client {
37
    pub fn new(hub: &str, token: &str, ca: Option<&Path>) -> Result<Client> {
38
        let tls = oxy_core::protocol::tls_client_config(ca).map_err(|e| anyhow::anyhow!("{e}"))?;
39
        let agent = ureq::AgentBuilder::new().tls_config(tls).timeout(Duration::from_secs(120)).build();
40
        let root = hub.trim_end_matches('/').to_string();
41
        Ok(Client { base: format!("{root}/api/v1"), root, token: token.into(), agent })
42
    }
43
44
    fn auth(&self) -> String {
45
        format!("Bearer {}", self.token)
46
    }
47
48
    fn fail(url: &str, e: ureq::Error) -> anyhow::Error {
49
        match e {
50
            ureq::Error::Status(code, r) => {
51
                let text = r.into_string().unwrap_or_default();
52
                let detail = serde_json::from_str::<serde_json::Value>(&text)
53
                    .ok()
54
                    .and_then(|v| v["detail"].as_str().map(String::from))
55
                    .unwrap_or(text);
56
                anyhow::anyhow!("{url}: HTTP {code}: {detail}")
57
            }
58
            e => anyhow::anyhow!("{url}: {e}"),
59
        }
60
    }
61
62
    /// The hub's manifest of image `hash`, `None` when it does not hold it.
63
    pub fn manifest(&self, hash: &str) -> Result<Option<Manifest>> {
64
        let url = format!("{}/flow/images/{hash}", self.base);
65
        match self.agent.get(&url).set("Authorization", &self.auth()).call() {
66
            Ok(r) => Ok(Some(r.into_json()?)),
67
            Err(ureq::Error::Status(404, _)) => Ok(None),
68
            Err(e) => Err(Self::fail(&url, e)),
69
        }
70
    }
71
72
    /// The hub's identity (`/.well-known/oxyhub-key`): the key its swarm
73
    /// tickets are signed with.
74
    pub fn hub_identity(&self) -> Result<oxy_core::identity::Identity> {
75
        let url = format!("{}/.well-known/oxyhub-key", self.root);
76
        let r = self.agent.get(&url).call().map_err(|e| Self::fail(&url, e))?;
77
        let mut out = Vec::new();
78
        std::io::Read::read_to_end(&mut r.into_reader(), &mut out)?;
79
        oxy_core::identity::Identity::decode(&out).map_err(|e| anyhow::anyhow!("{url}: not a hub identity: {e}"))
80
    }
81
82
    /// Tell the hub's tracker this runner is fetching image `hash`, or has
83
    /// stopped: the peers to ask and the ticket they admit.
84
    pub fn announce(&self, hash: &str, state: &str) -> Result<flow_core::api::Announced> {
85
        let url = format!("{}/flow/swarm/announce", self.base);
86
        let body = flow_core::api::Announce { image: hash.into(), state: state.into() };
87
        Ok(self.agent.post(&url).set("Authorization", &self.auth()).send_json(&body).map_err(|e| Self::fail(&url, e))?.into_json()?)
88
    }
89
90
    pub(crate) fn chunk(&self, hash: &str) -> Result<Vec<u8>> {
91
        let url = format!("{}/flow/images/chunks/{hash}", self.base);
92
        let r = self.agent.get(&url).set("Authorization", &self.auth()).call().map_err(|e| Self::fail(&url, e))?;
93
        let mut out = Vec::new();
94
        std::io::Read::read_to_end(&mut r.into_reader(), &mut out)?;
95
        Ok(out)
96
    }
97
98
    /// Send image `hash` from `home`: only the chunks the hub lacks, then
99
    /// the manifest, which the hub checks against the hash.
100
    pub fn push(&self, home: &Home, hash: &str) -> Result<Moved> {
101
        let start = Instant::now();
102
        let path = home.rootfs(hash);
103
        if !path.is_file() {
104
            bail!("{} is not in this Flow home ({}); import it first", &hash[..12.min(hash.len())], path.display());
105
        }
106
        let m = images::scan_file(&path, home.image_arch(hash), |_, _| Ok(()))?;
107
        if m.hash != hash {
108
            bail!("{} holds an image named {}, not {}: the file was changed", path.display(), &m.hash[..12], &hash[..12]);
109
        }
110
        let mut missing: HashSet<String> = HashSet::new();
111
        for ask in m.needed().chunks(50_000) {
112
            let url = format!("{}/flow/images/missing", self.base);
113
            let r: serde_json::Value = self
114
                .agent
115
                .post(&url)
116
                .set("Authorization", &self.auth())
117
                .send_json(serde_json::json!({ "chunks": ask }))
118
                .map_err(|e| Self::fail(&url, e))?
119
                .into_json()?;
120
            missing.extend(r["missing"].as_array().into_iter().flatten().filter_map(|h| h.as_str().map(String::from)));
121
        }
122
        // Read the image again, sending the missing chunks as they come.
123
        let (count, bytes) = (missing.len(), Mutex::new(0u64));
124
        let queue: Mutex<Vec<(String, Vec<u8>)>> = Mutex::new(Vec::new());
125
        let failed: Mutex<Option<anyhow::Error>> = Mutex::new(None);
126
        let sent = Mutex::new(HashSet::new());
127
        let send = |c: &str, b: &[u8]| -> Result<()> {
128
            let url = format!("{}/flow/images/chunks/{c}", self.base);
129
            self.agent.put(&url).set("Authorization", &self.auth()).send_bytes(b).map_err(|e| Self::fail(&url, e))?;
130
            *bytes.lock().unwrap() += b.len() as u64;
131
            Ok(())
132
        };
133
        images::scan_file(&path, None, |c: &Chunk, b: &[u8]| {
134
            if missing.contains(c.hash()) && sent.lock().unwrap().insert(c.hash().to_string()) {
135
                queue.lock().unwrap().push((c.hash().to_string(), b.to_vec()));
136
            }
137
            if queue.lock().unwrap().len() >= PARALLEL * 4 {
138
                let batch: Vec<_> = std::mem::take(&mut *queue.lock().unwrap());
139
                send_batch(&batch, &send, &failed);
140
            }
141
            match failed.lock().unwrap().take() {
142
                Some(e) => Err(images::Error::Bad(format!("{e:#}"))),
143
                None => Ok(()),
144
            }
145
        })?;
146
        let rest: Vec<_> = std::mem::take(&mut *queue.lock().unwrap());
147
        send_batch(&rest, &send, &failed);
148
        if let Some(e) = failed.lock().unwrap().take() {
149
            return Err(e);
150
        }
151
        let url = format!("{}/flow/images/{hash}", self.base);
152
        self.agent.put(&url).set("Authorization", &self.auth()).send_json(&m).map_err(|e| Self::fail(&url, e))?;
153
        let bytes = *bytes.lock().unwrap();
154
        Ok(Moved { chunks: count, bytes, peer_bytes: 0, elapsed: start.elapsed() })
155
    }
156
157
    /// Fetch image `hash` into `home`: the chunks its cache lacks, the
158
    /// image written and checked, its architecture recorded. `Ok(None)`
159
    /// when the hub does not hold it.
160
    pub fn fetch(&self, home: &Home, hash: &str) -> Result<Option<Moved>> {
161
        let start = Instant::now();
162
        let Some(m) = self.manifest(hash)? else { return Ok(None) };
163
        if m.hash != hash {
164
            bail!("the hub's manifest for {} names {}", &hash[..12], &m.hash[..12]);
165
        }
166
        let cache = chunk_cache(home);
167
        save_manifest(&home.root, &m)?;
168
        let wanted: Vec<String> = m.needed().into_iter().filter(|h| !cache_path(&cache, h).is_file()).map(String::from).collect();
169
        let (bytes, failed) = (Mutex::new(0u64), Mutex::new(None::<anyhow::Error>));
170
        let next = Mutex::new(wanted.iter());
171
        std::thread::scope(|s| {
172
            for _ in 0..PARALLEL {
173
                s.spawn(|| loop {
174
                    let Some(h) = next.lock().unwrap().next() else { break };
175
                    if failed.lock().unwrap().is_some() {
176
                        break;
177
                    }
178
                    let got = self.chunk(h).and_then(|b| {
179
                        if blake3::hash(&b).to_hex().as_str() != h {
180
                            bail!("chunk {} came back with other bytes", &h[..12]);
181
                        }
182
                        let p = cache_path(&cache, h);
183
                        std::fs::create_dir_all(p.parent().expect("a chunk directory"))?;
184
                        let tmp = p.with_extension("part");
185
                        std::fs::write(&tmp, &b)?;
186
                        std::fs::rename(&tmp, &p)?;
187
                        *bytes.lock().unwrap() += b.len() as u64;
188
                        Ok(())
189
                    });
190
                    if let Err(e) = got {
191
                        *failed.lock().unwrap() = Some(e);
192
                        break;
193
                    }
194
                });
195
            }
196
        });
197
        if let Some(e) = failed.into_inner().unwrap() {
198
            return Err(e);
199
        }
200
        write_image(home, &m)?;
201
        Ok(Some(Moved { chunks: wanted.len(), bytes: bytes.into_inner().unwrap(), peer_bytes: 0, elapsed: start.elapsed() }))
202
    }
203
}
204
205
/// Write image `m` into `home` from the chunk cache, checked against its
206
/// hash, and record its architecture.
207
pub(crate) fn write_image(home: &Home, m: &Manifest) -> Result<()> {
208
    let cache = chunk_cache(home);
209
    let out = home.rootfs(&m.hash);
210
    std::fs::create_dir_all(out.parent().expect("the cas directory"))?;
211
    images::assemble(m, &out, |c| Ok(std::fs::read(cache_path(&cache, c.hash()))?))
212
        .with_context(|| format!("write image {}", &m.hash[..12]))?;
213
    if let Some(arch) = &m.arch {
214
        let manifest = serde_json::json!({ "arch": arch, "imported_ms": std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0), "from": "hub" });
215
        std::fs::write(home.image_manifest(&m.hash), serde_json::to_vec(&manifest)?)?;
216
    }
217
    Ok(())
218
}
219
220
/// Where an image's chunk manifest is kept (`manifests/<hash>.json`), so
221
/// the runner can serve its chunks to peers out of the image itself.
222
pub(crate) fn manifest_path(root: &Path, hash: &str) -> PathBuf {
223
    root.join("manifests").join(format!("{hash}.json"))
224
}
225
226
pub(crate) fn save_manifest(root: &Path, m: &Manifest) -> Result<()> {
227
    let p = manifest_path(root, &m.hash);
228
    std::fs::create_dir_all(p.parent().expect("the manifests directory"))?;
229
    let tmp = p.with_extension("part");
230
    std::fs::write(&tmp, serde_json::to_vec(m)?)?;
231
    std::fs::rename(&tmp, &p)?;
232
    Ok(())
233
}
234
235
fn send_batch(batch: &[(String, Vec<u8>)], send: &(dyn Fn(&str, &[u8]) -> Result<()> + Sync), failed: &Mutex<Option<anyhow::Error>>) {
236
    std::thread::scope(|s| {
237
        for part in batch.chunks(batch.len().div_ceil(PARALLEL).max(1)) {
238
            s.spawn(move || {
239
                for (h, b) in part {
240
                    if let Err(e) = send(h, b) {
241
                        *failed.lock().unwrap() = Some(e);
242
                        return;
243
                    }
244
                }
245
            });
246
        }
247
    });
248
}
249
250
/// The home's chunk cache.
251
pub fn chunk_cache(home: &Home) -> PathBuf {
252
    home.root.join("chunks")
253
}
254
255
pub(crate) fn cache_path(cache: &Path, hash: &str) -> PathBuf {
256
    cache.join(&hash[..2]).join(hash)
257
}
258
259
/// A size for people: 991 MiB, 1.2 GiB.
260
pub fn human(bytes: u64) -> String {
261
    if bytes >= 1 << 30 {
262
        format!("{:.1} GiB", bytes as f64 / (1u64 << 30) as f64)
263
    } else {
264
        format!("{} MiB", bytes >> 20)
265
    }
266
}