vfsi-sync 0.1.14

Synchronous scalar and vectorized filesystem interfaces for VFSI
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
//! Write dispatch and acknowledged-progress completion.

use super::*;

// Sparse conflicts commonly give a request just one successor. Retain it
// inline; allocate a vector only when the request has additional successors.
struct WriteSuccessors {
    first: usize,
    more: Vec<usize>,
}

impl Default for WriteSuccessors {
    fn default() -> Self {
        Self {
            first: usize::MAX,
            more: Vec::new(),
        }
    }
}

impl WriteSuccessors {
    fn push(&mut self, successor: usize) {
        if self.first == usize::MAX {
            self.first = successor;
        } else {
            self.more.push(successor);
        }
    }

    fn take(&mut self) -> impl Iterator<Item = usize> {
        let first = std::mem::replace(&mut self.first, usize::MAX);
        std::iter::once(first)
            .filter(|&successor| successor != usize::MAX)
            .chain(std::mem::take(&mut self.more))
    }
}

type WriteRange = (usize, u64, u64, bool);

#[cfg(test)]
#[test]
fn large_write_graphs_have_linear_edges_and_preserve_initial_readiness() {
    for shape in ["overlap", "scatter", "append"] {
        let count = 4096;
        let mut prior = WriteDependencies::default();
        let mut successors: Vec<_> = (0..count).map(|_| WriteSuccessors::default()).collect();
        let mut remaining = vec![0; count];
        let mut ready = Vec::new();
        for index in 0..count {
            let start = if shape == "scatter" {
                index as u64 * 4
            } else {
                0
            };
            add_write_dependencies(
                (index, start, start + 4, shape == "append"),
                &mut prior,
                &mut successors,
                &mut remaining,
                &mut ready,
            );
        }
        let edges: usize = remaining.iter().sum();
        if shape == "scatter" {
            assert_eq!(edges, 0);
            assert_eq!(ready, (0..count).collect::<Vec<_>>());
        } else {
            assert!(edges <= count + 120, "{shape}: {edges} edges");
            assert_eq!(ready, [0]);
        }
        assert!(prior.prior.is_empty());
        assert!(prior.frontier.is_some());
    }
}

// Small/sparse object cohorts retain the cheap linear path. Larger ones remember
// only the latest writer of each disjoint byte interval. An overwritten writer
// remains ordered through its successors, so redundant transitive edges vanish.
#[derive(Default)]
struct WriteDependencies {
    prior: Vec<WriteRange>,
    frontier: Option<Box<WriteFrontier>>,
}

#[derive(Default)]
struct WriteFrontier {
    ranges: std::collections::BTreeMap<u64, (u64, usize)>,
    append: Option<usize>,
    dependencies: Vec<usize>,
}

impl WriteFrontier {
    fn insert(&mut self, (index, start, end, append): WriteRange, mut depend: impl FnMut(usize)) {
        self.dependencies.clear();
        self.dependencies.extend(self.append);
        if append {
            self.dependencies
                .extend(self.ranges.values().map(|&(_, writer)| writer));
            self.ranges.clear();
            self.append = Some(index);
        } else {
            // Preserve uncovered tails when replacing the most recent writer.
            if let Some((&left, &(right, writer))) = self.ranges.range(..=start).next_back()
                && right > start
            {
                self.ranges.remove(&left);
                self.dependencies.push(writer);
                if left < start {
                    self.ranges.insert(left, (start, writer));
                }
                if end < right {
                    self.ranges.insert(end, (right, writer));
                }
            }
            while let Some((&left, &(right, writer))) = self.ranges.range(start..end).next() {
                self.ranges.remove(&left);
                self.dependencies.push(writer);
                if end < right {
                    self.ranges.insert(end, (right, writer));
                }
            }
            self.ranges.insert(start, (end, index));
        }
        // One writer can own several fragments. Emit its edge only once.
        self.dependencies.sort_unstable();
        self.dependencies.dedup();
        for &writer in &self.dependencies {
            depend(writer);
        }
    }
}

