store-io-engine 0.5.1

The store-io engine: volumes, regions, barriers with flush sharing, receipts, provisioning and A/B slot objects over any store-io platform.
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
//! The batch layer: many writes, one barrier.
//!
//! - [`AppendBatch`] packs records back to back (no per-record padding) into
//!   pooled aligned buffers as they are added, so committing costs one
//!   reservation, the fewest possible device writes (kept in flight
//!   together) and one barrier.
//! - [`PageBatch`] collects page writes, checks them, and submits them
//!   together before one barrier.
//!
//! Records are staged straight into the buffers the device reads from: an
//! append is copied once, and [`AppendBatch::append_with`] lets the caller
//! encode in place with no copy at all.

use store_io_core::error::{Error, NotWrittenCause, Op};
use store_io_platform::{IoBuf, Platform};

use crate::io::{Chunks, Placed, ctx, ctx_range, not_written};
use crate::mapped::Mapped;
use crate::receipt::{DurableReceipt, RegionPos, WriteTicket};
use crate::region::{AppendRegion, PageRegion};
use crate::settle::{Wrote, settle_pumped};
use crate::store::Store;

/// Many appends written together and made durable with one barrier.
///
/// Records are packed back to back: record `i` starts where record `i - 1`
/// ended, and only the end of the whole batch is padded to a block. Each
/// `append` returns the record's offset within the batch; after a commit,
/// the record's region offset is the commit position's offset plus that.
///
/// A batch reuses its buffer list across commits, so a long-lived batch
/// makes no allocation after its first commit. Staged bytes live in the
/// store's buffer pool: a batch holds at most what the pool can lend
/// (`StoreOptions::buffers_per_class × StoreOptions::max_io`), and an append
/// beyond that fails with `PoolExhausted`, leaving the batch as it was.
///
/// ```
/// # use std::path::Path;
/// # use store_io_engine::{Store, StoreOptions};
/// # use store_io_sim::{SimConfig, SimPlatform};
/// # let platform = SimPlatform::new(SimConfig::volatile(1));
/// # let store = Store::create(platform, Path::new("/db"), StoreOptions::default())?;
/// let wal = store.provision_append_region("wal", 1 << 20)?;
/// let mut batch = wal.batch();
/// let a = batch.append(b"first record")?;
/// let b = batch.append_with(4, |buf| buf.copy_from_slice(b"next"))?;
/// let (pos, receipt) = batch.commit()?; // one write, one barrier
///
/// let mut out = [0u8; 16];
/// wal.read(pos.offset(), &mut out)?;
/// assert_eq!(&out[a as usize..b as usize], b"first record");
/// assert_eq!(&out[b as usize..], b"next");
/// assert!(receipt.durable_through() >= pos.offset() + 16);
/// # Ok::<(), store_io_core::error::Error>(())
/// ```
pub struct AppendBatch<P: Platform> {
    region: AppendRegion<P>,
    /// Staged bytes. Every buffer but the last holds whole blocks.
    chunks: Vec<IoBuf>,
    bytes: u64,
    records: usize,
}

