Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions changelog.d/rm-185-malformed-locks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Rust MongoDB server: a malformed `dropIndexes`, `dbStats` or `listDatabases` takes no lock

mongod parses a command before it takes the command's locks, so a request
it refuses as malformed takes none. `dropIndexes` without `index`, and
`dbStats` and `listDatabases` with a wrongly typed or unknown field, now
parse before their locks here too, and their `serverStatus.locks` deltas
match mongod 8.2.11's (none). The other malformed commands of RM-185 were
closed by RM-386.

#### Fixed

- `dropIndexes`, `dbStats` and `listDatabases` parse in
`parse_before_locks`, before authorization, admission and locks.
- The lock comparison's index-build check no longer requires a build poll of
a `createIndexes` that mongod refuses before any build.

Fixes RM-185 (tests/backlog/test_rm_185_locks.py::test_lock_counts_of_other_malformed_commands)
15 changes: 15 additions & 0 deletions changelog.d/rm-350-nonlogged-crash-retry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### A retryable write retried after a crash under `--data-nonlogged` is not applied again

A data-nonlogged store writes a session's `config.transactions` row and a
retryable `findAndModify`'s image without an oplog entry, and recovers its
data tables from the oplog after a crash, so a SIGKILL lost both: a retried
insert failed on a duplicate key and a retried `findAndModify` changed its
document a second time. Crash recovery now rebuilds the row and the image as
it replays each retryable write's entry, as mongod 8.2.11's
`SessionUpdateTracker` does, and both retries are answered as after a clean
restart. The change is in `secantus-storage`, which the Rust PostgreSQL
server links too.

#### Fixed

- Fixes RM-350 (tests/backlog/test_rm_350_retry_after_restart.py::test_a_retry_after_a_crash_under_data_nonlogged_applies_nothing_twice)
11 changes: 11 additions & 0 deletions changelog.d/rm-423-meta-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Rust MongoDB server: an aggregate `$sort` on a `$meta` key runs

A `$sort` stage keyed by `{$meta: "randVal"}` or `{$meta: "searchScore"}`
was refused as an unsupported stage; it now sorts as mongod 8.2 does.

#### Fixed

- `{$sort: {s: {$meta: "randVal"}}}` orders the documents by a random value
drawn for each, and a `searchScore` key, which has no value without
`$search`, ties every document, so the other keys decide the order.
Fixes RM-423 (tests/backlog/test_rm_found_2026_11_08_rm332.py::test_a_sort_on_a_meta_key_runs)
13 changes: 13 additions & 0 deletions changelog.d/rm-425-cmd-line-opts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Rust MongoDB server: `getCmdLineOpts` reports the command line

`secantusd-rs` now answers `getCmdLineOpts` as mongod 8.2 does: `argv` is the
command line the process was started with, and `parsed` holds each option it
was given under its mongod configuration-file path (`--port 27031
--storage-path D` is `{net: {port: 27031}, storage: {dbPath: D}}`). The same
`parsed` is the `Options set by command line` startup log line and the
`cmdLine` of each `local.startup_log` record. Flags with no mongod option
(`--oplog-async`, `--cache-size`, ...) do not appear.

#### Fixed

- Fixes RM-425 (tests/backlog/test_rm_cmd_line_opts.py::test_get_cmd_line_opts_reports_the_command_line)
15 changes: 11 additions & 4 deletions crates/secantus-commands/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4075,23 +4075,30 @@ fn index_conflict(
}

