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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Space: reservations, hard caps and per-tag accounting.
//!
//! A [`Reservation`] sets space aside before it is needed: `reserve` checks
//! the tag's hard cap and grows the container so that provisioning from the
//! reservation can never run out of space. It is the only place where a full
//! file system or quota surfaces as [`Error::NoSpace`], and nothing is ever
//! poisoned by it.
//!
//! The container always holds every region and every outstanding
//! reservation: `reserve` grows it to keep that true, provisioning from a
//! reservation turns reserved bytes into used bytes without allocating, and
//! provisioning without one grows the container past the outstanding
//! reservations so it can never take space promised to someone else.
//!
//! Tags are opaque to store-io (a tenant, a class). Each region records its
//! tag in the region table, so per-tag usage is derived again on every open;
//! caps and outstanding reservations live in memory.

use std::sync::{MutexGuard, PoisonError};

use store_io_core::error::{Error, ErrorContext, NotWrittenCause, ReserveTag};
use store_io_format::meta::{RegionKind, RegionState, TableEntry};
use store_io_platform::Platform;

use crate::io::not_written;
use crate::options::RegionOptions;
use crate::region::{AppendRegion, PageRegion, Slot};
use crate::store::{Meta, Store, lock_meta};

/// Usage of one tag.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TagSpace {
    /// The tag.
    pub tag: u32,
    /// Data bytes of the tag's ready regions.
    pub logical: u64,
    /// Bytes the tag's ready regions occupy in the container, headers
    /// included, and those its provisionings and growths filling now hold
    /// (format 2: charged before their commit).
    pub physical: u64,
    /// Bytes reserved for the tag and not yet provisioned.
    pub reserved: u64,
    /// The tag's hard cap on `physical + reserved`, if any.
    pub cap: Option<u64>,
}

/// Where a store's space goes.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SpaceReport {
    /// Bytes the container file holds.
    pub container: u64,
    /// Bytes held by ready regions, headers included, and by provisionings
    /// and growths filling now (format 2: marked before their commit).
    /// Format 2 also counts an unavailable region whose entry passes the
    /// entry checks, by its capacity and headers (its span stays allocated
    /// either way).
    pub regions: u64,
    /// Format 1: bytes of released regions, reused by a later region of
    /// exactly the same size. Format 2: free bytes inside the container,
    /// usable by any region (the complement of everything allocated, less
    /// what reservations hold).
    pub released: u64,
    /// Bytes reserved and not yet provisioned.
    pub reserved: u64,
    /// Usage per tag, in tag order.
    pub tags: Vec<TagSpace>,
}

/// In-memory accounting, guarded by `Inner::acct`. Lock order: `meta`, then
/// `acct`.
#[derive(Debug, Default)]
pub(crate) struct Accounting {
    tags: Vec<TagSpace>,
    reserved: u64,
}

impl Accounting {
    /// Usage derived from the ready entries of a region table.
    pub(crate) fn from_entries(entries: &[TableEntry], block: u64) -> Self {
        let mut a = Self::default();
        for e in entries.iter().filter(|e| e.state == RegionState::Ready) {
            a.add(e, block);
        }
        a
    }

    /// Usage `(tag, logical, physical)` per format-2 entry.
    /// Sorted first, so many tags cost O(n log n), not an insertion each.
    pub(crate) fn from_usage(usage: &[(u32, u64, u64)]) -> Self {
        let mut sorted = usage.to_vec();
        sorted.sort_unstable_by_key(|u| u.0);
        let mut a = Self::default();
        for (tag, logical, physical) in sorted {
            match a.tags.last_mut() {
                Some(t) if t.tag == tag => {
                    t.logical = t.logical.saturating_add(logical);
                    t.physical = t.physical.saturating_add(physical);
                }
                _ => a.tags.push(TagSpace {
                    tag,
                    logical,
                    physical,
                    ..TagSpace::default()
                }),
            }
        }
        a
    }

    fn tag(&mut self, tag: u32) -> &mut TagSpace {
        let i = match self.tags.binary_search_by_key(&tag, |t| t.tag) {
            Ok(i) => i,
            Err(i) => {
                self.tags.insert(
                    i,
                    TagSpace {
                        tag,
                        ..TagSpace::default()
                    },
                );
                i
            }
        };
        &mut self.tags[i]
    }

    /// Counts a newly provisioned format-2 region.
    pub(crate) fn add_usage(&mut self, tag: u32, logical: u64, physical: u64) {
        let t = self.tag(tag);
        t.logical = t.logical.saturating_add(logical);
        t.physical = t.physical.saturating_add(physical);
    }

    /// Stops counting a released format-2 region.
    pub(crate) fn remove_usage(&mut self, tag: u32, logical: u64, physical: u64) {
        let t = self.tag(tag);
        t.logical = t.logical.saturating_sub(logical);
        t.physical = t.physical.saturating_sub(physical);
    }

