hyperfile 0.11.4

A file-like interface abstraction layer provides random read and write capabilities for S3
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
//! Multi-instance concurrency integration tests.
//!
//! Two `Hyper` instances opening the same URI are independent and
//! coordinate only through S3 optimistic concurrency control. These
//! tests exercise that coordination:
//!
//!  - `concurrent_two_writers_optimistic_cc`: default
//!    `RetryLastWriterWins` policy; both writers succeed, last writer
//!    overwrites.
//!  - `concurrent_read_while_writer_flushes`: reader observes
//!    self-consistent checkpoints while writer churns.
//!  - `concurrent_writer_a_fails_writer_b_succeeds`: writer A has an
//!    always-fail interceptor; writer B commits normally.
//!  - `concurrent_two_writers_fail_fast_policy`: `FailFast` policy;
//!    exactly one writer returns Ok, the other sees
//!    `ErrorKind::AlreadyExists`.
//!
//! See `docs/concurrency.md` for the full behavior model.
//!
//! Run with:
//! ```bash
//! cargo test --test integration_s3_concurrent -- --ignored --test-threads=1
//! ```

mod common;
use common::*;

use std::io::ErrorKind;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use hyperfile::file::hyper::Hyper;
use hyperfile::file::flags::FileFlags;
use hyperfile::file::mode::FileMode;
use hyperfile::config::{FlushConflictPolicy, HyperFileRuntimeConfig};

// --------------------------------------------------------------------
// Concurrent-writer / concurrent-reader tests (A group)
// --------------------------------------------------------------------
//
// hyperfile's concurrency model:
//   * Single Hyper instance serializes writes via a semaphore(1) when
//     `range-lock` feature is disabled (the default test configuration).
//   * Multiple Hyper instances opening the same S3 URI are INDEPENDENT
//     and rely on S3-level optimistic concurrency control through
//     `OnDiskState` (checksum / ETag) threaded into `flush_inode`.
//
// These tests exercise the multi-instance case. The expected invariant
// is that two writers racing on the same URI cannot silently both commit
// divergent state — either one wins and the other gets a well-defined
// error, or they serialize so the last writer's view is consistent.

/// Two writers open the same URI, write different data, flush concurrently.
/// Under optimistic concurrency control we expect:
///   * At most one of the two flushes reports Ok.
///   * After both finish, reopening shows a state consistent with the
///     winning writer (either A's or B's bytes, not a mix).
#[tokio::test]
#[ignore]
async fn concurrent_two_writers_optimistic_cc() {
    let _ = env_logger::try_init();
    let client = make_client().await;
    let tf = TestFile::new(&client).await;

    // Seed an empty file so both writers open the same persisted baseline.
    {
        let mut hyper = Hyper::fs_open_or_create_with_default_opt(
            &client,
            tf.uri(),
            FileFlags::rdwr(),
            FileMode::default_file(),
        )
        .await
        .expect("create");
        let _ = hyper.fs_release().await.expect("release");
    }

    let uri = tf.uri().to_string();
    let client_a = client.clone();
    let client_b = client.clone();
    let uri_a = uri.clone();
    let uri_b = uri.clone();

    let writer_a = async move {
        let mut hyper = Hyper::fs_open(&client_a, &uri_a, FileFlags::rdwr())
            .await
            .expect("open A");
        // A writes 4 KiB of 0xAA.
        let data = vec![0xAAu8; 4096];
        let _ = hyper.fs_write(0, &data).await.expect("write A");
        let flush = hyper.fs_flush().await;
        let _ = hyper.fs_release().await;
        flush
    };

    let writer_b = async move {
        let mut hyper = Hyper::fs_open(&client_b, &uri_b, FileFlags::rdwr())
            .await
            .expect("open B");
        // B writes 4 KiB of 0xBB.
        let data = vec![0xBBu8; 4096];
        let _ = hyper.fs_write(0, &data).await.expect("write B");
        let flush = hyper.fs_flush().await;
        let _ = hyper.fs_release().await;
        flush
    };

    let (a_flush, b_flush) = tokio::join!(writer_a, writer_b);

    let a_ok = a_flush.is_ok();
    let b_ok = b_flush.is_ok();
    println!(
        "[two_writers_occ] A flush={:?} B flush={:?}",
        a_flush, b_flush
    );

    // Invariant 1: at least one writer must succeed (otherwise we made no
    // progress). Invariant 2: at most one succeeds if OCC is working.
    assert!(
        a_ok || b_ok,
        "both writers failed: A={:?} B={:?}",
        a_flush, b_flush,
    );

    // Read the final persisted state.
    let mut buf = vec![0u8; 4096];
    let mut hyper = Hyper::fs_open(&client, &uri, FileFlags::rdonly())
        .await
        .expect("reopen");
    let stat = hyper.fs_getattr().expect("getattr");
    let n = hyper.fs_read(0, &mut buf).await.expect("read");
    let _ = hyper.fs_release().await;

    println!(
        "[two_writers_occ] final size={} first_byte=0x{:02X} last_byte=0x{:02X}",
        stat.st_size, buf[0], buf[n - 1]
    );

    // The persisted bytes must be a single writer's data, not a mix.
    let all_aa = buf.iter().all(|&b| b == 0xAA);
    let all_bb = buf.iter().all(|&b| b == 0xBB);
    assert!(
        all_aa || all_bb,
        "persisted content is a mix of A and B — consistency broken (first={:#x} last={:#x})",
        buf[0], buf[n - 1],
    );

    // OCC invariant: if both flushes claimed Ok, there's a good chance the
    // second one silently overwrote the first. Record the observation.
    if a_ok && b_ok {
        println!(
            "[two_writers_occ] WARNING: both flushes reported Ok — OCC may not be enforcing exclusion"
        );
    }

    tf.cleanup(&client).await;
}

