kache 0.11.0-rc.1

Zero-copy, content-addressed build cache for Rust, C/C++ and more. No copies, no wasted disk — just hardlinks locally and S3 for sharing.
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
441
442
443
444
445
446
447
//! Transport abstraction for the remote cache.
//!
//! The remote layout ([`crate::remote_layout`]) and the manifest/shard sync
//! ([`crate::remote`]) speak in opaque byte objects addressed by key. This
//! module is the seam between that key/bytes vocabulary and whatever actually
//! stores the bytes — today S3, next a plain shared folder (#414).
//!
//! Everything above the trait (pack framing, zstd, blake3 validation, key
//! naming) is transport-independent and stays shared.

use anyhow::{Context, Result};
use async_trait::async_trait;
use aws_sdk_s3::error::ProvideErrorMetadata;
use aws_sdk_s3::operation::head_object::HeadObjectError;
use bytes::Bytes;
use std::sync::Arc;

use crate::config::RemoteConfig;

/// A fetched object plus the timing split callers report as transfer telemetry.
#[derive(Debug)]
pub struct GetObject {
    /// `Bytes` so a restore doesn't copy the whole pack a second time.
    pub body: Bytes,
    /// Time to response headers, ms.
    pub request_ms: u64,
    /// Time spent reading the body, ms.
    pub body_ms: u64,
}

/// Byte-object transport backing the remote cache.
///
/// Absence is not an error: `head` answers `false` and `get` answers `None`, so
/// callers can take a clean miss path without inspecting transport-specific
/// error codes.
#[async_trait]
pub trait RemoteBackend: Send + Sync {
    /// Whether `key` exists.
    async fn head(&self, key: &str) -> Result<bool>;

    /// Fetch `key`, or `None` when it is absent.
    ///
    /// `max_bytes` checks the object's *advertised* size before the body is
    /// buffered. A remote that advertises no size is not bounded.
    async fn get(&self, key: &str, max_bytes: Option<u64>) -> Result<Option<GetObject>>;

    /// Store `body` at `key`.
    async fn put(&self, key: &str, body: Vec<u8>, content_type: Option<&str>) -> Result<()>;

    /// Keys under `prefix`.
    async fn list(&self, prefix: &str) -> Result<Vec<String>>;

    /// Where `key` lives, for logs and errors (e.g. `s3://bucket/key`).
    fn describe(&self, key: &str) -> String;
}

/// S3 (and S3-compatible: MinIO, Ceph RGW, R2) transport.
pub struct S3Backend {
    client: aws_sdk_s3::Client,
    bucket: String,
}

impl S3Backend {
    pub fn new(client: aws_sdk_s3::Client, bucket: String) -> Self {
        Self { client, bucket }
    }
}

/// True when a GetObject failure means "no such object" rather than a
/// transport/service error.
///
/// A structured code is authoritative: `NoSuchBucket` is also a 404, and
/// treating it as absence would turn a misconfigured bucket into a cache miss.
/// The raw status is consulted only when there is no code, because some S3
/// clones answer a missing key with a bare 404 and no XML error body.
fn is_missing_get_object(
    err: &aws_sdk_s3::error::SdkError<
        aws_sdk_s3::operation::get_object::GetObjectError,
        aws_smithy_runtime_api::client::orchestrator::HttpResponse,
    >,
) -> bool {
    match err.as_service_error() {
        Some(se) if se.code().is_some() || se.is_no_such_key() => {
            se.is_no_such_key()
                || matches!(
                    se.code(),
                    Some("NotFound" | "NoSuchKey" | "404" | "NoSuchObject")
                )
        }
        _ => err
            .raw_response()
            .is_some_and(|r| r.status().as_u16() == 404),
    }
}

fn is_missing_head_object(err: &HeadObjectError) -> bool {
    err.is_not_found()
        || matches!(
            err.code(),
            Some("NotFound" | "NoSuchKey" | "404" | "NoSuchObject")
        )
}

fn describe_head_object_error(err: &HeadObjectError, http_status: Option<u16>) -> String {
    let mut details = Vec::new();

    if let Some(status) = http_status {
        details.push(format!("HTTP {status}"));
    }
    if let Some(code) = err.code() {
        details.push(format!("code={code}"));
    }
    if let Some(message) = err.message() {
        details.push(format!("message={message}"));
    }
    // Fallback: if no structured info, include Display output
    if details.is_empty() || (http_status.is_none() && err.code().is_none()) {
        details.push(err.to_string());
    }

    details.join(", ")
}

#[async_trait]
impl RemoteBackend for S3Backend {
    async fn head(&self, key: &str) -> Result<bool> {
        match self
            .client
            .head_object()
            .bucket(&self.bucket)
            .key(key)
            .send()
            .await
        {
            Ok(_) => Ok(true),
            Err(e) => {
                let http_status = e.raw_response().map(|r| r.status().as_u16());
                let err = e.into_service_error();
                if is_missing_head_object(&err) {
                    Ok(false)
                } else {
                    Err(anyhow::anyhow!(
                        "S3 head_object error for {}: {}",
                        self.describe(key),
                        describe_head_object_error(&err, http_status)
                    ))
                }
            }
        }
    }