    /// Counts a newly ready region.
    pub(crate) fn add(&mut self, e: &TableEntry, block: u64) {
        let physical = e.extent_len(block).unwrap_or(u64::MAX);
        let t = self.tag(e.tag);
        t.logical = t.logical.saturating_add(e.data_size);
        t.physical = t.physical.saturating_add(physical);
    }

    /// Stops counting a released region.
    pub(crate) fn remove(&mut self, e: &TableEntry, block: u64) {
        let physical = e.extent_len(block).unwrap_or(0);
        let t = self.tag(e.tag);
        t.logical = t.logical.saturating_sub(e.data_size);
        t.physical = t.physical.saturating_sub(physical);
    }

    /// Whether `extra` more bytes fit under the tag's cap.
    pub(crate) fn fits(&mut self, tag: u32, extra: u64) -> bool {
        let t = self.tag(tag);
        t.cap.is_none_or(|cap| {
            t.physical
                .checked_add(t.reserved)
                .and_then(|u| u.checked_add(extra))
                .is_some_and(|u| u <= cap)
        })
    }

    /// Bytes reserved and not yet provisioned, all tags.
    pub(crate) fn reserved(&self) -> u64 {
        self.reserved
    }

    /// Moves `bytes` of `tag` from reserved to provisioned (or back to free
    /// when a reservation is dropped).
    pub(crate) fn unreserve(&mut self, tag: u32, bytes: u64) {
        self.reserved = self.reserved.saturating_sub(bytes);
        let t = self.tag(tag);
        t.reserved = t.reserved.saturating_sub(bytes);
    }

    fn reserve(&mut self, tag: u32, bytes: u64) {
        self.reserved = self.reserved.saturating_add(bytes);
        let t = self.tag(tag);
        t.reserved = t.reserved.saturating_add(bytes);
    }
}

pub(crate) fn lock_acct(a: &std::sync::Mutex<Accounting>) -> MutexGuard<'_, Accounting> {
    a.lock().unwrap_or_else(PoisonError::into_inner)
}

pub(crate) fn no_space(tag: u32) -> Error {
    Error::NoSpace {
        tag: ReserveTag::Caller(tag),
        ctx: ErrorContext::default(),
    }
}

/// Space set aside for provisioning under one tag. Regions provisioned from
/// it can never fail for lack of space; whatever is left when it is dropped
/// returns to the free pool.
pub struct Reservation<P: Platform>
where
    P::Queue: Send,
{
    store: Store<P>,
    tag: u32,
    bytes: u64,
}

impl<P: Platform> core::fmt::Debug for Reservation<P>
where
    P::Queue: Send,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Reservation")
            .field("tag", &self.tag)
            .field("remaining", &self.bytes)
            .finish()
    }
}

impl<P: Platform> Drop for Reservation<P>
where
    P::Queue: Send,
{
    fn drop(&mut self) {
        lock_acct(&self.store.inner.acct).unreserve(self.tag, self.bytes);
    }
}