/// A writer repeatedly writes and flushes; a concurrent reader opens
/// the URI while that's happening. Every reader open must yield a
/// self-consistent view (the size must match what can be read).
#[tokio::test]
#[ignore]
async fn concurrent_read_while_writer_flushes() {
    let _ = env_logger::try_init();
    let client = make_client().await;
    let tf = TestFile::new(&client).await;

    // Seed a known initial state.
    {
        let mut hyper = Hyper::fs_open_or_create_with_default_opt(
            &client,
            tf.uri(),
            FileFlags::rdwr(),
            FileMode::default_file(),
        )
        .await
        .expect("create");
        let initial = vec![0x11u8; 4096];
        let _ = hyper.fs_write(0, &initial).await.expect("write");
        let _ = hyper.fs_release().await.expect("release");
    }

    let stop = Arc::new(std::sync::atomic::AtomicBool::new(false));

    let uri = tf.uri().to_string();
    let client_w = client.clone();
    let uri_w = uri.clone();
    let stop_w = stop.clone();

    let writer = async move {
        let mut iterations = 0u32;
        while !stop_w.load(Ordering::SeqCst) && iterations < 8 {
            let mut hyper = Hyper::fs_open(&client_w, &uri_w, FileFlags::rdwr())
                .await
                .expect("writer open");
            // Each iteration writes a new 4 KiB pattern filled with the
            // iteration number (low byte), then flushes.
            let byte = 0x20u8 + (iterations as u8);
            let data = vec![byte; 4096];
            let _ = hyper.fs_write(0, &data).await.expect("writer write");
            let _ = hyper.fs_flush().await;
            let _ = hyper.fs_release().await;
            iterations += 1;
            tokio::task::yield_now().await;
        }
        iterations
    };

    let uri_r = uri.clone();
    let client_r = client.clone();
    let stop_r = stop.clone();

    let reader = async move {
        let mut observations = Vec::new();
        let mut reads = 0u32;
        while !stop_r.load(Ordering::SeqCst) && reads < 20 {
            let mut hyper = Hyper::fs_open(&client_r, &uri_r, FileFlags::rdonly())
                .await
                .expect("reader open");
            let size = hyper.fs_getattr().expect("getattr").st_size as usize;
            let mut buf = vec![0u8; size];
            let _ = hyper.fs_read(0, &mut buf).await.expect("read");
            let _ = hyper.fs_release().await;

            // Every read must see a homogeneous block — a single writer's
            // pattern. If the size says 4096 but bytes diverge, the
            // checkpoint is inconsistent.
            let first = buf.first().copied();
            let all_same = buf.iter().all(|&b| Some(b) == first);
            observations.push((size, first, all_same));

            reads += 1;
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }
        observations
    };

    // Run writer + reader concurrently; have a separate future arm the
    // stop flag after a short window.
    let stop_arm = stop.clone();
    let timer = async move {
        tokio::time::sleep(std::time::Duration::from_secs(2)).await;
        stop_arm.store(true, Ordering::SeqCst);
    };

    let (writer_iters, observations, _) = tokio::join!(writer, reader, timer);

    println!(
        "[read_while_writer_flushes] writer did {} iterations, reader made {} reads",
        writer_iters, observations.len()
    );

    // Every observation must be self-consistent (homogeneous buffer).
    for (i, (size, first, ok)) in observations.iter().enumerate() {
        assert!(
            *ok,
            "observation #{} inconsistent: size={} first_byte={:?} buffer not homogeneous",
            i, size, first,
        );
    }

    tf.cleanup(&client).await;
}