/// `dropIndexes` — drop a named index, or all of them with `"*"`.
pub fn drop_indexes(doc: &Document, ctx: &mut CommandContext) -> HandlerResult {
/// `dropIndexes`'s `index`, parsed as mongod's IDL parses it: before the
/// command's locks, so a request without it takes none (RM-185, measured
/// 8.2.11). Returns the list of names when `index` is one.
pub fn drop_indexes_parse(doc: &Document) -> Result<Option<Vec<String>>, CommandError> {
// A list of names is a third form `index` takes, beside a name and a key.
let names: Option<Vec<String>> = match doc.get("index") {
Some(Bson::Array(a)) => a.iter().map(|v| v.as_str().map(String::from)).collect(),
_ => None,
};
if doc.get("index").is_none() {
return Ok(CommandError::new(
return Err(CommandError::new(
40414,
"IDLFailedToParse",
"BSON field 'dropIndexes.index' is missing but a required field",
)
.into_reply());
));
}
if names.is_none() {
argtypes::require_index_name_or_key(doc, "index", "dropIndexes.index")?;
}
Ok(names)
}

pub fn drop_indexes(doc: &Document, ctx: &mut CommandContext) -> HandlerResult {
let names = drop_indexes_parse(doc)?;
let coll = coll_arg(doc, "dropIndexes")?;
let storage = ctx.storage()?;
let exists = storage
Expand Down
38 changes: 31 additions & 7 deletions crates/secantus-commands/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,43 @@ pub fn fsync(doc: &Document, ctx: &mut CommandContext) -> HandlerResult {
/// `getCmdLineOpts` — parsed command line; drivers read
/// `parsed.security.authorization` to detect `--auth`.
pub fn get_cmd_line_opts(_doc: &Document, ctx: &mut CommandContext) -> HandlerResult {
let mut argv = vec![Bson::String("secantus".to_string())];
if ctx.require_auth {
argv.push(Bson::String("--auth".to_string()));
}
let argv: Vec<Bson> = match COMMAND_LINE.get() {
Some((argv, _)) => argv.iter().cloned().map(Bson::String).collect(),
None => {
let mut argv = vec![Bson::String("secantus".to_string())];
if ctx.require_auth {
argv.push(Bson::String("--auth".to_string()));
}
argv
}
};
Ok(doc! { "argv": argv, "parsed": parsed_options(ctx.require_auth), "ok": 1.0 })
}

/// The process's command line, recorded once by the `secantusd-rs` binary
/// before it serves: `argv` as the process was started, and the options it
/// gave under their mongod configuration-file paths (RM-425). An embedded
/// server has no command line and leaves it unset.
static COMMAND_LINE: std::sync::OnceLock<(Vec<String>, Document)> = std::sync::OnceLock::new();

/// Record the process's command line for `getCmdLineOpts`, the `Options set
/// by command line` log line and `local.startup_log`'s `cmdLine`. The first
/// call wins.
pub fn set_command_line(argv: Vec<String>, parsed: Document) {
let _ = COMMAND_LINE.set((argv, parsed));
}

/// The options the server was started with, as `getCmdLineOpts` reports
/// them under `parsed`; also the `cmdLine` of each `local.startup_log`
/// record, which mongod takes from the same place.
/// record, which mongod takes from the same place. Without a recorded
/// command line (an embedded server) it is `{net: {}, storage: {}}`, and
/// authorization from a configuration file shows as mongod shows it.
pub fn parsed_options(require_auth: bool) -> Document {
let mut parsed = doc! { "net": {}, "storage": {} };
if require_auth {
let mut parsed = match COMMAND_LINE.get() {
Some((_, parsed)) => parsed.clone(),
None => doc! { "net": {}, "storage": {} },
};
if require_auth && !parsed.contains_key("security") {
parsed.insert("security", doc! { "authorization": "enabled" });
}
parsed
Expand Down
3 changes: 3 additions & 0 deletions crates/secantus-commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,9 @@ fn parse_before_locks(
"count" => crud::count_parse(doc),
"findAndModify" | "findandmodify" => findandmodify::parse(doc),
"createIndexes" => argtypes::require_index_specs(doc),
"dropIndexes" => admin::drop_indexes_parse(doc).map(drop),
"dbStats" | "dbstats" => stats_admin::parse_db_stats(doc).map(drop),
"listDatabases" => stats_admin::parse_list_databases(doc).map(drop),
"drop" => admin::drop_parse(doc),
"listCollections" => admin::list_collections_parse(doc),
"listIndexes" => admin::list_indexes_parse(doc),
Expand Down
22 changes: 18 additions & 4 deletions crates/secantus-commands/src/stats_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ fn is_timeseries_definition(definition: &Document) -> bool {
.is_some_and(|stage| stage.contains_key("$_internalUnpackBucket"))
}

/// `dbStats`.
pub fn db_stats(doc: &Document, ctx: &mut CommandContext) -> HandlerResult {
/// `dbStats`'s fields, parsed as mongod's IDL parses them: before the
/// command's locks, so a malformed one takes none (RM-185, measured 8.2.11).
pub fn parse_db_stats(doc: &Document) -> Result<(i64, bool), CommandError> {
let name = doc.keys().next().map_or("dbStats", String::as_str);
unknown_field(doc, "dbStats", &[name, "scale", "freeStorage"])?;
let scale = scale_of(doc, "dbStats")?.unwrap_or(1);
Expand Down Expand Up @@ -172,6 +173,12 @@ pub fn db_stats(doc: &Document, ctx: &mut CommandContext) -> HandlerResult {
))
}
};
Ok((scale, free))
}

/// `dbStats`.
pub fn db_stats(doc: &Document, ctx: &mut CommandContext) -> HandlerResult {
let (scale, free) = parse_db_stats(doc)?;
let t = totals(ctx, &ctx.db_name)?;
let scaled = |n: i64| n as f64 / scale as f64;
let mut out = doc! {
Expand Down Expand Up @@ -599,8 +606,9 @@ fn bounded_size(
Ok((size, count))
}

/// `listDatabases`.
pub fn list_databases(doc: &Document, ctx: &mut CommandContext) -> HandlerResult {
/// `listDatabases`'s fields, parsed as mongod's IDL parses them: before
/// the command's locks (RM-185, measured 8.2.11). Returns `nameOnly`.
pub fn parse_list_databases(doc: &Document) -> Result<bool, CommandError> {
unknown_field(
doc,
"listDatabases",
Expand Down Expand Up @@ -639,6 +647,12 @@ pub fn list_databases(doc: &Document, ctx: &mut CommandContext) -> HandlerResult
))
}
}
Ok(name_only)
}

/// `listDatabases`.
pub fn list_databases(doc: &Document, ctx: &mut CommandContext) -> HandlerResult {
let name_only = parse_list_databases(doc)?;
if ctx.db_name != "admin" {
return Err(err(
13,
Expand Down
93 changes: 90 additions & 3 deletions crates/secantus-core/src/sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,13 +1163,26 @@ pub fn sort_documents_placed(
sizing: &Sizing,
limit: Option<usize>,
) -> R<(Vec<Document>, Vec<usize>)> {
let sort = crate::aggregate::sort_fields(spec)?;
let (sort, metas) = meta_sort_fields(spec)?;
let mut keyed: Vec<(Vec<Bson>, Document)> = Vec::with_capacity(docs.len());
for d in docs {
keyed.push((crate::aggregate::sort_key_values(&d, &sort)?, d));
let mut key = if sort.is_empty() {
Vec::new()
} else {
crate::aggregate::sort_key_values(&d, &sort)?
};
for (at, meta) in &metas {
key.insert(*at, meta.value());
}
keyed.push((key, d));
}
let mut dirs: Vec<bool> = sort.iter().map(|(_, rev)| *rev).collect();
for (at, _) in &metas {
// A `$meta` key sorts descending, as mongod's does.
dirs.insert(*at, true);
}
let cmp = |ka: &[Bson], kb: &[Bson]| {
for (i, (_, rev)) in sort.iter().enumerate() {
for (i, rev) in dirs.iter().enumerate() {
let c = crate::order::cmp(&ka[i], &kb[i]);
let c = if *rev { c.reverse() } else { c };
if c != Ordering::Equal {
Expand All @@ -1185,10 +1198,84 @@ pub fn sort_documents_placed(
.unzip())
}

/// A `$meta` key of a `$sort` stage.
enum MetaKey {
/// `{$meta: "randVal"}`: a fresh random double for each document.
RandVal,
/// `{$meta: "searchScore"}`: no value without `$search`, so every
/// document ties on it.
SearchScore,
}

impl MetaKey {
fn value(&self) -> Bson {
match self {
MetaKey::RandVal => Bson::Double(rand::random::<f64>()),
MetaKey::SearchScore => Bson::Null,
}
}
}

/// The field keys of `spec`, and its `$meta` keys with the place each takes
/// in the sort key. mongod 8.2.1 sorts a `$sort` stage by `{$meta:
/// "randVal"}` and by `{$meta: "searchScore"}` (RM-423); any other `$meta`,
/// and a spec with no key at all, is left to [`crate::aggregate::sort_fields`].
#[allow(clippy::type_complexity)]
fn meta_sort_fields(spec: &Bson) -> R<(Vec<(String, bool)>, Vec<(usize, MetaKey)>)> {
let Bson::Document(d) = spec else {
return Ok((crate::aggregate::sort_fields(spec)?, Vec::new()));
};
let meta = |v: &Bson| match v {
Bson::Document(m) if m.len() == 1 => match m.get("$meta") {
Some(Bson::String(k)) if k == "randVal" => Some(MetaKey::RandVal),
Some(Bson::String(k)) if k == "searchScore" => Some(MetaKey::SearchScore),
_ => None,
},
_ => None,
};
let mut fields = Document::new();
let mut metas = Vec::new();
for (i, (k, v)) in d.iter().enumerate() {
match meta(v) {
Some(m) => metas.push((i, m)),
None => {
fields.insert(k.clone(), v.clone());
}
}
}
if metas.is_empty() || !fields.is_empty() {
let sort = crate::aggregate::sort_fields(&Bson::Document(fields))?;
return Ok((sort, metas));
}
Ok((Vec::new(), metas))
}

#[cfg(test)]
mod tests {
use super::*;

/// RM-423: a `searchScore` key ties every document, leaving the order
/// to the other keys and then to arrival; a `randVal` key alone sorts.
#[test]
fn a_meta_sort_key_ties_or_draws() {
let docs: Vec<Document> = (0..6).map(|i| doc! { "_id": i, "a": i % 2 }).collect();
let sizing = Sizing {
sizes: None,
source: SortSource::QueryLayer,
fields: None,
added: None,
budget: memlimit::MAX_BYTES,
};
let spec = Bson::Document(doc! { "a": -1, "s": { "$meta": "searchScore" } });
let out = sort_documents(docs.clone(), &spec, &sizing, None).unwrap();
let ids: Vec<i32> = out.iter().map(|d| d.get_i32("_id").unwrap()).collect();
assert_eq!(ids, vec![1, 3, 5, 0, 2, 4]);
let spec = Bson::Document(doc! { "s": { "$meta": "randVal" } });
assert_eq!(sort_documents(docs, &spec, &sizing, None).unwrap().len(), 6);
let spec = Bson::Document(doc! { "s": { "$meta": "textScore" } });
assert!(sort_documents(Vec::new(), &spec, &sizing, None).is_err());
}

fn items(n: i32, len: usize) -> Vec<(Vec<Bson>, Document)> {
(0..n)
.map(|i| {
Expand Down
Loading
Loading