fn add_write_dependencies(
    range: WriteRange,
    prior: &mut WriteDependencies,
    successors: &mut [WriteSuccessors],
    remaining_dependencies: &mut [usize],
    wave_indices: &mut Vec<usize>,
) {
    let (index, offset, end, append) = range;
    let count = prior.prior.len();
    let dense = prior
        .prior
        .last()
        .is_some_and(|&(last, _, _, _)| remaining_dependencies[last] > count / 4);
    // Dense graphs benefit early; a BTreeMap costs more than a few cheap
    // comparisons in a sparse chain. Both thresholds bound the quadratic prefix.
    if prior.frontier.is_none() && count >= 16 && (count >= 256 || dense) {
        let mut frontier = Box::<WriteFrontier>::default();
        for previous in prior.prior.drain(..) {
            frontier.insert(previous, |_| {});
        }
        prior.prior = Vec::new();
        prior.frontier = Some(frontier);
    }
    let mut depend = |earlier: usize| {
        successors[earlier].push(index);
        remaining_dependencies[index] += 1;
    };
    if let Some(frontier) = &mut prior.frontier {
        frontier.insert(range, depend);
    } else {
        for &(earlier, start, earlier_end, earlier_append) in &prior.prior {
            if earlier_append || append || (offset < earlier_end && start < end) {
                depend(earlier);
            }
        }
        prior.prior.push(range);
    }
    if remaining_dependencies[index] == 0 {
        wave_indices.push(index);
    }
}

impl<F: VectorBackend> FsClient<F> {
    #[doc(hidden)]
    pub fn vwrite_native(&self, requests: &[FsWrite<'_, F>]) -> VfResult<Vec<FsWriteResult>> {
        self.vwrite_mapped_native(requests, |request| *request)
    }

    /// Adapter constructing cheap borrowed requests without a temporary vector.
    /// The mapper must return the same file, offset, and payload on each call.
    #[doc(hidden)]
    pub fn vwrite_mapped_native<'b, T>(
        &self,
        requests: &[T],
        project: impl Fn(&T) -> FsWrite<'b, F>,
    ) -> VfResult<Vec<FsWriteResult>>
    where
        F: 'b,
    {
        let writes = self.write_ops(requests, &project)?;
        // Drop the backend guard before invoking caller projection code.
        let result = self.lock()?.vwrite_impl(&writes);
        let results = result.map_err(|error| {
            with_indexed_context(error, "vwrite_native", |index| {
                requests
                    .get(index)
                    .map(&project)
                    .map(|request| request.file().path())
            })
        })?;
        if results.len() != requests.len() {
            return Err(wrong_result_count(
                "vwrite_native",
                requests.len(),
                results.len(),
            ));
        }
        validate_write_results("vwrite_native", &writes, &results).map_err(|error| {
            with_indexed_context(error, "vwrite_native", |index| {
                requests
                    .get(index)
                    .map(&project)
                    .map(|request| request.file().path())
            })
        })?;
        Ok(results.into_iter().map(write_result).collect())
    }