/// Two writers again, but writer A's flush is injected with always-fail
/// (the instance never commits). Writer B writes normally. Expected:
///   * A's flush returns Err (and rollback leaves it consistent).
///   * B's flush returns Ok.
///   * After both finish, the persisted content matches B's write.
#[tokio::test]
#[ignore]
async fn concurrent_writer_a_fails_writer_b_succeeds() {
    let _ = env_logger::try_init();
    let client = make_client().await;
    let tf = TestFile::new(&client).await;

    // Seed empty file.
    {
        let mut hyper = Hyper::fs_open_or_create_with_default_opt(
            &client,
            tf.uri(),
            FileFlags::rdwr(),
            FileMode::default_file(),
        )
        .await
        .expect("create");
        let _ = hyper.fs_release().await.expect("release");
    }

    let uri = tf.uri().to_string();
    let client_a = client.clone();
    let client_b = client.clone();
    let uri_a = uri.clone();
    let uri_b = uri.clone();

    let writer_a = async move {
        let mut hyper = Hyper::fs_open(&client_a, &uri_a, FileFlags::rdwr())
            .await
            .expect("open A");
        hyper.with_staging_interceptor(AlwaysFailFlushInode::new());
        let data = vec![0xAAu8; 2048];
        let _ = hyper.fs_write(0, &data).await.expect("write A");
        let flush = hyper.fs_flush().await;
        let _ = hyper.fs_release().await;
        flush
    };

    let writer_b = async move {
        let mut hyper = Hyper::fs_open(&client_b, &uri_b, FileFlags::rdwr())
            .await
            .expect("open B");
        let data = vec![0xBBu8; 2048];
        let _ = hyper.fs_write(0, &data).await.expect("write B");
        let flush = hyper.fs_flush().await;
        let _ = hyper.fs_release().await;
        flush
    };

    let (a_flush, b_flush) = tokio::join!(writer_a, writer_b);

    println!(
        "[a_fails_b_succeeds] A flush={:?} B flush={:?}",
        a_flush, b_flush
    );

    assert!(
        a_flush.is_err(),
        "writer A should fail due to injected interceptor, got {:?}",
        a_flush,
    );
    assert!(
        b_flush.is_ok(),
        "writer B should succeed, got {:?}",
        b_flush,
    );

    // Persisted content must be exactly B's bytes.
    let mut buf = vec![0u8; 2048];
    let mut hyper = Hyper::fs_open(&client, &uri, FileFlags::rdonly())
        .await
        .expect("reopen");
    let size = hyper.fs_getattr().expect("getattr").st_size as usize;
    let _ = hyper.fs_read(0, &mut buf).await.expect("read");
    let _ = hyper.fs_release().await;

    assert_eq!(size, 2048, "unexpected persisted size: {}", size);
    assert!(
        buf.iter().all(|&b| b == 0xBB),
        "persisted bytes do not match writer B's data (first={:#x}, last={:#x})",
        buf[0], buf[2047],
    );

    tf.cleanup(&client).await;
}

