win-durable-ioring 0.3.2

A ring for Windows file I/O with durability scheduled into its work: writes are tagged with epochs, and durability through an epoch is requested and reported in the ring's own completion stream. The Windows driver for durable-ioring, built on windows-ioring-sys. No unsafe code.
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
// Copyright (c) 2026 Mike Grier

use std::fs::{self, OpenOptions};
use std::io;
use std::os::windows::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, PoisonError};

use win_shared_os_owned_handle::SharedHandle;
use win_time_sys::{InterruptTime, TimePoint};

use super::{DeliveryMode, Dioring, Setup, SetupError, SetupRefusal, TargetSetup};
use crate::windows_backend::{FileOptions, OpenedFile, Win32FileTarget};
use durable_ioring::DurableRing;
use durable_ioring::backend::IoBackend;
use durable_ioring::{DurabilityProvider, FlushRequest};
use durable_ioring::{FlushDomain, IoTargetKey};
use durable_ioring::{LineageHandle, StampClock};

// Pushes, completions and the readiness signal: DI-3.2.2.1.
mod pushes;
// Seals and durability in the default lineage: DI-3.2.3.
mod seals;
// Failures and their resolution, through the fault seam: DI-3.2.4.
mod failures;
// Failure stamps, from the instance's clock: WT-2.2.2.
mod stamps;
// Markings, and a failure's final record: DI-3.2.4.2.
mod markings;
// Lineage handles: minting, ending, retiring, and failures across lineages: DI-3.2.5.1.
mod lineages;
// Gated operations, and the cycles a push or a seal would close: DI-3.2.5.2.
mod gates;
// The consumer's durability provider: DI-3.2.6.
mod providers;
// Removing a file, and ending the instance: DI-3.2.7.
mod construction;
mod ending;
mod pumped;
mod signalled;
mod submits;
// A whole raw disk, through an instance, against an unpartitioned scratch virtual disk: DI-D-62.
mod raw_disk;
// A raw volume, through an instance, against a scratch virtual disk: DI-D-61.
mod raw_volume;
// A file opened unbuffered, through an instance: DI-D-60.
mod unbuffered;

type Ring = Dioring<Vec<u8>>;
type V = crate::WindowsIds<u64>;

/// Queue sizes for every instance these tests build. Small: nothing here pushes I/O.
const QUEUE: u32 = 16;

fn setup(
    files: Vec<TargetSetup>,
    buffers: Vec<Vec<u8>>,
    provider: Option<Box<dyn DurabilityProvider<V>>>,
) -> Setup<'static, 'static, u64, Vec<u8>> {
    Setup {
        submission_queue_size: QUEUE,
        completion_queue_size: QUEUE,
        files,
        buffers,
        provider,
        diagnostics: None,
        delivery: DeliveryMode::Pool(None),
    }
}

/// A mock interrupt-time clock a test moves by hand (DI-D-38). Clones share one reading, so a test
/// keeps a clone to move the clock it gave away. It keeps `Steady`'s promise by construction: it
/// only moves forwards.
#[derive(Clone, Debug, Default)]
pub(crate) struct MockClock(Arc<AtomicU64>);

impl MockClock {
    /// A clock reading `ticks`.
    pub(crate) fn at(ticks: u64) -> Self {
        Self(Arc::new(AtomicU64::new(ticks)))
    }

    /// Move the clock forwards by `ticks`.
    pub(crate) fn advance(&self, ticks: u64) {
        self.0.fetch_add(ticks, Ordering::SeqCst);
    }
}

impl StampClock for MockClock {
    type Instant = TimePoint<InterruptTime>;

    fn now(&self) -> TimePoint<InterruptTime> {
        TimePoint::from_ticks(self.0.load(Ordering::SeqCst))
    }
}

/// An instance given nothing.
pub(crate) fn empty() -> Ring {
    Ring::new(setup(Vec::new(), Vec::new(), None))
        .expect("build an empty instance")
        .0
}