impl<P: Platform> AppendBatch<P>
where
    P::Queue: Send,
{
    pub(crate) fn new(region: AppendRegion<P>) -> Self {
        Self {
            region,
            chunks: Vec::new(),
            bytes: 0,
            records: 0,
        }
    }

    fn store(&self) -> &Store<P> {
        self.region.store()
    }

    /// Bytes still free in the last staged buffer.
    fn room(&self) -> usize {
        let chunk = self.store().chunk();
        self.chunks.last().map_or(0, |b| chunk - b.len())
    }

    /// Starts a new, empty staging buffer.
    fn grow(&mut self) -> Result<(), Error> {
        let mut buf = self
            .store()
            .take(self.store().chunk(), ctx(self.region.state()))?;
        // Length 0 always fits.
        let _empty = buf.set_len(0);
        self.chunks.push(buf);
        Ok(())
    }

    /// Restores the staged length to `len` bytes (undoing a failed append).
    fn truncate(&mut self, len: u64) {
        let mut total: u64 = self.chunks.iter().map(|b| b.len() as u64).sum();
        while let Some(last) = self.chunks.last_mut() {
            let l = last.len() as u64;
            if total - l >= len {
                total -= l;
                let _dropped = self.chunks.pop();
                continue;
            }
            // `len - (total - l) ≤ l`: the length fits.
            let _fits = last.set_len((len - (total - l)) as usize);
            break;
        }
    }

    /// Adds a record (at least one byte) and returns its offset within the
    /// batch. The bytes are copied once, into the buffer the device will
    /// read.
    ///
    /// # Errors
    ///
    /// `NotWritten(Empty)`, or `NotWritten(PoolExhausted)` (the batch is left
    /// exactly as it was).
    pub fn append(&mut self, data: &[u8]) -> Result<u64, Error> {
        if data.is_empty() {
            return Err(not_written(
                NotWrittenCause::Empty,
                ctx(self.region.state()),
            ));
        }
        let at = self.bytes;
        let mut rest = data;
        while !rest.is_empty() {
            let room = self.room();
            if room == 0 {
                if let Err(e) = self.grow() {
                    self.truncate(at);
                    return Err(e);
                }
                continue;
            }
            let n = room.min(rest.len());
            if let Some(last) = self.chunks.last_mut() {
                let l = last.len();
                last.as_mut_capacity()[l..l + n].copy_from_slice(&rest[..n]);
                // `l + n ≤ chunk ≤ capacity`.
                let _fits = last.set_len(l + n);
            }
            rest = &rest[n..];
        }
        self.bytes += data.len() as u64;
        self.records += 1;
        Ok(at)
    }

    /// Adds a record of `len` bytes encoded in place by `fill`, with no copy,
    /// and returns its offset within the batch. `fill` receives a zeroed
    /// slice of exactly `len` bytes inside the buffer the device will read.
    /// If `fill` panics the record is not added.
    ///
    /// # Errors
    ///
    /// `NotWritten(Empty)`, `NotWritten(TooLarge)` beyond
    /// [`Self::max_in_place`], or `NotWritten(PoolExhausted)`.
    pub fn append_with(&mut self, len: usize, fill: impl FnOnce(&mut [u8])) -> Result<u64, Error> {
        let c = ctx(self.region.state());
        if len == 0 {
            return Err(not_written(NotWrittenCause::Empty, c));
        }
        if len > self.max_in_place() {
            return Err(not_written(NotWrittenCause::TooLarge, c));
        }
        if self.room() < len {
            // A record lent in place must be contiguous, so it starts a new
            // buffer. The previous buffer's trailing partial block moves with
            // it, keeping every buffer but the last a whole number of blocks.
            let block = self.store().block() as usize;
            let mut fresh = self.store().take(self.store().chunk(), c)?;
            let mut carried = 0;
            if let Some(last) = self.chunks.last_mut() {
                let l = last.len();
                carried = l % block;
                fresh.as_mut_capacity()[..carried].copy_from_slice(&last.as_slice()[l - carried..]);
                // Shrinking always fits.
                let _fits = last.set_len(l - carried);
            }
            let _fits = fresh.set_len(carried);
            if self.chunks.last().is_some_and(IoBuf::is_empty) {
                let _emptied = self.chunks.pop();
            }
            self.chunks.push(fresh);
        }
        if let Some(last) = self.chunks.last_mut() {
            let l = last.len();
            let slot = &mut last.as_mut_capacity()[l..l + len];
            // Pool memory is reused: never lend stale bytes.
            slot.fill(0);
            fill(slot);
            // `l + len ≤ chunk ≤ capacity` (checked by `room` or the carry).
            let _fits = last.set_len(l + len);
        }
        let at = self.bytes;
        self.bytes += len as u64;
        self.records += 1;
        Ok(at)
    }

    /// The largest record [`Self::append_with`] accepts: the largest pooled
    /// buffer less one block.
    #[must_use]
    pub fn max_in_place(&self) -> usize {
        self.store().chunk() - self.store().block() as usize
    }

    /// Records staged.
    #[must_use]
    pub fn len(&self) -> usize {
        self.records
    }

    /// Whether nothing is staged.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.records == 0
    }

    /// Bytes staged (before the final padding).
    #[must_use]
    pub fn bytes(&self) -> u64 {
        self.bytes
    }

    /// Drops everything staged.
    pub fn clear(&mut self) {
        self.chunks.clear();
        self.bytes = 0;
        self.records = 0;
    }

    /// Writes the batch as one contiguous append and returns where it starts
    /// and a ticket, once every write has completed. Not yet durable: pass
    /// the ticket to [`AppendRegion::sync_through`], or use [`Self::commit`].
    /// The batch is kept if the write fails before space is reserved, and is
    /// empty after every other outcome.
    ///
    /// # Errors
    ///
    /// Before any I/O, the batch kept: `NotWritten(Empty | RegionFull |
    /// ReadOnly | NotPositioned)`, `Io` if the store's queue could not be
    /// prepared, or `Poisoned`. After: `DurabilityUnknown` (the domain is now
    /// poisoned).
    pub fn write(&mut self) -> Result<(RegionPos, WriteTicket), Error> {
        let c = ctx(self.region.state());
        if self.records == 0 {
            return Err(not_written(NotWrittenCause::Empty, c));
        }
        let (f, _pass) = self.region.writable()?;
        let s = self.region.store();
        let state = self.region.state();
        let block = s.block() as usize;
        let (mut lane, reserved) = s.lane_and_reservation(state, f, self.bytes, c)?;
        let r = reserved.range();
        if let Some(last) = self.chunks.last_mut() {
            let l = last.len();
            let padded = l.div_ceil(block) * block;
            last.as_mut_capacity()[l..padded].fill(0);
            // A staged buffer's capacity is a whole number of blocks.
            let _fits = last.set_len(padded);
        }
        let mut src = Chunks {
            bufs: self.chunks.drain(..),
            offset: r.start,
        };
        let outcome = s.pump(&mut lane, &mut Mapped::new(&state.map, &mut src));
        drop(src);
        self.bytes = 0;
        self.records = 0;
        // As `AppendRegion::append`: the settle step completes the
        // reservation or poisons.
        let r = reserved.disarm();
        let ticket = settle_pumped(s, state, Wrote::Append(r), outcome)?;
        Ok((ticket.pos(), ticket))
    }

    /// Writes the batch and makes it durable with one barrier. Returns where
    /// the batch starts and the receipt.
    ///
    /// # Errors
    ///
    /// As [`Self::write`] and [`AppendRegion::sync_through`].
    pub fn commit(&mut self) -> Result<(RegionPos, DurableReceipt), Error> {
        let (pos, ticket) = self.write()?;
        let receipt = self.region.sync_through(&ticket)?;
        Ok((pos, receipt))
    }
}