/// Same scenario as `concurrent_two_writers_optimistic_cc` but both
/// writers are configured with `FlushConflictPolicy::FailFast`. In this
/// mode the later writer must NOT silently overwrite the earlier one —
/// exactly one writer succeeds, the other observes
/// `ErrorKind::AlreadyExists` and does not retry.
#[tokio::test]
#[ignore]
async fn concurrent_two_writers_fail_fast_policy() {
    let _ = env_logger::try_init();
    let client = make_client().await;
    let tf = TestFile::new(&client).await;

    // Seed empty file.
    {
        let mut hyper = Hyper::fs_open_or_create_with_default_opt(
            &client,
            tf.uri(),
            FileFlags::rdwr(),
            FileMode::default_file(),
        )
        .await
        .expect("create");
        let _ = hyper.fs_release().await.expect("release");
    }

    let runtime_cfg = HyperFileRuntimeConfig {
        flush_conflict_policy: FlushConflictPolicy::FailFast,
        ..HyperFileRuntimeConfig::default()
    };

    let uri = tf.uri().to_string();
    let client_a = client.clone();
    let client_b = client.clone();
    let uri_a = uri.clone();
    let uri_b = uri.clone();
    let cfg_a = runtime_cfg.clone();
    let cfg_b = runtime_cfg.clone();

    // Both writers open before either writes. Joined without this, the two only
    // overlapped by chance: B's open trailed A's by a few milliseconds and A
    // published about 15 ms after its own, so a slow request on B's open let it
    // start from A's checkpoint — two writers one after the other, both rightly
    // succeeding, and the test failing as if the policy had not held.
    let opened = std::sync::Arc::new(tokio::sync::Barrier::new(2));
    let (opened_a, opened_b) = (opened.clone(), opened.clone());
    let writer_a = async move {
        let mut hyper = Hyper::fs_open_opt(&client_a, &uri_a, FileFlags::rdwr(), &cfg_a)
            .await
            .expect("open A");
        opened_a.wait().await;
        let data = vec![0xAAu8; 4096];
        let _ = hyper.fs_write(0, &data).await.expect("write A");
        let flush = hyper.fs_flush().await;
        let _ = hyper.fs_release().await;
        flush
    };

    let writer_b = async move {
        let mut hyper = Hyper::fs_open_opt(&client_b, &uri_b, FileFlags::rdwr(), &cfg_b)
            .await
            .expect("open B");
        opened_b.wait().await;
        let data = vec![0xBBu8; 4096];
        let _ = hyper.fs_write(0, &data).await.expect("write B");
        let flush = hyper.fs_flush().await;
        let _ = hyper.fs_release().await;
        flush
    };

    let (a_flush, b_flush) = tokio::join!(writer_a, writer_b);
    println!(
        "[fail_fast_policy] A flush={:?} B flush={:?}",
        a_flush, b_flush
    );

    // Exactly one must be Ok; the other must be AlreadyExists.
    let a_ok = a_flush.is_ok();
    let b_ok = b_flush.is_ok();
    assert!(
        a_ok ^ b_ok,
        "expected exactly one writer to succeed under FailFast, got A={:?} B={:?}",
        a_flush, b_flush,
    );

    // The failing side must report AlreadyExists specifically, not a
    // generic ResourceBusy or Other.
    let failed = if a_ok { b_flush } else { a_flush };
    let err = failed.as_ref().err().expect("failing writer should have error");
    assert_eq!(
        err.kind(),
        ErrorKind::AlreadyExists,
        "failing writer's error kind should be AlreadyExists under FailFast, got {:?}",
        err,
    );

    // Persisted content must match the winning writer (homogeneous bytes).
    let mut buf = vec![0u8; 4096];
    let mut hyper = Hyper::fs_open(&client, &uri, FileFlags::rdonly())
        .await
        .expect("reopen");
    let _ = hyper.fs_read(0, &mut buf).await.expect("read");
    let _ = hyper.fs_release().await;

    let all_aa = buf.iter().all(|&b| b == 0xAA);
    let all_bb = buf.iter().all(|&b| b == 0xBB);
    assert!(
        all_aa || all_bb,
        "persisted content is a mix of A and B (first={:#x} last={:#x})",
        buf[0], buf[4095],
    );

    tf.cleanup(&client).await;
}