/// A temporary file, removed when dropped.
pub(crate) struct TempFile(pub(crate) PathBuf);

impl TempFile {
    pub(crate) fn new(tag: &str) -> Self {
        static NEXT: AtomicU32 = AtomicU32::new(0);
        let path = std::env::temp_dir().join(format!(
            "durable-ioring-setup-{tag}-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        ));
        fs::write(&path, b"x").expect("create the temporary file");
        Self(path)
    }

    pub(crate) fn open(&self) -> OpenedFile {
        OpenedFile::open(&self.0, FileOptions::new().read(true).write(true))
            .expect("open the temporary file")
    }

    /// Whether any handle to the file is open: an exclusive open fails exactly then.
    pub(crate) fn is_held(&self) -> bool {
        held(&self.0)
    }
}

impl Drop for TempFile {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.0);
    }
}

fn held(path: &Path) -> bool {
    OpenOptions::new()
        .read(true)
        .share_mode(0)
        .open(path)
        .is_err()
}

fn domain(name: &str) -> FlushDomain {
    FlushDomain::new(name.as_bytes().to_vec())
}

fn file(key: u64, temp: &TempFile, domains: &[&str]) -> TargetSetup {
    TargetSetup {
        key: IoTargetKey(key),
        target: target(temp.open(), domains),
    }
}

/// A target over `file`, declared with `domains`.
pub(crate) fn target(file: OpenedFile, domains: &[&str]) -> Win32FileTarget {
    Win32FileTarget::new(file).with_domains(domains.iter().map(|d| domain(d)))
}

/// A provider that serves fixed domains and is never asked for anything here.
#[derive(Debug)]
struct Serves(Vec<FlushDomain>);

impl DurabilityProvider<V> for Serves {
    fn domains(&self) -> Vec<FlushDomain> {
        self.0.clone()
    }

    fn make_durable(&mut self, _: FlushRequest<V>) {
        unreachable!("construction asks a provider for its domains and nothing else")
    }
}

fn serves(domains: &[&str]) -> Option<Box<dyn DurabilityProvider<V>>> {
    Some(Box::new(Serves(
        domains.iter().map(|d| domain(d)).collect(),
    )))
}

/// A provider that serves fixed domains and keeps every request it is handed, unanswered, for the
/// test to answer through the shared list.
#[derive(Debug)]
pub(crate) struct Keeper {
    domains: Vec<FlushDomain>,
    kept: Kept,
}

/// The requests a `Keeper` has been handed and not yet given up.
pub(crate) type Kept = Arc<Mutex<Vec<FlushRequest<V>>>>;

impl DurabilityProvider<V> for Keeper {
    fn domains(&self) -> Vec<FlushDomain> {
        self.domains.clone()
    }

    fn make_durable(&mut self, request: FlushRequest<V>) {
        self.kept
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .push(request);
    }
}

/// A `Keeper` serving `domains`, and the list it keeps its requests in.
pub(crate) fn keeper(domains: &[&str]) -> (Option<Box<dyn DurabilityProvider<V>>>, Kept) {
    let kept = Arc::new(Mutex::new(Vec::new()));
    let provider = Keeper {
        domains: domains.iter().map(|d| domain(d)).collect(),
        kept: Arc::clone(&kept),
    };
    (Some(Box::new(provider)), kept)
}

fn refused(
    result: Result<(Ring, LineageHandle), SetupError<u64, Vec<u8>>>,
) -> SetupError<u64, Vec<u8>> {
    match result {
        Ok(_) => panic!("construction should have been refused"),
        Err(error) => error,
    }
}

#[test]
fn an_instance_given_nothing_builds_empty() {
    let ring = empty();
    assert!(ring.provider.is_none());
    let engine = ring.engine();
    assert_eq!(engine.backend.registered_buffers(), 0);
    assert!(engine.core.target(IoTargetKey(0)).is_none());
    assert!(engine.backend.target(IoTargetKey(0)).is_none());
}