/// Many page writes submitted together and made durable with one barrier.
///
/// Writes are checked as they are added, copied once into pooled buffers,
/// sorted by position at commit, and kept in flight together. Two writes in
/// one batch must not overlap.
///
/// ```
/// # use std::path::Path;
/// # use store_io_engine::{Store, StoreOptions};
/// # use store_io_sim::{SimConfig, SimPlatform};
/// # let platform = SimPlatform::new(SimConfig::volatile(1));
/// # let store = Store::create(platform, Path::new("/db"), StoreOptions::default())?;
/// let pages = store.provision_page_region("pages", 1 << 20)?;
/// let bs = pages.block_size() as usize;
/// let mut batch = pages.batch();
/// batch.write(pages.pos(8 * bs as u64)?, &vec![2; bs])?;
/// batch.write(pages.pos(0)?, &vec![1; bs])?;
/// let receipt = batch.commit()?; // both writes, one barrier
/// assert_eq!(receipt.durable_through(), 9 * bs as u64);
/// # Ok::<(), store_io_core::error::Error>(())
/// ```
pub struct PageBatch<P: Platform> {
    region: PageRegion<P>,
    /// `(region offset, staged bytes)`.
    writes: Vec<(u64, IoBuf)>,
}

impl<P: Platform> PageBatch<P>
where
    P::Queue: Send,
{
    pub(crate) fn new(region: PageRegion<P>) -> Self {
        Self {
            region,
            writes: Vec::new(),
        }
    }

    /// Adds a write of whole blocks at `pos`. Large writes are split into
    /// pooled pieces.
    ///
    /// # Errors
    ///
    /// `NotWritten` (foreign, stale, misaligned, empty, out of range, pool
    /// exhausted); the batch is left as it was.
    pub fn write(&mut self, pos: RegionPos, data: &[u8]) -> Result<(), Error> {
        let end = self.region.check(&pos, data.len())?;
        let s = self.region.store();
        let c = ctx_range(self.region.state(), pos.offset, end);
        let chunk = s.chunk();
        let before = self.writes.len();
        for (i, piece) in data.chunks(chunk).enumerate() {
            match s.take(piece.len(), c) {
                Ok(mut buf) => {
                    buf.as_mut_slice().copy_from_slice(piece);
                    self.writes.push((pos.offset + (i * chunk) as u64, buf));
                }
                Err(e) => {
                    self.writes.truncate(before);
                    return Err(e);
                }
            }
        }
        Ok(())
    }

    /// Adds a write of `len` bytes (whole blocks, at most one pooled buffer)
    /// encoded in place by `fill`, which receives a zeroed slice inside the
    /// buffer the device will read. If `fill` panics nothing is added.
    ///
    /// # Errors
    ///
    /// As [`Self::write`], and `NotWritten(TooLarge)` beyond one buffer.
    pub fn write_with(
        &mut self,
        pos: RegionPos,
        len: usize,
        fill: impl FnOnce(&mut [u8]),
    ) -> Result<(), Error> {
        let end = self.region.check(&pos, len)?;
        let s = self.region.store();
        let c = ctx_range(self.region.state(), pos.offset, end);
        if len > s.chunk() {
            return Err(not_written(NotWrittenCause::TooLarge, c));
        }
        let mut buf = s.take(len, c)?;
        let slot = buf.as_mut_slice();
        slot.fill(0);
        fill(slot);
        self.writes.push((pos.offset, buf));
        Ok(())
    }

    /// Writes staged.
    #[must_use]
    pub fn len(&self) -> usize {
        self.writes.len()
    }

    /// Whether nothing is staged.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.writes.is_empty()
    }

    /// Drops everything staged.
    pub fn clear(&mut self) {
        self.writes.clear();
    }

    /// Submits every write together and makes them durable with one barrier.
    /// The receipt names the span from the first written byte to the last;
    /// it proves durable every write in the batch (and every other write to
    /// this store that completed before the barrier). The batch is kept if
    /// the commit fails before any I/O (`NotWritten`, `Poisoned`, or `Io`
    /// preparing the queue), and is empty after every other outcome.
    ///
    /// # Errors
    ///
    /// `NotWritten(Empty | Overlap | ReadOnly)` before any I/O (the batch is
    /// kept); `Io` if the store's queue could not be prepared (nothing was
    /// submitted; the batch is kept) or the platform refused the first write
    /// (nothing was written or poisoned; the batch is empty), so
    /// [`Self::is_empty`] tells which; `Poisoned`; or `DurabilityUnknown`.
    pub fn commit(&mut self) -> Result<DurableReceipt, Error> {
        let state = self.region.state();
        let c = ctx(state);
        if self.writes.is_empty() {
            return Err(not_written(NotWrittenCause::Empty, c));
        }
        let s = self.region.store();
        let _pass = self.region.pass()?;
        s.ready_for(state)?;
        self.writes.sort_unstable_by_key(|(at, _)| *at);
        let overlap = self
            .writes
            .windows(2)
            .any(|w| w[0].0 + w[0].1.len() as u64 > w[1].0);
        if overlap {
            return Err(not_written(NotWrittenCause::Overlap, c));
        }
        let start = self.writes.first().map_or(0, |(at, _)| *at);
        let end = self.writes.last().map_or(0, |(at, b)| at + b.len() as u64);
        // Several writes run concurrently on a parallel lane; one stays on
        // the caller's thread (D-096).
        let mut lane = if self.writes.len() > 1 {
            s.par_lane(state, Op::Write)?
        } else {
            s.lane(state, Op::Write)?
        };
        let mut src = Placed {
            writes: self.writes.drain(..),
        };
        let outcome = s.pump(&mut lane, &mut Mapped::new(&state.map, &mut src));
        drop(src);
        // The whole span settles as one write; its receipt follows the
        // barrier below.
        let _span = settle_pumped(s, state, Wrote::Page { start, end }, outcome)?;
        drop(lane);
        let (need, synced) = s.barrier_for(state)?;
        Ok(s.receipt(state, start, end, need, false, synced))
    }
}

impl<P: Platform> core::fmt::Debug for AppendBatch<P> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("AppendBatch")
            .field("region", &self.region)
            .field("records", &self.records)
            .field("bytes", &self.bytes)
            .finish()
    }
}

impl<P: Platform> core::fmt::Debug for PageBatch<P> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("PageBatch")
            .field("region", &self.region)
            .field("writes", &self.writes.len())
            .finish()
    }
}