impl<P: Platform> Reservation<P>
where
    P::Queue: Send,
{
    /// The reservation's tag.
    #[must_use]
    pub fn tag(&self) -> u32 {
        self.tag
    }

    /// Bytes still reserved. A region uses its data size plus two header
    /// blocks (see [`Store::extent_of`]).
    #[must_use]
    pub fn remaining(&self) -> u64 {
        self.bytes
    }

    /// Sets `extent` bytes aside for a format-2 provisioning in flight: no
    /// longer this reservation's to give, still counted reserved in the
    /// store's accounting until the commit moves them to the region (so
    /// every placement meanwhile sizes the container past them).
    pub(crate) fn hold(&mut self, extent: u64) -> Result<(), Error> {
        if extent > self.bytes {
            return Err(no_space(self.tag));
        }
        self.bytes -= extent;
        Ok(())
    }

    /// Gives back `extent` bytes [`Self::hold`] set aside (a provisioning
    /// that failed before its commit).
    pub(crate) fn unhold(&mut self, extent: u64) {
        self.bytes += extent;
    }

    /// Takes `extent` bytes for a region being provisioned.
    pub(crate) fn consume(&mut self, extent: u64) -> Result<(), Error> {
        if extent > self.bytes {
            return Err(no_space(self.tag));
        }
        self.bytes -= extent;
        lock_acct(&self.store.inner.acct).unreserve(self.tag, extent);
        Ok(())
    }

    /// Provisions an append region from this reservation.
    ///
    /// # Errors
    ///
    /// As [`Store::provision_append_region`]; `NoSpace` if the reservation
    /// is too small.
    pub fn provision_append_region(
        &mut self,
        name: &str,
        size: u64,
    ) -> Result<AppendRegion<P>, Error> {
        self.provision_append_region_with(name, RegionOptions::default().size(size))
    }

    /// Provisions an append region from this reservation as `opts` say (a
    /// capped region takes its whole capacity from it).
    ///
    /// # Errors
    ///
    /// As [`Store::provision_append_region_with`]; `NoSpace` if the
    /// reservation is too small.
    pub fn provision_append_region_with(
        &mut self,
        name: &str,
        opts: RegionOptions,
    ) -> Result<AppendRegion<P>, Error> {
        let store = self.store.clone();
        let state = store.provision(name, RegionKind::Append, opts, Some(self))?;
        Ok(AppendRegion::from_parts(store, state))
    }

    /// Provisions a page region from this reservation.
    ///
    /// # Errors
    ///
    /// As [`Store::provision_page_region`]; `NoSpace` if the reservation is
    /// too small.
    pub fn provision_page_region(&mut self, name: &str, size: u64) -> Result<PageRegion<P>, Error> {
        self.provision_page_region_with(name, RegionOptions::default().size(size))
    }

    /// Provisions an append region from this reservation with the store's
    /// [`StoreOptions::region_defaults`](crate::StoreOptions::region_defaults).
    ///
    /// # Errors
    ///
    /// As [`Store::provision_append_region_default`]; `NoSpace` if the
    /// reservation is too small.
    pub fn provision_append_region_default(
        &mut self,
        name: &str,
    ) -> Result<AppendRegion<P>, Error> {
        let opts = self.store.region_defaults()?;
        self.provision_append_region_with(name, opts)
    }

    /// Provisions a page region from this reservation with the store's
    /// [`StoreOptions::region_defaults`](crate::StoreOptions::region_defaults).
    ///
    /// # Errors
    ///
    /// As [`Self::provision_append_region_default`].
    pub fn provision_page_region_default(&mut self, name: &str) -> Result<PageRegion<P>, Error> {
        let opts = self.store.region_defaults()?;
        self.provision_page_region_with(name, opts)
    }

    /// Provisions a page region from this reservation as `opts` say.
    ///
    /// # Errors
    ///
    /// As [`Self::provision_append_region_with`].
    pub fn provision_page_region_with(
        &mut self,
        name: &str,
        opts: RegionOptions,
    ) -> Result<PageRegion<P>, Error> {
        let store = self.store.clone();
        let state = store.provision(name, RegionKind::Page, opts, Some(self))?;
        Ok(PageRegion::from_parts(store, state))
    }

    /// Provisions a small-object slot from this reservation.
    ///
    /// # Errors
    ///
    /// As [`Store::provision_slot`]; `NoSpace` if the reservation is too
    /// small.
    pub fn provision_slot(&mut self, name: &str) -> Result<Slot<P>, Error> {
        let store = self.store.clone();
        let state =
            store.provision(name, RegionKind::Slot, RegionOptions::default(), Some(self))?;
        Ok(Slot::from_parts(store, state))
    }
}