#[test]
fn an_instance_has_only_its_default_lineage_to_begin_with() {
    let ring = empty();
    let lineages = ring.lineages();
    assert_eq!(lineages.len(), 1);
    let default = &lineages[0];
    assert!(default.is_default);
    assert_eq!(default.lineage, ring.default_lineage().lineage());
    assert!(default.description.is_none());
    assert!(default.durable_through.is_none());
    assert!(default.sealed_through.is_none());
}

#[test]
fn each_instance_has_its_own_default_lineage() {
    let (a, b) = (empty(), empty());
    assert_ne!(a.default_lineage().lineage(), b.default_lineage().lineage());
    assert_eq!(
        a.default_lineage().lineage(),
        a.default_lineage().lineage(),
        "stable for one instance"
    );
}

#[test]
fn files_given_at_construction_are_registered_in_order() {
    let temps: Vec<TempFile> = (0..3)
        .map(|i| TempFile::new(&format!("order{i}")))
        .collect();
    let files = temps
        .iter()
        .enumerate()
        .map(|(i, t)| file(10 + i as u64, t, &[]))
        .collect();
    let ring = Ring::new(setup(files, Vec::new(), None)).expect("build").0;
    let engine = ring.engine();
    for i in 0..3_u32 {
        let key = IoTargetKey(10 + u64::from(i));
        assert_eq!(engine.backend.registration_index(key), Some(i));
        assert!(engine.core.target(key).is_some(), "the core decides for it");
    }
}

#[test]
fn the_ring_holds_construction_files_until_the_instance_is_dropped() {
    let temp = TempFile::new("held");
    let ring = Ring::new(setup(vec![file(1, &temp, &[])], Vec::new(), None))
        .expect("build")
        .0;
    assert!(temp.is_held(), "the instance holds the file it was given");
    drop(ring);
    assert!(!temp.is_held(), "dropping the instance releases it");
}

#[test]
fn a_duplicate_key_is_refused_before_anything_is_created_and_everything_comes_back() {
    let (a, b) = (TempFile::new("dup-a"), TempFile::new("dup-b"));
    let given = vec![file(5, &a, &[]), file(5, &b, &[])];
    let originals: Vec<SharedHandle> = given.iter().map(|f| f.target.handle().clone()).collect();
    let error = refused(Ring::new(setup(given, vec![vec![1, 2, 3]], serves(&["P"]))));
    assert!(matches!(
        error.reason,
        SetupRefusal::DuplicateKey(IoTargetKey(5))
    ));
    assert_eq!(error.files.len(), 2);
    for (back, original) in error.files.iter().zip(&originals) {
        assert!(
            back.target.handle().same_handle(original),
            "the same handle comes back, in order"
        );
    }
    assert_eq!(error.buffers, Some(vec![vec![1, 2, 3]]));
    assert_eq!(
        error
            .provider
            .expect("the provider comes back")
            .domains()
            .len(),
        1
    );
    drop((error.files, originals));
    assert!(!a.is_held() && !b.is_held(), "no ring took a handle");
}

#[test]
fn a_provider_naming_a_domain_twice_is_refused_and_everything_comes_back() {
    let temp = TempFile::new("pdup");
    let error = refused(Ring::new(setup(
        vec![file(1, &temp, &["A"])],
        vec![vec![0; 8]],
        serves(&["A", "B", "A"]),
    )));
    match error.reason {
        SetupRefusal::DuplicateProviderDomain(domain) => assert_eq!(domain.bytes(), b"A"),
        other => panic!("refused for the wrong reason: {other:?}"),
    }
    assert_eq!(error.files.len(), 1);
    assert_eq!(error.buffers, Some(vec![vec![0; 8]]));
    assert!(error.provider.is_some());
}