    async fn get(&self, key: &str, max_bytes: Option<u64>) -> Result<Option<GetObject>> {
        let request_start = std::time::Instant::now();
        let resp = match self
            .client
            .get_object()
            .bucket(&self.bucket)
            .key(key)
            .send()
            .await
        {
            Ok(resp) => resp,
            Err(e) if is_missing_get_object(&e) => return Ok(None),
            Err(e) => {
                return Err(anyhow::Error::new(e))
                    .with_context(|| format!("GET {}", self.describe(key)));
            }
        };
        let request_ms = request_start.elapsed().as_millis() as u64;

        // Refuse before collecting: the point of the cap is to not buffer the
        // body at all.
        if let Some(max) = max_bytes
            && let Some(len) = resp.content_length()
            && len as u64 > max
        {
            anyhow::bail!("{} too large: {len} bytes (max {max})", self.describe(key));
        }

        let body_start = std::time::Instant::now();
        let body = resp
            .body
            .collect()
            .await
            .with_context(|| format!("reading body of {}", self.describe(key)))?;
        let body_ms = body_start.elapsed().as_millis() as u64;

        Ok(Some(GetObject {
            body: body.into_bytes(),
            request_ms,
            body_ms,
        }))
    }

    async fn put(&self, key: &str, body: Vec<u8>, content_type: Option<&str>) -> Result<()> {
        let mut req = self
            .client
            .put_object()
            .bucket(&self.bucket)
            .key(key)
            .body(body.into());

        if let Some(ct) = content_type {
            req = req.content_type(ct);
        }

        req.send()
            .await
            .with_context(|| format!("PUT {}", self.describe(key)))?;

        Ok(())
    }

    async fn list(&self, prefix: &str) -> Result<Vec<String>> {
        let mut keys = Vec::new();
        let mut continuation_token: Option<String> = None;

        loop {
            let mut req = self
                .client
                .list_objects_v2()
                .bucket(&self.bucket)
                .prefix(prefix);

            if let Some(token) = &continuation_token {
                req = req.continuation_token(token);
            }

            let resp = req
                .send()
                .await
                .with_context(|| format!("listing s3://{}/{prefix}", self.bucket))?;

            keys.extend(
                resp.contents()
                    .iter()
                    .filter_map(|o| o.key())
                    .map(String::from),
            );

            // A truncated response must carry a continuation token. Some
            // non-AWS S3 implementations (MinIO/Ceph RGW/R2/proxies) have
            // emitted is_truncated=true with a missing/empty token; treat that
            // as end-of-pagination rather than looping forever re-listing page 1.
            match resp.next_continuation_token() {
                Some(token) if resp.is_truncated == Some(true) && !token.is_empty() => {
                    continuation_token = Some(token.to_string());
                }
                _ => break,
            }
        }

        Ok(keys)
    }

    fn describe(&self, key: &str) -> String {
        format!("s3://{}/{}", self.bucket, key)
    }
}

/// Build the backend named by `remote`.
///
/// `Arc` rather than `Box`: the prefetch path fans shard downloads out across
/// `tokio::spawn`, which needs an owned `'static` handle per task.
pub async fn create_backend(
    remote: &RemoteConfig,
    pool_idle_secs: u64,
) -> Result<Arc<dyn RemoteBackend>> {
    let client = crate::remote::create_s3_client(remote, pool_idle_secs).await?;
    Ok(Arc::new(S3Backend::new(client, remote.bucket.clone())))
}

#[cfg(test)]
mod tests {
    use super::*;
    use aws_sdk_s3::error::ErrorMetadata;
    use aws_smithy_http_client::test_util::wire::{ReplayedEvent, WireMockServer};

    async fn mock_backend(events: Vec<ReplayedEvent>) -> (WireMockServer, S3Backend) {
        let server = WireMockServer::start(events).await;
        let conf = aws_sdk_s3::config::Builder::new()
            .behavior_version(aws_sdk_s3::config::BehaviorVersion::latest())
            .region(aws_sdk_s3::config::Region::new("us-east-1"))
            .credentials_provider(aws_sdk_s3::config::Credentials::new(
                "AK", "SK", None, None, "test",
            ))
            .endpoint_url(server.endpoint_url())
            .http_client(server.http_client())
            .force_path_style(true)
            .build();
        let backend = S3Backend::new(aws_sdk_s3::Client::from_conf(conf), "bucket".to_string());
        (server, backend)
    }

    #[tokio::test]
    async fn get_under_the_cap_is_allowed() {
        let (_server, backend) = mock_backend(vec![ReplayedEvent::with_body("hello")]).await;

        let fetched = backend
            .get("k", Some(MAX_METADATA_BYTES_TEST))
            .await
            .expect("under-cap GET succeeds")
            .expect("object is present");
        assert_eq!(fetched.body, "hello");
    }

