From be966a846512b3cf689abe31f14a924c7bf75df4 Mon Sep 17 00:00:00 2001 From: zipg Date: Thu, 10 Sep 2026 21:07:40 +0800 Subject: [PATCH] fix(mongodb): accept sharded cluster floating-point counts --- crates/dbx-core/src/db/mongo_driver.rs | 98 ++++++++++++++++++++- crates/dbx-core/tests/live_mongodb_count.rs | 37 ++++++++ 2 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 crates/dbx-core/tests/live_mongodb_count.rs diff --git a/crates/dbx-core/src/db/mongo_driver.rs b/crates/dbx-core/src/db/mongo_driver.rs index c5c048bb46..6b021124fb 100644 --- a/crates/dbx-core/src/db/mongo_driver.rs +++ b/crates/dbx-core/src/db/mongo_driver.rs @@ -1240,7 +1240,7 @@ async fn find_documents_with_total( } Some(count.await.map_err(|e| e.to_string())) } else { - Some(col.estimated_document_count().await.map_err(|e| e.to_string())) + Some(estimated_document_count(client, database, collection).await) }; let mut find = col.find(filter_doc).skip(skip).limit(limit); @@ -1361,12 +1361,49 @@ pub async fn count_documents( if !accurate && filter_doc.is_empty() { // Legacy count() permits the metadata-backed fast path; countDocuments() must scan accurately. - col.estimated_document_count().await.map_err(|e| e.to_string()) + estimated_document_count(client, database, collection).await } else { col.count_documents(filter_doc).await.map_err(|e| e.to_string()) } } +async fn estimated_document_count(client: &Client, database: &str, collection: &str) -> Result { + let result = client.database(database).run_command(doc! { "count": collection }).await; + match result { + Ok(result) => parse_count_command_result(&result), + Err(error) => parse_count_command_error(&error.kind).ok_or_else(|| error.to_string()), + } +} + +/// Resolves a failed `count` command the way the driver's `estimated_document_count` does: +/// NamespaceNotFound (code 26) means the collection is missing, i.e. an empty count, while any +/// other error must propagate. Mirrors mongodb's internal `Error::is_ns_not_found`. +fn parse_count_command_error(kind: &mongodb::error::ErrorKind) -> Option { + match kind { + mongodb::error::ErrorKind::Command(error) if error.code == 26 => Some(0), + _ => None, + } +} + +fn parse_count_command_result(result: &Document) -> Result { + let value = result.get("n").ok_or_else(|| "MongoDB count command response is missing the 'n' field".to_string())?; + + match value { + Bson::Int32(value) => u64::try_from(*value), + Bson::Int64(value) => u64::try_from(*value), + Bson::Double(value) + if value.is_finite() + && *value >= 0.0 + && value.fract() == 0.0 + && *value <= super::JS_MAX_SAFE_INTEGER as f64 => + { + Ok(*value as u64) + } + _ => return Err(format!("MongoDB count command returned an invalid 'n' value: {value:?}")), + } + .map_err(|_| format!("MongoDB count command returned a negative 'n' value: {value:?}")) +} + /// Find MongoDB documents in a browser-friendly representation. #[allow(clippy::too_many_arguments)] pub async fn find_documents_extended_json( @@ -1399,7 +1436,7 @@ pub async fn find_documents_extended_json( } count.await.map_err(|e| e.to_string()) } else { - col.estimated_document_count().await.map_err(|e| e.to_string()) + estimated_document_count(client, database, collection).await }; let mut find = col.find(filter_doc).skip(skip).limit(limit); @@ -2843,6 +2880,61 @@ mod tests { assert_eq!(resolve_mongo_find_total(Ok(250), false, 100, 25), (250, false)); } + #[test] + fn parses_sharded_mongo_count_returned_as_integral_double() { + let response = doc! { + "shards": { "shard01": 887_286_174.0, "shard02": 885_925_656.0 }, + "n": 1_773_211_830.0, + "ok": 1.0, + }; + + assert_eq!(parse_count_command_result(&response), Ok(1_773_211_830)); + } + + #[test] + fn parses_integer_mongo_count_results() { + assert_eq!(parse_count_command_result(&doc! { "n": 42_i32 }), Ok(42)); + assert_eq!(parse_count_command_result(&doc! { "n": 4_294_967_296_i64 }), Ok(4_294_967_296)); + } + + #[test] + fn rejects_invalid_mongo_count_results() { + for response in [ + doc! {}, + doc! { "n": -1_i32 }, + doc! { "n": 1.5 }, + doc! { "n": f64::NAN }, + doc! { "n": (super::super::JS_MAX_SAFE_INTEGER as f64) + 1.0 }, + doc! { "n": "10" }, + ] { + assert!(parse_count_command_result(&response).is_err(), "response should be rejected: {response:?}"); + } + } + + /// `CommandError` is `#[non_exhaustive]` with a private field, so tests build it through the + /// driver's derived `Deserialize`. + fn count_command_error(code: i32, code_name: &str) -> mongodb::error::CommandError { + serde_json::from_str(&format!(r#"{{"code": {code}, "codeName": "{code_name}", "errmsg": "count failed"}}"#)) + .unwrap() + } + + #[test] + fn maps_mongo_namespace_not_found_count_errors_to_zero() { + let kind = mongodb::error::ErrorKind::Command(count_command_error(26, "NamespaceNotFound")); + + assert_eq!(parse_count_command_error(&kind), Some(0)); + } + + #[test] + fn propagates_mongo_count_errors_other_than_namespace_not_found() { + for (code, code_name) in [(13, "Unauthorized"), (17405, "CommandNotFound")] { + let kind = mongodb::error::ErrorKind::Command(count_command_error(code, code_name)); + + assert_eq!(parse_count_command_error(&kind), None, "code {code} should propagate"); + } + assert_eq!(parse_count_command_error(&mongodb::error::ErrorKind::Shutdown), None); + } + #[test] fn detects_mongo_secondary_only_list_databases_errors() { assert!(list_databases_requires_secondary_fallback("NotWritablePrimary: not master")); diff --git a/crates/dbx-core/tests/live_mongodb_count.rs b/crates/dbx-core/tests/live_mongodb_count.rs new file mode 100644 index 0000000000..f82ccb4d1a --- /dev/null +++ b/crates/dbx-core/tests/live_mongodb_count.rs @@ -0,0 +1,37 @@ +//! Live MongoDB count regressions. +//! +//! Run with a writable MongoDB server: +//! ```text +//! DBX_LIVE_MONGODB_URL='mongodb://127.0.0.1:27017' \ +//! cargo test -p dbx-core --test live_mongodb_count -- --ignored --nocapture +//! ``` + +use std::time::Duration; + +use dbx_core::db::mongo_driver; + +#[tokio::test] +#[ignore = "requires DBX_LIVE_MONGODB_URL pointing at a writable MongoDB database"] +async fn count_supports_fast_accurate_and_browser_total_paths() { + let url = std::env::var("DBX_LIVE_MONGODB_URL").expect("DBX_LIVE_MONGODB_URL"); + let client = mongo_driver::connect(&url, Duration::from_secs(10), Duration::from_secs(60)).await.unwrap(); + let database = std::env::var("DBX_LIVE_MONGODB_DATABASE").unwrap_or_else(|_| "dbx_live_count".to_string()); + let collection = format!("count_paths_{}", std::process::id()); + + mongo_driver::insert_documents(&client, &database, &collection, r#"[{"n":1},{"n":2},{"n":3}]"#).await.unwrap(); + + let fast_count = mongo_driver::count_documents(&client, &database, &collection, None, false).await.unwrap(); + let accurate_count = mongo_driver::count_documents(&client, &database, &collection, None, true).await.unwrap(); + let browser_result = + mongo_driver::find_documents_extended_json(&client, &database, &collection, 0, 2, None, None, None, None) + .await + .unwrap(); + + assert_eq!(fast_count, 3); + assert_eq!(accurate_count, 3); + assert_eq!(browser_result.total, 3); + assert!(!browser_result.total_is_exact); + assert_eq!(browser_result.documents.len(), 2); + + mongo_driver::drop_collection(&client, &database, &collection).await.unwrap(); +}