impl<P: Platform> Store<P>
where
    P::Queue: Send,
{
    /// Bytes a region of `data_size` data bytes occupies in the container:
    /// the data size rounded up to the block, plus two header blocks.
    #[must_use]
    pub fn extent_of(&self, data_size: u64) -> Option<u64> {
        let block = self.block();
        store_io_core::align::align_up(data_size, block).and_then(|d| self.inner.layout.extent(d))
    }

    /// Sets (or with `None` removes) the hard cap of `tag`: the most bytes
    /// its regions and reservations may hold together. Caps live in memory;
    /// set them again after every open.
    pub fn set_cap(&self, tag: u32, cap: Option<u64>) {
        lock_acct(&self.inner.acct).tag(tag).cap = cap;
    }

    /// Reserves `bytes` for later provisioning under `tag`: checks the tag's
    /// cap and grows the container so that provisioning from the reservation
    /// cannot run out of space.
    ///
    /// # Errors
    ///
    /// `NoSpace { tag }` if the cap or the file system is exceeded (nothing
    /// changes and nothing is poisoned); `NotWritten(ReadOnly)`; `Poisoned`;
    /// `Io` for other allocation failures.
    pub fn reserve(&self, bytes: u64, tag: u32) -> Result<Reservation<P>, Error> {
        self.ready()?;
        let inner = &self.inner;
        if inner.frozen.load(std::sync::atomic::Ordering::Acquire) {
            // Allocation is frozen (spec §9 step 2).
            return Err(not_written(
                NotWrittenCause::Frozen,
                ErrorContext::default(),
            ));
        }
        let mut meta = lock_meta(&inner.meta);
        let mut acct = lock_acct(&inner.acct);
        if !acct.fits(tag, bytes) {
            return Err(no_space(tag));
        }
        let target = self
            .tail(&meta)
            .checked_add(acct.reserved())
            .and_then(|t| t.checked_add(bytes))
            .ok_or_else(|| not_written(NotWrittenCause::TooLarge, ErrorContext::default()))?;
        self.grow_to(&mut meta, target, tag)?;
        acct.reserve(tag, bytes);
        Ok(Reservation {
            store: self.clone(),
            tag,
            bytes,
        })
    }

    /// Where space goes: the container, regions, released extents,
    /// reservations, and usage per tag.
    #[must_use]
    pub fn space(&self) -> SpaceReport {
        let inner = &self.inner;
        let meta = lock_meta(&inner.meta);
        let acct = lock_acct(&inner.acct);
        let block = self.block();
        let sum = |state| {
            meta.entries
                .iter()
                .filter(|e| e.state == state)
                .filter_map(|e| e.extent_len(block))
                .sum()
        };
        if let Some(v2) = meta.v2.as_deref() {
            return SpaceReport {
                container: meta.container_len,
                // Marked by provisionings and growths filling now: in use.
                regions: v2.usage.iter().map(|u| u.2).sum::<u64>() + v2.marked,
                // The trailing free space holds the reservations: not
                // free for anyone else.
                released: v2.free.free_bytes().saturating_sub(acct.reserved()),
                reserved: acct.reserved(),
                tags: acct.tags.clone(),
            };
        }
        SpaceReport {
            container: meta.container_len,
            regions: sum(RegionState::Ready),
            released: sum(RegionState::Released),
            reserved: acct.reserved(),
            tags: acct.tags.clone(),
        }
    }

    /// The end of the last extent in the table (the start of free tail
    /// space).
    pub(crate) fn tail(&self, meta: &Meta) -> u64 {
        match meta.v2.as_deref() {
            Some(v2) => v2.free.end(),
            None => self.inner.layout.tail(&meta.entries),
        }
    }

    /// Grows the container to at least `target` bytes.
    ///
    /// Refused with `Corruption(Truncated)` while a region the open found cut
    /// off by a short container exists: growth would zero-fill that region's
    /// missing tail, and the next open would find it whole and ready, its
    /// lost bytes read as zeros (an append region would silently end early).
    ///
    /// Only format 2 can have one here: a format-1 store with a region cut
    /// short opens read-only (from 0.5 every open of format 1 does), and a
    /// read-only store never grows; a store created as format 1 has none.
    pub(crate) fn grow_to(&self, meta: &mut Meta, target: u64, tag: u32) -> Result<(), Error> {
        if target <= meta.container_len {
            return Ok(());
        }
        let inner = &self.inner;
        // Any format-2 entry past the file's end: one listed as unavailable
        // (`ContainerShort`) and one whose name does not decode, which has
        // no listed region. Growing would make zeros of its lost tail.
        if meta.v2.as_deref().is_some_and(|v| v.cut_short) {
            return Err(Error::Corruption {
                kind: store_io_core::error::CorruptionKind::Truncated,
                ctx: ErrorContext::default(),
            });
        }
        inner
            .platform
            .allocate(&inner.file, target)
            .map_err(|raw| crate::store::space_err(store_io_core::error::Op::Allocate, raw, tag))?;
        meta.container_len = target;
        if let Some(v2) = meta.v2.as_deref_mut() {
            // The new space past the old end is free.
            v2.rebuild_free(target);
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use std::path::Path;
    use std::time::{Duration, Instant};

    use store_io_sim::{SimConfig, SimPlatform};

    use super::lock_acct;
    use crate::store::{Store, StoreOptions};

    /// The lock order is metadata, then accounting, everywhere. Checked
    /// deterministically: with the accounting lock held here, a `reserve`
    /// on another thread must take the metadata lock first and then wait.
    /// In the reverse order it would wait on accounting holding nothing,
    /// and a thread taking them in the documented order could deadlock with
    /// it (the timing-based test alone missed this on a CI runner).
    #[test]
    fn test_reserve_takes_the_metadata_lock_before_the_accounting_lock() {
        let p = SimPlatform::new(SimConfig::power_safe(42));
        let s = Store::create(p, Path::new("/db"), StoreOptions::default()).unwrap();
        let acct = lock_acct(&s.inner.acct);
        let worker = {
            let s = s.clone();
            std::thread::spawn(move || drop(s.reserve(4096, 1)))
        };
        let deadline = Instant::now() + Duration::from_secs(30);
        let mut meta_taken = false;
        while Instant::now() < deadline {
            if s.inner.meta.try_lock().is_err() {
                meta_taken = true;
                break;
            }
            std::thread::sleep(Duration::from_millis(1));
        }
        drop(acct);
        worker.join().unwrap();
        assert!(
            meta_taken,
            "reserve waited for the accounting lock without holding the metadata lock"
        );
    }
}