From 298bf6b86f4e5b67decf8a629cd18b17a5200e92 Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sat, 7 Nov 2026 17:46:42 +0000 Subject: [PATCH 1/4] Rust MongoDB server: a spilled sort's file is mongod's, chunked and snappy-compressed (RM-325) The sorter writes mongod 8.2's SortedFileWriter format: serializeForSorter records in 64 KiB chunks, each compressed by a port of mongod's snappy 1.1.10 (CRC32-C hash). spilledDataStorageSize is now mongod's to the byte for a sort that sees mongod's input order and for $bucketAuto. RM-325 stays open for the $group and $setWindowFields spills; filed RM-395, RM-396, RM-397. Co-Authored-By: Claude Opus 5.5 --- changelog.d/rm-325-spill-file-format.md | 14 + crates/Cargo.lock | 1 + crates/secantus-core/Cargo.toml | 3 + crates/secantus-core/src/lib.rs | 2 + crates/secantus-core/src/snappy.rs | 314 +++++++++++ crates/secantus-core/src/sorter.rs | 323 ++++++----- crates/secantus-core/src/spill_file.rs | 627 +++++++++++++++++++++ crates/secantus-pg/Cargo.lock | 7 + crates/secantus-server-py/Cargo.lock | 1 + crates/secantus-storage-adapter/Cargo.lock | 1 + crates/secantus-storage-py/Cargo.lock | 7 + crates/secantus-storage/Cargo.lock | 7 + crates/secantusdb/Cargo.lock | 1 + tasks/backlog-rust-mongodb.md | 58 +- tests/backlog/test_rm_found_2026_10_26.py | 140 ++++- 15 files changed, 1350 insertions(+), 156 deletions(-) create mode 100644 changelog.d/rm-325-spill-file-format.md create mode 100644 crates/secantus-core/src/snappy.rs create mode 100644 crates/secantus-core/src/spill_file.rs diff --git a/changelog.d/rm-325-spill-file-format.md b/changelog.d/rm-325-spill-file-format.md new file mode 100644 index 000000000..ac7713192 --- /dev/null +++ b/changelog.d/rm-325-spill-file-format.md @@ -0,0 +1,14 @@ +### A spilled sort takes the bytes on disk that mongod's does + +When a sort passes its memory budget, mongod writes its sorted runs to a spill file, and the profiler, `explain` and `serverStatus` report the file's size as `spilledDataStorageSize`. The Rust MongoDB server spilled the same runs, records and bytes, but wrote them as uncompressed BSON, so it reported about twenty times mongod's figure: 2480400 bytes where mongod 8.2 reports 130545. Its sorter now writes mongod's file. Each record is in mongod's own `serializeForSorter` form, the records go into 64 KiB chunks, and each chunk is snappy-compressed by a port of the compressor mongod builds in (snappy 1.1.10, with its CRC32-C hash), or stored as it is when compression saves less than a tenth. A query-layer sort, a pipeline `$sort` and `$bucketAuto` now report mongod's size to the byte whenever the sorter sees its documents in the order mongod's does. The size was checked by decoding mongod's own spill file, copied out of `_tmp` while a cursor held it open. + +Some spills still differ, and each is on the backlog. A classic `$group` writes this server's own partial states, and a `$setWindowFields` uses a file where mongod uses a WiredTiger table (RM-325). A sort that reorders the documents a scan reads is fed them already sorted, so it spills different runs (RM-395). + +#### Fixed + +- `secantus-core` (`sorter`, new `spill_file`, new `snappy`): a sort's runs are written as mongod 8.2's `SortedFileWriter` writes them. A record is a `Value` key (an array for a compound sort), then the document: as BSON for the query layer's sort, as a `Document` of the fields kept for a pipeline `$sort`, and inside `{output, generatedSortKey}` for `$bucketAuto`. Records go into 64 KiB chunks, each prefixed with its signed `int32` length and snappy-compressed when that saves a tenth. `spilledBytes` is now the bytes before compression, and `spilledDataStorageSize` and `metrics.query.sort.spillToDiskBytes` are the bytes written. Records convert from this crate's BSON and back byte for byte, so strings that are not UTF-8 and escaped field names survive a spill. The file's size on disk is checked against what was written. +- Partly fixes RM-325 (tests/backlog/test_rm_found_2026_10_26.py::test_spill_file_storage_size). The item stays open for the `$group` and `$setWindowFields` spills (`test_spill_storage_size`). +- Regression tests: `sorter::tests::spills_and_merges_count_as_mongods_do` (130545 bytes, merges included), `sorter::tests::a_spill_file_takes_what_mongods_takes`, `spill_file::tests::*` and `snappy::tests::*` (the CRC matches the arm64 `__crc32cw` instruction). +- Filed RM-395: a query-layer sort is handed documents already sorted, so it spills different runs from mongod's (`test_leading_sort_spill_storage_size`). +- Filed RM-396: a `$sort` after `$addFields` spills at slightly different record boundaries (`test_added_fields_sort_spill_storage_size`). +- Filed RM-397: a `$group` of `$sum` and `$count` counts 16 bytes a group less memory than mongod (`test_group_sum_spilled_bytes`). diff --git a/crates/Cargo.lock b/crates/Cargo.lock index e58af8666..9dc311d39 100644 --- a/crates/Cargo.lock +++ b/crates/Cargo.lock @@ -1786,6 +1786,7 @@ dependencies = [ "rand 0.9.4", "regex", "regex-automata", + "snap", ] [[package]] diff --git a/crates/secantus-core/Cargo.toml b/crates/secantus-core/Cargo.toml index 67bba2514..fa3b8e3af 100644 --- a/crates/secantus-core/Cargo.toml +++ b/crates/secantus-core/Cargo.toml @@ -29,6 +29,9 @@ geo = "0.28" # backtrack; it's tried first for every pattern. Patterns it can't compile # (backreferences / lookaround) fall back to `fancy-regex` below. regex = "1" +# Reads back the snappy chunks of a spill file (`spill_file.rs`); they are +# written by `snappy.rs`, mongod's own compressor ported. +snap = "1" # The longest-match, rule-ordered scanner of `timelib` (the date parser mongod # uses for `$dateFromString` / `$toDate`) needs a DFA that reports EVERY # pattern matching at each length; `regex` only offers leftmost-first. Already diff --git a/crates/secantus-core/src/lib.rs b/crates/secantus-core/src/lib.rs index 1b1f4cb87..c5acefe51 100644 --- a/crates/secantus-core/src/lib.rs +++ b/crates/secantus-core/src/lib.rs @@ -44,8 +44,10 @@ pub mod query; pub mod rawstr; pub mod s2legacy; pub mod sbe; +mod snappy; pub mod sorter; pub mod sortkey; +mod spill_file; mod stl; pub(crate) mod timelib; pub mod update; diff --git a/crates/secantus-core/src/snappy.rs b/crates/secantus-core/src/snappy.rs new file mode 100644 index 000000000..10d5f86c9 --- /dev/null +++ b/crates/secantus-core/src/snappy.rs @@ -0,0 +1,314 @@ +//! snappy's compressor as mongod 8.2 builds it (RM-325): snappy 1.1.10 +//! (`src/third_party/snappy/dist`), whose hash table is indexed by a CRC32-C +//! on every platform mongod ships for -- its `config.h` for Linux x86-64 +//! sets `SNAPPY_HAVE_X86_CRC32`, for Linux and macOS arm64 +//! `SNAPPY_HAVE_NEON_CRC32`, and both instructions compute the same +//! function. +//! +//! The format is snappy's, so any decoder reads it (the `snap` crate reads +//! it back here), but which matches a compressor finds -- and so how many +//! bytes it writes -- is the compressor's own: the hash, the table's size +//! for each 64 KiB fragment, the sixteen probes it makes before skipping +//! ahead. mongod's sorter reports the compressed size of every chunk it +//! spills, so this is a port of `CompressFragment` line for line, not a +//! use of another snappy implementation (the `snap` crate ports an older +//! one, and spilled a sort 11 bytes off mongod's). + +/// `kBlockSize`: input is compressed in fragments of this size. +const BLOCK: usize = 1 << 16; +/// `kMinHashTableBits`, `kMaxHashTableBits`. +const MIN_TABLE: usize = 1 << 8; +const MAX_TABLE: usize = 1 << 14; +/// `kInputMarginBytes`. +const MARGIN: usize = 15; + +/// The CRC32-C table, reflected (polynomial 0x82F63B78). +const CRC32C: [u32; 256] = { + let mut t = [0u32; 256]; + let mut i = 0; + while i < 256 { + let mut c = i as u32; + let mut k = 0; + while k < 8 { + c = if c & 1 != 0 { + (c >> 1) ^ 0x82F6_3B78 + } else { + c >> 1 + }; + k += 1; + } + t[i] = c; + i += 1; + } + t +}; + +/// `_mm_crc32_u32(crc, v)` / `__crc32cw(crc, v)`: the CRC32-C of `v`'s +/// four bytes from `crc`, with no inversion either side. +fn crc32c_u32(crc: u32, v: u32) -> u32 { + let mut c = crc ^ v; + for _ in 0..4 { + c = CRC32C[(c & 0xff) as usize] ^ (c >> 8); + } + c +} + +/// `TableEntry`: the slot for these four bytes. `mask` is twice the +/// table's last index, as the C++ masks a byte offset into a `uint16_t` +/// table. +fn entry(bytes: u32, mask: u32) -> usize { + ((crc32c_u32(bytes, mask) & mask) >> 1) as usize +} + +/// `CalculateTableSize`. +fn table_size(n: usize) -> usize { + if n > MAX_TABLE { + MAX_TABLE + } else if n < MIN_TABLE { + MIN_TABLE + } else { + 2 << (usize::BITS - 1 - (n - 1).leading_zeros()) + } +} + +/// `snappy::Compress` of `input`. +pub(crate) fn compress(input: &[u8]) -> Vec { + let mut out = Vec::with_capacity(32 + input.len() + input.len() / 6); + let mut n = u32::try_from(input.len()).expect("a spill chunk is under 4 GiB"); + // `Varint::Encode32`. + while n >= 0x80 { + out.push((n as u8) | 0x80); + n >>= 7; + } + out.push(n as u8); + let mut table = vec![0u16; MAX_TABLE]; + for fragment in input.chunks(BLOCK) { + let size = table_size(fragment.len()); + table[..size].fill(0); + compress_fragment(fragment, &mut table[..size], &mut out); + } + out +} + +fn load32(b: &[u8], i: usize) -> u32 { + u32::from_le_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) +} + +fn load64(b: &[u8], i: usize) -> u64 { + let mut a = [0u8; 8]; + a.copy_from_slice(&b[i..i + 8]); + u64::from_le_bytes(a) +} + +/// `EmitLiteral`. +fn emit_literal(out: &mut Vec, literal: &[u8]) { + let n = literal.len() - 1; + if n < 60 { + out.push((n << 2) as u8); + } else { + let count = ((usize::BITS - 1 - n.leading_zeros()) >> 3) as usize + 1; + out.push(((59 + count) << 2) as u8); + out.extend_from_slice(&(n as u32).to_le_bytes()[..count]); + } + out.extend_from_slice(literal); +} + +/// `EmitCopyAtMost64`. +fn emit_copy_at_most_64(out: &mut Vec, offset: usize, len: usize) { + debug_assert!((4..=64).contains(&len) && offset < 65536); + if len < 12 && offset < 2048 { + out.push((1 | ((len - 4) << 2) | ((offset >> 8) << 5)) as u8); + out.push(offset as u8); + } else { + out.push((2 | ((len - 1) << 2)) as u8); + out.extend_from_slice(&(offset as u16).to_le_bytes()); + } +} + +/// `EmitCopy`. +fn emit_copy(out: &mut Vec, offset: usize, mut len: usize) { + if len < 12 { + return emit_copy_at_most_64(out, offset, len); + } + while len >= 68 { + emit_copy_at_most_64(out, offset, 64); + len -= 64; + } + if len > 64 { + emit_copy_at_most_64(out, offset, 60); + len -= 60; + } + emit_copy_at_most_64(out, offset, len); +} + +/// `FindMatchLength`: how many bytes from `s1` match those from `s2`, +/// `s2` reading no further than the end. +fn match_length(b: &[u8], s1: usize, s2: usize) -> usize { + b[s2..] + .iter() + .zip(&b[s1..]) + .take_while(|(x, y)| x == y) + .count() +} + +/// `internal::CompressFragment`. Where the C++ carries the next bytes in +/// `data` it reads them again here: it only ever holds the input at `ip`. +fn compress_fragment(input: &[u8], table: &mut [u16], out: &mut Vec) { + let end = input.len(); + let mask = 2 * (table.len() as u32 - 1); + let mut ip = 0usize; + if end >= MARGIN { + let limit = end - MARGIN; + let mut preload = load32(input, ip + 1); + 'emit: loop { + let next_emit = ip; + ip += 1; + let mut data = load64(input, ip); + let mut skip: u32 = 32; + let mut candidate: usize = 0; + let mut found = false; + if limit >= ip && limit - ip >= 16 { + let delta = ip; + 'probe: for j in 0..4 { + for k in 0..4 { + let i = 4 * j + k; + let dword = if i == 0 { preload } else { data as u32 }; + let e = entry(dword, mask); + candidate = table[e] as usize; + table[e] = (delta + i) as u16; + if load32(input, candidate) == dword { + // A literal of the i + 1 bytes before the match. + out.push((i << 2) as u8); + out.extend_from_slice(&input[next_emit..next_emit + i + 1]); + ip += i; + found = true; + break 'probe; + } + data >>= 8; + } + data = load64(input, ip + 4 * j + 4); + } + if !found { + ip += 16; + skip += 16; + } + } + if !found { + loop { + let e = entry(data as u32, mask); + let between = (skip >> 5) as usize; + skip += skip >> 5; + let next_ip = ip + between; + if next_ip > limit { + ip = next_emit; + break 'emit; + } + candidate = table[e] as usize; + table[e] = ip as u16; + if data as u32 == load32(input, candidate) { + break; + } + data = load32(input, next_ip) as u64; + ip = next_ip; + } + emit_literal(out, &input[next_emit..ip]); + } + // emit_match: + loop { + let base = ip; + let matched = 4 + match_length(input, candidate + 4, ip + 4); + ip += matched; + emit_copy(out, base - candidate, matched); + if ip >= limit { + break 'emit; + } + let e = entry(load32(input, ip - 1), mask); + table[e] = (ip - 1) as u16; + let here = load32(input, ip); + let e = entry(here, mask); + candidate = table[e] as usize; + table[e] = ip as u16; + if here != load32(input, candidate) { + break; + } + } + preload = load32(input, ip + 1); + } + } + if ip < end { + emit_literal(out, &input[ip..]); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn round_trip(input: &[u8]) -> Vec { + let c = compress(input); + let back = snap::raw::Decoder::new().decompress_vec(&c).unwrap(); + assert_eq!(back, input); + c + } + + #[test] + fn crc32c_is_the_instructions() { + // `_mm_crc32_u32(0, 0)` is 0, and the standard CRC32-C check value + // of "123456789" (inverted either side) is 0xE3069283. + assert_eq!(crc32c_u32(0, 0), 0); + let mut c = !0u32; + for b in b"123456789" { + c = CRC32C[((c ^ *b as u32) & 0xff) as usize] ^ (c >> 8); + } + assert_eq!(!c, 0xE306_9283); + } + + #[cfg(target_arch = "aarch64")] + #[test] + fn crc32c_is_the_arm_instruction() { + let mut x: u32 = 0x1234_5678; + for _ in 0..10_000 { + x = x.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + let m = 2 * ((1u32 << (x % 7 + 8)) - 1); + // SAFETY: every arm64 Mac has the CRC extension. + let hw = unsafe { std::arch::aarch64::__crc32cw(x, m) }; + assert_eq!(crc32c_u32(x, m), hw); + } + } + + #[test] + fn table_sizes_are_snappys() { + assert_eq!(table_size(1), 256); + assert_eq!(table_size(256), 256); + assert_eq!(table_size(257), 512); + assert_eq!(table_size(1000), 1024); + assert_eq!(table_size(1 << 14), 1 << 14); + assert_eq!(table_size(1 << 16), 1 << 14); + } + + #[test] + fn short_and_incompressible_inputs_are_one_literal() { + assert_eq!(round_trip(b""), [0]); + assert_eq!(round_trip(b"abc"), [3, 2 << 2, b'a', b'b', b'c']); + let mut x: u64 = 0x9E37_79B9_7F4A_7C15; + let noise: Vec = (0..70_000) + .map(|_| { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + x as u8 + }) + .collect(); + round_trip(&noise); + } + + #[test] + fn runs_become_copies() { + // A literal of one byte, then copies of the run at offset 1: 64, + // 64, ..., as `EmitCopy` splits them. + let c = round_trip(&[b'x'; 200]); + assert_eq!(&c[..3], &[200, 1, 0][..]); + assert_eq!(c[3], b'x'); + round_trip(&b"abcdabcdabcdefghefghefgh".repeat(5000)); + } +} diff --git a/crates/secantus-core/src/sorter.rs b/crates/secantus-core/src/sorter.rs index 55d2e8906..9315341e0 100644 --- a/crates/secantus-core/src/sorter.rs +++ b/crates/secantus-core/src/sorter.rs @@ -32,22 +32,20 @@ use std::cmp::Ordering; use std::collections::BTreeSet; -use std::fs::File; -use std::io::{BufReader, BufWriter, Read, Write}; use std::path::PathBuf; use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering}; use bson::{doc, Bson, Document}; use crate::fallback::Fallback; -use crate::memlimit; +use crate::{memlimit, spill_file}; type R = Result; /// `sizeof(FileIterator)` on mongod 8.2.11 (arm64), measured. const FILE_ITERATOR: usize = 136; /// `kSortedFileBufferSize`. -const SORTED_FILE_BUFFER: usize = 64 << 10; +const SORTED_FILE_BUFFER: usize = spill_file::CHUNK; /// Which of mongod's sorts this is, for what a document counts. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -164,72 +162,18 @@ fn refused(budget: usize) -> Fallback { static NEXT_FILE: AtomicU64 = AtomicU64::new(0); -/// `Value::serializeForSorter`: the bytes one value takes in a spill. -fn value_bytes(v: &Bson) -> usize { - 1 + match v { - Bson::Null | Bson::Undefined | Bson::MinKey | Bson::MaxKey => 0, - Bson::ObjectId(_) => 12, - Bson::Int32(_) => 4, - Bson::Int64(_) | Bson::Double(_) | Bson::DateTime(_) | Bson::Timestamp(_) => 8, - Bson::Decimal128(_) => 16, - Bson::Boolean(_) => 1, - Bson::String(x) | Bson::Symbol(x) | Bson::JavaScriptCode(x) => 4 + x.len(), - Bson::Binary(b) => 1 + 4 + b.bytes.len(), - Bson::RegularExpression(r) => r.pattern.len() + 1 + r.options.len() + 1, - Bson::Document(d) => document_bytes(d), - Bson::Array(a) => 4 + a.iter().map(value_bytes).sum::(), - Bson::JavaScriptCodeWithScope(c) => 4 + c.code.len() + memlimit::bson_len(&c.scope), - Bson::DbPointer(_) => 12 + 33, - } -} +/// A key and its document as the sorter holds them, with the document's +/// position in the input (`$bucketAuto` writes it into each record). +type Held = (Vec, Document, i64); -/// `Document::serializeForSorter`: its fields, then its metadata (a byte -/// when it has none). -fn document_bytes(d: &Document) -> usize { - 4 + d - .iter() - .map(|(k, v)| k.len() + 1 + value_bytes(v)) - .sum::() - + 1 -} - -/// The bytes a key and its document take in a spill (the sort's -/// `spilledBytes`): the key as a `Value` (an array of them for a compound -/// sort) and the document as this sorter keeps it -- a classic `Document`, -/// the query layer's working-set member (its BSON), or `$bucketAuto`'s -/// wrapper around the document. -fn spilled_bytes(keys: &[Bson], doc: &Document, sizing: &Sizing) -> usize { - let key = match keys { - [one] => value_bytes(one), - many => 1 + 4 + many.iter().map(value_bytes).sum::(), - }; - let kept = || -> Document { - doc.iter() - .filter(|(k, _)| { - sizing.fields.is_none_or(|f| f.contains(k.as_str())) - || sizing.added.is_some_and(|a| a.contains(k.as_str())) - }) - .map(|(k, v)| (k.clone(), v.clone())) - .collect() - }; - let value = match sizing.source { - SortSource::QueryLayer => memlimit::bson_len(doc), - SortSource::Sbe => memlimit::bson_len(&kept()), - SortSource::BucketAuto => { - // `{output: , generatedSortKey: }`. - 4 + 7 + 1 + document_bytes(&kept()) + 17 + 9 + 1 - } - SortSource::Stored | SortSource::Modified | SortSource::Unwound => document_bytes(&kept()), - }; - key + value -} - -fn io_error(what: &str, path: &std::path::Path, e: impl std::fmt::Display) -> Fallback { +fn io_error(path: &std::path::Path, e: spill_file::Error) -> Fallback { Fallback::mongo( 1, format!( - "the sort could not {what} its spill file {}: {e}", - path.display() + "the sort could not {} its spill file {}: {}", + e.what, + path.display(), + e.detail ), ) } @@ -238,6 +182,9 @@ fn io_error(what: &str, path: &std::path::Path, e: impl std::fmt::Display) -> Fa struct Run { path: PathBuf, records: usize, + /// How many values each key holds: one is written as itself, more as + /// an array of them. + arity: usize, } impl Drop for Run { @@ -253,111 +200,169 @@ impl Drop for Run { } } +/// The document as the sort keeps it: only the fields mongod's dependency +/// analysis leaves it, and those a stage before it added. +fn kept(doc: &Document, sizing: &Sizing) -> Document { + if sizing.fields.is_none() { + return doc.clone(); + } + doc.iter() + .filter(|(k, _)| { + sizing.fields.is_none_or(|f| f.contains(k.as_str())) + || sizing.added.is_some_and(|a| a.contains(k.as_str())) + }) + .map(|(k, v)| (k.clone(), v.clone())) + .collect() +} + +/// One record as this sorter's mongod counterpart serializes it: the key +/// as a `Value` (an array of them for a compound sort), then the document +/// as that sorter keeps it -- the query layer's (and SBE's) as BSON, a +/// pipeline `$sort`'s as a `Document`, `$bucketAuto`'s inside the +/// `{output, generatedSortKey}` document it sorts. +fn write_record( + (keys, d, pos): &Held, + sizing: &Sizing, + out: &mut Vec, +) -> Result<(), spill_file::Error> { + match keys.as_slice() { + [one] => spill_file::write_value(one, out)?, + many => spill_file::write_value(&Bson::Array(many.to_vec()), out)?, + } + match sizing.source { + SortSource::QueryLayer => spill_file::write_bson(d, out), + SortSource::Sbe => spill_file::write_bson(&kept(d, sizing), out), + SortSource::BucketAuto => spill_file::write_document( + &doc! { "output": kept(d, sizing), "generatedSortKey": *pos }, + out, + ), + SortSource::Stored | SortSource::Modified | SortSource::Unwound => { + spill_file::write_document(&kept(d, sizing), out) + } + } +} + +fn read_record( + at: &mut spill_file::Cursor, + arity: usize, + source: SortSource, +) -> Result { + let key = spill_file::read_value(at)?; + let keys = match (arity, key) { + (1, k) => vec![k], + (_, Bson::Array(k)) if k.len() == arity => k, + _ => { + return Err(spill_file::Error { + what: "read", + detail: "a record whose key is not the sort's".into(), + }) + } + }; + let (d, pos) = match source { + SortSource::QueryLayer | SortSource::Sbe => (spill_file::read_bson(at)?, 0), + SortSource::BucketAuto => { + let mut w = spill_file::read_document(at)?; + match (w.remove("output"), w.remove("generatedSortKey")) { + (Some(Bson::Document(d)), Some(Bson::Int64(pos))) => (d, pos), + _ => { + return Err(spill_file::Error { + what: "read", + detail: "a $bucketAuto record without its document".into(), + }) + } + } + } + SortSource::Stored | SortSource::Modified | SortSource::Unwound => { + (spill_file::read_document(at)?, 0) + } + }; + Ok((keys, d, pos)) +} + /// Write sorted `items` as a run, counted as one spill. -fn write_run(items: impl Iterator, Document)>, sizing: &Sizing) -> R { +fn write_run(items: impl Iterator, sizing: &Sizing) -> R { let bucket_auto = sizing.source == SortSource::BucketAuto; let dir = memlimit::spill_dir(); - std::fs::create_dir_all(&dir).map_err(|e| io_error("create the directory for", &dir, e))?; + std::fs::create_dir_all(&dir).map_err(|e| { + io_error( + &dir, + spill_file::Error { + what: "create the directory for", + detail: e.to_string(), + }, + ) + })?; let path = dir.join(format!( - "secantus-sort-{}-{}.bson", + "secantus-sort-{}-{}.spill", std::process::id(), NEXT_FILE.fetch_add(1, AtomicOrdering::Relaxed) )); - let file = File::create(&path).map_err(|e| io_error("create", &path, e))?; + let mut out = spill_file::Writer::create(&path).map_err(|e| io_error(&path, e))?; + // Registered before anything is written, so a failure below removes it. let mut run = Run { path: path.clone(), records: 0, + arity: 1, }; - let mut out = BufWriter::new(file); - let mut bytes = 0i64; - for (keys, d) in items { - bytes += spilled_bytes(&keys, &d, sizing) as i64; - let mut buf = Vec::new(); - crate::bsondoc::write(&doc! { "k": keys, "d": d }, &mut buf) - .map_err(|e| io_error("encode a record for", &path, e))?; - out.write_all(&buf) - .map_err(|e| io_error("write", &path, e))?; + for item in items { + run.arity = item.0.len(); + write_record(&item, sizing, out.buffer()).map_err(|e| io_error(&path, e))?; + out.added().map_err(|e| io_error(&path, e))?; run.records += 1; } - out.flush().map_err(|e| io_error("write", &path, e))?; - let file = out - .into_inner() - .map_err(|e| io_error("write", &path, e.error()))?; - file.sync_all().map_err(|e| io_error("write", &path, e))?; - let stored = file - .metadata() - .map_err(|e| io_error("measure", &path, e))? - .len() as i64; + let written = out.done().map_err(|e| io_error(&path, e))?; if bucket_auto { - memlimit::record_bucket_auto_spill(run.records as i64, bytes, stored); + memlimit::record_bucket_auto_spill(run.records as i64, written.bytes, written.stored); } else { - memlimit::record_sort_spill(run.records as i64, bytes, stored); + memlimit::record_sort_spill(run.records as i64, written.bytes, written.stored); } Ok(run) } /// A run being read back. struct Reader { - input: BufReader, + input: spill_file::Reader, path: PathBuf, left: usize, + arity: usize, + source: SortSource, } impl Reader { - fn open(run: &Run) -> R { - let file = File::open(&run.path).map_err(|e| io_error("open", &run.path, e))?; + fn open(run: &Run, source: SortSource) -> R { Ok(Reader { - input: BufReader::new(file), + input: spill_file::Reader::open(&run.path).map_err(|e| io_error(&run.path, e))?, path: run.path.clone(), left: run.records, + arity: run.arity, + source, }) } - fn next(&mut self) -> R, Document)>> { + fn next(&mut self) -> R> { if self.left == 0 { return Ok(None); } self.left -= 1; - let mut len = [0u8; 4]; - self.input - .read_exact(&mut len) - .map_err(|e| io_error("read", &self.path, e))?; - let size = usize::try_from(i32::from_le_bytes(len)) - .ok() - .filter(|n| *n >= 5) - .ok_or_else(|| io_error("read", &self.path, "a record with a bad length"))?; - let mut bytes = vec![0u8; size]; - bytes[..4].copy_from_slice(&len); + let (arity, source) = (self.arity, self.source); self.input - .read_exact(&mut bytes[4..]) - .map_err(|e| io_error("read", &self.path, e))?; - let mut record = - crate::bsondoc::from_bytes(&bytes).map_err(|e| io_error("read", &self.path, e))?; - let keys = match record.remove("k") { - Some(Bson::Array(k)) => k, - _ => return Err(io_error("read", &self.path, "a record without its key")), - }; - let d = match record.remove("d") { - Some(Bson::Document(d)) => d, - _ => { - return Err(io_error( - "read", - &self.path, - "a record without its document", - )) - } - }; - Ok(Some((keys, d))) + .record(|at| read_record(at, arity, source)) + .map(Some) + .map_err(|e| io_error(&self.path, e)) } } /// Merge `runs` (in order) into one stream, equal keys in run order. fn merge_runs( runs: &[Run], + source: SortSource, cmp: &dyn Fn(&[Bson], &[Bson]) -> Ordering, - mut emit: impl FnMut((Vec, Document)) -> R<()>, + mut emit: impl FnMut(Held) -> R<()>, ) -> R<()> { - let mut readers = runs.iter().map(Reader::open).collect::>>()?; + let mut readers = runs + .iter() + .map(|r| Reader::open(r, source)) + .collect::>>()?; let mut heads = Vec::with_capacity(readers.len()); for r in readers.iter_mut() { heads.push(r.next()?); @@ -366,11 +371,11 @@ fn merge_runs( crate::deadline::check()?; let mut least: Option = None; for (i, h) in heads.iter().enumerate() { - let Some((k, _)) = h else { continue }; + let Some((k, _, _)) = h else { continue }; match least { None => least = Some(i), Some(l) => { - let (lk, _) = heads[l].as_ref().expect("a head"); + let (lk, _, _) = heads[l].as_ref().expect("a head"); if cmp(k, lk) == Ordering::Less { least = Some(i); } @@ -398,10 +403,10 @@ fn merge_spills( while runs.len() > target { let mut next = Vec::new(); for chunk in runs.chunks(parallel) { - let mut merged: Vec<(Vec, Document)> = Vec::new(); + let mut merged: Vec = Vec::new(); // Read the chunk through a writer of its own: mongod writes each // merged chunk as a run, a single run included. - merge_runs(chunk, cmp, |item| { + merge_runs(chunk, sizing.source, cmp, |item| { merged.push(item); Ok(()) })?; @@ -424,7 +429,12 @@ pub fn blocking_sort( let budget = sizing.budget; let reserve = reserved(budget, sizing.source.file_iterator()); let effective = budget.saturating_sub(reserve); - let size = |(k, d): &(Vec, Document)| sized_key(k, sizing.source) + doc_size(d, sizing); + let size = |(k, d, _): &Held| sized_key(k, sizing.source) + doc_size(d, sizing); + let items = items + .into_iter() + .enumerate() + .map(|(i, (k, d))| (k, d, i as i64)); + let unheld = |held: Vec| held.into_iter().map(|(k, d, _)| (k, d)).collect(); let n = items.len() as i64; let bucket_auto = sizing.source == SortSource::BucketAuto; let record = |bytes: usize| { @@ -436,7 +446,7 @@ pub fn blocking_sort( if let Some(k) = limit { // mongod's top-k sorter keeps only the k first; a limit of one never // goes near its budget. - let mut items = items; + let mut items: Vec = items.collect(); items.sort_by(|a, b| cmp(&a.0, &b.0)); items.truncate(k); let held: usize = items.iter().map(size).sum(); @@ -444,16 +454,16 @@ pub fn blocking_sort( if k > 1 && held > effective && !memlimit::disk_allowed() { return Err(refused(budget)); } - return Ok(items); + return Ok(unheld(items)); } let iterators = (reserve / sizing.source.file_iterator()).max(1); let parallel = (budget / SORTED_FILE_BUFFER).max(2); - let mut held: Vec<(Vec, Document)> = Vec::new(); + let mut held: Vec = Vec::new(); let mut runs: Vec = Vec::new(); let mut used = 0usize; let mut total = 0usize; - let spill = |held: &mut Vec<(Vec, Document)>, runs: &mut Vec| -> R<()> { + let spill = |held: &mut Vec, runs: &mut Vec| -> R<()> { if held.is_empty() { return Ok(()); } @@ -482,13 +492,13 @@ pub fn blocking_sort( record(total); if runs.is_empty() { held.sort_by(|a, b| cmp(&a.0, &b.0)); - return Ok(held); + return Ok(unheld(held)); } spill(&mut held, &mut runs)?; let runs = merge_spills(runs, parallel, parallel, cmp, sizing)?; let mut out = Vec::with_capacity(n as usize); - merge_runs(&runs, cmp, |item| { - out.push(item); + merge_runs(&runs, sizing.source, cmp, |(k, d, _)| { + out.push((k, d)); Ok(()) })?; Ok(out) @@ -617,6 +627,43 @@ mod tests { (29, 1200, 649_200) ); assert_eq!(report.sort_spilled_bytes, 2_461_200); + // Its file, every run in mongod's 64 KiB snappy chunks (RM-325). + assert_eq!(report.sort_spilled_data_storage_size, 130_545); + } + + /// Run `items` through a query-layer sort under `budget`, already in + /// the order a collection scan reads them: `(spills, spilledBytes, + /// spilledDataStorageSize)`. + fn spilled(items: Vec<(Vec, Document)>, budget: usize) -> (i64, i64, i64) { + let sizing = Sizing { + source: SortSource::QueryLayer, + fields: None, + added: None, + budget, + }; + let (out, report) = memlimit::captured(|| blocking_sort(items, &cmp, &sizing, None)); + out.unwrap(); + ( + report.sort_spills, + report.sort_spilled_bytes, + report.sort_spilled_data_storage_size, + ) + } + + #[test] + fn a_spill_file_takes_what_mongods_takes() { + // Measured against mongod 8.2.1 (arm64), `$sort: {n: 1}` under a + // 200000-byte budget: the profile's `sortSpills`, + // `sortSpilledBytes` and `sortSpilledDataStorageSize`. + let keyed = |d: Document| (vec![d.get("n").unwrap().clone()], d); + let xs = (0..300).map(|i| keyed(doc! { "_id": i, "s": "x".repeat(1000), "n": i })); + assert_eq!(spilled(xs.collect(), 200_000), (2, 310_200, 17_921)); + let ints = (0..5000).map(|i| keyed(doc! { "_id": i, "a": i * 7, "b": i % 13, "n": i })); + assert_eq!(spilled(ints.collect(), 200_000), (2, 200_000, 94_667)); + let only = (0..9000i32).map(|i| keyed(doc! { "_id": i, "n": i64::from(i) })); + assert_eq!(spilled(only.collect(), 200_000), (3, 306_000, 135_744)); + let dbl = (0..9000).map(|i| keyed(doc! { "_id": i, "n": f64::from(i) })); + assert_eq!(spilled(dbl.collect(), 200_000), (3, 306_000, 109_279)); } /// The smallest budget a pipeline runs under with `allowDiskUse: false`, diff --git a/crates/secantus-core/src/spill_file.rs b/crates/secantus-core/src/spill_file.rs new file mode 100644 index 000000000..5dc271377 --- /dev/null +++ b/crates/secantus-core/src/spill_file.rs @@ -0,0 +1,627 @@ +//! A spill file as mongod 8.2's external sorter writes it (RM-325). +//! +//! `SortedFileWriter` (`sorter_template_defs.h`) appends each record's +//! `serializeForSorter` bytes -- the key's, then the value's -- to a buffer, +//! and once the buffer passes `kSortedFileBufferSize` (64 KiB) writes it as +//! one chunk: an `int32` length, then the buffer snappy-compressed (the +//! length negated; [`crate::snappy`] is mongod's compressor) when that saves a tenth, or the buffer as it is. A run's +//! last chunk is written when the run is done. What the file takes on disk +//! -- every chunk's length and bytes -- is the sort's +//! `spilledDataStorageSize`, and the bytes before compression its +//! `spilledBytes`. +//! +//! The records are in mongod's own formats, so the compression sees the +//! bytes mongod's sees: +//! +//! - `Value::serializeForSorter`: the type byte, then the value -- a string +//! as its length and bytes (no NUL), binary data as its subtype, length +//! and bytes, a document in the `Document` form below, an array as its +//! count and values; +//! - `Document::serializeForSorter`: the field count, each field's name (a +//! C string) and `Value`, then its metadata (one zero byte: none); +//! - `BSONObj::serializeForSorter`: the BSON itself. +//! +//! Each is converted from the BSON this crate's encoder writes +//! ([`crate::bsondoc::write`]) and back, byte for byte, so a string that is +//! not UTF-8 or a field name that had to be escaped comes back as it went. + +use std::fs::File; +use std::io::{BufReader, BufWriter, Read, Write}; +use std::path::{Path, PathBuf}; + +use bson::{Bson, Document}; + +/// `kSortedFileBufferSize`. +pub(crate) const CHUNK: usize = 64 << 10; + +/// What a failure is about, for the caller to name in its error. +#[derive(Debug)] +pub(crate) struct Error { + pub what: &'static str, + pub detail: String, +} + +type R = Result; + +fn err(what: &'static str, detail: impl std::fmt::Display) -> Error { + Error { + what, + detail: detail.to_string(), + } +} + +fn corrupt(detail: impl std::fmt::Display) -> Error { + err("read", detail) +} + +// ------------------------------------------------------------- formats -- + +/// The BSON of `{"": v}`: the element after the 4-byte length. +fn element_bson(v: &Bson) -> R> { + let mut d = Document::new(); + d.insert("", v.clone()); + let mut buf = Vec::new(); + crate::bsondoc::write(&d, &mut buf).map_err(|e| err("encode a record for", e))?; + Ok(buf) +} + +/// `Value::serializeForSorter` of `v`. +pub(crate) fn write_value(v: &Bson, out: &mut Vec) -> R<()> { + let bson = element_bson(v)?; + // Length, type, the empty name's NUL, then the value. + let mut at = Cursor::new(&bson[6..]); + value_from_bson(bson[4], &mut at, out) +} + +/// `Document::serializeForSorter` of `d`. +pub(crate) fn write_document(d: &Document, out: &mut Vec) -> R<()> { + let mut bson = Vec::new(); + crate::bsondoc::write(d, &mut bson).map_err(|e| err("encode a record for", e))?; + let mut at = Cursor::new(&bson); + document_from_bson(&mut at, out) +} + +/// `BSONObj::serializeForSorter` of `d`: its BSON. +pub(crate) fn write_bson(d: &Document, out: &mut Vec) -> R<()> { + crate::bsondoc::write(d, out).map_err(|e| err("encode a record for", e)) +} + +/// The value a [`write_value`] wrote. +pub(crate) fn read_value(at: &mut Cursor) -> R { + let mut bson = vec![0, 0, 0, 0]; + let t = at.byte()?; + bson.push(t); + bson.push(0); + value_to_bson(t, at, &mut bson)?; + bson.push(0); + finish_doc(&mut bson, 0)?; + let mut d = crate::bsondoc::from_bytes(&bson).map_err(corrupt)?; + d.remove("") + .ok_or_else(|| corrupt("a value that is not one")) +} + +/// The document a [`write_document`] wrote. +pub(crate) fn read_document(at: &mut Cursor) -> R { + let mut bson = Vec::new(); + document_to_bson(at, &mut bson)?; + crate::bsondoc::from_bytes(&bson).map_err(corrupt) +} + +/// The document a [`write_bson`] wrote. +pub(crate) fn read_bson(at: &mut Cursor) -> R { + let len = at.peek_i32()?; + let len = usize::try_from(len) + .ok() + .filter(|n| *n >= 5) + .ok_or_else(|| corrupt("a document with a bad length"))?; + crate::bsondoc::from_bytes(at.take(len)?).map_err(corrupt) +} + +/// Bytes being read, with every read bounds-checked: a short or damaged +/// spill fails the query, never panics. +pub(crate) struct Cursor<'a> { + bytes: &'a [u8], + pos: usize, +} + +impl<'a> Cursor<'a> { + pub(crate) fn new(bytes: &'a [u8]) -> Self { + Cursor { bytes, pos: 0 } + } + + #[cfg(test)] + fn at_end(&self) -> bool { + self.pos >= self.bytes.len() + } + + fn take(&mut self, n: usize) -> R<&'a [u8]> { + let end = self + .pos + .checked_add(n) + .filter(|e| *e <= self.bytes.len()) + .ok_or_else(|| corrupt("a record that runs past its chunk"))?; + let out = &self.bytes[self.pos..end]; + self.pos = end; + Ok(out) + } + + fn byte(&mut self) -> R { + Ok(self.take(1)?[0]) + } + + fn i32(&mut self) -> R { + let b = self.take(4)?; + Ok(i32::from_le_bytes([b[0], b[1], b[2], b[3]])) + } + + fn peek_i32(&self) -> R { + let b = self + .bytes + .get(self.pos..self.pos + 4) + .ok_or_else(|| corrupt("a record that runs past its chunk"))?; + Ok(i32::from_le_bytes([b[0], b[1], b[2], b[3]])) + } + + fn len(&mut self) -> R { + usize::try_from(self.i32()?).map_err(|_| corrupt("a negative length")) + } + + /// A C string, its NUL included. + fn cstr(&mut self) -> R<&'a [u8]> { + let rest = &self.bytes[self.pos.min(self.bytes.len())..]; + let n = rest + .iter() + .position(|b| *b == 0) + .ok_or_else(|| corrupt("a name without its NUL"))?; + self.take(n + 1) + } +} + +/// The fixed width of a value of BSON type `t`, for the types whose value +/// is the same bytes in both forms. +fn fixed(t: u8) -> Option { + match t { + 0x0A | 0x06 | 0xFF | 0x7F => Some(0), + 0x08 => Some(1), + 0x10 => Some(4), + 0x01 | 0x09 | 0x11 | 0x12 => Some(8), + 0x07 => Some(12), + 0x13 => Some(16), + _ => None, + } +} + +/// One BSON value of type `t` at `at`, written in `Value` form (its type +/// byte first). +fn value_from_bson(t: u8, at: &mut Cursor, out: &mut Vec) -> R<()> { + out.push(t); + if let Some(n) = fixed(t) { + out.extend_from_slice(at.take(n)?); + return Ok(()); + } + match t { + // String, JavaScript, symbol: the length without its NUL. + 0x02 | 0x0D | 0x0E => { + let n = at.len()?; + let s = at.take(n)?; + let text = s.get(..n.saturating_sub(1)).unwrap_or_default(); + out.extend_from_slice(&(text.len() as i32).to_le_bytes()); + out.extend_from_slice(text); + } + 0x03 => document_from_bson(at, out)?, + 0x04 => { + let start = at.pos; + let total = at.len()?; + let mut items = Vec::new(); + let mut count = 0i32; + loop { + let et = at.byte()?; + if et == 0 { + break; + } + at.cstr()?; + value_from_bson(et, at, &mut items)?; + count += 1; + } + if at.pos - start != total { + return Err(corrupt("an array with a bad length")); + } + out.extend_from_slice(&count.to_le_bytes()); + out.extend_from_slice(&items); + } + // Binary data: the subtype, then the length. + 0x05 => { + let n = at.len()?; + let subtype = at.byte()?; + out.push(subtype); + out.extend_from_slice(&(n as i32).to_le_bytes()); + out.extend_from_slice(at.take(n)?); + } + // Regular expression: the pattern and the flags, as they are. + 0x0B => { + out.extend_from_slice(at.cstr()?); + out.extend_from_slice(at.cstr()?); + } + // DBPointer: the ObjectId, then the namespace as a C string. + 0x0C => { + let n = at.len()?; + let ns = at.take(n)?; + out.extend_from_slice(at.take(12)?); + out.extend_from_slice(ns); + } + // JavaScript with a scope: the code's length and bytes, then the + // scope's BSON. + 0x0F => { + let _total = at.len()?; + let n = at.len()?; + let code = at.take(n)?; + let text = code.get(..n.saturating_sub(1)).unwrap_or_default(); + out.extend_from_slice(&(text.len() as i32).to_le_bytes()); + out.extend_from_slice(text); + let scope = at.peek_i32()?; + let scope = usize::try_from(scope).map_err(|_| corrupt("a scope with a bad length"))?; + out.extend_from_slice(at.take(scope)?); + } + _ => { + return Err(err( + "encode a record for", + format!("a value of type {t:#04x}"), + )) + } + } + Ok(()) +} + +/// A BSON document at `at`, written in `Document` form. +fn document_from_bson(at: &mut Cursor, out: &mut Vec) -> R<()> { + let start = at.pos; + let total = at.len()?; + let count_at = out.len(); + out.extend_from_slice(&[0; 4]); + let mut count = 0i32; + loop { + let t = at.byte()?; + if t == 0 { + break; + } + out.extend_from_slice(at.cstr()?); + value_from_bson(t, at, out)?; + count += 1; + } + if at.pos - start != total { + return Err(corrupt("a document with a bad length")); + } + out[count_at..count_at + 4].copy_from_slice(&count.to_le_bytes()); + // `DocumentMetadataFields::serializeForSorter`: none. + out.push(0); + Ok(()) +} + +/// Write the length of the document that starts at `start` of `bson`. +fn finish_doc(bson: &mut [u8], start: usize) -> R<()> { + let n = i32::try_from(bson.len() - start).map_err(|_| corrupt("a record too large"))?; + bson[start..start + 4].copy_from_slice(&n.to_le_bytes()); + Ok(()) +} + +/// One `Value`'s value (its type `t` already read) at `at`, written as a +/// BSON value. +fn value_to_bson(t: u8, at: &mut Cursor, out: &mut Vec) -> R<()> { + if let Some(n) = fixed(t) { + out.extend_from_slice(at.take(n)?); + return Ok(()); + } + match t { + 0x02 | 0x0D | 0x0E => { + let n = at.len()?; + out.extend_from_slice(&((n + 1) as i32).to_le_bytes()); + out.extend_from_slice(at.take(n)?); + out.push(0); + } + 0x03 => document_to_bson(at, out)?, + 0x04 => { + let n = at.len()?; + let start = out.len(); + out.extend_from_slice(&[0; 4]); + for i in 0..n { + let et = at.byte()?; + out.push(et); + out.extend_from_slice(i.to_string().as_bytes()); + out.push(0); + value_to_bson(et, at, out)?; + } + out.push(0); + finish_doc(out, start)?; + } + 0x05 => { + let subtype = at.byte()?; + let n = at.len()?; + out.extend_from_slice(&(n as i32).to_le_bytes()); + out.push(subtype); + out.extend_from_slice(at.take(n)?); + } + 0x0B => { + out.extend_from_slice(at.cstr()?); + out.extend_from_slice(at.cstr()?); + } + 0x0C => { + let oid = at.take(12)?; + let ns = at.cstr()?; + out.extend_from_slice(&(ns.len() as i32).to_le_bytes()); + out.extend_from_slice(ns); + out.extend_from_slice(oid); + } + 0x0F => { + let start = out.len(); + out.extend_from_slice(&[0; 4]); + let n = at.len()?; + out.extend_from_slice(&((n + 1) as i32).to_le_bytes()); + out.extend_from_slice(at.take(n)?); + out.push(0); + let scope = usize::try_from(at.peek_i32()?) + .map_err(|_| corrupt("a scope with a bad length"))?; + out.extend_from_slice(at.take(scope)?); + finish_doc(out, start)?; + } + _ => return Err(corrupt(format!("a value of type {t:#04x}"))), + } + Ok(()) +} + +/// One `Document` at `at`, written as BSON. +fn document_to_bson(at: &mut Cursor, out: &mut Vec) -> R<()> { + let n = at.len()?; + let start = out.len(); + out.extend_from_slice(&[0; 4]); + for _ in 0..n { + let name = at.cstr()?; + let t = at.byte()?; + out.push(t); + out.extend_from_slice(name); + value_to_bson(t, at, out)?; + } + if at.byte()? != 0 { + return Err(corrupt("a document with metadata")); + } + out.push(0); + finish_doc(out, start) +} + +// --------------------------------------------------------------- files -- + +/// What a run wrote: `bytes` before compression (`spilledBytes`), `stored` +/// on disk (`spilledDataStorageSize`). +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub(crate) struct Written { + pub bytes: i64, + pub stored: i64, +} + +/// `SortedFileWriter`: records into chunks, chunks into a file. +pub(crate) struct Writer { + path: PathBuf, + out: BufWriter, + buffer: Vec, + written: Written, +} + +impl Writer { + pub(crate) fn create(path: &Path) -> R { + let file = File::create(path).map_err(|e| err("create", e))?; + Ok(Writer { + path: path.to_path_buf(), + out: BufWriter::new(file), + buffer: Vec::new(), + written: Written::default(), + }) + } + + /// The buffer the next record is written into; [`Writer::added`] once + /// it is. + pub(crate) fn buffer(&mut self) -> &mut Vec { + &mut self.buffer + } + + /// `addAlreadySorted`: a record was written to the buffer. + pub(crate) fn added(&mut self) -> R<()> { + if self.buffer.len() > CHUNK { + self.write_chunk()?; + } + Ok(()) + } + + /// `writeChunk`. + fn write_chunk(&mut self) -> R<()> { + let size = self.buffer.len(); + if size == 0 { + return Ok(()); + } + self.written.bytes += size as i64; + let compressed = crate::snappy::compress(&self.buffer); + let (signed, payload) = if compressed.len() < size / 10 * 9 { + (-(compressed.len() as i64), &compressed[..]) + } else { + (size as i64, &self.buffer[..]) + }; + let signed = i32::try_from(signed).map_err(|_| err("write", "a chunk too large"))?; + self.out + .write_all(&signed.to_le_bytes()) + .and_then(|()| self.out.write_all(payload)) + .map_err(|e| err("write", e))?; + self.written.stored += 4 + payload.len() as i64; + self.buffer.clear(); + Ok(()) + } + + /// `done`: the last chunk written and the file synced. + pub(crate) fn done(mut self) -> R { + self.write_chunk()?; + self.out.flush().map_err(|e| err("write", e))?; + let file = self.out.into_inner().map_err(|e| err("write", e.error()))?; + file.sync_all().map_err(|e| err("write", e))?; + let on_disk = file.metadata().map_err(|e| err("measure", e))?.len() as i64; + if on_disk != self.written.stored { + return Err(err( + "write", + format!( + "{} holds {on_disk} bytes, not the {} written", + self.path.display(), + self.written.stored + ), + )); + } + Ok(self.written) + } +} + +/// A run being read back, chunk by chunk. +pub(crate) struct Reader { + input: BufReader, + chunk: Vec, + pos: usize, +} + +impl Reader { + pub(crate) fn open(path: &Path) -> R { + let file = File::open(path).map_err(|e| err("open", e))?; + Ok(Reader { + input: BufReader::new(file), + chunk: Vec::new(), + pos: 0, + }) + } + + /// Read the next record with `f`, from the next chunk when this one is + /// finished. A record never spans two chunks. + pub(crate) fn record(&mut self, f: impl FnOnce(&mut Cursor) -> R) -> R { + if self.pos >= self.chunk.len() { + self.next_chunk()?; + } + let mut at = Cursor::new(&self.chunk[self.pos..]); + let out = f(&mut at)?; + self.pos += at.pos; + Ok(out) + } + + fn next_chunk(&mut self) -> R<()> { + let mut len = [0u8; 4]; + self.input + .read_exact(&mut len) + .map_err(|e| err("read", e))?; + let signed = i32::from_le_bytes(len); + let n = signed.unsigned_abs() as usize; + let mut payload = vec![0u8; n]; + self.input + .read_exact(&mut payload) + .map_err(|e| err("read", e))?; + self.chunk = if signed < 0 { + snap::raw::Decoder::new() + .decompress_vec(&payload) + .map_err(|e| corrupt(format!("a chunk that does not decompress: {e}")))? + } else { + payload + }; + self.pos = 0; + if self.chunk.is_empty() { + return Err(corrupt("an empty chunk")); + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use bson::doc; + use bson::oid::ObjectId; + use bson::spec::BinarySubtype; + + fn every_type() -> Document { + doc! { + "d": 1.5, "s": "héllo", "o": { "a": 1, "b": [1, "x", { "c": null }] }, + "a": [], "bin": bson::Binary { subtype: BinarySubtype::Uuid, bytes: vec![1; 16] }, + "u": Bson::Undefined, "oid": ObjectId::from_bytes([7; 12]), "t": true, + "date": bson::DateTime::from_millis(-5), "n": Bson::Null, + "re": bson::Regex { pattern: "a.b".into(), options: "i".into() }, + "js": Bson::JavaScriptCode("f()".into()), + "sym": Bson::Symbol("sym".into()), + "jsw": Bson::JavaScriptCodeWithScope(bson::JavaScriptCodeWithScope { + code: "g()".into(), scope: doc! { "x": 1 } }), + "i": 7, "ts": bson::Timestamp { time: 3, increment: 4 }, "l": 9i64, + "dec": "1.10".parse::().unwrap(), + "min": Bson::MinKey, "max": Bson::MaxKey, + "bad": crate::bsondoc::invalid_utf8(vec![0xff, 0xfe]), + } + } + + #[test] + fn values_and_documents_round_trip() { + let d = every_type(); + let mut buf = Vec::new(); + write_document(&d, &mut buf).unwrap(); + write_value( + &Bson::Array(vec![Bson::Int32(1), Bson::String("k".into())]), + &mut buf, + ) + .unwrap(); + write_bson(&d, &mut buf).unwrap(); + let mut at = Cursor::new(&buf); + assert_eq!(read_document(&mut at).unwrap(), d); + assert_eq!( + read_value(&mut at).unwrap(), + Bson::Array(vec![Bson::Int32(1), Bson::String("k".into())]) + ); + assert_eq!(read_bson(&mut at).unwrap(), d); + assert!(at.at_end()); + } + + #[test] + fn the_forms_are_mongods() { + // `Value::serializeForSorter` of "ab": type, length, bytes. + let mut v = Vec::new(); + write_value(&Bson::String("ab".into()), &mut v).unwrap(); + assert_eq!(v, [2, 2, 0, 0, 0, b'a', b'b']); + // `Document::serializeForSorter` of {a: 1}: count, name, value, + // no metadata. + let mut d = Vec::new(); + write_document(&doc! { "a": 1 }, &mut d).unwrap(); + assert_eq!(d, [1, 0, 0, 0, b'a', 0, 0x10, 1, 0, 0, 0, 0]); + // Binary data: subtype before length. + let mut b = Vec::new(); + write_value( + &Bson::Binary(bson::Binary { + subtype: BinarySubtype::Generic, + bytes: vec![9], + }), + &mut b, + ) + .unwrap(); + assert_eq!(b, [5, 0, 1, 0, 0, 0, 9]); + } + + #[test] + fn chunks_compress_when_that_saves_a_tenth() { + let dir = std::env::temp_dir().join(format!("secantus-spill-file-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("run"); + let mut w = Writer::create(&path).unwrap(); + let d = doc! { "s": "x".repeat(1000) }; + for _ in 0..100 { + write_bson(&d, w.buffer()).unwrap(); + w.added().unwrap(); + } + let written = w.done().unwrap(); + assert_eq!(written.bytes, 100 * 1013); + // Two chunks (the first passes 64 KiB), each far smaller compressed. + assert!(written.stored < written.bytes / 10, "{written:?}"); + assert_eq!( + std::fs::metadata(&path).unwrap().len() as i64, + written.stored + ); + let mut r = Reader::open(&path).unwrap(); + for _ in 0..100 { + assert_eq!(r.record(read_bson).unwrap(), d); + } + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/secantus-pg/Cargo.lock b/crates/secantus-pg/Cargo.lock index 08802f0b0..a9b7a683d 100644 --- a/crates/secantus-pg/Cargo.lock +++ b/crates/secantus-pg/Cargo.lock @@ -2300,6 +2300,7 @@ dependencies = [ "rand 0.9.5", "regex", "regex-automata", + "snap", ] [[package]] @@ -2612,6 +2613,12 @@ dependencies = [ "serde_core", ] +[[package]] +name = "snap" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "199905e6153d6405f9728fe44daace35f8f837bbf830bb6e85fbd5828709a886" + [[package]] name = "snowball_stemmers_rs" version = "1.0.1" diff --git a/crates/secantus-server-py/Cargo.lock b/crates/secantus-server-py/Cargo.lock index 4a5d47cee..ca0a35c71 100644 --- a/crates/secantus-server-py/Cargo.lock +++ b/crates/secantus-server-py/Cargo.lock @@ -2306,6 +2306,7 @@ dependencies = [ "rand 0.9.5", "regex", "regex-automata", + "snap", ] [[package]] diff --git a/crates/secantus-storage-adapter/Cargo.lock b/crates/secantus-storage-adapter/Cargo.lock index 307e5f9aa..ef0db0e9e 100644 --- a/crates/secantus-storage-adapter/Cargo.lock +++ b/crates/secantus-storage-adapter/Cargo.lock @@ -1249,6 +1249,7 @@ dependencies = [ "rand", "regex", "regex-automata", + "snap", ] [[package]] diff --git a/crates/secantus-storage-py/Cargo.lock b/crates/secantus-storage-py/Cargo.lock index b31d13de6..67a39c66c 100644 --- a/crates/secantus-storage-py/Cargo.lock +++ b/crates/secantus-storage-py/Cargo.lock @@ -1095,6 +1095,7 @@ dependencies = [ "rand", "regex", "regex-automata", + "snap", ] [[package]] @@ -1220,6 +1221,12 @@ version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +[[package]] +name = "snap" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "199905e6153d6405f9728fe44daace35f8f837bbf830bb6e85fbd5828709a886" + [[package]] name = "spade" version = "2.15.1" diff --git a/crates/secantus-storage/Cargo.lock b/crates/secantus-storage/Cargo.lock index ddd38fb2a..26e97540e 100644 --- a/crates/secantus-storage/Cargo.lock +++ b/crates/secantus-storage/Cargo.lock @@ -1032,6 +1032,7 @@ dependencies = [ "rand", "regex", "regex-automata", + "snap", ] [[package]] @@ -1148,6 +1149,12 @@ version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +[[package]] +name = "snap" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "199905e6153d6405f9728fe44daace35f8f837bbf830bb6e85fbd5828709a886" + [[package]] name = "spade" version = "2.15.1" diff --git a/crates/secantusdb/Cargo.lock b/crates/secantusdb/Cargo.lock index 759c404ea..5e0e6d38d 100644 --- a/crates/secantusdb/Cargo.lock +++ b/crates/secantusdb/Cargo.lock @@ -2335,6 +2335,7 @@ dependencies = [ "rand 0.9.4", "regex", "regex-automata", + "snap", ] [[package]] diff --git a/tasks/backlog-rust-mongodb.md b/tasks/backlog-rust-mongodb.md index 10ab158ce..625085fca 100644 --- a/tasks/backlog-rust-mongodb.md +++ b/tasks/backlog-rust-mongodb.md @@ -997,15 +997,21 @@ live. Not filed here: `$rankFusion` / `$score` / `$scoreFusion` (RM-198), 246 SnapshotUnavailable, by name, rather than answer as of now; without `atClusterTime` it reads the latest data, which is correct. `secantusd-rs` creates its stores the other way, mongod's (RM-210). -- [ ] **A spill's on-disk size is this server's file, not mongod's.** - The memory budgets spill as mongod 8.2.11 does -- the same spills, - records and bytes for a sort, a `$group` and a `$setWindowFields` - (RM-211, RM-315) -- but `spilledDataStorageSize` (the profile's - `sortSpilledDataStorageSize` / `groupSpilledDataStorageSize`, explain, - `serverStatus`'s `metrics.query`) is the size of this server's - uncompressed spill file. mongod's sorter compresses each run (130545 - bytes for the 2461200 a 300-document sort spills) and a `$group` or - `$setWindowFields` spills to a WiredTiger table (4096 bytes). +- [ ] **A `$group`'s and a `$setWindowFields`'s spills take this + server's size on disk, not mongod's.** A sort's spill file is mongod + 8.2's -- 64 KiB chunks of `serializeForSorter` records, each + snappy-compressed by mongod's own compressor -- so a sort's and a + `$bucketAuto`'s `spilledDataStorageSize` are mongod's to the byte + (`test_spill_file_storage_size`). Two spillers are not: a classic + `$group` writes its partial states as this server's own BSON + documents, where mongod writes each group's key and its accumulators' + `getValue(toBeMerged)` values to a sorter file (16321 bytes for a + `$push` of 300 1000-byte strings on 8.2.1, against 320100 here); and a + `$setWindowFields` spills its partition cache to a file, where mongod's + goes to a WiredTiger temporary table (`spilledDataStorageSize: 4096`, + against 310800). The first needs every accumulator's merge value in + mongod's form; the second a WiredTiger table, which the engine crate + cannot reach today. Backlog-ID: RM-325 Verify: tests/backlog/test_rm_found_2026_10_26.py::test_spill_storage_size - [ ] **A sort under a cursor stage reports its spills once, not per @@ -1041,6 +1047,40 @@ live. Not filed here: `$rankFusion` / `$score` / `$scoreFusion` (RM-198), front of it in the cursor's plan). This server lists neither. Backlog-ID: RM-328 Verify: tests/backlog/test_rm_found_2026_10_26.py::test_explain_memory_counters +- [ ] **A query-layer sort spills its documents in sorted order, not as its + scan read them.** mongod 8.2's `SORT` takes the documents as the stage + below hands them over (a forward `COLLSCAN` for a plain collection) + and spills each run sorted. Here the read hands the sorter documents it + has already sorted -- `find`, the aggregate pipeline's leading `$sort`, + `explain`'s executor -- so a sort that reorders its input spills + different runs: the same spills, records and bytes, but a different + file (130528 bytes against mongod 8.2.1's 130545 for 300 documents + sorted by `{s: 1, n: -1}`; measured by copying mongod's live spill file + out of `_tmp`). A sort whose input is already in order, a pipeline + `$sort` after another stage, and the stream's `SORT` under a write + (`sort_members`, given a budget) spill as mongod does. The fix is for + the read to run the budgeted sort over its input, as `sort_members` + does, instead of the command sorting the sorted output again. + Backlog-ID: RM-395 + Verify: tests/backlog/test_rm_found_2026_10_26.py::test_leading_sort_spill_storage_size +- [ ] **A `$sort` after `$addFields` spills at slightly different record + boundaries.** Over documents of varying size, mongod 8.2.1's first two + runs of a `$sort` after `$addFields` hold about three records more + than this server's (its merged runs are 60909 and 55591 bytes before + compression, these 59997 and 56503), so the file differs (39691 bytes + against 39694) though the spills, records and bytes agree. The + memory a modified document counts (`SortSource::Modified`, measured by + bisection over documents of one size) is not quite mongod's for + these. + Backlog-ID: RM-396 + Verify: tests/backlog/test_rm_found_2026_10_26.py::test_added_fields_sort_spill_storage_size +- [ ] **A `$group` of `$sum` and `$count` counts less memory than mongod.** + Spilling 300 groups of `{t: {$sum: "$n"}, c: {$count: {}}}` 22 times, + mongod 8.2.1 reports `groupSpilledBytes: 115200` and this server + 110400: the spills and records agree, so each group's accumulators + are counted 16 bytes short. + Backlog-ID: RM-397 + Verify: tests/backlog/test_rm_found_2026_10_26.py::test_group_sum_spilled_bytes - **Scope decision — SBE's `$concatArrays` refusal names a count mongod never set.** Over its budget mongod 8.2.11 says "Concatentating array of 2 elements and N bytes with array of 87684434848907256 elements and M diff --git a/tests/backlog/test_rm_found_2026_10_26.py b/tests/backlog/test_rm_found_2026_10_26.py index 8d505d344..fb1f28d4d 100644 --- a/tests/backlog/test_rm_found_2026_10_26.py +++ b/tests/backlog/test_rm_found_2026_10_26.py @@ -53,33 +53,155 @@ def _profiled(db: Any, pipeline: list, knobs: dict, comment: str) -> dict: # ------------------------------------------- RM-325 spill storage size -- -def _storage_sizes(client: Any) -> dict: - db = _spill_db(client) - sort = _profiled( +def _bucket_auto_storage(client: Any, db: Any) -> dict: + def counters() -> dict: + return client.admin.command("serverStatus")["metrics"]["query"]["bucketAuto"] + + before = counters() + _profiled( db, - [{"$sort": {"s": 1, "n": -1}}, {"$project": {"_id": 1}}], - {"internalQueryMaxBlockingSortMemoryUsageBytes": 50_000}, - "rm316sort", + [{"$bucketAuto": {"groupBy": "$n", "buckets": 5, "output": {"s": {"$push": "$s"}}}}], + {"internalDocumentSourceBucketAutoMaxMemoryBytes": 50_000}, + "rm325bucket", ) + after = counters() + return {k: after[k] - before[k] for k in after} + + +def _sort_storage_sizes(client: Any) -> dict: + db = _spill_db(client) + sizes = {} + pipelines = { + # The query layer's sort, over documents that arrive in its order: + # a `Value` key (an array of two) and the document's BSON. + "in order": [{"$sort": {"n": 1, "s": 1}}], + # A pipeline `$sort`: the fields kept, as a `Document`. + "after $project": [{"$project": {"s": 1, "k": 1}}, {"$sort": {"k": -1, "s": 1}}], + } + for name, pipeline in pipelines.items(): + entry = _profiled( + db, pipeline, {"internalQueryMaxBlockingSortMemoryUsageBytes": 50_000}, "rm325" + name + ) + sizes[name] = (entry.get("sortSpills"), entry.get("sortSpilledDataStorageSize")) + sizes["$bucketAuto"] = _bucket_auto_storage(client, db) + return sizes + + +@pytest.mark.backlog_fixed("RM-325") +def test_spill_file_storage_size(rust_mongo: Any, mongod: Any) -> None: + """A sorter's spill file was this server's own; it is now mongod 8.2's: + 64 KiB chunks of `serializeForSorter` records, each snappy-compressed by + mongod's compressor, so a sort's and `$bucketAuto`'s + `spilledDataStorageSize` are mongod's to the byte.""" + assert _sort_storage_sizes(rust_mongo) == _sort_storage_sizes(mongod) + + +def _storage_sizes(client: Any) -> dict: + db = _spill_db(client) group = _profiled( db, [{"$unwind": "$s"}, {"$group": {"_id": "$n", "s": {"$push": "$s"}}}], {"internalDocumentSourceGroupMaxMemoryBytes": 5_000}, "rm316group", ) + + def window_storage() -> int: + return client.admin.command("serverStatus")["metrics"]["query"]["setWindowFields"][ + "spilledDataStorageSize" + ] + + before = window_storage() + _profiled( + db, + [{"$setWindowFields": {"partitionBy": "$k", "output": {"t": {"$push": "$s"}}}}], + {"internalDocumentSourceSetWindowFieldsMaxMemoryBytes": 50_000}, + "rm325window", + ) return { - "sort": sort.get("sortSpilledDataStorageSize"), "group": group.get("groupSpilledDataStorageSize"), + "window": window_storage() - before, } @pytest.mark.backlog("RM-325") def test_spill_storage_size(rust_mongo: Any, mongod: Any) -> None: - """mongod 8.2.11 compresses a sort's runs (130545 bytes for 2461200) and - spills a `$group` to a WiredTiger table.""" + """mongod 8.2 spills a classic `$group` to a sorter file of its + accumulators' merge values (16321 bytes here) and a `$setWindowFields` + to a WiredTiger table (4096).""" assert _storage_sizes(rust_mongo) == _storage_sizes(mongod) +def _leading_sort_storage(client: Any) -> Any: + db = _spill_db(client) + entry = _profiled( + db, + [{"$sort": {"s": 1, "n": -1}}, {"$project": {"_id": 1}}], + {"internalQueryMaxBlockingSortMemoryUsageBytes": 50_000}, + "rm316sort", + ) + return entry.get("sortSpilledDataStorageSize") + + +@pytest.mark.backlog("RM-395") +def test_leading_sort_spill_storage_size(rust_mongo: Any, mongod: Any) -> None: + """mongod 8.2's `SORT` takes documents as its collection scan reads them, + so a sort that reverses that order spills runs of the first documents + read, each sorted (130545 bytes).""" + assert _leading_sort_storage(rust_mongo) == _leading_sort_storage(mongod) + + +def _added_fields_sort(client: Any) -> Any: + client.drop_database("rm396") + coll = client.rm396.c + coll.insert_many( + [ + { + "_id": i, + "k": i % 7, + "s": f"name-{i * 37 % 1000:04d}" * 20, + "a": [i, i + 1], + "o": {"x": i, "y": "z" * (i % 30)}, + "n": i, + } + for i in range(400) + ] + ) + entry = _profiled( + client.rm396, + [{"$addFields": {"t": {"$multiply": ["$n", 2]}}}, {"$sort": {"t": -1}}], + {"internalQueryMaxBlockingSortMemoryUsageBytes": 60_000}, + "rm396", + ) + return (entry.get("sortSpills"), entry.get("sortSpilledDataStorageSize")) + + +@pytest.mark.backlog("RM-396") +def test_added_fields_sort_spill_storage_size(rust_mongo: Any, mongod: Any) -> None: + """mongod 8.2 spills a `$sort` after `$addFields` at its own record + boundaries: its first two runs of documents of varying size hold about + three records more than this server's, so the merged runs (and the + file, 39691 bytes) differ though the spills and bytes agree.""" + assert _added_fields_sort(rust_mongo) == _added_fields_sort(mongod) + + +def _group_sum_spilled_bytes(client: Any) -> Any: + db = _spill_db(client) + entry = _profiled( + db, + [{"$unwind": "$s"}, {"$group": {"_id": "$n", "t": {"$sum": "$n"}, "c": {"$count": {}}}}], + {"internalDocumentSourceGroupMaxMemoryBytes": 5_000}, + "rm397", + ) + return (entry.get("groupSpills"), entry.get("groupSpilledBytes")) + + +@pytest.mark.backlog("RM-397") +def test_group_sum_spilled_bytes(rust_mongo: Any, mongod: Any) -> None: + """mongod 8.2 counts a `$group` of `$sum` and `$count` as 115200 bytes + over its 22 spills; this server counts 110400.""" + assert _group_sum_spilled_bytes(rust_mongo) == _group_sum_spilled_bytes(mongod) + + # --------------------------------------- RM-326 sort stats per batch -- From 214a5e6c84e02f0da97b4d59e035811c4da46759 Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sat, 7 Nov 2026 22:29:35 +0000 Subject: [PATCH 2/4] Crash-recovery test records why it failed; RM-401 filed (watch item) The hard-kill transactions test failed once under load: a snapshot read at the first ack's time (16 min before the run) was refused as too old. mongod 8.2.11 keeps history across kill -9 (probed), so the expectation stands. Not reproduced in 28 runs; the test now asserts its ack file is fresh and every ack lies in the run's wall-clock window, so a recurrence names its cause. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QpXX74nAaz1CHpMnJwfkJp --- changelog.d/crash-test-evidence.md | 7 +++++++ tasks/backlog-rust-mongodb.md | 19 +++++++++++++++++++ tests/test_crash_recovery.py | 13 +++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 changelog.d/crash-test-evidence.md diff --git a/changelog.d/crash-test-evidence.md b/changelog.d/crash-test-evidence.md new file mode 100644 index 000000000..d2105838c --- /dev/null +++ b/changelog.d/crash-test-evidence.md @@ -0,0 +1,7 @@ +### A crash-recovery test records why it fails, the next time it does + +`test_hard_kill_mid_transactions_keeps_every_acked_commit_and_its_history` failed once during a local gate. After the hard kill, a snapshot read at the first acknowledged commit's cluster time was refused as too old, because that time was 16 minutes before the run began. It has not been reproduced. The test now checks that its ack file is new and that every ack's cluster time falls inside the run's own wall-clock window. A repeat will show whether the ack file came from another run or the server minted a stale timestamp. The case is filed as RM-401. + +#### Changed + +- `tests/test_crash_recovery.py`: two checks that capture the cause of RM-401's failure. Filed RM-401. diff --git a/tasks/backlog-rust-mongodb.md b/tasks/backlog-rust-mongodb.md index 625085fca..23943f1a6 100644 --- a/tasks/backlog-rust-mongodb.md +++ b/tasks/backlog-rust-mongodb.md @@ -2298,6 +2298,25 @@ dropped (four), and `listIndexes` lists by name. Backlog-ID: RM-115 Verify: none -- local gpg lock, environment +- [ ] **NOT REPRODUCED -- left open as a watch item. A crash-recovery + test read at an acknowledged commit's time 16 minutes before its own + run (2026-11-07).** `test_crash_recovery.py::test_hard_kill_mid_transactions_keeps_every_acked_commit_and_its_history` + failed once, in a local `gate_rm.py` run under load: after the hard kill + and replay, a snapshot read at the first ack's cluster time, + `Timestamp(1794087900, 63)` (21:45:00 UTC), was refused as older than the + oldest timestamp, while the reopened server's own cluster time was + 22:01:13. Every ack should be seconds old: `mint_ts` mints from the wall + clock, and the reopened store keeps 300 s of history. So either the + test's `acks.txt` was not this run's (the writer appends to it) or the + writer's server minted a stale timestamp. mongod 8.2.11 keeps snapshot + history across `kill -9` and a 1 s checkpoint cadence (probed + 2026-11-07), so the test's expectation is right. Not reproduced: 8 + serial and 20 four-way-parallel runs passed, and so did a variant that + spans several checkpoints. The test now asserts the ack file is fresh + and every ack falls inside the run's wall-clock window, so the next + failure names which cause it was. + Backlog-ID: RM-401 + Verify: none -- intermittent, not reproduced; the test's new checks capture the cause on the next occurrence - [ ] **NOT REPRODUCED -- left open as a watch item. `test_tmp_retention_guard.py::test_default_tmp_retention_policy_is_allowed` timed out ONCE (2026-09-30).** It passed in every full-suite run of the 2026-10-07 batch (14,500+ tests under `-n auto`). Its nested collect-only pytest hit the 300s diff --git a/tests/test_crash_recovery.py b/tests/test_crash_recovery.py index 91f8e75bf..efa819ce6 100644 --- a/tests/test_crash_recovery.py +++ b/tests/test_crash_recovery.py @@ -295,6 +295,13 @@ def test_hard_kill_mid_transactions_keeps_every_acked_commit_and_its_history(tmp ack_file = tmp_path / "acks.txt" port_file = tmp_path / "port.txt" env = {**os.environ, "SECANTUS_DATA_NONLOGGED": "1", "SECANTUS_CHECKPOINT_SECONDS": "1"} + # Evidence for a failure seen once (2026-11-07, a local gate under load): + # a read at the first ack's time was refused as older than the oldest + # timestamp, and that ack's cluster time was 16 minutes before the run. + # Either the ack file was not this run's (the writer appends) or the + # writer's server minted a stale timestamp. These two checks say which. + assert not ack_file.exists(), f"{ack_file} exists before the writer started" + started = int(time.time()) proc = subprocess.Popen( [sys.executable, "-c", _TXN_WRITER, str(store), str(ack_file), str(port_file)], env=env, @@ -317,6 +324,12 @@ def test_hard_kill_mid_transactions_keeps_every_acked_commit_and_its_history(tmp lo, hi, secs, inc = (int(x) for x in line.split(":")) acks.append((lo, hi, Timestamp(secs, inc))) assert len(acks) >= 5, f"too few commits acked: {proc.stderr.read().decode()[-2000:]}" + ended = int(time.time()) + outside = [t for _, _, t in acks if not started - 1 <= t.time <= ended + 1] + assert not outside, ( + f"acks minted outside this run's wall-clock window [{started}, {ended}]: " + f"{outside[:3]}; {ack_file}: {ack_file.read_text()[:400]!r}" + ) srv = _open_after_crash(_secantus_server, tmp_path) try: host, port = srv.address From a144e348b988bc85f5b108d34b187a0ef0e6f9ca Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sat, 7 Nov 2026 18:44:39 +0000 Subject: [PATCH 3/4] Rust MongoDB server: a spilled sort is counted once per cursor-stage load, spillToDisk per chunk (RM-326, in part) Filed RM-397, RM-398, RM-399. Co-Authored-By: Claude Opus 5.5 --- changelog.d/rm-326-sort-counts-per-load.md | 12 + crates/secantus-commands/src/agg_executor.rs | 284 ++++++++++++++++++ crates/secantus-commands/src/aggregate.rs | 25 ++ crates/secantus-commands/src/explain/order.rs | 6 + crates/secantus-core/src/aggregate.rs | 62 +++- crates/secantus-core/src/memlimit.rs | 54 +++- crates/secantus-core/src/sorter.rs | 14 +- crates/secantus-core/src/spill_file.rs | 5 +- tasks/backlog-rust-mongodb.md | 54 +++- tests/backlog/test_rm_326_sort_counts.py | 238 +++++++++++++++ tests/backlog/test_rm_found_2026_10_26.py | 7 +- 11 files changed, 734 insertions(+), 27 deletions(-) create mode 100644 changelog.d/rm-326-sort-counts-per-load.md create mode 100644 tests/backlog/test_rm_326_sort_counts.py diff --git a/changelog.d/rm-326-sort-counts-per-load.md b/changelog.d/rm-326-sort-counts-per-load.md new file mode 100644 index 000000000..d225bf1cf --- /dev/null +++ b/changelog.d/rm-326-sort-counts-per-load.md @@ -0,0 +1,12 @@ +### A spilled sort is counted the way mongod's cursor stage counts it + +When the stages after a pipeline's sort read it through mongod's cursor stage, mongod adds the sort's counts again for every buffer it loads from the query layer. A `$setWindowFields` over 300 documents whose sort spilled 13 times is therefore profiled with `sortSpills: 52`, and a first batch of 101 documents behind an `$addFields` with 39. The Rust MongoDB server now reports the same numbers in the profile and the slow-query line. It works out how many buffers mongod loads, of 32 documents at first and doubling, from the batch the client asked for. That count includes the `$skip` and `$limit` mongod moves into the query layer, and the 4 MB a buffer stops at. `metrics.query.sort.spillToDisk` now counts what mongod counts: one for each 64 KB chunk a sorter writes to its spill file, `$bucketAuto`'s sorter included. + +#### Fixed + +- `secantus-core` (`aggregate`, `memlimit`, `sorter`), `secantus-commands` (`aggregate`, `agg_executor`): the profile's and the slow-query line's `sortSpills`, `sortSpilledRecords`, `sortSpilledBytes`, `sortSpilledDataStorageSize` and `sortTotalDataSizeBytes` count a query-layer sort once per cursor-stage load, as mongod 8.2.11 does. `spillToDisk` counts sorter file chunks, and `$bucketAuto`'s spills now count in `metrics.query.sort` too. +- Fixes RM-326 in part (tests/backlog/test_rm_found_2026_10_26.py::test_sort_profile_per_batch, tests/backlog/test_rm_326_sort_counts.py::test_sort_counts_per_load). RM-326 stays open for stages whose reads depend on the documents (`$unwind`, `$match`, a bounded or partitioned window): `test_sort_counts_per_load_through_stages_that_filter`. +- Regression tests: `agg_executor::tests::the_cursor_stage_loads_as_mongod_does`, `agg_executor::tests::reads_that_depend_on_the_documents_are_not_followed`. +- Filed RM-400: a `getMore` reports none of its cursor's sort counts, and `totalKeysSorted` / `totalBytesSorted` count each sort once (`test_rm_326_sort_counts.py::test_get_more_sort_counts`). +- Filed RM-398: an SBE sort counts its bytes in `totalBytesSorted`, which mongod's does not (`test_rm_326_sort_counts.py::test_sbe_sort_bytes`). +- Filed RM-399: a sort with a limit does not spill as mongod's top-k sorter does (`test_rm_326_sort_counts.py::test_top_k_sort_spills`). diff --git a/crates/secantus-commands/src/agg_executor.rs b/crates/secantus-commands/src/agg_executor.rs index eb1f37303..271667c95 100644 --- a/crates/secantus-commands/src/agg_executor.rs +++ b/crates/secantus-commands/src/agg_executor.rs @@ -217,6 +217,202 @@ pub(crate) fn holds_executor( .map_or(true, |b| b.len() as i64 >= FIRST_LOAD) } +/// The bytes a cursor-stage buffer stops at. +const LOAD_BYTES: usize = 4 * 1024 * 1024; + +/// What the stages read of their input to give `pulls` documents of +/// output: some number of documents, or everything to its end. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Reads { + Some(usize), + All, +} + +/// How many buffers mongod's cursor stage loads from its executor while +/// `stages` (the pipeline after the stages the query layer took) give +/// `pulls` documents. `sizes`: the BSON size of each document the executor +/// hands over, in order, before the `$skip` and `$limit` it takes from +/// `stages`. `limited`: the executor already took a `$limit`. `None`: what +/// the stages read depends on the documents (a `$match`, an `$unwind`, a +/// `$setWindowFields` window), which is not followed here. +/// +/// mongod 8.2.11: the `$skip`s and the least `$limit` that only stages +/// passing each document on stand before are moved into the executor +/// (`extractSkipForPushdown`, `extractLimitForPushdown`). Its cursor stage +/// (`DocumentSourceCursor::loadBatch`) then loads a buffer of 32 documents +/// at first and twice as many after each that filled by count -- without a +/// count when the executor took a `$limit` -- and stops a buffer once it +/// holds more than 4 MB; a load that reaches the end of the executor is its +/// last. Asking for the document after the last one loads again. +pub(crate) fn cursor_loads( + stages: &[Bson], + sizes: &[usize], + limited: bool, + pulls: usize, +) -> Option { + let count = |stage: &Bson, name: &str| { + stage + .as_document() + .and_then(|d| d.get(name)) + .and_then(crate::util::as_i64) + .and_then(|n| usize::try_from(n).ok()) + }; + let (mut skip, mut limit) = (0usize, None::); + let mut left: Vec<&Bson> = Vec::new(); + let mut moving = true; + for (i, stage) in stages.iter().enumerate() { + if moving { + if let Some(k) = count(stage, "$skip") { + skip = skip.saturating_add(k); + continue; + } + if let Some(k) = count(stage, "$limit") { + let k = k.saturating_add(skip); + limit = Some(limit.map_or(k, |l| l.min(k))); + continue; + } + moving = swaps_with_skip_and_limit(stage, stages.get(i + 1)); + } + left.push(stage); + } + let sizes = &sizes[..limit.map_or(sizes.len(), |l| l.min(sizes.len()))]; + let sizes = &sizes[skip.min(sizes.len())..]; + let mut cap = if limited || limit.is_some() { + usize::MAX + } else { + FIRST_LOAD as usize + }; + let mut reads = Reads::Some(pulls); + for stage in left.into_iter().rev() { + let (name, spec) = stage.as_document().and_then(|d| d.iter().next())?; + let n = || crate::util::as_i64(spec).and_then(|n| usize::try_from(n).ok()); + reads = match (name.as_str(), reads) { + (_, Reads::Some(0)) => Reads::Some(0), + (secantus_core::sbe::ENGINE_STAGE, r) => r, + // A `$limit` asks for no more once it has passed its count. + ("$limit", Reads::All) => Reads::Some(n()?), + ("$limit", Reads::Some(p)) => Reads::Some(p.min(n()?)), + ("$skip", Reads::Some(p)) => Reads::Some(p + n()?), + ( + "$project" | "$addFields" | "$set" | "$unset" | "$replaceRoot" | "$replaceWith" + | "$lookup" | "$graphLookup" | "$unionWith", + r, + ) => r, + ("$setWindowFields", Reads::Some(_)) if whole_input_window(spec) => Reads::All, + (_, Reads::Some(_)) if blocks_bson(stage) => Reads::All, + (_, Reads::All) => Reads::All, + _ => return None, + }; + } + let wanted = match reads { + Reads::Some(p) if p <= sizes.len() => Some(p), + _ => None, + }; + let (mut at, mut loads) = (0usize, 0usize); + loop { + if wanted.is_some_and(|w| at >= w) { + return Some(loads); + } + loads += 1; + let (mut count, mut bytes) = (0usize, 0usize); + loop { + let Some(size) = sizes.get(at) else { + return Some(loads); + }; + at += 1; + count += 1; + bytes += size; + if count >= cap { + cap = cap.saturating_mul(2); + break; + } + if bytes > LOAD_BYTES { + break; + } + } + } +} + +/// mongod's `canSwapWithSkippingOrLimitingStage`: a stage that hands on +/// each document it is given, one for one. A `$lookup` / `$graphLookup` +/// that takes in the `$unwind` after it does not. +fn swaps_with_skip_and_limit(stage: &Bson, next: Option<&Bson>) -> bool { + match name_of(stage) { + "$project" + | "$addFields" + | "$set" + | "$unset" + | "$replaceRoot" + | "$replaceWith" + | secantus_core::sbe::ENGINE_STAGE => true, + name @ ("$lookup" | "$graphLookup") => { + let as_field = stage + .as_document() + .and_then(|d| d.get_document(name).ok()) + .and_then(|spec| spec.get_str("as").ok()); + let unwound = next + .and_then(Bson::as_document) + .and_then(|d| match d.get("$unwind") { + Some(Bson::String(path)) => Some(path.clone()), + Some(Bson::Document(spec)) => spec.get_str("path").ok().map(str::to_string), + _ => None, + }); + !matches!((as_field, unwound), (Some(a), Some(u)) if u == format!("${a}")) + } + _ => false, + } +} + +fn blocks_bson(stage: &Bson) -> bool { + stage.as_document().is_some_and(blocks) || matches!(name_of(stage), "$out" | "$merge") +} + +fn name_of(stage: &Bson) -> &str { + stage + .as_document() + .and_then(|d| d.keys().next()) + .map_or("", String::as_str) +} + +/// A `$setWindowFields` over one partition with an output whose window +/// reaches its end: it reads everything before its first document. +fn whole_input_window(spec: &Bson) -> bool { + let Some(spec) = spec.as_document() else { + return false; + }; + if spec.contains_key("partitionBy") { + return false; + } + let Ok(output) = spec.get_document("output") else { + return false; + }; + // The functions that take no window, each of which reads at most a + // bounded distance ahead. + const UNWINDOWED: &[&str] = &[ + "$rank", + "$denseRank", + "$documentNumber", + "$shift", + "$locf", + "$linearFill", + "$expMovingAvg", + ]; + output.values().filter_map(Bson::as_document).any(|out| { + let function = out.keys().find(|k| *k != "window").map(String::as_str); + match out.get_document("window") { + Ok(window) => ["documents", "range"].iter().any(|kind| { + window + .get_array(kind) + .ok() + .and_then(|b| b.get(1)) + .is_some_and(|upper| matches!(upper, Bson::String(s) if s == "unbounded")) + }), + // A windowed function with no window: the whole partition. + Err(_) => function.is_some_and(|f| !UNWINDOWED.contains(&f)), + } + }) +} + #[cfg(test)] mod tests { use super::*; @@ -302,4 +498,92 @@ mod tests { let d = vec![m, doc! { "$group": { "_id": "$a" } }]; assert_eq!(executor(&d, &on_a), Executor::HeldIfLarge(2)); } + + fn b(d: Document) -> Bson { + Bson::Document(d) + } + + /// The loads measured on mongod 8.2.11 (2026-11-07) for 300 documents + /// of 1 KB behind a sort that spilled: `sortSpills` was 13 times these. + #[test] + fn the_cursor_stage_loads_as_mongod_does() { + let sizes = vec![1060; 300]; + let af = b(doc! { "$addFields": { "z": 1 } }); + let group = b(doc! { "$group": { "_id": "$k" } }); + let window = b(doc! { "$setWindowFields": { + "sortBy": { "n": 1 }, "output": { "t": { "$sum": "$n" } } } }); + let cases: Vec<(Vec, usize, usize)> = vec![ + // A first batch of 101: 32 + 64 + 128 documents read. + (vec![af.clone()], 101, 3), + (vec![af.clone()], 10, 1), + (vec![af.clone()], 40, 2), + // All 300, and the end found: 32 + 64 + 128 + 76. + (vec![af.clone()], 301, 4), + (vec![af.clone(), group.clone()], 8, 4), + (vec![window.clone()], 102, 4), + // The `$skip` moves into the executor, which hands over 200. + (vec![af.clone(), b(doc! { "$skip": 100 })], 101, 3), + (vec![af.clone(), b(doc! { "$skip": 150 })], 101, 3), + // A `$limit` in the executor lifts the count: one buffer. + (vec![af.clone(), b(doc! { "$limit": 50 })], 51, 1), + ( + vec![ + af.clone(), + b(doc! { "$skip": 10 }), + b(doc! { "$limit": 150 }), + ], + 101, + 1, + ), + ]; + for (stages, pulls, loads) in cases { + assert_eq!( + cursor_loads(&stages, &sizes, false, pulls), + Some(loads), + "{stages:?} {pulls}" + ); + } + // A `$limit` the executor took before the stages. + assert_eq!( + cursor_loads(std::slice::from_ref(&af), &sizes[..200], true, 101), + Some(1) + ); + // 32 documents fill the first buffer; the end is a second load. + assert_eq!( + cursor_loads(std::slice::from_ref(&window), &sizes[..32], false, 33), + Some(2) + ); + assert_eq!(cursor_loads(&[window], &sizes[..33], false, 34), Some(2)); + // A buffer stops past 4 MB, without doubling. + let big = vec![1 << 20; 10]; + assert_eq!( + cursor_loads(std::slice::from_ref(&group), &big, true, 1), + Some(3) + ); + assert_eq!(cursor_loads(&[group], &big, false, 1), Some(3)); + } + + #[test] + fn reads_that_depend_on_the_documents_are_not_followed() { + let sizes = vec![1060; 300]; + for stage in [ + doc! { "$unwind": "$s" }, + doc! { "$match": { "k": 1 } }, + doc! { "$setWindowFields": { "sortBy": { "n": 1 }, "output": { "r": { "$rank": {} } } } }, + doc! { "$setWindowFields": { + "partitionBy": "$k", "sortBy": { "n": 1 }, "output": { "t": { "$sum": "$n" } } } }, + ] { + assert_eq!( + cursor_loads(&[b(stage.clone())], &sizes, false, 101), + None, + "{stage:?}" + ); + } + // Read to the end, they read everything. + let then_group = [ + b(doc! { "$match": { "k": 1 } }), + b(doc! { "$group": { "_id": "$k" } }), + ]; + assert_eq!(cursor_loads(&then_group, &sizes, false, 2), Some(4)); + } } diff --git a/crates/secantus-commands/src/aggregate.rs b/crates/secantus-commands/src/aggregate.rs index 43dad5127..61e7ab2dc 100644 --- a/crates/secantus-commands/src/aggregate.rs +++ b/crates/secantus-commands/src/aggregate.rs @@ -986,6 +986,8 @@ pub fn aggregate(doc: &Document, ctx: &mut CommandContext) -> HandlerResult { // The pipeline's engine markers switch to SBE and back; whatever they // leave, the command ends in the classic engine. let _engine = secantus_core::sbe::enter(false); + // Whatever an earlier run on this thread left unread. + secantus_core::memlimit::take_cursor_read(); let result = run_segmented( input, &working_pipeline, @@ -1015,8 +1017,31 @@ pub fn aggregate(doc: &Document, ctx: &mut CommandContext) -> HandlerResult { // The pipeline has read its leading-sort verdict, or never will. drop(leading_guard); let prefix = format!("Executor error during aggregate command on namespace: {ns}"); + let total = result.len(); let (first_batch, mut cursor_id) = split_docs_into_cursor_checked(result, batch_size, &ns, cursors, false, Some(&prefix))?; + // A query-layer sort that spilled is reported once for each buffer the + // cursor stage loaded from it for this batch (RM-326): in the + // operation's own counts, never in what `explain` says of its stages. + let read = secantus_core::memlimit::take_cursor_read() + .filter(|_| !crate::explain::order::is_uncounted()); + if let Some(read) = read { + let wanted = if batch_size < 0 { + DEFAULT_BATCH_SIZE as usize + } else { + batch_size as usize + }; + // A batch that came up short asked the pipeline for one more. + let pulls = first_batch.len() + usize::from(first_batch.len() == total && total < wanted); + let downstream = working_pipeline.get(read.at..).unwrap_or_default(); + if !downstream.is_empty() { + let loads = + crate::agg_executor::cursor_loads(downstream, &read.sizes, read.limited, pulls); + if let Some(more) = loads.and_then(|n| n.checked_sub(1)).filter(|n| *n > 0) { + secantus_core::memlimit::add_sort_counts(&read.sort, more as i64); + } + } + } // `batchSize: 0` asks for the cursor and no documents, and mongod opens // one even over nothing -- a missing collection included. if batch_size == 0 && cursor_id == 0 { diff --git a/crates/secantus-commands/src/explain/order.rs b/crates/secantus-commands/src/explain/order.rs index e2039eec3..713af64d1 100644 --- a/crates/secantus-commands/src/explain/order.rs +++ b/crates/secantus-commands/src/explain/order.rs @@ -1482,6 +1482,12 @@ pub(crate) fn uncounted(f: impl FnOnce() -> T) -> T { f() } +/// Inside [`uncounted`]: a run `explain` (or the server itself) makes for +/// what it says of itself, not the operation the client sent. +pub(crate) fn is_uncounted() -> bool { + UNCOUNTED.with(std::cell::Cell::get) +} + /// What a command does with the documents a plan reads, for counting the /// indexes it uses. #[derive(Clone, Copy)] diff --git a/crates/secantus-core/src/aggregate.rs b/crates/secantus-core/src/aggregate.rs index a79c4135b..ab3fc8ab2 100644 --- a/crates/secantus-core/src/aggregate.rs +++ b/crates/secantus-core/src/aggregate.rs @@ -24,7 +24,9 @@ use std::cmp::Ordering; use crate::collation::Collation; use crate::numeric::{as_int_like, int_to_bson}; -use crate::{deadline, densify, expressions, fill, group, order, paths, query, windowfields}; +use crate::{ + deadline, densify, expressions, fill, group, memlimit, order, paths, query, windowfields, +}; pub use crate::fallback::Fallback; @@ -60,8 +62,19 @@ pub fn apply_pipeline( DOWNSTREAM.with(|d| all.extend(d.borrow().iter().cloned())); group::downstream_fields(&all) }; + // The query layer's `$sort` when it spilled: what it counted, and the + // stage its executor's documents reach through the cursor stage (RM-326). + // Its sizes are those of the documents that reach that stage. + let mut cursor_read: Option = None; + let note_read = |read: &mut Option, i: usize, docs: &[Document]| { + if let Some(mut read) = read.take_if(|read| read.at == i) { + read.sizes = docs.iter().map(memlimit::bson_len).collect(); + memlimit::note_cursor_read(read); + } + }; let mut i = 0; while i < pipeline.len() { + note_read(&mut cursor_read, i, &docs); let Bson::Document(s) = &pipeline[i] else { return Err(Fallback::Defer); }; @@ -90,7 +103,19 @@ pub fn apply_pipeline( crate::sorter::SortSource::QueryLayer => None, _ => scanned.as_ref(), }; - window_sort_budget(&docs, spec, vars, source, fields)?; + let before = memlimit::report(); + let sorted = window_sort_budget(&docs, spec, vars, source, fields)?; + let sort = memlimit::report().less(&before); + if source == crate::sorter::SortSource::QueryLayer && sort.sort_spills > 0 { + // The executor hands the stage its documents sorted. + let sizes = sorted.iter().map(|(_, d)| memlimit::bson_len(d)).collect(); + memlimit::note_cursor_read(memlimit::CursorRead { + sort, + sizes, + at: i, + limited: false, + }); + } } let (out, next) = window_fields_streamed(spec, docs, &pipeline[i + 1..], vars, coll)?; docs = out; @@ -125,6 +150,7 @@ pub fn apply_pipeline( .and_then(|st| st.get("$limit")) .and_then(as_int_like) .and_then(|n| usize::try_from(n).ok()); + let before = memlimit::report(); docs = match source { None => sort_stage(docs, spec)?, Some(source) => { @@ -138,12 +164,35 @@ pub fn apply_pipeline( budgeted_sort_stage(docs, spec, source, fields, added, limit)? } }; + let sort = memlimit::report().less(&before); + if source == Some(crate::sorter::SortSource::QueryLayer) && sort.sort_spills > 0 { + // The stages after it the query layer also takes. + let mut at = i + 1; + while pipeline.get(at).is_some_and(|st| { + matches!( + stage_name_of(st), + "$match" | "$skip" | "$limit" | "$project" | "$unset" + ) + }) { + at += 1; + } + let limited = pipeline[i + 1..at] + .iter() + .any(|st| stage_name_of(st) == "$limit"); + cursor_read = Some(memlimit::CursorRead { + sort, + sizes: Vec::new(), + at, + limited, + }); + } i += 1; continue; } docs = apply_stage(name, spec, docs, vars, coll)?; i += 1; } + note_read(&mut cursor_read, pipeline.len(), &docs); deadline::check_now()?; Ok(docs) } @@ -293,9 +342,9 @@ fn window_sort_budget( vars: &Document, source: crate::sorter::SortSource, fields: Option<&std::collections::BTreeSet>, -) -> R<()> { +) -> R, Document)>> { let Some(spec) = spec.as_document() else { - return Ok(()); + return Ok(Vec::new()); }; let by = spec.get("partitionBy"); let sort = match spec.get("sortBy") { @@ -303,7 +352,7 @@ fn window_sort_budget( _ => Vec::new(), }; if by.is_none() && sort.is_empty() { - return Ok(()); + return Ok(Vec::new()); } let mut items: Vec<(Vec, Document)> = Vec::with_capacity(docs.len()); for d in docs { @@ -337,8 +386,7 @@ fn window_sort_budget( budget: crate::memlimit::budgets().sort, added: None, }; - crate::sorter::blocking_sort(items, &cmp, &sizing, None)?; - Ok(()) + crate::sorter::blocking_sort(items, &cmp, &sizing, None) } /// The stages that take one document and give back at most one, which run diff --git a/crates/secantus-core/src/memlimit.rs b/crates/secantus-core/src/memlimit.rs index dd85d25b5..15d8fc3c1 100644 --- a/crates/secantus-core/src/memlimit.rs +++ b/crates/secantus-core/src/memlimit.rs @@ -180,6 +180,7 @@ thread_local! { }) }; static ALLOW_DISK_USE: Cell = const { Cell::new(true) }; static REPORT: RefCell = RefCell::new(SpillReport::default()); + static CURSOR_READ: RefCell> = const { RefCell::new(None) }; static SPILL_DIR: RefCell> = const { RefCell::new(None) }; } @@ -298,8 +299,9 @@ pub(crate) fn record_sort(keys: i64, bytes: i64) { SORT_METRICS.total_bytes_sorted.add(bytes); } -/// A sort wrote a run (a spill, or a merge of runs) of `records`. -pub(crate) fn record_sort_spill(records: i64, bytes: i64, storage: i64) { +/// A sort wrote a run (a spill, or a merge of runs) of `records`, as +/// `chunks` chunks of its file. +pub(crate) fn record_sort_spill(records: i64, bytes: i64, storage: i64, chunks: i64) { REPORT.with(|r| { let mut r = r.borrow_mut(); r.used_disk = true; @@ -308,14 +310,18 @@ pub(crate) fn record_sort_spill(records: i64, bytes: i64, storage: i64) { r.sort_spilled_bytes += bytes; r.sort_spilled_data_storage_size += storage; }); - SORT_METRICS.spill_to_disk.add(1); + SORT_METRICS.spill_to_disk.add(chunks); SORT_METRICS.spill_to_disk_bytes.add(storage); } -/// `$bucketAuto`'s sorter wrote a run: counted as the stage's own. -pub(crate) fn record_bucket_auto_spill(records: i64, bytes: i64, storage: i64) { +/// `$bucketAuto`'s sorter wrote a run: counted as the stage's own, and its +/// chunks as every sorter's (mongod 8.2.11: `metrics.query.sort` counts +/// what the sorter's file writer writes, whichever stage it sorts for). +pub(crate) fn record_bucket_auto_spill(records: i64, bytes: i64, storage: i64, chunks: i64) { REPORT.with(|r| r.borrow_mut().used_disk = true); BUCKET_AUTO_METRICS.add(1, records, bytes, storage); + SORT_METRICS.spill_to_disk.add(chunks); + SORT_METRICS.spill_to_disk_bytes.add(storage); } /// `$setWindowFields` spilled its partition cache. @@ -346,6 +352,44 @@ pub fn disk_allowed() -> bool { /// Forget what an earlier operation on this thread spilled. pub fn reset_report() { REPORT.with(|r| *r.borrow_mut() = SpillReport::default()); + CURSOR_READ.with(|c| c.borrow_mut().take()); +} + +/// A query-layer sort that spilled, read by the stages after it through +/// mongod's cursor stage (RM-326). mongod 8.2.11's cursor stage adds its +/// executor's summary to its own at every buffer it loads, and never resets +/// the sort's counts there, so the operation reports them once per load. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct CursorRead { + /// What the sort counted, once. + pub sort: SpillReport, + /// The size of each document the executor hands over, in order. + pub sizes: Vec, + /// The stage of the pipeline the cursor stage feeds. + pub at: usize, + /// The query layer took a `$limit` after the sort. + pub limited: bool, +} + +pub(crate) fn note_cursor_read(read: CursorRead) { + CURSOR_READ.with(|c| *c.borrow_mut() = Some(read)); +} + +/// The query-layer sort of the pipeline just run, when it spilled. +pub fn take_cursor_read() -> Option { + CURSOR_READ.with(|c| c.borrow_mut().take()) +} + +/// Count `sort`'s counts `times` more in the operation's report. +pub fn add_sort_counts(sort: &SpillReport, times: i64) { + REPORT.with(|r| { + let mut r = r.borrow_mut(); + r.sort_spills += sort.sort_spills * times; + r.sort_spilled_records += sort.sort_spilled_records * times; + r.sort_spilled_bytes += sort.sort_spilled_bytes * times; + r.sort_spilled_data_storage_size += sort.sort_spilled_data_storage_size * times; + r.sort_total_data_size += sort.sort_total_data_size * times; + }); } /// What the operation has spilled so far. diff --git a/crates/secantus-core/src/sorter.rs b/crates/secantus-core/src/sorter.rs index 9315341e0..f3c570db2 100644 --- a/crates/secantus-core/src/sorter.rs +++ b/crates/secantus-core/src/sorter.rs @@ -312,9 +312,19 @@ fn write_run(items: impl Iterator, sizing: &Sizing) -> R { } let written = out.done().map_err(|e| io_error(&path, e))?; if bucket_auto { - memlimit::record_bucket_auto_spill(run.records as i64, written.bytes, written.stored); + memlimit::record_bucket_auto_spill( + run.records as i64, + written.bytes, + written.stored, + written.chunks, + ); } else { - memlimit::record_sort_spill(run.records as i64, written.bytes, written.stored); + memlimit::record_sort_spill( + run.records as i64, + written.bytes, + written.stored, + written.chunks, + ); } Ok(run) } diff --git a/crates/secantus-core/src/spill_file.rs b/crates/secantus-core/src/spill_file.rs index 5dc271377..ecb01d43e 100644 --- a/crates/secantus-core/src/spill_file.rs +++ b/crates/secantus-core/src/spill_file.rs @@ -390,11 +390,13 @@ fn document_to_bson(at: &mut Cursor, out: &mut Vec) -> R<()> { // --------------------------------------------------------------- files -- /// What a run wrote: `bytes` before compression (`spilledBytes`), `stored` -/// on disk (`spilledDataStorageSize`). +/// on disk (`spilledDataStorageSize`), and the `chunks` it was written in +/// (`metrics.query.sort.spillToDisk` counts them, RM-326). #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub(crate) struct Written { pub bytes: i64, pub stored: i64, + pub chunks: i64, } /// `SortedFileWriter`: records into chunks, chunks into a file. @@ -449,6 +451,7 @@ impl Writer { .and_then(|()| self.out.write_all(payload)) .map_err(|e| err("write", e))?; self.written.stored += 4 + payload.len() as i64; + self.written.chunks += 1; self.buffer.clear(); Ok(()) } diff --git a/tasks/backlog-rust-mongodb.md b/tasks/backlog-rust-mongodb.md index 23943f1a6..b0f51f4cd 100644 --- a/tasks/backlog-rust-mongodb.md +++ b/tasks/backlog-rust-mongodb.md @@ -1014,16 +1014,52 @@ live. Not filed here: `$rankFusion` / `$score` / `$scoreFusion` (RM-198), cannot reach today. Backlog-ID: RM-325 Verify: tests/backlog/test_rm_found_2026_10_26.py::test_spill_storage_size -- [ ] **A sort under a cursor stage reports its spills once, not per - batch.** When a query-layer sort feeds later stages through - mongod's cursor stage, 8.2.11's profile adds the sort's counters - again for every batch that stage reads: a `$setWindowFields` over 300 - documents, whose sort spills 13 times (as its `explain` says), is - profiled with `sortSpills: 52` -- four batches of 32, 64, 128 and 256. - `metrics.query.sort.spillToDisk` also counts differently from the - spills (`+26` for a sort of 29). This server reports each sort once. +- [ ] **A sort under a cursor stage is counted once behind stages whose + reads depend on the documents.** mongod 8.2.11's cursor stage adds + its executor's sort counts to its own at every buffer it loads + (`DocumentSourceCursor::loadBatch`), so a query-layer sort that + spilled is profiled once per load: this server counts the loads where + the stages after the cursor stage read a number of documents known + from the batch (one-for-one stages, `$skip`, `$limit`, a stage that + reads everything, a window that reaches its end). Behind an + `$unwind`, a `$match` that does not read to the end, a `$rank` or a + bounded window, or a partitioned `$setWindowFields`, what they read + for the first batch depends on the documents, and the sort is counted + once: `sortSpills: 13` where mongod says 39 (an `$unwind` of strings, + a `$rank`), or 42 (a partitioned window). Following those needs what + each stage read of its input, which the eager pipeline does not keep. Backlog-ID: RM-326 - Verify: tests/backlog/test_rm_found_2026_10_26.py::test_sort_profile_per_batch + Verify: tests/backlog/test_rm_326_sort_counts.py::test_sort_counts_per_load_through_stages_that_filter +- [ ] **A `getMore` reports none of its cursor's sort counts, and the + sort counters count each sort once.** mongod 8.2.11 profiles every + `getMore` of a cursor whose plan sorted with the sort's counts so far + (`sortSpills: 52` on the `getMore` after an aggregate that reported + 52; 29 on a `find`'s), and adds an operation's `keysSorted` and + `sortTotalDataSizeBytes` to `metrics.query.sort.totalKeysSorted` / + `totalBytesSorted` when it ends (`curop_metrics.cpp`), each `getMore` + included: 2400 keys for a sort of 300 read through four loads and one + `getMore`, 600 for a `find` of 300 and its `getMore`. This server's + `getMore` entries carry no sort counts, and it adds each sort's keys + and bytes once, as it sorts. + Backlog-ID: RM-400 + Verify: tests/backlog/test_rm_326_sort_counts.py::test_get_more_sort_counts +- [ ] **An SBE sort counts its bytes in `totalBytesSorted`.** mongod + 8.2.11's slot-based sort in front of a `$group` (`[{$sort: {n: 1}}, + {$group: {_id: "$k", f: {$first: "$n"}}}]` over 300 documents) adds + its 300 keys to `metrics.query.sort.totalKeysSorted` and nothing to + `totalBytesSorted`. This server adds 17700 bytes. + Backlog-ID: RM-398 + Verify: tests/backlog/test_rm_326_sort_counts.py::test_sbe_sort_bytes +- [ ] **A sort with a limit spills as mongod's top-k sorter does not.** + Over its budget mongod 8.2.11 spills a sort with a limit + (`TopKSorter`): `[$sort, {$limit: 200}, $addFields]` over 300 1-KB + documents spills 13 runs of 900 records under a 50000-byte budget, + where this server's limited sort spills nothing. A `$limit` mongod + moves into the sort past an `$addFields` (`extractLimitForPushdown`) + is not taken in here, so that sort spills as a whole sort would: 900 + records where mongod's spill 592. + Backlog-ID: RM-399 + Verify: tests/backlog/test_rm_326_sort_counts.py::test_top_k_sort_spills - [ ] **`$setWindowFields` refuses at slightly the wrong budget for some windows.** The partition cache and the functions are counted as mongod 8.2.11 counts them for whole-partition, `["unbounded", "current"]` and diff --git a/tests/backlog/test_rm_326_sort_counts.py b/tests/backlog/test_rm_326_sort_counts.py new file mode 100644 index 000000000..bf1a1246c --- /dev/null +++ b/tests/backlog/test_rm_326_sort_counts.py @@ -0,0 +1,238 @@ +"""Gap tests for the sort counters, found 2026-11-07 while closing part of +RM-326, by comparing the profile's sort counts and `serverStatus`'s +`metrics.query.sort` with mongod 8.2.11. + +Each test lowers the sort budget on both servers (`setParameter`), runs the +same work over the same small documents and compares what mongod reports. +""" + +from __future__ import annotations + +from typing import Any + +import pytest + +from tests.backlog.test_rm_found_2026_10_26 import _profiled, _spill_db, knob + +SORT_BUDGET = {"internalQueryMaxBlockingSortMemoryUsageBytes": 50_000} +FIELDS = ("sortSpills", "sortSpilledRecords", "sortSpilledBytes", "sortTotalDataSizeBytes") +ADD = {"$addFields": {"z": 1}} + + +def _sort_metrics(client: Any) -> dict: + sort = client.admin.command("serverStatus")["metrics"]["query"]["sort"] + return {k: int(sort[k]) for k in ("spillToDisk", "totalKeysSorted", "totalBytesSorted")} + + +def _find(tree: Any, wanted: Any) -> dict: + """The first document in an explain's tree that `wanted` accepts.""" + if isinstance(tree, dict): + if wanted(tree): + return tree + tree = list(tree.values()) + if isinstance(tree, list): + for item in tree: + found = _find(item, wanted) + if found: + return found + return {} + + +def _counts(db: Any, pipelines: dict) -> dict: + out = {} + for name, pipeline in pipelines.items(): + entry = _profiled(db, pipeline, SORT_BUDGET, f"rm326-{name}") + out[name] = {k: entry.get(k) for k in FIELDS} + return out + + +# ---------------------------------- RM-326 sort counts per load -- + + +def _per_load(client: Any) -> dict: + db = _spill_db(client) + out = _counts( + db, + { + "first batch": [{"$sort": {"n": -1}}, ADD], + "skip moved in": [{"$sort": {"n": -1}}, ADD, {"$skip": 150}], + "read to the end": [ + {"$sort": {"n": -1}}, + ADD, + {"$group": {"_id": "$k", "c": {"$sum": 1}}}, + ], + "window to the end": [ + { + "$setWindowFields": { + "sortBy": {"n": 1}, + "output": { + "t": {"$sum": "$n", "window": {"documents": ["current", "unbounded"]}} + }, + } + } + ], + }, + ) + # `explain` reports the sort's own counts, once. + for name, k in SORT_BUDGET.items(): + client.admin.command({"setParameter": 1, name: k}) + try: + explained = db.command( + "explain", + {"aggregate": "c", "pipeline": [{"$sort": {"n": -1}}, ADD], "cursor": {}}, + verbosity="executionStats", + ) + finally: + for name in SORT_BUDGET: + client.admin.command({"setParameter": 1, name: 104_857_600}) + stats = _find(explained, lambda d: "executionStages" in d) + sort = _find(stats, lambda d: d.get("stage") == "SORT") + out["explain"] = {k: sort.get(k) for k in ("spills", "spilledRecords", "spilledBytes")} + # `$bucketAuto`'s sorter writes chunks the sort counters count. + db.c.drop() + db.c.insert_many([{"_id": i, "s": "x" * 1000, "n": i} for i in range(300)]) + before = _sort_metrics(client) + with knob(client, "internalDocumentSourceBucketAutoMaxMemoryBytes", 50_000): + list( + db.c.aggregate( + [{"$bucketAuto": {"groupBy": "$n", "buckets": 5, "output": {"s": {"$push": "$s"}}}}] + ) + ) + out["bucketAuto chunks"] = _sort_metrics(client)["spillToDisk"] - before["spillToDisk"] + return out + + +@pytest.mark.backlog_fixed("RM-326") +def test_sort_counts_per_load(rust_mongo: Any, mongod: Any) -> None: + """mongod 8.2.11 counted a query-layer sort once per buffer its cursor + stage loaded (39 for a sort that spilled 13 under a first batch of 101, + 52 when the stages read it to the end) -- `explain` gave the sort's own + 13 -- and `spillToDisk` once per chunk any sorter wrote, `$bucketAuto`'s + included (32 for its 20 spills).""" + assert _per_load(rust_mongo) == _per_load(mongod) + + +def _depends_on_documents(client: Any) -> dict: + db = _spill_db(client) + return _counts( + db, + { + "$unwind": [{"$sort": {"n": -1}}, {"$unwind": "$s"}, {"$project": {"_id": 1}}], + "$rank": [{"$setWindowFields": {"sortBy": {"n": 1}, "output": {"r": {"$rank": {}}}}}], + "running total": [ + { + "$setWindowFields": { + "sortBy": {"n": 1}, + "output": { + "t": {"$sum": "$n", "window": {"documents": ["unbounded", "current"]}} + }, + } + } + ], + "partitioned": [ + { + "$setWindowFields": { + "partitionBy": "$k", + "sortBy": {"n": 1}, + "output": {"t": {"$sum": "$n"}}, + } + } + ], + }, + ) + + +@pytest.mark.backlog("RM-326") +def test_sort_counts_per_load_through_stages_that_filter(rust_mongo: Any, mongod: Any) -> None: + """mongod 8.2.11 counts the sort per load behind an `$unwind` (39), a + `$rank` or a running total (39), and a partitioned window (42).""" + assert _depends_on_documents(rust_mongo) == _depends_on_documents(mongod) + + +# ------------------------------- RM-400 getMore and the sort counters -- + + +def _get_mores(client: Any) -> dict: + db = _spill_db(client) + out = {} + for name, run in { + "aggregate": lambda: list( + db.c.aggregate( + [{"$setWindowFields": {"sortBy": {"n": 1}, "output": {"t": {"$sum": "$n"}}}}], + comment="rm397-aggregate", + ) + ), + "find": lambda: list( + db.c.find({}, {"_id": 1}).sort([("s", 1), ("n", -1)]).comment("rm397-find") + ), + }.items(): + before = _sort_metrics(client) + for k, v in SORT_BUDGET.items(): + client.admin.command({"setParameter": 1, k: v}) + db.command({"profile": 2}) + try: + run() + finally: + db.command({"profile": 0}) + for k in SORT_BUDGET: + client.admin.command({"setParameter": 1, k: 104_857_600}) + after = _sort_metrics(client) + entries = db.system.profile.find( + {"op": "getmore", "originatingCommand.comment": f"rm397-{name}"} + ) + out[name] = { + "getMore": [{k: e.get(k) for k in FIELDS} for e in entries], + "totalKeysSorted": after["totalKeysSorted"] - before["totalKeysSorted"], + "totalBytesSorted": after["totalBytesSorted"] - before["totalBytesSorted"], + } + return out + + +@pytest.mark.backlog("RM-400") +def test_get_more_sort_counts(rust_mongo: Any, mongod: Any) -> None: + """mongod 8.2.11 profiles a cursor's `getMore` with its sort's counts so + far (`sortSpills: 52` after an aggregate that reported 52), and adds the + operation's `keysSorted` / `sortTotalDataSizeBytes` to `totalKeysSorted` / + `totalBytesSorted` for each operation, the `getMore`s included (2400 + keys for a sort of 300 read through four loads, then one `getMore`).""" + assert _get_mores(rust_mongo) == _get_mores(mongod) + + +# ------------------------------------- RM-398 SBE's sort bytes -- + + +def _sbe_sorted(client: Any) -> dict: + db = _spill_db(client) + before = _sort_metrics(client) + list(db.c.aggregate([{"$sort": {"n": 1}}, {"$group": {"_id": "$k", "f": {"$first": "$n"}}}])) + after = _sort_metrics(client) + return {k: after[k] - before[k] for k in ("totalKeysSorted", "totalBytesSorted")} + + +@pytest.mark.backlog("RM-398") +def test_sbe_sort_bytes(rust_mongo: Any, mongod: Any) -> None: + """mongod 8.2.11's SBE sort in front of a `$group` counts its keys (300) + and no bytes in `totalBytesSorted`.""" + assert _sbe_sorted(rust_mongo) == _sbe_sorted(mongod) + + +# --------------------------------- RM-399 a sort with a limit spills -- + + +def _top_k(client: Any) -> dict: + db = _spill_db(client) + return _counts( + db, + { + "$limit after the sort": [{"$sort": {"n": -1}}, {"$limit": 200}, ADD], + "$limit moved into the sort": [{"$sort": {"n": -1}}, ADD, {"$limit": 50}], + }, + ) + + +@pytest.mark.backlog("RM-399") +def test_top_k_sort_spills(rust_mongo: Any, mongod: Any) -> None: + """mongod 8.2.11's sort with a limit over its budget spills: 13 runs, + 900 records for a limit of 200 over 300 documents, and 592 records for a + limit of 50 it moved past an `$addFields`.""" + assert _top_k(rust_mongo) == _top_k(mongod) diff --git a/tests/backlog/test_rm_found_2026_10_26.py b/tests/backlog/test_rm_found_2026_10_26.py index fb1f28d4d..5b35bdd44 100644 --- a/tests/backlog/test_rm_found_2026_10_26.py +++ b/tests/backlog/test_rm_found_2026_10_26.py @@ -225,10 +225,11 @@ def spill_to_disk() -> int: } -@pytest.mark.backlog("RM-326") +@pytest.mark.backlog_fixed("RM-326") def test_sort_profile_per_batch(rust_mongo: Any, mongod: Any) -> None: - """mongod 8.2.11 profiles the sort in front of a `$setWindowFields` once - per cursor-stage batch: `sortSpills: 52` for a sort that spilled 13.""" + """mongod 8.2.11 profiled the sort in front of a `$setWindowFields` once + per cursor-stage batch: `sortSpills: 52` for a sort that spilled 13, and + `spillToDisk` counted the 20 chunks its runs were written as.""" assert _per_batch(rust_mongo) == _per_batch(mongod) From 74631eb53909b6d58a3a529106f1a2fbfe4e85dc Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sat, 7 Nov 2026 23:20:55 +0000 Subject: [PATCH 4/4] RM-325's spill sizes compare per platform; RM-402 filed; RM-397 is arm64-only mongod 8.2's spill sizes differ by platform (in-order sort: 130545 bytes on arm64, 131252 on x86_64), and each platform's mongod is the reference (Joe, 2026-11-07). RM-325's regression test keeps every case on arm64 and drops the after-$project case on x86_64, where it is RM-402's gap (122288 vs 122253, CI). RM-397's gap exists on arm64 only (x86_64 agrees). Verified on arm64: regression passes, RM-402 ELSEWHERE, RM-397 OPEN. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QpXX74nAaz1CHpMnJwfkJp --- changelog.d/rm-325-per-platform.md | 7 ++++++ tasks/backlog-rust-mongodb.md | 14 +++++++++++- tests/backlog/test_rm_found_2026_10_26.py | 28 +++++++++++++++++++---- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 changelog.d/rm-325-per-platform.md diff --git a/changelog.d/rm-325-per-platform.md b/changelog.d/rm-325-per-platform.md new file mode 100644 index 000000000..335730b55 --- /dev/null +++ b/changelog.d/rm-325-per-platform.md @@ -0,0 +1,7 @@ +### Spill sizes are compared with each platform's own mongod + +mongod 8.2 writes spill files of different sizes on arm64 and x86_64. The same in-order sort stores 130,545 bytes on Apple silicon and 131,252 on CI's x86_64 Linux. Each platform's mongod is the reference. RM-325's regression test therefore compares every case on arm64. On x86_64 it leaves out the one case that still differs there, a sort after `$project` (122,288 bytes against mongod's 122,253), which is filed as RM-402 and checked on x86_64 by CI. RM-397's `$group` gap exists only on arm64, so its gap test now runs there only. + +#### Changed + +- `tests/backlog/test_rm_found_2026_10_26.py`: RM-325's regression test compares by platform, RM-402 has a new x86_64-only gap test, and RM-397's gap test runs on arm64 only. Filed RM-402. diff --git a/tasks/backlog-rust-mongodb.md b/tasks/backlog-rust-mongodb.md index b0f51f4cd..d189fe6cf 100644 --- a/tasks/backlog-rust-mongodb.md +++ b/tasks/backlog-rust-mongodb.md @@ -1110,11 +1110,23 @@ live. Not filed here: `$rankFusion` / `$score` / `$scoreFusion` (RM-198), these. Backlog-ID: RM-396 Verify: tests/backlog/test_rm_found_2026_10_26.py::test_added_fields_sort_spill_storage_size +- [ ] **On x86_64 a sort after `$project` stores 35 bytes more spill than + mongod.** mongod 8.2's spill sizes differ by platform (the in-order + sort stores 130545 bytes on arm64 and 131252 on x86_64), and each + platform's mongod is the reference (Joe, 2026-11-07). On arm64 this + server agrees on every RM-325 case. On CI's x86_64 Linux the 29 + spills of `[{$project: {s: 1, k: 1}}, {$sort: {k: -1, s: 1}}]` store + 122288 bytes where mongod stores 122253. Checked on x86_64 only, by + CI. Found 2026-11-07, snapshot PR #2447. + Backlog-ID: RM-402 + Verify: tests/backlog/test_rm_found_2026_10_26.py::test_x86_spill_after_project_storage_size - [ ] **A `$group` of `$sum` and `$count` counts less memory than mongod.** Spilling 300 groups of `{t: {$sum: "$n"}, c: {$count: {}}}` 22 times, mongod 8.2.1 reports `groupSpilledBytes: 115200` and this server 110400: the spills and records agree, so each group's accumulators - are counted 16 bytes short. + are counted 16 bytes short. That is mongod on arm64: on CI's x86_64 + Linux mongod and this server agree (2026-11-07), so the gap test runs + on arm64 only. Backlog-ID: RM-397 Verify: tests/backlog/test_rm_found_2026_10_26.py::test_group_sum_spilled_bytes - **Scope decision — SBE's `$concatArrays` refusal names a count mongod diff --git a/tests/backlog/test_rm_found_2026_10_26.py b/tests/backlog/test_rm_found_2026_10_26.py index 5b35bdd44..67680ac8f 100644 --- a/tests/backlog/test_rm_found_2026_10_26.py +++ b/tests/backlog/test_rm_found_2026_10_26.py @@ -15,7 +15,7 @@ import pytest -from tests.backlog.conftest import mongo_answer +from tests.backlog.conftest import PLATFORM, mongo_answer, only_on DEFAULT = 104_857_600 @@ -92,8 +92,24 @@ def test_spill_file_storage_size(rust_mongo: Any, mongod: Any) -> None: """A sorter's spill file was this server's own; it is now mongod 8.2's: 64 KiB chunks of `serializeForSorter` records, each snappy-compressed by mongod's compressor, so a sort's and `$bucketAuto`'s - `spilledDataStorageSize` are mongod's to the byte.""" - assert _sort_storage_sizes(rust_mongo) == _sort_storage_sizes(mongod) + `spilledDataStorageSize` are mongod's to the byte -- each platform's + mongod, whose sizes differ (the in-order sort: 130545 bytes on arm64, + 131252 on x86_64). On x86_64 the `after $project` case is RM-402's gap.""" + ours, theirs = _sort_storage_sizes(rust_mongo), _sort_storage_sizes(mongod) + if PLATFORM == "x86_64": + ours.pop("after $project") + theirs.pop("after $project") + assert ours == theirs + + +@pytest.mark.backlog("RM-402") +def test_x86_spill_after_project_storage_size(rust_mongo: Any, mongod: Any) -> None: + """On x86_64 mongod 8.2 stores 122253 bytes for the 29 spills of a sort + after `$project`; this server stores 122288 (CI, 2026-11-07). On arm64 + the two agree, pinned by RM-325's regression test.""" + only_on("x86_64", why="the arm64 sizes agree (RM-325's regression test)") + ours = _sort_storage_sizes(rust_mongo)["after $project"] + assert ours == _sort_storage_sizes(mongod)["after $project"] def _storage_sizes(client: Any) -> dict: @@ -197,8 +213,10 @@ def _group_sum_spilled_bytes(client: Any) -> Any: @pytest.mark.backlog("RM-397") def test_group_sum_spilled_bytes(rust_mongo: Any, mongod: Any) -> None: - """mongod 8.2 counts a `$group` of `$sum` and `$count` as 115200 bytes - over its 22 spills; this server counts 110400.""" + """mongod 8.2 on arm64 counts a `$group` of `$sum` and `$count` as + 115200 bytes over its 22 spills; this server counts 110400. On x86_64 + mongod and this server agree (CI, 2026-11-07).""" + only_on("arm64", why="x86_64 mongod and this server agree") assert _group_sum_spilled_bytes(rust_mongo) == _group_sum_spilled_bytes(mongod)