    /// Write every byte in each positional request, retrying short writes in
    /// vector waves. Like `vwrite_native`, this is not transactional: an error may
    /// follow a successfully written prefix. Overlapping requests through the
    /// same object complete in input order, including different open instances
    /// and hard-link aliases. Unknown identities are ordered conservatively.
    #[doc(hidden)]
    pub fn vwrite_all_native(&self, requests: &[FsWrite<'_, F>]) -> VfResult<Vec<FsWriteResult>> {
        self.vwrite_all_mapped_native(requests, |request| *request)
    }

    /// Complete mapped writes with whole-batch preflight and dependency waves.
    /// Each borrowed request is projected once, before acquiring the backend lock.
    #[doc(hidden)]
    pub fn vwrite_all_mapped_native<'b, T>(
        &self,
        requests: &[T],
        project: impl Fn(&T) -> FsWrite<'b, F>,
    ) -> VfResult<Vec<FsWriteResult>>
    where
        F: 'b,
    {
        // Whole-batch validation precedes identity queries and all writes,
        // including validation of empty requests and foreign/closed handles.
        let mut prepared = Vec::with_capacity(requests.len());
        let mut raw_files = Vec::with_capacity(requests.len());
        for (index, request) in requests.iter().map(&project).enumerate() {
            self.validate_owner(request.file(), index)?;
            let raw = request.file().raw().map_err(|error| {
                error
                    .with_index(index)
                    .with_context("vwrite_all_native", request.file().path())
            })?;
            let VfOffset::At(offset) = request.offset() else {
                return Err(VfError::client(index, crate::ERR_INVAL)
                    .with_context("vwrite_all_native", request.file().path()));
            };
            offset
                .checked_add(request.data().len() as u64)
                .ok_or_else(|| {
                    VfError::client(index, libc::EOVERFLOW as u32)
                        .with_context("vwrite_all_native", request.file().path())
                })?;
            // Reuse the offset allocation to retain the cheap borrowed request.
            prepared.push((request, offset));
            if !request.data().is_empty() {
                raw_files.push(raw);
            }
        }
        let graph_nodes = if raw_files.len() > 1 {
            prepared.len()
        } else {
            0
        };
        let mut successors: Vec<WriteSuccessors> = (0..graph_nodes)
            .map(|_| WriteSuccessors::default())
            .collect();
        let mut remaining_dependencies = vec![0usize; graph_nodes];
        let mut wave_indices = Vec::new();
        if raw_files.len() < 2 {
            // A scalar nonempty request needs neither identity nor graph work.
            wave_indices.extend(
                prepared
                    .iter()
                    .map(|(request, _)| *request)
                    .enumerate()
                    .filter(|(_, request)| !request.data().is_empty())
                    .map(|(index, _)| index),
            );
        } else {
            let map_identity_error = |error: VfError| {
                let error = error.map_index(|raw_index| {
                    prepared
                        .iter()
                        .map(|(request, _)| *request)
                        .enumerate()
                        .filter(|(_, request)| !request.data().is_empty())
                        .nth(raw_index)
                        .map_or(raw_index, |(index, _)| index)
                });
                with_indexed_context(error, "vwrite_all_native", |index| {
                    prepared
                        .get(index)
                        .map(|(request, _)| *request)
                        .map(|request| request.file().path())
                })
            };
            let mut backend = self.lock()?;
            backend
                .prepare_object_refs(&raw_files)
                .map_err(map_identity_error)?;
            let mut unknown = false;
            let mut prior_by_object = HashMap::new();
            let mut prior_unknown = WriteDependencies::default();
            for (index, request) in prepared.iter().map(|(request, _)| *request).enumerate() {
                if request.data().is_empty() {
                    continue;
                }
                let object = backend
                    .open_object_ref(request.file().raw()?)
                    .map_err(|error| {
                        error
                            .with_index(index)
                            .with_context("vwrite_all_native", request.file().path())
                    })?;
                if object.is_none() && !unknown {
                    // Unknown may alias any earlier object. Rebuild the prefix
                    // as one group, without repeating identity queries. Known
                    // batches need only one pass over backend references.
                    unknown = true;
                    prior_by_object.clear();
                    for successors in successors.iter_mut() {
                        *successors = WriteSuccessors::default();
                    }
                    remaining_dependencies.fill(0);
                    wave_indices.clear();
                    for (earlier, previous) in prepared[..index]
                        .iter()
                        .map(|(request, _)| *request)
                        .enumerate()
                    {
                        if previous.data().is_empty() {
                            continue;
                        }
                        let offset = prepared[earlier].1;
                        add_write_dependencies(
                            (
                                earlier,
                                offset,
                                offset + previous.data().len() as u64,
                                previous.file().append,
                            ),
                            &mut prior_unknown,
                            &mut successors,
                            &mut remaining_dependencies,
                            &mut wave_indices,
                        );
                    }
                }
                let prior = if unknown {
                    &mut prior_unknown
                } else {
                    prior_by_object
                        .entry(object.expect("known object reference"))
                        .or_default()
                };
                let offset = prepared[index].1;
                // Complete conflicting requests before dispatching later bytes,
                // so retries cannot interleave aliases or overwrite their data.
                add_write_dependencies(
                    (
                        index,
                        offset,
                        offset + request.data().len() as u64,
                        request.file().append,
                    ),
                    prior,
                    &mut successors,
                    &mut remaining_dependencies,
                    &mut wave_indices,
                );
            }
        }
        drop(raw_files);
        let mut totals = vec![0usize; prepared.len()];
        let mut stable = vec![true; prepared.len()];
        let mut next_wave_indices = Vec::new();
        while !wave_indices.is_empty() {
            next_wave_indices.clear();
            let wave: Vec<_> = wave_indices
                .iter()
                .map(|&index| {
                    let request = prepared[index].0;
                    let VfOffset::At(offset) = request.offset() else {
                        return Err(VfError::client(index, crate::ERR_INVAL)
                            .with_context("vwrite_all_native", request.file().path()));
                    };
                    let offset = offset.checked_add(totals[index] as u64).ok_or_else(|| {
                        VfError::client(index, libc::EOVERFLOW as u32)
                            .with_context("vwrite_all_native", request.file().path())
                    })?;
                    Ok(FsWrite::new(
                        request.file(),
                        VfOffset::At(offset),
                        &request.data()[totals[index]..],
                    ))
                })
                .collect::<VfResult<_>>()?;
            let results = self.vwrite_native(&wave).map_err(|error| {
                error.map_index(|index| wave_indices.get(index).copied().unwrap_or(index))
            })?;
            for (&index, result) in wave_indices.iter().zip(results) {
                let request = prepared[index].0;
                if result.bytes_written == 0 {
                    return Err(VfError::client(index, crate::ERR_IO)
                        .with_context("vwrite_all_native", request.file().path()));
                }
                totals[index] += result.bytes_written;
                if request.file().append {
                    // Preserve the last reported end even when outside writers
                    // append between short-write waves. This is an aggregate
                    // completion offset, not a guarantee of a contiguous extent.
                    prepared[index].1 = result
                        .offset
                        .checked_add(result.bytes_written as u64)
                        .and_then(|end| end.checked_sub(totals[index] as u64))
                        .ok_or_else(|| {
                            VfError::transport_with_kind(
                                Some(index),
                                crate::TransportKind::InvalidReply,
                                "append completion offset cannot be represented",
                            )
                        })?;
                }
                stable[index] &= result.stable;
                if totals[index] == request.data().len() {
                    // Release each edge once, only after all bytes of its
                    // predecessor are acknowledged. Never rescan blocked items.
                    if let Some(successors) = successors.get_mut(index) {
                        for successor in successors.take() {
                            remaining_dependencies[successor] -= 1;
                            if remaining_dependencies[successor] == 0 {
                                next_wave_indices.push(successor);
                            }
                        }
                    }
                } else {
                    next_wave_indices.push(index);
                }
            }
            // Newly released successors may precede or follow ongoing short
            // writes. Match the original input order within every vector wave.
            if !next_wave_indices.is_sorted() {
                next_wave_indices.sort_unstable();
            }
            std::mem::swap(&mut wave_indices, &mut next_wave_indices);
        }
        Ok(prepared
            .into_iter()
            .enumerate()
            .map(|(index, (_, offset))| FsWriteResult {
                offset,
                bytes_written: totals[index],
                stable: stable[index],
            })
            .collect())
    }

    fn write_ops<'a, 'b, T>(
        &self,
        requests: &'a [T],
        project: impl Fn(&T) -> FsWrite<'b, F>,
    ) -> VfResult<Vec<WriteOp<&'a FileOperand, &'a [u8]>>>
    where
        F: 'b,
        'b: 'a,
    {
        let mut writes = Vec::with_capacity(requests.len());
        for (index, request) in requests.iter().map(&project).enumerate() {
            self.validate_owner(request.file(), index)?;
            writes.push(WriteOp::new(
                request
                    .file()
                    .raw()
                    .map_err(|error| error.with_index(index))?,
                // Canonicalize append requests before common result validation:
                // the backend selects EOF, not the caller's positional offset.
                if request.file().append {
                    VfOffset::End
                } else {
                    request.offset()
                },
                request.data(),
            ));
        }
        Ok(writes)
    }
}