openjd-snapshots 0.1.2

[Experimental] Job attachments snapshot library for content-addressed file tree operations. The v2023 on-disk manifest format is stable and used by AWS Deadline Cloud; the v2025 format is an experimental draft.
Documentation
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright by contributors to this project.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

//! Integration tests for cache_sync_manifest across all cache type combinations.

use aws_sdk_s3::config::{Credentials, Region};
use openjd_snapshots::{
    cache_sync_manifest, AsyncDataCache, CacheSyncOptions, FileEntry, FileSystemDataCache,
    HashAlgorithm, Manifest, ManifestRef, RelManifest, S3DataCache,
};
use s3s::auth::SimpleAuth;
use s3s::service::S3ServiceBuilder;
use s3s_fs::FileSystem;
use std::path::Path;
use std::sync::Arc;
use tempfile::TempDir;

const BUCKET: &str = "test-bucket";
const PREFIX: &str = "Data";
const ACCESS_KEY: &str = "AKIAIOSFODNN7EXAMPLE";
const SECRET_KEY: &str = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";

fn test_manifest(files: Vec<FileEntry>) -> RelManifest {
    RelManifest::Snapshot(Manifest::new(HashAlgorithm::Xxh128, -1).with_files(files))
}

fn make_s3_client(tmp: &Path) -> aws_sdk_s3::Client {
    let fs_root = tmp.join("s3");
    std::fs::create_dir_all(&fs_root).unwrap();
    let fs = FileSystem::new(&fs_root).unwrap();
    let service = {
        let mut b = S3ServiceBuilder::new(fs);
        b.set_auth(SimpleAuth::from_single(ACCESS_KEY, SECRET_KEY));
        b.build()
    };
    let http_client = s3s_aws::Client::from(service);
    let cred = Credentials::new(ACCESS_KEY, SECRET_KEY, None, None, "test");
    let config = aws_sdk_s3::Config::builder()
        .behavior_version_latest()
        .credentials_provider(cred)
        .http_client(http_client)
        .region(Region::new("us-west-2"))
        .endpoint_url("http://localhost:0")
        .force_path_style(true)
        .build();
    aws_sdk_s3::Client::from_conf(config)
}

fn make_fs_cache() -> (TempDir, Arc<dyn AsyncDataCache>) {
    let tmp = TempDir::new().unwrap();
    let cache: Arc<dyn AsyncDataCache> =
        Arc::new(FileSystemDataCache::new(tmp.path().join("data")).unwrap());
    (tmp, cache)
}

async fn make_s3_cache() -> (TempDir, Arc<dyn AsyncDataCache>) {
    let tmp = TempDir::new().unwrap();
    let client = make_s3_client(tmp.path());
    client.create_bucket().bucket(BUCKET).send().await.unwrap();
    let cache: Arc<dyn AsyncDataCache> = Arc::new(S3DataCache::new(
        BUCKET.to_string(),
        PREFIX.to_string(),
        client,
    ));
    (tmp, cache)
}

async fn put(cache: &Arc<dyn AsyncDataCache>, hash: &str, alg: &str, data: &[u8]) {
    cache.put_object(hash, alg, data.to_vec()).await.unwrap();
}

async fn exists(cache: &Arc<dyn AsyncDataCache>, hash: &str, alg: &str) -> bool {
    cache.object_exists(hash, alg).await.unwrap_or(false)
}

async fn get(cache: &Arc<dyn AsyncDataCache>, hash: &str, alg: &str) -> Vec<u8> {
    cache.get_object(hash, alg).await.unwrap()
}

async fn run_sync_test(src: Arc<dyn AsyncDataCache>, dst: Arc<dyn AsyncDataCache>) {
    put(&src, "hash_a", "xxh128", b"alpha").await;
    put(&src, "hash_b", "xxh128", b"bravo").await;

    let manifest = test_manifest(vec![
        {
            let mut e = FileEntry::new("a.txt");
            e.hash = Some("hash_a".into());
            e.size = Some(5);
            e
        },
        {
            let mut e = FileEntry::new("b.txt");
            e.hash = Some("hash_b".into());
            e.size = Some(5);
            e
        },
    ]);

    let result = cache_sync_manifest(
        &[&manifest as &dyn ManifestRef],
        src,
        dst.clone(),
        CacheSyncOptions::default(),
    )
    .await
    .unwrap();

    assert_eq!(result.statistics.copied_objects, 2);
    assert_eq!(result.statistics.skipped_objects, 0);
    assert!(exists(&dst, "hash_a", "xxh128").await);
    assert_eq!(get(&dst, "hash_b", "xxh128").await, b"bravo");
}