#[test]
fn a_ring_that_cannot_be_created_refuses_construction_and_everything_comes_back() {
    let temp = TempFile::new("noring");
    let mut given = setup(vec![file(1, &temp, &[])], vec![vec![7; 4]], serves(&["P"]));
    // Zero is not refused -- the kernel rounds it up -- but a queue no system can allocate is.
    given.submission_queue_size = u32::MAX;
    given.completion_queue_size = u32::MAX;
    let error = refused(Ring::new(given));
    assert!(
        matches!(error.reason, SetupRefusal::Ring(_)),
        "{:?}",
        error.reason
    );
    assert_eq!(error.files.len(), 1);
    assert_eq!(error.buffers, Some(vec![vec![7; 4]]));
    assert!(error.provider.is_some());
}

#[test]
fn buffers_given_at_construction_are_registered() {
    let ring = Ring::new(setup(Vec::new(), vec![vec![0; 64], vec![0; 128]], None))
        .expect("build")
        .0;
    assert_eq!(ring.engine().backend.registered_buffers(), 2);
}

#[test]
fn files_and_buffers_are_registered_together() {
    let temp = TempFile::new("both");
    let ring = Ring::new(setup(vec![file(1, &temp, &["A"])], vec![vec![0; 32]], None))
        .expect("build")
        .0;
    assert!(ring.engine().core.target(IoTargetKey(1)).is_some());
    assert_eq!(ring.engine().backend.registered_buffers(), 1);
    assert!(temp.is_held());
}

#[test]
fn an_instance_is_built_with_any_epoch_id_and_context_type() {
    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
    struct Lsn(u64);
    let ring = Dioring::<Vec<u8>, Lsn, u32>::new(Setup {
        submission_queue_size: QUEUE,
        completion_queue_size: QUEUE,
        files: Vec::new(),
        buffers: Vec::new(),
        provider: None,
        diagnostics: None,
        delivery: DeliveryMode::Pool(None),
    })
    .expect("build")
    .0;
    assert_eq!(ring.lineages().len(), 1);
}

#[test]
fn an_instance_delivers_on_the_pool_its_environment_names() {
    // This pins that an instance built with an environment delivers at all; that the pool named is
    // the one a drain runs on is pinned by holding its only thread, in pool delivery's own tests.
    use windows_threadpool_sys::callback_env::CallbackEnviron;
    use windows_threadpool_sys::pool::ThreadpoolPool;

    use durable_ioring::{DurabilityRequest, Entry};
    use pushes::{GIVEN, Harness, given, temp};

    let threads = ThreadpoolPool::new().expect("create a pool");
    let mut env = CallbackEnviron::new();
    env.set_pool(&threads);
    let file = temp(&[0; 16]);
    let ring = pushes::Ring::new(Setup {
        submission_queue_size: QUEUE,
        completion_queue_size: QUEUE,
        files: vec![given(GIVEN, &file)],
        buffers: Vec::new(),
        provider: None,
        diagnostics: None,
        delivery: DeliveryMode::Pool(Some(&mut env)),
    })
    .expect("build")
    .0;
    let mut harness = Harness::new(ring);
    harness.write(GIVEN, 0, b"pooled", 1, 1);
    harness.next();
    assert_eq!(harness.seal(1), DurabilityRequest::Submitted);
    assert!(matches!(harness.next_entry(), Entry::Durable { .. }));
    // The instance ends first, then `env`, then the pool it names -- the reverse of declaration.
    harness.finish();
}

/// The refusals hand back their error rather than panicking or hiding it.
#[test]
fn a_setup_error_prints_without_a_debug_buffer_type() {
    struct Opaque;
    // `Opaque` is not `Debug`; the error still is.
    let error: SetupError<u64, Opaque> = SetupError {
        reason: SetupRefusal::Ring(io::Error::other("x")),
        files: Vec::new(),
        buffers: Some(vec![Opaque, Opaque]),
        provider: None,
    };
    let text = format!("{error:?}");
    assert!(text.contains("Some(2)"), "{text}");
}