From 0f400ebdb3418e19cb25afa41966f5d150ade942 Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sun, 15 Nov 2026 04:18:26 +0000 Subject: [PATCH 1/7] Rust MongoDB server: a time-series read of the meta field scans the index over the buckets (RM-457; RM-701 filed) Co-Authored-By: Claude Opus 5.5 --- changelog.d/rm-457-ts-meta-index-scan.md | 13 ++ crates/secantus-commands/src/admin.rs | 16 +++ crates/secantus-commands/src/agg_stream.rs | 28 +++- crates/secantus-commands/src/ts_buckets.rs | 122 ++++++++++++++++++ tasks/backlog-rust-mongodb.md | 29 ++--- .../backlog/test_rm_found_2026_11_09_rm348.py | 22 +++- 6 files changed, 206 insertions(+), 24 deletions(-) create mode 100644 changelog.d/rm-457-ts-meta-index-scan.md diff --git a/changelog.d/rm-457-ts-meta-index-scan.md b/changelog.d/rm-457-ts-meta-index-scan.md new file mode 100644 index 000000000..297534a30 --- /dev/null +++ b/changelog.d/rm-457-ts-meta-index-scan.md @@ -0,0 +1,13 @@ +### A time-series read of the meta field scans the index over the buckets + +The Rust MongoDB server now builds a time-series collection's default +`m_1_t_1` index over its buckets, keyed as mongod keys it there +(`{meta: 1, control.min.t: 1, control.max.t: 1}`), and an `aggregate` +filtering on the meta field reads the buckets in that index's order, 32 at +a time and series by series, as mongod 8.2.11 does. + +#### Fixed + +- A time-series `aggregate` filtering on the meta field read the whole + collection at the command instead of buffering buckets from the index. + Fixes RM-457 (tests/backlog/test_rm_found_2026_11_09_rm348.py::test_a_time_series_read_of_the_meta_index_buffers_its_buckets) diff --git a/crates/secantus-commands/src/admin.rs b/crates/secantus-commands/src/admin.rs index b2c8b0a2e..0ea8eef57 100644 --- a/crates/secantus-commands/src/admin.rs +++ b/crates/secantus-commands/src/admin.rs @@ -591,6 +591,22 @@ pub fn create(doc: &Document, ctx: &mut CommandContext) -> HandlerResult { storage .create_index(&ctx.db_name, &coll, &name, &key, &spec) .map_err(command_error)?; + // mongod builds it over the buckets, keyed as it keys it there + // (`{meta: 1, control.min.t: 1, control.max.t: 1}`): a read of the + // meta field scans it (RM-457). + if let Some(layout) = crate::ts_store::layout_of(storage, &ctx.db_name, &coll) { + let on_buckets = crate::ts_buckets::bucket_key(&key, &layout); + let spec = doc! { "key": on_buckets.clone(), "name": name.clone() }; + storage + .create_index( + &ctx.db_name, + &crate::ts_store::buckets_ns(&coll), + &name, + &on_buckets, + &spec, + ) + .map_err(command_error)?; + } } Ok(doc! { "ok": 1.0 }) } diff --git a/crates/secantus-commands/src/agg_stream.rs b/crates/secantus-commands/src/agg_stream.rs index 167dacaaa..d53bfa052 100644 --- a/crates/secantus-commands/src/agg_stream.rs +++ b/crates/secantus-commands/src/agg_stream.rs @@ -446,12 +446,16 @@ fn time_series( .and_then(|o| crate::timeseries::Fields::of(&o)) .and_then(|f| f.default_index()) .map(|(name, _)| name); - let own_index = storage - .list_indexes(ask.db, ask.coll) - .map_err(read_error)? + let listed = storage.list_indexes(ask.db, ask.coll).map_err(read_error)?; + let names: Vec<&str> = listed .iter() .filter_map(|spec| spec.get_str("name").ok()) - .any(|name| name != "_id_" && Some(name) != default_index.as_deref()); + .collect(); + let own_index = names + .iter() + .any(|name| *name != "_id_" && Some(*name) != default_index.as_deref()); + // A dropped default index leaves its copy over the buckets: unused. + let default_index = default_index.filter(|d| names.contains(&d.as_str())); if own_index { return Ok(None); } @@ -460,9 +464,19 @@ fn time_series( else { return Ok(None); }; - if scan.filters_meta() { - return Ok(None); - } + // A filter on the meta field is mongod's scan of the default index + // over the buckets (RM-457). + let scan = if scan.filters_meta() { + match default_index { + Some(name) => match scan.through_index(storage, &name).map_err(read_error)? { + Some(scan) => scan, + None => return Ok(None), + }, + None => return Ok(None), + } + } else { + scan + }; let cursor = AggStream::new( Feed::Buckets(Box::new(scan)), Some(FIRST_BUFFER), diff --git a/crates/secantus-commands/src/ts_buckets.rs b/crates/secantus-commands/src/ts_buckets.rs index 30f6cccf5..136818eb4 100644 --- a/crates/secantus-commands/src/ts_buckets.rs +++ b/crates/secantus-commands/src/ts_buckets.rs @@ -305,6 +305,16 @@ pub(crate) struct BucketScan { filter: Document, /// The `_id` of the last bucket buffered. after: Option<[u8; 12]>, + /// The index over the buckets the scan reads in the order of, and the + /// last entry it reached and the buckets it has met (a multikey index + /// holds a bucket more than once). + index: Option, +} + +struct IndexPosition { + name: String, + last: Option<(Vec, i64)>, + seen: std::collections::HashSet, } /// What one refill of a [`BucketScan`] buffered. @@ -347,9 +357,40 @@ impl BucketScan { layout, filter, after: None, + index: None, })) } + /// This scan read in the order of index `name` over the buckets, as + /// mongod's IXSCAN of it under `$_internalUnpackBucket`; `None` when + /// the buckets hold no such index or the store cannot stream it. + pub(crate) fn through_index( + mut self, + storage: &dyn crate::storage::Storage, + name: &str, + ) -> Result, crate::storage::StorageError> { + let ns = crate::ts_store::buckets_ns(&self.coll); + if !storage + .list_indexes(&self.db, &ns)? + .iter() + .any(|ix| ix.get_str("name") == Ok(name)) + { + return Ok(None); + } + if storage + .index_entries_from(&self.db, &ns, name, &[], None, 0, false)? + .is_none() + { + return Ok(None); + } + self.index = Some(IndexPosition { + name: name.to_string(), + last: None, + seen: Default::default(), + }); + Ok(Some(self)) + } + /// The bucket-level filter names the meta field, which an index over /// the buckets can answer. pub(crate) fn filters_meta(&self) -> bool { @@ -376,6 +417,9 @@ impl BucketScan { want: usize, max_bytes: usize, ) -> Result { + if self.index.is_some() { + return self.next_indexed(storage, want, max_bytes); + } let rows = crate::ts_store::stored_bucket_bytes(storage, &self.db, &self.coll)?; let mut out = Buffered { docs: Vec::new(), @@ -415,6 +459,84 @@ impl BucketScan { } } +impl BucketScan { + /// [`Self::next_buffer`] through the index: its entries from the last + /// one reached, each bucket fetched as it is now. + fn next_indexed( + &mut self, + storage: &dyn crate::storage::Storage, + want: usize, + max_bytes: usize, + ) -> Result { + let ns = crate::ts_store::buckets_ns(&self.coll); + let err = |e| crate::util::command_error(e); + let mut out = Buffered { + docs: Vec::new(), + exhausted: false, + examined: 0, + }; + let (mut count, mut bytes) = (0usize, 0usize); + const CHUNK: usize = 64; + loop { + if count >= want || (count > 0 && bytes > max_bytes) { + return Ok(out); + } + let Some(pos) = self.index.as_mut() else { + out.exhausted = true; + return Ok(out); + }; + let (from, after) = match &pos.last { + Some((k, rid)) => (k.clone(), Some(*rid)), + None => (Vec::new(), None), + }; + let entries = storage + .index_entries_from(&self.db, &ns, &pos.name, &from, after, CHUNK, false) + .map_err(err)? + .unwrap_or_default(); + if entries.is_empty() { + out.exhausted = true; + return Ok(out); + } + let mut stopped = false; + for (key, rid) in entries { + if count >= want || (count > 0 && bytes > max_bytes) { + stopped = true; + break; + } + let pos = self.index.as_mut().expect("indexed scan"); + pos.last = Some((key, rid)); + if !pos.seen.insert(rid) { + continue; + } + let Some(Some(raw)) = storage + .records_by_id(&self.db, &ns, &[rid]) + .map_err(err)? + .and_then(|mut v| v.pop()) + else { + continue; + }; + out.examined += 1; + let document = secantus_core::bsondoc::from_bytes(&raw[..]) + .map_err(|e| CommandError::new(8, "UnknownError", e.to_string()))?; + if !self.filter.is_empty() && !bucket_matches(&document, &self.filter)? { + continue; + } + bytes += raw.len(); + count += 1; + for m in crate::ts_store::unpack(&document, &self.layout)? { + out.docs.push( + crate::bsondoc::to_vec(&m) + .map_err(|e| CommandError::new(8, "UnknownError", e.to_string()))?, + ); + } + } + if stopped { + return Ok(out); + } + } + } +} + /// Whether bucket document `bucket` passes the bucket-level filter. The /// `$_internalExpr*` comparisons are `mongod`'s: no type bracketing, a /// missing field below everything, and an array met on the path always diff --git a/tasks/backlog-rust-mongodb.md b/tasks/backlog-rust-mongodb.md index 973a9f00b..7e626adb5 100644 --- a/tasks/backlog-rust-mongodb.md +++ b/tasks/backlog-rust-mongodb.md @@ -2780,21 +2780,20 @@ differs on 5 scenarios. What is left: not yet confirmed from `explain`. Measured 2026-11-09. Backlog-ID: RM-456 Verify: tests/backlog/test_rm_found_2026_11_09_rm348.py::test_a_time_series_projection_reads_a_bucket_at_a_time -- [ ] **RUST MongoDB server: a time-series read an index over the buckets - could answer reads the collection at the command.** A filter on the - meta field (`{m: {$lt: 40}}`) is an index scan of the default - `m_1_t_1` index for mongod 8.2.11, which buffers 32 buckets in that - index's order -- series by series -- and returns 1,279 more after the - delete; this server reads such a read (and any over a collection with - an index of its own, or under a hint or a collation) whole: 3,199. - The buckets are stored since 2026-11-11, but the indexes are not - built over them: a time-series collection's index specs are kept on - the collection, every read scans the buckets in `_id` order, and - `collStats` reports its index sizes as 0. Streaming it means indexes - over the buckets and a bucket scan in an index's order. Measured - 2026-11-09. - Backlog-ID: RM-457 - Verify: tests/backlog/test_rm_found_2026_11_09_rm348.py::test_a_time_series_read_of_the_meta_index_buffers_its_buckets +- [ ] **RUST MongoDB server: a time-series read under a hint, or over a + collection with an index of its own, reads the collection at the + command.** Hinted to `m_1_t_1` (`{$match: {v: {$gte: 0}}}`), mongod + 8.2.11 scans that index over the buckets, buffering 32 series by + series, and returns 1,279 more after the delete; this server reads it + whole: 3,999. So too a read under a collation, and any read of a + collection with an index besides the default one. Since RM-457 the + default `m_1_t_1` index is built over the buckets at create and a + filter on the meta field scans it; an index `createIndexes` adds is + kept on the collection only, `dropIndexes` leaves the bucket copy of + `m_1_t_1` in place, and `collStats` still reports index sizes of 0. + Found while working RM-457, 2026-11-15. + Verify: tests/backlog/test_rm_found_2026_11_09_rm348.py::test_a_hinted_time_series_read_scans_the_index_over_the_buckets + Backlog-ID: RM-701 - **Not probed (RM-083's remainder, 2026-10-17)** -- `oplogReplay`, `$meta: "textScore"` sorts, an `explain: true` aggregate. Every `{$meta}` shape measured on 8.2.11 matches since 2026-10-17, except a diff --git a/tests/backlog/test_rm_found_2026_11_09_rm348.py b/tests/backlog/test_rm_found_2026_11_09_rm348.py index 33bd99c60..9f316ecdc 100644 --- a/tests/backlog/test_rm_found_2026_11_09_rm348.py +++ b/tests/backlog/test_rm_found_2026_11_09_rm348.py @@ -63,12 +63,14 @@ def read(c: Any) -> Any: assert _after_delete(rust_mongo, read) == want -@pytest.mark.backlog("RM-457") +@pytest.mark.backlog_fixed("RM-457") def test_a_time_series_read_of_the_meta_index_buffers_its_buckets( rust_mongo: Any, mongod: Any ) -> None: """mongod 8.2.11 reads a filter on the meta field from the default - `m_1_t_1` index, buffering 32 buckets series by series: 1,279.""" + `m_1_t_1` index, buffering 32 buckets series by series: 1,279. This + server read it whole, 3,199, until that index was built over the + buckets and the read scanned it.""" def read(c: Any) -> Any: return c.aggregate([{"$match": {"m": {"$lt": 40}}}], batchSize=1) @@ -76,3 +78,19 @@ def read(c: Any) -> Any: want = _after_delete(mongod, read) assert want == 1279 assert _after_delete(rust_mongo, read) == want + + +@pytest.mark.backlog("RM-701") +def test_a_hinted_time_series_read_scans_the_index_over_the_buckets( + rust_mongo: Any, mongod: Any +) -> None: + """mongod 8.2.11 reads a time-series `aggregate` hinted to the default + `m_1_t_1` index by scanning that index over the buckets, buffering 32 + of them series by series; this server reads a hinted read whole.""" + + def read(c: Any) -> Any: + return c.aggregate([{"$match": {"v": {"$gte": 0}}}], batchSize=1, hint="m_1_t_1") + + want = _after_delete(mongod, read) + assert want == 1279 + assert _after_delete(rust_mongo, read) == want From 10a15dcd41cf4842a1c7c494fdf7c25cd4128481 Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sun, 15 Nov 2026 04:04:51 +0000 Subject: [PATCH 2/7] Rust MongoDB server: a time-series aggregate behind an inclusion $project reads a bucket at a time (RM-456) Co-Authored-By: Claude Opus 5.5 --- .../rm-456-time-series-inclusion-project.md | 19 ++++++ crates/secantus-commands/src/agg_stream.rs | 60 ++++++++++++++++-- tasks/backlog-rust-mongodb.md | 25 ++++---- .../backlog/test_rm_found_2026_11_09_rm348.py | 6 +- tests/backlog/test_rm_found_rm456.py | 63 +++++++++++++++++++ tests/mongod_live/test_time_series_cursors.py | 7 +++ 6 files changed, 163 insertions(+), 17 deletions(-) create mode 100644 changelog.d/rm-456-time-series-inclusion-project.md create mode 100644 tests/backlog/test_rm_found_rm456.py diff --git a/changelog.d/rm-456-time-series-inclusion-project.md b/changelog.d/rm-456-time-series-inclusion-project.md new file mode 100644 index 000000000..00aab8274 --- /dev/null +++ b/changelog.d/rm-456-time-series-inclusion-project.md @@ -0,0 +1,19 @@ +### Rust MongoDB server: a time-series `aggregate` behind an inclusion `$project` reads a bucket at a time + +A time-series `aggregate` of `$match` and an inclusion `$project` now unpacks +its buckets one at a time as the cursor asks, with no cursor-stage buffer, as +mongod 8.2.11's slot-based engine does; an inclusion `$project` alone reads +through the 32-bucket buffer like the other streamed stages. Both used to read +the whole collection at the command. + +#### Fixed + +- A time-series cursor behind an inclusion `$project` returned 3,999 more + measurements after a delete where mongod returns 71 (with a `$match`) or + 2,303 (without one). + +Files RM-702 (an insert racing the TTL monitor fails with 112). + +Fixes RM-456 (tests/backlog/test_rm_found_2026_11_09_rm348.py::test_a_time_series_projection_reads_a_bucket_at_a_time) + +Filed RM-702: an insert into a time-series bucket the TTL monitor removes fails with 112. diff --git a/crates/secantus-commands/src/agg_stream.rs b/crates/secantus-commands/src/agg_stream.rs index d53bfa052..0c0e2dedc 100644 --- a/crates/secantus-commands/src/agg_stream.rs +++ b/crates/secantus-commands/src/agg_stream.rs @@ -410,6 +410,33 @@ fn excludes_only(spec: &Bson) -> bool { matches!(spec, Bson::Document(d) if !d.is_empty() && d.values().all(zero)) } +/// A `$project` that only keeps fields by name (`{v: 1}`, `_id` left out or +/// not): the fields mongod's `$_internalUnpackBucket` must unpack are then +/// known, and its slot-based engine takes the pipeline. +fn includes_only(spec: &Bson) -> bool { + fn kept(v: &Bson) -> bool { + match v { + Bson::Boolean(b) => *b, + Bson::Int32(n) => *n != 0, + Bson::Int64(n) => *n != 0, + Bson::Double(n) => *n != 0.0, + _ => false, + } + } + let Bson::Document(d) = spec else { + return false; + }; + d.iter().any(|(k, v)| k != "_id" && kept(v)) + && d.iter().all(|(k, v)| { + kept(v) + || (k == "_id" + && matches!( + v, + Bson::Boolean(_) | Bson::Int32(_) | Bson::Int64(_) | Bson::Double(_) + )) + }) +} + /// The cursor of a pipeline over a time-series collection: its buckets read /// through the cursor stage, each unpacked as the cursor reaches it /// (`ts_buckets::BucketScan`, RM-348). Read whole instead: under a hint or @@ -428,9 +455,27 @@ fn time_series( .and_then(|d| d.get("$project")) .is_some_and(excludes_only)) }; + let project = |s: &Bson| { + stage_name(s) == "$project" + && s.as_document() + .and_then(|d| d.get("$project")) + .is_some_and(includes_only) + }; + // `$match` and an inclusion `$project` are the slot-based engine's: it + // unpacks bucket by bucket as the cursor asks, with no cursor-stage + // buffer (measured 8.2.11, 2026-11-15: the rest of the first bucket + // after a delete, 71, RM-456). An inclusion `$project` alone is read + // through the cursor stage's buffer like the stages above (2,303). + let per_bucket = ask.pipeline.iter().any(project) + && ask.pipeline.iter().any(|s| stage_name(s) == "$match") + && ask + .pipeline + .iter() + .all(|s| project(s) || stage_name(s) == "$match"); + let only_projects = !ask.pipeline.is_empty() && ask.pipeline.iter().all(project); if ask.hint.is_some() || ask.collation.is_some() - || !ask.pipeline.iter().all(fits) + || !(per_bucket || only_projects || ask.pipeline.iter().all(fits)) || ask.pipeline.iter().any(mentions_meta) { return Ok(None); @@ -477,12 +522,13 @@ fn time_series( } else { scan }; - let cursor = AggStream::new( + let mut cursor = AggStream::new( Feed::Buckets(Box::new(scan)), - Some(FIRST_BUFFER), + Some(if per_bucket { 1 } else { FIRST_BUFFER }), ask.pipeline.to_vec(), ask, ); + cursor.per_bucket = per_bucket; Ok(Some((cursor, Vec::new()))) } @@ -719,6 +765,9 @@ struct AggStream { out_calls: VecDeque, /// The calls each stage owes the next document it passes on. owed: Vec, + /// A time-series read the slot-based engine unpacks one bucket at a + /// time, with no doubling cursor-stage buffer (RM-456). + per_bucket: bool, } fn stage_name(s: &Bson) -> &str { @@ -772,6 +821,7 @@ impl AggStream { in_plan: 0, ticks: secantus_core::deadline::FRESH_TICKS, out_calls: VecDeque::new(), + per_bucket: false, owed: vec![0; stages_len], } } @@ -943,7 +993,9 @@ impl AggStream { } Feed::Buckets(scan) => { let n = self.buffer.unwrap_or(FIRST_BUFFER); - self.buffer = Some(n * 2); + if !self.per_bucket { + self.buffer = Some(n * 2); + } let read = scan.next_buffer(storage, n, BUFFER_BYTES)?; Ok(StreamedBatch { docs: read.docs, diff --git a/tasks/backlog-rust-mongodb.md b/tasks/backlog-rust-mongodb.md index 7e626adb5..fdefc31b8 100644 --- a/tasks/backlog-rust-mongodb.md +++ b/tasks/backlog-rust-mongodb.md @@ -2769,17 +2769,6 @@ cursor options, `getMore`, `killCursors`, the cursors of `aggregate` and the listing commands, tailable cursors, `count`, `distinct`). The Rust server differs on 5 scenarios. What is left: -- [ ] **RUST MongoDB server: a time-series `aggregate` that needs only - some fields reads the collection at the command.** Behind an inclusion - `$project` (`{$project: {v: 1}}`) mongod 8.2.11 reads the buckets one - at a time as the cursor asks, not 32 at once: over the same 100 - buckets it returns 71 more after the delete (the rest of the first - bucket), and this server, which streams only stages that need the - whole measurement, 3,999. Likely its slot-based engine, which takes - `$_internalUnpackBucket` once the fields it must unpack are known; - not yet confirmed from `explain`. Measured 2026-11-09. - Backlog-ID: RM-456 - Verify: tests/backlog/test_rm_found_2026_11_09_rm348.py::test_a_time_series_projection_reads_a_bucket_at_a_time - [ ] **RUST MongoDB server: a time-series read under a hint, or over a collection with an index of its own, reads the collection at the command.** Hinted to `m_1_t_1` (`{$match: {v: {$gte: 0}}}`), mongod @@ -2794,6 +2783,20 @@ differs on 5 scenarios. What is left: Found while working RM-457, 2026-11-15. Verify: tests/backlog/test_rm_found_2026_11_09_rm348.py::test_a_hinted_time_series_read_scans_the_index_over_the_buckets Backlog-ID: RM-701 +- [ ] **RUST MongoDB server: an insert into a time-series bucket the TTL + monitor removes fails.** Inserting into a bucket that has expired + while the TTL monitor runs every second, mongod 8.2.11 never fails an + insert (it writes a new bucket); this server returns the write error + 112, "the time-series bucket was removed while it was written" + (`bucket_catalog.rs`: the update of the open bucket matched nothing), + to the client. Seen once in `tests/mongod_live/test_time_series_buckets.py::test_ttl_expires_whole_buckets` + in a gate run, reproduced by the gap test (a race: it inserts until + the first failure, up to 90 TTL passes). Also read, not yet probed: + `commit` returns `Ok` without writing when the bucket it staged into + is no longer open (`ts_store`'s delete clears it first), which may + lose a staged insert silently. Found working RM-456, 2026-11-15. + Verify: tests/backlog/test_rm_found_rm456.py::test_an_insert_into_a_bucket_the_ttl_monitor_removes_succeeds + Backlog-ID: RM-702 - **Not probed (RM-083's remainder, 2026-10-17)** -- `oplogReplay`, `$meta: "textScore"` sorts, an `explain: true` aggregate. Every `{$meta}` shape measured on 8.2.11 matches since 2026-10-17, except a diff --git a/tests/backlog/test_rm_found_2026_11_09_rm348.py b/tests/backlog/test_rm_found_2026_11_09_rm348.py index 9f316ecdc..018aee5c5 100644 --- a/tests/backlog/test_rm_found_2026_11_09_rm348.py +++ b/tests/backlog/test_rm_found_2026_11_09_rm348.py @@ -50,10 +50,12 @@ def read(c: Any) -> Any: assert _after_delete(rust_mongo, read) == want -@pytest.mark.backlog("RM-456") +@pytest.mark.backlog_fixed("RM-456") def test_a_time_series_projection_reads_a_bucket_at_a_time(rust_mongo: Any, mongod: Any) -> None: """mongod 8.2.11 reads a time-series `aggregate` behind an inclusion - `$project` one bucket at a time: the rest of the first bucket, 71.""" + `$project` one bucket at a time: the rest of the first bucket, 71. + This server read it whole, 3,999, until such a pipeline read its + buckets one at a time with no cursor-stage buffer.""" def read(c: Any) -> Any: return c.aggregate([{"$match": {"v": {"$gte": 0}}}, {"$project": {"v": 1}}], batchSize=1) diff --git a/tests/backlog/test_rm_found_rm456.py b/tests/backlog/test_rm_found_rm456.py new file mode 100644 index 000000000..db6c6af72 --- /dev/null +++ b/tests/backlog/test_rm_found_rm456.py @@ -0,0 +1,63 @@ +"""Gap test for what was found on 2026-11-15 while working RM-456: an +insert into a time-series bucket the TTL monitor is removing fails. + +The reference is mongod 8.2.11 (`mongod`); the Rust server is +`secantusd-rs` (`rust_mongo`). +""" + +from __future__ import annotations + +import datetime as dt +import threading +import time +import uuid +from typing import Any + +import pytest + + +def _insert_into_expiring_buckets(client: Any, seconds: float) -> list: + """Insert, from four threads for `seconds` or until one fails with the TTL monitor + running every second, measurements into buckets that have already + expired; the errors.""" + client.admin.command("setParameter", 1, ttlMonitorSleepSecs=1) + errors: list = [] + try: + db = client["rm_found_rm456"] + name = "ts" + uuid.uuid4().hex[:8] + db.create_collection( + name, timeseries={"timeField": "t", "metaField": "m"}, expireAfterSeconds=60 + ) + old = dt.datetime.now(dt.timezone.utc).replace(tzinfo=None) - dt.timedelta(hours=3) + deadline = time.monotonic() + seconds + + def insert(meta: int) -> None: + i = 0 + while time.monotonic() < deadline and not errors: + try: + db[name].insert_one({"t": old + dt.timedelta(milliseconds=i), "m": meta}) + except Exception as e: # noqa: BLE001 -- collected and asserted on + errors.append(str(e)[:120]) + i += 1 + + threads = [threading.Thread(target=insert, args=(m,)) for m in range(4)] + for t in threads: + t.start() + for t in threads: + t.join() + finally: + client.admin.command("setParameter", 1, ttlMonitorSleepSecs=60) + return errors + + +@pytest.mark.backlog("RM-702") +def test_an_insert_into_a_bucket_the_ttl_monitor_removes_succeeds( + rust_mongo: Any, mongod: Any +) -> None: + """mongod 8.2.11 retries an insert whose bucket the TTL monitor removed + under it, into a new bucket: no insert fails. This server returns the + write error 112, "the time-series bucket was removed while it was + written".""" + assert _insert_into_expiring_buckets(mongod, 10) == [] + # The race is a window in each TTL pass: up to 90 passes to find it. + assert _insert_into_expiring_buckets(rust_mongo, 90) == [] diff --git a/tests/mongod_live/test_time_series_cursors.py b/tests/mongod_live/test_time_series_cursors.py index ad0207880..7034be748 100644 --- a/tests/mongod_live/test_time_series_cursors.py +++ b/tests/mongod_live/test_time_series_cursors.py @@ -67,6 +67,13 @@ def _write_late_and_early(c: Any) -> None: "then $addFields": ([MATCH, {"$addFields": {"w": 1}}], _delete_all), "then $unset": ([MATCH, {"$unset": "m"}], _delete_all), "then an exclusion $project": ([MATCH, {"$project": {"m": 0}}], _delete_all), + "then an inclusion $project (RM-456)": ([MATCH, {"$project": {"v": 1}}], _delete_all), + "an inclusion $project leaving out _id": ( + [MATCH, {"$project": {"v": 1, "_id": 0}}], + _delete_all, + ), + "an inclusion $project before a $match": ([{"$project": {"v": 1}}, MATCH], _delete_all), + "an inclusion $project alone": ([{"$project": {"v": 1, "_id": 0}}], _delete_all), "then $skip": ([MATCH, {"$skip": 1}], _delete_all), "then $limit": ([MATCH, {"$limit": 5000}], _delete_all), "a later bucket written, an earlier one passed": ([], _write_late_and_early), From 2e963468003291a992db7b01820fd10147005975 Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sun, 15 Nov 2026 05:00:22 +0000 Subject: [PATCH 3/7] Rust MongoDB server: an insert's write conflict names the _id index; a retryable insert does not count a null (RM-087) Co-Authored-By: Claude Opus 5.5 --- .../rm-087-conflict-cause-and-null-stmtids.md | 16 ++++++++++ crates/secantus-commands/src/session.rs | 17 +++++++--- crates/secantus-commands/src/storage.rs | 7 +++++ crates/secantus-storage-adapter/src/lib.rs | 4 +++ crates/secantus-storage/src/lib.rs | 31 +++++++++++++++++-- tasks/backlog-rust-mongodb.md | 11 ++----- tests/backlog/test_rm_085_112.py | 8 ++--- 7 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 changelog.d/rm-087-conflict-cause-and-null-stmtids.md diff --git a/changelog.d/rm-087-conflict-cause-and-null-stmtids.md b/changelog.d/rm-087-conflict-cause-and-null-stmtids.md new file mode 100644 index 000000000..5279ad567 --- /dev/null +++ b/changelog.d/rm-087-conflict-cause-and-null-stmtids.md @@ -0,0 +1,16 @@ +### Rust MongoDB server: an insert's write conflict names the `_id` index, and a retryable insert does not count a null + +A transaction's insert that conflicts on an `_id` another open transaction +inserted now answers 112 with mongod 8.2.11's cause, naming the index and the +table it conflicted in (`WiredTigerIdIndex::_insert: index: _id_; uri: +table:`), where the cause was empty. A retryable insert drops a null +from its `documents` before counting them against `stmtIds`, as mongod does, +instead of refusing the write (16). + +#### Fixed + +- Fixes RM-087 (tests/backlog/test_rm_085_112.py::test_an_insert_conflict_names_the_index_table) +- Fixes RM-087 (tests/backlog/test_rm_085_112.py::test_a_retryable_insert_does_not_count_a_null) + +The storage change (`secantus-storage`'s write-conflict cause) is linked by the +PostgreSQL server too; it reads nothing new from it. diff --git a/crates/secantus-commands/src/session.rs b/crates/secantus-commands/src/session.rs index b37607c66..f3528f8ac 100644 --- a/crates/secantus-commands/src/session.rs +++ b/crates/secantus-commands/src/session.rs @@ -952,6 +952,8 @@ fn statement( let mut result = { let mut t = txn.lock().unwrap_or_else(|e| e.into_inner()); let handle = t.handle.as_mut().expect("handle created above").as_mut(); + // A cause left by an earlier statement is not this one's. + let _ = storage.take_write_conflict_cause(); match storage.run_in_user_transaction(handle, &mut || crate::run_handler(handler, doc, ctx)) { Ok(r) => r, @@ -971,11 +973,16 @@ fn statement( return result; } if name == "insert" && code == 112 { - // An insert conflicts on an index, and mongod names its own table for - // that index as the cause, or nothing. + // An insert conflicts on an index, and mongod names that index and + // its table as the cause (`WiredTigerIdIndex::_insert: ...` for the + // `_id` index), or nothing where the storage does not say (RM-087). + let cause = storage.take_write_conflict_cause().unwrap_or_default(); result.insert( "errmsg", - "Caused by :: :: Please retry your operation or multi-document transaction.", + format!( + "Caused by :: {cause} :: Please retry your operation or multi-document \ + transaction." + ), ); } crate::finish_txn_statement(registry, txn, &mut result); @@ -1247,9 +1254,11 @@ fn retryable_write( // The statement ids are part of the command: a malformed set is refused // before the session's number moves. let count = match name { + // mongod drops a null from `documents` before it counts them + // against `stmtIds` (RM-087, 8.2.11). "insert" => match (&ctx.raw_insert_documents, doc.get_array("documents")) { (Some(raw), _) => raw.len(), - (None, Ok(docs)) => docs.len(), + (None, Ok(docs)) => docs.iter().filter(|d| !matches!(d, Bson::Null)).count(), _ => 0, }, "update" => doc.get_array("updates").map_or(0, Vec::len), diff --git a/crates/secantus-commands/src/storage.rs b/crates/secantus-commands/src/storage.rs index 6749197b7..85e6799d9 100644 --- a/crates/secantus-commands/src/storage.rs +++ b/crates/secantus-commands/src/storage.rs @@ -1695,6 +1695,13 @@ pub trait Storage: Send + Sync { Ok(f()) } + /// What this thread's last write conflict conflicted in, as mongod names + /// it in a 112's cause (`WiredTigerIdIndex::_insert: index: _id_; uri: + /// table:`), taken so it is reported once. Default: unknown. + fn take_write_conflict_cause(&self) -> Option { + None + } + /// Has the catalog entry of `db.coll` (its indexes, its options, its /// existence) changed since `handle`'s snapshot was taken? mongod refuses /// such a transaction's write there (112). Default: never. diff --git a/crates/secantus-storage-adapter/src/lib.rs b/crates/secantus-storage-adapter/src/lib.rs index 9c019be68..07f737395 100644 --- a/crates/secantus-storage-adapter/src/lib.rs +++ b/crates/secantus-storage-adapter/src/lib.rs @@ -302,6 +302,10 @@ impl CmdStorage for StorageAdapter { self.inner.with_user_transaction(h, f).map_err(map_err) } + fn take_write_conflict_cause(&self) -> Option { + secantus_storage::take_write_conflict_cause() + } + fn catalog_changed_since_snapshot( &self, handle: &(dyn std::any::Any + Send), diff --git a/crates/secantus-storage/src/lib.rs b/crates/secantus-storage/src/lib.rs index b3472deaf..46e176bad 100644 --- a/crates/secantus-storage/src/lib.rs +++ b/crates/secantus-storage/src/lib.rs @@ -5749,6 +5749,11 @@ thread_local! { /// The document row this thread last wrote (or tried to): the row a /// write conflict surfacing from that write collided on. static LAST_ROW: RefCell> = const { RefCell::new(None) }; + /// What the last write conflict on this thread conflicted in, as mongod + /// names it in the 112's cause (`WiredTigerIdIndex::_insert: index: _id_; + /// uri: table:`): set where the `_id` index row is written, taken + /// by the command layer (RM-087). + static CONFLICT_CAUSE: RefCell> = const { RefCell::new(None) }; /// Set by `with_statement_txn` for the duration of a sync-mode autocommit /// write statement. When true, `emit_oplog_entries` parks its minted range @@ -6746,6 +6751,25 @@ fn test_point(point: TestPoint, row: &WrittenRow) { /// The document row this thread last wrote or tried to write. Read right /// after a write conflict, it is the row the conflict was on. +/// Take the cause of the last write conflict this thread met, where the +/// storage knows it: the `_id` index of an insert. `None` for a conflict +/// with no named cause, or none at all. +pub fn take_write_conflict_cause() -> Option { + CONFLICT_CAUSE.with(|c| c.borrow_mut().take()) +} + +/// mongod's cause for a conflict writing a collection's `_id` index row. +fn note_id_index_conflict(r: Result) -> Result { + if matches!(r, Err(StorageError::WriteConflict)) { + CONFLICT_CAUSE.with(|c| { + *c.borrow_mut() = Some(format!( + "WiredTigerIdIndex::_insert: index: _id_; uri: {NAT_SEQ_TABLE}" + )) + }); + } + r +} + pub fn last_row_written() -> Option { LAST_ROW.with(|l| l.borrow().clone()) } @@ -16948,14 +16972,15 @@ impl Storage { recordid: i64, ) -> Result { rev.reset()?; - note_id_key(db, coll, id_key)?; + CONFLICT_CAUSE.with(|c| c.borrow_mut().take()); + note_id_index_conflict(note_id_key(db, coll, id_key))?; rev.set_key_ssu(db, &key_coll(db, coll), id_key); rev.set_value_q(recordid); - match rev.insert() { + note_id_index_conflict(match rev.insert() { Ok(()) => Ok(recordid), Err(e) if e.is_duplicate_key() => Err(StorageError::DuplicateId), Err(e) => Err(e.into()), - } + }) } /// The `_id` index lookup: resolve a doc's `id_key` to its RecordId (the diff --git a/tasks/backlog-rust-mongodb.md b/tasks/backlog-rust-mongodb.md index fdefc31b8..f058d4891 100644 --- a/tasks/backlog-rust-mongodb.md +++ b/tasks/backlog-rust-mongodb.md @@ -2819,13 +2819,8 @@ scenarios (no server-side JavaScript, a scope decision), the collation key of a duplicate-key message (7.078) and the `createIndexes` below. The probe masks only the write-conflict cause. What is left: -- [ ] **RUST MongoDB server: smaller differences.** A write conflict on - an insert has an empty cause where mongod names its index and table - (`Caused by :: WiredTigerIdIndex::_insert: index: _id_; uri: - table: ::`). A retryable insert counts a null among its - `documents` against `stmtIds` and refuses it (16), where mongod drops - the null first and inserts. The other session errors are worded for a - session with no user (not measured with one). A `createIndexes` that +- [ ] **RUST MongoDB server: smaller differences.** The session errors + are worded for a session with no user (not measured with one). A `createIndexes` that waits on a transaction past `maxTimeMS` (the probe's "an index built under a transaction"): mongod 8.2.11 adds a `writeConcernError` (50) to its 50 in 2 runs of 2 on 2026-11-09, where this server answers a @@ -2833,7 +2828,7 @@ masks only the write-conflict cause. What is left: and oplog-entry dumps of the statement-id and `findAndModify` retry errors are mongod's since 2026-11-09.) Backlog-ID: RM-087 - Verify: tests/backlog/test_rm_085_112.py::test_an_insert_conflict_names_the_index_table, tests/backlog/test_rm_085_112.py::test_a_retryable_insert_does_not_count_a_null + Verify: none -- the wording is unmeasured with a user, and mongod's writeConcernError on the createIndexes timeout varies between runs (2 of 2 then, 0 of 12 before), so no test can pin one answer ### 7.071 Time-series collections -- 2026-10-11 diff --git a/tests/backlog/test_rm_085_112.py b/tests/backlog/test_rm_085_112.py index b31a9f1a0..c543b74f3 100644 --- a/tests/backlog/test_rm_085_112.py +++ b/tests/backlog/test_rm_085_112.py @@ -361,12 +361,12 @@ def send(number: int, body: list) -> None: assert got == ref -@pytest.mark.backlog("RM-087") +@pytest.mark.backlog_fixed("RM-087") def test_an_insert_conflict_names_the_index_table(rust_mongo: Any, mongod_rs: Any) -> None: """mongod 8.2.11: a transaction's insert of an `_id` another open transaction inserted answers 112, naming the index and the table it conflicted in (`WiredTigerIdIndex::_insert: index: _id_; uri: - table:`); the Rust server's cause is empty.""" + table:`); the Rust server's cause was empty until 2026-11-15.""" coll = _name("rm087") def probe(client: Any) -> str: @@ -388,12 +388,12 @@ def probe(client: Any) -> str: assert got == ref -@pytest.mark.backlog("RM-087") +@pytest.mark.backlog_fixed("RM-087") def test_a_retryable_insert_does_not_count_a_null(rust_mongo: Any, mongod_rs: Any) -> None: """mongod 8.2.11 drops a null from an insert's `documents` before it counts them against `stmtIds`: a retryable insert of `[{_id: 1}, null]` with one statement id inserts the document. The Rust server's session - layer counts the null and refuses it (16).""" + layer counted the null and refused it (16) until 2026-11-15.""" coll = _name("rm087") request = SON( [ From 1f93877df3c07597057b3f68016c50f272bbfad2 Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sun, 15 Nov 2026 06:07:55 +0000 Subject: [PATCH 4/7] Rust MongoDB server: time-series indexes list on the buckets as mongod keeps them (RM-548) Co-Authored-By: Claude Opus 5.5 --- changelog.d/rm-548-ts-bucket-index-listing.md | 17 +++ crates/secantus-commands/src/admin.rs | 19 +++ crates/secantus-commands/src/timeseries.rs | 114 +++++++++++++++++- crates/secantus-commands/src/ts_bucket_ns.rs | 63 ++++++++-- crates/secantus-storage/src/lib.rs | 1 + tasks/backlog-rust-mongodb.md | 13 -- .../backlog/test_rm_found_2026_11_11_rm088.py | 41 ++++++- 7 files changed, 242 insertions(+), 26 deletions(-) create mode 100644 changelog.d/rm-548-ts-bucket-index-listing.md diff --git a/changelog.d/rm-548-ts-bucket-index-listing.md b/changelog.d/rm-548-ts-bucket-index-listing.md new file mode 100644 index 000000000..f566c4550 --- /dev/null +++ b/changelog.d/rm-548-ts-bucket-index-listing.md @@ -0,0 +1,17 @@ +### Rust MongoDB server: a time-series collection's indexes list on its buckets as mongod keeps them + +`listIndexes` of `system.buckets.` now shows each index of a +time-series collection as mongod 8.2.11 keeps it on the buckets. + +#### Fixed + +- A TTL index on the time field is keyed `{control.min.