async fn run_sync_skip_test(src: Arc<dyn AsyncDataCache>, dst: Arc<dyn AsyncDataCache>) {
    put(&src, "hash_a", "xxh128", b"alpha").await;
    put(&src, "hash_b", "xxh128", b"bravo").await;
    put(&dst, "hash_a", "xxh128", b"alpha").await; // pre-populate

    let manifest = test_manifest(vec![
        {
            let mut e = FileEntry::new("a.txt");
            e.hash = Some("hash_a".into());
            e.size = Some(5);
            e
        },
        {
            let mut e = FileEntry::new("b.txt");
            e.hash = Some("hash_b".into());
            e.size = Some(5);
            e
        },
    ]);

    let result = cache_sync_manifest(
        &[&manifest as &dyn ManifestRef],
        src,
        dst,
        CacheSyncOptions::default(),
    )
    .await
    .unwrap();

    assert_eq!(result.statistics.copied_objects, 1);
    assert_eq!(result.statistics.skipped_objects, 1);
}

// ===== FS → FS =====

#[tokio::test]
async fn cache_sync_fs_to_fs() {
    let (_sd, src) = make_fs_cache();
    let (_dd, dst) = make_fs_cache();
    run_sync_test(src, dst).await;
}

#[tokio::test]
async fn cache_sync_fs_to_fs_skip_existing() {
    let (_sd, src) = make_fs_cache();
    let (_dd, dst) = make_fs_cache();
    run_sync_skip_test(src, dst).await;
}

// ===== FS → S3 =====

#[tokio::test]
async fn cache_sync_fs_to_s3() {
    let (_sd, src) = make_fs_cache();
    let (_s3d, dst) = make_s3_cache().await;
    run_sync_test(src, dst).await;
}

#[tokio::test]
async fn cache_sync_fs_to_s3_skip_existing() {
    let (_sd, src) = make_fs_cache();
    let (_s3d, dst) = make_s3_cache().await;
    run_sync_skip_test(src, dst).await;
}

// ===== S3 → FS =====

#[tokio::test]
async fn cache_sync_s3_to_fs() {
    let (_s3d, src) = make_s3_cache().await;
    let (_dd, dst) = make_fs_cache();
    run_sync_test(src, dst).await;
}

#[tokio::test]
async fn cache_sync_s3_to_fs_skip_existing() {
    let (_s3d, src) = make_s3_cache().await;
    let (_dd, dst) = make_fs_cache();
    run_sync_skip_test(src, dst).await;
}

// ===== S3 → S3 =====

#[tokio::test]
async fn cache_sync_s3_to_s3() {
    let (_s3d, src) = make_s3_cache().await;
    let (_s3d2, dst) = make_s3_cache().await;
    run_sync_test(src, dst).await;
}

#[tokio::test]
async fn cache_sync_s3_to_s3_skip_existing() {
    let (_s3d, src) = make_s3_cache().await;
    let (_s3d2, dst) = make_s3_cache().await;
    run_sync_skip_test(src, dst).await;
}

// ===== Cancellation =====