    #[tokio::test]
    async fn get_over_the_cap_is_refused() {
        // The cap is checked against the advertised Content-Length before the
        // body is buffered, so a 5-byte body still trips a 1-byte cap.
        let (_server, backend) = mock_backend(vec![ReplayedEvent::with_body("hello")]).await;

        let err = backend
            .get("k", Some(1))
            .await
            .expect_err("over-cap length must be rejected");
        let msg = err.to_string();
        assert!(msg.contains("too large"), "unexpected message: {msg}");
        assert!(
            msg.contains("s3://bucket/k"),
            "should name the object: {msg}"
        );
    }

    #[tokio::test]
    async fn get_without_a_cap_is_unbounded() {
        let (_server, backend) = mock_backend(vec![ReplayedEvent::with_body("hello")]).await;

        let fetched = backend
            .get("k", None)
            .await
            .expect("uncapped GET succeeds")
            .expect("object is present");
        assert_eq!(fetched.body, "hello");
    }

    #[tokio::test]
    async fn get_absent_object_is_none_not_an_error() {
        let (_server, backend) = mock_backend(vec![ReplayedEvent::status(404)]).await;

        let fetched = backend.get("k", None).await.expect("404 is a clean miss");
        assert!(fetched.is_none());
    }

    #[tokio::test]
    async fn get_at_exactly_the_cap_is_allowed() {
        // The guard is strictly greater, so a body equal to the cap passes.
        let (_server, backend) = mock_backend(vec![ReplayedEvent::with_body("hello")]).await;

        let fetched = backend
            .get("k", Some(5))
            .await
            .expect("at-cap GET succeeds")
            .expect("object is present");
        assert_eq!(fetched.body, "hello");
    }

    /// A 404 carrying a structured S3 error code.
    fn s3_error(code: &str) -> ReplayedEvent {
        ReplayedEvent::HttpResponse {
            status: 404,
            body: format!(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
                 <Error><Code>{code}</Code><Message>test</Message></Error>"
            )
            .into(),
        }
    }

    #[tokio::test]
    async fn get_no_such_key_is_a_miss() {
        let (_server, backend) = mock_backend(vec![s3_error("NoSuchKey")]).await;

        let fetched = backend.get("k", None).await.expect("NoSuchKey is a miss");
        assert!(fetched.is_none());
    }

    #[tokio::test]
    async fn get_no_such_bucket_is_an_error_not_a_miss() {
        // NoSuchBucket is also a 404. Reporting it as absence would turn a
        // misconfigured bucket into an ordinary cache miss.
        let (_server, backend) = mock_backend(vec![s3_error("NoSuchBucket")]).await;

        backend
            .get("k", None)
            .await
            .expect_err("NoSuchBucket must not classify as absence");
    }

    #[tokio::test]
    async fn get_bare_404_without_a_code_is_a_miss() {
        // Some S3 clones answer a missing key with a bare 404 and no XML body.
        let (_server, backend) = mock_backend(vec![ReplayedEvent::status(404)]).await;

        let fetched = backend.get("k", None).await.expect("bare 404 is a miss");
        assert!(fetched.is_none());
    }

    #[tokio::test]
    async fn list_follows_the_continuation_token_across_pages() {
        fn page(key: &str, next: Option<&str>) -> ReplayedEvent {
            let (truncated, token) = match next {
                Some(t) => (
                    "true",
                    format!("<NextContinuationToken>{t}</NextContinuationToken>"),
                ),
                None => ("false", String::new()),
            };
            ReplayedEvent::with_body(format!(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
                 <ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\
                 <Name>bucket</Name><KeyCount>1</KeyCount><MaxKeys>1</MaxKeys>\
                 <IsTruncated>{truncated}</IsTruncated>{token}\
                 <Contents><Key>{key}</Key><Size>10</Size></Contents></ListBucketResult>"
            ))
        }

        let (_server, backend) =
            mock_backend(vec![page("a.json", Some("tok")), page("b.json", None)]).await;

        let keys = backend.list("p/").await.expect("paginated LIST succeeds");
        assert_eq!(keys, vec!["a.json".to_string(), "b.json".to_string()]);
    }

    /// Mirrors `remote::MAX_METADATA_BYTES`; the cap under test is the
    /// backend's `max_bytes` argument, not that specific constant.
    const MAX_METADATA_BYTES_TEST: u64 = 64 * 1024 * 1024;

    #[test]
    fn generic_head_object_not_found_codes_are_treated_as_misses() {
        let err = HeadObjectError::generic(ErrorMetadata::builder().code("NotFound").build());
        assert!(is_missing_head_object(&err));

        let err = HeadObjectError::generic(ErrorMetadata::builder().code("NoSuchKey").build());
        assert!(is_missing_head_object(&err));
    }

    #[test]
    fn generic_head_object_non_miss_code_is_not_treated_as_missing() {
        let err = HeadObjectError::generic(
            ErrorMetadata::builder()
                .code("SignatureDoesNotMatch")
                .build(),
        );
        assert!(!is_missing_head_object(&err));
    }
}