#![allow(dead_code)]
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::fs::{self, File, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(unix)]
use std::os::unix::fs::{DirBuilderExt, FileExt, MetadataExt, OpenOptionsExt, PermissionsExt};
pub const BLOCK_POSITIONS: usize = 64;
pub const FULL_STRIDE: usize = 2 * 1024 * 1024;
pub const WINDOW_STRIDE: usize = 10 * 1024 * 1024;
pub const DEFAULT_MAX_SPILL_BYTES: u64 = 64 * 1024 * 1024 * 1024;
pub const MAX_RECURRENT_STATE_BYTES: usize = 16 * 1024 * 1024;
pub const MAX_KV_PAYLOAD_BYTES: usize = 64 * 1024 * 1024;
pub const SPILL_MAGIC: [u8; 8] = *b"INKYSPIL";
pub const SPILL_FORMAT_VERSION: u32 = 1;
pub const SPILL_HEADER_LEN: usize = 80;
const HEADER_PREFIX_LEN: usize = 48;
static TEMP_NONCE: AtomicU64 = AtomicU64::new(1);
#[derive(Clone)]
pub struct Sha256 {
state: [u32; 8],
buf: [u8; 64],
buf_len: usize,
total_len: u64,
}
const SHA256_H0: [u32; 8] = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
];
const SHA256_K: [u32; 64] = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
];
impl Sha256 {
pub fn new() -> Self {
Self {
state: SHA256_H0,
buf: [0u8; 64],
buf_len: 0,
total_len: 0,
}
}
pub fn update(&mut self, mut data: &[u8]) {
self.total_len = self.total_len.wrapping_add(data.len() as u64);
if self.buf_len > 0 {
let take = (64 - self.buf_len).min(data.len());
self.buf[self.buf_len..self.buf_len + take].copy_from_slice(&data[..take]);
self.buf_len += take;
data = &data[take..];
if self.buf_len == 64 {
let block = self.buf;
self.compress(&block);
self.buf_len = 0;
}
}
while data.len() >= 64 {
let block: [u8; 64] = data[..64].try_into().unwrap();
self.compress(&block);
data = &data[64..];
}
if !data.is_empty() {
self.buf[..data.len()].copy_from_slice(data);
self.buf_len = data.len();
}
}
pub fn finalize(mut self) -> [u8; 32] {
let bit_len = self.total_len.wrapping_mul(8);
self.buf[self.buf_len] = 0x80;
self.buf_len += 1;
if self.buf_len > 56 {
self.buf[self.buf_len..64].fill(0);
let block = self.buf;
self.compress(&block);
self.buf_len = 0;
}
self.buf[self.buf_len..56].fill(0);
self.buf[56..64].copy_from_slice(&bit_len.to_be_bytes());
let block = self.buf;
self.compress(&block);
let mut out = [0u8; 32];
for (i, word) in self.state.iter().enumerate() {
out[i * 4..(i + 1) * 4].copy_from_slice(&word.to_be_bytes());
}
out
}
fn compress(&mut self, block: &[u8; 64]) {
let mut w = [0u32; 64];
for i in 0..16 {
w[i] = u32::from_be_bytes([
block[i * 4],
block[i * 4 + 1],
block[i * 4 + 2],
block[i * 4 + 3],
]);
}
for i in 16..64 {
let s0 = w[i - 15].rotate_right(7) ^ w[i - 15].rotate_right(18) ^ (w[i - 15] >> 3);
let s1 = w[i - 2].rotate_right(17) ^ w[i - 2].rotate_right(19) ^ (w[i - 2] >> 10);
w[i] = w[i - 16].wrapping_add(s0).wrapping_add(w[i - 7]).wrapping_add(s1);
}
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = self.state;
for i in 0..64 {
let s1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
let ch = (e & f) ^ ((!e) & g);
let t1 = h.wrapping_add(s1).wrapping_add(ch).wrapping_add(SHA256_K[i]).wrapping_add(w[i]);
let s0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
let maj = (a & b) ^ (a & c) ^ (b & c);
let t2 = s0.wrapping_add(maj);
h = g;
g = f;
f = e;
e = d.wrapping_add(t1);
d = c;
c = b;
b = a;
a = t1.wrapping_add(t2);
}
self.state[0] = self.state[0].wrapping_add(a);
self.state[1] = self.state[1].wrapping_add(b);
self.state[2] = self.state[2].wrapping_add(c);
self.state[3] = self.state[3].wrapping_add(d);
self.state[4] = self.state[4].wrapping_add(e);
self.state[5] = self.state[5].wrapping_add(f);
self.state[6] = self.state[6].wrapping_add(g);
self.state[7] = self.state[7].wrapping_add(h);
}
}
fn ct_eq_32(a: &[u8; 32], b: &[u8; 32]) -> bool {
let mut diff = 0u8;
for i in 0..32 {
diff |= a[i] ^ b[i];
}
diff == 0
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
#[repr(u32)]
pub enum SpillBlockKind {
Full = 0,
Window = 1,
}
impl SpillBlockKind {
pub fn from_u32(v: u32) -> Option<Self> {
match v {
0 => Some(Self::Full),
1 => Some(Self::Window),
_ => None,
}
}
pub fn tag(self) -> char {
match self {
Self::Full => 'f',
Self::Window => 'w',
}
}
pub fn default_stride(self) -> usize {
match self {
Self::Full => FULL_STRIDE,
Self::Window => WINDOW_STRIDE,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub struct SpillKey {
pub kind: SpillBlockKind,
pub id: u64,
}
impl SpillKey {
pub const fn new(kind: SpillBlockKind, id: u64) -> Self {
Self { kind, id }
}
pub const fn full(id: u64) -> Self {
Self { kind: SpillBlockKind::Full, id }
}
pub const fn window(id: u64) -> Self {
Self { kind: SpillBlockKind::Window, id }
}
fn blk_filename(self) -> String {
format!("blk-{}-{:016x}.blk", self.kind.tag(), self.id)
}
fn tmp_filename(self, pid: u32, nonce: u64) -> String {
format!("blk-{}-{:016x}.tmp.{:08x}.{:016x}", self.kind.tag(), self.id, pid, nonce)
}
fn parse_blk_filename(name: &str) -> Option<Self> {
let rest = name.strip_prefix("blk-")?;
let rest = rest.strip_suffix(".blk")?;
let (tag, hex) = rest.split_once('-')?;
if hex.len() != 16 {
return None;
}
let kind = match tag {
"f" => SpillBlockKind::Full,
"w" => SpillBlockKind::Window,
_ => return None,
};
let id = u64::from_str_radix(hex, 16).ok()?;
Some(Self { kind, id })
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum BlockTier {
None,
Memory,
Disk,
Both,
}
impl BlockTier {
pub fn in_memory(self) -> bool {
matches!(self, Self::Memory | Self::Both)
}
pub fn on_disk(self) -> bool {
matches!(self, Self::Disk | Self::Both)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ResidentSlot {
pub kind: SpillBlockKind,
pub slot: u32,
pub generation: u32,
pub offset: u64,
}
#[derive(Debug)]
pub enum SpillError {
Io(io::Error),
InvalidDir(String),
Pool(String),
InvalidLength { expected: usize, got: usize },
PayloadTooLarge(usize),
}
impl fmt::Display for SpillError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "spill I/O error: {e}"),
Self::InvalidDir(s) => write!(f, "invalid spill directory: {s}"),
Self::Pool(s) => write!(f, "pool error: {s}"),
Self::InvalidLength { expected, got } => {
write!(f, "payload length mismatch: expected {expected}, got {got}")
}
Self::PayloadTooLarge(n) => write!(f, "payload length {n} exceeds safety cap"),
}
}
}
impl From<io::Error> for SpillError {
fn from(e: io::Error) -> Self {
Self::Io(e)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum MissReason {
NotFound,
NotRegularFile,
TruncatedHeader,
BadMagic,
UnsupportedVersion,
BadBlockKind,
MetadataMismatch,
LengthMismatch,
SizeMismatch,
PayloadTooLarge,
ChecksumMismatch,
IoReadError,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum WriteOutcome {
Written { on_disk_bytes: u64 },
OverBudget,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct RestoredBlock {
pub key: SpillKey,
pub generation: u64,
pub kv_bytes: Vec<u8>,
pub recurrent_state: Option<Vec<u8>>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ReadOutcome {
Hit(RestoredBlock),
Miss(MissReason),
}
pub trait SpillPoolBackend {
fn stride_bytes(&self, kind: SpillBlockKind) -> usize;
fn capacity_slots(&self, kind: SpillBlockKind) -> usize;
fn used_slots(&self, kind: SpillBlockKind) -> usize;
fn free_slots(&self, kind: SpillBlockKind) -> usize;
fn read_slot_bytes(&self, slot: ResidentSlot) -> Result<&[u8], SpillError>;
fn take_free_slot(&mut self, kind: SpillBlockKind) -> Result<ResidentSlot, SpillError>;
fn release_cold_slot(&mut self, slot: ResidentSlot) -> Result<(), SpillError>;
fn install_restored_slot(&mut self, slot: ResidentSlot, kv_bytes: &[u8]) -> Result<(), SpillError>;
fn add_ref(&mut self, slot: ResidentSlot) -> Result<u32, SpillError>;
fn drop_ref(&mut self, slot: ResidentSlot) -> Result<u32, SpillError>;
fn refs(&self, slot: ResidentSlot) -> Result<u32, SpillError>;
}
#[derive(Clone, Debug)]
struct HostSlot {
generation: u32,
occupied: bool,
sealed: bool,
refs: u32,
bytes: Vec<u8>,
}
#[derive(Clone, Debug)]
struct HostKindPool {
kind: SpillBlockKind,
stride: usize,
slots: Vec<HostSlot>,
used: usize,
}
impl HostKindPool {
fn new(kind: SpillBlockKind, capacity: usize, stride: usize) -> Self {
let slots = (0..capacity)
.map(|_| HostSlot {
generation: 0,
occupied: false,
sealed: false,
refs: 0,
bytes: vec![0u8; stride],
})
.collect();
Self { kind, stride, slots, used: 0 }
}
fn slot_index(&self, slot: ResidentSlot) -> Result<usize, SpillError> {
if slot.kind != self.kind {
return Err(SpillError::Pool("block kind mismatch".into()));
}
if slot.slot == 0 {
return Err(SpillError::Pool("slot 0 is invalid".into()));
}
let idx = (slot.slot - 1) as usize;
if idx >= self.slots.len() {
return Err(SpillError::Pool("slot out of range".into()));
}
let rec = &self.slots[idx];
if !rec.occupied || rec.generation != slot.generation {
return Err(SpillError::Pool("stale slot generation".into()));
}
Ok(idx)
}
fn take_free(&mut self) -> Result<ResidentSlot, SpillError> {
for (idx, rec) in self.slots.iter_mut().enumerate() {
if !rec.occupied && rec.refs == 0 {
let next_gen = rec
.generation
.checked_add(1)
.ok_or_else(|| SpillError::Pool("generation exhausted".into()))?;
rec.bytes.fill(0);
rec.generation = next_gen;
rec.occupied = true;
rec.sealed = false;
rec.refs = 0;
self.used += 1;
let offset = (idx as u64)
.checked_mul(self.stride as u64)
.ok_or_else(|| SpillError::Pool("offset overflow".into()))?;
return Ok(ResidentSlot {
kind: self.kind,
slot: (idx as u32) + 1,
generation: next_gen,
offset,
});
}
}
Err(SpillError::Pool("no free slots".into()))
}
fn release(&mut self, slot: ResidentSlot) -> Result<(), SpillError> {
let idx = self.slot_index(slot)?;
let rec = &mut self.slots[idx];
if rec.refs != 0 {
return Err(SpillError::Pool("cannot release slot with live refs".into()));
}
let next_gen = rec
.generation
.checked_add(1)
.ok_or_else(|| SpillError::Pool("generation exhausted".into()))?;
rec.bytes.fill(0);
rec.generation = next_gen;
rec.occupied = false;
rec.sealed = false;
self.used = self.used.saturating_sub(1);
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct HostBlockPoolBackend {
full: HostKindPool,
window: HostKindPool,
}
impl HostBlockPoolBackend {
pub fn new(
full_capacity: usize,
window_capacity: usize,
full_stride: usize,
window_stride: usize,
) -> Self {
Self {
full: HostKindPool::new(SpillBlockKind::Full, full_capacity, full_stride),
window: HostKindPool::new(SpillBlockKind::Window, window_capacity, window_stride),
}
}
fn pool(&self, kind: SpillBlockKind) -> &HostKindPool {
match kind {
SpillBlockKind::Full => &self.full,
SpillBlockKind::Window => &self.window,
}
}
fn pool_mut(&mut self, kind: SpillBlockKind) -> &mut HostKindPool {
match kind {
SpillBlockKind::Full => &mut self.full,
SpillBlockKind::Window => &mut self.window,
}
}
pub fn write_and_seal(&mut self, slot: ResidentSlot, kv_bytes: &[u8]) -> Result<(), SpillError> {
self.install_restored_slot(slot, kv_bytes)
}
#[cfg(test)]
fn raw_slot_bytes(&self, kind: SpillBlockKind, slot_one_based: u32) -> &[u8] {
&self.pool(kind).slots[(slot_one_based - 1) as usize].bytes
}
}
impl SpillPoolBackend for HostBlockPoolBackend {
fn stride_bytes(&self, kind: SpillBlockKind) -> usize {
self.pool(kind).stride
}
fn capacity_slots(&self, kind: SpillBlockKind) -> usize {
self.pool(kind).slots.len()
}
fn used_slots(&self, kind: SpillBlockKind) -> usize {
self.pool(kind).used
}
fn free_slots(&self, kind: SpillBlockKind) -> usize {
let p = self.pool(kind);
p.slots.len().saturating_sub(p.used)
}
fn read_slot_bytes(&self, slot: ResidentSlot) -> Result<&[u8], SpillError> {
let p = self.pool(slot.kind);
let idx = p.slot_index(slot)?;
if !p.slots[idx].sealed {
return Err(SpillError::Pool("cannot spill unsealed slot".into()));
}
Ok(&p.slots[idx].bytes)
}
fn take_free_slot(&mut self, kind: SpillBlockKind) -> Result<ResidentSlot, SpillError> {
self.pool_mut(kind).take_free()
}
fn release_cold_slot(&mut self, slot: ResidentSlot) -> Result<(), SpillError> {
self.pool_mut(slot.kind).release(slot)
}
fn install_restored_slot(&mut self, slot: ResidentSlot, kv_bytes: &[u8]) -> Result<(), SpillError> {
let p = self.pool_mut(slot.kind);
if kv_bytes.len() != p.stride {
return Err(SpillError::InvalidLength { expected: p.stride, got: kv_bytes.len() });
}
let idx = p.slot_index(slot)?;
if p.slots[idx].sealed {
return Err(SpillError::Pool("sealed slot is immutable".into()));
}
p.slots[idx].bytes.copy_from_slice(kv_bytes);
p.slots[idx].sealed = true;
Ok(())
}
fn add_ref(&mut self, slot: ResidentSlot) -> Result<u32, SpillError> {
let p = self.pool_mut(slot.kind);
let idx = p.slot_index(slot)?;
p.slots[idx].refs = p.slots[idx]
.refs
.checked_add(1)
.ok_or_else(|| SpillError::Pool("refcount overflow".into()))?;
Ok(p.slots[idx].refs)
}
fn drop_ref(&mut self, slot: ResidentSlot) -> Result<u32, SpillError> {
let p = self.pool_mut(slot.kind);
let idx = p.slot_index(slot)?;
p.slots[idx].refs = p.slots[idx]
.refs
.checked_sub(1)
.ok_or_else(|| SpillError::Pool("refcount underflow".into()))?;
Ok(p.slots[idx].refs)
}
fn refs(&self, slot: ResidentSlot) -> Result<u32, SpillError> {
let p = self.pool(slot.kind);
let idx = p.slot_index(slot)?;
Ok(p.slots[idx].refs)
}
}
#[derive(Clone, Copy, Debug)]
struct DiskEntry {
bytes: u64,
lru_seq: u64,
}
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub struct SpillStoreStats {
pub writes_committed: u64,
pub bytes_written: u64,
pub reads_hit: u64,
pub reads_miss: u64,
pub corrupt_files_removed: u64,
pub disk_gc_evictions: u64,
pub over_budget_drops: u64,
pub orphans_swept: u64,
}
pub struct SpillStore {
dir: PathBuf,
max_bytes: u64,
used_bytes: u64,
lru_clock: u64,
entries: HashMap<SpillKey, DiskEntry>,
stats: SpillStoreStats,
}
fn enable_nocache(file: &File) {
#[cfg(target_os = "macos")]
{
use std::os::fd::AsRawFd;
unsafe {
libc::fcntl(file.as_raw_fd(), libc::F_NOCACHE, 1);
}
}
#[cfg(not(target_os = "macos"))]
{
let _ = file;
}
}
fn fsync_dir(dir: &Path) -> io::Result<()> {
let f = File::open(dir)?;
f.sync_all()
}
fn pread_exact(file: &File, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> {
#[cfg(unix)]
{
while !buf.is_empty() {
let n = file.read_at(buf, offset)?;
if n == 0 {
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "unexpected EOF in pread"));
}
buf = &mut buf[n..];
offset = offset.checked_add(n as u64).ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "pread offset overflow")
})?;
}
Ok(())
}
#[cfg(not(unix))]
{
use std::io::{Read, Seek, SeekFrom};
let mut f = file;
f.seek(SeekFrom::Start(offset))?;
f.read_exact(buf)
}
}
fn encode_header(
key: SpillKey,
generation: u64,
kv_bytes: &[u8],
recurrent_state: Option<&[u8]>,
) -> [u8; SPILL_HEADER_LEN] {
let state_slice = recurrent_state.unwrap_or(&[]);
let mut header = [0u8; SPILL_HEADER_LEN];
header[0..8].copy_from_slice(&SPILL_MAGIC);
header[8..12].copy_from_slice(&SPILL_FORMAT_VERSION.to_le_bytes());
header[12..16].copy_from_slice(&(key.kind as u32).to_le_bytes());
header[16..24].copy_from_slice(&key.id.to_le_bytes());
header[24..32].copy_from_slice(&generation.to_le_bytes());
header[32..40].copy_from_slice(&(kv_bytes.len() as u64).to_le_bytes());
header[40..48].copy_from_slice(&(state_slice.len() as u64).to_le_bytes());
let mut hasher = Sha256::new();
hasher.update(&header[0..HEADER_PREFIX_LEN]);
hasher.update(kv_bytes);
if !state_slice.is_empty() {
hasher.update(state_slice);
}
let digest = hasher.finalize();
header[48..80].copy_from_slice(&digest);
header
}
impl SpillStore {
pub fn open(dir: impl AsRef<Path>, max_bytes: u64) -> Result<Self, SpillError> {
let dir = dir.as_ref().to_path_buf();
prepare_spill_dir(&dir)?;
let mut store = Self {
dir,
max_bytes,
used_bytes: 0,
lru_clock: 0,
entries: HashMap::new(),
stats: SpillStoreStats::default(),
};
store.scan_and_sweep()?;
Ok(store)
}
pub fn dir(&self) -> &Path {
&self.dir
}
pub fn max_bytes(&self) -> u64 {
self.max_bytes
}
pub fn used_bytes(&self) -> u64 {
self.used_bytes
}
pub fn stats(&self) -> SpillStoreStats {
self.stats
}
pub fn contains(&self, key: SpillKey) -> bool {
self.entries.contains_key(&key)
}
pub fn entry_count(&self) -> usize {
self.entries.len()
}
pub fn block_path(&self, key: SpillKey) -> PathBuf {
self.dir.join(key.blk_filename())
}
fn next_lru(&mut self) -> u64 {
self.lru_clock = self.lru_clock.wrapping_add(1);
self.lru_clock
}
fn scan_and_sweep(&mut self) -> Result<(), SpillError> {
let mut discovered: Vec<(SpillKey, u64, u64)> = Vec::new();
for entry in fs::read_dir(&self.dir)? {
let entry = entry?;
let path = entry.path();
let Some(name) = entry.file_name().to_str().map(str::to_owned) else {
continue;
};
let meta = match fs::symlink_metadata(&path) {
Ok(m) => m,
Err(_) => continue,
};
if name.contains(".tmp.") {
if !meta.is_dir() && fs::remove_file(&path).is_ok() {
self.stats.orphans_swept += 1;
}
continue;
}
if let Some(key) = SpillKey::parse_blk_filename(&name) {
if !meta.file_type().is_file() {
let _ = fs::remove_file(&path);
self.stats.corrupt_files_removed += 1;
continue;
}
let mtime_key = meta
.modified()
.ok()
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| d.as_nanos() as u64)
.unwrap_or(0);
discovered.push((key, meta.len(), mtime_key));
}
}
discovered.sort_by_key(|&(key, _, mtime)| (mtime, key));
for (key, len, _) in discovered {
let seq = self.next_lru();
self.entries.insert(key, DiskEntry { bytes: len, lru_seq: seq });
self.used_bytes = self.used_bytes.saturating_add(len);
}
if self.stats.orphans_swept > 0 {
let _ = fsync_dir(&self.dir);
}
let empty = HashSet::new();
self.evict_lru_to_fit(0, &empty);
Ok(())
}
pub fn evict_lru_to_fit(
&mut self,
additional: u64,
protected: &HashSet<SpillKey>,
) -> Vec<SpillKey> {
let mut evicted = Vec::new();
while self.used_bytes.saturating_add(additional) > self.max_bytes {
let victim = self
.entries
.iter()
.filter(|(k, _)| !protected.contains(k))
.min_by_key(|(k, e)| (e.lru_seq, **k))
.map(|(k, _)| *k);
let Some(victim_key) = victim else {
break;
};
if let Some(entry) = self.entries.remove(&victim_key) {
let _ = fs::remove_file(self.block_path(victim_key));
self.used_bytes = self.used_bytes.saturating_sub(entry.bytes);
self.stats.disk_gc_evictions += 1;
evicted.push(victim_key);
}
}
evicted
}
pub fn remove_block(&mut self, key: SpillKey) -> bool {
let path = self.block_path(key);
let removed_file = fs::remove_file(&path).is_ok();
if let Some(entry) = self.entries.remove(&key) {
self.used_bytes = self.used_bytes.saturating_sub(entry.bytes);
true
} else {
removed_file
}
}
pub fn write_block(
&mut self,
key: SpillKey,
generation: u64,
kv_bytes: &[u8],
recurrent_state: Option<&[u8]>,
protected: &HashSet<SpillKey>,
) -> Result<(WriteOutcome, Vec<SpillKey>), SpillError> {
if kv_bytes.len() > MAX_KV_PAYLOAD_BYTES {
return Err(SpillError::PayloadTooLarge(kv_bytes.len()));
}
let state_slice = recurrent_state.unwrap_or(&[]);
if state_slice.len() > MAX_RECURRENT_STATE_BYTES {
return Err(SpillError::PayloadTooLarge(state_slice.len()));
}
let total_bytes = (SPILL_HEADER_LEN as u64)
.checked_add(kv_bytes.len() as u64)
.and_then(|v| v.checked_add(state_slice.len() as u64))
.ok_or_else(|| SpillError::PayloadTooLarge(usize::MAX))?;
if total_bytes > self.max_bytes {
self.stats.over_budget_drops += 1;
return Ok((WriteOutcome::OverBudget, Vec::new()));
}
let existing_bytes = self.entries.get(&key).map(|e| e.bytes).unwrap_or(0);
let net_additional = total_bytes.saturating_sub(existing_bytes);
let mut protected_with_self = protected.clone();
protected_with_self.insert(key);
let evicted = self.evict_lru_to_fit(net_additional, &protected_with_self);
if self.used_bytes.saturating_sub(existing_bytes).saturating_add(total_bytes) > self.max_bytes {
self.stats.over_budget_drops += 1;
return Ok((WriteOutcome::OverBudget, evicted));
}
let header = encode_header(key, generation, kv_bytes, recurrent_state);
let pid = std::process::id();
let nonce = TEMP_NONCE.fetch_add(1, Ordering::Relaxed);
let tmp_path = self.dir.join(key.tmp_filename(pid, nonce));
let final_path = self.block_path(key);
let write_result = (|| -> io::Result<()> {
let mut opts = OpenOptions::new();
opts.write(true).create_new(true);
#[cfg(unix)]
{
opts.mode(0o600).custom_flags(libc::O_NOFOLLOW);
}
let mut file = opts.open(&tmp_path)?;
#[cfg(unix)]
{
file.set_permissions(fs::Permissions::from_mode(0o600))?;
}
enable_nocache(&file);
file.write_all(&header)?;
file.write_all(kv_bytes)?;
if !state_slice.is_empty() {
file.write_all(state_slice)?;
}
file.sync_all()?;
drop(file);
fs::rename(&tmp_path, &final_path)?;
fsync_dir(&self.dir)?;
Ok(())
})();
if let Err(e) = write_result {
let _ = fs::remove_file(&tmp_path);
return Err(SpillError::Io(e));
}
if let Some(old) = self.entries.remove(&key) {
self.used_bytes = self.used_bytes.saturating_sub(old.bytes);
}
let seq = self.next_lru();
self.entries.insert(key, DiskEntry { bytes: total_bytes, lru_seq: seq });
self.used_bytes = self.used_bytes.saturating_add(total_bytes);
self.stats.writes_committed += 1;
self.stats.bytes_written = self.stats.bytes_written.saturating_add(total_bytes);
Ok((WriteOutcome::Written { on_disk_bytes: total_bytes }, evicted))
}
pub fn read_block(&mut self, key: SpillKey, expected_kv_len: Option<usize>) -> ReadOutcome {
let path = self.block_path(key);
let mut opts = OpenOptions::new();
opts.read(true);
#[cfg(unix)]
{
opts.custom_flags(libc::O_NOFOLLOW);
}
let file = match opts.open(&path) {
Ok(f) => f,
Err(e) => {
#[cfg(unix)]
if e.raw_os_error() == Some(libc::ELOOP) {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::NotRegularFile);
}
if let Some(old) = self.entries.remove(&key) {
self.used_bytes = self.used_bytes.saturating_sub(old.bytes);
}
self.stats.reads_miss += 1;
let reason = if e.kind() == io::ErrorKind::NotFound {
MissReason::NotFound
} else {
MissReason::IoReadError
};
return ReadOutcome::Miss(reason);
}
};
enable_nocache(&file);
let meta = match file.metadata() {
Ok(m) => m,
Err(_) => {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::IoReadError);
}
};
if !meta.is_file() {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::NotRegularFile);
}
let file_len = meta.len();
if file_len < SPILL_HEADER_LEN as u64 {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::TruncatedHeader);
}
let mut header = [0u8; SPILL_HEADER_LEN];
if pread_exact(&file, &mut header, 0).is_err() {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::TruncatedHeader);
}
if header[0..8] != SPILL_MAGIC {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::BadMagic);
}
let version = u32::from_le_bytes(header[8..12].try_into().unwrap());
if version != SPILL_FORMAT_VERSION {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::UnsupportedVersion);
}
let raw_kind = u32::from_le_bytes(header[12..16].try_into().unwrap());
let Some(kind) = SpillBlockKind::from_u32(raw_kind) else {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::BadBlockKind);
};
let block_id = u64::from_le_bytes(header[16..24].try_into().unwrap());
if kind != key.kind || block_id != key.id {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::MetadataMismatch);
}
let generation = u64::from_le_bytes(header[24..32].try_into().unwrap());
let kv_len_u64 = u64::from_le_bytes(header[32..40].try_into().unwrap());
let state_len_u64 = u64::from_le_bytes(header[40..48].try_into().unwrap());
let stored_digest: [u8; 32] = header[48..80].try_into().unwrap();
let (Ok(kv_len), Ok(state_len)) = (usize::try_from(kv_len_u64), usize::try_from(state_len_u64)) else {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::PayloadTooLarge);
};
if kv_len > MAX_KV_PAYLOAD_BYTES || state_len > MAX_RECURRENT_STATE_BYTES {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::PayloadTooLarge);
}
if let Some(exp) = expected_kv_len {
if kv_len != exp {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::LengthMismatch);
}
}
let expected_file_len = (SPILL_HEADER_LEN as u64)
.checked_add(kv_len_u64)
.and_then(|v| v.checked_add(state_len_u64));
if expected_file_len != Some(file_len) {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::SizeMismatch);
}
let mut kv_bytes = vec![0u8; kv_len];
if kv_len > 0 && pread_exact(&file, &mut kv_bytes, SPILL_HEADER_LEN as u64).is_err() {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::IoReadError);
}
let recurrent_state = if state_len > 0 {
let mut st = vec![0u8; state_len];
let st_off = (SPILL_HEADER_LEN as u64).saturating_add(kv_len_u64);
if pread_exact(&file, &mut st, st_off).is_err() {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::IoReadError);
}
Some(st)
} else {
None
};
let mut hasher = Sha256::new();
hasher.update(&header[0..HEADER_PREFIX_LEN]);
hasher.update(&kv_bytes);
if let Some(st) = &recurrent_state {
hasher.update(st);
}
let computed = hasher.finalize();
if !ct_eq_32(&computed, &stored_digest) {
self.drop_corrupt(key);
return ReadOutcome::Miss(MissReason::ChecksumMismatch);
}
let seq = self.next_lru();
if let Some(entry) = self.entries.get_mut(&key) {
entry.lru_seq = seq;
entry.bytes = file_len;
} else {
self.entries.insert(key, DiskEntry { bytes: file_len, lru_seq: seq });
self.used_bytes = self.used_bytes.saturating_add(file_len);
}
self.stats.reads_hit += 1;
ReadOutcome::Hit(RestoredBlock {
key,
generation,
kv_bytes,
recurrent_state,
})
}
fn drop_corrupt(&mut self, key: SpillKey) {
let path = self.block_path(key);
let _ = fs::remove_file(&path);
if let Some(old) = self.entries.remove(&key) {
self.used_bytes = self.used_bytes.saturating_sub(old.bytes);
}
self.stats.reads_miss += 1;
self.stats.corrupt_files_removed += 1;
}
}
fn prepare_spill_dir(dir: &Path) -> Result<(), SpillError> {
match fs::symlink_metadata(dir) {
Ok(meta) => {
if meta.file_type().is_symlink() {
return Err(SpillError::InvalidDir(format!(
"{} must not be a symlink",
dir.display()
)));
}
if !meta.is_dir() {
return Err(SpillError::InvalidDir(format!(
"{} exists and is not a directory",
dir.display()
)));
}
#[cfg(unix)]
{
let mode = meta.mode() & 0o777;
if mode != 0o700 {
fs::set_permissions(dir, fs::Permissions::from_mode(0o700))?;
}
}
}
Err(e) if e.kind() == io::ErrorKind::NotFound => {
let mut builder = fs::DirBuilder::new();
builder.recursive(true);
#[cfg(unix)]
{
builder.mode(0o700);
}
builder.create(dir)?;
#[cfg(unix)]
{
fs::set_permissions(dir, fs::Permissions::from_mode(0o700))?;
}
}
Err(e) => return Err(SpillError::Io(e)),
}
Ok(())
}
#[derive(Clone, Debug)]
struct ManagedBlock {
tier: BlockTier,
resident: Option<ResidentSlot>,
recurrent_state: Option<Vec<u8>>,
last_used_seq: u64,
io_pins: u32,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ReserveError {
ExceedsCapacity {
need_full: usize,
cap_full: usize,
need_window: usize,
cap_window: usize,
},
WouldBlock {
need_full: usize,
avail_full: usize,
need_window: usize,
avail_window: usize,
},
BackendFailed,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum PrepareRestoreError {
CacheMiss(SpillKey),
Reserve(ReserveError),
}
#[derive(Debug)]
pub struct WorkingSetReservation {
id: u64,
full_slots: Vec<ResidentSlot>,
window_slots: Vec<ResidentSlot>,
}
impl WorkingSetReservation {
pub fn id(&self) -> u64 {
self.id
}
pub fn full_count(&self) -> usize {
self.full_slots.len()
}
pub fn window_count(&self) -> usize {
self.window_slots.len()
}
pub fn full_slots(&self) -> &[ResidentSlot] {
&self.full_slots
}
pub fn window_slots(&self) -> &[ResidentSlot] {
&self.window_slots
}
}
#[derive(Debug)]
pub struct PreparedRestore {
pub pinned_in_memory: Vec<(SpillKey, ResidentSlot)>,
pub disk_full: Vec<SpillKey>,
pub disk_window: Vec<SpillKey>,
pub reservation: WorkingSetReservation,
}
#[derive(Clone, Debug)]
pub struct RestoredWorkingSet {
pub full_slots: Vec<(SpillKey, ResidentSlot)>,
pub window_slots: Vec<(SpillKey, ResidentSlot)>,
pub recurrent_states: HashMap<SpillKey, Vec<u8>>,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct RestoreWorkingSetMiss {
pub failed_key: SpillKey,
pub reason: MissReason,
}
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub struct SpillManagerStats {
pub lazy_spills_written: u64,
pub clean_mem_evictions: u64,
pub over_budget_mem_drops: u64,
pub reservations_granted: u64,
pub reservations_would_block: u64,
pub reservations_released: u64,
pub blocks_restored_from_disk: u64,
pub restore_cache_misses: u64,
}
pub struct SpillManager<P: SpillPoolBackend> {
pool: P,
store: SpillStore,
blocks: HashMap<SpillKey, ManagedBlock>,
lru_clock: u64,
next_reservation_id: u64,
active_reservations: HashSet<u64>,
stats: SpillManagerStats,
}
impl<P: SpillPoolBackend> SpillManager<P> {
pub fn new(pool: P, store: SpillStore) -> Self {
Self {
pool,
store,
blocks: HashMap::new(),
lru_clock: 0,
next_reservation_id: 1,
active_reservations: HashSet::new(),
stats: SpillManagerStats::default(),
}
}
pub fn pool(&self) -> &P {
&self.pool
}
pub fn pool_mut(&mut self) -> &mut P {
&mut self.pool
}
pub fn store(&self) -> &SpillStore {
&self.store
}
pub fn stats(&self) -> SpillManagerStats {
self.stats
}
pub fn active_reservation_count(&self) -> usize {
self.active_reservations.len()
}
pub fn tier(&self, key: SpillKey) -> BlockTier {
match self.blocks.get(&key) {
Some(b) => b.tier,
None => {
if self.store.contains(key) {
BlockTier::Disk
} else {
BlockTier::None
}
}
}
}
pub fn resident_slot(&self, key: SpillKey) -> Option<ResidentSlot> {
self.blocks.get(&key).and_then(|b| b.resident)
}
pub fn in_memory_recurrent_state(&self, key: SpillKey) -> Option<&[u8]> {
self.blocks.get(&key).and_then(|b| b.recurrent_state.as_deref())
}
fn next_lru(&mut self) -> u64 {
self.lru_clock = self.lru_clock.wrapping_add(1);
self.lru_clock
}
pub fn alloc_slot_lazy(&mut self, kind: SpillBlockKind) -> Result<ResidentSlot, ReserveError> {
if self.pool.capacity_slots(kind) == 0 {
return Err(ReserveError::ExceedsCapacity {
need_full: usize::from(kind == SpillBlockKind::Full),
cap_full: self.pool.capacity_slots(SpillBlockKind::Full),
need_window: usize::from(kind == SpillBlockKind::Window),
cap_window: self.pool.capacity_slots(SpillBlockKind::Window),
});
}
if self.pool.free_slots(kind) == 0 {
if self.evictable_count(kind) == 0 {
return Err(ReserveError::WouldBlock {
need_full: usize::from(kind == SpillBlockKind::Full),
avail_full: self.pool.free_slots(SpillBlockKind::Full)
+ self.evictable_count(SpillBlockKind::Full),
need_window: usize::from(kind == SpillBlockKind::Window),
avail_window: self.pool.free_slots(SpillBlockKind::Window)
+ self.evictable_count(SpillBlockKind::Window),
});
}
self.evict_one_lru_cold(kind).map_err(|_| ReserveError::BackendFailed)?;
}
self.pool.take_free_slot(kind).map_err(|_| ReserveError::BackendFailed)
}
pub fn register_sealed_block(
&mut self,
key: SpillKey,
slot: ResidentSlot,
recurrent_state: Option<Vec<u8>>,
) -> Result<(), SpillError> {
if slot.kind != key.kind {
return Err(SpillError::Pool("block kind mismatch on register".into()));
}
let _ = self.pool.read_slot_bytes(slot)?;
let seq = self.next_lru();
let on_disk = self.store.contains(key);
let tier = if on_disk { BlockTier::Both } else { BlockTier::Memory };
self.blocks.insert(
key,
ManagedBlock {
tier,
resident: Some(slot),
recurrent_state,
last_used_seq: seq,
io_pins: 0,
},
);
Ok(())
}
pub fn index_existing_disk_blocks(&mut self) {
let keys: Vec<SpillKey> = self.store.entries.keys().copied().collect();
for key in keys {
if !self.blocks.contains_key(&key) {
let seq = self.next_lru();
self.blocks.insert(
key,
ManagedBlock {
tier: BlockTier::Disk,
resident: None,
recurrent_state: None,
last_used_seq: seq,
io_pins: 0,
},
);
}
}
}
pub fn pin(&mut self, key: SpillKey) -> Result<ResidentSlot, SpillError> {
let slot = self
.blocks
.get(&key)
.and_then(|b| b.resident)
.ok_or_else(|| SpillError::Pool("cannot pin non-resident block".into()))?;
self.pool.add_ref(slot)?;
let seq = self.next_lru();
if let Some(b) = self.blocks.get_mut(&key) {
b.last_used_seq = seq;
}
Ok(slot)
}
pub fn unpin(&mut self, key: SpillKey) -> Result<u32, SpillError> {
let slot = self
.blocks
.get(&key)
.and_then(|b| b.resident)
.ok_or_else(|| SpillError::Pool("cannot unpin non-resident block".into()))?;
let refs = self.pool.drop_ref(slot)?;
let seq = self.next_lru();
if let Some(b) = self.blocks.get_mut(&key) {
b.last_used_seq = seq;
}
Ok(refs)
}
pub fn touch(&mut self, key: SpillKey) {
let seq = self.next_lru();
if let Some(b) = self.blocks.get_mut(&key) {
b.last_used_seq = seq;
}
}
pub fn evictable_count(&self, kind: SpillBlockKind) -> usize {
self.blocks
.iter()
.filter(|(k, b)| {
k.kind == kind
&& b.tier.in_memory()
&& b.io_pins == 0
&& b.resident
.and_then(|s| self.pool.refs(s).ok())
.map(|r| r == 0)
.unwrap_or(false)
})
.count()
}
fn pick_lru_evictable(&self, kind: SpillBlockKind) -> Option<SpillKey> {
self.blocks
.iter()
.filter(|(k, b)| {
k.kind == kind
&& b.tier.in_memory()
&& b.io_pins == 0
&& b.resident
.and_then(|s| self.pool.refs(s).ok())
.map(|r| r == 0)
.unwrap_or(false)
})
.min_by_key(|(k, b)| (b.last_used_seq, **k))
.map(|(k, _)| *k)
}
fn evict_one_lru_cold(&mut self, kind: SpillBlockKind) -> Result<SpillKey, SpillError> {
let key = self
.pick_lru_evictable(kind)
.ok_or_else(|| SpillError::Pool("no evictable cold block".into()))?;
let (tier, slot) = {
let b = self.blocks.get_mut(&key).unwrap();
b.io_pins = b.io_pins.saturating_add(1);
(b.tier, b.resident.expect("resident slot"))
};
if tier == BlockTier::Both && self.store.contains(key) {
self.pool.release_cold_slot(slot)?;
if let Some(b) = self.blocks.get_mut(&key) {
b.tier = BlockTier::Disk;
b.resident = None;
b.recurrent_state = None;
b.io_pins = b.io_pins.saturating_sub(1);
}
self.stats.clean_mem_evictions += 1;
return Ok(key);
}
let kv_copy = self.pool.read_slot_bytes(slot)?.to_vec();
let state_copy = self.blocks.get(&key).and_then(|b| b.recurrent_state.clone());
let protected: HashSet<SpillKey> = self
.blocks
.iter()
.filter(|(_, b)| b.io_pins > 0)
.map(|(k, _)| *k)
.collect();
let write_res = self.store.write_block(
key,
u64::from(slot.generation),
&kv_copy,
state_copy.as_deref(),
&protected,
);
let (outcome, disk_gc_evicted) = match write_res {
Ok(v) => v,
Err(_) => {
self.pool.release_cold_slot(slot)?;
self.blocks.remove(&key);
self.stats.over_budget_mem_drops += 1;
return Ok(key);
}
};
for gc_key in disk_gc_evicted {
if let Some(gb) = self.blocks.get_mut(&gc_key) {
match gb.tier {
BlockTier::Disk => {
self.blocks.remove(&gc_key);
}
BlockTier::Both => {
gb.tier = BlockTier::Memory;
}
BlockTier::Memory | BlockTier::None => {}
}
}
}
self.pool.release_cold_slot(slot)?;
match outcome {
WriteOutcome::Written { .. } => {
if let Some(b) = self.blocks.get_mut(&key) {
b.tier = BlockTier::Disk;
b.resident = None;
b.recurrent_state = None;
b.io_pins = b.io_pins.saturating_sub(1);
}
self.stats.lazy_spills_written += 1;
}
WriteOutcome::OverBudget => {
self.blocks.remove(&key);
self.stats.over_budget_mem_drops += 1;
}
}
Ok(key)
}
pub fn reserve_working_set(
&mut self,
count_full: usize,
count_window: usize,
) -> Result<WorkingSetReservation, ReserveError> {
let cap_full = self.pool.capacity_slots(SpillBlockKind::Full);
let cap_window = self.pool.capacity_slots(SpillBlockKind::Window);
if count_full > cap_full || count_window > cap_window {
return Err(ReserveError::ExceedsCapacity {
need_full: count_full,
cap_full,
need_window: count_window,
cap_window,
});
}
let free_full = self.pool.free_slots(SpillBlockKind::Full);
let evict_full = self.evictable_count(SpillBlockKind::Full);
let avail_full = free_full.saturating_add(evict_full);
let free_window = self.pool.free_slots(SpillBlockKind::Window);
let evict_window = self.evictable_count(SpillBlockKind::Window);
let avail_window = free_window.saturating_add(evict_window);
if count_full > avail_full || count_window > avail_window {
self.stats.reservations_would_block += 1;
return Err(ReserveError::WouldBlock {
need_full: count_full,
avail_full,
need_window: count_window,
avail_window,
});
}
let mut full_slots = Vec::with_capacity(count_full);
let mut window_slots = Vec::with_capacity(count_window);
let claim = (|| -> Result<(), SpillError> {
for _ in 0..count_full {
if self.pool.free_slots(SpillBlockKind::Full) == 0 {
self.evict_one_lru_cold(SpillBlockKind::Full)?;
}
let s = self.pool.take_free_slot(SpillBlockKind::Full)?;
self.pool.add_ref(s)?;
full_slots.push(s);
}
for _ in 0..count_window {
if self.pool.free_slots(SpillBlockKind::Window) == 0 {
self.evict_one_lru_cold(SpillBlockKind::Window)?;
}
let s = self.pool.take_free_slot(SpillBlockKind::Window)?;
self.pool.add_ref(s)?;
window_slots.push(s);
}
Ok(())
})();
if claim.is_err() {
for s in full_slots.into_iter().chain(window_slots.into_iter()) {
let _ = self.pool.drop_ref(s);
let _ = self.pool.release_cold_slot(s);
}
return Err(ReserveError::BackendFailed);
}
let id = self.next_reservation_id;
self.next_reservation_id = self.next_reservation_id.wrapping_add(1);
self.active_reservations.insert(id);
self.stats.reservations_granted += 1;
Ok(WorkingSetReservation { id, full_slots, window_slots })
}
pub fn release_reservation(&mut self, reservation: WorkingSetReservation) {
self.active_reservations.remove(&reservation.id);
for s in reservation.full_slots.into_iter().chain(reservation.window_slots.into_iter()) {
let _ = self.pool.drop_ref(s);
let _ = self.pool.release_cold_slot(s);
}
self.stats.reservations_released += 1;
}
pub fn prepare_working_set_restore(
&mut self,
full_keys: &[SpillKey],
window_keys: &[SpillKey],
) -> Result<PreparedRestore, PrepareRestoreError> {
let mut seen = HashSet::new();
let mut in_mem_keys = Vec::new();
let mut disk_full = Vec::new();
let mut disk_window = Vec::new();
for &key in full_keys {
if key.kind != SpillBlockKind::Full {
return Err(PrepareRestoreError::CacheMiss(key));
}
if !seen.insert(key) {
continue;
}
match self.tier(key) {
BlockTier::Memory | BlockTier::Both => in_mem_keys.push(key),
BlockTier::Disk => disk_full.push(key),
BlockTier::None => return Err(PrepareRestoreError::CacheMiss(key)),
}
}
for &key in window_keys {
if key.kind != SpillBlockKind::Window {
return Err(PrepareRestoreError::CacheMiss(key));
}
if !seen.insert(key) {
continue;
}
match self.tier(key) {
BlockTier::Memory | BlockTier::Both => in_mem_keys.push(key),
BlockTier::Disk => disk_window.push(key),
BlockTier::None => return Err(PrepareRestoreError::CacheMiss(key)),
}
}
let mut pinned_in_memory = Vec::with_capacity(in_mem_keys.len());
for &key in &in_mem_keys {
match self.pin(key) {
Ok(slot) => pinned_in_memory.push((key, slot)),
Err(_) => {
for (pk, _) in pinned_in_memory {
let _ = self.unpin(pk);
}
return Err(PrepareRestoreError::CacheMiss(key));
}
}
}
for &key in disk_full.iter().chain(disk_window.iter()) {
let seq = self.next_lru();
let entry = self.blocks.entry(key).or_insert_with(|| ManagedBlock {
tier: BlockTier::Disk,
resident: None,
recurrent_state: None,
last_used_seq: seq,
io_pins: 0,
});
entry.io_pins = entry.io_pins.saturating_add(1);
}
match self.reserve_working_set(disk_full.len(), disk_window.len()) {
Ok(reservation) => Ok(PreparedRestore {
pinned_in_memory,
disk_full,
disk_window,
reservation,
}),
Err(e) => {
for &key in disk_full.iter().chain(disk_window.iter()) {
if let Some(b) = self.blocks.get_mut(&key) {
b.io_pins = b.io_pins.saturating_sub(1);
}
}
for (pk, _) in pinned_in_memory {
let _ = self.unpin(pk);
}
Err(PrepareRestoreError::Reserve(e))
}
}
}
pub fn abort_prepared_restore(&mut self, prepared: PreparedRestore) {
for &key in prepared.disk_full.iter().chain(prepared.disk_window.iter()) {
if let Some(b) = self.blocks.get_mut(&key) {
b.io_pins = b.io_pins.saturating_sub(1);
}
}
self.release_reservation(prepared.reservation);
for (key, _) in prepared.pinned_in_memory {
let _ = self.unpin(key);
}
}
pub fn execute_working_set_restore(
&mut self,
prepared: PreparedRestore,
) -> Result<RestoredWorkingSet, RestoreWorkingSetMiss> {
let PreparedRestore {
pinned_in_memory,
disk_full,
disk_window,
reservation,
} = prepared;
let full_stride = self.pool.stride_bytes(SpillBlockKind::Full);
let window_stride = self.pool.stride_bytes(SpillBlockKind::Window);
let mut verified_full = Vec::with_capacity(disk_full.len());
let mut verified_window = Vec::with_capacity(disk_window.len());
let release_io_pins = |blocks: &mut HashMap<SpillKey, ManagedBlock>,
df: &[SpillKey],
dw: &[SpillKey]| {
for &k in df.iter().chain(dw.iter()) {
if let Some(b) = blocks.get_mut(&k) {
b.io_pins = b.io_pins.saturating_sub(1);
}
}
};
for &key in &disk_full {
if self.blocks.get(&key).and_then(|b| b.resident).is_some() {
verified_full.push(None);
continue;
}
match self.store.read_block(key, Some(full_stride)) {
ReadOutcome::Hit(blk) => verified_full.push(Some(blk)),
ReadOutcome::Miss(reason) => {
release_io_pins(&mut self.blocks, &disk_full, &disk_window);
self.blocks.remove(&key);
self.release_reservation(reservation);
for (pk, _) in pinned_in_memory {
let _ = self.unpin(pk);
}
self.stats.restore_cache_misses += 1;
return Err(RestoreWorkingSetMiss { failed_key: key, reason });
}
}
}
for &key in &disk_window {
if self.blocks.get(&key).and_then(|b| b.resident).is_some() {
verified_window.push(None);
continue;
}
match self.store.read_block(key, Some(window_stride)) {
ReadOutcome::Hit(blk) => verified_window.push(Some(blk)),
ReadOutcome::Miss(reason) => {
release_io_pins(&mut self.blocks, &disk_full, &disk_window);
self.blocks.remove(&key);
self.release_reservation(reservation);
for (pk, _) in pinned_in_memory {
let _ = self.unpin(pk);
}
self.stats.restore_cache_misses += 1;
return Err(RestoreWorkingSetMiss { failed_key: key, reason });
}
}
}
release_io_pins(&mut self.blocks, &disk_full, &disk_window);
self.active_reservations.remove(&reservation.id);
let mut full_slots = Vec::new();
let mut window_slots = Vec::new();
let mut recurrent_states = HashMap::new();
for (key, slot) in pinned_in_memory {
if let Some(st) = self.in_memory_recurrent_state(key).map(|s| s.to_vec()) {
recurrent_states.insert(key, st);
}
match key.kind {
SpillBlockKind::Full => full_slots.push((key, slot)),
SpillBlockKind::Window => window_slots.push((key, slot)),
}
}
for ((&key, maybe_blk), slot) in disk_full
.iter()
.zip(verified_full.into_iter())
.zip(reservation.full_slots.into_iter())
{
if let Some(existing) = self.blocks.get(&key).and_then(|b| b.resident) {
self.pool.add_ref(existing).expect("pin existing restored full slot");
let _ = self.pool.drop_ref(slot);
let _ = self.pool.release_cold_slot(slot);
if let Some(st) = self.in_memory_recurrent_state(key).map(|s| s.to_vec()) {
recurrent_states.insert(key, st);
}
self.touch(key);
full_slots.push((key, existing));
} else {
let blk = maybe_blk.expect("verified full block present");
self.pool
.install_restored_slot(slot, &blk.kv_bytes)
.expect("verified block fits pre-reserved full slot");
let seq = self.next_lru();
let io_pins = self.blocks.get(&key).map(|b| b.io_pins).unwrap_or(0);
if let Some(st) = &blk.recurrent_state {
recurrent_states.insert(blk.key, st.clone());
}
self.blocks.insert(
blk.key,
ManagedBlock {
tier: BlockTier::Both,
resident: Some(slot),
recurrent_state: blk.recurrent_state,
last_used_seq: seq,
io_pins,
},
);
full_slots.push((blk.key, slot));
self.stats.blocks_restored_from_disk += 1;
}
}
for ((&key, maybe_blk), slot) in disk_window
.iter()
.zip(verified_window.into_iter())
.zip(reservation.window_slots.into_iter())
{
if let Some(existing) = self.blocks.get(&key).and_then(|b| b.resident) {
self.pool.add_ref(existing).expect("pin existing restored window slot");
let _ = self.pool.drop_ref(slot);
let _ = self.pool.release_cold_slot(slot);
if let Some(st) = self.in_memory_recurrent_state(key).map(|s| s.to_vec()) {
recurrent_states.insert(key, st);
}
self.touch(key);
window_slots.push((key, existing));
} else {
let blk = maybe_blk.expect("verified window block present");
self.pool
.install_restored_slot(slot, &blk.kv_bytes)
.expect("verified block fits pre-reserved window slot");
let seq = self.next_lru();
let io_pins = self.blocks.get(&key).map(|b| b.io_pins).unwrap_or(0);
if let Some(st) = &blk.recurrent_state {
recurrent_states.insert(blk.key, st.clone());
}
self.blocks.insert(
blk.key,
ManagedBlock {
tier: BlockTier::Both,
resident: Some(slot),
recurrent_state: blk.recurrent_state,
last_used_seq: seq,
io_pins,
},
);
window_slots.push((blk.key, slot));
self.stats.blocks_restored_from_disk += 1;
}
}
Ok(RestoredWorkingSet {
full_slots,
window_slots,
recurrent_states,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
struct ScratchTestDir {
path: PathBuf,
}
impl ScratchTestDir {
fn new(label: &str) -> Self {
let base = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("spill-tests");
fs::create_dir_all(&base).expect("create target/spill-tests");
#[cfg(unix)]
let _ = fs::set_permissions(&base, fs::Permissions::from_mode(0o700));
let nonce = TEMP_NONCE.fetch_add(1, Ordering::Relaxed);
let path = base.join(format!("{label}-{}-{nonce}", std::process::id()));
let _ = fs::remove_dir_all(&path);
fs::create_dir_all(&path).expect("create scratch test dir");
#[cfg(unix)]
fs::set_permissions(&path, fs::Permissions::from_mode(0o700))
.expect("chmod 0700 scratch test dir");
Self { path }
}
fn path(&self) -> &Path {
&self.path
}
}
impl Drop for ScratchTestDir {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.path);
}
}
fn hex(bytes: &[u8]) -> String {
let mut s = String::with_capacity(bytes.len() * 2);
for b in bytes {
use std::fmt::Write;
let _ = write!(&mut s, "{b:02x}");
}
s
}
fn deterministic_kv(key: SpillKey, len: usize) -> Vec<u8> {
let mut x = key.id.wrapping_mul(0x9e37_79b9_7f4a_7c15) ^ ((key.kind as u64 + 1) << 56) ^ 0xa076_1d64_78bd_642f;
let mut out = vec![0u8; len];
for chunk in out.chunks_mut(8) {
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
let v = x.wrapping_mul(0x2545_f491_4f6c_dd1d).to_le_bytes();
let n = chunk.len();
chunk.copy_from_slice(&v[..n]);
}
out
}
fn deterministic_state(key: SpillKey, len: usize) -> Vec<u8> {
let mut x = key.id.wrapping_mul(0xd1b5_4a32_d192_ed03) ^ 0x55aa_33cc_9966_11ee;
let mut out = vec![0u8; len];
for chunk in out.chunks_mut(8) {
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
let v = x.wrapping_mul(0x94d0_49bb_1331_11eb).to_le_bytes();
let n = chunk.len();
chunk.copy_from_slice(&v[..n]);
}
out
}
#[test]
fn sha256_matches_nist_vectors() {
let h_empty = Sha256::new().finalize();
assert_eq!(
hex(&h_empty),
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
);
let mut h_abc = Sha256::new();
h_abc.update(b"abc");
assert_eq!(
hex(&h_abc.finalize()),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
let mut h_448 = Sha256::new();
h_448.update(b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
assert_eq!(
hex(&h_448.finalize()),
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
);
let payload = deterministic_kv(SpillKey::full(42), 131_072);
let mut whole = Sha256::new();
whole.update(&payload);
let mut chunked = Sha256::new();
for c in payload.chunks(137) {
chunked.update(c);
}
assert_eq!(whole.finalize(), chunked.finalize());
}
#[cfg(unix)]
#[test]
fn spill_dir_and_files_have_strict_unix_permissions() {
let scratch = ScratchTestDir::new("perms");
let dir = scratch.path().join("spill");
let mut store = SpillStore::open(&dir, 1024 * 1024).unwrap();
let dir_mode = fs::symlink_metadata(&dir).unwrap().mode() & 0o777;
assert_eq!(dir_mode, 0o700, "spill dir must be mode 0700");
let key = SpillKey::full(7);
let kv = deterministic_kv(key, 4096);
let (outcome, _) = store.write_block(key, 1, &kv, None, &HashSet::new()).unwrap();
assert!(matches!(outcome, WriteOutcome::Written { .. }));
let file_mode = fs::symlink_metadata(store.block_path(key)).unwrap().mode() & 0o777;
assert_eq!(file_mode, 0o600, "spilled .blk files must be mode 0600");
}
#[test]
fn lazy_lru_spill_never_writes_eagerly_and_evicts_strict_lru_under_pressure() {
let scratch = ScratchTestDir::new("lazy-lru");
let dir = scratch.path().join("spill");
let full_stride = 4096;
let win_stride = 8192;
let pool = HostBlockPoolBackend::new(4, 2, full_stride, win_stride);
let store = SpillStore::open(&dir, 64 * 1024 * 1024).unwrap();
let mut mgr = SpillManager::new(pool, store);
let mut slots = Vec::new();
for id in 0..4u64 {
let key = SpillKey::full(id);
let slot = mgr.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
let kv = deterministic_kv(key, full_stride);
mgr.pool_mut().write_and_seal(slot, &kv).unwrap();
let state = if id == 2 { Some(deterministic_state(key, 1024)) } else { None };
mgr.register_sealed_block(key, slot, state).unwrap();
mgr.pin(key).unwrap();
slots.push((key, slot));
}
assert_eq!(mgr.store().stats().writes_committed, 0);
assert_eq!(mgr.store().entry_count(), 0);
for id in [2u64, 0, 3, 1] {
assert_eq!(mgr.unpin(SpillKey::full(id)).unwrap(), 0);
}
mgr.pin(SpillKey::full(0)).unwrap();
assert_eq!(mgr.store().stats().writes_committed, 0);
assert_eq!(mgr.store().entry_count(), 0);
for id in 0..4u64 {
assert_eq!(mgr.tier(SpillKey::full(id)), BlockTier::Memory);
}
let k4 = SpillKey::full(4);
let s4 = mgr.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
mgr.pool_mut().write_and_seal(s4, &deterministic_kv(k4, full_stride)).unwrap();
mgr.register_sealed_block(k4, s4, None).unwrap();
mgr.pin(k4).unwrap();
assert_eq!(mgr.stats().lazy_spills_written, 1);
assert_eq!(mgr.tier(SpillKey::full(2)), BlockTier::Disk);
assert_eq!(mgr.tier(SpillKey::full(0)), BlockTier::Memory); assert_eq!(mgr.tier(SpillKey::full(3)), BlockTier::Memory);
assert_eq!(mgr.tier(SpillKey::full(1)), BlockTier::Memory);
let k5 = SpillKey::full(5);
let s5 = mgr.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
mgr.pool_mut().write_and_seal(s5, &deterministic_kv(k5, full_stride)).unwrap();
mgr.register_sealed_block(k5, s5, None).unwrap();
mgr.pin(k5).unwrap();
assert_eq!(mgr.stats().lazy_spills_written, 2);
assert_eq!(mgr.tier(SpillKey::full(3)), BlockTier::Disk);
assert_eq!(mgr.tier(SpillKey::full(0)), BlockTier::Memory);
assert_eq!(mgr.tier(SpillKey::full(1)), BlockTier::Memory);
let prepared = mgr.prepare_working_set_restore(&[SpillKey::full(2)], &[]).unwrap();
let restored = mgr.execute_working_set_restore(prepared).unwrap();
assert_eq!(restored.full_slots.len(), 1);
let (rk, rslot) = restored.full_slots[0];
assert_eq!(rk, SpillKey::full(2));
assert_eq!(
mgr.pool().read_slot_bytes(rslot).unwrap(),
deterministic_kv(SpillKey::full(2), full_stride).as_slice()
);
assert_eq!(
restored.recurrent_states.get(&SpillKey::full(2)).map(Vec::as_slice),
Some(deterministic_state(SpillKey::full(2), 1024).as_slice())
);
assert_eq!(mgr.tier(SpillKey::full(2)), BlockTier::Both);
assert_eq!(mgr.tier(SpillKey::full(1)), BlockTier::Disk);
mgr.unpin(SpillKey::full(2)).unwrap();
let writes_before = mgr.store().stats().writes_committed;
let _s6 = mgr.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
assert_eq!(mgr.store().stats().writes_committed, writes_before);
assert_eq!(mgr.stats().clean_mem_evictions, 1);
assert_eq!(mgr.tier(SpillKey::full(2)), BlockTier::Disk);
}
#[test]
fn disk_budget_enforces_max_spill_bytes_with_lru_gc() {
let scratch = ScratchTestDir::new("disk-gc");
let dir = scratch.path().join("spill");
let kv_len = 4096usize;
let one_file = (SPILL_HEADER_LEN + kv_len) as u64;
let max_bytes = one_file * 3;
let mut store = SpillStore::open(&dir, max_bytes).unwrap();
for id in 0..8u64 {
let key = SpillKey::full(id);
let kv = deterministic_kv(key, kv_len);
let (out, _evicted) = store.write_block(key, 1, &kv, None, &HashSet::new()).unwrap();
assert_eq!(out, WriteOutcome::Written { on_disk_bytes: one_file });
assert!(
store.used_bytes() <= max_bytes,
"used_bytes {} exceeded max_bytes {}",
store.used_bytes(),
max_bytes
);
let actual_bytes: u64 = fs::read_dir(&dir)
.unwrap()
.map(|e| e.unwrap().metadata().unwrap().len())
.sum();
assert!(actual_bytes <= max_bytes);
}
assert_eq!(store.entry_count(), 3);
assert_eq!(store.stats().disk_gc_evictions, 5);
for id in 0..5u64 {
assert!(matches!(
store.read_block(SpillKey::full(id), Some(kv_len)),
ReadOutcome::Miss(MissReason::NotFound)
));
}
for id in 5..8u64 {
let key = SpillKey::full(id);
match store.read_block(key, Some(kv_len)) {
ReadOutcome::Hit(blk) => assert_eq!(blk.kv_bytes, deterministic_kv(key, kv_len)),
ReadOutcome::Miss(r) => panic!("expected surviving block {id}, got miss {r:?}"),
}
}
let _ = store.read_block(SpillKey::full(6), Some(kv_len));
let _ = store.read_block(SpillKey::full(7), Some(kv_len));
let _ = store.read_block(SpillKey::full(5), Some(kv_len));
let k8 = SpillKey::full(8);
store
.write_block(k8, 1, &deterministic_kv(k8, kv_len), None, &HashSet::new())
.unwrap();
assert!(!store.contains(SpillKey::full(6)));
assert!(store.contains(SpillKey::full(5)));
assert!(store.contains(SpillKey::full(7)));
assert!(store.contains(SpillKey::full(8)));
let dir0 = scratch.path().join("spill-zero");
let pool0 = HostBlockPoolBackend::new(1, 1, 512, 512);
let store0 = SpillStore::open(&dir0, 0).unwrap();
let mut mgr0 = SpillManager::new(pool0, store0);
let s0 = mgr0.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
mgr0.pool_mut().write_and_seal(s0, &deterministic_kv(SpillKey::full(1), 512)).unwrap();
mgr0.register_sealed_block(SpillKey::full(1), s0, None).unwrap();
let _s1 = mgr0.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
assert_eq!(mgr0.store().used_bytes(), 0);
assert_eq!(mgr0.tier(SpillKey::full(1)), BlockTier::None);
assert_eq!(mgr0.stats().over_budget_mem_drops, 1);
}
#[test]
fn atomic_working_set_reservation_prevents_partial_restore_thrashing() {
let scratch = ScratchTestDir::new("atomic-reserve");
let dir = scratch.path().join("spill");
let f_stride = 2048;
let w_stride = 4096;
let pool = HostBlockPoolBackend::new(4, 3, f_stride, w_stride);
let mut store = SpillStore::open(&dir, 64 * 1024 * 1024).unwrap();
for id in 1..=6u64 {
let k = SpillKey::full(id);
store.write_block(k, 1, &deterministic_kv(k, f_stride), None, &HashSet::new()).unwrap();
}
for id in 1..=4u64 {
let k = SpillKey::window(id);
store.write_block(k, 1, &deterministic_kv(k, w_stride), None, &HashSet::new()).unwrap();
}
let mut mgr = SpillManager::new(pool, store);
mgr.index_existing_disk_blocks();
let cold_f = SpillKey::full(100);
let sf = mgr.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
mgr.pool_mut().write_and_seal(sf, &deterministic_kv(cold_f, f_stride)).unwrap();
mgr.register_sealed_block(cold_f, sf, None).unwrap();
assert!(matches!(
mgr.reserve_working_set(5, 1),
Err(ReserveError::ExceedsCapacity { .. })
));
assert_eq!(mgr.tier(cold_f), BlockTier::Memory);
let lane_a_full = [SpillKey::full(1), SpillKey::full(2), SpillKey::full(3)];
let lane_a_win = [SpillKey::window(1), SpillKey::window(2)];
let prep_a = mgr.prepare_working_set_restore(&lane_a_full, &lane_a_win).unwrap();
assert_eq!(prep_a.reservation.full_count(), 3);
assert_eq!(prep_a.reservation.window_count(), 2);
assert_eq!(mgr.tier(cold_f), BlockTier::Memory);
assert_eq!(mgr.stats().lazy_spills_written, 0);
let lane_b_full = [SpillKey::full(4), SpillKey::full(5), SpillKey::full(6)];
let lane_b_win = [SpillKey::window(3), SpillKey::window(4)];
let err_b = mgr.prepare_working_set_restore(&lane_b_full, &lane_b_win).unwrap_err();
assert!(matches!(
err_b,
PrepareRestoreError::Reserve(ReserveError::WouldBlock {
need_full: 3,
avail_full: 1,
need_window: 2,
avail_window: 1,
})
));
assert_eq!(mgr.tier(cold_f), BlockTier::Memory, "WouldBlock must not spill cold_f");
assert_eq!(mgr.stats().lazy_spills_written, 0);
assert_eq!(mgr.stats().reservations_would_block, 1);
let err_c = mgr.reserve_working_set(1, 2).unwrap_err();
assert!(matches!(err_c, ReserveError::WouldBlock { .. }));
assert_eq!(mgr.tier(cold_f), BlockTier::Memory, "asymmetric WouldBlock must not spill Full");
let res_a = mgr.execute_working_set_restore(prep_a).unwrap();
assert_eq!(res_a.full_slots.len(), 3);
assert_eq!(res_a.window_slots.len(), 2);
for &(k, s) in &res_a.full_slots {
assert_eq!(mgr.pool().read_slot_bytes(s).unwrap(), deterministic_kv(k, f_stride).as_slice());
}
for &(k, s) in &res_a.window_slots {
assert_eq!(mgr.pool().read_slot_bytes(s).unwrap(), deterministic_kv(k, w_stride).as_slice());
}
for &(k, _) in res_a.full_slots.iter().chain(res_a.window_slots.iter()) {
assert_eq!(mgr.unpin(k).unwrap(), 0);
}
let prep_b = mgr.prepare_working_set_restore(&lane_b_full, &lane_b_win).unwrap();
let res_b = mgr.execute_working_set_restore(prep_b).unwrap();
for &(k, s) in &res_b.full_slots {
assert_eq!(mgr.pool().read_slot_bytes(s).unwrap(), deterministic_kv(k, f_stride).as_slice());
}
for &(k, s) in &res_b.window_slots {
assert_eq!(mgr.pool().read_slot_bytes(s).unwrap(), deterministic_kv(k, w_stride).as_slice());
}
}
#[test]
fn restore_never_evicts_its_own_in_memory_working_set() {
let scratch = ScratchTestDir::new("self-pin");
let dir = scratch.path().join("spill");
let stride = 1024;
let pool = HostBlockPoolBackend::new(2, 1, stride, stride);
let mut store = SpillStore::open(&dir, 1024 * 1024).unwrap();
for id in [2u64, 3] {
let k = SpillKey::full(id);
store.write_block(k, 1, &deterministic_kv(k, stride), None, &HashSet::new()).unwrap();
}
let mut mgr = SpillManager::new(pool, store);
mgr.index_existing_disk_blocks();
let k1 = SpillKey::full(1);
let s1 = mgr.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
mgr.pool_mut().write_and_seal(s1, &deterministic_kv(k1, stride)).unwrap();
mgr.register_sealed_block(k1, s1, None).unwrap();
let k99 = SpillKey::full(99);
let s99 = mgr.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
mgr.pool_mut().write_and_seal(s99, &deterministic_kv(k99, stride)).unwrap();
mgr.register_sealed_block(k99, s99, None).unwrap();
mgr.pin(k99).unwrap();
let err = mgr.prepare_working_set_restore(&[k1, SpillKey::full(2)], &[]).unwrap_err();
assert!(matches!(err, PrepareRestoreError::Reserve(ReserveError::WouldBlock { .. })));
assert_eq!(mgr.tier(k1), BlockTier::Memory);
assert_eq!(mgr.pool().refs(s1).unwrap(), 0, "failed prepare must unpin full(1)");
mgr.unpin(k99).unwrap();
let prep = mgr.prepare_working_set_restore(&[k1, SpillKey::full(2)], &[]).unwrap();
let res = mgr.execute_working_set_restore(prep).unwrap();
assert_eq!(res.full_slots.len(), 2);
assert_eq!(mgr.tier(k1), BlockTier::Memory);
assert_eq!(mgr.tier(SpillKey::full(2)), BlockTier::Both);
assert_eq!(mgr.tier(k99), BlockTier::Disk);
}
#[test]
fn corrupt_torn_bitflip_zero_and_orphan_files_cause_clean_cache_miss_never_corruption() {
let scratch = ScratchTestDir::new("corruption");
let dir = scratch.path().join("spill");
let kv_len = 2048usize;
let state_len = 512usize;
prepare_spill_dir(&dir).unwrap();
let orphan1 = dir.join("blk-f-0000000000000001.tmp.00001234.0000000000000001");
let orphan2 = dir.join("blk-w-0000000000000099.tmp.00005678.0000000000000002");
fs::write(&orphan1, b"half-written-tmp-data").unwrap();
fs::write(&orphan2, b"").unwrap();
let mut store = SpillStore::open(&dir, 64 * 1024 * 1024).unwrap();
assert_eq!(store.stats().orphans_swept, 2);
assert!(!orphan1.exists());
assert!(!orphan2.exists());
let write_good = |s: &mut SpillStore, id: u64| -> (SpillKey, PathBuf) {
let key = SpillKey::full(id);
let kv = deterministic_kv(key, kv_len);
let st = deterministic_state(key, state_len);
s.write_block(key, 3, &kv, Some(&st), &HashSet::new()).unwrap();
let p = s.block_path(key);
(key, p)
};
let (k1, p1) = write_good(&mut store, 1);
fs::write(&p1, b"").unwrap();
assert_eq!(store.read_block(k1, Some(kv_len)), ReadOutcome::Miss(MissReason::TruncatedHeader));
assert!(!p1.exists(), "corrupt file should be unlinked");
let (k2, p2) = write_good(&mut store, 2);
let raw = fs::read(&p2).unwrap();
fs::write(&p2, &raw[..40]).unwrap();
assert_eq!(store.read_block(k2, Some(kv_len)), ReadOutcome::Miss(MissReason::TruncatedHeader));
let (k3, p3) = write_good(&mut store, 3);
let raw = fs::read(&p3).unwrap();
fs::write(&p3, &raw[..SPILL_HEADER_LEN + kv_len / 2]).unwrap();
assert_eq!(store.read_block(k3, Some(kv_len)), ReadOutcome::Miss(MissReason::SizeMismatch));
let (k4, p4) = write_good(&mut store, 4);
let raw = fs::read(&p4).unwrap();
fs::write(&p4, &raw[..raw.len() - 1]).unwrap();
assert_eq!(store.read_block(k4, Some(kv_len)), ReadOutcome::Miss(MissReason::SizeMismatch));
let (k5, p5) = write_good(&mut store, 5);
let mut raw = fs::read(&p5).unwrap();
raw.push(0xAA);
fs::write(&p5, &raw).unwrap();
assert_eq!(store.read_block(k5, Some(kv_len)), ReadOutcome::Miss(MissReason::SizeMismatch));
let flip_offsets = [
(0usize, MissReason::BadMagic),
(8, MissReason::UnsupportedVersion),
(12, MissReason::BadBlockKind),
(16, MissReason::MetadataMismatch),
(24, MissReason::ChecksumMismatch), (48, MissReason::ChecksumMismatch), (SPILL_HEADER_LEN, MissReason::ChecksumMismatch), (SPILL_HEADER_LEN + kv_len / 2, MissReason::ChecksumMismatch), (SPILL_HEADER_LEN + kv_len - 1, MissReason::ChecksumMismatch), (SPILL_HEADER_LEN + kv_len + state_len / 2, MissReason::ChecksumMismatch), ];
for (i, &(off, expected_reason)) in flip_offsets.iter().enumerate() {
let (k, p) = write_good(&mut store, 100 + i as u64);
let mut raw = fs::read(&p).unwrap();
raw[off] ^= 0x04;
fs::write(&p, &raw).unwrap();
assert_eq!(
store.read_block(k, Some(kv_len)),
ReadOutcome::Miss(expected_reason),
"bit flip at offset {off} must return {expected_reason:?}"
);
assert!(!p.exists(), "corrupt file at offset {off} must be unlinked");
}
let (k200, p200) = write_good(&mut store, 200);
let (k201, p201) = write_good(&mut store, 201);
fs::copy(&p200, &p201).unwrap();
assert_eq!(store.read_block(k201, Some(kv_len)), ReadOutcome::Miss(MissReason::MetadataMismatch));
match store.read_block(k200, Some(kv_len)) {
ReadOutcome::Hit(blk) => {
assert_eq!(blk.kv_bytes, deterministic_kv(k200, kv_len));
assert_eq!(blk.recurrent_state, Some(deterministic_state(k200, state_len)));
}
ReadOutcome::Miss(r) => panic!("block 200 should be intact, got {r:?}"),
}
let pool = HostBlockPoolBackend::new(3, 1, kv_len, kv_len);
let store2 = SpillStore::open(&dir, 64 * 1024 * 1024).unwrap();
let mut mgr = SpillManager::new(pool, store2);
let ka = SpillKey::full(300);
let kb = SpillKey::full(301);
mgr.store.write_block(ka, 1, &deterministic_kv(ka, kv_len), None, &HashSet::new()).unwrap();
mgr.store.write_block(kb, 1, &deterministic_kv(kb, kv_len), None, &HashSet::new()).unwrap();
let pb = mgr.store.block_path(kb);
let mut raw_b = fs::read(&pb).unwrap();
raw_b[SPILL_HEADER_LEN + 7] ^= 0x80;
fs::write(&pb, &raw_b).unwrap();
mgr.index_existing_disk_blocks();
let prep = mgr.prepare_working_set_restore(&[ka, kb], &[]).unwrap();
let miss = mgr.execute_working_set_restore(prep).unwrap_err();
assert_eq!(miss.failed_key, kb);
assert_eq!(miss.reason, MissReason::ChecksumMismatch);
assert_eq!(mgr.active_reservation_count(), 0);
assert_eq!(mgr.pool().used_slots(SpillBlockKind::Full), 0);
assert_eq!(mgr.pool().free_slots(SpillBlockKind::Full), 3);
for slot_i in 1..=3u32 {
assert!(
mgr.pool().raw_slot_bytes(SpillBlockKind::Full, slot_i).iter().all(|&b| b == 0),
"slot {slot_i} must remain zero-filled after corrupt-block rollback"
);
}
}
#[test]
#[ignore = "internal child helper spawned by kill9_mid_write_loses_cache_never_correctness"]
fn kill9_writer_helper() {
let Ok(dir) = std::env::var("INKY_SPILL_KILL9_DIR") else {
return;
};
let round: u64 = std::env::var("INKY_SPILL_KILL9_ROUND")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(0);
let kv_len: usize = std::env::var("INKY_SPILL_KILL9_KV_LEN")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(256 * 1024);
let state_len: usize = std::env::var("INKY_SPILL_KILL9_STATE_LEN")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(32 * 1024);
let mut store = SpillStore::open(&dir, 512 * 1024 * 1024).expect("child open spill store");
let empty = HashSet::new();
let mut seq = 0u64;
loop {
let id = (round << 32) | seq;
let kind = if seq % 4 == 0 {
SpillBlockKind::Window
} else {
SpillBlockKind::Full
};
let key = SpillKey::new(kind, id);
let kv = deterministic_kv(key, kv_len);
let st = if seq % 3 == 0 {
Some(deterministic_state(key, state_len))
} else {
None
};
let _ = store.write_block(key, seq + 1, &kv, st.as_deref(), &empty);
if seq == 0 {
println!("READY");
let _ = io::stdout().flush();
} else {
println!("COMMIT {seq}");
let _ = io::stdout().flush();
}
seq = seq.wrapping_add(1);
if seq >= 500 {
break;
}
}
}
#[cfg(unix)]
#[test]
fn kill9_mid_write_loses_cache_never_correctness() {
use std::os::unix::process::ExitStatusExt;
let scratch = ScratchTestDir::new("kill9-gate");
let exe = std::env::current_exe().expect("current test binary path");
let kv_len = 256 * 1024usize; let state_len = 32 * 1024usize;
let mut total_hits = 0u64;
let mut total_misses = 0u64;
let mut total_orphans_swept = 0u64;
let mut total_kills_verified = 0u64;
let rounds = 10u64;
for round in 0..rounds {
let dir = scratch.path().join(format!("round-{round}"));
prepare_spill_dir(&dir).unwrap();
let mut child = Command::new(&exe)
.args([
"--exact",
"spill::tests::kill9_writer_helper",
"--ignored",
"--nocapture",
])
.env("INKY_SPILL_KILL9_DIR", &dir)
.env("INKY_SPILL_KILL9_ROUND", round.to_string())
.env("INKY_SPILL_KILL9_KV_LEN", kv_len.to_string())
.env("INKY_SPILL_KILL9_STATE_LEN", state_len.to_string())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.expect("spawn kill9 writer helper");
let child_pid = child.id() as libc::pid_t;
let stdout = child.stdout.take().expect("child stdout");
let mut reader = BufReader::new(stdout);
let mut line = String::new();
let mut saw_ready = false;
let deadline = Instant::now() + Duration::from_secs(10);
while Instant::now() < deadline {
line.clear();
match reader.read_line(&mut line) {
Ok(0) => break,
Ok(_) => {
if line.trim() == "READY" {
saw_ready = true;
break;
}
}
Err(_) => break,
}
}
assert!(saw_ready, "round {round}: child writer did not report READY");
if round % 2 == 0 {
let hunt_deadline = Instant::now() + Duration::from_millis(250);
while Instant::now() < hunt_deadline {
let has_tmp = fs::read_dir(&dir)
.ok()
.map(|rd| {
rd.filter_map(Result::ok)
.any(|e| e.file_name().to_string_lossy().contains(".tmp."))
})
.unwrap_or(false);
if has_tmp {
break;
}
std::thread::yield_now();
}
} else {
std::thread::sleep(Duration::from_millis(2 + (round * 3) % 17));
}
let rc = unsafe { libc::kill(child_pid, libc::SIGKILL) };
assert_eq!(rc, 0, "round {round}: SIGKILL failed");
let status = child.wait().expect("wait for killed child");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"round {round}: child should terminate from SIGKILL, got {status:?}"
);
total_kills_verified += 1;
let mut store = SpillStore::open(&dir, 512 * 1024 * 1024).expect("recover spill store");
total_orphans_swept += store.stats().orphans_swept;
let remaining_tmps: Vec<_> = fs::read_dir(&dir)
.unwrap()
.filter_map(Result::ok)
.filter(|e| e.file_name().to_string_lossy().contains(".tmp."))
.collect();
assert!(
remaining_tmps.is_empty(),
"round {round}: orphan tmp files remained after open(): {remaining_tmps:?}"
);
let max_id = store
.entries
.keys()
.map(|k| k.id & 0xffff_ffff)
.max()
.unwrap_or(0);
let check_limit = (max_id + 8).min(500);
for seq in 0..=check_limit {
let id = (round << 32) | seq;
let kind = if seq % 4 == 0 {
SpillBlockKind::Window
} else {
SpillBlockKind::Full
};
let key = SpillKey::new(kind, id);
let expected_kv = deterministic_kv(key, kv_len);
let expected_state = if seq % 3 == 0 {
Some(deterministic_state(key, state_len))
} else {
None
};
let (recovered_kv, recovered_state) = match store.read_block(key, Some(kv_len)) {
ReadOutcome::Hit(blk) => {
total_hits += 1;
(blk.kv_bytes, blk.recurrent_state)
}
ReadOutcome::Miss(_) => {
total_misses += 1;
(deterministic_kv(key, kv_len), expected_state.clone())
}
};
assert_eq!(
recovered_kv, expected_kv,
"round {round} seq {seq}: K/V bytes must be bit-identical"
);
assert_eq!(
recovered_state, expected_state,
"round {round} seq {seq}: recurrent state must be bit-identical"
);
}
}
assert_eq!(total_kills_verified, rounds);
assert!(total_hits >= rounds, "expected committed hits across SIGKILL rounds, got {total_hits}");
assert!(total_misses > 0, "expected cache misses on interrupted tail, got {total_misses}");
eprintln!(
"[kill9-gate] rounds={rounds} kills={total_kills_verified} hits={total_hits} misses={total_misses} orphans_swept={total_orphans_swept}"
);
}
#[test]
fn pending_restore_blocks_are_protected_from_disk_gc() {
let scratch = ScratchTestDir::new("io-pin-gc");
let dir = scratch.path().join("spill");
let stride = 2048usize;
let one_file = (SPILL_HEADER_LEN + stride) as u64;
let mut store = SpillStore::open(&dir, one_file * 2).unwrap();
let k1 = SpillKey::full(1);
let k2 = SpillKey::full(2);
store.write_block(k1, 1, &deterministic_kv(k1, stride), None, &HashSet::new()).unwrap();
store.write_block(k2, 1, &deterministic_kv(k2, stride), None, &HashSet::new()).unwrap();
let pool = HostBlockPoolBackend::new(2, 1, stride, stride);
let mut mgr = SpillManager::new(pool, store);
mgr.index_existing_disk_blocks();
let k3 = SpillKey::full(3);
let s3 = mgr.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
mgr.pool_mut().write_and_seal(s3, &deterministic_kv(k3, stride)).unwrap();
mgr.register_sealed_block(k3, s3, None).unwrap();
let prep_a = mgr.prepare_working_set_restore(&[k1], &[]).unwrap();
let _s4 = mgr.alloc_slot_lazy(SpillBlockKind::Full).unwrap();
assert!(mgr.store().contains(k1), "io-pinned block k1 must survive disk GC");
assert!(!mgr.store().contains(k2), "unpinned block k2 should be evicted by disk GC");
assert!(mgr.store().contains(k3), "spilled block k3 should be on disk");
let res_a = mgr.execute_working_set_restore(prep_a).unwrap();
assert_eq!(res_a.full_slots.len(), 1);
assert_eq!(
mgr.pool().read_slot_bytes(res_a.full_slots[0].1).unwrap(),
deterministic_kv(k1, stride).as_slice()
);
}
#[test]
fn concurrent_restores_of_shared_disk_block_deduplicate_without_slot_leak() {
let scratch = ScratchTestDir::new("dedup-restore");
let dir = scratch.path().join("spill");
let stride = 2048usize;
let mut store = SpillStore::open(&dir, 64 * 1024 * 1024).unwrap();
let shared = SpillKey::full(77);
let state = deterministic_state(shared, 512);
store
.write_block(shared, 1, &deterministic_kv(shared, stride), Some(&state), &HashSet::new())
.unwrap();
let pool = HostBlockPoolBackend::new(2, 1, stride, stride);
let mut mgr = SpillManager::new(pool, store);
mgr.index_existing_disk_blocks();
let prep_a = mgr.prepare_working_set_restore(&[shared], &[]).unwrap();
let prep_b = mgr.prepare_working_set_restore(&[shared], &[]).unwrap();
assert_eq!(mgr.pool().used_slots(SpillBlockKind::Full), 2);
let res_a = mgr.execute_working_set_restore(prep_a).unwrap();
let slot_a = res_a.full_slots[0].1;
let res_b = mgr.execute_working_set_restore(prep_b).unwrap();
let slot_b = res_b.full_slots[0].1;
assert_eq!(slot_a, slot_b, "concurrent restores of same block must share one slot");
assert_eq!(mgr.pool().refs(slot_a).unwrap(), 2);
assert_eq!(mgr.pool().used_slots(SpillBlockKind::Full), 1);
assert_eq!(mgr.pool().free_slots(SpillBlockKind::Full), 1);
assert_eq!(res_b.recurrent_states.get(&shared), Some(&state));
assert_eq!(mgr.unpin(shared).unwrap(), 1);
assert_eq!(mgr.unpin(shared).unwrap(), 0);
}
#[test]
fn full_scale_block_and_recurrent_state_roundtrip() {
let scratch = ScratchTestDir::new("full-scale");
let dir = scratch.path().join("spill");
let cs_bytes = crate::engine::Engine::cs_image_bytes();
let mut store = SpillStore::open(&dir, 32 * 1024 * 1024).unwrap();
let key = SpillKey::full(4242);
let kv = deterministic_kv(key, FULL_STRIDE);
let st = deterministic_state(key, cs_bytes);
let (out, evicted) = store
.write_block(key, 9, &kv, Some(&st), &HashSet::new())
.unwrap();
assert!(evicted.is_empty());
assert_eq!(
out,
WriteOutcome::Written {
on_disk_bytes: (SPILL_HEADER_LEN + FULL_STRIDE + cs_bytes) as u64
}
);
match store.read_block(key, Some(FULL_STRIDE)) {
ReadOutcome::Hit(blk) => {
assert_eq!(blk.key, key);
assert_eq!(blk.generation, 9);
assert_eq!(blk.kv_bytes, kv);
assert_eq!(blk.recurrent_state.as_deref(), Some(st.as_slice()));
}
ReadOutcome::Miss(r) => panic!("expected full-scale hit, got {r:?}"),
}
}
}