#[tokio::test]
async fn cache_sync_cancellation_via_progress() {
    let (_sd, src) = make_fs_cache();
    let (_dd, dst) = make_fs_cache();

    // Put many objects so cancellation has a chance to fire
    for i in 0..20 {
        put(&src, &format!("hash_{i}"), "xxh128", b"data").await;
    }

    let manifest = test_manifest(
        (0..20)
            .map(|i| {
                let mut e = FileEntry::new(format!("f{i}.txt"));
                e.hash = Some(format!("hash_{i}"));
                e.size = Some(4);
                e
            })
            .collect(),
    );

    let call_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let cc = call_count.clone();

    let result = cache_sync_manifest(
        &[&manifest as &dyn ManifestRef],
        src,
        dst,
        CacheSyncOptions {
            on_progress: Some(Box::new(move |_stats| {
                // Cancel after 2 progress callbacks
                cc.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 2
            })),
            ..Default::default()
        },
    )
    .await;

    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(
        err.to_string().contains("cancelled"),
        "expected Cancelled, got: {err}"
    );
    assert!(
        call_count.load(std::sync::atomic::Ordering::Relaxed) >= 2,
        "progress should have been called at least twice"
    );
}

// ===== Progress callbacks =====

#[tokio::test]
async fn cache_sync_progress_reports_statistics() {
    let (_sd, src) = make_fs_cache();
    let (_dd, dst) = make_fs_cache();

    put(&src, "hash_a", "xxh128", b"alpha").await;
    put(&src, "hash_b", "xxh128", b"bravo").await;

    let manifest = test_manifest(vec![
        {
            let mut e = FileEntry::new("a.txt");
            e.hash = Some("hash_a".into());
            e.size = Some(5);
            e
        },
        {
            let mut e = FileEntry::new("b.txt");
            e.hash = Some("hash_b".into());
            e.size = Some(5);
            e
        },
    ]);

    let final_stats = Arc::new(std::sync::Mutex::new(None));
    let fs = final_stats.clone();

    let result = cache_sync_manifest(
        &[&manifest as &dyn ManifestRef],
        src,
        dst,
        CacheSyncOptions {
            on_progress: Some(Box::new(move |stats| {
                *fs.lock().unwrap() = Some(stats.clone());
                true
            })),
            ..Default::default()
        },
    )
    .await
    .unwrap();

    assert_eq!(result.statistics.total_objects, 2);
    assert_eq!(result.statistics.copied_objects, 2);

    // Progress callback should have been invoked with meaningful stats
    let last = final_stats.lock().unwrap().clone().unwrap();
    assert!(last.copied_objects > 0 || last.skipped_objects > 0);
}

// ===== Error: source object missing =====

#[tokio::test]
async fn cache_sync_missing_source_object_errors() {
    let (_sd, src) = make_fs_cache();
    let (_dd, dst) = make_fs_cache();

    // Don't put anything in source — manifest references a hash that doesn't exist
    let manifest = test_manifest(vec![{
        let mut e = FileEntry::new("missing.txt");
        e.hash = Some("nonexistent_hash".into());
        e.size = Some(100);
        e
    }]);

    let result = cache_sync_manifest(
        &[&manifest as &dyn ManifestRef],
        src,
        dst,
        CacheSyncOptions::default(),
    )
    .await;

    assert!(
        result.is_err(),
        "should error when source object is missing"
    );
}

// ===== Mixed manifest types =====

#[tokio::test]
async fn cache_sync_multiple_manifests_deduplicates() {
    let (_sd, src) = make_fs_cache();
    let (_dd, dst) = make_fs_cache();

    put(&src, "shared", "xxh128", b"shared_data").await;
    put(&src, "unique_a", "xxh128", b"aaa").await;
    put(&src, "unique_b", "xxh128", b"bbb").await;

    let manifest1 = test_manifest(vec![
        {
            let mut e = FileEntry::new("shared.txt");
            e.hash = Some("shared".into());
            e.size = Some(11);
            e
        },
        {
            let mut e = FileEntry::new("a.txt");
            e.hash = Some("unique_a".into());
            e.size = Some(3);
            e
        },
    ]);
    let manifest2 = test_manifest(vec![
        {
            let mut e = FileEntry::new("shared_copy.txt");
            e.hash = Some("shared".into()); // same hash as manifest1
            e.size = Some(11);
            e
        },
        {
            let mut e = FileEntry::new("b.txt");
            e.hash = Some("unique_b".into());
            e.size = Some(3);
            e
        },
    ]);

    let result = cache_sync_manifest(
        &[
            &manifest1 as &dyn ManifestRef,
            &manifest2 as &dyn ManifestRef,
        ],
        src,
        dst.clone(),
        CacheSyncOptions::default(),
    )
    .await
    .unwrap();

    // "shared" hash appears in both manifests but should only be copied once
    assert_eq!(result.statistics.copied_objects, 3); // shared + unique_a + unique_b
    assert!(exists(&dst, "shared", "xxh128").await);
    assert!(exists(&dst, "unique_a", "xxh128").await);
    assert!(exists(&dst, "unique_b", "xxh128").await);
}

