1use std::sync::{MutexGuard, PoisonError};
20
21use store_io_core::error::{Error, ErrorContext, NotWrittenCause, ReserveTag};
22use store_io_format::meta::{RegionKind, RegionState, TableEntry};
23use store_io_platform::Platform;
24
25use crate::io::not_written;
26use crate::options::RegionOptions;
27use crate::region::{AppendRegion, PageRegion, Slot};
28use crate::store::{Meta, Store, lock_meta};
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
32pub struct TagSpace {
33 pub tag: u32,
35 pub logical: u64,
37 pub physical: u64,
41 pub reserved: u64,
43 pub cap: Option<u64>,
45}
46
47#[derive(Debug, Clone, PartialEq, Eq, Default)]
49pub struct SpaceReport {
50 pub container: u64,
52 pub regions: u64,
58 pub released: u64,
63 pub reserved: u64,
65 pub tags: Vec<TagSpace>,
67}
68
69#[derive(Debug, Default)]
72pub(crate) struct Accounting {
73 tags: Vec<TagSpace>,
74 reserved: u64,
75}
76
77impl Accounting {
78 pub(crate) fn from_entries(entries: &[TableEntry], block: u64) -> Self {
80 let mut a = Self::default();
81 for e in entries.iter().filter(|e| e.state == RegionState::Ready) {
82 a.add(e, block);
83 }
84 a
85 }
86
87 pub(crate) fn from_usage(usage: &[(u32, u64, u64)]) -> Self {
90 let mut sorted = usage.to_vec();
91 sorted.sort_unstable_by_key(|u| u.0);
92 let mut a = Self::default();
93 for (tag, logical, physical) in sorted {
94 match a.tags.last_mut() {
95 Some(t) if t.tag == tag => {
96 t.logical = t.logical.saturating_add(logical);
97 t.physical = t.physical.saturating_add(physical);
98 }
99 _ => a.tags.push(TagSpace {
100 tag,
101 logical,
102 physical,
103 ..TagSpace::default()
104 }),
105 }
106 }
107 a
108 }
109
110 fn tag(&mut self, tag: u32) -> &mut TagSpace {
111 let i = match self.tags.binary_search_by_key(&tag, |t| t.tag) {
112 Ok(i) => i,
113 Err(i) => {
114 self.tags.insert(
115 i,
116 TagSpace {
117 tag,
118 ..TagSpace::default()
119 },
120 );
121 i
122 }
123 };
124 &mut self.tags[i]
125 }
126
127 pub(crate) fn add_usage(&mut self, tag: u32, logical: u64, physical: u64) {
129 let t = self.tag(tag);
130 t.logical = t.logical.saturating_add(logical);
131 t.physical = t.physical.saturating_add(physical);
132 }
133
134 pub(crate) fn remove_usage(&mut self, tag: u32, logical: u64, physical: u64) {
136 let t = self.tag(tag);
137 t.logical = t.logical.saturating_sub(logical);
138 t.physical = t.physical.saturating_sub(physical);
139 }
140
141 pub(crate) fn add(&mut self, e: &TableEntry, block: u64) {
143 let physical = e.extent_len(block).unwrap_or(u64::MAX);
144 let t = self.tag(e.tag);
145 t.logical = t.logical.saturating_add(e.data_size);
146 t.physical = t.physical.saturating_add(physical);
147 }
148
149 pub(crate) fn remove(&mut self, e: &TableEntry, block: u64) {
151 let physical = e.extent_len(block).unwrap_or(0);
152 let t = self.tag(e.tag);
153 t.logical = t.logical.saturating_sub(e.data_size);
154 t.physical = t.physical.saturating_sub(physical);
155 }
156
157 pub(crate) fn fits(&mut self, tag: u32, extra: u64) -> bool {
159 let t = self.tag(tag);
160 t.cap.is_none_or(|cap| {
161 t.physical
162 .checked_add(t.reserved)
163 .and_then(|u| u.checked_add(extra))
164 .is_some_and(|u| u <= cap)
165 })
166 }
167
168 pub(crate) fn reserved(&self) -> u64 {
170 self.reserved
171 }
172
173 pub(crate) fn unreserve(&mut self, tag: u32, bytes: u64) {
176 self.reserved = self.reserved.saturating_sub(bytes);
177 let t = self.tag(tag);
178 t.reserved = t.reserved.saturating_sub(bytes);
179 }
180
181 fn reserve(&mut self, tag: u32, bytes: u64) {
182 self.reserved = self.reserved.saturating_add(bytes);
183 let t = self.tag(tag);
184 t.reserved = t.reserved.saturating_add(bytes);
185 }
186}
187
188pub(crate) fn lock_acct(a: &std::sync::Mutex<Accounting>) -> MutexGuard<'_, Accounting> {
189 a.lock().unwrap_or_else(PoisonError::into_inner)
190}
191
192pub(crate) fn no_space(tag: u32) -> Error {
193 Error::NoSpace {
194 tag: ReserveTag::Caller(tag),
195 ctx: ErrorContext::default(),
196 }
197}
198
199pub struct Reservation<P: Platform>
203where
204 P::Queue: Send,
205{
206 store: Store<P>,
207 tag: u32,
208 bytes: u64,
209}
210
211impl<P: Platform> core::fmt::Debug for Reservation<P>
212where
213 P::Queue: Send,
214{
215 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
216 f.debug_struct("Reservation")
217 .field("tag", &self.tag)
218 .field("remaining", &self.bytes)
219 .finish()
220 }
221}
222
223impl<P: Platform> Drop for Reservation<P>
224where
225 P::Queue: Send,
226{
227 fn drop(&mut self) {
228 lock_acct(&self.store.inner.acct).unreserve(self.tag, self.bytes);
229 }
230}
231
232impl<P: Platform> Reservation<P>
233where
234 P::Queue: Send,
235{
236 #[must_use]
238 pub fn tag(&self) -> u32 {
239 self.tag
240 }
241
242 #[must_use]
245 pub fn remaining(&self) -> u64 {
246 self.bytes
247 }
248
249 pub(crate) fn hold(&mut self, extent: u64) -> Result<(), Error> {
254 if extent > self.bytes {
255 return Err(no_space(self.tag));
256 }
257 self.bytes -= extent;
258 Ok(())
259 }
260
261 pub(crate) fn unhold(&mut self, extent: u64) {
264 self.bytes += extent;
265 }
266
267 pub(crate) fn consume(&mut self, extent: u64) -> Result<(), Error> {
269 if extent > self.bytes {
270 return Err(no_space(self.tag));
271 }
272 self.bytes -= extent;
273 lock_acct(&self.store.inner.acct).unreserve(self.tag, extent);
274 Ok(())
275 }
276
277 pub fn provision_append_region(
284 &mut self,
285 name: &str,
286 size: u64,
287 ) -> Result<AppendRegion<P>, Error> {
288 self.provision_append_region_with(name, RegionOptions::default().size(size))
289 }
290
291 pub fn provision_append_region_with(
299 &mut self,
300 name: &str,
301 opts: RegionOptions,
302 ) -> Result<AppendRegion<P>, Error> {
303 let store = self.store.clone();
304 let state = store.provision(name, RegionKind::Append, opts, Some(self))?;
305 Ok(AppendRegion::from_parts(store, state))
306 }
307
308 pub fn provision_page_region(&mut self, name: &str, size: u64) -> Result<PageRegion<P>, Error> {
315 self.provision_page_region_with(name, RegionOptions::default().size(size))
316 }
317
318 pub fn provision_append_region_default(
326 &mut self,
327 name: &str,
328 ) -> Result<AppendRegion<P>, Error> {
329 let opts = self.store.region_defaults()?;
330 self.provision_append_region_with(name, opts)
331 }
332
333 pub fn provision_page_region_default(&mut self, name: &str) -> Result<PageRegion<P>, Error> {
340 let opts = self.store.region_defaults()?;
341 self.provision_page_region_with(name, opts)
342 }
343
344 pub fn provision_page_region_with(
350 &mut self,
351 name: &str,
352 opts: RegionOptions,
353 ) -> Result<PageRegion<P>, Error> {
354 let store = self.store.clone();
355 let state = store.provision(name, RegionKind::Page, opts, Some(self))?;
356 Ok(PageRegion::from_parts(store, state))
357 }
358
359 pub fn provision_slot(&mut self, name: &str) -> Result<Slot<P>, Error> {
366 let store = self.store.clone();
367 let state =
368 store.provision(name, RegionKind::Slot, RegionOptions::default(), Some(self))?;
369 Ok(Slot::from_parts(store, state))
370 }
371}
372
373impl<P: Platform> Store<P>
374where
375 P::Queue: Send,
376{
377 #[must_use]
380 pub fn extent_of(&self, data_size: u64) -> Option<u64> {
381 let block = self.block();
382 store_io_core::align::align_up(data_size, block).and_then(|d| self.inner.layout.extent(d))
383 }
384
385 pub fn set_cap(&self, tag: u32, cap: Option<u64>) {
389 lock_acct(&self.inner.acct).tag(tag).cap = cap;
390 }
391
392 pub fn reserve(&self, bytes: u64, tag: u32) -> Result<Reservation<P>, Error> {
402 self.ready()?;
403 let inner = &self.inner;
404 if inner.frozen.load(std::sync::atomic::Ordering::Acquire) {
405 return Err(not_written(
407 NotWrittenCause::Frozen,
408 ErrorContext::default(),
409 ));
410 }
411 let mut meta = lock_meta(&inner.meta);
412 let mut acct = lock_acct(&inner.acct);
413 if !acct.fits(tag, bytes) {
414 return Err(no_space(tag));
415 }
416 let target = self
417 .tail(&meta)
418 .checked_add(acct.reserved())
419 .and_then(|t| t.checked_add(bytes))
420 .ok_or_else(|| not_written(NotWrittenCause::TooLarge, ErrorContext::default()))?;
421 self.grow_to(&mut meta, target, tag)?;
422 acct.reserve(tag, bytes);
423 Ok(Reservation {
424 store: self.clone(),
425 tag,
426 bytes,
427 })
428 }
429
430 #[must_use]
433 pub fn space(&self) -> SpaceReport {
434 let inner = &self.inner;
435 let meta = lock_meta(&inner.meta);
436 let acct = lock_acct(&inner.acct);
437 let block = self.block();
438 let sum = |state| {
439 meta.entries
440 .iter()
441 .filter(|e| e.state == state)
442 .filter_map(|e| e.extent_len(block))
443 .sum()
444 };
445 if let Some(v2) = meta.v2.as_deref() {
446 return SpaceReport {
447 container: meta.container_len,
448 regions: v2.usage.iter().map(|u| u.2).sum::<u64>() + v2.marked,
450 released: v2.free.free_bytes().saturating_sub(acct.reserved()),
453 reserved: acct.reserved(),
454 tags: acct.tags.clone(),
455 };
456 }
457 SpaceReport {
458 container: meta.container_len,
459 regions: sum(RegionState::Ready),
460 released: sum(RegionState::Released),
461 reserved: acct.reserved(),
462 tags: acct.tags.clone(),
463 }
464 }
465
466 pub(crate) fn tail(&self, meta: &Meta) -> u64 {
469 match meta.v2.as_deref() {
470 Some(v2) => v2.free.end(),
471 None => self.inner.layout.tail(&meta.entries),
472 }
473 }
474
475 pub(crate) fn grow_to(&self, meta: &mut Meta, target: u64, tag: u32) -> Result<(), Error> {
486 if target <= meta.container_len {
487 return Ok(());
488 }
489 let inner = &self.inner;
490 if meta.v2.as_deref().is_some_and(|v| v.cut_short) {
494 return Err(Error::Corruption {
495 kind: store_io_core::error::CorruptionKind::Truncated,
496 ctx: ErrorContext::default(),
497 });
498 }
499 inner
500 .platform
501 .allocate(&inner.file, target)
502 .map_err(|raw| crate::store::space_err(store_io_core::error::Op::Allocate, raw, tag))?;
503 meta.container_len = target;
504 if let Some(v2) = meta.v2.as_deref_mut() {
505 v2.rebuild_free(target);
507 }
508 Ok(())
509 }
510}
511
512#[cfg(test)]
513mod tests {
514 #![allow(clippy::unwrap_used)]
515
516 use std::path::Path;
517 use std::time::{Duration, Instant};
518
519 use store_io_sim::{SimConfig, SimPlatform};
520
521 use super::lock_acct;
522 use crate::store::{Store, StoreOptions};
523
524 #[test]
531 fn test_reserve_takes_the_metadata_lock_before_the_accounting_lock() {
532 let p = SimPlatform::new(SimConfig::power_safe(42));
533 let s = Store::create(p, Path::new("/db"), StoreOptions::default()).unwrap();
534 let acct = lock_acct(&s.inner.acct);
535 let worker = {
536 let s = s.clone();
537 std::thread::spawn(move || drop(s.reserve(4096, 1)))
538 };
539 let deadline = Instant::now() + Duration::from_secs(30);
540 let mut meta_taken = false;
541 while Instant::now() < deadline {
542 if s.inner.meta.try_lock().is_err() {
543 meta_taken = true;
544 break;
545 }
546 std::thread::sleep(Duration::from_millis(1));
547 }
548 drop(acct);
549 worker.join().unwrap();
550 assert!(
551 meta_taken,
552 "reserve waited for the accounting lock without holding the metadata lock"
553 );
554 }
555}