// ===== Skips symlinks, deleted, unhashed =====

#[tokio::test]
async fn cache_sync_skips_non_syncable_entries() {
    let (_sd, src) = make_fs_cache();
    let (_dd, dst) = make_fs_cache();

    put(&src, "real_hash", "xxh128", b"real").await;

    let manifest = test_manifest(vec![
        {
            // Hashed file — should sync
            let mut e = FileEntry::new("real.txt");
            e.hash = Some("real_hash".into());
            e.size = Some(4);
            e
        },
        // Symlink — should skip
        FileEntry::symlink("link.txt", "real.txt"),
        // Unhashed file — should skip
        FileEntry::file("unhashed.txt", 100, 1),
    ]);

    let result = cache_sync_manifest(
        &[&manifest as &dyn ManifestRef],
        src,
        dst.clone(),
        CacheSyncOptions::default(),
    )
    .await
    .unwrap();

    assert_eq!(result.statistics.total_objects, 1); // only the hashed file
    assert_eq!(result.statistics.copied_objects, 1);
    assert!(exists(&dst, "real_hash", "xxh128").await);
}

// ===== Chunk hashes =====

#[tokio::test]
async fn cache_sync_handles_chunk_hashes() {
    let (_sd, src) = make_fs_cache();
    let (_dd, dst) = make_fs_cache();

    put(&src, "chunk_0", "xxh128", b"part0").await;
    put(&src, "chunk_1", "xxh128", b"part1").await;
    put(&src, "chunk_2", "xxh128", b"part2").await;

    let manifest = test_manifest(vec![{
        let mut e = FileEntry::file("big.bin", 30, 1);
        e.chunk_hashes = Some(vec!["chunk_0".into(), "chunk_1".into(), "chunk_2".into()]);
        e
    }]);

    let result = cache_sync_manifest(
        &[&manifest as &dyn ManifestRef],
        src,
        dst.clone(),
        CacheSyncOptions::default(),
    )
    .await
    .unwrap();

    assert_eq!(result.statistics.copied_objects, 3); // one per chunk
    assert!(exists(&dst, "chunk_0", "xxh128").await);
    assert!(exists(&dst, "chunk_1", "xxh128").await);
    assert!(exists(&dst, "chunk_2", "xxh128").await);
}

// ===== Empty manifest =====

#[tokio::test]
async fn cache_sync_empty_manifest() {
    let (_sd, src) = make_fs_cache();
    let (_dd, dst) = make_fs_cache();

    let manifest = test_manifest(vec![]);

    let result = cache_sync_manifest(
        &[&manifest as &dyn ManifestRef],
        src,
        dst,
        CacheSyncOptions::default(),
    )
    .await
    .unwrap();

    assert_eq!(result.statistics.total_objects, 0);
    assert_eq!(result.statistics.copied_objects, 0);
    assert_eq!(result.statistics.skipped_objects, 0);
}

// ===== Same source and destination =====

#[tokio::test]
async fn cache_sync_same_cache_is_noop() {
    let (_sd, cache) = make_fs_cache();

    put(&cache, "hash_a", "xxh128", b"alpha").await;

    let manifest = test_manifest(vec![{
        let mut e = FileEntry::new("a.txt");
        e.hash = Some("hash_a".into());
        e.size = Some(5);
        e
    }]);

    let result = cache_sync_manifest(
        &[&manifest as &dyn ManifestRef],
        cache.clone(),
        cache,
        CacheSyncOptions::default(),
    )
    .await
    .unwrap();

    assert_eq!(result.statistics.skipped_objects, 1);
    assert_eq!(result.statistics.copied_objects, 0);
}