From c3f8bc8e39fcc8a72d608b9eb030cbd018e5fdc4 Mon Sep 17 00:00:00 2001 From: Lee Hannigan Date: Thu, 22 Oct 2026 00:12:27 +0000 Subject: [PATCH 01/44] fix(mongodb): retry an unconditional PutItem that loses the create race Amazon DynamoDB applies concurrent PutItem calls on one key one after another. A call without a condition succeeds, the last writer's item is stored, and with ReturnValues ALL_OLD each call returns the item it replaced. Measured on 2026-10-21 against the service in us-east-1 with client retries off: 32 writers x 63 rounds of racing unconditional puts on a new key per round answered 2,016 of 2,016 with 200 on hash, GSI and stream tables; 32 writers on one key for 63 rounds answered 2,016 of 2,016 on GSI and stream tables; one key with half the writers sending PutItem and half DeleteItem answered 22,400 puts and 22,400 deletes with 200 and no error (seven runs of 64 writers x 100 operations on a hash table, three runs of 32 x 200 on a GSI table); 32 writers racing PutItem against DeleteItem, UpdateItem and BatchWriteItem on one key answered every call with 200; and 64 writers with one putter per three deleters on one key answered 960 puts and 2,880 deletes with 200. In every cell the ALL_OLD images, the final item, the index and the stream records formed one serial history. tests/test_put_item_create_race.py runs against the service as well as against ExtendDB and checks the race on 8 clients. On MongoDB, two puts that create the same item could each find no document and each insert it. The second insert fails with a duplicate-key error (E11000), which findAndModify reports as a command error. The data collection has two unique indexes: _id_, and one on the key fields (pk, plus the sort key field on a table with a sort key). MongoDB retries such an upsert itself only when the filter is an equality on the fields of the index that raised the error. The put filters on pk and, on a table with a sort key, the sort key field, and every measured duplicate-key error names _id_, so the server does not retry it. The sessionless path (no condition, no index, no stream) returned the error as an internal error (HTTP 500), and the transaction path (a table with an index or a stream) classified it as fatal, which also returned an internal error. On a 16-core host the race test failed 2 or 3 of its 9 cases per run, each failure a racing put answering InternalServerError; 32 writers creating a new key per round got 3 InternalServerError responses in 2,016 puts; and one-key put and delete churn from 8 and 32 threads got 66 and 45 in 2,016 puts. Both paths now retry the put under the transient retry budget of 50 attempts with the same backoff the write-conflict retry uses. The next attempt finds the item the other put created, replaces it and returns it as the old image, so the ALL_OLD images form one chain; if a delete removed that item first, the next attempt inserts. On the transaction path the index update and the stream record take that item as the old image too. A put whose 50 attempts all lost the create race returns the internal error the PostgreSQL backend returns when its own retries of this race run out, with the attempt count in the message. is_duplicate_key now recognizes the command form as well as the write form that insert_one returns. The MongoDB xfail marker on the plain-table race tests is removed. devtools/run-mongodb-tests exports EXTENDDB_TEST_MONGODB_URI, and two storage crate tests use it, on the sessionless path and on the transaction path each: one forces every attempt to lose the race with a document that holds the item's _id under another pk and checks the give-up error after 50 attempts and that the document is unchanged; the other replaces or deletes that document 100 ms after the put starts and checks that the put succeeds, returns the other put's item or no old image, and stores its own item. Without the URI the tests print a SKIP line; with EXTENDDB_TEST_MONGODB_CONTAINER set and no URI they fail. The scratch databases are dropped when a test fails. Verified against MongoDB 7.0 as a single-node replica set. The race test passes 9 of 9 cases in each of 16 runs. The same cells as the service measurement answered every unconditional PutItem, DeleteItem and BatchWriteItem with 200 and no invariant violation; one-key put and delete churn answered 44,800 puts and 44,800 deletes with 200 (ten runs of 16 x 400 and four of 64 x 100) while 4,663 puts lost a create race and succeeded on a later attempt. With the backoff removed from the sessionless retry the lost-race-then-success test fails after 50 attempts; with a cap of 5 lost races put back on either path, with the transaction give-up error disabled, or with the transaction arm giving up at the first lost race, the storage crate tests fail; with the sessionless retry disabled the race test fails its 3 plain-table cases. One-key contention on GSI and stream tables still answers TransactionConflictException for some puts (issue 426), unchanged here. Closes #356 --- crates/storage-mongodb/src/data_engine.rs | 390 ++++++++++++++++++++-- devtools/run-mongodb-tests | 4 + tests/test_put_item_create_race.py | 18 - 3 files changed, 374 insertions(+), 38 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 3fda55293..0e1642b37 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -413,7 +413,24 @@ impl MongoEngine { // single-document atomicity instead. Two concurrent writes // serialize at the storage engine level; one wins the last- // writer-wins race and the other's version is overwritten. - // No txn, no retry loop, no possible 500 from contention. + // + // The one exception is two puts that both create the item: each + // finds no document and each inserts, and the second insert fails + // with a duplicate-key error. The collection has two unique + // indexes: `_id_`, and one on the key fields (`pk`, plus the sort + // key field on a table with a sort key). MongoDB retries such an + // upsert itself only when the filter is an equality on the fields + // of the index that raised the error. This filter names `pk` and, + // on a table with a sort key, the sort key field, so the server + // would retry a collision on the key-field index. Every + // duplicate-key error measured on MongoDB 7.0 names `_id_`, which + // the filter does not name, so the server does not retry it and + // this loop does. The error means another put's item is already in + // the collection, so the next attempt finds that item, replaces it + // and returns it as the old image; if a delete removed that item + // first, the next attempt inserts. Attempts share the transient + // retry budget and back off between them, as the transaction path + // below does. if condition.is_none() && stream.is_none() && self.gsi_cache_get_fresh(&key_info.table_id) == Some(false) @@ -424,13 +441,23 @@ impl MongoEngine { .upsert(true) .return_document(ReturnDocument::Before) .build(); - let old_doc = coll - .find_one_and_replace(key_filter, new_doc) - .with_options(opts) - .await - .map_err(|e| StorageError::Internal(e.to_string()))?; - let old_item = old_doc.as_ref().map(document_to_item).transpose()?; - return Ok(if return_old { old_item } else { None }); + for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + match coll + .find_one_and_replace(key_filter.clone(), &new_doc) + .with_options(opts.clone()) + .await + { + Ok(old_doc) => { + let old_item = old_doc.as_ref().map(document_to_item).transpose()?; + return Ok(if return_old { old_item } else { None }); + } + Err(e) if is_duplicate_key(&e) => { + backoff_sleep(attempt).await; + } + Err(e) => return Err(StorageError::Internal(e.to_string())), + } + } + return Err(create_race_exhausted(TRANSIENT_RETRY_ATTEMPTS)); } let mut session = self @@ -440,6 +467,7 @@ impl MongoEngine { .map_err(|e| StorageError::Internal(e.to_string()))?; let tx_options = self.write_transaction_options(); + let mut lost_create_races = 0; for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { let new_doc = @@ -455,6 +483,8 @@ impl MongoEngine { .await .map_err(|e| StorageError::Internal(e.to_string()))?; + // Set when the unconditional upsert below lost a create race. + let mut lost_create_race = false; let attempt_res: Result, TxErr> = async { let old_item: Option; @@ -521,12 +551,26 @@ impl MongoEngine { .upsert(true) .return_document(ReturnDocument::Before) .build(); - let old_doc = coll + // Same create race as the fast path above: a + // duplicate-key error means another put created the + // item first. Run the transaction again, so that it + // finds that item, replaces it, and removes its index + // entries and records it as the stream old image. A + // lost create race uses the same retry budget and + // backoff as a write conflict. + let old_doc = match coll .find_one_and_replace(key_filter.clone(), new_doc) .with_options(opts) .session(&mut session) .await - .map_err(TxErr::from)?; + { + Ok(old_doc) => old_doc, + Err(e) if is_duplicate_key(&e) => { + lost_create_race = true; + return Err(TxErr::Transient); + } + Err(e) => return Err(TxErr::from(e)), + }; old_item = old_doc.as_ref().map(document_to_item).transpose()?; } @@ -564,6 +608,9 @@ impl MongoEngine { }, Err(TxErr::Transient) => { let _ = session.abort_transaction().await; + if lost_create_race { + lost_create_races += 1; + } backoff_sleep(attempt).await; continue; } @@ -574,6 +621,12 @@ impl MongoEngine { } } + // Every attempt lost the create race: give up with the same error + // as the sessionless path above. + if lost_create_races == TRANSIENT_RETRY_ATTEMPTS { + return Err(create_race_exhausted(lost_create_races)); + } + // Retry ceiling exhausted. RFC-0003 §4.3 requires // `TransactionConflictException` when a single-item write can't // serialize against concurrent activity — never a bare 500. @@ -3609,17 +3662,36 @@ enum CommitOutcome { RerunBody, } -/// Detect a duplicate-key error (E11000, code 11000). Used at -/// conditional-insert sites — a duplicate is the manifestation of a -/// conditional-put race, so it must be surfaced as -/// `ConditionalCheckFailedException` rather than a 500. +/// Detect a duplicate-key error (E11000, code 11000). +/// +/// The driver reports it in two forms: `insert_one` returns it as a write +/// error, and `findAndModify` (`find_one_and_replace` with upsert) returns +/// it as a command error. At conditional-insert sites a duplicate is the +/// manifestation of a conditional-put race, so it must be surfaced as +/// `ConditionalCheckFailedException` rather than a 500. At unconditional +/// upsert sites it means another put created the item first, and the put +/// is retried so that it replaces that item. fn is_duplicate_key(e: &mongodb::error::Error) -> bool { - matches!( - *e.kind, - mongodb::error::ErrorKind::Write(mongodb::error::WriteFailure::WriteError( - mongodb::error::WriteError { code: 11000, .. } - )) - ) + matches!(*e.kind, mongodb::error::ErrorKind::Command(ref c) if c.code == 11000) + || matches!( + *e.kind, + mongodb::error::ErrorKind::Write(mongodb::error::WriteFailure::WriteError( + mongodb::error::WriteError { code: 11000, .. } + )) + ) +} + +/// The error when every attempt of an unconditional PutItem lost the create +/// race: each attempt found no item, and its insert hit an item another +/// writer created, which was deleted again before the next attempt read it. +/// `attempt` is the number of attempts made. The message is the internal +/// error (HTTP 500) the PostgreSQL backend returns when its own retries of +/// this race run out. +fn create_race_exhausted(attempt: u32) -> StorageError { + StorageError::Internal(format!( + "PutItem could not create the item after {attempt} attempts: each insert lost the \ + create race to a concurrent writer" + )) } /// Build a `mongodb::error::Error` carrying the given error labels. @@ -4136,6 +4208,284 @@ mod tests { assert!(!is_transient_write_conflict(&e)); } + /// Build a write error with the given code, the form `insert_one` + /// returns. + fn write_error(code: i32) -> mongodb::error::Error { + let write_error: mongodb::error::WriteError = bson::from_document(doc! { + "code": code, + "codeName": "SyntheticTestError", + "errmsg": "synthetic write error", + }) + .expect("static WriteError document deserializes"); + mongodb::error::ErrorKind::Write(mongodb::error::WriteFailure::WriteError(write_error)) + .into() + } + + #[test] + fn duplicate_key_is_recognized_as_a_command_error_and_as_a_write_error() { + // findAndModify, which an upserting find_one_and_replace sends, + // reports E11000 as a command error. + assert!(is_duplicate_key(&command_error(11000))); + // insert_one reports it as a write error. + assert!(is_duplicate_key(&write_error(11000))); + assert!(!is_duplicate_key(&command_error(112))); + assert!(!is_duplicate_key(&write_error(112))); + assert!(!is_transient_write_conflict(&command_error(11000))); + } + + /// Connection string of the MongoDB replica set for a test that needs a + /// server, or `None` after reporting that the test is skipped. + /// + /// `devtools/run-mongodb-tests` exports `EXTENDDB_TEST_MONGODB_URI` + /// together with `EXTENDDB_TEST_MONGODB_CONTAINER`. Without the URI the + /// test prints a SKIP line and returns, as the PostgreSQL storage crate's + /// live tests do. The line is written to the stderr handle, because the + /// test harness captures `eprintln!` output of a passing test. When the + /// container variable is set and the URI is not, the test is running + /// under that runner and the URI export is missing, so it fails instead + /// of skipping. + fn live_mongodb_uri(test: &str) -> Option { + let set = |name: &str| std::env::var(name).ok().filter(|v| !v.trim().is_empty()); + if let Some(uri) = set("EXTENDDB_TEST_MONGODB_URI") { + return Some(uri); + } + if let Some(container) = set("EXTENDDB_TEST_MONGODB_CONTAINER") { + panic!( + "{test}: EXTENDDB_TEST_MONGODB_CONTAINER is set ({container}) but \ + EXTENDDB_TEST_MONGODB_URI is not; devtools/run-mongodb-tests exports both" + ); + } + use std::io::Write as _; + let _ = writeln!( + std::io::stderr(), + "SKIP {test}: EXTENDDB_TEST_MONGODB_URI is not set, so there is no MongoDB replica \ + set to run against." + ); + None + } + + /// Run `body` with an engine on two new databases of the replica set at + /// `uri`, then drop both databases, also when `body` panics. + async fn with_scratch_engine(uri: &str, body: F) + where + F: FnOnce(MongoEngine, mongodb::Database) -> Fut, + Fut: std::future::Future, + { + use futures::FutureExt as _; + let client = mongodb::Client::with_uri_str(uri) + .await + .expect("EXTENDDB_TEST_MONGODB_URI must parse"); + let suffix = uuid::Uuid::new_v4().simple().to_string(); + let data_db = client.database(&format!("extenddb_test_{suffix}")); + let catalog_db = client.database(&format!("extenddb_test_{suffix}_catalog")); + let engine = MongoEngine { + client: client.clone(), + catalog_db: catalog_db.clone(), + data_db: data_db.clone(), + region: "us-east-1".to_owned(), + gsi_cache: Default::default(), + tx_read_concern: mongodb::options::ReadConcern::snapshot(), + }; + let outcome = std::panic::AssertUnwindSafe(body(engine, data_db.clone())) + .catch_unwind() + .await; + let _ = data_db.drop().await; + let _ = catalog_db.drop().await; + if let Err(panic) = outcome { + std::panic::resume_unwind(panic); + } + } + + /// Key info for a table with a string hash key `pk`. The GSI cache is set + /// so that an unconditional put takes the sessionless path (`has_gsi` + /// false) or the transaction path (`has_gsi` true). The catalog has no + /// index rows, so the transaction path writes no index entries. + fn hash_key_table(engine: &MongoEngine, table_id: &str, has_gsi: bool) -> TableKeyInfo { + engine.gsi_cache_set(table_id, has_gsi); + let key_schema = vec![KeySchemaElement { + attribute_name: "pk".to_owned(), + key_type: extenddb_core::types::KeyType::Hash, + }]; + TableKeyInfo { + table_id: table_id.to_owned(), + key_schema: key_schema.clone(), + base_key_schema: key_schema, + attribute_definitions: vec![extenddb_core::types::AttributeDefinition { + attribute_name: "pk".to_owned(), + attribute_type: ScalarAttributeType::S, + }], + ..Default::default() + } + } + + fn item_with(pk: &str, w: &str) -> Item { + let mut item = Item::new(); + item.insert("pk".to_owned(), AttributeValue::S(pk.to_owned())); + item.insert("w".to_owned(), AttributeValue::S(w.to_owned())); + item + } + + fn stored_document(item: &Item, key_info: &TableKeyInfo) -> Document { + item_to_document(item, &key_info.key_schema, &key_info.attribute_definitions) + .expect("test item converts to a document") + } + + /// An unconditional PutItem whose insert keeps colliding on `_id` retries + /// for the whole transient retry budget, then gives up with the create + /// race error, on the sessionless path and on the transaction path. + /// + /// A document that holds the item's `_id` under a different `pk` makes + /// every attempt collide: the put's filter on `pk` finds no item, and its + /// insert raises the same duplicate-key error a put gets when another + /// put created the item first. The obstructing document stays as it was. + #[tokio::test] + async fn unconditional_put_retries_a_lost_create_race_and_then_gives_up() { + let Some(uri) = + live_mongodb_uri("unconditional_put_retries_a_lost_create_race_and_then_gives_up") + else { + return; + }; + with_scratch_engine(&uri, |engine, data_db| async move { + let maps = ExpressionMaps::default(); + for (table_id, has_gsi) in [("sessionless-put", false), ("transaction-put", true)] { + let key_info = hash_key_table(&engine, table_id, has_gsi); + let item = item_with("k1", "new"); + let id = stored_document(&item, &key_info) + .get("_id") + .cloned() + .expect("document has an _id"); + let coll = data_db.collection::(&data_collection_name(table_id)); + coll.insert_one(doc! { "_id": id.clone(), "pk": "another-key", "item_data": {} }) + .await + .expect("insert the obstructing document"); + + let res = engine + .put_item_impl(&key_info, item, true, None, &maps, None) + .await; + + let want = + format!("could not create the item after {TRANSIENT_RETRY_ATTEMPTS} attempts"); + match res { + Err(StorageError::Internal(msg)) => assert!( + msg.contains(&want), + "{table_id}: want the create race error, got: {msg}" + ), + other => panic!("{table_id}: want the create race error, got: {other:?}"), + } + assert_eq!( + coll.count_documents(doc! {}).await.expect("count"), + 1, + "{table_id}: the put wrote a document" + ); + let left = coll + .find_one(doc! { "_id": id }) + .await + .expect("read back") + .expect("obstructing document is still there"); + assert_eq!(left.get_str("pk").expect("pk"), "another-key", "{table_id}"); + } + }) + .await; + } + + /// What happens to the obstructing document while the put retries. + #[derive(Clone, Copy, Debug)] + enum Obstruction { + /// It is replaced, under the same `_id`, with the item of another put + /// that created the key. + BecomesTheOtherPutsItem, + /// It is deleted, as a DeleteItem removes the item another put + /// created. + IsDeleted, + } + + /// An unconditional PutItem that lost the create race succeeds on a later + /// attempt, on the sessionless path and on the transaction path. + /// + /// The put starts while a document holds the item's `_id` under another + /// `pk`, so its first attempts collide. 100 ms later another task either + /// replaces that document with the item of another put on the same key, + /// or deletes it. The put's attempts back off between collisions and + /// together take longer than 100 ms, so a later attempt either replaces + /// the other put's item and returns it as the old image, or inserts and + /// returns no old image. In both cases the stored document is this put's + /// item. + #[tokio::test] + async fn unconditional_put_that_lost_a_create_race_succeeds_on_a_later_attempt() { + let Some(uri) = live_mongodb_uri( + "unconditional_put_that_lost_a_create_race_succeeds_on_a_later_attempt", + ) else { + return; + }; + with_scratch_engine(&uri, |engine, data_db| async move { + let maps = ExpressionMaps::default(); + for (path, has_gsi) in [("sessionless", false), ("transaction", true)] { + for change in [Obstruction::BecomesTheOtherPutsItem, Obstruction::IsDeleted] { + let table_id = format!("{path}-{change:?}"); + let key_info = hash_key_table(&engine, &table_id, has_gsi); + let item = item_with("k1", "this-put"); + let other_put = item_with("k1", "other-put"); + let other_put_doc = stored_document(&other_put, &key_info); + let id = other_put_doc + .get("_id") + .cloned() + .expect("document has an _id"); + let coll = data_db.collection::(&data_collection_name(&table_id)); + coll.insert_one( + doc! { "_id": id.clone(), "pk": "another-key", "item_data": {} }, + ) + .await + .expect("insert the obstructing document"); + + let put = + engine.put_item_impl(&key_info, item.clone(), true, None, &maps, None); + let clear = async { + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + match change { + Obstruction::BecomesTheOtherPutsItem => { + coll.replace_one(doc! { "_id": id.clone() }, other_put_doc.clone()) + .await + .expect("replace the obstructing document"); + } + Obstruction::IsDeleted => { + coll.delete_one(doc! { "_id": id.clone() }) + .await + .expect("delete the obstructing document"); + } + } + }; + let (res, ()) = tokio::join!(put, clear); + + let old = match res { + Ok(old) => old, + Err(e) => panic!("{table_id}: want the put to succeed, got: {e:?}"), + }; + let want_old = match change { + Obstruction::BecomesTheOtherPutsItem => Some(other_put), + Obstruction::IsDeleted => None, + }; + assert_eq!(old, want_old, "{table_id}: old image"); + assert_eq!( + coll.count_documents(doc! {}).await.expect("count"), + 1, + "{table_id}: documents in the collection" + ); + let stored = coll + .find_one(doc! { "_id": id }) + .await + .expect("read back") + .expect("the put's document is stored"); + assert_eq!( + document_to_item(&stored).expect("stored document converts to an item"), + item, + "{table_id}: stored item" + ); + } + } + }) + .await; + } + #[derive(Default)] struct RecordingGsiIndexWriter { direct_writes: AtomicUsize, diff --git a/devtools/run-mongodb-tests b/devtools/run-mongodb-tests index 14ecb9867..2198f3e57 100755 --- a/devtools/run-mongodb-tests +++ b/devtools/run-mongodb-tests @@ -264,6 +264,10 @@ export EXTENDDB_TEST_MONGODB_CONTAINER="$CONTAINER_NAME" # physical index lifecycle after exercising the DynamoDB API. export EXTENDDB_TEST_MONGODB_CONTAINER="$CONTAINER_NAME" +# Storage crate tests that need a live replica set connect here. Each test +# uses its own databases and drops them when it finishes. +export EXTENDDB_TEST_MONGODB_URI="mongodb://localhost:$MONGO_PORT/?replicaSet=rs0&directConnection=true" + # Some tests read EXTENDDB_CONFIG to invoke `extenddb settings` against # the running instance. Without this, they fall back to `./extenddb.toml` # in the repo root, which is a stale dev config that points at a diff --git a/tests/test_put_item_create_race.py b/tests/test_put_item_create_race.py index 752628d2a..cb5736617 100644 --- a/tests/test_put_item_create_race.py +++ b/tests/test_put_item_create_race.py @@ -32,22 +32,6 @@ ROUNDS = 15 -# TEMPORARY: on MongoDB, a put without a condition on a table with no index and -# no stream returns HTTP 500 when it loses the create race. The MongoDB -# write-race fix repairs it. That fix lands separately. -# TODO: remove this marker once the MongoDB write-race fix is on main. -# Only an assertion counts as the expected failure. The marker is not strict, -# because the race does not fire in every run: these tests still pass now and -# then on MongoDB without the fix. The MongoDB test runner sets -# EXTENDDB_TEST_MONGODB_CONTAINER. -XFAIL_UNTIL_MONGODB_FIX = pytest.mark.xfail( - bool(os.environ.get("EXTENDDB_TEST_MONGODB_CONTAINER", "").strip()), - reason="MongoDB returns HTTP 500 for a lost create race, fixed by the MongoDB write-race fix", - raises=AssertionError, - strict=False, -) - - @pytest.fixture(scope="module") def raw_client(endpoint_url): """A client that never retries, so every failure is seen.""" @@ -225,7 +209,6 @@ def _assert_one_chain(out: list, final: str): assert seen == WRITERS, f"old images do not form one chain: {prev}, final {final}" -@XFAIL_UNTIL_MONGODB_FIX @pytest.mark.parametrize("table_kind", ["hash", "range"]) def test_racing_puts_with_all_old_all_succeed( request, dynamodb_client, raw_client, table_kind @@ -237,7 +220,6 @@ def test_racing_puts_with_all_old_all_succeed( _assert_one_chain(out, _final_w(dynamodb_client, table, table_kind, pk)) -@XFAIL_UNTIL_MONGODB_FIX def test_racing_puts_that_return_consumed_capacity_all_succeed( dynamodb_client, raw_client, hash_table ): From db082abbdaf1ba78f4a3cc0c056f3734bae4a427 Mon Sep 17 00:00:00 2001 From: Lee Hannigan Date: Fri, 23 Oct 2026 15:10:27 +0000 Subject: [PATCH 02/44] fix(mongodb): retry an unconditional PutItem that conflicts on a GSI table Amazon DynamoDB applies concurrent PutItem calls on one key one after another and answers each with 200; with a global secondary index the index holds the final item's entry only. Measured on 2026-10-23 against the service in us-east-1 with client retries off: 32 writers putting one key of a table with a GSI, 2,016 puts, 2,016 answered 200, and the index held the entry of the final item and no other. On MongoDB a put on a table with an index writes the item and its index entries in one transaction. MongoDB aborts the loser of a same-document conflict at once instead of queueing it, so of N writers on one key one commits per round and the rest start over, and a writer needs on the order of N attempts, more under the connection pool's queueing. The retry budget of 50 attempts ran out for a share of the puts, and the put answered the 400 TransactionConflictException main reserves for a single-item write that could not serialize against concurrent activity. The budget is now 1,000 attempts for every write path in data_engine.rs. The backoff is random, so a writer can lose many rounds in a row, and the chance of losing every attempt falls off exponentially with the budget: 400 attempts left one put in 2,016 without a commit on a host at a load average of 30 and 20 of 1,984 with 64 writers. backoff_sleep is capped at 12.8 ms, so 1,000 attempts bound a write at about 6.4 s of backoff plus the attempts' own round trips before it gives up; measured against a transaction another session holds open, a put returns after 10.5 s. A PutItem that runs out of attempts returns StorageError::Transient, which the engine answers as HTTP 503 ServiceUnavailable. The service answers a PutItem with TransactionConflictException only while a TransactWriteItems holds the item, which this backend cannot tell apart from a race against other puts; the DynamoDB error-handling documentation lists ServiceUnavailable (HTTP 503) as "OK to retry? Yes", and the SDK retry modes retry a 503 on their own, whereas a 400 is handed to the application. Measured on MongoDB 7.0 (single-node replica set, client never retries), 32 writers putting one key of a GSI table, 2,016 puts: main (9d132022) answered 303 of 2,016 with 400 TransactionConflictException; c3f8bc8e (#431) answered 322 of 2,016 with 400 TransactionConflictException; this branch answered 2,016 with 200 in each of 3 runs of the wire test, and the index held the final item's entry only. tests/test_put_item_gsi_contention.py runs this shape and fails on main and on c3f8bc8e. Two storage crate tests run against the replica set named by EXTENDDB_TEST_MONGODB_URI: 32 writers putting one key on the transaction path all commit, and a put whose every attempt conflicts with a transaction another session holds open gives up with the transient error after 1,000 attempts, in 10.5 s, leaving the held item unchanged. Fixes #426 --- crates/storage-mongodb/src/data_engine.rs | 220 ++++++++++++++++++++-- docs/rfcs/0000-mongodb-backend.md | 4 +- tests/test_put_item_gsi_contention.py | 126 +++++++++++++ 3 files changed, 337 insertions(+), 13 deletions(-) create mode 100644 tests/test_put_item_gsi_contention.py diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 0e1642b37..c1fb7ab51 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -627,12 +627,19 @@ impl MongoEngine { return Err(create_race_exhausted(lost_create_races)); } - // Retry ceiling exhausted. RFC-0003 §4.3 requires - // `TransactionConflictException` when a single-item write can't - // serialize against concurrent activity — never a bare 500. - Err(StorageError::TransactionConflict( - "PutItem: too many concurrent write conflicts, giving up".to_owned(), - )) + // Retry ceiling exhausted. Amazon DynamoDB applies concurrent puts + // on one item one after another and answers each with 200; it + // answers `TransactionConflictException` to a PutItem only when the + // item is held by an in-flight TransactWriteItems, which this + // backend cannot tell apart from a lost race against other puts. + // Give up with a transient error, which the engine answers as HTTP + // 503 ServiceUnavailable: every AWS SDK retries that, and the retry + // succeeds once the contention has passed, whereas a + // `TransactionConflictException` is surfaced to the application. + Err(StorageError::Transient(format!( + "PutItem: the write lost {TRANSIENT_RETRY_ATTEMPTS} attempts to concurrent writers; \ + retry the request" + ))) } async fn get_item_impl( @@ -3544,11 +3551,22 @@ pub(crate) struct GsiBackfillProgress { // ── Contention / retry helpers ────────────────────────────────────────── /// Maximum number of times to retry a write that MongoDB aborted as a -/// transient conflict. Small enough that we don't lock a partition on -/// sustained hot-key contention; large enough to absorb ordinary -/// snapshot-isolation aborts. Matches the OCC retry ceiling elsewhere -/// in this file. -const TRANSIENT_RETRY_ATTEMPTS: u32 = 50; +/// transient conflict. +/// +/// MongoDB aborts the loser of a same-document conflict at once instead of +/// queueing it, so a hot key is settled by retries with backoff: of N +/// writers on one key, one commits per round and the rest start over. The +/// backoff is random, so a writer can lose many rounds in a row, and the +/// chance of losing every attempt falls off exponentially with the budget. +/// Measured with 32 clients putting one key of a table with a GSI (2,016 +/// puts per run, MongoDB 7.0 on one 16-core host): 50 attempts left 15 to +/// 25 percent of the puts without a commit; 400 left none in four runs at a +/// load average of 15 and one put in 2,016 at a load average of 30, and 20 +/// of 1,984 with 64 clients. With `backoff_sleep` capped at 12.8 ms, 1,000 +/// attempts bound the wait at about 6.4 s of backoff plus the attempts' own +/// round trips before the write gives up, under the SDKs' request timeouts. +/// Same ceiling for every write path in this file. +const TRANSIENT_RETRY_ATTEMPTS: u32 = 1000; /// Return type of `try_build_native_update`. Distinguishes an /// operator-document update (`{$set, $unset, $inc}`) from an @@ -4486,6 +4504,186 @@ mod tests { .await; } + /// Concurrent unconditional puts on one key of a table with a GSI all + /// commit: 32 writers put the same key 8 times each, every put returns + /// Ok, and the index holds the final item's entry only. On the service + /// every one of these puts answers 200; the backend reaches that through + /// its retry budget. + /// + /// The table has a GSI row in the catalog, so `sync_indexes_in_session` + /// writes an index entry per put and keeps the GSI cache at `true`. + /// Without the row the first committed put flips the cache to `false`, + /// the other writers take the sessionless path, and a sessionless + /// findAndModify on a document an open transaction has written waits for + /// that transaction; with 32 writers starting transactions without pause + /// the run stalled for minutes. + #[tokio::test(flavor = "multi_thread", worker_threads = 8)] + async fn concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit() { + let Some(uri) = + live_mongodb_uri("concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit") + else { + return; + }; + with_scratch_engine(&uri, |engine, data_db| async move { + const WRITERS: usize = 32; + const PUTS: usize = 8; + let engine = std::sync::Arc::new(engine); + let key_info = std::sync::Arc::new(hash_key_table(&engine, "hot-key", true)); + let gsi_key_schema = vec![KeySchemaElement { + attribute_name: "gk".to_owned(), + key_type: extenddb_core::types::KeyType::Hash, + }]; + let projection = Projection { + projection_type: ProjectionType::All, + non_key_attributes: None, + }; + engine + .catalog_db + .collection::("indexes") + .insert_one(doc! { + "_id": { "table_id": "hot-key", "index_name": "gk-index" }, + "index_id": "hot-key-gk-index", + "index_type": "GSI", + "key_schema": bson::to_bson(&gsi_key_schema).expect("key schema"), + "projection": bson::to_bson(&projection).expect("projection"), + "index_status": "ACTIVE", + }) + .await + .expect("insert the GSI catalog row"); + let start = std::sync::Arc::new(tokio::sync::Barrier::new(WRITERS)); + let mut tasks = Vec::with_capacity(WRITERS); + for w in 0..WRITERS { + let (engine, key_info, start) = (engine.clone(), key_info.clone(), start.clone()); + tasks.push(tokio::spawn(async move { + let maps = ExpressionMaps::default(); + start.wait().await; + let mut failures = Vec::new(); + for n in 0..PUTS { + let mut item = item_with("k1", &format!("w{w}-{n}")); + item.insert("gk".to_owned(), AttributeValue::S(format!("g-w{w}-{n}"))); + if let Err(e) = engine + .put_item_impl(&key_info, item, false, None, &maps, None) + .await + { + failures.push(format!("{e:?}")); + } + } + failures + })); + } + let mut failures = Vec::new(); + for task in tasks { + failures.extend(task.await.expect("writer task")); + } + assert!( + failures.is_empty(), + "{} of {} puts failed: {:?}", + failures.len(), + WRITERS * PUTS, + &failures[..failures.len().min(3)] + ); + let coll = data_db.collection::(&data_collection_name("hot-key")); + assert_eq!(coll.count_documents(doc! {}).await.expect("count"), 1); + let stored = coll + .find_one(doc! {}) + .await + .expect("read") + .expect("the item"); + let stored = document_to_item(&stored).expect("stored document converts to an item"); + let idx_coll = + data_db.collection::(&data_collection_name("hot-key-gk-index")); + use futures::TryStreamExt as _; + let entries: Vec = idx_coll + .find(doc! {}) + .await + .expect("read the index") + .try_collect() + .await + .expect("collect the index"); + assert_eq!(entries.len(), 1, "index entries: {entries:?}"); + let entry = document_to_item(&entries[0]).expect("index entry converts to an item"); + assert_eq!( + entry.get("gk"), + stored.get("gk"), + "index entry {entry:?} for {stored:?}" + ); + }) + .await; + } + + /// A put whose every attempt conflicts gives up with a transient error, + /// the one the engine answers as HTTP 503, not with a transaction + /// conflict. Another session writes the item inside a transaction it + /// never commits, so each attempt of the put hits a write conflict until + /// the budget is spent; the held transaction is then aborted. + #[tokio::test] + async fn unconditional_put_that_loses_every_attempt_gives_up_with_a_transient_error() { + let Some(uri) = live_mongodb_uri( + "unconditional_put_that_loses_every_attempt_gives_up_with_a_transient_error", + ) else { + return; + }; + with_scratch_engine(&uri, |engine, data_db| async move { + let maps = ExpressionMaps::default(); + let key_info = hash_key_table(&engine, "held-key", true); + let coll = data_db.collection::(&data_collection_name("held-key")); + let held = stored_document(&item_with("k1", "held"), &key_info); + coll.insert_one(held.clone()) + .await + .expect("insert the item"); + + let mut holder = engine.client.start_session().await.expect("session"); + holder + .start_transaction() + .with_options(engine.write_transaction_options()) + .await + .expect("start the holding transaction"); + let rewritten = stored_document(&item_with("k1", "rewritten"), &key_info); + coll.replace_one(doc! { "pk": "k1" }, rewritten) + .session(&mut holder) + .await + .expect("write the item inside the held transaction"); + + let started = std::time::Instant::now(); + let res = engine + .put_item_impl( + &key_info, + item_with("k1", "this-put"), + false, + None, + &maps, + None, + ) + .await; + let elapsed = started.elapsed(); + let _ = holder.abort_transaction().await; + + match res { + Err(StorageError::Transient(msg)) => assert!( + msg.contains(&format!("lost {TRANSIENT_RETRY_ATTEMPTS} attempts")), + "message: {msg}" + ), + other => panic!("want the transient exhausted-budget error, got: {other:?}"), + } + // The budget is bounded: 1,000 attempts with backoff capped at + // 12.8 ms end under a client's read timeout. + assert!( + elapsed < std::time::Duration::from_secs(30), + "took {elapsed:?}" + ); + let stored = coll + .find_one(doc! { "pk": "k1" }) + .await + .expect("read") + .expect("item"); + assert_eq!( + document_to_item(&stored).expect("item"), + item_with("k1", "held") + ); + }) + .await; + } + #[derive(Default)] struct RecordingGsiIndexWriter { direct_writes: AtomicUsize, diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index d5caacfce..4d029b362 100644 --- a/docs/rfcs/0000-mongodb-backend.md +++ b/docs/rfcs/0000-mongodb-backend.md @@ -136,7 +136,7 @@ Transaction implementation: `data_engine.rs` — `transact_write_items_impl`, `t ### Write conflict handling -**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for any of: the `TransientTransactionError` label, the `UnknownTransactionCommitResult` label, or a raw `WriteConflict` (code 112). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs) up to `TRANSIENT_RETRY_ATTEMPTS` (50). Exhausted retries on single-item operations return a `StorageError::Internal`; on `TransactWriteItems`, they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. +**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for any of: the `TransientTransactionError` label, the `UnknownTransactionCommitResult` label, or a raw `WriteConflict` (code 112). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs, capped at 12.8 ms) up to `TRANSIENT_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). Exhausted retries on PutItem return a `StorageError::Transient`, answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on DeleteItem and UpdateItem they return `TransactionConflict`; on `TransactWriteItems`, they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. **UpdateItem** additionally uses an optimistic-concurrency version guard on top of the transaction. A `_v` counter is stored on each document. The write path reads the current `_v` under the transaction snapshot, applies the update expression in memory, sets `_v = current_version + 1`, then executes `replace_one` filtered on both the primary key AND the expected `_v`. If `matched_count == 0`, a concurrent writer committed a higher version between the snapshot read and the replace; the attempt aborts and the outer retry loop re-reads. The version guard doubles up when a native-fast-path update (unconditional, no stream, no GSI) is possible — that path uses a single `find_one_and_update` with `$inc: {_v: 1}` outside a transaction, and always bumps the counter so a concurrent session-scoped update against a stale snapshot fails its versioned filter and retries. @@ -234,7 +234,7 @@ No changes to `crates/engine/`, `crates/server/`, `crates/storage/` (trait defin | Decision | Choice | Rationale | |---|---|---| | Conditional writes | Read + evaluate + write inside a MongoDB transaction session | Snapshot atomicity gives DynamoDB's contract; loaded item reused for `ReturnValuesOnConditionCheckFailure = ALL_OLD` without a follow-up read. Analyzer-gated pushdown fast path skips the session for a certified subset on tables with no GSIs / streams. | -| UpdateItem concurrency | `_v` version guard inside snapshot txn + WriteConflict retry with jittered exponential backoff | Prevents lost updates; retry ceiling (50) bounds tail latency under sustained contention. | +| UpdateItem concurrency | `_v` version guard inside snapshot txn + WriteConflict retry with jittered exponential backoff | Prevents lost updates; retry ceiling (1,000) bounds tail latency under sustained contention. | | WriteConflict handling | Detect via `TransientTransactionError` label, `UnknownTransactionCommitResult` label, or raw code 112; retry with backoff | Converts a raw HTTP 500 into a retryable operation; TWI exhaustion surfaces as `TransactionCanceled` with per-op `TransactionConflict` reasons. | | GSI updates | Synchronous inline within the base write's session, with 60-second TTL cache short-circuit for tables with no GSIs; async worker-driven backfill on UpdateTable | No Change Stream recovery; GSI reads are strongly consistent; UpdateTable matches DDB's async CREATING → ACTIVE contract. | | GSI/LSI index docs | Composite `_id` includes both index and base keys; `base_pk` / `base_sk_?` stored as first-class fields | GSI keys are non-unique; base-key disambiguation prevents cross-item overwrite. Base keys as fields let index pagination form a compound cursor without traversing item_data. | diff --git a/tests/test_put_item_gsi_contention.py b/tests/test_put_item_gsi_contention.py new file mode 100644 index 000000000..360c7d439 --- /dev/null +++ b/tests/test_put_item_gsi_contention.py @@ -0,0 +1,126 @@ +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 + +"""Many unconditional PutItem calls on one key of a table with a GSI. + +Amazon DynamoDB applies the puts one after another and answers 200 to each +one; the last writer wins, and the index holds the final item's entry only. +On the MongoDB backend a put on a table with an index writes the item and its +index entries in one transaction, so concurrent puts on one key abort each +other and are retried. These tests fail on a server whose retry budget runs +out under this contention and reports the lost race to the client. +""" + +from __future__ import annotations + +import os +import threading +import time +import uuid + +import boto3 +import pytest +from botocore.config import Config +from botocore.exceptions import ClientError + +from conftest import scoped_table + +WRITERS = 32 +PUTS_PER_WRITER = 63 + + +@pytest.fixture(scope="module") +def raw_client(endpoint_url): + """A client that never retries, so every failure is seen.""" + kwargs: dict = { + "service_name": "dynamodb", + "region_name": os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), + "config": Config( + retries={"total_max_attempts": 1, "mode": "standard"}, + max_pool_connections=WRITERS * 2, + ), + } + if endpoint_url: + kwargs["endpoint_url"] = endpoint_url + if endpoint_url.startswith("https://"): + kwargs["verify"] = False + return boto3.client(**kwargs) + + +@pytest.fixture(scope="module") +def gsi_table(dynamodb_client): + with scoped_table( + dynamodb_client, + attribute_definitions=[ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "gk", "AttributeType": "S"}, + ], + GlobalSecondaryIndexes=[ + { + "IndexName": "gk-index", + "KeySchema": [{"AttributeName": "gk", "KeyType": "HASH"}], + "Projection": {"ProjectionType": "ALL"}, + } + ], + ) as name: + yield name + + +def _hammer(client, table: str, pk: str) -> list[str]: + """WRITERS threads each put PUTS_PER_WRITER items on `pk`; return the failures.""" + start = threading.Barrier(WRITERS, timeout=30) + failures: list[str] = [] + errors: list[BaseException] = [] + lock = threading.Lock() + + def run(i: int): + try: + start.wait() + for n in range(PUTS_PER_WRITER): + item = {"pk": {"S": pk}, "gk": {"S": f"g-{pk}-{i}-{n}"}, "w": {"S": f"w{i}-{n}"}} + try: + client.put_item(TableName=table, Item=item) + except ClientError as e: + code = f"{e.response['ResponseMetadata']['HTTPStatusCode']} {e.response['Error']['Code']}" + with lock: + failures.append(code) + except BaseException as e: # noqa: BLE001 - surfaced below + errors.append(e) + + threads = [threading.Thread(target=run, args=(i,)) for i in range(WRITERS)] + for t in threads: + t.start() + for t in threads: + t.join() + if errors: + raise errors[0] + return failures + + +def _gsi_entries(client, table: str, pk: str) -> list[str]: + out: list[str] = [] + kwargs = {"TableName": table, "IndexName": "gk-index"} + while True: + resp = client.scan(**kwargs) + out.extend(i["gk"]["S"] for i in resp["Items"] if i["pk"]["S"] == pk) + if "LastEvaluatedKey" not in resp: + return sorted(out) + kwargs["ExclusiveStartKey"] = resp["LastEvaluatedKey"] + + +def test_unconditional_puts_on_one_key_of_a_gsi_table_all_succeed( + dynamodb_client, raw_client, gsi_table +): + pk = f"hot-{uuid.uuid4().hex[:8]}" + failures = _hammer(raw_client, gsi_table, pk) + total = WRITERS * PUTS_PER_WRITER + assert not failures, f"{len(failures)} of {total} puts failed: {sorted(set(failures))}" + final = dynamodb_client.get_item( + TableName=gsi_table, Key={"pk": {"S": pk}}, ConsistentRead=True + )["Item"] + # The GSI is eventually consistent on Amazon DynamoDB, so poll. + want = [final["gk"]["S"]] + deadline = time.monotonic() + 30 + while (got := _gsi_entries(dynamodb_client, gsi_table, pk)) != want and time.monotonic() < deadline: + time.sleep(0.5) + assert got == want, f"GSI entries for {pk}: want {want}, got {got}" From 544103c72930c1df8cf0862b45978197c318c6e5 Mon Sep 17 00:00:00 2001 From: Lee Hannigan Date: Fri, 23 Oct 2026 21:20:25 +0000 Subject: [PATCH 03/44] fix(mongodb): give the commit retry and the create race their own budgets TRANSIENT_RETRY_ATTEMPTS sized three different loops, and raising it to 1,000 for hot-key contention raised all three. Two of them are not contention loops and no measurement in this branch says anything about running them 1,000 times: commit_write_transaction retries commitTransaction in place after an UnknownTransactionCommitResult. The label means the commit's outcome is unknown, the commit is idempotent and the body must not be re-run, so those attempts wait for one reply rather than racing other writers. COMMIT_RETRY_ATTEMPTS keeps it at the 50 every write path used before. An unconditional PutItem that keeps losing the create race gives up with an internal error. #431 measured that path at 50 attempts with this backoff: a put blocked by a document another writer removes 100 ms after the put starts commits well inside it, and the storage tests fail with the cap put back to 5. CREATE_RACE_ATTEMPTS keeps it at 50, on the sessionless path and on the transaction path. The transaction path now gives up as soon as that budget is spent instead of carrying lost races through the contention budget. TRANSIENT_RETRY_ATTEMPTS stays at 1,000 and now sizes the contention loops only: put, delete, update, transact and the GSI backfill. The constant's doc comment said 1,000 attempts "bound the wait at about 6.4 s of backoff". backoff_sleep draws each sleep uniformly from 0 to the current base, capped at 12.8 ms, so 6.4 s is the mean of the sleeps and 12.8 s is the bound, before the attempts' own round trips. Measured on MongoDB 7.0 as a single-node replica set, a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.46 s, 9.85 s and 10.00 s over three runs, where each attempt also waits out MongoDB's lock timeout. The comment now states the mean, the bound and the measurement, and the test prints the elapsed time it already measured. The RFC's write-conflict paragraph said is_transient_write_conflict returns true for the UnknownTransactionCommitResult label. It does not, by design: under that label the commit may already have applied, so the body must not be re-run, and classify_commit_error handles it alone. The paragraph now also names the commit and create-race budgets and carries the corrected mean and bound. Issue #426 --- crates/storage-mongodb/src/data_engine.rs | 73 +++++++++++++++++------ docs/rfcs/0000-mongodb-backend.md | 2 +- 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index c1fb7ab51..392319f98 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -441,7 +441,7 @@ impl MongoEngine { .upsert(true) .return_document(ReturnDocument::Before) .build(); - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + for attempt in 0..CREATE_RACE_ATTEMPTS { match coll .find_one_and_replace(key_filter.clone(), &new_doc) .with_options(opts.clone()) @@ -457,7 +457,7 @@ impl MongoEngine { Err(e) => return Err(StorageError::Internal(e.to_string())), } } - return Err(create_race_exhausted(TRANSIENT_RETRY_ATTEMPTS)); + return Err(create_race_exhausted(CREATE_RACE_ATTEMPTS)); } let mut session = self @@ -610,6 +610,13 @@ impl MongoEngine { let _ = session.abort_transaction().await; if lost_create_race { lost_create_races += 1; + // The create race has its own budget: a put that has + // lost it this many times is not waiting out + // contention on an item, so it gives up here rather + // than spending the contention budget. + if lost_create_races >= CREATE_RACE_ATTEMPTS { + return Err(create_race_exhausted(lost_create_races)); + } } backoff_sleep(attempt).await; continue; @@ -621,12 +628,6 @@ impl MongoEngine { } } - // Every attempt lost the create race: give up with the same error - // as the sessionless path above. - if lost_create_races == TRANSIENT_RETRY_ATTEMPTS { - return Err(create_race_exhausted(lost_create_races)); - } - // Retry ceiling exhausted. Amazon DynamoDB applies concurrent puts // on one item one after another and answers each with 200; it // answers `TransactionConflictException` to a PutItem only when the @@ -3449,7 +3450,7 @@ impl MongoEngine { ) -> Result { #[cfg(not(feature = "test-hooks"))] let _ = gate_table; - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + for attempt in 0..COMMIT_RETRY_ATTEMPTS { let commit_res = session.commit_transaction().await; #[cfg(feature = "test-hooks")] let commit_res = self @@ -3562,12 +3563,44 @@ pub(crate) struct GsiBackfillProgress { /// puts per run, MongoDB 7.0 on one 16-core host): 50 attempts left 15 to /// 25 percent of the puts without a commit; 400 left none in four runs at a /// load average of 15 and one put in 2,016 at a load average of 30, and 20 -/// of 1,984 with 64 clients. With `backoff_sleep` capped at 12.8 ms, 1,000 -/// attempts bound the wait at about 6.4 s of backoff plus the attempts' own -/// round trips before the write gives up, under the SDKs' request timeouts. -/// Same ceiling for every write path in this file. +/// of 1,984 with 64 clients. `backoff_sleep` draws each sleep uniformly from +/// 0 to 12.8 ms once the base is capped, so 1,000 attempts sleep about 6.4 s +/// in total on average and 12.8 s at worst, plus the attempts' own round +/// trips, before the write gives up. Against a transaction another session +/// holds open, where an attempt waits out MongoDB's lock timeout, a put that +/// loses all 1,000 attempts returns in about 10 s, under the SDKs' request +/// timeouts. +/// +/// This budget sizes the contention retry loops only: the put, delete, +/// update, transact and GSI backfill loops, which re-run their transaction +/// body after a conflict. The commit retry and the create-race give-up have +/// their own budgets below, because the measurements that set this one say +/// nothing about either. const TRANSIENT_RETRY_ATTEMPTS: u32 = 1000; +/// Maximum number of times to retry `commitTransaction` in place after an +/// `UnknownTransactionCommitResult`. +/// +/// Not a contention loop: the label means the commit's outcome is unknown, +/// the commit is idempotent and the body must not be re-run, so these +/// attempts are waiting for one reply rather than racing other writers. No +/// measurement sizes a longer wait, and a longer one only delays the HTTP +/// 500 this path answers when the outcome stays unknown, so it keeps the +/// value every write path used before the contention budget was raised. +const COMMIT_RETRY_ATTEMPTS: u32 = 50; + +/// Maximum number of attempts of an unconditional PutItem that may lose the +/// create race before it gives up. +/// +/// A lost create race is not a write conflict: the attempt found no item and +/// its insert hit an item another writer created and deleted again. #431 +/// measured this budget, with its backoff, as the one that lets the race +/// resolve: a put blocked by a document that is removed 100 ms after the put +/// starts commits well inside it, and the storage tests fail when the cap is +/// put back to 5. A longer budget only delays the HTTP 500 this path +/// answers, so the contention budget above does not govern it. +const CREATE_RACE_ATTEMPTS: u32 = 50; + /// Return type of `try_build_native_update`. Distinguishes an /// operator-document update (`{$set, $unset, $inc}`) from an /// aggregation-pipeline update (needed for numeric ADD, which @@ -4349,8 +4382,8 @@ mod tests { } /// An unconditional PutItem whose insert keeps colliding on `_id` retries - /// for the whole transient retry budget, then gives up with the create - /// race error, on the sessionless path and on the transaction path. + /// for the whole create-race budget, then gives up with the create race + /// error, on the sessionless path and on the transaction path. /// /// A document that holds the item's `_id` under a different `pk` makes /// every attempt collide: the put's filter on `pk` finds no item, and its @@ -4382,7 +4415,7 @@ mod tests { .await; let want = - format!("could not create the item after {TRANSIENT_RETRY_ATTEMPTS} attempts"); + format!("could not create the item after {CREATE_RACE_ATTEMPTS} attempts"); match res { Err(StorageError::Internal(msg)) => assert!( msg.contains(&want), @@ -4665,8 +4698,12 @@ mod tests { ), other => panic!("want the transient exhausted-budget error, got: {other:?}"), } - // The budget is bounded: 1,000 attempts with backoff capped at - // 12.8 ms end under a client's read timeout. + // The sleeps alone come to about 6.4 s on average and 12.8 s at + // worst. Each attempt against a document a transaction holds also + // waits out MongoDB's lock timeout, so the wall time measured here + // runs to about 10 s. The bound asserted is the one a caller + // cares about: the write answers inside a client's read timeout. + println!("the put lost every attempt and answered in {elapsed:?}"); assert!( elapsed < std::time::Duration::from_secs(30), "took {elapsed:?}" diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index 4d029b362..61f7fb77c 100644 --- a/docs/rfcs/0000-mongodb-backend.md +++ b/docs/rfcs/0000-mongodb-backend.md @@ -136,7 +136,7 @@ Transaction implementation: `data_engine.rs` — `transact_write_items_impl`, `t ### Write conflict handling -**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for any of: the `TransientTransactionError` label, the `UnknownTransactionCommitResult` label, or a raw `WriteConflict` (code 112). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs, capped at 12.8 ms) up to `TRANSIENT_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). Exhausted retries on PutItem return a `StorageError::Transient`, answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on DeleteItem and UpdateItem they return `TransactionConflict`; on `TransactWriteItems`, they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. +**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place up to `COMMIT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs, each sleep drawn uniformly from 0 to the current base, capped at 12.8 ms) up to `TRANSIENT_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answers in about 10 s. An unconditional PutItem that keeps losing the create race has its own budget, `CREATE_RACE_ATTEMPTS` (50). Exhausted retries on PutItem return a `StorageError::Transient`, answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on DeleteItem and UpdateItem they return `TransactionConflict`; on `TransactWriteItems`, they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. **UpdateItem** additionally uses an optimistic-concurrency version guard on top of the transaction. A `_v` counter is stored on each document. The write path reads the current `_v` under the transaction snapshot, applies the update expression in memory, sets `_v = current_version + 1`, then executes `replace_one` filtered on both the primary key AND the expected `_v`. If `matched_count == 0`, a concurrent writer committed a higher version between the snapshot read and the replace; the attempt aborts and the outer retry loop re-reads. The version guard doubles up when a native-fast-path update (unconditional, no stream, no GSI) is possible — that path uses a single `find_one_and_update` with `$inc: {_v: 1}` outside a transaction, and always bumps the counter so a concurrent session-scoped update against a stale snapshot fails its versioned filter and retries. From b013ec61dd0cc364a18fcce5a868de24aa10e880 Mon Sep 17 00:00:00 2001 From: Lee Hannigan Date: Fri, 23 Oct 2026 23:20:22 +0000 Subject: [PATCH 04/44] fix(mongodb): set the restore fields in the scratch test engine main added four fields to MongoEngine in #384. The with_scratch_engine test helper builds the engine with a struct literal, so against a main that carries #384 it failed to compile with E0063. It now sets the four fields the way backup_live_tests.rs does. The comments on the retry budget give the measured wall time of a put that loses every attempt as a range from six runs on a shared host. --- crates/storage-mongodb/src/data_engine.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 2b8042325..56c6925b8 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -3568,8 +3568,8 @@ pub(crate) struct GsiBackfillProgress { /// in total on average and 12.8 s at worst, plus the attempts' own round /// trips, before the write gives up. Against a transaction another session /// holds open, where an attempt waits out MongoDB's lock timeout, a put that -/// loses all 1,000 attempts returns in about 10 s, under the SDKs' request -/// timeouts. +/// loses all 1,000 attempts returned in 9.5 to 12.7 s over six runs on a +/// shared host, under the SDKs' request timeouts. /// /// This budget sizes the contention retry loops only: the put, delete, /// update, transact and GSI backfill loops, which re-run their transaction @@ -4329,6 +4329,10 @@ mod tests { let suffix = uuid::Uuid::new_v4().simple().to_string(); let data_db = client.database(&format!("extenddb_test_{suffix}")); let catalog_db = client.database(&format!("extenddb_test_{suffix}_catalog")); + // The four restore and liveness fields as `MongoEngine::new` sets + // them, with the slot count derived from the driver's default pool + // (10), as in backup_live_tests.rs. + let slots = crate::backup_engine::restore_server_slot_count(10); let engine = MongoEngine { client: client.clone(), catalog_db: catalog_db.clone(), @@ -4336,6 +4340,10 @@ mod tests { region: "us-east-1".to_owned(), gsi_cache: Default::default(), tx_read_concern: mongodb::options::ReadConcern::snapshot(), + backup_liveness: crate::backup_engine::BackupLiveness::default(), + restore_server_slot_count: slots, + restore_server_slots: std::sync::Arc::new(tokio::sync::Semaphore::new(slots)), + restore_account_slots: crate::backup_engine::RestoreAccountSlots::default(), }; let outcome = std::panic::AssertUnwindSafe(body(engine, data_db.clone())) .catch_unwind() @@ -4701,7 +4709,8 @@ mod tests { // The sleeps alone come to about 6.4 s on average and 12.8 s at // worst. Each attempt against a document a transaction holds also // waits out MongoDB's lock timeout, so the wall time measured here - // runs to about 10 s. The bound asserted is the one a caller + // ran 9.5 to 12.7 s on a shared host. The bound asserted is the + // one a caller // cares about: the write answers inside a client's read timeout. println!("the put lost every attempt and answered in {elapsed:?}"); assert!( From b0c45f44dffbe53a33773e1bcbb78ce0e4ecae3b Mon Sep 17 00:00:00 2001 From: Lee Hannigan Date: Sat, 24 Oct 2026 00:40:31 +0000 Subject: [PATCH 05/44] fix(mongodb): stop retrying a contended write after 10 s The 1,000-attempt budget bounds attempts, not the time a caller waits. At 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting when the client read timeout fired at 120 s, past the SDKs' default read timeouts. The contention loops of PutItem, DeleteItem, UpdateItem and TransactWriteItems now read CONTENTION_DEADLINE (10 s) before each attempt after the first. A single-item write that reaches it answers ServiceUnavailable (HTTP 503), which the SDKs retry. A transaction that reaches it answers TransactionCanceledException with a TransactionConflict reason per item, as it does when its attempts run out and as the service answers a transaction that loses to contention. The GSI backfill loop runs in the background worker and takes no deadline. --- crates/storage-mongodb/src/data_engine.rs | 151 ++++++++++++++++++++-- docs/rfcs/0000-mongodb-backend.md | 2 +- 2 files changed, 140 insertions(+), 13 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 56c6925b8..3497eca00 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -468,8 +468,12 @@ impl MongoEngine { let tx_options = self.write_transaction_options(); let mut lost_create_races = 0; + let contention_started = std::time::Instant::now(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + if contention_deadline_passed(attempt, contention_started) { + return Err(contention_deadline_exhausted("PutItem")); + } let new_doc = item_to_document(&item, &key_info.key_schema, &key_info.attribute_definitions)?; // Capture before the transaction's first database operation. If @@ -711,8 +715,12 @@ impl MongoEngine { .map_err(|e| StorageError::Internal(e.to_string()))?; let tx_options = self.write_transaction_options(); + let contention_started = std::time::Instant::now(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + if contention_deadline_passed(attempt, contention_started) { + return Err(contention_deadline_exhausted("DeleteItem")); + } // Capture before the transaction's first database operation so a // concurrent catalog mutation cannot be hidden by the snapshot. let cache_generation = self.gsi_cache_generation(&key_info.table_id); @@ -936,8 +944,12 @@ impl MongoEngine { .map_err(|e| StorageError::Internal(e.to_string()))?; let tx_options = self.write_transaction_options(); + let contention_started = std::time::Instant::now(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + if contention_deadline_passed(attempt, contention_started) { + return Err(contention_deadline_exhausted("UpdateItem")); + } // Capture before the transaction's first database operation. A // later UpdateTable must invalidate this observation rather than // allowing a stale no-index result to be published. @@ -2252,7 +2264,18 @@ impl MongoEngine { ) }); + let contention_started = std::time::Instant::now(); + for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + // A transaction that reaches the deadline stops retrying and + // falls through to the give-up after the loop, which answers + // `TransactionCanceledException` with a `TransactionConflict` + // reason per item: the error the service answers for a + // transaction that loses to contention. Only the single-item + // writes answer the deadline with HTTP 503. + if contention_deadline_passed(attempt, contention_started) { + break; + } let idempotency = idem_owned.as_ref().map(|(a, t, f)| IdempotencyKey { account_id: a.as_str(), token: t.as_str(), @@ -3280,6 +3303,10 @@ impl MongoEngine { ) .build(); + // No contention deadline here. This loop runs inside the background + // GSI backfill job in `ttl_worker.rs`, not inside a request: no + // caller is waiting for a reply, and giving up on the clock would + // leave the index CREATING with its cursor where it stopped. for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { session .start_transaction() @@ -3578,6 +3605,34 @@ pub(crate) struct GsiBackfillProgress { /// nothing about either. const TRANSIENT_RETRY_ATTEMPTS: u32 = 1000; +/// Wall-clock deadline for the contention retry loop of one request. +/// +/// The attempt budget bounds the number of attempts, not the time a caller +/// waits, because an attempt's own round trip has no bound. The same 1,000 +/// attempts against a transaction another session holds open returned in 9.5 +/// to 12.7 s over six runs on a shared host, and under many writers on one +/// key the attempts queue behind each other: at 256 writers putting one key +/// of a GSI table, 89 of 5,120 calls were still waiting when the client's +/// 120 s read timeout fired, and the 5,120 calls measured p50 29 s and p99 +/// 120 s. The budget alone therefore let a request outlive every SDK's +/// default read timeout. +/// +/// 10 s sits above the 9.5 to 12.7 s a put spends losing all 1,000 attempts +/// and below botocore's 60 s default read timeout, so a caller gets an answer +/// to retry rather than a socket timeout. Which bound a write reaches first +/// depends on the host's load, and both answer the same HTTP 503, so the +/// caller's handling does not change with it. +/// +/// The deadline is read before an attempt starts, so a request answers within +/// the deadline plus one attempt. It governs the loops that serve a request: +/// put, delete, update and transact. The single-item writes answer it with +/// HTTP 503; a transaction answers it as it answers a spent attempt budget, +/// with `TransactionCanceledException`. The GSI backfill loop shares the attempt +/// budget and takes no deadline: it runs in the background worker in +/// `ttl_worker.rs`, where no caller is waiting for a reply and giving up +/// leaves the index CREATING. +const CONTENTION_DEADLINE: std::time::Duration = std::time::Duration::from_secs(10); + /// Maximum number of times to retry `commitTransaction` in place after an /// `UnknownTransactionCommitResult`. /// @@ -3745,6 +3800,31 @@ fn create_race_exhausted(attempt: u32) -> StorageError { )) } +/// Whether the contention retry loop of a request that entered it at +/// `started` must stop before running attempt number `attempt`. +/// +/// The first attempt of a request always runs, so a request never answers +/// 503 without having tried the write once. +fn contention_deadline_passed(attempt: u32, started: std::time::Instant) -> bool { + attempt > 0 && started.elapsed() >= CONTENTION_DEADLINE +} + +/// The error for a write that stopped retrying because `CONTENTION_DEADLINE` +/// passed while it was losing to concurrent writers. `operation` names the +/// API operation. +/// +/// Like the attempt-budget give-up of PutItem this is a +/// [`StorageError::Transient`], which the engine answers as HTTP 503 +/// `ServiceUnavailable`: the SDK retry modes retry a 503 on their own and the +/// retry succeeds once the contention has passed, whereas a 400 +/// `TransactionConflictException` is handed to the application. +fn contention_deadline_exhausted(operation: &str) -> StorageError { + StorageError::Transient(format!( + "{operation}: the write lost to concurrent writers for {} seconds; retry the request", + CONTENTION_DEADLINE.as_secs() + )) +} + /// Build a `mongodb::error::Error` carrying the given error labels. /// /// The driver's `Error::new` is crate private, but `Error::new` copies the @@ -4259,6 +4339,46 @@ mod tests { assert!(!is_transient_write_conflict(&e)); } + /// The first attempt of a request runs whatever the clock reads, and no + /// attempt starts once the deadline has passed. + #[test] + fn the_contention_deadline_stops_the_loop_after_the_first_attempt() { + let spent = std::time::Instant::now() + .checked_sub(CONTENTION_DEADLINE + std::time::Duration::from_secs(1)) + .expect("the process clock reaches one deadline back"); + assert!(!contention_deadline_passed(0, spent)); + assert!(contention_deadline_passed(1, spent)); + assert!(contention_deadline_passed( + TRANSIENT_RETRY_ATTEMPTS - 1, + spent + )); + let fresh = std::time::Instant::now(); + assert!(!contention_deadline_passed(1, fresh)); + assert!(!contention_deadline_passed( + TRANSIENT_RETRY_ATTEMPTS - 1, + fresh + )); + } + + /// The give-up on the deadline is the transient error the engine answers + /// as HTTP 503, and its message names the operation and the wait. + #[test] + fn the_deadline_give_up_is_transient_and_names_the_operation() { + for operation in ["PutItem", "DeleteItem", "UpdateItem"] { + match contention_deadline_exhausted(operation) { + StorageError::Transient(msg) => { + assert!(msg.starts_with(&format!("{operation}: ")), "{msg}"); + assert!( + msg.contains("lost to concurrent writers for 10 seconds"), + "{msg}" + ); + assert!(msg.contains("retry the request"), "{msg}"); + } + other => panic!("want Transient, got {other:?}"), + } + } + } + /// Build a write error with the given code, the form `insert_one` /// returns. fn write_error(code: i32) -> mongodb::error::Error { @@ -4656,7 +4776,8 @@ mod tests { /// the one the engine answers as HTTP 503, not with a transaction /// conflict. Another session writes the item inside a transaction it /// never commits, so each attempt of the put hits a write conflict until - /// the budget is spent; the held transaction is then aborted. + /// one of the two bounds is reached; the held transaction is then + /// aborted. #[tokio::test] async fn unconditional_put_that_loses_every_attempt_gives_up_with_a_transient_error() { let Some(uri) = live_mongodb_uri( @@ -4700,21 +4821,27 @@ mod tests { let _ = holder.abort_transaction().await; match res { - Err(StorageError::Transient(msg)) => assert!( - msg.contains(&format!("lost {TRANSIENT_RETRY_ATTEMPTS} attempts")), - "message: {msg}" - ), + Err(StorageError::Transient(msg)) => { + let on_the_deadline = msg.contains("lost to concurrent writers for 10 seconds"); + let on_the_attempts = + msg.contains(&format!("lost {TRANSIENT_RETRY_ATTEMPTS} attempts")); + assert!(on_the_deadline || on_the_attempts, "message: {msg}"); + println!("the put gave up with: {msg}"); + } other => panic!("want the transient exhausted-budget error, got: {other:?}"), } - // The sleeps alone come to about 6.4 s on average and 12.8 s at - // worst. Each attempt against a document a transaction holds also - // waits out MongoDB's lock timeout, so the wall time measured here - // ran 9.5 to 12.7 s on a shared host. The bound asserted is the - // one a caller - // cares about: the write answers inside a client's read timeout. + // Two bounds end this put, and both answer HTTP 503. The 1,000 + // attempts take 9.5 to 12.7 s against a document a transaction + // holds, measured over nine runs on a shared host, because each + // attempt waits out MongoDB's lock timeout on top of the backoff + // sleeps, which come to about 6.4 s on average. The deadline + // stops the loop at 10 s, so which bound the put reaches first + // depends on the host's load. The bound asserted here is the one + // a caller cares about: the deadline plus the one attempt that + // was already running. println!("the put lost every attempt and answered in {elapsed:?}"); assert!( - elapsed < std::time::Duration::from_secs(30), + elapsed < CONTENTION_DEADLINE + std::time::Duration::from_secs(5), "took {elapsed:?}" ); let stored = coll diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index 61f7fb77c..501aedb1a 100644 --- a/docs/rfcs/0000-mongodb-backend.md +++ b/docs/rfcs/0000-mongodb-backend.md @@ -136,7 +136,7 @@ Transaction implementation: `data_engine.rs` — `transact_write_items_impl`, `t ### Write conflict handling -**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place up to `COMMIT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs, each sleep drawn uniformly from 0 to the current base, capped at 12.8 ms) up to `TRANSIENT_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answers in about 10 s. An unconditional PutItem that keeps losing the create race has its own budget, `CREATE_RACE_ATTEMPTS` (50). Exhausted retries on PutItem return a `StorageError::Transient`, answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on DeleteItem and UpdateItem they return `TransactionConflict`; on `TransactWriteItems`, they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. +**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place up to `COMMIT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs, each sleep drawn uniformly from 0 to the current base, capped at 12.8 ms) up to `TRANSIENT_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race has its own budget, `CREATE_RACE_ATTEMPTS` (50). A single-item write that reaches the deadline returns the `StorageError::Transient` built by `contention_deadline_exhausted`, answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; so does PutItem when its attempt budget runs out. A `TransactWriteItems` that reaches the deadline answers as it does when its attempts run out. DeleteItem and UpdateItem that run out of attempts return `TransactionConflict`; `TransactWriteItems` that runs out of attempts surfaces a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. **UpdateItem** additionally uses an optimistic-concurrency version guard on top of the transaction. A `_v` counter is stored on each document. The write path reads the current `_v` under the transaction snapshot, applies the update expression in memory, sets `_v = current_version + 1`, then executes `replace_one` filtered on both the primary key AND the expected `_v`. If `matched_count == 0`, a concurrent writer committed a higher version between the snapshot read and the replace; the attempt aborts and the outer retry loop re-reads. The version guard doubles up when a native-fast-path update (unconditional, no stream, no GSI) is possible — that path uses a single `find_one_and_update` with `$inc: {_v: 1}` outside a transaction, and always bumps the counter so a concurrent session-scoped update against a stale snapshot fails its versioned filter and retries. From a15e4011c1d262d507b0174341fe6e8e27052c07 Mon Sep 17 00:00:00 2001 From: Lee Hannigan Date: Sat, 24 Oct 2026 03:40:30 +0000 Subject: [PATCH 06/44] fix(mongodb): make the contention deadline a value a test can set The deadline and the 1,000-attempt budget take about the same time against a contended key, so a test could not say which bound ended a write. The engine now carries the deadline, set to CONTENTION_DEADLINE in every production construction, and a storage test sets it: 2 s makes the deadline the bound, 600 s makes the attempt budget the bound. Three paths that had no test now have one. A contended PutItem, DeleteItem and UpdateItem that reach the deadline each answer the transient error the engine answers as HTTP 503, with the operation and the deadline in the message. A TransactWriteItems that reaches the deadline answers a canceled transaction with a TransactionConflict reason per operation, which is what the service answers a transaction that loses to contention. The test named for the attempt budget reaches the attempt budget again and asserts the 1,000-attempt message rather than either message. Each test is bounded to the deadline it sets, so a loop that stopped reading the deadline fails it. The doc comment on the deadline said both bounds answer the same HTTP 503, which holds for PutItem and TransactWriteItems only. On this branch DeleteItem and UpdateItem answer 503 on the deadline and 400 TransactionConflictException when their attempt budget runs out first, measured in 2 of 4 rounds against a document a transaction held. The comment and the RFC paragraph now say that, and name the change that gives all three single-item writes one answer. --- .../storage-mongodb/src/backup_live_tests.rs | 1 + crates/storage-mongodb/src/data_engine.rs | 420 +++++++++++++++--- crates/storage-mongodb/src/lib.rs | 7 + docs/rfcs/0000-mongodb-backend.md | 2 +- 4 files changed, 370 insertions(+), 60 deletions(-) diff --git a/crates/storage-mongodb/src/backup_live_tests.rs b/crates/storage-mongodb/src/backup_live_tests.rs index 75ed23761..5bfdacdbb 100644 --- a/crates/storage-mongodb/src/backup_live_tests.rs +++ b/crates/storage-mongodb/src/backup_live_tests.rs @@ -116,6 +116,7 @@ async fn engine_on(catalog: &str, data: &str, liveness: BackupLiveness) -> Mongo restore_server_slot_count: slots, restore_server_slots: Arc::new(tokio::sync::Semaphore::new(slots)), restore_account_slots: super::RestoreAccountSlots::default(), + contention_deadline: crate::data_engine::CONTENTION_DEADLINE, } } diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 3497eca00..396764b14 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -471,8 +471,11 @@ impl MongoEngine { let contention_started = std::time::Instant::now(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { - if contention_deadline_passed(attempt, contention_started) { - return Err(contention_deadline_exhausted("PutItem")); + if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { + return Err(contention_deadline_exhausted( + "PutItem", + self.contention_deadline, + )); } let new_doc = item_to_document(&item, &key_info.key_schema, &key_info.attribute_definitions)?; @@ -718,8 +721,11 @@ impl MongoEngine { let contention_started = std::time::Instant::now(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { - if contention_deadline_passed(attempt, contention_started) { - return Err(contention_deadline_exhausted("DeleteItem")); + if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { + return Err(contention_deadline_exhausted( + "DeleteItem", + self.contention_deadline, + )); } // Capture before the transaction's first database operation so a // concurrent catalog mutation cannot be hidden by the snapshot. @@ -947,8 +953,11 @@ impl MongoEngine { let contention_started = std::time::Instant::now(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { - if contention_deadline_passed(attempt, contention_started) { - return Err(contention_deadline_exhausted("UpdateItem")); + if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { + return Err(contention_deadline_exhausted( + "UpdateItem", + self.contention_deadline, + )); } // Capture before the transaction's first database operation. A // later UpdateTable must invalidate this observation rather than @@ -2273,7 +2282,7 @@ impl MongoEngine { // reason per item: the error the service answers for a // transaction that loses to contention. Only the single-item // writes answer the deadline with HTTP 503. - if contention_deadline_passed(attempt, contention_started) { + if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { break; } let idempotency = idem_owned.as_ref().map(|(a, t, f)| IdempotencyKey { @@ -3620,8 +3629,16 @@ const TRANSIENT_RETRY_ATTEMPTS: u32 = 1000; /// 10 s sits above the 9.5 to 12.7 s a put spends losing all 1,000 attempts /// and below botocore's 60 s default read timeout, so a caller gets an answer /// to retry rather than a socket timeout. Which bound a write reaches first -/// depends on the host's load, and both answer the same HTTP 503, so the -/// caller's handling does not change with it. +/// depends on the host's load, and which error class the caller is told +/// depends on that for two of the four operations: PutItem answers HTTP 503 +/// on either bound, and `TransactWriteItems` answers +/// `TransactionCanceledException` on either bound, but DeleteItem and +/// UpdateItem answer HTTP 503 on the deadline and 400 +/// `TransactionConflictException` when their attempt budget runs out first +/// (measured in 2 of 4 rounds against a held document, where the 1,000 +/// attempts and the deadline both land near 10 s). The 400 of those two +/// paths is what #437 replaces, after which every single-item write answers +/// 503 whichever bound it reaches. /// /// The deadline is read before an attempt starts, so a request answers within /// the deadline plus one attempt. It governs the loops that serve a request: @@ -3631,7 +3648,14 @@ const TRANSIENT_RETRY_ATTEMPTS: u32 = 1000; /// budget and takes no deadline: it runs in the background worker in /// `ttl_worker.rs`, where no caller is waiting for a reply and giving up /// leaves the index CREATING. -const CONTENTION_DEADLINE: std::time::Duration = std::time::Duration::from_secs(10); +/// +/// The engine carries the deadline in +/// [`MongoEngine::contention_deadline`](crate::MongoEngine) so that a test can +/// set it: a deadline shorter than the attempt budget's run time reaches the +/// deadline first, a longer one reaches the attempt budget first, and neither +/// test has to race 1,000 attempts against a wall clock. Every production +/// construction uses this value. +pub(crate) const CONTENTION_DEADLINE: std::time::Duration = std::time::Duration::from_secs(10); /// Maximum number of times to retry `commitTransaction` in place after an /// `UnknownTransactionCommitResult`. @@ -3801,27 +3825,33 @@ fn create_race_exhausted(attempt: u32) -> StorageError { } /// Whether the contention retry loop of a request that entered it at -/// `started` must stop before running attempt number `attempt`. +/// `started` must stop before running attempt number `attempt`, given the +/// `deadline` the engine carries. /// /// The first attempt of a request always runs, so a request never answers /// 503 without having tried the write once. -fn contention_deadline_passed(attempt: u32, started: std::time::Instant) -> bool { - attempt > 0 && started.elapsed() >= CONTENTION_DEADLINE +fn contention_deadline_passed( + attempt: u32, + started: std::time::Instant, + deadline: std::time::Duration, +) -> bool { + attempt > 0 && started.elapsed() >= deadline } -/// The error for a write that stopped retrying because `CONTENTION_DEADLINE` -/// passed while it was losing to concurrent writers. `operation` names the -/// API operation. +/// The error for a write that stopped retrying because the engine's +/// contention deadline passed while it was losing to concurrent writers. +/// `operation` names the API operation and `deadline` is the deadline that +/// passed. /// /// Like the attempt-budget give-up of PutItem this is a /// [`StorageError::Transient`], which the engine answers as HTTP 503 /// `ServiceUnavailable`: the SDK retry modes retry a 503 on their own and the /// retry succeeds once the contention has passed, whereas a 400 /// `TransactionConflictException` is handed to the application. -fn contention_deadline_exhausted(operation: &str) -> StorageError { +fn contention_deadline_exhausted(operation: &str, deadline: std::time::Duration) -> StorageError { StorageError::Transient(format!( "{operation}: the write lost to concurrent writers for {} seconds; retry the request", - CONTENTION_DEADLINE.as_secs() + deadline.as_secs() )) } @@ -4346,17 +4376,32 @@ mod tests { let spent = std::time::Instant::now() .checked_sub(CONTENTION_DEADLINE + std::time::Duration::from_secs(1)) .expect("the process clock reaches one deadline back"); - assert!(!contention_deadline_passed(0, spent)); - assert!(contention_deadline_passed(1, spent)); + assert!(!contention_deadline_passed(0, spent, CONTENTION_DEADLINE)); + assert!(contention_deadline_passed(1, spent, CONTENTION_DEADLINE)); assert!(contention_deadline_passed( TRANSIENT_RETRY_ATTEMPTS - 1, - spent + spent, + CONTENTION_DEADLINE )); let fresh = std::time::Instant::now(); - assert!(!contention_deadline_passed(1, fresh)); + assert!(!contention_deadline_passed(1, fresh, CONTENTION_DEADLINE)); assert!(!contention_deadline_passed( TRANSIENT_RETRY_ATTEMPTS - 1, - fresh + fresh, + CONTENTION_DEADLINE + )); + // A deadline a test sets decides the answer, so the same elapsed + // time stops a short-deadline loop and lets a long-deadline loop + // keep its attempts. + assert!(contention_deadline_passed( + 1, + spent, + std::time::Duration::from_secs(1) + )); + assert!(!contention_deadline_passed( + 1, + spent, + CONTENTION_DEADLINE * 60 )); } @@ -4365,7 +4410,7 @@ mod tests { #[test] fn the_deadline_give_up_is_transient_and_names_the_operation() { for operation in ["PutItem", "DeleteItem", "UpdateItem"] { - match contention_deadline_exhausted(operation) { + match contention_deadline_exhausted(operation, CONTENTION_DEADLINE) { StorageError::Transient(msg) => { assert!(msg.starts_with(&format!("{operation}: ")), "{msg}"); assert!( @@ -4377,6 +4422,14 @@ mod tests { other => panic!("want Transient, got {other:?}"), } } + // The message carries the deadline that passed, not the default. + match contention_deadline_exhausted("PutItem", std::time::Duration::from_secs(2)) { + StorageError::Transient(msg) => assert!( + msg.contains("lost to concurrent writers for 2 seconds"), + "{msg}" + ), + other => panic!("want Transient, got {other:?}"), + } } /// Build a write error with the given code, the form `insert_one` @@ -4441,6 +4494,21 @@ mod tests { where F: FnOnce(MongoEngine, mongodb::Database) -> Fut, Fut: std::future::Future, + { + with_scratch_engine_deadline(uri, CONTENTION_DEADLINE, body).await; + } + + /// `with_scratch_engine` with the engine's contention deadline set, so a + /// test can decide which bound a contended write reaches: a deadline + /// below the time the attempt budget takes makes the deadline the bound, + /// one far above it makes the attempt budget the bound. + async fn with_scratch_engine_deadline( + uri: &str, + contention_deadline: std::time::Duration, + body: F, + ) where + F: FnOnce(MongoEngine, mongodb::Database) -> Fut, + Fut: std::future::Future, { use futures::FutureExt as _; let client = mongodb::Client::with_uri_str(uri) @@ -4464,6 +4532,7 @@ mod tests { restore_server_slot_count: slots, restore_server_slots: std::sync::Arc::new(tokio::sync::Semaphore::new(slots)), restore_account_slots: crate::backup_engine::RestoreAccountSlots::default(), + contention_deadline, }; let outcome = std::panic::AssertUnwindSafe(body(engine, data_db.clone())) .catch_unwind() @@ -4778,33 +4847,268 @@ mod tests { /// never commits, so each attempt of the put hits a write conflict until /// one of the two bounds is reached; the held transaction is then /// aborted. + /// The key of `item_with(pk, ...)` on a hash-key table. + fn hash_key(pk: &str) -> Item { + let mut key = Item::new(); + key.insert("pk".to_owned(), AttributeValue::S(pk.to_owned())); + key + } + + /// `SET w = :w` with its placeholder, for an UpdateItem under contention. + fn set_w(value: &str) -> (Vec, ExpressionMaps) { + let actions = vec![UpdateAction::Set { + path: vec![PathElement::Attribute("w".to_owned())], + value: Expr::Placeholder(":w".to_owned()), + }]; + let maps = ExpressionMaps::new( + std::collections::HashMap::new(), + std::collections::HashMap::from([( + ":w".to_owned(), + AttributeValue::S(value.to_owned()), + )]), + ); + (actions, maps) + } + + /// Hold `pk` of `table_id` inside a transaction that is never committed, + /// so every attempt of a contended write on that key conflicts. The + /// caller aborts the returned session when it is done. + async fn hold_item_in_a_transaction( + engine: &MongoEngine, + data_db: &mongodb::Database, + key_info: &TableKeyInfo, + pk: &str, + ) -> mongodb::ClientSession { + let coll = data_db.collection::(&data_collection_name(&key_info.table_id)); + coll.insert_one(stored_document(&item_with(pk, "held"), key_info)) + .await + .expect("insert the item to hold"); + let mut holder = engine.client.start_session().await.expect("session"); + holder + .start_transaction() + .with_options(engine.write_transaction_options()) + .await + .expect("start the holding transaction"); + coll.replace_one( + doc! { "pk": pk }, + stored_document(&item_with(pk, "rewritten"), key_info), + ) + .session(&mut holder) + .await + .expect("write the item inside the held transaction"); + holder + } + + /// Each single-item write that reaches the engine's contention deadline + /// gives up with the transient error the engine answers as HTTP 503 + /// `ServiceUnavailable`, and the message names the operation and the + /// deadline that passed. + /// + /// The deadline is 2 s here, below the 9.5 to 12.7 s the 1,000 attempts + /// take against a document a transaction holds, so the deadline is the + /// bound each of the three writes reaches, with no race between the two. + /// The attempt budget is the bound in + /// `unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error`. #[tokio::test] - async fn unconditional_put_that_loses_every_attempt_gives_up_with_a_transient_error() { + async fn a_single_item_write_that_reaches_the_deadline_answers_a_transient_error() { let Some(uri) = live_mongodb_uri( - "unconditional_put_that_loses_every_attempt_gives_up_with_a_transient_error", + "a_single_item_write_that_reaches_the_deadline_answers_a_transient_error", ) else { return; }; - with_scratch_engine(&uri, |engine, data_db| async move { + let deadline = std::time::Duration::from_secs(2); + with_scratch_engine_deadline(&uri, deadline, |engine, data_db| async move { let maps = ExpressionMaps::default(); - let key_info = hash_key_table(&engine, "held-key", true); - let coll = data_db.collection::(&data_collection_name("held-key")); - let held = stored_document(&item_with("k1", "held"), &key_info); - coll.insert_one(held.clone()) - .await - .expect("insert the item"); + let key_info = hash_key_table(&engine, "deadline-key", true); + let coll = data_db.collection::(&data_collection_name("deadline-key")); + let mut holder = hold_item_in_a_transaction(&engine, &data_db, &key_info, "k1").await; + let key = hash_key("k1"); + let (actions, update_maps) = set_w("this-update"); + + for operation in ["PutItem", "DeleteItem", "UpdateItem"] { + let started = std::time::Instant::now(); + let res = match operation { + "PutItem" => engine + .put_item_impl( + &key_info, + item_with("k1", "this-put"), + false, + None, + &maps, + None, + ) + .await + .map(|_| ()), + "DeleteItem" => engine + .delete_item_impl(&key_info, &key, false, None, &maps, None) + .await + .map(|_| ()), + _ => engine + .update_item_impl( + &key_info, + &key, + &actions, + false, + false, + None, + &update_maps, + None, + ) + .await + .map(|_| ()), + }; + let elapsed = started.elapsed(); + match res { + Err(StorageError::Transient(msg)) => { + assert_eq!( + msg, + format!( + "{operation}: the write lost to concurrent writers for 2 \ + seconds; retry the request" + ) + ); + println!("{operation} gave up on the deadline in {elapsed:?}: {msg}"); + } + other => { + panic!("{operation}: want the transient deadline error, got: {other:?}") + } + } + assert!( + elapsed >= deadline, + "{operation} answered in {elapsed:?}, before the deadline" + ); + assert!( + elapsed < deadline + std::time::Duration::from_secs(5), + "{operation} took {elapsed:?}, so a bound other than the deadline ended it" + ); + } - let mut holder = engine.client.start_session().await.expect("session"); - holder - .start_transaction() - .with_options(engine.write_transaction_options()) + let _ = holder.abort_transaction().await; + // No give-up wrote anything: the held item is as it was inserted. + let stored = coll + .find_one(doc! { "pk": "k1" }) .await - .expect("start the holding transaction"); - let rewritten = stored_document(&item_with("k1", "rewritten"), &key_info); - coll.replace_one(doc! { "pk": "k1" }, rewritten) - .session(&mut holder) + .expect("read") + .expect("item"); + assert_eq!( + document_to_item(&stored).expect("item"), + item_with("k1", "held") + ); + }) + .await; + } + + /// A `TransactWriteItems` that reaches the deadline answers a canceled + /// transaction with a `TransactionConflict` reason per item, which is + /// what live DynamoDB answers a transaction that loses to contention + /// (107 of 160 calls at 16 writers on one key, probes/R10-1.out). It does + /// not answer the HTTP 503 the single-item writes answer. + #[tokio::test] + async fn a_transaction_that_reaches_the_deadline_answers_a_canceled_transaction() { + let Some(uri) = live_mongodb_uri( + "a_transaction_that_reaches_the_deadline_answers_a_canceled_transaction", + ) else { + return; + }; + let deadline = std::time::Duration::from_secs(2); + with_scratch_engine_deadline(&uri, deadline, |engine, data_db| async move { + let key_info = hash_key_table(&engine, "deadline-transact", true); + let coll = data_db.collection::(&data_collection_name("deadline-transact")); + let mut holder = hold_item_in_a_transaction(&engine, &data_db, &key_info, "k1").await; + + // Two operations, one on the held key and one on a free key, so + // the transaction conflicts and the answer carries one reason + // per operation rather than one for the contended item. + let ops = vec![ + OwnedTransactWriteOp::Put { + key_info: key_info.clone(), + item: item_with("k1", "this-transaction"), + condition: None, + maps: ExpressionMaps::default(), + return_values_on_ccf: ReturnValuesOnConditionCheckFailure::None, + stream: None, + }, + OwnedTransactWriteOp::Put { + key_info: key_info.clone(), + item: item_with("k2", "this-transaction"), + condition: None, + maps: ExpressionMaps::default(), + return_values_on_ccf: ReturnValuesOnConditionCheckFailure::None, + stream: None, + }, + ]; + + let started = std::time::Instant::now(); + let res = engine.transact_write_items_impl(&ops, None).await; + let elapsed = started.elapsed(); + let _ = holder.abort_transaction().await; + + match res { + Err(StorageError::TransactionCanceled(reasons)) => { + assert_eq!(reasons.len(), ops.len(), "one reason per operation"); + for reason in &reasons { + assert_eq!(reason.code, "TransactionConflict"); + assert_eq!( + reason.message.as_deref(), + Some("Transaction is ongoing for the item") + ); + } + println!("the transaction was canceled in {elapsed:?}: {reasons:?}"); + } + other => panic!("want a canceled transaction, got: {other:?}"), + } + assert!( + elapsed >= deadline, + "the transaction answered in {elapsed:?}, before the deadline" + ); + assert!( + elapsed < deadline + std::time::Duration::from_secs(5), + "took {elapsed:?}, so a bound other than the deadline ended it" + ); + + // Neither operation of the canceled transaction was applied. + let stored = coll + .find_one(doc! { "pk": "k1" }) .await - .expect("write the item inside the held transaction"); + .expect("read") + .expect("item"); + assert_eq!( + document_to_item(&stored).expect("item"), + item_with("k1", "held") + ); + assert!( + coll.find_one(doc! { "pk": "k2" }) + .await + .expect("read") + .is_none(), + "the free key of the canceled transaction must not be written" + ); + }) + .await; + } + + /// An unconditional PutItem that spends its whole attempt budget on + /// contention gives up with the transient error naming the attempts, + /// which the engine answers as HTTP 503. + /// + /// The deadline is set far above the time the budget takes (9.5 to 12.7 s + /// against a document a transaction holds, over nine runs on a shared + /// host), so the attempt budget is the bound this test reaches. With the + /// production deadline of 10 s either bound can fire first, which is why + /// the deadline is the test's input rather than its race. + #[tokio::test] + async fn unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error() { + let Some(uri) = live_mongodb_uri( + "unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error", + ) else { + return; + }; + let deadline = std::time::Duration::from_secs(600); + with_scratch_engine_deadline(&uri, deadline, |engine, data_db| async move { + let maps = ExpressionMaps::default(); + let key_info = hash_key_table(&engine, "held-key", true); + let coll = data_db.collection::(&data_collection_name("held-key")); + let mut holder = hold_item_in_a_transaction(&engine, &data_db, &key_info, "k1").await; let started = std::time::Instant::now(); let res = engine @@ -4822,26 +5126,23 @@ mod tests { match res { Err(StorageError::Transient(msg)) => { - let on_the_deadline = msg.contains("lost to concurrent writers for 10 seconds"); - let on_the_attempts = - msg.contains(&format!("lost {TRANSIENT_RETRY_ATTEMPTS} attempts")); - assert!(on_the_deadline || on_the_attempts, "message: {msg}"); - println!("the put gave up with: {msg}"); + assert_eq!( + msg, + format!( + "PutItem: the write lost {TRANSIENT_RETRY_ATTEMPTS} attempts to \ + concurrent writers; retry the request" + ) + ); + println!("the put spent its attempt budget in {elapsed:?}: {msg}"); } - other => panic!("want the transient exhausted-budget error, got: {other:?}"), + other => panic!("want the transient spent-budget error, got: {other:?}"), } - // Two bounds end this put, and both answer HTTP 503. The 1,000 - // attempts take 9.5 to 12.7 s against a document a transaction - // holds, measured over nine runs on a shared host, because each - // attempt waits out MongoDB's lock timeout on top of the backoff - // sleeps, which come to about 6.4 s on average. The deadline - // stops the loop at 10 s, so which bound the put reaches first - // depends on the host's load. The bound asserted here is the one - // a caller cares about: the deadline plus the one attempt that - // was already running. - println!("the put lost every attempt and answered in {elapsed:?}"); + // The 1,000 attempts take 9.5 to 12.7 s on a shared host, and + // the sleeps alone come to 12.8 s at worst, so the bound here is + // loose: what it rules out is a budget that no longer ends the + // loop at all. assert!( - elapsed < CONTENTION_DEADLINE + std::time::Duration::from_secs(5), + elapsed < std::time::Duration::from_secs(120), "took {elapsed:?}" ); let stored = coll @@ -4903,6 +5204,7 @@ mod tests { restore_server_slot_count: 1, restore_server_slots: std::sync::Arc::new(tokio::sync::Semaphore::new(1)), restore_account_slots: Default::default(), + contention_deadline: CONTENTION_DEADLINE, }; let base_coll = engine.data_db.collection::("base"); let key_schema = vec![KeySchemaElement { diff --git a/crates/storage-mongodb/src/lib.rs b/crates/storage-mongodb/src/lib.rs index c1d28882f..df55af9cb 100644 --- a/crates/storage-mongodb/src/lib.rs +++ b/crates/storage-mongodb/src/lib.rs @@ -365,6 +365,12 @@ pub struct MongoEngine { /// Restores each account may run at once in this process; see /// [`backup_engine::RestoreAccountSlots`]. pub(crate) restore_account_slots: backup_engine::RestoreAccountSlots, + /// Wall-clock deadline for the contention retry loop of one request; see + /// [`crate::data_engine::CONTENTION_DEADLINE`], which every production + /// construction uses. A test sets it short to make the deadline the bound + /// a contended write reaches, or long to make the attempt budget the + /// bound, instead of racing the two. + pub(crate) contention_deadline: std::time::Duration, } /// Build a MongoDB client from a connection string, applying the shared @@ -574,6 +580,7 @@ impl MongoEngine { restore_server_slot_count, restore_server_slots: Arc::new(tokio::sync::Semaphore::new(restore_server_slot_count)), restore_account_slots: backup_engine::RestoreAccountSlots::default(), + contention_deadline: data_engine::CONTENTION_DEADLINE, }) } diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index 501aedb1a..13c46ed72 100644 --- a/docs/rfcs/0000-mongodb-backend.md +++ b/docs/rfcs/0000-mongodb-backend.md @@ -136,7 +136,7 @@ Transaction implementation: `data_engine.rs` — `transact_write_items_impl`, `t ### Write conflict handling -**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place up to `COMMIT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs, each sleep drawn uniformly from 0 to the current base, capped at 12.8 ms) up to `TRANSIENT_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race has its own budget, `CREATE_RACE_ATTEMPTS` (50). A single-item write that reaches the deadline returns the `StorageError::Transient` built by `contention_deadline_exhausted`, answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; so does PutItem when its attempt budget runs out. A `TransactWriteItems` that reaches the deadline answers as it does when its attempts run out. DeleteItem and UpdateItem that run out of attempts return `TransactionConflict`; `TransactWriteItems` that runs out of attempts surfaces a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. +**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place up to `COMMIT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs, each sleep drawn uniformly from 0 to the current base, capped at 12.8 ms) up to `TRANSIENT_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race has its own budget, `CREATE_RACE_ATTEMPTS` (50). A single-item write that reaches the deadline returns the `StorageError::Transient` built by `contention_deadline_exhausted`, answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; so does PutItem when its attempt budget runs out. A `TransactWriteItems` that reaches the deadline answers as it does when its attempts run out. DeleteItem and UpdateItem that run out of attempts return `TransactionConflict`; `TransactWriteItems` that runs out of attempts surfaces a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. The two bounds take about the same time on a contended key, so which error class DeleteItem and UpdateItem answer depends on which bound their loop reaches first: 503 on the deadline and 400 `TransactionConflictException` on the spent budget, with a delete against a held document answering 400 in 2 of 4 measured rounds. #437 routes those two exhaustions through the same transient give-up, after which every single-item write answers 503 whichever bound it reaches. PutItem and `TransactWriteItems` already answer one error class on either bound. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. **UpdateItem** additionally uses an optimistic-concurrency version guard on top of the transaction. A `_v` counter is stored on each document. The write path reads the current `_v` under the transaction snapshot, applies the update expression in memory, sets `_v = current_version + 1`, then executes `replace_one` filtered on both the primary key AND the expected `_v`. If `matched_count == 0`, a concurrent writer committed a higher version between the snapshot read and the replace; the attempt aborts and the outer retry loop re-reads. The version guard doubles up when a native-fast-path update (unconditional, no stream, no GSI) is possible — that path uses a single `find_one_and_update` with `$inc: {_v: 1}` outside a transaction, and always bumps the counter so a concurrent session-scoped update against a stale snapshot fails its versioned filter and retries. From f7d91149f58e31168d17ace3dab1dce3b00dfecd Mon Sep 17 00:00:00 2001 From: Lee Hannigan Date: Sat, 24 Oct 2026 03:40:30 +0000 Subject: [PATCH 07/44] test(contention): hold the contended puts to the contract the backend has tests/test_put_item_gsi_contention.py asserted that all 2,016 puts of 32 writers on one key answer 200. The retry deadline this PR adds makes that false under load: a put that has been losing for ten seconds is shed with 503 ServiceUnavailable. The file failed 1 of 4 runs in the shape CI uses, two xdist workers on one server, and failed with 240 of 2,016 shed when another hot-key load shared the server. The test now asserts the contract the retry loop gives. Every call answers 200 or 503 ServiceUnavailable, never 400 TransactionConflictException and never 500; the stored item is one writer's item, whole, carrying the gk that writer sent with that w; and the index holds exactly one entry for it. The number shed depends on the load the server is under, so the test reports it instead of asserting it. The docstring keeps what the service answered to this shape, 200 to every call, and says where ExtendDB diverges. docs/differences-from-dynamodb.md gets a row for that divergence under Capacity and Throttling, with the measurements at 64 and 256 writers and what each operation answers at each bound. --- docs/differences-from-dynamodb.md | 1 + tests/test_put_item_gsi_contention.py | 33 +++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index 2d6469b7e..d73e75e5a 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -146,6 +146,7 @@ The same on all four backends. | Provisioned throughput | Token bucket per table/partition | Token bucket per table/partition, matching DynamoDB's burst and refill behavior | | On-demand capacity | Automatic scaling | Fixed initial burst capacity (4000 WCU / 12000 RCU), no auto-scaling | | Throttling | Always on; throttles requests that exceed provisioned/burst capacity. No setting to disable | Configurable via `throttling_enabled` runtime setting (default: `true`) | +| Sustained contention on one item (MongoDB backend) | Applies concurrent single-item writes on one key one after another and answers 200 to each. Measured to every call at 32 and 64 writers putting one key of a table with a GSI. A `PutItem` is answered 400 `TransactionConflictException` only while an in-flight `TransactWriteItems` holds the item (33 of 80 calls measured), and a `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | A write on a table with a secondary index writes the item and its index entries in one transaction, so concurrent writes on one key abort each other and are retried. One request's retry loop stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key: `PutItem` answers 503 `ServiceUnavailable` on either bound, which the SDK retry modes retry and which succeeds once the contention has passed. `DeleteItem` and `UpdateItem` answer 503 on the deadline and 400 `TransactionConflictException` when their 1,000 attempts run out first. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. Measured with one GSI table and all writers on one key: at 64 writers all 1,280 puts committed and none were shed, with the longest call 9.7 s; at 256 writers 1,262 of 5,120 committed and 3,858 were shed, with the longest call 11.7 s. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | ## Operations Not Implemented diff --git a/tests/test_put_item_gsi_contention.py b/tests/test_put_item_gsi_contention.py index 360c7d439..14707d217 100644 --- a/tests/test_put_item_gsi_contention.py +++ b/tests/test_put_item_gsi_contention.py @@ -5,10 +5,19 @@ Amazon DynamoDB applies the puts one after another and answers 200 to each one; the last writer wins, and the index holds the final item's entry only. +That is what the service answered to every call of this shape, up to 64 +writers on one key. + On the MongoDB backend a put on a table with an index writes the item and its index entries in one transaction, so concurrent puts on one key abort each -other and are retried. These tests fail on a server whose retry budget runs -out under this contention and reports the lost race to the client. +other and are retried. The retry loop stops after ten seconds, so under load +ExtendDB may answer 503 ServiceUnavailable where the service answers 200, and +a client with the SDK default retry mode retries it. What this test holds the +backend to is the contract the retry loop gives: every call answers 200 or 503 +ServiceUnavailable, never 400 TransactionConflictException and never 500; the +stored item is one writer's item, whole; and the index holds exactly one entry +for it. These tests fail on a server that reports a lost race to the client as +a 400 or a 500. """ from __future__ import annotations @@ -27,6 +36,9 @@ WRITERS = 32 PUTS_PER_WRITER = 63 +# The only failure a contended put may answer. The server sheds a put that +# has been losing to other writers for CONTENTION_DEADLINE (10 s). +SHED = "503 ServiceUnavailable" @pytest.fixture(scope="module") @@ -108,16 +120,29 @@ def _gsi_entries(client, table: str, pk: str) -> list[str]: kwargs["ExclusiveStartKey"] = resp["LastEvaluatedKey"] -def test_unconditional_puts_on_one_key_of_a_gsi_table_all_succeed( +def test_unconditional_puts_on_one_key_of_a_gsi_table_commit_or_are_shed( dynamodb_client, raw_client, gsi_table ): pk = f"hot-{uuid.uuid4().hex[:8]}" failures = _hammer(raw_client, gsi_table, pk) total = WRITERS * PUTS_PER_WRITER - assert not failures, f"{len(failures)} of {total} puts failed: {sorted(set(failures))}" + # How many of the contended puts were shed. The count depends on the load + # the server is under, so it is reported rather than asserted; what is + # asserted is that a shed put is the only failure a caller can get. + print(f"{len(failures)} of {total} puts were shed, {total - len(failures)} committed") + assert set(failures) <= {SHED}, ( + f"{len(failures)} of {total} puts failed with something other than a shed put: " + f"{sorted(set(failures))}" + ) final = dynamodb_client.get_item( TableName=gsi_table, Key={"pk": {"S": pk}}, ConsistentRead=True )["Item"] + # The stored item is one writer's item, whole: its `gk` is the one that + # writer sent with that `w`, not another put's. + writer, _, nth = final["w"]["S"][1:].partition("-") + assert 0 <= int(writer) < WRITERS, final + assert 0 <= int(nth) < PUTS_PER_WRITER, final + assert final["gk"]["S"] == f"g-{pk}-{writer}-{nth}", final # The GSI is eventually consistent on Amazon DynamoDB, so poll. want = [final["gk"]["S"]] deadline = time.monotonic() + 30 From 09d6bf94473353fa87e20a9a9718a28ca504d62b Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 10:38:47 +0000 Subject: [PATCH 08/44] test(mongodb): run the create-race tests as live data engine tests Main added a live test module for the data engine in #473: its tests are #[ignore]d, read EXTENDDB_TEST_MONGODB_URI through one uri() helper, fail rather than skip when a run passes --ignored without it, and run in the mongodb-rust job through devtools/run-mongodb-tests --rust-live. Its scratch_engine helper builds the engine with MongoEngine::new and struct update syntax, so a field added to MongoEngine needs no change there. The two create-race storage tests move into that module as data_engine_live_tests_put_race.rs. They used their own URI gate, which printed SKIP without the variable, and a with_scratch_engine helper that built MongoEngine from a struct literal. Against main that literal missed the four fields #384 added, and the test build failed with E0063. Both helpers are gone, and the tests check what they checked before. devtools/run-mongodb-tests exported EXTENDDB_TEST_MONGODB_URI a second time for these tests. Main exports it since #473, so the line is removed. --- crates/storage-mongodb/src/data_engine.rs | 253 ------------------ .../src/data_engine_live_tests.rs | 3 + .../src/data_engine_live_tests_put_race.rs | 190 +++++++++++++ devtools/run-mongodb-tests | 4 - 4 files changed, 193 insertions(+), 257 deletions(-) create mode 100644 crates/storage-mongodb/src/data_engine_live_tests_put_race.rs diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index a97ab89ed..2a5dc361b 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -4253,259 +4253,6 @@ mod tests { assert!(!is_transient_write_conflict(&command_error(11000))); } - /// Connection string of the MongoDB replica set for a test that needs a - /// server, or `None` after reporting that the test is skipped. - /// - /// `devtools/run-mongodb-tests` exports `EXTENDDB_TEST_MONGODB_URI` - /// together with `EXTENDDB_TEST_MONGODB_CONTAINER`. Without the URI the - /// test prints a SKIP line and returns, as the PostgreSQL storage crate's - /// live tests do. The line is written to the stderr handle, because the - /// test harness captures `eprintln!` output of a passing test. When the - /// container variable is set and the URI is not, the test is running - /// under that runner and the URI export is missing, so it fails instead - /// of skipping. - fn live_mongodb_uri(test: &str) -> Option { - let set = |name: &str| std::env::var(name).ok().filter(|v| !v.trim().is_empty()); - if let Some(uri) = set("EXTENDDB_TEST_MONGODB_URI") { - return Some(uri); - } - if let Some(container) = set("EXTENDDB_TEST_MONGODB_CONTAINER") { - panic!( - "{test}: EXTENDDB_TEST_MONGODB_CONTAINER is set ({container}) but \ - EXTENDDB_TEST_MONGODB_URI is not; devtools/run-mongodb-tests exports both" - ); - } - use std::io::Write as _; - let _ = writeln!( - std::io::stderr(), - "SKIP {test}: EXTENDDB_TEST_MONGODB_URI is not set, so there is no MongoDB replica \ - set to run against." - ); - None - } - - /// Run `body` with an engine on two new databases of the replica set at - /// `uri`, then drop both databases, also when `body` panics. - async fn with_scratch_engine(uri: &str, body: F) - where - F: FnOnce(MongoEngine, mongodb::Database) -> Fut, - Fut: std::future::Future, - { - use futures::FutureExt as _; - let client = mongodb::Client::with_uri_str(uri) - .await - .expect("EXTENDDB_TEST_MONGODB_URI must parse"); - let suffix = uuid::Uuid::new_v4().simple().to_string(); - let data_db = client.database(&format!("extenddb_test_{suffix}")); - let catalog_db = client.database(&format!("extenddb_test_{suffix}_catalog")); - let engine = MongoEngine { - client: client.clone(), - catalog_db: catalog_db.clone(), - data_db: data_db.clone(), - region: "us-east-1".to_owned(), - gsi_cache: Default::default(), - tx_read_concern: mongodb::options::ReadConcern::snapshot(), - }; - let outcome = std::panic::AssertUnwindSafe(body(engine, data_db.clone())) - .catch_unwind() - .await; - let _ = data_db.drop().await; - let _ = catalog_db.drop().await; - if let Err(panic) = outcome { - std::panic::resume_unwind(panic); - } - } - - /// Key info for a table with a string hash key `pk`. The GSI cache is set - /// so that an unconditional put takes the sessionless path (`has_gsi` - /// false) or the transaction path (`has_gsi` true). The catalog has no - /// index rows, so the transaction path writes no index entries. - fn hash_key_table(engine: &MongoEngine, table_id: &str, has_gsi: bool) -> TableKeyInfo { - engine.gsi_cache_set(table_id, has_gsi); - let key_schema = vec![KeySchemaElement { - attribute_name: "pk".to_owned(), - key_type: extenddb_core::types::KeyType::Hash, - }]; - TableKeyInfo { - table_id: table_id.to_owned(), - key_schema: key_schema.clone(), - base_key_schema: key_schema, - attribute_definitions: vec![extenddb_core::types::AttributeDefinition { - attribute_name: "pk".to_owned(), - attribute_type: ScalarAttributeType::S, - }], - ..Default::default() - } - } - - fn item_with(pk: &str, w: &str) -> Item { - let mut item = Item::new(); - item.insert("pk".to_owned(), AttributeValue::S(pk.to_owned())); - item.insert("w".to_owned(), AttributeValue::S(w.to_owned())); - item - } - - fn stored_document(item: &Item, key_info: &TableKeyInfo) -> Document { - item_to_document(item, &key_info.key_schema, &key_info.attribute_definitions) - .expect("test item converts to a document") - } - - /// An unconditional PutItem whose insert keeps colliding on `_id` retries - /// for the whole transient retry budget, then gives up with the create - /// race error, on the sessionless path and on the transaction path. - /// - /// A document that holds the item's `_id` under a different `pk` makes - /// every attempt collide: the put's filter on `pk` finds no item, and its - /// insert raises the same duplicate-key error a put gets when another - /// put created the item first. The obstructing document stays as it was. - #[tokio::test] - async fn unconditional_put_retries_a_lost_create_race_and_then_gives_up() { - let Some(uri) = - live_mongodb_uri("unconditional_put_retries_a_lost_create_race_and_then_gives_up") - else { - return; - }; - with_scratch_engine(&uri, |engine, data_db| async move { - let maps = ExpressionMaps::default(); - for (table_id, has_gsi) in [("sessionless-put", false), ("transaction-put", true)] { - let key_info = hash_key_table(&engine, table_id, has_gsi); - let item = item_with("k1", "new"); - let id = stored_document(&item, &key_info) - .get("_id") - .cloned() - .expect("document has an _id"); - let coll = data_db.collection::(&data_collection_name(table_id)); - coll.insert_one(doc! { "_id": id.clone(), "pk": "another-key", "item_data": {} }) - .await - .expect("insert the obstructing document"); - - let res = engine - .put_item_impl(&key_info, item, true, None, &maps, None) - .await; - - let want = - format!("could not create the item after {TRANSIENT_RETRY_ATTEMPTS} attempts"); - match res { - Err(StorageError::Internal(msg)) => assert!( - msg.contains(&want), - "{table_id}: want the create race error, got: {msg}" - ), - other => panic!("{table_id}: want the create race error, got: {other:?}"), - } - assert_eq!( - coll.count_documents(doc! {}).await.expect("count"), - 1, - "{table_id}: the put wrote a document" - ); - let left = coll - .find_one(doc! { "_id": id }) - .await - .expect("read back") - .expect("obstructing document is still there"); - assert_eq!(left.get_str("pk").expect("pk"), "another-key", "{table_id}"); - } - }) - .await; - } - - /// What happens to the obstructing document while the put retries. - #[derive(Clone, Copy, Debug)] - enum Obstruction { - /// It is replaced, under the same `_id`, with the item of another put - /// that created the key. - BecomesTheOtherPutsItem, - /// It is deleted, as a DeleteItem removes the item another put - /// created. - IsDeleted, - } - - /// An unconditional PutItem that lost the create race succeeds on a later - /// attempt, on the sessionless path and on the transaction path. - /// - /// The put starts while a document holds the item's `_id` under another - /// `pk`, so its first attempts collide. 100 ms later another task either - /// replaces that document with the item of another put on the same key, - /// or deletes it. The put's attempts back off between collisions and - /// together take longer than 100 ms, so a later attempt either replaces - /// the other put's item and returns it as the old image, or inserts and - /// returns no old image. In both cases the stored document is this put's - /// item. - #[tokio::test] - async fn unconditional_put_that_lost_a_create_race_succeeds_on_a_later_attempt() { - let Some(uri) = live_mongodb_uri( - "unconditional_put_that_lost_a_create_race_succeeds_on_a_later_attempt", - ) else { - return; - }; - with_scratch_engine(&uri, |engine, data_db| async move { - let maps = ExpressionMaps::default(); - for (path, has_gsi) in [("sessionless", false), ("transaction", true)] { - for change in [Obstruction::BecomesTheOtherPutsItem, Obstruction::IsDeleted] { - let table_id = format!("{path}-{change:?}"); - let key_info = hash_key_table(&engine, &table_id, has_gsi); - let item = item_with("k1", "this-put"); - let other_put = item_with("k1", "other-put"); - let other_put_doc = stored_document(&other_put, &key_info); - let id = other_put_doc - .get("_id") - .cloned() - .expect("document has an _id"); - let coll = data_db.collection::(&data_collection_name(&table_id)); - coll.insert_one( - doc! { "_id": id.clone(), "pk": "another-key", "item_data": {} }, - ) - .await - .expect("insert the obstructing document"); - - let put = - engine.put_item_impl(&key_info, item.clone(), true, None, &maps, None); - let clear = async { - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - match change { - Obstruction::BecomesTheOtherPutsItem => { - coll.replace_one(doc! { "_id": id.clone() }, other_put_doc.clone()) - .await - .expect("replace the obstructing document"); - } - Obstruction::IsDeleted => { - coll.delete_one(doc! { "_id": id.clone() }) - .await - .expect("delete the obstructing document"); - } - } - }; - let (res, ()) = tokio::join!(put, clear); - - let old = match res { - Ok(old) => old, - Err(e) => panic!("{table_id}: want the put to succeed, got: {e:?}"), - }; - let want_old = match change { - Obstruction::BecomesTheOtherPutsItem => Some(other_put), - Obstruction::IsDeleted => None, - }; - assert_eq!(old, want_old, "{table_id}: old image"); - assert_eq!( - coll.count_documents(doc! {}).await.expect("count"), - 1, - "{table_id}: documents in the collection" - ); - let stored = coll - .find_one(doc! { "_id": id }) - .await - .expect("read back") - .expect("the put's document is stored"); - assert_eq!( - document_to_item(&stored).expect("stored document converts to an item"), - item, - "{table_id}: stored item" - ); - } - } - }) - .await; - } - #[derive(Default)] struct RecordingGsiIndexWriter { direct_writes: AtomicUsize, diff --git a/crates/storage-mongodb/src/data_engine_live_tests.rs b/crates/storage-mongodb/src/data_engine_live_tests.rs index 4ea858a58..e1761076f 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests.rs @@ -38,6 +38,9 @@ mod txn_create; #[path = "data_engine_live_tests_repair_pass.rs"] mod repair_pass; +#[path = "data_engine_live_tests_put_race.rs"] +mod put_race; + fn uri() -> String { std::env::var("EXTENDDB_TEST_MONGODB_URI").expect( "set EXTENDDB_TEST_MONGODB_URI to a MongoDB replica set, e.g. \ diff --git a/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs b/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs new file mode 100644 index 000000000..bc0c3eefb --- /dev/null +++ b/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs @@ -0,0 +1,190 @@ +// Copyright 2026 ExtendDB contributors +// SPDX-License-Identifier: Apache-2.0 + +//! Live MongoDB tests for an unconditional PutItem whose insert hits the +//! `_id_` unique index: the put's filter on the key finds no item, and +//! another document already holds the item's `_id`. On the sessionless path +//! that is how a put that loses the create race to another put fails. On the +//! transaction path a put that loses the race to another put gets a write +//! conflict instead, and the duplicate-key arm meets only a document that +//! holds the `_id` without matching the key filter, which is the document +//! these tests insert. +//! +//! The tests read the replica set URI from `EXTENDDB_TEST_MONGODB_URI` and are +//! `#[ignore]`d, like the tests in the parent module. + +use bson::{Bson, Document, doc}; +use extenddb_core::expression::ExpressionMaps; +use extenddb_core::types::{Item, TableKeyInfo}; +use extenddb_storage::error::StorageError; + +use super::super::TRANSIENT_RETRY_ATTEMPTS; +use super::{finish, item_key, s, scratch_engine, table, with}; +use crate::MongoEngine; +use crate::data::{data_collection_name, document_to_item, item_to_document}; + +/// Key info for a table with a string hash key `pk`. The GSI cache is set so +/// that an unconditional put takes the sessionless path (`has_gsi` false) or +/// the transaction path (`has_gsi` true). The catalog has no index rows, so +/// the transaction path writes no index entries. +fn hash_table(engine: &MongoEngine, table_id: &str, has_gsi: bool) -> TableKeyInfo { + let key_info = table(engine, table_id, false); + engine.gsi_cache_set(table_id, has_gsi); + key_info +} + +/// The item at the key `item_key` builds, with attribute `w` set to `w`. +fn item_with_w(w: &str) -> Item { + with(item_key(false), "w", s(w)) +} + +/// The document a put of `item` stores. +fn stored_document(item: &Item, key_info: &TableKeyInfo) -> Document { + item_to_document(item, &key_info.key_schema, &key_info.attribute_definitions) + .expect("test item converts to a document") +} + +/// The `_id` of the document a put of `item` stores. +fn document_id(item: &Item, key_info: &TableKeyInfo) -> Bson { + stored_document(item, key_info) + .get("_id") + .cloned() + .expect("document has an _id") +} + +/// An unconditional PutItem whose insert keeps colliding on `_id` retries for +/// the whole retry budget, then gives up with the create race error, on the +/// sessionless path and on the transaction path. +/// +/// A document that holds the item's `_id` under a different `pk` makes every +/// attempt collide: the put's filter on `pk` finds no item, and its insert +/// raises the duplicate-key error. The obstructing document stays as it was. +#[tokio::test] +#[ignore = "needs a MongoDB replica set in EXTENDDB_TEST_MONGODB_URI"] +async fn live_unconditional_put_retries_a_lost_create_race_and_then_gives_up() { + let engine = scratch_engine().await; + let maps = ExpressionMaps::default(); + for (table_id, has_gsi) in [("sessionless-put", false), ("transaction-put", true)] { + let key_info = hash_table(&engine, table_id, has_gsi); + let item = item_with_w("new"); + let id = document_id(&item, &key_info); + let coll = engine + .data_db + .collection::(&data_collection_name(table_id)); + coll.insert_one(doc! { "_id": id.clone(), "pk": "another-key", "item_data": {} }) + .await + .expect("insert the obstructing document"); + + let res = engine + .put_item_impl(&key_info, item, true, None, &maps, None) + .await; + + let want = format!("could not create the item after {TRANSIENT_RETRY_ATTEMPTS} attempts"); + match res { + Err(StorageError::Internal(msg)) => assert!( + msg.contains(&want), + "{table_id}: want the create race error, got: {msg}" + ), + other => panic!("{table_id}: want the create race error, got: {other:?}"), + } + assert_eq!( + coll.count_documents(doc! {}).await.expect("count"), + 1, + "{table_id}: the put wrote a document" + ); + let left = coll + .find_one(doc! { "_id": id }) + .await + .expect("read back") + .expect("obstructing document is still there"); + assert_eq!(left.get_str("pk").expect("pk"), "another-key", "{table_id}"); + } + finish(engine).await; +} + +/// What happens to the obstructing document while the put retries. +#[derive(Clone, Copy, Debug)] +enum Obstruction { + /// It is replaced, under the same `_id`, with the item of another put + /// that created the key. + BecomesTheOtherPutsItem, + /// It is deleted, as a DeleteItem removes the item another put created. + IsDeleted, +} + +/// An unconditional PutItem whose first attempts collide on `_id` succeeds on +/// a later attempt, on the sessionless path and on the transaction path. +/// +/// The put starts while a document holds the item's `_id` under another +/// `pk`, so its first attempts collide. 100 ms later another task either +/// replaces that document with the item of another put on the same key, or +/// deletes it. The put's attempts back off between collisions and together +/// take longer than 100 ms, so a later attempt either replaces the other +/// put's item and returns it as the old image, or inserts and returns no old +/// image. In both cases the stored document is this put's item. +#[tokio::test] +#[ignore = "needs a MongoDB replica set in EXTENDDB_TEST_MONGODB_URI"] +async fn live_unconditional_put_that_lost_a_create_race_succeeds_on_a_later_attempt() { + let engine = scratch_engine().await; + let maps = ExpressionMaps::default(); + for (path, has_gsi) in [("sessionless", false), ("transaction", true)] { + for change in [Obstruction::BecomesTheOtherPutsItem, Obstruction::IsDeleted] { + let table_id = format!("{path}-{change:?}"); + let key_info = hash_table(&engine, &table_id, has_gsi); + let item = item_with_w("this-put"); + let other_put = item_with_w("other-put"); + let other_put_doc = stored_document(&other_put, &key_info); + let id = document_id(&other_put, &key_info); + let coll = engine + .data_db + .collection::(&data_collection_name(&table_id)); + coll.insert_one(doc! { "_id": id.clone(), "pk": "another-key", "item_data": {} }) + .await + .expect("insert the obstructing document"); + + let put = engine.put_item_impl(&key_info, item.clone(), true, None, &maps, None); + let clear = async { + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + match change { + Obstruction::BecomesTheOtherPutsItem => { + coll.replace_one(doc! { "_id": id.clone() }, other_put_doc.clone()) + .await + .expect("replace the obstructing document"); + } + Obstruction::IsDeleted => { + coll.delete_one(doc! { "_id": id.clone() }) + .await + .expect("delete the obstructing document"); + } + } + }; + let (res, ()) = tokio::join!(put, clear); + + let old = match res { + Ok(old) => old, + Err(e) => panic!("{table_id}: want the put to succeed, got: {e:?}"), + }; + let want_old = match change { + Obstruction::BecomesTheOtherPutsItem => Some(other_put), + Obstruction::IsDeleted => None, + }; + assert_eq!(old, want_old, "{table_id}: old image"); + assert_eq!( + coll.count_documents(doc! {}).await.expect("count"), + 1, + "{table_id}: documents in the collection" + ); + let stored = coll + .find_one(doc! { "_id": id }) + .await + .expect("read back") + .expect("the put's document is stored"); + assert_eq!( + document_to_item(&stored).expect("stored document converts to an item"), + item, + "{table_id}: stored item" + ); + } + } + finish(engine).await; +} diff --git a/devtools/run-mongodb-tests b/devtools/run-mongodb-tests index c8c70fde0..a41cc78f0 100755 --- a/devtools/run-mongodb-tests +++ b/devtools/run-mongodb-tests @@ -320,10 +320,6 @@ export EXTENDDB_TEST_MONGODB_CONTAINER="$CONTAINER_NAME" # physical index lifecycle after exercising the DynamoDB API. export EXTENDDB_TEST_MONGODB_CONTAINER="$CONTAINER_NAME" -# Storage crate tests that need a live replica set connect here. Each test -# uses its own databases and drops them when it finishes. -export EXTENDDB_TEST_MONGODB_URI="mongodb://localhost:$MONGO_PORT/?replicaSet=rs0&directConnection=true" - # Some tests read EXTENDDB_CONFIG to invoke `extenddb settings` against # the running instance. Without this, they fall back to `./extenddb.toml` # in the repo root, which is a stale dev config that points at a From 0d3f76d5660a462ddf739e4267814d1d4a86cdc3 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 10:38:47 +0000 Subject: [PATCH 09/44] docs(mongodb): say a create race in a transaction is a write conflict The comment on the duplicate-key arm of the transaction path said the arm handles a put that lost the create race to another put. It does not. On MongoDB 7.0, two snapshot transactions that upsert the same _id fail the second upsert with WriteConflict (code 112, "WiredTigerIdIndex::_insert: index: _id_") and the TransientTransactionError label, both while the first transaction is open and after it committed past the second one's snapshot. TxErr::from already retried that error before this branch. The comment now says the arm is a defence: it is reached only when a document in the transaction's snapshot holds the item's _id without matching the key filter, which is the document the storage tests insert. --- crates/storage-mongodb/src/data_engine.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 2a5dc361b..4a203c37d 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -551,13 +551,21 @@ impl MongoEngine { .upsert(true) .return_document(ReturnDocument::Before) .build(); - // Same create race as the fast path above: a - // duplicate-key error means another put created the - // item first. Run the transaction again, so that it - // finds that item, replaces it, and removes its index - // entries and records it as the stream old image. A - // lost create race uses the same retry budget and - // backoff as a write conflict. + // A put that loses the create race to another put does + // not reach the duplicate-key arm here. Inside a + // snapshot transaction MongoDB 7.0 reports that + // collision as WriteConflict (code 112, + // `WiredTigerIdIndex::_insert: index: _id_`) with the + // TransientTransactionError label, whether the other + // put is still open or committed after this + // transaction's snapshot, and `TxErr::from` retries it. + // The arm is a defence: a duplicate-key error here means + // a document in this transaction's snapshot holds the + // item's `_id` without matching the key filter. It runs + // the transaction again with the retry budget and + // backoff of a write conflict, and the put gives up with + // the create race error when every attempt meets that + // document. let old_doc = match coll .find_one_and_replace(key_filter.clone(), new_doc) .with_options(opts) From 08c49954a96940c51230db9cf0c2396658ee52d1 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 10:38:47 +0000 Subject: [PATCH 10/44] test(put-item): give each create race writer its own client test_put_item_create_race.py shared one boto3 client across its 8 writer threads. A botocore client used from several threads at once can fail a request with a TLS error, and with retries off and the MongoDB xfail marker gone, that error fails the test. Each writer now has its own client. --- tests/test_put_item_create_race.py | 53 ++++++++++++++++-------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/tests/test_put_item_create_race.py b/tests/test_put_item_create_race.py index cb5736617..15c2166a8 100644 --- a/tests/test_put_item_create_race.py +++ b/tests/test_put_item_create_race.py @@ -33,21 +33,23 @@ @pytest.fixture(scope="module") -def raw_client(endpoint_url): - """A client that never retries, so every failure is seen.""" +def raw_clients(endpoint_url): + """One client per writer thread, none of which retries, so every failure is seen. + + A botocore client shared by several threads can fail a request with a TLS + error under concurrent use. With retries off that error would fail the + test, so each writer gets a client of its own. + """ kwargs: dict = { "service_name": "dynamodb", "region_name": os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), - "config": Config( - retries={"total_max_attempts": 1, "mode": "standard"}, - max_pool_connections=WRITERS * 2, - ), + "config": Config(retries={"total_max_attempts": 1, "mode": "standard"}), } if endpoint_url: kwargs["endpoint_url"] = endpoint_url if endpoint_url.startswith("https://"): kwargs["verify"] = False - return boto3.client(**kwargs) + return [boto3.client(**kwargs) for _ in range(WRITERS)] @pytest.fixture(scope="module") @@ -137,9 +139,9 @@ def _key(table_kind: str, pk: str) -> dict: def _race( - client, table: str, table_kind: str, extra: dict, batch: bool = False + clients: list, table: str, table_kind: str, extra: dict, batch: bool = False ) -> tuple[str, list]: - """WRITERS clients put the same new key at once, each with its own `w`. + """WRITERS writers put the same new key at once, each with its own `w` and its own client. Each writer's item also carries index keys unique to it: `gk` across the whole table, and `lk` within the key. With `batch`, each writer sends a @@ -154,6 +156,7 @@ def _race( errors: list[BaseException] = [] def run(i: int): + client = clients[i] item = { **_key(table_kind, pk), "w": {"S": f"w{i}"}, @@ -211,21 +214,21 @@ def _assert_one_chain(out: list, final: str): @pytest.mark.parametrize("table_kind", ["hash", "range"]) def test_racing_puts_with_all_old_all_succeed( - request, dynamodb_client, raw_client, table_kind + request, dynamodb_client, raw_clients, table_kind ): table = request.getfixturevalue(f"{table_kind}_table") for _ in range(ROUNDS): - pk, out = _race(raw_client, table, table_kind, {"ReturnValues": "ALL_OLD"}) + pk, out = _race(raw_clients, table, table_kind, {"ReturnValues": "ALL_OLD"}) _assert_all_succeed(out) _assert_one_chain(out, _final_w(dynamodb_client, table, table_kind, pk)) def test_racing_puts_that_return_consumed_capacity_all_succeed( - dynamodb_client, raw_client, hash_table + dynamodb_client, raw_clients, hash_table ): extra = {"ReturnConsumedCapacity": "TOTAL"} for _ in range(ROUNDS): - pk, out = _race(raw_client, hash_table, "hash", extra) + pk, out = _race(raw_clients, hash_table, "hash", extra) _assert_all_succeed(out) assert _final_w(dynamodb_client, hash_table, "hash", pk) in { f"w{i}" for i in range(WRITERS) @@ -233,21 +236,21 @@ def test_racing_puts_that_return_consumed_capacity_all_succeed( def test_racing_puts_check_their_condition_against_the_winner( - dynamodb_client, raw_client, range_table + dynamodb_client, raw_clients, range_table ): """A condition that every item satisfies never fails, whoever created the item.""" extra = {"ConditionExpression": "attribute_not_exists(zz)", "ReturnValues": "ALL_OLD"} for _ in range(ROUNDS): - pk, out = _race(raw_client, range_table, "range", extra) + pk, out = _race(raw_clients, range_table, "range", extra) _assert_all_succeed(out) _assert_one_chain(out, _final_w(dynamodb_client, range_table, "range", pk)) -def test_racing_conditional_creates_have_one_winner(raw_client, hash_table): +def test_racing_conditional_creates_have_one_winner(raw_clients, hash_table): """Control: with attribute_not_exists(pk), exactly one put creates the item.""" for _ in range(ROUNDS): _, out = _race( - raw_client, hash_table, "hash", {"ConditionExpression": "attribute_not_exists(pk)"} + raw_clients, hash_table, "hash", {"ConditionExpression": "attribute_not_exists(pk)"} ) codes = sorted(o[1] if o[0] == "error" else "ok" for o in out) assert codes == ["ConditionalCheckFailedException"] * (WRITERS - 1) + ["ok"], codes @@ -267,12 +270,12 @@ def _gsi_keys(client, table: str) -> dict[str, list[str]]: def test_racing_puts_on_a_gsi_table_leave_only_the_final_entry( - dynamodb_client, raw_client, gsi_table + dynamodb_client, raw_clients, gsi_table ): """The GSI holds the final item's entry and none of the items it replaced.""" want: dict[str, list[str]] = {} for _ in range(ROUNDS): - pk, out = _race(raw_client, gsi_table, "hash", {}) + pk, out = _race(raw_clients, gsi_table, "hash", {}) _assert_all_succeed(out) want[pk] = [_final(dynamodb_client, gsi_table, "hash", pk)["gk"]["S"]] # The GSI is eventually consistent on Amazon DynamoDB, so poll. @@ -284,11 +287,11 @@ def test_racing_puts_on_a_gsi_table_leave_only_the_final_entry( def test_racing_puts_on_an_lsi_table_leave_only_the_final_entry( - dynamodb_client, raw_client, lsi_table + dynamodb_client, raw_clients, lsi_table ): """A strongly consistent LSI query returns the final item's entry only.""" for _ in range(ROUNDS): - pk, out = _race(raw_client, lsi_table, "range", {"ReturnValues": "ALL_OLD"}) + pk, out = _race(raw_clients, lsi_table, "range", {"ReturnValues": "ALL_OLD"}) _assert_all_succeed(out) final = _final(dynamodb_client, lsi_table, "range", pk) _assert_one_chain(out, final["w"]["S"]) @@ -347,23 +350,23 @@ def _assert_stream_chains(dynamodb_client, streams_client, table: str, pks: list def test_racing_puts_on_a_stream_table_all_succeed( - dynamodb_client, raw_client, streams_client, stream_table + dynamodb_client, raw_clients, streams_client, stream_table ): pks = [] for _ in range(ROUNDS): - pk, out = _race(raw_client, stream_table, "hash", {}) + pk, out = _race(raw_clients, stream_table, "hash", {}) _assert_all_succeed(out) pks.append(pk) _assert_stream_chains(dynamodb_client, streams_client, stream_table, pks) def test_racing_batch_puts_on_a_stream_table_all_succeed( - dynamodb_client, raw_client, streams_client, stream_table + dynamodb_client, raw_clients, streams_client, stream_table ): """BatchWriteItem puts read the current item for the stream record too.""" pks = [] for _ in range(ROUNDS): - pk, out = _race(raw_client, stream_table, "hash", {}, batch=True) + pk, out = _race(raw_clients, stream_table, "hash", {}, batch=True) _assert_all_succeed(out) pks.append(pk) _assert_stream_chains(dynamodb_client, streams_client, stream_table, pks) From 921232814126641f22955e49fd9a4abdb9b06cd6 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 10:47:13 +0000 Subject: [PATCH 11/44] test(mongodb): build every test engine through one constructor Merged with main, the storage-mongodb test build failed with E0063: the MongoEngine literals in stream_records_live_tests.rs (from #543) and in snapshot_engine.rs (from #395) do not set contention_deadline. Each test that needed an engine listed every field, so each field added to MongoEngine broke every such literal. MongoEngine::new now builds the engine in from_parts, and a test-only MongoEngine::for_test calls the same function with the driver's default pool of 10 connections. The test literals in backup_live_tests.rs, stream_records_live_tests.rs, snapshot_engine.rs and the restore backfill test now call for_test and set only the fields they change with struct update syntax. A new field is set in from_parts and nowhere else. The four contention storage tests move from the data_engine test module into data_engine_live_tests_contention.rs, next to the create-race tests #431 moved. They build the engine through the live test module's scratch_engine, are #[ignore]d like the other live tests and run under devtools/run-mongodb-tests --rust-live, and set the deadline with struct update syntax. The with_scratch_engine helpers and the second URI gate go with them. What each test checks is unchanged. --- .../storage-mongodb/src/backup_live_tests.rs | 17 +- crates/storage-mongodb/src/data_engine.rs | 559 +----------------- .../src/data_engine_live_tests.rs | 3 + .../src/data_engine_live_tests_contention.rs | 438 ++++++++++++++ crates/storage-mongodb/src/lib.rs | 48 +- crates/storage-mongodb/src/snapshot_engine.rs | 11 +- .../src/stream_records_live_tests.rs | 17 +- 7 files changed, 502 insertions(+), 591 deletions(-) create mode 100644 crates/storage-mongodb/src/data_engine_live_tests_contention.rs diff --git a/crates/storage-mongodb/src/backup_live_tests.rs b/crates/storage-mongodb/src/backup_live_tests.rs index 439170717..c787caf84 100644 --- a/crates/storage-mongodb/src/backup_live_tests.rs +++ b/crates/storage-mongodb/src/backup_live_tests.rs @@ -102,21 +102,12 @@ impl Fixture { async fn engine_on(catalog: &str, data: &str, liveness: BackupLiveness) -> MongoEngine { let client = mongodb::Client::with_uri_str(uri()).await.expect("client"); - // The driver default pool (10) and the slot count production derives - // from it, so the tests see the same admission bound as `serve`. - let slots = super::restore_server_slot_count(10); + // `for_test` derives the restore slot count from the driver default pool + // (10), as production does, so the tests see the same admission bound as + // `serve`. MongoEngine { - client: client.clone(), - catalog_db: client.database(catalog), - data_db: client.database(data), - region: "us-east-1".to_owned(), - gsi_cache: Arc::default(), - tx_read_concern: mongodb::options::ReadConcern::snapshot(), backup_liveness: liveness, - restore_server_slot_count: slots, - restore_server_slots: Arc::new(tokio::sync::Semaphore::new(slots)), - restore_account_slots: super::RestoreAccountSlots::default(), - contention_deadline: crate::data_engine::CONTENTION_DEADLINE, + ..MongoEngine::for_test(&client, client.database(catalog), client.database(data)) } } diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 9f26d4fa4..4c46b7491 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -4485,551 +4485,6 @@ mod tests { assert!(!is_transient_write_conflict(&command_error(11000))); } - /// Connection string of the MongoDB replica set for a test that needs a - /// server, or `None` after reporting that the test is skipped. - /// - /// `devtools/run-mongodb-tests` exports `EXTENDDB_TEST_MONGODB_URI` - /// together with `EXTENDDB_TEST_MONGODB_CONTAINER`. Without the URI the - /// test prints a SKIP line and returns, as the PostgreSQL storage crate's - /// live tests do. The line is written to the stderr handle, because the - /// test harness captures `eprintln!` output of a passing test. When the - /// container variable is set and the URI is not, the test is running - /// under that runner and the URI export is missing, so it fails instead - /// of skipping. - fn live_mongodb_uri(test: &str) -> Option { - let set = |name: &str| std::env::var(name).ok().filter(|v| !v.trim().is_empty()); - if let Some(uri) = set("EXTENDDB_TEST_MONGODB_URI") { - return Some(uri); - } - if let Some(container) = set("EXTENDDB_TEST_MONGODB_CONTAINER") { - panic!( - "{test}: EXTENDDB_TEST_MONGODB_CONTAINER is set ({container}) but \ - EXTENDDB_TEST_MONGODB_URI is not; devtools/run-mongodb-tests exports both" - ); - } - use std::io::Write as _; - let _ = writeln!( - std::io::stderr(), - "SKIP {test}: EXTENDDB_TEST_MONGODB_URI is not set, so there is no MongoDB replica \ - set to run against." - ); - None - } - - /// Run `body` with an engine on two new databases of the replica set at - /// `uri`, then drop both databases, also when `body` panics. - async fn with_scratch_engine(uri: &str, body: F) - where - F: FnOnce(MongoEngine, mongodb::Database) -> Fut, - Fut: std::future::Future, - { - with_scratch_engine_deadline(uri, CONTENTION_DEADLINE, body).await; - } - - /// `with_scratch_engine` with the engine's contention deadline set, so a - /// test can decide which bound a contended write reaches: a deadline - /// below the time the attempt budget takes makes the deadline the bound, - /// one far above it makes the attempt budget the bound. - async fn with_scratch_engine_deadline( - uri: &str, - contention_deadline: std::time::Duration, - body: F, - ) where - F: FnOnce(MongoEngine, mongodb::Database) -> Fut, - Fut: std::future::Future, - { - use futures::FutureExt as _; - let client = mongodb::Client::with_uri_str(uri) - .await - .expect("EXTENDDB_TEST_MONGODB_URI must parse"); - let suffix = uuid::Uuid::new_v4().simple().to_string(); - let data_db = client.database(&format!("extenddb_test_{suffix}")); - let catalog_db = client.database(&format!("extenddb_test_{suffix}_catalog")); - // The four restore and liveness fields as `MongoEngine::new` sets - // them, with the slot count derived from the driver's default pool - // (10), as in backup_live_tests.rs. - let slots = crate::backup_engine::restore_server_slot_count(10); - let engine = MongoEngine { - client: client.clone(), - catalog_db: catalog_db.clone(), - data_db: data_db.clone(), - region: "us-east-1".to_owned(), - gsi_cache: Default::default(), - tx_read_concern: mongodb::options::ReadConcern::snapshot(), - backup_liveness: crate::backup_engine::BackupLiveness::default(), - restore_server_slot_count: slots, - restore_server_slots: std::sync::Arc::new(tokio::sync::Semaphore::new(slots)), - restore_account_slots: crate::backup_engine::RestoreAccountSlots::default(), - contention_deadline, - }; - let outcome = std::panic::AssertUnwindSafe(body(engine, data_db.clone())) - .catch_unwind() - .await; - let _ = data_db.drop().await; - let _ = catalog_db.drop().await; - if let Err(panic) = outcome { - std::panic::resume_unwind(panic); - } - } - - /// Key info for a table with a string hash key `pk`. The GSI cache is set - /// so that an unconditional put takes the sessionless path (`has_gsi` - /// false) or the transaction path (`has_gsi` true). The catalog has no - /// index rows, so the transaction path writes no index entries. - fn hash_key_table(engine: &MongoEngine, table_id: &str, has_gsi: bool) -> TableKeyInfo { - engine.gsi_cache_set(table_id, has_gsi); - let key_schema = vec![KeySchemaElement { - attribute_name: "pk".to_owned(), - key_type: extenddb_core::types::KeyType::Hash, - }]; - TableKeyInfo { - table_id: table_id.to_owned(), - key_schema: key_schema.clone(), - base_key_schema: key_schema, - attribute_definitions: vec![extenddb_core::types::AttributeDefinition { - attribute_name: "pk".to_owned(), - attribute_type: ScalarAttributeType::S, - }], - ..Default::default() - } - } - - fn item_with(pk: &str, w: &str) -> Item { - let mut item = Item::new(); - item.insert("pk".to_owned(), AttributeValue::S(pk.to_owned())); - item.insert("w".to_owned(), AttributeValue::S(w.to_owned())); - item - } - - fn stored_document(item: &Item, key_info: &TableKeyInfo) -> Document { - item_to_document(item, &key_info.key_schema, &key_info.attribute_definitions) - .expect("test item converts to a document") - } - - /// Concurrent unconditional puts on one key of a table with a GSI all - /// commit: 32 writers put the same key 8 times each, every put returns - /// Ok, and the index holds the final item's entry only. On the service - /// every one of these puts answers 200; the backend reaches that through - /// its retry budget. - /// - /// The table has a GSI row in the catalog, so `sync_indexes_in_session` - /// writes an index entry per put and keeps the GSI cache at `true`. - /// Without the row the first committed put flips the cache to `false`, - /// the other writers take the sessionless path, and a sessionless - /// findAndModify on a document an open transaction has written waits for - /// that transaction; with 32 writers starting transactions without pause - /// the run stalled for minutes. - #[tokio::test(flavor = "multi_thread", worker_threads = 8)] - async fn concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit() { - let Some(uri) = - live_mongodb_uri("concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit") - else { - return; - }; - with_scratch_engine(&uri, |engine, data_db| async move { - const WRITERS: usize = 32; - const PUTS: usize = 8; - let engine = std::sync::Arc::new(engine); - let key_info = std::sync::Arc::new(hash_key_table(&engine, "hot-key", true)); - let gsi_key_schema = vec![KeySchemaElement { - attribute_name: "gk".to_owned(), - key_type: extenddb_core::types::KeyType::Hash, - }]; - let projection = Projection { - projection_type: ProjectionType::All, - non_key_attributes: None, - }; - engine - .catalog_db - .collection::("indexes") - .insert_one(doc! { - "_id": { "table_id": "hot-key", "index_name": "gk-index" }, - "index_id": "hot-key-gk-index", - "index_type": "GSI", - "key_schema": bson::to_bson(&gsi_key_schema).expect("key schema"), - "projection": bson::to_bson(&projection).expect("projection"), - "index_status": "ACTIVE", - }) - .await - .expect("insert the GSI catalog row"); - let start = std::sync::Arc::new(tokio::sync::Barrier::new(WRITERS)); - let mut tasks = Vec::with_capacity(WRITERS); - for w in 0..WRITERS { - let (engine, key_info, start) = (engine.clone(), key_info.clone(), start.clone()); - tasks.push(tokio::spawn(async move { - let maps = ExpressionMaps::default(); - start.wait().await; - let mut failures = Vec::new(); - for n in 0..PUTS { - let mut item = item_with("k1", &format!("w{w}-{n}")); - item.insert("gk".to_owned(), AttributeValue::S(format!("g-w{w}-{n}"))); - if let Err(e) = engine - .put_item_impl(&key_info, item, false, None, &maps, None) - .await - { - failures.push(format!("{e:?}")); - } - } - failures - })); - } - let mut failures = Vec::new(); - for task in tasks { - failures.extend(task.await.expect("writer task")); - } - assert!( - failures.is_empty(), - "{} of {} puts failed: {:?}", - failures.len(), - WRITERS * PUTS, - &failures[..failures.len().min(3)] - ); - let coll = data_db.collection::(&data_collection_name("hot-key")); - assert_eq!(coll.count_documents(doc! {}).await.expect("count"), 1); - let stored = coll - .find_one(doc! {}) - .await - .expect("read") - .expect("the item"); - let stored = document_to_item(&stored).expect("stored document converts to an item"); - let idx_coll = - data_db.collection::(&data_collection_name("hot-key-gk-index")); - use futures::TryStreamExt as _; - let entries: Vec = idx_coll - .find(doc! {}) - .await - .expect("read the index") - .try_collect() - .await - .expect("collect the index"); - assert_eq!(entries.len(), 1, "index entries: {entries:?}"); - let entry = document_to_item(&entries[0]).expect("index entry converts to an item"); - assert_eq!( - entry.get("gk"), - stored.get("gk"), - "index entry {entry:?} for {stored:?}" - ); - }) - .await; - } - - /// A put whose every attempt conflicts gives up with a transient error, - /// the one the engine answers as HTTP 503, not with a transaction - /// conflict. Another session writes the item inside a transaction it - /// never commits, so each attempt of the put hits a write conflict until - /// one of the two bounds is reached; the held transaction is then - /// aborted. - /// The key of `item_with(pk, ...)` on a hash-key table. - fn hash_key(pk: &str) -> Item { - let mut key = Item::new(); - key.insert("pk".to_owned(), AttributeValue::S(pk.to_owned())); - key - } - - /// `SET w = :w` with its placeholder, for an UpdateItem under contention. - fn set_w(value: &str) -> (Vec, ExpressionMaps) { - let actions = vec![UpdateAction::Set { - path: vec![PathElement::Attribute("w".to_owned())], - value: Expr::Placeholder(":w".to_owned()), - }]; - let maps = ExpressionMaps::new( - std::collections::HashMap::new(), - std::collections::HashMap::from([( - ":w".to_owned(), - AttributeValue::S(value.to_owned()), - )]), - ); - (actions, maps) - } - - /// Hold `pk` of `table_id` inside a transaction that is never committed, - /// so every attempt of a contended write on that key conflicts. The - /// caller aborts the returned session when it is done. - async fn hold_item_in_a_transaction( - engine: &MongoEngine, - data_db: &mongodb::Database, - key_info: &TableKeyInfo, - pk: &str, - ) -> mongodb::ClientSession { - let coll = data_db.collection::(&data_collection_name(&key_info.table_id)); - coll.insert_one(stored_document(&item_with(pk, "held"), key_info)) - .await - .expect("insert the item to hold"); - let mut holder = engine.client.start_session().await.expect("session"); - holder - .start_transaction() - .with_options(engine.write_transaction_options()) - .await - .expect("start the holding transaction"); - coll.replace_one( - doc! { "pk": pk }, - stored_document(&item_with(pk, "rewritten"), key_info), - ) - .session(&mut holder) - .await - .expect("write the item inside the held transaction"); - holder - } - - /// Each single-item write that reaches the engine's contention deadline - /// gives up with the transient error the engine answers as HTTP 503 - /// `ServiceUnavailable`, and the message names the operation and the - /// deadline that passed. - /// - /// The deadline is 2 s here, below the 9.5 to 12.7 s the 1,000 attempts - /// take against a document a transaction holds, so the deadline is the - /// bound each of the three writes reaches, with no race between the two. - /// The attempt budget is the bound in - /// `unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error`. - #[tokio::test] - async fn a_single_item_write_that_reaches_the_deadline_answers_a_transient_error() { - let Some(uri) = live_mongodb_uri( - "a_single_item_write_that_reaches_the_deadline_answers_a_transient_error", - ) else { - return; - }; - let deadline = std::time::Duration::from_secs(2); - with_scratch_engine_deadline(&uri, deadline, |engine, data_db| async move { - let maps = ExpressionMaps::default(); - let key_info = hash_key_table(&engine, "deadline-key", true); - let coll = data_db.collection::(&data_collection_name("deadline-key")); - let mut holder = hold_item_in_a_transaction(&engine, &data_db, &key_info, "k1").await; - let key = hash_key("k1"); - let (actions, update_maps) = set_w("this-update"); - - for operation in ["PutItem", "DeleteItem", "UpdateItem"] { - let started = std::time::Instant::now(); - let res = match operation { - "PutItem" => engine - .put_item_impl( - &key_info, - item_with("k1", "this-put"), - false, - None, - &maps, - None, - ) - .await - .map(|_| ()), - "DeleteItem" => engine - .delete_item_impl(&key_info, &key, false, None, &maps, None) - .await - .map(|_| ()), - _ => engine - .update_item_impl( - &key_info, - &key, - &actions, - false, - false, - None, - &update_maps, - None, - ) - .await - .map(|_| ()), - }; - let elapsed = started.elapsed(); - match res { - Err(StorageError::Transient(msg)) => { - assert_eq!( - msg, - format!( - "{operation}: the write lost to concurrent writers for 2 \ - seconds; retry the request" - ) - ); - println!("{operation} gave up on the deadline in {elapsed:?}: {msg}"); - } - other => { - panic!("{operation}: want the transient deadline error, got: {other:?}") - } - } - assert!( - elapsed >= deadline, - "{operation} answered in {elapsed:?}, before the deadline" - ); - assert!( - elapsed < deadline + std::time::Duration::from_secs(5), - "{operation} took {elapsed:?}, so a bound other than the deadline ended it" - ); - } - - let _ = holder.abort_transaction().await; - // No give-up wrote anything: the held item is as it was inserted. - let stored = coll - .find_one(doc! { "pk": "k1" }) - .await - .expect("read") - .expect("item"); - assert_eq!( - document_to_item(&stored).expect("item"), - item_with("k1", "held") - ); - }) - .await; - } - - /// A `TransactWriteItems` that reaches the deadline answers a canceled - /// transaction with a `TransactionConflict` reason per item, which is - /// what live DynamoDB answers a transaction that loses to contention - /// (107 of 160 calls at 16 writers on one key, probes/R10-1.out). It does - /// not answer the HTTP 503 the single-item writes answer. - #[tokio::test] - async fn a_transaction_that_reaches_the_deadline_answers_a_canceled_transaction() { - let Some(uri) = live_mongodb_uri( - "a_transaction_that_reaches_the_deadline_answers_a_canceled_transaction", - ) else { - return; - }; - let deadline = std::time::Duration::from_secs(2); - with_scratch_engine_deadline(&uri, deadline, |engine, data_db| async move { - let key_info = hash_key_table(&engine, "deadline-transact", true); - let coll = data_db.collection::(&data_collection_name("deadline-transact")); - let mut holder = hold_item_in_a_transaction(&engine, &data_db, &key_info, "k1").await; - - // Two operations, one on the held key and one on a free key, so - // the transaction conflicts and the answer carries one reason - // per operation rather than one for the contended item. - let ops = vec![ - OwnedTransactWriteOp::Put { - key_info: key_info.clone(), - item: item_with("k1", "this-transaction"), - condition: None, - maps: ExpressionMaps::default(), - return_values_on_ccf: ReturnValuesOnConditionCheckFailure::None, - stream: None, - }, - OwnedTransactWriteOp::Put { - key_info: key_info.clone(), - item: item_with("k2", "this-transaction"), - condition: None, - maps: ExpressionMaps::default(), - return_values_on_ccf: ReturnValuesOnConditionCheckFailure::None, - stream: None, - }, - ]; - - let started = std::time::Instant::now(); - let res = engine.transact_write_items_impl(&ops, None).await; - let elapsed = started.elapsed(); - let _ = holder.abort_transaction().await; - - match res { - Err(StorageError::TransactionCanceled(reasons)) => { - assert_eq!(reasons.len(), ops.len(), "one reason per operation"); - for reason in &reasons { - assert_eq!(reason.code, "TransactionConflict"); - assert_eq!( - reason.message.as_deref(), - Some("Transaction is ongoing for the item") - ); - } - println!("the transaction was canceled in {elapsed:?}: {reasons:?}"); - } - other => panic!("want a canceled transaction, got: {other:?}"), - } - assert!( - elapsed >= deadline, - "the transaction answered in {elapsed:?}, before the deadline" - ); - assert!( - elapsed < deadline + std::time::Duration::from_secs(5), - "took {elapsed:?}, so a bound other than the deadline ended it" - ); - - // Neither operation of the canceled transaction was applied. - let stored = coll - .find_one(doc! { "pk": "k1" }) - .await - .expect("read") - .expect("item"); - assert_eq!( - document_to_item(&stored).expect("item"), - item_with("k1", "held") - ); - assert!( - coll.find_one(doc! { "pk": "k2" }) - .await - .expect("read") - .is_none(), - "the free key of the canceled transaction must not be written" - ); - }) - .await; - } - - /// An unconditional PutItem that spends its whole attempt budget on - /// contention gives up with the transient error naming the attempts, - /// which the engine answers as HTTP 503. - /// - /// The deadline is set far above the time the budget takes (9.5 to 12.7 s - /// against a document a transaction holds, over nine runs on a shared - /// host), so the attempt budget is the bound this test reaches. With the - /// production deadline of 10 s either bound can fire first, which is why - /// the deadline is the test's input rather than its race. - #[tokio::test] - async fn unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error() { - let Some(uri) = live_mongodb_uri( - "unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error", - ) else { - return; - }; - let deadline = std::time::Duration::from_secs(600); - with_scratch_engine_deadline(&uri, deadline, |engine, data_db| async move { - let maps = ExpressionMaps::default(); - let key_info = hash_key_table(&engine, "held-key", true); - let coll = data_db.collection::(&data_collection_name("held-key")); - let mut holder = hold_item_in_a_transaction(&engine, &data_db, &key_info, "k1").await; - - let started = std::time::Instant::now(); - let res = engine - .put_item_impl( - &key_info, - item_with("k1", "this-put"), - false, - None, - &maps, - None, - ) - .await; - let elapsed = started.elapsed(); - let _ = holder.abort_transaction().await; - - match res { - Err(StorageError::Transient(msg)) => { - assert_eq!( - msg, - format!( - "PutItem: the write lost {TRANSIENT_RETRY_ATTEMPTS} attempts to \ - concurrent writers; retry the request" - ) - ); - println!("the put spent its attempt budget in {elapsed:?}: {msg}"); - } - other => panic!("want the transient spent-budget error, got: {other:?}"), - } - // The 1,000 attempts take 9.5 to 12.7 s on a shared host, and - // the sleeps alone come to 12.8 s at worst, so the bound here is - // loose: what it rules out is a budget that no longer ends the - // loop at all. - assert!( - elapsed < std::time::Duration::from_secs(120), - "took {elapsed:?}" - ); - let stored = coll - .find_one(doc! { "pk": "k1" }) - .await - .expect("read") - .expect("item"); - assert_eq!( - document_to_item(&stored).expect("item"), - item_with("k1", "held") - ); - }) - .await; - } - #[derive(Default)] struct RecordingGsiIndexWriter { direct_writes: AtomicUsize, @@ -5066,17 +4521,13 @@ mod tests { .await .expect("MongoDB URI must parse without connecting"); let engine = MongoEngine { - client: client.clone(), - catalog_db: client.database("extenddb_catalog"), - data_db: client.database("extenddb_data"), - region: "us-east-1".to_owned(), - gsi_cache: Default::default(), - tx_read_concern: mongodb::options::ReadConcern::snapshot(), - backup_liveness: Default::default(), restore_server_slot_count: 1, restore_server_slots: std::sync::Arc::new(tokio::sync::Semaphore::new(1)), - restore_account_slots: Default::default(), - contention_deadline: CONTENTION_DEADLINE, + ..MongoEngine::for_test( + &client, + client.database("extenddb_catalog"), + client.database("extenddb_data"), + ) }; let base_coll = engine.data_db.collection::("base"); let key_schema = vec![KeySchemaElement { diff --git a/crates/storage-mongodb/src/data_engine_live_tests.rs b/crates/storage-mongodb/src/data_engine_live_tests.rs index e1761076f..4ffe79c89 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests.rs @@ -41,6 +41,9 @@ mod repair_pass; #[path = "data_engine_live_tests_put_race.rs"] mod put_race; +#[path = "data_engine_live_tests_contention.rs"] +mod contention; + fn uri() -> String { std::env::var("EXTENDDB_TEST_MONGODB_URI").expect( "set EXTENDDB_TEST_MONGODB_URI to a MongoDB replica set, e.g. \ diff --git a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs new file mode 100644 index 000000000..d02715a92 --- /dev/null +++ b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs @@ -0,0 +1,438 @@ +// Copyright 2026 ExtendDB contributors +// SPDX-License-Identifier: Apache-2.0 + +//! Live MongoDB tests for writes that lose to concurrent writers on one item: +//! puts on one key of a table with a GSI, and the two bounds that end a +//! contended write, the engine's contention deadline and the attempt budget. +//! +//! Each test sets the engine's deadline, so a test decides which bound a +//! contended write reaches instead of racing 1,000 attempts against a wall +//! clock. The tests read the replica set URI from `EXTENDDB_TEST_MONGODB_URI` +//! and are `#[ignore]`d, like the tests in the parent module. + +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use bson::{Document, doc}; +use extenddb_core::expression::{Expr, ExpressionMaps, PathElement, UpdateAction}; +use extenddb_core::types::{ + AttributeValue, Item, KeySchemaElement, KeyType, Projection, ProjectionType, + ReturnValuesOnConditionCheckFailure, TableKeyInfo, +}; +use extenddb_storage::error::StorageError; + +use super::super::{CONTENTION_DEADLINE, OwnedTransactWriteOp, TRANSIENT_RETRY_ATTEMPTS}; +use super::{finish, scratch_engine, table}; +use crate::MongoEngine; +use crate::data::{data_collection_name, document_to_item, item_to_document}; + +/// An engine on fresh databases with its contention deadline set to +/// `contention_deadline`. A deadline below the time the attempt budget takes +/// makes the deadline the bound a contended write reaches; one far above it +/// makes the attempt budget the bound. +async fn engine_with_deadline(contention_deadline: Duration) -> MongoEngine { + MongoEngine { + contention_deadline, + ..scratch_engine().await + } +} + +/// Key info for a table with a string hash key `pk`, with the GSI cache set +/// so that every write takes the transaction path. The catalog has no index +/// rows unless a test inserts one, so the writes write no index entries. +fn gsi_path_table(engine: &MongoEngine, table_id: &str) -> TableKeyInfo { + let key_info = table(engine, table_id, false); + engine.gsi_cache_set(table_id, true); + key_info +} + +fn item_with(pk: &str, w: &str) -> Item { + let mut item = Item::new(); + item.insert("pk".to_owned(), AttributeValue::S(pk.to_owned())); + item.insert("w".to_owned(), AttributeValue::S(w.to_owned())); + item +} + +/// The key of `item_with(pk, ...)`. +fn hash_key(pk: &str) -> Item { + let mut key = Item::new(); + key.insert("pk".to_owned(), AttributeValue::S(pk.to_owned())); + key +} + +fn stored_document(item: &Item, key_info: &TableKeyInfo) -> Document { + item_to_document(item, &key_info.key_schema, &key_info.attribute_definitions) + .expect("test item converts to a document") +} + +/// `SET w = :w` with its placeholder, for an UpdateItem under contention. +fn set_w(value: &str) -> (Vec, ExpressionMaps) { + let actions = vec![UpdateAction::Set { + path: vec![PathElement::Attribute("w".to_owned())], + value: Expr::Placeholder(":w".to_owned()), + }]; + let maps = ExpressionMaps::new( + std::collections::HashMap::new(), + std::collections::HashMap::from([(":w".to_owned(), AttributeValue::S(value.to_owned()))]), + ); + (actions, maps) +} + +/// Hold `pk` of the table inside a transaction that is never committed, so +/// every attempt of a contended write on that key conflicts. The caller +/// aborts the returned session when it is done. +async fn hold_item_in_a_transaction( + engine: &MongoEngine, + key_info: &TableKeyInfo, + pk: &str, +) -> mongodb::ClientSession { + let coll = engine + .data_db + .collection::(&data_collection_name(&key_info.table_id)); + coll.insert_one(stored_document(&item_with(pk, "held"), key_info)) + .await + .expect("insert the item to hold"); + let mut holder = engine.client.start_session().await.expect("session"); + holder + .start_transaction() + .with_options(engine.write_transaction_options()) + .await + .expect("start the holding transaction"); + coll.replace_one( + doc! { "pk": pk }, + stored_document(&item_with(pk, "rewritten"), key_info), + ) + .session(&mut holder) + .await + .expect("write the item inside the held transaction"); + holder +} + +/// The item stored under `pk`, read outside any transaction. +async fn stored_item(engine: &MongoEngine, key_info: &TableKeyInfo, pk: &str) -> Option { + engine + .data_db + .collection::(&data_collection_name(&key_info.table_id)) + .find_one(doc! { "pk": pk }) + .await + .expect("read") + .map(|d| document_to_item(&d).expect("stored document converts to an item")) +} + +/// Concurrent unconditional puts on one key of a table with a GSI all commit: +/// 32 writers put the same key 8 times each, every put returns Ok, and the +/// index holds the final item's entry only. On the service every one of these +/// puts answers 200; the backend reaches that through its retry budget. +/// +/// The table has a GSI row in the catalog, so `sync_indexes_in_session` +/// writes an index entry per put and keeps the GSI cache at `true`. Without +/// the row the first committed put flips the cache to `false`, the other +/// writers take the sessionless path, and a sessionless findAndModify on a +/// document an open transaction has written waits for that transaction; with +/// 32 writers starting transactions without pause the run stalled for +/// minutes. +#[tokio::test(flavor = "multi_thread", worker_threads = 8)] +#[ignore = "needs a MongoDB replica set in EXTENDDB_TEST_MONGODB_URI"] +async fn live_concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit() { + const WRITERS: usize = 32; + const PUTS: usize = 8; + let engine = Arc::new(engine_with_deadline(CONTENTION_DEADLINE).await); + let key_info = Arc::new(gsi_path_table(&engine, "hot-key")); + let gsi_key_schema = vec![KeySchemaElement { + attribute_name: "gk".to_owned(), + key_type: KeyType::Hash, + }]; + let projection = Projection { + projection_type: ProjectionType::All, + non_key_attributes: None, + }; + engine + .catalog_db + .collection::("indexes") + .insert_one(doc! { + "_id": { "table_id": "hot-key", "index_name": "gk-index" }, + "index_id": "hot-key-gk-index", + "index_type": "GSI", + "key_schema": bson::to_bson(&gsi_key_schema).expect("key schema"), + "projection": bson::to_bson(&projection).expect("projection"), + "index_status": "ACTIVE", + }) + .await + .expect("insert the GSI catalog row"); + let start = Arc::new(tokio::sync::Barrier::new(WRITERS)); + let mut tasks = Vec::with_capacity(WRITERS); + for w in 0..WRITERS { + let (engine, key_info, start) = (engine.clone(), key_info.clone(), start.clone()); + tasks.push(tokio::spawn(async move { + let maps = ExpressionMaps::default(); + start.wait().await; + let mut failures = Vec::new(); + for n in 0..PUTS { + let mut item = item_with("k1", &format!("w{w}-{n}")); + item.insert("gk".to_owned(), AttributeValue::S(format!("g-w{w}-{n}"))); + if let Err(e) = engine + .put_item_impl(&key_info, item, false, None, &maps, None) + .await + { + failures.push(format!("{e:?}")); + } + } + failures + })); + } + let mut failures = Vec::new(); + for task in tasks { + failures.extend(task.await.expect("writer task")); + } + assert!( + failures.is_empty(), + "{} of {} puts failed: {:?}", + failures.len(), + WRITERS * PUTS, + &failures[..failures.len().min(3)] + ); + let coll = engine + .data_db + .collection::(&data_collection_name("hot-key")); + assert_eq!(coll.count_documents(doc! {}).await.expect("count"), 1); + let stored = stored_item(&engine, &key_info, "k1") + .await + .expect("the item"); + let idx_coll = engine + .data_db + .collection::(&data_collection_name("hot-key-gk-index")); + use futures::TryStreamExt as _; + let entries: Vec = idx_coll + .find(doc! {}) + .await + .expect("read the index") + .try_collect() + .await + .expect("collect the index"); + assert_eq!(entries.len(), 1, "index entries: {entries:?}"); + let entry = document_to_item(&entries[0]).expect("index entry converts to an item"); + assert_eq!( + entry.get("gk"), + stored.get("gk"), + "index entry {entry:?} for {stored:?}" + ); + let engine = Arc::try_unwrap(engine).ok().expect("all writers done"); + finish(engine).await; +} + +/// Each single-item write that reaches the engine's contention deadline gives +/// up with the transient error the engine answers as HTTP 503 +/// `ServiceUnavailable`, and the message names the operation and the deadline +/// that passed. +/// +/// The deadline is 2 s here, below the 9.5 to 12.7 s the 1,000 attempts take +/// against a document a transaction holds, so the deadline is the bound each +/// of the three writes reaches, with no race between the two. The attempt +/// budget is the bound in +/// `live_unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error`. +#[tokio::test] +#[ignore = "needs a MongoDB replica set in EXTENDDB_TEST_MONGODB_URI"] +async fn live_a_single_item_write_that_reaches_the_deadline_answers_a_transient_error() { + let deadline = Duration::from_secs(2); + let engine = engine_with_deadline(deadline).await; + let maps = ExpressionMaps::default(); + let key_info = gsi_path_table(&engine, "deadline-key"); + let mut holder = hold_item_in_a_transaction(&engine, &key_info, "k1").await; + let key = hash_key("k1"); + let (actions, update_maps) = set_w("this-update"); + + for operation in ["PutItem", "DeleteItem", "UpdateItem"] { + let started = Instant::now(); + let res = match operation { + "PutItem" => engine + .put_item_impl( + &key_info, + item_with("k1", "this-put"), + false, + None, + &maps, + None, + ) + .await + .map(|_| ()), + "DeleteItem" => engine + .delete_item_impl(&key_info, &key, false, None, &maps, None) + .await + .map(|_| ()), + _ => engine + .update_item_impl( + &key_info, + &key, + &actions, + false, + false, + None, + &update_maps, + None, + ) + .await + .map(|_| ()), + }; + let elapsed = started.elapsed(); + match res { + Err(StorageError::Transient(msg)) => { + assert_eq!( + msg, + format!( + "{operation}: the write lost to concurrent writers for 2 seconds; \ + retry the request" + ) + ); + println!("{operation} gave up on the deadline in {elapsed:?}: {msg}"); + } + other => panic!("{operation}: want the transient deadline error, got: {other:?}"), + } + assert!( + elapsed >= deadline, + "{operation} answered in {elapsed:?}, before the deadline" + ); + assert!( + elapsed < deadline + Duration::from_secs(5), + "{operation} took {elapsed:?}, so a bound other than the deadline ended it" + ); + } + + let _ = holder.abort_transaction().await; + // No give-up wrote anything: the held item is as it was inserted. + assert_eq!( + stored_item(&engine, &key_info, "k1").await, + Some(item_with("k1", "held")) + ); + finish(engine).await; +} + +/// A `TransactWriteItems` that reaches the deadline answers a canceled +/// transaction with a `TransactionConflict` reason per item, which is what +/// live DynamoDB answers a transaction that loses to contention (107 of 160 +/// calls at 16 writers on one key). It does not answer the HTTP 503 the +/// single-item writes answer. +#[tokio::test] +#[ignore = "needs a MongoDB replica set in EXTENDDB_TEST_MONGODB_URI"] +async fn live_a_transaction_that_reaches_the_deadline_answers_a_canceled_transaction() { + let deadline = Duration::from_secs(2); + let engine = engine_with_deadline(deadline).await; + let key_info = gsi_path_table(&engine, "deadline-transact"); + let mut holder = hold_item_in_a_transaction(&engine, &key_info, "k1").await; + + // Two operations, one on the held key and one on a free key, so the + // transaction conflicts and the answer carries one reason per operation + // rather than one for the contended item. + let ops = vec![ + OwnedTransactWriteOp::Put { + key_info: key_info.clone(), + item: item_with("k1", "this-transaction"), + condition: None, + maps: ExpressionMaps::default(), + return_values_on_ccf: ReturnValuesOnConditionCheckFailure::None, + stream: None, + }, + OwnedTransactWriteOp::Put { + key_info: key_info.clone(), + item: item_with("k2", "this-transaction"), + condition: None, + maps: ExpressionMaps::default(), + return_values_on_ccf: ReturnValuesOnConditionCheckFailure::None, + stream: None, + }, + ]; + + let started = Instant::now(); + let res = engine.transact_write_items_impl(&ops, None).await; + let elapsed = started.elapsed(); + let _ = holder.abort_transaction().await; + + match res { + Err(StorageError::TransactionCanceled(reasons)) => { + assert_eq!(reasons.len(), ops.len(), "one reason per operation"); + for reason in &reasons { + assert_eq!(reason.code, "TransactionConflict"); + assert_eq!( + reason.message.as_deref(), + Some("Transaction is ongoing for the item") + ); + } + println!("the transaction was canceled in {elapsed:?}: {reasons:?}"); + } + other => panic!("want a canceled transaction, got: {other:?}"), + } + assert!( + elapsed >= deadline, + "the transaction answered in {elapsed:?}, before the deadline" + ); + assert!( + elapsed < deadline + Duration::from_secs(5), + "took {elapsed:?}, so a bound other than the deadline ended it" + ); + + // Neither operation of the canceled transaction was applied. + assert_eq!( + stored_item(&engine, &key_info, "k1").await, + Some(item_with("k1", "held")) + ); + assert_eq!( + stored_item(&engine, &key_info, "k2").await, + None, + "the free key of the canceled transaction must not be written" + ); + finish(engine).await; +} + +/// An unconditional PutItem that spends its whole attempt budget on +/// contention gives up with the transient error naming the attempts, which +/// the engine answers as HTTP 503. +/// +/// The deadline is set far above the time the budget takes (9.5 to 12.7 s +/// against a document a transaction holds, over nine runs on a shared host), +/// so the attempt budget is the bound this test reaches. With the production +/// deadline of 10 s either bound can fire first, which is why the deadline is +/// the test's input rather than its race. +#[tokio::test] +#[ignore = "needs a MongoDB replica set in EXTENDDB_TEST_MONGODB_URI"] +async fn live_unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error() { + let engine = engine_with_deadline(Duration::from_secs(600)).await; + let maps = ExpressionMaps::default(); + let key_info = gsi_path_table(&engine, "held-key"); + let mut holder = hold_item_in_a_transaction(&engine, &key_info, "k1").await; + + let started = Instant::now(); + let res = engine + .put_item_impl( + &key_info, + item_with("k1", "this-put"), + false, + None, + &maps, + None, + ) + .await; + let elapsed = started.elapsed(); + let _ = holder.abort_transaction().await; + + match res { + Err(StorageError::Transient(msg)) => { + assert_eq!( + msg, + format!( + "PutItem: the write lost {TRANSIENT_RETRY_ATTEMPTS} attempts to concurrent \ + writers; retry the request" + ) + ); + println!("the put spent its attempt budget in {elapsed:?}: {msg}"); + } + other => panic!("want the transient spent-budget error, got: {other:?}"), + } + // The 1,000 attempts take 9.5 to 12.7 s on a shared host, and the sleeps + // alone come to 12.8 s at worst, so the bound here is loose: what it rules + // out is a budget that no longer ends the loop at all. + assert!(elapsed < Duration::from_secs(120), "took {elapsed:?}"); + assert_eq!( + stored_item(&engine, &key_info, "k1").await, + Some(item_with("k1", "held")) + ); + finish(engine).await; +} diff --git a/crates/storage-mongodb/src/lib.rs b/crates/storage-mongodb/src/lib.rs index 0e7351be4..53e275885 100644 --- a/crates/storage-mongodb/src/lib.rs +++ b/crates/storage-mongodb/src/lib.rs @@ -636,8 +636,31 @@ impl MongoEngine { let catalog_db = catalog_database(&client); let data_db = client.database("extenddb_data"); + Ok(Self::from_parts( + client, + catalog_db, + data_db, + region, + tx_read_concern, + pool_size, + )) + } + + /// The engine on `catalog_db` and `data_db` of `client`, with every other + /// field at the value `new` gives it for a pool of `pool_size` + /// connections. `new` and [`MongoEngine::for_test`] both build the engine + /// here, so a field added to `MongoEngine` is set in this function and in + /// no test. + fn from_parts( + client: mongodb::Client, + catalog_db: mongodb::Database, + data_db: mongodb::Database, + region: &str, + tx_read_concern: mongodb::options::ReadConcern, + pool_size: u32, + ) -> Self { let restore_server_slot_count = backup_engine::restore_server_slot_count(pool_size); - Ok(Self { + Self { client, catalog_db, data_db, @@ -649,7 +672,28 @@ impl MongoEngine { restore_server_slots: Arc::new(tokio::sync::Semaphore::new(restore_server_slot_count)), restore_account_slots: backup_engine::RestoreAccountSlots::default(), contention_deadline: data_engine::CONTENTION_DEADLINE, - }) + } + } + + /// An engine for a test, on `catalog_db` and `data_db` of `client`, in + /// region `us-east-1` with snapshot read concern, and every other field + /// as `new` sets it for the driver's default pool of 10 connections. A + /// test that needs another value for a field sets it with struct update + /// syntax on the result. + #[cfg(test)] + pub(crate) fn for_test( + client: &mongodb::Client, + catalog_db: mongodb::Database, + data_db: mongodb::Database, + ) -> Self { + Self::from_parts( + client.clone(), + catalog_db, + data_db, + "us-east-1", + mongodb::options::ReadConcern::snapshot(), + 10, + ) } /// Read concern to use for a newly-opened multi-document transaction. diff --git a/crates/storage-mongodb/src/snapshot_engine.rs b/crates/storage-mongodb/src/snapshot_engine.rs index fc8eefa48..296224d04 100644 --- a/crates/storage-mongodb/src/snapshot_engine.rs +++ b/crates/storage-mongodb/src/snapshot_engine.rs @@ -470,18 +470,9 @@ mod tests { .insert_one(doc! { "_id": "control_plane_delay_seconds", "value": "0" }) .await .expect("setting"); - let slots = crate::backup_engine::restore_server_slot_count(10); let engine = MongoEngine { - client: client.clone(), - catalog_db, - data_db: client.database(&names[1]), - region: "us-east-1".to_owned(), - gsi_cache: Arc::default(), tx_read_concern: ReadConcern::majority(), - backup_liveness: crate::backup_engine::BackupLiveness::default(), - restore_server_slot_count: slots, - restore_server_slots: Arc::new(tokio::sync::Semaphore::new(slots)), - restore_account_slots: crate::backup_engine::RestoreAccountSlots::default(), + ..MongoEngine::for_test(&client, catalog_db, client.database(&names[1])) }; let worker = tokio::spawn(crate::ttl_worker::gsi_backfill_worker( Arc::new(engine.clone()), diff --git a/crates/storage-mongodb/src/stream_records_live_tests.rs b/crates/storage-mongodb/src/stream_records_live_tests.rs index bf769836a..07e3b70e2 100644 --- a/crates/storage-mongodb/src/stream_records_live_tests.rs +++ b/crates/storage-mongodb/src/stream_records_live_tests.rs @@ -12,8 +12,6 @@ //! Each test uses its own catalog and data databases and drops them at the //! end. Rows are inserted directly in the shape the engine writes them. -use std::sync::Arc; - use extenddb_storage::StreamEngine; use extenddb_storage::error::StorageError; use extenddb_storage::util::stream_arn; @@ -42,18 +40,13 @@ impl Fixture { async fn new() -> Self { let tag = uuid::Uuid::new_v4().simple().to_string(); let client = mongodb::Client::with_uri_str(uri()).await.expect("client"); - let slots = crate::backup_engine::restore_server_slot_count(10); let engine = MongoEngine { - client: client.clone(), - catalog_db: client.database(&format!("s{}_cat", &tag[..12])), - data_db: client.database(&format!("s{}_data", &tag[..12])), region: REGION.to_owned(), - gsi_cache: Arc::default(), - tx_read_concern: mongodb::options::ReadConcern::snapshot(), - backup_liveness: crate::backup_engine::BackupLiveness::default(), - restore_server_slot_count: slots, - restore_server_slots: Arc::new(tokio::sync::Semaphore::new(slots)), - restore_account_slots: crate::backup_engine::RestoreAccountSlots::default(), + ..MongoEngine::for_test( + &client, + client.database(&format!("s{}_cat", &tag[..12])), + client.database(&format!("s{}_data", &tag[..12])), + ) }; Self { engine } } From 6c36c14a0f2273548344c69f95722a7ced9f7dae Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:25:34 +0000 Subject: [PATCH 12/44] fix(mongodb): give the requests of one BatchWriteItem one contention deadline BatchWriteItem calls put_item or delete_item once per request, and each call started its own CONTENTION_DEADLINE, so a batch of contended requests could keep the client waiting 10 s per request. When a later request gave up with the transient error, the handler failed the whole batch with HTTP 503 after the earlier requests had committed. The SDKs retry a 503, so the retry sent those requests again, and a put or delete that another client had overwritten in between was applied a second time. The new extenddb_storage::request_clock module carries the start of an API request in a task-local value. The BatchWriteItem handler runs inside request_clock::scope, and the MongoDB contention loops measure the deadline from that start (contention_clock_start), so the requests of one batch share one deadline. The first attempt of every request still runs, so a request that comes after the deadline is tried once. Outside a scope a loop measures from the start of its own call, as before. A request whose write fails with StorageError::Transient or StorageError::TransactionConflict is returned in UnprocessedItems, and the requests after it are still tried. On MongoDB the first is the contention give-up and the second a spent attempt budget of DeleteItem; neither applied the write. The batch answers the error only when none of its requests was applied, so a client that retries it resends nothing that took effect. Every other error still fails the batch. The capacity of a request returned unprocessed is still counted. Amazon DynamoDB answers a batch this way. Measured in us-east-1 with client retries off while TransactWriteItems calls kept updating one item: 46 of 80 batches [put held, put free] and 53 of 80 batches [put free, put held] answered 200 with the held item's request in UnprocessedItems and the free item written, each reporting 2 capacity units, the units of both requests. 54 of 80 batches with only the held request answered 500 InternalServerError, and none answered 200 with its request unprocessed. The handler change applies to every backend: a batch put or delete that returns one of these two errors on another backend is now returned in UnprocessedItems where it failed the batch before. Tests: - request_clock: a call sees the start of the scope it runs in, and an inner scope replaces it until it ends. - the_contention_clock_starts_with_the_request_scope: the MongoDB loops read the scope's start, and their own start outside a scope. - live_a_write_in_a_request_scope_measures_the_deadline_from_the_scope_start: in a scope that started 5 s ago with a 2 s deadline, a put of a held item gives up after one attempt and a put of a free item commits. - tests/test_mongodb_batch_write_contention.py: the shape from the review, [delete A, put B] with B held past the deadline and another client putting A at 5 s, now answers 200 with only the put of B unprocessed and the other client's A kept; and in a batch whose first request is held for 6 s and whose second is held past the deadline, the second request gives up when the batch's deadline passes and is returned unprocessed. --- crates/engine/src/batch_write_item.rs | 111 +++++++++- crates/storage-mongodb/src/data_engine.rs | 38 +++- .../src/data_engine_live_tests_contention.rs | 71 ++++++ crates/storage/src/lib.rs | 1 + crates/storage/src/request_clock.rs | 68 ++++++ tests/test_mongodb_batch_write_contention.py | 207 ++++++++++++++++++ 6 files changed, 485 insertions(+), 11 deletions(-) create mode 100644 crates/storage/src/request_clock.rs create mode 100644 tests/test_mongodb_batch_write_contention.py diff --git a/crates/engine/src/batch_write_item.rs b/crates/engine/src/batch_write_item.rs index b0381c7a7..62bc33242 100755 --- a/crates/engine/src/batch_write_item.rs +++ b/crates/engine/src/batch_write_item.rs @@ -4,6 +4,7 @@ //! `BatchWriteItem` operation handler. use std::collections::HashMap; +use std::time::Instant; use serde_json::Value; @@ -17,6 +18,7 @@ use extenddb_core::validation::{ validate_attribute_name_sizes, validate_batch_item_keys, validate_batch_key_only, validate_item_nesting_depth, validate_item_size, validate_key_sizes, }; +use extenddb_storage::error::StorageError; use crate::OperationContext; use crate::capacity_helpers; @@ -32,13 +34,28 @@ use crate::{DispatchMetrics, DispatchResult}; /// executed individually — no cross-item conditions. `DynamoDB` limits: max 25 /// items total, max 16 MB request size, max 400 KB per item. /// +/// A write the backend cannot apply now (see `is_deferrable`) does not stop +/// the batch: its request is returned in UnprocessedItems and the requests +/// after it are still tried. +/// /// # Errors /// /// Returns `DynamoDbError` for validation failures, missing tables, or storage errors. +/// A write the backend cannot apply now fails the batch only when no request +/// of the batch was applied. pub async fn handle_batch_write_item( body: Value, ctx: &OperationContext, ) -> Result { + // The storage calls of the batch run in one request clock scope, so a + // backend that bounds how long a contended write keeps retrying measures + // one bound for the whole batch instead of one per write request. + extenddb_storage::request_clock::scope(Instant::now(), write_batch(body, ctx)).await +} + +/// The body of [`handle_batch_write_item`], which runs it inside the batch's +/// request clock scope. +async fn write_batch(body: Value, ctx: &OperationContext) -> Result { let input: BatchWriteItemInput = serde_json::from_value(body).map_err(crate::deserialize_error)?; @@ -78,6 +95,12 @@ pub async fn handle_batch_write_item( input.return_consumed_capacity == extenddb_core::types::ReturnConsumedCapacity::Indexes; let mut per_table_gsi: HashMap> = HashMap::new(); let mut per_table_lsi: HashMap> = HashMap::new(); + // The write requests the backend could not apply now (see `is_deferrable`), + // returned to the client in UnprocessedItems, and the first such error, + // which the batch answers when none of its requests was applied. + let mut unprocessed: HashMap> = HashMap::new(); + let mut first_deferral: Option = None; + let mut applied = 0usize; for (table_name, reqs) in &input.request_items { let key_info = ctx @@ -124,7 +147,7 @@ pub async fn handle_batch_write_item( let item_wcu = capacity_helpers::write_capacity_units(item_size_bytes(&put.item)); total_wcu += item_wcu; *per_table_wcu.entry(table_name.clone()).or_default() += item_wcu; - let old_item = ctx + let written = ctx .storage .put_item( &key_info, @@ -134,8 +157,25 @@ pub async fn handle_batch_write_item( &empty_maps, stream.as_ref(), ) - .await - .map_err(storage_err_to_dynamo)?; + .await; + let old_item = match written { + Ok(old_item) => old_item, + Err(e) if is_deferrable(&e) => { + tracing::warn!( + table = %table_name, + error = %e, + "BatchWriteItem: a put request was not applied; it is returned in UnprocessedItems" + ); + unprocessed + .entry(table_name.clone()) + .or_default() + .push(wr.clone()); + first_deferral.get_or_insert(e); + continue; + } + Err(e) => return Err(storage_err_to_dynamo(e)), + }; + applied += 1; if want_index_cc { let (gsi, lsi) = capacity_helpers::index_write_units( old_item.as_ref(), @@ -178,7 +218,7 @@ pub async fn handle_batch_write_item( let item_wcu = capacity_helpers::write_capacity_units(item_size_bytes(&del.key)); total_wcu += item_wcu; *per_table_wcu.entry(table_name.clone()).or_default() += item_wcu; - let old_item = ctx + let written = ctx .storage .delete_item( &key_info, @@ -188,8 +228,25 @@ pub async fn handle_batch_write_item( &empty_maps, stream.as_ref(), ) - .await - .map_err(storage_err_to_dynamo)?; + .await; + let old_item = match written { + Ok(old_item) => old_item, + Err(e) if is_deferrable(&e) => { + tracing::warn!( + table = %table_name, + error = %e, + "BatchWriteItem: a delete request was not applied; it is returned in UnprocessedItems" + ); + unprocessed + .entry(table_name.clone()) + .or_default() + .push(wr.clone()); + first_deferral.get_or_insert(e); + continue; + } + Err(e) => return Err(storage_err_to_dynamo(e)), + }; + applied += 1; if want_index_cc { let (gsi, lsi) = capacity_helpers::index_write_units( old_item.as_ref(), @@ -210,6 +267,14 @@ pub async fn handle_batch_write_item( } } + // No request of the batch was applied, so answering an error is safe: a + // client that retries it resends requests none of which took effect. + if applied == 0 + && let Some(e) = first_deferral + { + return Err(storage_err_to_dynamo(e)); + } + // With INDEXES, each per-table entry carries the arms the batch actually // touched: base table plus every affected index, aggregate = their sum // (pinned by the ground-truth runs of 2026-08-24: an indexed put and a @@ -241,7 +306,7 @@ pub async fn handle_batch_write_item( let wcu = total_wcu; let output = BatchWriteItemOutput { - unprocessed_items: HashMap::new(), + unprocessed_items: unprocessed, consumed_capacity, item_collection_metrics: if all_icm.is_empty() { None @@ -259,6 +324,38 @@ pub async fn handle_batch_write_item( }) } +/// Whether a failed write leaves its request for the client to retry, in +/// UnprocessedItems, instead of failing the batch. +/// +/// Neither error applied the write. `Transient` is a write the backend could +/// not apply now and expects to apply on a retry; on MongoDB a write that kept +/// losing to concurrent writers until its contention deadline or its attempt +/// budget ran out answers it. `TransactionConflict` is a write that lost to a +/// transaction holding the item. Every other error still fails the batch. +/// +/// Amazon DynamoDB answers a request it cannot apply this way. Measured in +/// us-east-1 on 2026-11-03 with client retries off, while TransactWriteItems +/// calls kept updating one item: of 80 batches that put the held item and then +/// a free item, 46 answered 200 with the held item's request in +/// UnprocessedItems and the free item written, and 53 of 80 batches with the +/// two requests the other way round did the same. Each of those 99 batches +/// reported 2 write capacity units, the units of both requests, which is why +/// the capacity of a request returned unprocessed is still counted here. A +/// batch whose only request was on the held item answered 500 +/// InternalServerError in 54 of 80 calls, never 200 with its request +/// unprocessed. +/// +/// Failing the batch after an earlier request was applied is what this +/// avoids: an SDK that retries the error resends the whole batch, so the +/// requests already applied are applied again, after any write another +/// client made to the same items in between. +fn is_deferrable(e: &StorageError) -> bool { + matches!( + e, + StorageError::Transient(_) | StorageError::TransactionConflict(_) + ) +} + /// Validate that no two write requests in the same table target the same key. /// /// `DynamoDB` returns `ValidationException` for duplicate keys within a single diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 4c46b7491..919b283c4 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -468,7 +468,7 @@ impl MongoEngine { let tx_options = self.write_transaction_options(); let mut lost_create_races = 0; - let contention_started = std::time::Instant::now(); + let contention_started = contention_clock_start(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { @@ -726,7 +726,7 @@ impl MongoEngine { .map_err(|e| StorageError::Internal(e.to_string()))?; let tx_options = self.write_transaction_options(); - let contention_started = std::time::Instant::now(); + let contention_started = contention_clock_start(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { @@ -959,7 +959,7 @@ impl MongoEngine { .map_err(|e| StorageError::Internal(e.to_string()))?; let tx_options = self.write_transaction_options(); - let contention_started = std::time::Instant::now(); + let contention_started = contention_clock_start(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { @@ -2291,7 +2291,7 @@ impl MongoEngine { ) }); - let contention_started = std::time::Instant::now(); + let contention_started = contention_clock_start(); for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { // A transaction that reaches the deadline stops retrying and @@ -3856,6 +3856,18 @@ fn contention_deadline_passed( attempt > 0 && started.elapsed() >= deadline } +/// The instant a contention retry loop measures the engine's deadline from. +/// +/// A caller that makes several storage calls for one API request runs them +/// inside [`extenddb_storage::request_clock::scope`], as `BatchWriteItem` +/// does, and then every loop of that request measures from the start the +/// scope carries, so the request's writes share one deadline. Outside a +/// scope, which is where a single-item request and the TTL worker call from, +/// a loop measures from the start of its own call. +fn contention_clock_start() -> std::time::Instant { + extenddb_storage::request_clock::started().unwrap_or_else(std::time::Instant::now) +} + /// The error for a write that stopped retrying because the engine's /// contention deadline passed while it was losing to concurrent writers. /// `operation` names the API operation and `deadline` is the deadline that @@ -4433,6 +4445,24 @@ mod tests { )); } + /// A loop measures the deadline from the start its request clock scope + /// carries, so the writes of one batch share one deadline, and from its + /// own start outside a scope. + #[tokio::test] + async fn the_contention_clock_starts_with_the_request_scope() { + let before = std::time::Instant::now(); + let outside = contention_clock_start(); + assert!(outside >= before, "outside a scope the clock starts now"); + let batch_started = before + .checked_sub(std::time::Duration::from_secs(30)) + .expect("the process clock reaches 30 seconds back"); + let inside = extenddb_storage::request_clock::scope(batch_started, async { + (contention_clock_start(), contention_clock_start()) + }) + .await; + assert_eq!(inside, (batch_started, batch_started)); + } + /// The give-up on the deadline is the transient error the engine answers /// as HTTP 503, and its message names the operation and the wait. #[test] diff --git a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs index d02715a92..dcef72361 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs @@ -306,6 +306,77 @@ async fn live_a_single_item_write_that_reaches_the_deadline_answers_a_transient_ finish(engine).await; } +/// A write inside a request clock scope measures the deadline from the +/// scope's start, which is how the write requests of one `BatchWriteItem` +/// share one deadline. +/// +/// The scope here started 5 s ago and the deadline is 2 s, so a put of the +/// held item makes its first attempt and gives up before a second one, +/// instead of retrying for 2 s from its own start. A put of a free item in the +/// same scope commits, because the first attempt of a write always runs. +#[tokio::test] +#[ignore = "needs a MongoDB replica set in EXTENDDB_TEST_MONGODB_URI"] +async fn live_a_write_in_a_request_scope_measures_the_deadline_from_the_scope_start() { + let deadline = Duration::from_secs(2); + let engine = engine_with_deadline(deadline).await; + let maps = ExpressionMaps::default(); + let key_info = gsi_path_table(&engine, "deadline-scope"); + let mut holder = hold_item_in_a_transaction(&engine, &key_info, "k1").await; + let request_started = Instant::now() + .checked_sub(Duration::from_secs(5)) + .expect("the process clock reaches five seconds back"); + + let started = Instant::now(); + let (held, free) = extenddb_storage::request_clock::scope(request_started, async { + let held = engine + .put_item_impl( + &key_info, + item_with("k1", "this-put"), + false, + None, + &maps, + None, + ) + .await; + let free = engine + .put_item_impl( + &key_info, + item_with("k2", "this-put"), + false, + None, + &maps, + None, + ) + .await; + (held, free) + }) + .await; + let elapsed = started.elapsed(); + let _ = holder.abort_transaction().await; + + match held { + Err(StorageError::Transient(msg)) => assert_eq!( + msg, + "PutItem: the write lost to concurrent writers for 2 seconds; retry the request" + ), + other => panic!("want the transient deadline error, got: {other:?}"), + } + assert!(free.is_ok(), "the put of the free item: {free:?}"); + assert!( + elapsed < Duration::from_secs(1), + "the two puts took {elapsed:?}, so the held put measured a deadline of its own" + ); + assert_eq!( + stored_item(&engine, &key_info, "k1").await, + Some(item_with("k1", "held")) + ); + assert_eq!( + stored_item(&engine, &key_info, "k2").await, + Some(item_with("k2", "this-put")) + ); + finish(engine).await; +} + /// A `TransactWriteItems` that reaches the deadline answers a canceled /// transaction with a `TransactionConflict` reason per item, which is what /// live DynamoDB answers a transaction that loses to contention (107 of 160 diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 27cef1f70..830af4f65 100755 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -21,6 +21,7 @@ pub mod management_store_factory; pub mod migrations; pub mod operations; pub mod policy_migration; +pub mod request_clock; pub mod server_components; pub mod settings_store; pub mod snapshot; diff --git a/crates/storage/src/request_clock.rs b/crates/storage/src/request_clock.rs new file mode 100644 index 000000000..675ef973c --- /dev/null +++ b/crates/storage/src/request_clock.rs @@ -0,0 +1,68 @@ +// Copyright 2026 ExtendDB contributors +// SPDX-License-Identifier: Apache-2.0 + +//! The start of the API request that a storage call serves. +//! +//! A backend that stops retrying a contended write after a deadline needs to +//! know when the request began. A caller that makes several storage calls for +//! one request runs them inside [`scope`], and the backend measures the +//! deadline from the instant the scope carries, so the calls share one +//! deadline instead of each getting a deadline of its own. `BatchWriteItem` +//! does this for its write requests. Outside a scope [`started`] is `None` +//! and the backend measures from the start of each call, which is what a +//! single-item request and a background worker get. + +use std::future::Future; +use std::time::Instant; + +tokio::task_local! { + static REQUEST_STARTED: Instant; +} + +/// Run `f` with `started` as the start of the request its storage calls +/// serve. +pub async fn scope(started: Instant, f: F) -> F::Output { + REQUEST_STARTED.scope(started, f).await +} + +/// The start of the request the running storage call serves, when its caller +/// set one with [`scope`]. +#[must_use] +pub fn started() -> Option { + REQUEST_STARTED.try_with(|started| *started).ok() +} + +#[cfg(test)] +mod tests { + use std::time::Duration; + + use super::*; + + #[tokio::test] + async fn a_call_sees_the_start_of_the_scope_it_runs_in() { + assert_eq!(started(), None, "no scope, no request start"); + let earlier = Instant::now() + .checked_sub(Duration::from_secs(5)) + .expect("the process clock reaches five seconds back"); + let seen = scope(earlier, async { + // A storage call awaited inside the scope sees the same start. + let inner = async { started() }.await; + (started(), inner) + }) + .await; + assert_eq!(seen, (Some(earlier), Some(earlier))); + assert_eq!(started(), None, "the start does not outlive its scope"); + } + + #[tokio::test] + async fn an_inner_scope_replaces_the_start_until_it_ends() { + let outer = Instant::now(); + let inner = outer + Duration::from_secs(1); + let seen = scope(outer, async { + let in_inner = scope(inner, async { started() }).await; + (in_inner, started()) + }) + .await; + assert_eq!(seen, (Some(inner), Some(outer))); + } +} diff --git a/tests/test_mongodb_batch_write_contention.py b/tests/test_mongodb_batch_write_contention.py new file mode 100644 index 000000000..73f50633f --- /dev/null +++ b/tests/test_mongodb_batch_write_contention.py @@ -0,0 +1,207 @@ +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 + +"""BatchWriteItem on the MongoDB backend when one of its items stays contended. + +A mongosh session holds an item inside a MongoDB transaction it does not +commit, so every attempt of a write to that item conflicts until the session +aborts. The server stops retrying a contended write after CONTENTION_DEADLINE +(10 s), measured once for the whole batch. A request the server gives up on is +returned in UnprocessedItems and the batch answers 200, which is how Amazon +DynamoDB answers a batch with a request it cannot apply: measured with +TransactWriteItems calls holding one item, the held item's request came back +in UnprocessedItems and the other request of the batch was applied. The +requests already applied are not applied again. + +The holds run through mongosh in the container the MongoDB test runner names +in EXTENDDB_TEST_MONGODB_CONTAINER, so these tests run only under +devtools/run-mongodb-tests. +""" + +from __future__ import annotations + +import json +import os +import subprocess +import threading +import time +import uuid + +import boto3 +import pytest +from botocore.config import Config + +from conftest import scoped_table + +# The server's contention deadline, and the time past it within which a batch +# that reached it answers. A hold lasts longer than both together, so the +# deadline, not the end of a hold, is what ends a contended write. +DEADLINE = 10 +SLACK = 3 +HOLD = DEADLINE + SLACK + 2 + + +@pytest.fixture() +def mongodb_container() -> str: + container = os.environ.get("EXTENDDB_TEST_MONGODB_CONTAINER", "").strip() + if not container: + pytest.skip("requires devtools/run-mongodb-tests") + return container + + +def _client(endpoint_url, retries: bool): + """A client of its own; with `retries` it keeps the SDK's default retry mode.""" + kwargs: dict = { + "service_name": "dynamodb", + "region_name": os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), + } + if not retries: + kwargs["config"] = Config(retries={"total_max_attempts": 1, "mode": "standard"}) + if endpoint_url: + kwargs["endpoint_url"] = endpoint_url + if endpoint_url.startswith("https://"): + kwargs["verify"] = False + return boto3.client(**kwargs) + + +@pytest.fixture(scope="module") +def gsi_table(dynamodb_client): + """A table with a GSI, so every write to it runs in a MongoDB transaction.""" + with scoped_table( + dynamodb_client, + attribute_definitions=[ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "gk", "AttributeType": "S"}, + ], + GlobalSecondaryIndexes=[ + { + "IndexName": "gk-index", + "KeySchema": [{"AttributeName": "gk", "KeyType": "HASH"}], + "Projection": {"ProjectionType": "ALL"}, + } + ], + ) as name: + yield name + + +def _item(pk: str, w: str) -> dict: + return {"pk": {"S": pk}, "gk": {"S": f"g-{pk}"}, "w": {"S": w}} + + +def _w(client, table: str, pk: str) -> str | None: + item = client.get_item(TableName=table, Key={"pk": {"S": pk}}, ConsistentRead=True).get( + "Item" + ) + return item["w"]["S"] if item else None + + +class Hold: + """Hold the item at `pk` inside a MongoDB transaction for `seconds`, then abort it.""" + + def __init__(self, container: str, table: str, pk: str, seconds: float): + script = f""" +const catalog = db.getSiblingDB("extenddb_catalog"); +const table = catalog.tables.findOne({{"_id.table_name": {json.dumps(table)}}}); +const session = db.getMongo().startSession(); +session.startTransaction({{readConcern: {{level: "snapshot"}}, writeConcern: {{w: "majority"}}}}); +const coll = session.getDatabase("extenddb_data").getCollection("_ddb_" + table.table_id); +const res = coll.updateOne({{pk: {json.dumps(pk)}}}, {{$set: {{"item_data.held": {{S: "yes"}}}}}}); +print(JSON.stringify({{held: res.modifiedCount}})); +sleep({int(seconds * 1000)}); +session.abortTransaction(); +print(JSON.stringify({{released: true}})); +""" + self.proc = subprocess.Popen( + ["docker", "exec", container, "mongosh", "--quiet", "--eval", script], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + line = self.proc.stdout.readline() + if json.loads(line or "{}").get("held") != 1: + self.proc.kill() + pytest.fail(f"could not hold {pk}: {line!r} {self.proc.stderr.read()!r}") + + def wait(self) -> None: + out, err = self.proc.communicate(timeout=HOLD + 30) + assert self.proc.returncode == 0, err + assert '"released":true' in out, out + + +def test_a_contended_request_is_returned_unprocessed_and_the_rest_is_not_resent( + dynamodb_client, endpoint_url, mongodb_container, gsi_table +): + """The shape of a batch whose resend lost another client's write. + + The batch deletes A and puts B, which is held past the deadline. Five + seconds in, another client puts A again. The batch client keeps the SDK's + default retry mode, which resends a whole batch that answered 503. The + batch must answer 200 with only the put of B unprocessed, so the delete of + A is not sent again and the other client's A stays. + """ + a, b = f"a-{uuid.uuid4().hex[:8]}", f"b-{uuid.uuid4().hex[:8]}" + dynamodb_client.put_item(TableName=gsi_table, Item=_item(a, "v0")) + dynamodb_client.put_item(TableName=gsi_table, Item=_item(b, "v0")) + batch_client = _client(endpoint_url, retries=True) + other_client = _client(endpoint_url, retries=True) + + hold = Hold(mongodb_container, gsi_table, b, HOLD) + other = threading.Timer( + 5, lambda: other_client.put_item(TableName=gsi_table, Item=_item(a, "v2")) + ) + other.start() + started = time.monotonic() + resp = batch_client.batch_write_item( + RequestItems={ + gsi_table: [ + {"DeleteRequest": {"Key": {"pk": {"S": a}}}}, + {"PutRequest": {"Item": _item(b, "v1")}}, + ] + } + ) + elapsed = time.monotonic() - started + other.join() + hold.wait() + + print(f"the batch answered in {elapsed:.2f} s: {resp.get('UnprocessedItems')}") + assert resp["UnprocessedItems"] == {gsi_table: [{"PutRequest": {"Item": _item(b, "v1")}}]} + assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" + assert _w(dynamodb_client, gsi_table, a) == "v2", "the other client's put of A was lost" + assert _w(dynamodb_client, gsi_table, b) == "v0", "the unprocessed put of B was applied" + + +def test_the_requests_of_a_batch_share_one_contention_deadline( + dynamodb_client, endpoint_url, mongodb_container, gsi_table +): + """A request that reaches the deadline late in a batch does not get a deadline of its own. + + The batch puts B1, held for 6 s, and then B2, held past the deadline. B1 + commits once its hold ends. B2 is tried after that and gives up when the + deadline the batch started with passes, so the batch answers within that + deadline, with B2 unprocessed, rather than about 6 s plus a deadline later. + """ + b1, b2 = f"b1-{uuid.uuid4().hex[:8]}", f"b2-{uuid.uuid4().hex[:8]}" + for pk in (b1, b2): + dynamodb_client.put_item(TableName=gsi_table, Item=_item(pk, "v0")) + client = _client(endpoint_url, retries=False) + + hold_b2 = Hold(mongodb_container, gsi_table, b2, HOLD) + hold_b1 = Hold(mongodb_container, gsi_table, b1, 6) + started = time.monotonic() + resp = client.batch_write_item( + RequestItems={ + gsi_table: [ + {"PutRequest": {"Item": _item(b1, "v1")}}, + {"PutRequest": {"Item": _item(b2, "v1")}}, + ] + } + ) + elapsed = time.monotonic() - started + hold_b1.wait() + hold_b2.wait() + + print(f"the batch answered in {elapsed:.2f} s: {resp.get('UnprocessedItems')}") + assert resp["UnprocessedItems"] == {gsi_table: [{"PutRequest": {"Item": _item(b2, "v1")}}]} + assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" + assert _w(dynamodb_client, gsi_table, b1) == "v1" + assert _w(dynamodb_client, gsi_table, b2) == "v0" From d9e3299d42ef57dca7430f8e70a934c398f40c69 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:25:44 +0000 Subject: [PATCH 13/44] test(put-item): require most contended puts to commit and pin the attempt budget tests/test_put_item_gsi_contention.py checked only the error class of the calls that failed, so a server that gives up at the first conflict passed as long as one put committed. The test now also requires at least half of the 2,016 puts to commit. The floor is loose on purpose: the shed count depends on the server's load (240 of 2,016 with a second hot-key load on the same server), and the storage tests pin the budget and the deadline. Each writer thread now has a client of its own. A botocore client shared by threads can fail a request with a TLS error, which with client retries off fails the test. live_unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error built its expected message from TRANSIENT_RETRY_ATTEMPTS and bounded the time only from above, so it also passed with a budget of 50. It now expects the message for 1,000 attempts written out, and requires the give-up to take at least 5 s: backoff_sleep sleeps a uniform draw from 0 to 12.8 ms before all but the first eight attempts, so the sleeps of 1,000 attempts alone come to about 6.4 s, against about 0.3 s for 50 attempts. --- .../src/data_engine_live_tests_contention.rs | 24 +++++++---- tests/test_put_item_gsi_contention.py | 42 ++++++++++++------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs index dcef72361..54f849fba 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs @@ -21,7 +21,7 @@ use extenddb_core::types::{ }; use extenddb_storage::error::StorageError; -use super::super::{CONTENTION_DEADLINE, OwnedTransactWriteOp, TRANSIENT_RETRY_ATTEMPTS}; +use super::super::{CONTENTION_DEADLINE, OwnedTransactWriteOp}; use super::{finish, scratch_engine, table}; use crate::MongoEngine; use crate::data::{data_collection_name, document_to_item, item_to_document}; @@ -486,20 +486,30 @@ async fn live_unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_t match res { Err(StorageError::Transient(msg)) => { + // The budget is written out rather than read from + // `TRANSIENT_RETRY_ATTEMPTS`, so a change to the constant fails + // here instead of changing what the test expects. assert_eq!( msg, - format!( - "PutItem: the write lost {TRANSIENT_RETRY_ATTEMPTS} attempts to concurrent \ - writers; retry the request" - ) + "PutItem: the write lost 1000 attempts to concurrent writers; retry the request" ); println!("the put spent its attempt budget in {elapsed:?}: {msg}"); } other => panic!("want the transient spent-budget error, got: {other:?}"), } + // The loop made those attempts. `backoff_sleep` sleeps a uniform draw + // from 0 to 12.8 ms before all but the first eight attempts, so the + // sleeps of 1,000 attempts alone come to about 6.4 s (standard deviation + // about 0.12 s), before the 1,000 lock waits. A budget of 50 attempts + // sleeps about 0.3 s, and the create-race budget is also 50, so a loop + // that one of those ended answers well before this floor. + assert!( + elapsed >= Duration::from_secs(5), + "the put gave up in {elapsed:?}, too soon for 1,000 attempts" + ); // The 1,000 attempts take 9.5 to 12.7 s on a shared host, and the sleeps - // alone come to 12.8 s at worst, so the bound here is loose: what it rules - // out is a budget that no longer ends the loop at all. + // alone come to 12.8 s at worst, so the upper bound is loose: what it + // rules out is a budget that no longer ends the loop at all. assert!(elapsed < Duration::from_secs(120), "took {elapsed:?}"); assert_eq!( stored_item(&engine, &key_info, "k1").await, diff --git a/tests/test_put_item_gsi_contention.py b/tests/test_put_item_gsi_contention.py index 14707d217..c2a0202b3 100644 --- a/tests/test_put_item_gsi_contention.py +++ b/tests/test_put_item_gsi_contention.py @@ -17,7 +17,7 @@ ServiceUnavailable, never 400 TransactionConflictException and never 500; the stored item is one writer's item, whole; and the index holds exactly one entry for it. These tests fail on a server that reports a lost race to the client as -a 400 or a 500. +a 400 or a 500, or that sheds more than half of the puts. """ from __future__ import annotations @@ -42,21 +42,23 @@ @pytest.fixture(scope="module") -def raw_client(endpoint_url): - """A client that never retries, so every failure is seen.""" +def raw_clients(endpoint_url): + """One client per writer thread, none of which retries, so every failure is seen. + + A botocore client shared by several threads can fail a request with a TLS + error under concurrent use. With retries off that error would fail the + test, so each writer gets a client of its own. + """ kwargs: dict = { "service_name": "dynamodb", "region_name": os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), - "config": Config( - retries={"total_max_attempts": 1, "mode": "standard"}, - max_pool_connections=WRITERS * 2, - ), + "config": Config(retries={"total_max_attempts": 1, "mode": "standard"}), } if endpoint_url: kwargs["endpoint_url"] = endpoint_url if endpoint_url.startswith("https://"): kwargs["verify"] = False - return boto3.client(**kwargs) + return [boto3.client(**kwargs) for _ in range(WRITERS)] @pytest.fixture(scope="module") @@ -78,14 +80,18 @@ def gsi_table(dynamodb_client): yield name -def _hammer(client, table: str, pk: str) -> list[str]: - """WRITERS threads each put PUTS_PER_WRITER items on `pk`; return the failures.""" +def _hammer(clients: list, table: str, pk: str) -> list[str]: + """WRITERS threads each put PUTS_PER_WRITER items on `pk`, each with its own client. + + Returns the failures. + """ start = threading.Barrier(WRITERS, timeout=30) failures: list[str] = [] errors: list[BaseException] = [] lock = threading.Lock() def run(i: int): + client = clients[i] try: start.wait() for n in range(PUTS_PER_WRITER): @@ -121,19 +127,23 @@ def _gsi_entries(client, table: str, pk: str) -> list[str]: def test_unconditional_puts_on_one_key_of_a_gsi_table_commit_or_are_shed( - dynamodb_client, raw_client, gsi_table + dynamodb_client, raw_clients, gsi_table ): pk = f"hot-{uuid.uuid4().hex[:8]}" - failures = _hammer(raw_client, gsi_table, pk) + failures = _hammer(raw_clients, gsi_table, pk) total = WRITERS * PUTS_PER_WRITER - # How many of the contended puts were shed. The count depends on the load - # the server is under, so it is reported rather than asserted; what is - # asserted is that a shed put is the only failure a caller can get. - print(f"{len(failures)} of {total} puts were shed, {total - len(failures)} committed") + committed = total - len(failures) + # How many of the contended puts were shed depends on the load the server + # is under, so the count is reported and only bounded loosely: a shed put + # is the only failure a caller can get, and at least half of the puts + # commit. A server that gives up at the first conflict sheds nearly every + # put of this shape and fails the second check. + print(f"{len(failures)} of {total} puts were shed, {committed} committed") assert set(failures) <= {SHED}, ( f"{len(failures)} of {total} puts failed with something other than a shed put: " f"{sorted(set(failures))}" ) + assert committed >= total // 2, f"only {committed} of {total} puts committed" final = dynamodb_client.get_item( TableName=gsi_table, Key={"pk": {"S": pk}}, ConsistentRead=True )["Item"] From efe8bc9adca9e7e1374f6dc586392a5a693d600c Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:25:45 +0000 Subject: [PATCH 14/44] docs(mongodb): bring the contention comments and docs up to date - The sessionless PutItem path and the duplicate-key arm of the transaction path said their attempts share the transient retry budget. Both count against CREATE_RACE_ATTEMPTS since the create race got a budget of its own. - CONTENTION_DEADLINE's comment said only the loops that serve a request take the deadline. The TTL worker's conditional delete runs the delete loop too, so an expired item that stays contended holds the sweep until the deadline or the attempt budget ends that loop; the worker then logs the failure and moves on. The comment now says so, says that the requests of one BatchWriteItem share one deadline, and says that the attempt already running when the deadline passes has no bound of its own: it waits for a connection from the pool and for the commit's own retries, and the longest call at 256 writers on one key measured 11.7 s. It also states the cost of retrying for up to 10 s, CPU on the MongoDB server for as long as a key stays hot. - contention_deadline_exhausted printed the deadline with as_secs(), so a deadline a test sets below one second read as "0 seconds". It now prints as_secs_f64(): "10 seconds", "2 seconds", "0.25 seconds". - The RFC's decision table still listed the UnknownTransactionCommitResult label as a conflict the body retry detects, which the write conflict section contradicts. The row now says that label retries commitTransaction in place and never re-runs the body. - The RFC section and the code now name #475, the fix for #437, as the change that gives DeleteItem and UpdateItem the transient give-up. - docs/differences-from-dynamodb.md said all 1,280 puts committed at 64 writers. On a loaded host 106 and 149 of 1,280 were shed. The row now says the shed count depends on the host's load, keeps the 256-writer measurement, and says how a BatchWriteItem answers a request that reaches the deadline. --- crates/storage-mongodb/src/data_engine.rs | 80 +++++++++++++++-------- docs/differences-from-dynamodb.md | 2 +- docs/rfcs/0000-mongodb-backend.md | 4 +- 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 919b283c4..b6dc8a2d8 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -428,9 +428,9 @@ impl MongoEngine { // this loop does. The error means another put's item is already in // the collection, so the next attempt finds that item, replaces it // and returns it as the old image; if a delete removed that item - // first, the next attempt inserts. Attempts share the transient - // retry budget and back off between them, as the transaction path - // below does. + // first, the next attempt inserts. This path makes at most + // `CREATE_RACE_ATTEMPTS` attempts and backs off between them, the + // budget a lost create race has on the transaction path below. if condition.is_none() && stream.is_none() && self.gsi_cache_get_fresh(&key_info.table_id) == Some(false) @@ -569,10 +569,11 @@ impl MongoEngine { // The arm is a defence: a duplicate-key error here means // a document in this transaction's snapshot holds the // item's `_id` without matching the key filter. It runs - // the transaction again with the retry budget and - // backoff of a write conflict, and the put gives up with - // the create race error when every attempt meets that - // document. + // the transaction again with the backoff of a write + // conflict and counts the lost race against + // `CREATE_RACE_ATTEMPTS`, and the put gives up with the + // create race error once that many attempts have met + // such a document. let old_doc = match coll .find_one_and_replace(key_filter.clone(), new_doc) .with_options(opts) @@ -3654,18 +3655,37 @@ const TRANSIENT_RETRY_ATTEMPTS: u32 = 1000; /// UpdateItem answer HTTP 503 on the deadline and 400 /// `TransactionConflictException` when their attempt budget runs out first /// (measured in 2 of 4 rounds against a held document, where the 1,000 -/// attempts and the deadline both land near 10 s). The 400 of those two -/// paths is what #437 replaces, after which every single-item write answers -/// 503 whichever bound it reaches. +/// attempts and the deadline both land near 10 s). #475, the fix for #437, +/// replaces the 400 of those two paths, after which every single-item write +/// answers 503 whichever bound it reaches. /// /// The deadline is read before an attempt starts, so a request answers within -/// the deadline plus one attempt. It governs the loops that serve a request: -/// put, delete, update and transact. The single-item writes answer it with -/// HTTP 503; a transaction answers it as it answers a spent attempt budget, -/// with `TransactionCanceledException`. The GSI backfill loop shares the attempt -/// budget and takes no deadline: it runs in the background worker in -/// `ttl_worker.rs`, where no caller is waiting for a reply and giving up -/// leaves the index CREATING. +/// the deadline plus the attempt already running. That attempt has no bound +/// of its own: under many writers on one key it waits for a connection from +/// the pool (`pool_size`, 50 by default) and for the commit's own retries, +/// which can take a couple of seconds. At 256 writers on one key of a GSI table the +/// longest call measured 11.7 s. +/// +/// It governs the put, delete, update and transact loops. The single-item +/// writes answer it with HTTP 503; a transaction answers it as it answers a +/// spent attempt budget, with `TransactionCanceledException`. A loop measures +/// the deadline from the start of its request (see `contention_clock_start`): +/// the write requests of one `BatchWriteItem` share one deadline, and the +/// handler returns a request that reaches it in UnprocessedItems. The TTL +/// worker's conditional delete runs the delete loop too, outside any request, +/// so an expired item that stays contended holds the sweep until the deadline +/// or the attempt budget ends that loop, where 50 attempts ended it before +/// the budget was raised. The worker then logs the failure and moves to the +/// next item. +/// The GSI backfill loop shares the attempt budget and takes no deadline: it +/// runs in the background worker in `ttl_worker.rs`, where no caller is +/// waiting for a reply and giving up leaves the index CREATING. +/// +/// The cost of the longer retry is CPU on the MongoDB server: the losers of a +/// hot key retry with a backoff capped at 12.8 ms for up to the deadline, so +/// the server stays busy committing and aborting transactions on that key for +/// as long as the contention lasts, about 2 cores at 1,800 to 1,900 +/// transactions a second with 64 and 256 writers on one key. /// /// The engine carries the deadline in /// [`MongoEngine::contention_deadline`](crate::MongoEngine) so that a test can @@ -3871,7 +3891,8 @@ fn contention_clock_start() -> std::time::Instant { /// The error for a write that stopped retrying because the engine's /// contention deadline passed while it was losing to concurrent writers. /// `operation` names the API operation and `deadline` is the deadline that -/// passed. +/// passed, in seconds with any fraction, so a deadline a test sets below one +/// second does not read as 0 seconds. /// /// Like the attempt-budget give-up of PutItem this is a /// [`StorageError::Transient`], which the engine answers as HTTP 503 @@ -3881,7 +3902,7 @@ fn contention_clock_start() -> std::time::Instant { fn contention_deadline_exhausted(operation: &str, deadline: std::time::Duration) -> StorageError { StorageError::Transient(format!( "{operation}: the write lost to concurrent writers for {} seconds; retry the request", - deadline.as_secs() + deadline.as_secs_f64() )) } @@ -4480,13 +4501,20 @@ mod tests { other => panic!("want Transient, got {other:?}"), } } - // The message carries the deadline that passed, not the default. - match contention_deadline_exhausted("PutItem", std::time::Duration::from_secs(2)) { - StorageError::Transient(msg) => assert!( - msg.contains("lost to concurrent writers for 2 seconds"), - "{msg}" - ), - other => panic!("want Transient, got {other:?}"), + // The message carries the deadline that passed, not the default, and + // a deadline below one second keeps its fraction. + for (deadline, wait) in [ + (std::time::Duration::from_secs(2), "2 seconds"), + (std::time::Duration::from_millis(250), "0.25 seconds"), + (std::time::Duration::from_millis(1500), "1.5 seconds"), + ] { + match contention_deadline_exhausted("PutItem", deadline) { + StorageError::Transient(msg) => assert!( + msg.contains(&format!("lost to concurrent writers for {wait};")), + "{msg}" + ), + other => panic!("want Transient, got {other:?}"), + } } } diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index 47ad30bd0..a1e59bffc 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -149,7 +149,7 @@ The same on all four backends. | Provisioned throughput | Token bucket per table/partition | Token bucket per table/partition, matching DynamoDB's burst and refill behavior | | On-demand capacity | Automatic scaling | Fixed initial burst capacity (4000 WCU / 12000 RCU), no auto-scaling | | Throttling | Always on; throttles requests that exceed provisioned/burst capacity. No setting to disable | Configurable via `throttling_enabled` runtime setting (default: `true`) | -| Sustained contention on one item (MongoDB backend) | Applies concurrent single-item writes on one key one after another and answers 200 to each. Measured to every call at 32 and 64 writers putting one key of a table with a GSI. A `PutItem` is answered 400 `TransactionConflictException` only while an in-flight `TransactWriteItems` holds the item (33 of 80 calls measured), and a `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | A write on a table with a secondary index writes the item and its index entries in one transaction, so concurrent writes on one key abort each other and are retried. One request's retry loop stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key: `PutItem` answers 503 `ServiceUnavailable` on either bound, which the SDK retry modes retry and which succeeds once the contention has passed. `DeleteItem` and `UpdateItem` answer 503 on the deadline and 400 `TransactionConflictException` when their 1,000 attempts run out first. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. Measured with one GSI table and all writers on one key: at 64 writers all 1,280 puts committed and none were shed, with the longest call 9.7 s; at 256 writers 1,262 of 5,120 committed and 3,858 were shed, with the longest call 11.7 s. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | +| Sustained contention on one item (MongoDB backend) | Applies concurrent single-item writes on one key one after another and answers 200 to each. Measured to every call at 32 and 64 writers putting one key of a table with a GSI. A `PutItem` is answered 400 `TransactionConflictException` only while an in-flight `TransactWriteItems` holds the item (33 of 80 calls measured), and a `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | A write on a table with a secondary index writes the item and its index entries in one transaction, so concurrent writes on one key abort each other and are retried. One request's retry loop stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key: `PutItem` answers 503 `ServiceUnavailable` on either bound, which the SDK retry modes retry and which succeeds once the contention has passed. `DeleteItem` and `UpdateItem` answer 503 on the deadline and 400 `TransactionConflictException` when their 1,000 attempts run out first. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. In a `BatchWriteItem` the write requests share one deadline, measured from the start of the request: a request that reaches it is returned in `UnprocessedItems` and the other requests are still applied, which is how the service answers a batch with a request on an item a transaction holds. The batch answers the error only when none of its requests was applied. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | ## Operations Not Implemented diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index 13c46ed72..b2c24eb42 100644 --- a/docs/rfcs/0000-mongodb-backend.md +++ b/docs/rfcs/0000-mongodb-backend.md @@ -136,7 +136,7 @@ Transaction implementation: `data_engine.rs` — `transact_write_items_impl`, `t ### Write conflict handling -**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place up to `COMMIT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs, each sleep drawn uniformly from 0 to the current base, capped at 12.8 ms) up to `TRANSIENT_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race has its own budget, `CREATE_RACE_ATTEMPTS` (50). A single-item write that reaches the deadline returns the `StorageError::Transient` built by `contention_deadline_exhausted`, answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; so does PutItem when its attempt budget runs out. A `TransactWriteItems` that reaches the deadline answers as it does when its attempts run out. DeleteItem and UpdateItem that run out of attempts return `TransactionConflict`; `TransactWriteItems` that runs out of attempts surfaces a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. The two bounds take about the same time on a contended key, so which error class DeleteItem and UpdateItem answer depends on which bound their loop reaches first: 503 on the deadline and 400 `TransactionConflictException` on the spent budget, with a delete against a held document answering 400 in 2 of 4 measured rounds. #437 routes those two exhaustions through the same transient give-up, after which every single-item write answers 503 whichever bound it reaches. PutItem and `TransactWriteItems` already answer one error class on either bound. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. +**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place up to `COMMIT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff (`backoff_sleep`, base 50 µs, each sleep drawn uniformly from 0 to the current base, capped at 12.8 ms) up to `TRANSIENT_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The TTL worker's conditional delete runs the DeleteItem loop, so it takes the deadline too. `BatchWriteItem` runs its writes inside one `request_clock` scope, so the loops of one batch measure one deadline from the start of the request, and the handler returns a request that reaches it in `UnprocessedItems`. The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race has its own budget, `CREATE_RACE_ATTEMPTS` (50). A single-item write that reaches the deadline returns the `StorageError::Transient` built by `contention_deadline_exhausted`, answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; so does PutItem when its attempt budget runs out. A `TransactWriteItems` that reaches the deadline answers as it does when its attempts run out. DeleteItem and UpdateItem that run out of attempts return `TransactionConflict`; `TransactWriteItems` that runs out of attempts surfaces a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. The two bounds take about the same time on a contended key, so which error class DeleteItem and UpdateItem answer depends on which bound their loop reaches first: 503 on the deadline and 400 `TransactionConflictException` on the spent budget, with a delete against a held document answering 400 in 2 of 4 measured rounds. #475, the fix for #437, routes those two exhaustions through the same transient give-up, after which every single-item write answers 503 whichever bound it reaches. PutItem and `TransactWriteItems` already answer one error class on either bound. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. **UpdateItem** additionally uses an optimistic-concurrency version guard on top of the transaction. A `_v` counter is stored on each document. The write path reads the current `_v` under the transaction snapshot, applies the update expression in memory, sets `_v = current_version + 1`, then executes `replace_one` filtered on both the primary key AND the expected `_v`. If `matched_count == 0`, a concurrent writer committed a higher version between the snapshot read and the replace; the attempt aborts and the outer retry loop re-reads. The version guard doubles up when a native-fast-path update (unconditional, no stream, no GSI) is possible — that path uses a single `find_one_and_update` with `$inc: {_v: 1}` outside a transaction, and always bumps the counter so a concurrent session-scoped update against a stale snapshot fails its versioned filter and retries. @@ -235,7 +235,7 @@ No changes to `crates/engine/`, `crates/server/`, `crates/storage/` (trait defin |---|---|---| | Conditional writes | Read + evaluate + write inside a MongoDB transaction session | Snapshot atomicity gives DynamoDB's contract; loaded item reused for `ReturnValuesOnConditionCheckFailure = ALL_OLD` without a follow-up read. Analyzer-gated pushdown fast path skips the session for a certified subset on tables with no GSIs / streams. | | UpdateItem concurrency | `_v` version guard inside snapshot txn + WriteConflict retry with jittered exponential backoff | Prevents lost updates; retry ceiling (1,000) bounds tail latency under sustained contention. | -| WriteConflict handling | Detect via `TransientTransactionError` label, `UnknownTransactionCommitResult` label, or raw code 112; retry with backoff | Converts a raw HTTP 500 into a retryable operation; TWI exhaustion surfaces as `TransactionCanceled` with per-op `TransactionConflict` reasons. | +| WriteConflict handling | Detect via `TransientTransactionError` label or raw code 112 and re-run the transaction with backoff; after an `UnknownTransactionCommitResult` label, retry `commitTransaction` in place and never re-run the body | Converts a raw HTTP 500 into a retryable operation; a single-item write that reaches `CONTENTION_DEADLINE`, and a PutItem that spends its attempt budget, answer HTTP 503; TWI exhaustion surfaces as `TransactionCanceled` with per-op `TransactionConflict` reasons. | | GSI updates | Synchronous inline within the base write's session, with 60-second TTL cache short-circuit for tables with no GSIs; async worker-driven backfill on UpdateTable | No Change Stream recovery; GSI reads are strongly consistent; UpdateTable matches DDB's async CREATING → ACTIVE contract. | | GSI/LSI index docs | Composite `_id` includes both index and base keys; `base_pk` / `base_sk_?` stored as first-class fields | GSI keys are non-unique; base-key disambiguation prevents cross-item overwrite. Base keys as fields let index pagination form a compound cursor without traversing item_data. | | Composite `_id` | Netstring-encoded (`:,...`) | Unambiguous boundary between pk and sk regardless of content. | From b7c9258f151e8d40b81d1bb3a7561dd73ba82969 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:26:24 +0000 Subject: [PATCH 15/44] docs(mongodb): describe the duplicate-key arm the same way everywhere 0d3f76d56 rewrote the comment on the transaction path's duplicate-key arm: a put that loses the create race inside a snapshot transaction gets WriteConflict 112, so the arm only sees a document that holds the item's _id without matching the key filter. Two comments in the same function and the doc of create_race_exhausted still described the arm as the create race, and now say what it is. The create race test's client fixture no longer explains why each writer has its own client; the docstring states what the fixture gives. --- crates/storage-mongodb/src/data_engine.rs | 13 ++++++++----- tests/test_put_item_create_race.py | 7 +------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 4a203c37d..13a940e07 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -483,7 +483,8 @@ impl MongoEngine { .await .map_err(|e| StorageError::Internal(e.to_string()))?; - // Set when the unconditional upsert below lost a create race. + // Set when the unconditional upsert below hit a duplicate key on + // `_id`. let mut lost_create_race = false; let attempt_res: Result, TxErr> = async { let old_item: Option; @@ -629,8 +630,8 @@ impl MongoEngine { } } - // Every attempt lost the create race: give up with the same error - // as the sessionless path above. + // Every attempt met a document that holds the item's `_id`: give up + // with the create race error the sessionless path returns. if lost_create_races == TRANSIENT_RETRY_ATTEMPTS { return Err(create_race_exhausted(lost_create_races)); } @@ -3700,8 +3701,10 @@ fn is_duplicate_key(e: &mongodb::error::Error) -> bool { } /// The error when every attempt of an unconditional PutItem lost the create -/// race: each attempt found no item, and its insert hit an item another -/// writer created, which was deleted again before the next attempt read it. +/// race: on the sessionless path, each attempt found no item, and its insert +/// hit an item another writer created, which was deleted again before the +/// next attempt read it; on the transaction path, every attempt met a +/// document that holds the item's `_id` without matching the key filter. /// `attempt` is the number of attempts made. The message is the internal /// error (HTTP 500) the PostgreSQL backend returns when its own retries of /// this race run out. diff --git a/tests/test_put_item_create_race.py b/tests/test_put_item_create_race.py index 15c2166a8..9fd9416af 100644 --- a/tests/test_put_item_create_race.py +++ b/tests/test_put_item_create_race.py @@ -34,12 +34,7 @@ @pytest.fixture(scope="module") def raw_clients(endpoint_url): - """One client per writer thread, none of which retries, so every failure is seen. - - A botocore client shared by several threads can fail a request with a TLS - error under concurrent use. With retries off that error would fail the - test, so each writer gets a client of its own. - """ + """One client per writer thread, none of which retries, so every failure is seen.""" kwargs: dict = { "service_name": "dynamodb", "region_name": os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), From ed4fb62a3d8bf841cc9f7df9bbec06e94b9eb393 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:46:00 +0000 Subject: [PATCH 16/44] docs(mongodb): state the measured CPU cost of a hot key The comment on CONTENTION_DEADLINE gave the CPU cost of retrying a hot key for up to 10 s without a measurement from this tree. Measured on this head with 64 and then 256 writers putting one key of a GSI table for 20 s, client retries off, on a 16-core host at a load average of 18 to 21: - 64 writers: 457 puts committed and 12 were shed; mongod used a median of 132 percent CPU and ran 35,063 transactions, 34,606 of them aborted (1,666 a second). - 256 writers: 150 committed and 444 were shed; mongod used a median of 128 percent CPU and ran 39,335 transactions, 39,185 of them aborted (1,811 a second). --- crates/storage-mongodb/src/data_engine.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 840ebbde0..566d94f31 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -3687,8 +3687,10 @@ const TRANSIENT_RETRY_ATTEMPTS: u32 = 1000; /// The cost of the longer retry is CPU on the MongoDB server: the losers of a /// hot key retry with a backoff capped at 12.8 ms for up to the deadline, so /// the server stays busy committing and aborting transactions on that key for -/// as long as the contention lasts, about 2 cores at 1,800 to 1,900 -/// transactions a second with 64 and 256 writers on one key. +/// as long as the contention lasts. With 64 and 256 writers putting one key of +/// a GSI table for 20 s, on a 16-core host at a load average of about 20, +/// mongod used about 1.3 cores and ran 1,670 and 1,810 transactions a second, +/// of which 98.7 and 99.6 percent aborted. /// /// The engine carries the deadline in /// [`MongoEngine::contention_deadline`](crate::MongoEngine) so that a test can From 88420dd5e86fb5c4cc5968a6770133ff877f5764 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 13:31:52 +0000 Subject: [PATCH 17/44] fix(mongodb): count UpdateItem's native upsert create race against CREATE_RACE_ATTEMPTS Main's #473 added upsert_native_update, whose create race loop is sized by TRANSIENT_RETRY_ATTEMPTS. That is 50 on main and 1,000 on this branch, and the merge ac1f3b446 gave the loop this branch's value with no conflict, so a lost create race on _id retried up to 1,000 times with backoff, about 6.4 s of backoff before it answered TransactionConflictException, where main stops after 50. The loop now counts against CREATE_RACE_ATTEMPTS, the 50-attempt budget a lost create race has on the PutItem paths. Also from the review of this branch before the push: the BatchWriteItem doc comment says which writes answer TransactionConflict; the differences row says which error a batch answers when none of its requests was applied, and what the service answered; and a wire test checks that a batch whose only request is held past the deadline answers 503 and applies nothing. --- crates/engine/src/batch_write_item.rs | 6 +++-- .../src/data_engine_update_create.rs | 11 +++++---- docs/differences-from-dynamodb.md | 2 +- tests/test_mongodb_batch_write_contention.py | 23 +++++++++++++++++++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/batch_write_item.rs b/crates/engine/src/batch_write_item.rs index 62bc33242..a88f7abdf 100755 --- a/crates/engine/src/batch_write_item.rs +++ b/crates/engine/src/batch_write_item.rs @@ -330,8 +330,10 @@ async fn write_batch(body: Value, ctx: &OperationContext) -> Result, @@ -89,7 +90,7 @@ impl MongoEngine { .upsert(true) .return_document(ReturnDocument::After) .build(); - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + for attempt in 0..CREATE_RACE_ATTEMPTS { match coll .find_one_and_update(key_filter.clone(), update.clone()) .with_options(opts.clone()) diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index a1e59bffc..5ce3afed2 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -149,7 +149,7 @@ The same on all four backends. | Provisioned throughput | Token bucket per table/partition | Token bucket per table/partition, matching DynamoDB's burst and refill behavior | | On-demand capacity | Automatic scaling | Fixed initial burst capacity (4000 WCU / 12000 RCU), no auto-scaling | | Throttling | Always on; throttles requests that exceed provisioned/burst capacity. No setting to disable | Configurable via `throttling_enabled` runtime setting (default: `true`) | -| Sustained contention on one item (MongoDB backend) | Applies concurrent single-item writes on one key one after another and answers 200 to each. Measured to every call at 32 and 64 writers putting one key of a table with a GSI. A `PutItem` is answered 400 `TransactionConflictException` only while an in-flight `TransactWriteItems` holds the item (33 of 80 calls measured), and a `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | A write on a table with a secondary index writes the item and its index entries in one transaction, so concurrent writes on one key abort each other and are retried. One request's retry loop stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key: `PutItem` answers 503 `ServiceUnavailable` on either bound, which the SDK retry modes retry and which succeeds once the contention has passed. `DeleteItem` and `UpdateItem` answer 503 on the deadline and 400 `TransactionConflictException` when their 1,000 attempts run out first. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. In a `BatchWriteItem` the write requests share one deadline, measured from the start of the request: a request that reaches it is returned in `UnprocessedItems` and the other requests are still applied, which is how the service answers a batch with a request on an item a transaction holds. The batch answers the error only when none of its requests was applied. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | +| Sustained contention on one item (MongoDB backend) | Applies concurrent single-item writes on one key one after another and answers 200 to each. Measured to every call at 32 and 64 writers putting one key of a table with a GSI. A `PutItem` is answered 400 `TransactionConflictException` only while an in-flight `TransactWriteItems` holds the item (33 of 80 calls measured), and a `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | A write on a table with a secondary index writes the item and its index entries in one transaction, so concurrent writes on one key abort each other and are retried. One request's retry loop stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key: `PutItem` answers 503 `ServiceUnavailable` on either bound, which the SDK retry modes retry and which succeeds once the contention has passed. `DeleteItem` and `UpdateItem` answer 503 on the deadline and 400 `TransactionConflictException` when their 1,000 attempts run out first. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. In a `BatchWriteItem` the write requests share one deadline, measured from the start of the request: a request that reaches it is returned in `UnprocessedItems` and the other requests are still applied, which is how the service answers a batch with a request on an item a transaction holds. When none of its requests was applied, the batch answers the error its request gave up with: 503 `ServiceUnavailable`, or 400 `TransactionConflictException` when a `DeleteItem` runs out of its 1,000 attempts first. The service answered 500 `InternalServerError` to 54 of 80 batches whose only request was on an item a transaction held. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | ## Operations Not Implemented diff --git a/tests/test_mongodb_batch_write_contention.py b/tests/test_mongodb_batch_write_contention.py index 73f50633f..cb7ec9f07 100644 --- a/tests/test_mongodb_batch_write_contention.py +++ b/tests/test_mongodb_batch_write_contention.py @@ -30,6 +30,7 @@ import boto3 import pytest from botocore.config import Config +from botocore.exceptions import ClientError from conftest import scoped_table @@ -205,3 +206,25 @@ def test_the_requests_of_a_batch_share_one_contention_deadline( assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" assert _w(dynamodb_client, gsi_table, b1) == "v1" assert _w(dynamodb_client, gsi_table, b2) == "v0" + + +def test_a_batch_none_of_whose_requests_was_applied_answers_the_error( + dynamodb_client, endpoint_url, mongodb_container, gsi_table +): + """A batch whose only request reaches the deadline answers 503 and applies nothing.""" + b = f"b-{uuid.uuid4().hex[:8]}" + dynamodb_client.put_item(TableName=gsi_table, Item=_item(b, "v0")) + client = _client(endpoint_url, retries=False) + + hold = Hold(mongodb_container, gsi_table, b, HOLD) + started = time.monotonic() + with pytest.raises(ClientError) as err: + client.batch_write_item( + RequestItems={gsi_table: [{"PutRequest": {"Item": _item(b, "v1")}}]} + ) + elapsed = time.monotonic() - started + hold.wait() + + assert err.value.response["ResponseMetadata"]["HTTPStatusCode"] == 503 + assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" + assert _w(dynamodb_client, gsi_table, b) == "v0", "the put of B was applied" From 59c467b3aafae15631b6ba79e37cb4ed2671c0f4 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 14:27:55 +0000 Subject: [PATCH 18/44] test(put-item): skip the GSI hot-key test on Cassandra (#802) test_unconditional_puts_on_one_key_of_a_gsi_table_commit_or_are_shed holds the backend to the MongoDB retry loop's contract: every contended put answers 200 or a shed 503. The Cassandra backend answers some of these puts with 400 TransactionConflictException (17 of 2,016 in this branch's cassandra-pytest job on 2026-10-24, 31 of 2,016 on 2026-11-03), where the service answers 200 to all of them. That is a Cassandra write path problem outside this change, filed as #802, so the test is skipped on Cassandra until it is fixed. SQLite, PostgreSQL and MongoDB still run it. --- tests/test_put_item_gsi_contention.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_put_item_gsi_contention.py b/tests/test_put_item_gsi_contention.py index c2a0202b3..43847503a 100644 --- a/tests/test_put_item_gsi_contention.py +++ b/tests/test_put_item_gsi_contention.py @@ -32,7 +32,7 @@ from botocore.config import Config from botocore.exceptions import ClientError -from conftest import scoped_table +from conftest import configured_backend, scoped_table WRITERS = 32 PUTS_PER_WRITER = 63 @@ -126,6 +126,10 @@ def _gsi_entries(client, table: str, pk: str) -> list[str]: kwargs["ExclusiveStartKey"] = resp["LastEvaluatedKey"] +@pytest.mark.skipif( + configured_backend() == "cassandra", + reason="Cassandra answers some of these puts with TransactionConflictException (#802)", +) def test_unconditional_puts_on_one_key_of_a_gsi_table_commit_or_are_shed( dynamodb_client, raw_clients, gsi_table ): From 850729773b4462094930f3e12019bd860e76e81f Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:58:17 +0000 Subject: [PATCH 19/44] fix(mongodb): carry the driver error in the transaction create race arm Main changed TxErr::Transient to carry the text of the error that ended the attempt. The retry loop keeps the last one for the message it returns when the budget runs out. The duplicate-key arm of the transaction path now passes the driver's error text, as the other arms that return TxErr::Transient do. --- crates/storage-mongodb/src/data_engine.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 9365d63b9..146f4eff2 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -656,7 +656,7 @@ impl MongoEngine { Ok(old_doc) => old_doc, Err(e) if is_duplicate_key(&e) => { lost_create_race = true; - return Err(TxErr::Transient); + return Err(TxErr::Transient(e.to_string())); } Err(e) => return Err(TxErr::from(e)), }; From 4fcaf7ea6aeba82e1982c763d822217ca1599fae Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:00:39 +0000 Subject: [PATCH 20/44] refactor(mongodb): let the restore duplicate-key check use is_duplicate_key Main added is_duplicate_key_anywhere to the restore engine with its own arm for a command error with code 11000. It falls back to data_engine::is_duplicate_key for every other error kind. On this branch is_duplicate_key matches the command error form too, so the arm repeats that check. This commit removes the arm, and a command error reaches the same answer through the fallback. --- crates/storage-mongodb/src/restore_engine.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/storage-mongodb/src/restore_engine.rs b/crates/storage-mongodb/src/restore_engine.rs index 8e18ebb87..7741ca1eb 100644 --- a/crates/storage-mongodb/src/restore_engine.rs +++ b/crates/storage-mongodb/src/restore_engine.rs @@ -211,7 +211,6 @@ fn is_duplicate_key_anywhere(e: &mongodb::error::Error) -> bool { .write_errors .as_ref() .is_some_and(|errors| errors.iter().any(|w| w.code == DUPLICATE_KEY)), - mongodb::error::ErrorKind::Command(c) => c.code == DUPLICATE_KEY, _ => crate::data_engine::is_duplicate_key(e), } } From a48ef73edca7f62980b8c0cbf9be526647922a3b Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:42:18 +0000 Subject: [PATCH 21/44] fix(mongodb): give the contention loops a 1,000-attempt budget of their own Main made TRANSIENT_RETRY_ATTEMPTS crate-visible and pointed six backup and restore loops at it, so raising it to 1,000 would raise those loops too. The put, delete, update, transact and GSI backfill loops and the transaction in data_engine_key_repair.rs now read CONTENTION_RETRY_ATTEMPTS (1,000). TRANSIENT_RETRY_ATTEMPTS stays at 50 for the backup and restore loops, the commit retry and the create-race give-up. This branch gave the last two their own constants of 50, COMMIT_RETRY_ATTEMPTS and CREATE_RACE_ATTEMPTS, which now repeat TRANSIENT_RETRY_ATTEMPTS and go. The put race live test and UpdateItem's native upsert read TRANSIENT_RETRY_ATTEMPTS again, as on fix/mongodb-upsert-duplicate-key. The live test of a spent attempt budget expects the message of main's retry_budget_exhausted. --- crates/storage-mongodb/src/data_engine.rs | 96 ++++++++----------- .../src/data_engine_key_repair.rs | 4 +- .../src/data_engine_live_tests_contention.rs | 8 +- .../src/data_engine_live_tests_put_race.rs | 8 +- .../src/data_engine_update_create.rs | 11 +-- 5 files changed, 56 insertions(+), 71 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index fd04d5361..3f7dbb121 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -507,9 +507,9 @@ impl MongoEngine { // this loop does. The error means another put's item is already in // the collection, so the next attempt finds that item, replaces it // and returns it as the old image; if a delete removed that item - // first, the next attempt inserts. This path makes at most - // `CREATE_RACE_ATTEMPTS` attempts and backs off between them, the - // budget a lost create race has on the transaction path below. + // first, the next attempt inserts. Attempts share the transient + // retry budget and back off between them, as the transaction path + // below does. if condition.is_none() && stream.is_none() && self.gsi_cache_get_fresh(&key_info.table_id) == Some(false) @@ -520,7 +520,7 @@ impl MongoEngine { .upsert(true) .return_document(ReturnDocument::Before) .build(); - for attempt in 0..CREATE_RACE_ATTEMPTS { + for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { match coll .find_one_and_replace(key_filter.clone(), &new_doc) .with_options(opts.clone()) @@ -536,7 +536,7 @@ impl MongoEngine { Err(e) => return Err(StorageError::Internal(e.to_string())), } } - return Err(create_race_exhausted(CREATE_RACE_ATTEMPTS)); + return Err(create_race_exhausted(TRANSIENT_RETRY_ATTEMPTS)); } let mut session = self @@ -550,7 +550,7 @@ impl MongoEngine { let contention_started = contention_clock_start(); let mut last_cause = String::new(); - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + for attempt in 0..CONTENTION_RETRY_ATTEMPTS { if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { return Err(contention_deadline_exhausted( "PutItem", @@ -652,8 +652,8 @@ impl MongoEngine { // item's `_id` without matching the key filter. It runs // the transaction again with the backoff of a write // conflict and counts the attempt against - // `CREATE_RACE_ATTEMPTS`, and the put gives up with the - // create race error once that many attempts have met + // `TRANSIENT_RETRY_ATTEMPTS`, and the put gives up with + // the create race error once that many attempts have met // such a document. let old_doc = match coll .find_one_and_replace(key_filter.clone(), new_doc) @@ -715,7 +715,7 @@ impl MongoEngine { // contention on an item, so it gives up here with the // create race error rather than spending the // contention budget. - if lost_create_races >= CREATE_RACE_ATTEMPTS { + if lost_create_races >= TRANSIENT_RETRY_ATTEMPTS { return Err(create_race_exhausted(lost_create_races)); } } @@ -732,7 +732,7 @@ impl MongoEngine { // Retry ceiling exhausted: see `write_conflict::retry_budget_exhausted`. Err(crate::write_conflict::retry_budget_exhausted( "PutItem", - TRANSIENT_RETRY_ATTEMPTS, + CONTENTION_RETRY_ATTEMPTS, &last_cause, )) } @@ -808,7 +808,7 @@ impl MongoEngine { let contention_started = contention_clock_start(); let mut last_cause = String::new(); - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + for attempt in 0..CONTENTION_RETRY_ATTEMPTS { if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { return Err(contention_deadline_exhausted( "DeleteItem", @@ -916,7 +916,7 @@ impl MongoEngine { Err(crate::write_conflict::retry_budget_exhausted( "DeleteItem", - TRANSIENT_RETRY_ATTEMPTS, + CONTENTION_RETRY_ATTEMPTS, &last_cause, )) } @@ -1072,7 +1072,7 @@ impl MongoEngine { let contention_started = contention_clock_start(); let mut last_cause = String::new(); - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + for attempt in 0..CONTENTION_RETRY_ATTEMPTS { if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { return Err(contention_deadline_exhausted( "UpdateItem", @@ -1292,7 +1292,7 @@ impl MongoEngine { // the same contention on the same item. Err(crate::write_conflict::retry_budget_exhausted( "UpdateItem", - TRANSIENT_RETRY_ATTEMPTS, + CONTENTION_RETRY_ATTEMPTS, &last_cause, )) } @@ -2469,7 +2469,7 @@ impl MongoEngine { let contention_started = contention_clock_start(); let mut last_cause = String::new(); - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + for attempt in 0..CONTENTION_RETRY_ATTEMPTS { // A transaction that reaches the deadline stops retrying and // falls through to the give-up after the loop, which answers // `TransactionCanceledException` with a `TransactionConflict` @@ -2676,7 +2676,7 @@ impl MongoEngine { // TransactionCanceled to TransactionCanceledException; the // reason codes are echoed back in the message. tracing::warn!( - attempts = TRANSIENT_RETRY_ATTEMPTS, + attempts = CONTENTION_RETRY_ATTEMPTS, error = %last_cause, "TransactWriteItems: could not commit; answering TransactionCanceled" ); @@ -3586,7 +3586,7 @@ impl MongoEngine { // GSI backfill job in `ttl_worker.rs`, not inside a request: no // caller is waiting for a reply, and giving up on the clock would // leave the index CREATING with its cursor where it stopped. - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + for attempt in 0..CONTENTION_RETRY_ATTEMPTS { session .start_transaction() .with_options(tx_options.clone()) @@ -3756,7 +3756,7 @@ impl MongoEngine { ) -> Result { #[cfg(not(feature = "test-hooks"))] let _ = gate_table; - for attempt in 0..COMMIT_RETRY_ATTEMPTS { + for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { let commit_res = session.commit_transaction().await; #[cfg(feature = "test-hooks")] let commit_res = self @@ -3859,15 +3859,24 @@ pub(crate) struct GsiBackfillProgress { // ── Contention / retry helpers ────────────────────────────────────────── -/// Maximum number of times to retry a write that MongoDB aborted as a -/// transient conflict. Small enough that we don't lock a partition on -/// sustained hot-key contention; large enough to absorb ordinary -/// snapshot-isolation aborts. Matches the OCC retry ceiling elsewhere -/// in this file. +/// Maximum number of attempts of the retry loops that the measurements behind +/// [`CONTENTION_RETRY_ATTEMPTS`] do not cover: the backup and restore +/// transactions, the in-place retry of `commitTransaction` after an +/// `UnknownTransactionCommitResult`, and the create-race give-up of an +/// unconditional PutItem and of UpdateItem's native upsert. +/// +/// The commit retry waits for the reply of one idempotent commit and never +/// re-runs the body. A lost create race is not a write conflict: the attempt +/// found no item and its insert hit an item another writer created and deleted +/// again. #431 measured this budget, with its backoff, as the one that lets +/// that race resolve: a put blocked by a document that is removed 100 ms after +/// the put starts commits well inside it, and the storage tests fail when the +/// cap is put back to 5. A longer budget on either loop only delays the HTTP +/// 500 it answers. pub(crate) const TRANSIENT_RETRY_ATTEMPTS: u32 = 50; -/// Maximum number of times to retry a write that MongoDB aborted as a -/// transient conflict. +/// Maximum number of times a contention retry loop re-runs a transaction that +/// MongoDB aborted as a transient conflict. /// /// MongoDB aborts the loser of a same-document conflict at once instead of /// queueing it, so a hot key is settled by retries with backoff: of N @@ -3887,10 +3896,10 @@ pub(crate) const TRANSIENT_RETRY_ATTEMPTS: u32 = 50; /// shared host, under the SDKs' request timeouts. /// /// This budget sizes the contention retry loops only: the put, delete, -/// update, transact and GSI backfill loops, which re-run their transaction -/// body after a conflict. The commit retry and the create-race give-up have -/// their own budgets below, because the measurements that set this one say -/// nothing about either. +/// update, transact and GSI backfill loops and the transaction of the key +/// repair pass, which re-run their transaction body after a conflict. The +/// other retry loops read [`TRANSIENT_RETRY_ATTEMPTS`], because the +/// measurements that set this one say nothing about them. const CONTENTION_RETRY_ATTEMPTS: u32 = 1000; /// Wall-clock deadline for the contention retry loop of one request. @@ -3957,29 +3966,6 @@ const CONTENTION_RETRY_ATTEMPTS: u32 = 1000; /// construction uses this value. pub(crate) const CONTENTION_DEADLINE: std::time::Duration = std::time::Duration::from_secs(10); -/// Maximum number of times to retry `commitTransaction` in place after an -/// `UnknownTransactionCommitResult`. -/// -/// Not a contention loop: the label means the commit's outcome is unknown, -/// the commit is idempotent and the body must not be re-run, so these -/// attempts are waiting for one reply rather than racing other writers. No -/// measurement sizes a longer wait, and a longer one only delays the HTTP -/// 500 this path answers when the outcome stays unknown, so it keeps the -/// value every write path used before the contention budget was raised. -const COMMIT_RETRY_ATTEMPTS: u32 = 50; - -/// Maximum number of attempts of an unconditional PutItem that may lose the -/// create race before it gives up. -/// -/// A lost create race is not a write conflict: the attempt found no item and -/// its insert hit an item another writer created and deleted again. #431 -/// measured this budget, with its backoff, as the one that lets the race -/// resolve: a put blocked by a document that is removed 100 ms after the put -/// starts commits well inside it, and the storage tests fail when the cap is -/// put back to 5. A longer budget only delays the HTTP 500 this path -/// answers, so the contention budget above does not govern it. -const CREATE_RACE_ATTEMPTS: u32 = 50; - /// Return type of `try_build_native_update`. Distinguishes an /// operator-document update (`{$set, $unset, $inc}`) from an /// aggregation-pipeline update (needed for numeric ADD, which @@ -4124,7 +4110,7 @@ pub(crate) fn is_duplicate_key(e: &mongodb::error::Error) -> bool { /// The error when every attempt of an unconditional PutItem lost the create /// race: on the sessionless path, each attempt found no item, and its insert /// hit an item another writer created, which was deleted again before the -/// next attempt read it; on the transaction path, `CREATE_RACE_ATTEMPTS` +/// next attempt read it; on the transaction path, `TRANSIENT_RETRY_ATTEMPTS` /// attempts met a document that holds the item's `_id` without matching the /// key filter. /// `attempt` is the number of attempts made. The message is the internal @@ -4749,14 +4735,14 @@ mod tests { assert!(!contention_deadline_passed(0, spent, CONTENTION_DEADLINE)); assert!(contention_deadline_passed(1, spent, CONTENTION_DEADLINE)); assert!(contention_deadline_passed( - TRANSIENT_RETRY_ATTEMPTS - 1, + CONTENTION_RETRY_ATTEMPTS - 1, spent, CONTENTION_DEADLINE )); let fresh = std::time::Instant::now(); assert!(!contention_deadline_passed(1, fresh, CONTENTION_DEADLINE)); assert!(!contention_deadline_passed( - TRANSIENT_RETRY_ATTEMPTS - 1, + CONTENTION_RETRY_ATTEMPTS - 1, fresh, CONTENTION_DEADLINE )); diff --git a/crates/storage-mongodb/src/data_engine_key_repair.rs b/crates/storage-mongodb/src/data_engine_key_repair.rs index c46868d79..1d45bb5a0 100644 --- a/crates/storage-mongodb/src/data_engine_key_repair.rs +++ b/crates/storage-mongodb/src/data_engine_key_repair.rs @@ -57,7 +57,7 @@ use extenddb_core::types::{AttributeDefinition, KeySchemaElement}; use extenddb_storage::error::StorageError; use futures::TryStreamExt; -use super::{CommitOutcome, TRANSIENT_RETRY_ATTEMPTS, backoff_sleep, is_transient_write_conflict}; +use super::{CONTENTION_RETRY_ATTEMPTS, CommitOutcome, backoff_sleep, is_transient_write_conflict}; use crate::MongoEngine; use crate::data::data_collection_name; use crate::data::key_fields::{is_stored_without_key, repaired_document}; @@ -321,7 +321,7 @@ impl MongoEngine { .await .map_err(|e| StorageError::Internal(e.to_string()))?; let tx_options = self.write_transaction_options(); - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + for attempt in 0..CONTENTION_RETRY_ATTEMPTS { session .start_transaction() .with_options(tx_options.clone()) diff --git a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs index 54f849fba..91bdda527 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs @@ -487,11 +487,11 @@ async fn live_unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_t match res { Err(StorageError::Transient(msg)) => { // The budget is written out rather than read from - // `TRANSIENT_RETRY_ATTEMPTS`, so a change to the constant fails + // `CONTENTION_RETRY_ATTEMPTS`, so a change to the constant fails // here instead of changing what the test expects. - assert_eq!( - msg, - "PutItem: the write lost 1000 attempts to concurrent writers; retry the request" + assert!( + msg.starts_with("PutItem: could not commit in 1000 attempts: "), + "{msg}" ); println!("the put spent its attempt budget in {elapsed:?}: {msg}"); } diff --git a/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs b/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs index 16fce3843..bc0c3eefb 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs @@ -18,7 +18,7 @@ use extenddb_core::expression::ExpressionMaps; use extenddb_core::types::{Item, TableKeyInfo}; use extenddb_storage::error::StorageError; -use super::super::CREATE_RACE_ATTEMPTS; +use super::super::TRANSIENT_RETRY_ATTEMPTS; use super::{finish, item_key, s, scratch_engine, table, with}; use crate::MongoEngine; use crate::data::{data_collection_name, document_to_item, item_to_document}; @@ -53,8 +53,8 @@ fn document_id(item: &Item, key_info: &TableKeyInfo) -> Bson { } /// An unconditional PutItem whose insert keeps colliding on `_id` retries for -/// the whole create race budget, then gives up with the create race error, on -/// the sessionless path and on the transaction path. +/// the whole retry budget, then gives up with the create race error, on the +/// sessionless path and on the transaction path. /// /// A document that holds the item's `_id` under a different `pk` makes every /// attempt collide: the put's filter on `pk` finds no item, and its insert @@ -79,7 +79,7 @@ async fn live_unconditional_put_retries_a_lost_create_race_and_then_gives_up() { .put_item_impl(&key_info, item, true, None, &maps, None) .await; - let want = format!("could not create the item after {CREATE_RACE_ATTEMPTS} attempts"); + let want = format!("could not create the item after {TRANSIENT_RETRY_ATTEMPTS} attempts"); match res { Err(StorageError::Internal(msg)) => assert!( msg.contains(&want), diff --git a/crates/storage-mongodb/src/data_engine_update_create.rs b/crates/storage-mongodb/src/data_engine_update_create.rs index af7ede29f..a7f4e7e01 100644 --- a/crates/storage-mongodb/src/data_engine_update_create.rs +++ b/crates/storage-mongodb/src/data_engine_update_create.rs @@ -38,7 +38,7 @@ use extenddb_core::types::{Item, KeySchemaElement, TableKeyInfo}; use extenddb_storage::error::StorageError; use mongodb::options::{FindOneAndUpdateOptions, ReturnDocument}; -use super::{CREATE_RACE_ATTEMPTS, backoff_sleep}; +use super::{TRANSIENT_RETRY_ATTEMPTS, backoff_sleep}; use crate::MongoEngine; use crate::data::key_fields::insert_only_key_fields; @@ -70,10 +70,9 @@ impl MongoEngine { /// /// `key` and `key_info` supply the `$setOnInsert` fields that make a /// created document match what PutItem writes. Attempts that lose the - /// create race count against `CREATE_RACE_ATTEMPTS`, the budget a lost - /// create race has on the PutItem paths, and back off between them; when - /// every attempt loses, the error is the same `TransactionConflict` the - /// transaction path returns. + /// create race share the transient retry budget and back off between + /// them; when every attempt loses, the error is the same + /// `TransactionConflict` the transaction path returns. pub(crate) async fn upsert_native_update( &self, coll: &mongodb::Collection, @@ -90,7 +89,7 @@ impl MongoEngine { .upsert(true) .return_document(ReturnDocument::After) .build(); - for attempt in 0..CREATE_RACE_ATTEMPTS { + for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { match coll .find_one_and_update(key_filter.clone(), update.clone()) .with_options(opts.clone()) From 6c5b12cc1c269bb13a8bc4a384fae0ed4346a414 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:44:21 +0000 Subject: [PATCH 22/44] refactor(mongodb): give up on the contention deadline beside retry_budget_exhausted Main added write_conflict::retry_budget_exhausted, the Transient error of a single-item write whose attempts ran out. Its message names the operation, the attempts and the last error that ended an attempt. This branch gave up on the contention deadline through a second Transient helper in data_engine.rs, with a message of its own. contention_deadline_exhausted now sits beside retry_budget_exhausted in write_conflict.rs and has the same form: its message names the operation, the deadline and the last error. Its unit test moves with it, and the live tests of the deadline expect the new message. The CONTENTION_DEADLINE comment now says that every single-item write answers 503 on either bound. --- crates/storage-mongodb/src/data_engine.rs | 78 ++++--------------- .../src/data_engine_live_tests_contention.rs | 15 ++-- crates/storage-mongodb/src/write_conflict.rs | 38 +++++++++ 3 files changed, 57 insertions(+), 74 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 3f7dbb121..60c52d0ef 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -552,9 +552,10 @@ impl MongoEngine { let mut last_cause = String::new(); for attempt in 0..CONTENTION_RETRY_ATTEMPTS { if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { - return Err(contention_deadline_exhausted( + return Err(crate::write_conflict::contention_deadline_exhausted( "PutItem", self.contention_deadline, + &last_cause, )); } let new_doc = @@ -810,9 +811,10 @@ impl MongoEngine { let mut last_cause = String::new(); for attempt in 0..CONTENTION_RETRY_ATTEMPTS { if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { - return Err(contention_deadline_exhausted( + return Err(crate::write_conflict::contention_deadline_exhausted( "DeleteItem", self.contention_deadline, + &last_cause, )); } // Capture before the transaction's first database operation so a @@ -1074,9 +1076,10 @@ impl MongoEngine { let mut last_cause = String::new(); for attempt in 0..CONTENTION_RETRY_ATTEMPTS { if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { - return Err(contention_deadline_exhausted( + return Err(crate::write_conflict::contention_deadline_exhausted( "UpdateItem", self.contention_deadline, + &last_cause, )); } // Capture before the transaction's first database operation. A @@ -3917,16 +3920,13 @@ const CONTENTION_RETRY_ATTEMPTS: u32 = 1000; /// 10 s sits above the 9.5 to 12.7 s a put spends losing all 1,000 attempts /// and below botocore's 60 s default read timeout, so a caller gets an answer /// to retry rather than a socket timeout. Which bound a write reaches first -/// depends on the host's load, and which error class the caller is told -/// depends on that for two of the four operations: PutItem answers HTTP 503 -/// on either bound, and `TransactWriteItems` answers -/// `TransactionCanceledException` on either bound, but DeleteItem and -/// UpdateItem answer HTTP 503 on the deadline and 400 -/// `TransactionConflictException` when their attempt budget runs out first -/// (measured in 2 of 4 rounds against a held document, where the 1,000 -/// attempts and the deadline both land near 10 s). #475, the fix for #437, -/// replaces the 400 of those two paths, after which every single-item write -/// answers 503 whichever bound it reaches. +/// depends on the host's load: against a held document, where the 1,000 +/// attempts and the deadline both land near 10 s, a DeleteItem reached its +/// attempt budget first in 2 of 4 rounds. The caller gets the same error class +/// on either bound: HTTP 503 for the single-item writes (see +/// `write_conflict::retry_budget_exhausted` and +/// `write_conflict::contention_deadline_exhausted`) and +/// `TransactionCanceledException` for `TransactWriteItems`. /// /// The deadline is read before an attempt starts, so a request answers within /// the deadline plus the attempt already running. That attempt has no bound @@ -4149,24 +4149,6 @@ fn contention_clock_start() -> std::time::Instant { extenddb_storage::request_clock::started().unwrap_or_else(std::time::Instant::now) } -/// The error for a write that stopped retrying because the engine's -/// contention deadline passed while it was losing to concurrent writers. -/// `operation` names the API operation and `deadline` is the deadline that -/// passed, in seconds with any fraction, so a deadline a test sets below one -/// second does not read as 0 seconds. -/// -/// Like the attempt-budget give-up of PutItem this is a -/// [`StorageError::Transient`], which the engine answers as HTTP 503 -/// `ServiceUnavailable`: the SDK retry modes retry a 503 on their own and the -/// retry succeeds once the contention has passed, whereas a 400 -/// `TransactionConflictException` is handed to the application. -fn contention_deadline_exhausted(operation: &str, deadline: std::time::Duration) -> StorageError { - StorageError::Transient(format!( - "{operation}: the write lost to concurrent writers for {} seconds; retry the request", - deadline.as_secs_f64() - )) -} - /// Build a `mongodb::error::Error` carrying the given error labels. /// /// The driver's `Error::new` is crate private, but `Error::new` copies the @@ -4779,40 +4761,6 @@ mod tests { assert_eq!(inside, (batch_started, batch_started)); } - /// The give-up on the deadline is the transient error the engine answers - /// as HTTP 503, and its message names the operation and the wait. - #[test] - fn the_deadline_give_up_is_transient_and_names_the_operation() { - for operation in ["PutItem", "DeleteItem", "UpdateItem"] { - match contention_deadline_exhausted(operation, CONTENTION_DEADLINE) { - StorageError::Transient(msg) => { - assert!(msg.starts_with(&format!("{operation}: ")), "{msg}"); - assert!( - msg.contains("lost to concurrent writers for 10 seconds"), - "{msg}" - ); - assert!(msg.contains("retry the request"), "{msg}"); - } - other => panic!("want Transient, got {other:?}"), - } - } - // The message carries the deadline that passed, not the default, and - // a deadline below one second keeps its fraction. - for (deadline, wait) in [ - (std::time::Duration::from_secs(2), "2 seconds"), - (std::time::Duration::from_millis(250), "0.25 seconds"), - (std::time::Duration::from_millis(1500), "1.5 seconds"), - ] { - match contention_deadline_exhausted("PutItem", deadline) { - StorageError::Transient(msg) => assert!( - msg.contains(&format!("lost to concurrent writers for {wait};")), - "{msg}" - ), - other => panic!("want Transient, got {other:?}"), - } - } - } - /// Build a write error with the given code, the form `insert_one` /// returns. fn write_error(code: i32) -> mongodb::error::Error { diff --git a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs index 91bdda527..f67f81154 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs @@ -276,12 +276,9 @@ async fn live_a_single_item_write_that_reaches_the_deadline_answers_a_transient_ let elapsed = started.elapsed(); match res { Err(StorageError::Transient(msg)) => { - assert_eq!( - msg, - format!( - "{operation}: the write lost to concurrent writers for 2 seconds; \ - retry the request" - ) + assert!( + msg.starts_with(&format!("{operation}: could not commit in 2 seconds: ")), + "{msg}" ); println!("{operation} gave up on the deadline in {elapsed:?}: {msg}"); } @@ -355,9 +352,9 @@ async fn live_a_write_in_a_request_scope_measures_the_deadline_from_the_scope_st let _ = holder.abort_transaction().await; match held { - Err(StorageError::Transient(msg)) => assert_eq!( - msg, - "PutItem: the write lost to concurrent writers for 2 seconds; retry the request" + Err(StorageError::Transient(msg)) => assert!( + msg.starts_with("PutItem: could not commit in 2 seconds: "), + "{msg}" ), other => panic!("want the transient deadline error, got: {other:?}"), } diff --git a/crates/storage-mongodb/src/write_conflict.rs b/crates/storage-mongodb/src/write_conflict.rs index 1ba39c867..1164c1050 100644 --- a/crates/storage-mongodb/src/write_conflict.rs +++ b/crates/storage-mongodb/src/write_conflict.rs @@ -15,6 +15,8 @@ //! loops instead of folding it into `StorageError::Internal`, which answers //! HTTP 500. +use std::time::Duration; + use extenddb_storage::error::StorageError; use crate::data_engine::is_transient_write_conflict; @@ -68,6 +70,23 @@ pub(crate) fn retry_budget_exhausted(operation: &str, attempts: u32, cause: &str )) } +/// [`retry_budget_exhausted`] for the other bound of the same retry loops: a +/// single-item write that stopped retrying when the engine's contention +/// deadline passed, after an attempt that failed with `cause`. +/// +/// `deadline` is written in seconds with any fraction, so a deadline a test +/// sets below one second does not read as 0 seconds. +pub(crate) fn contention_deadline_exhausted( + operation: &str, + deadline: Duration, + cause: &str, +) -> StorageError { + StorageError::Transient(format!( + "{operation}: could not commit in {} seconds: {cause}", + deadline.as_secs_f64() + )) +} + /// Note that attempt `attempt` of `operation` ended in an error that a re-run /// may clear: one debug line, and the cause back to the caller, which keeps the /// last one for [`retry_budget_exhausted`]. @@ -145,6 +164,25 @@ mod tests { } } + #[test] + fn a_passed_deadline_is_transient_and_names_the_operation_the_deadline_and_the_cause() { + let cause = "Command failed: WriteConflict (code 112) on the item"; + // A deadline below one second keeps its fraction. + for (operation, deadline, seconds) in [ + ("PutItem", crate::data_engine::CONTENTION_DEADLINE, "10"), + ("DeleteItem", Duration::from_millis(250), "0.25"), + ("UpdateItem", Duration::from_millis(1500), "1.5"), + ] { + match contention_deadline_exhausted(operation, deadline, cause) { + StorageError::Transient(msg) => assert_eq!( + msg, + format!("{operation}: could not commit in {seconds} seconds: {cause}") + ), + other => panic!("want Transient, got {other:?}"), + } + } + } + #[test] fn a_retry_hands_its_cause_back_for_the_exhaustion_message() { assert_eq!( From b53bacea5f94b0b35a8a6a8fff8907c048742e32 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:46:14 +0000 Subject: [PATCH 23/44] test(mongodb): build the stream lifecycle test engine through for_test Main added stream_live_tests.rs with a MongoEngine struct literal. This branch added the contention_deadline field, which that literal does not set, so the test build failed. The test engine now comes from MongoEngine::for_test, which gives every field the value the literal gave it. --- crates/storage-mongodb/src/stream_live_tests.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/crates/storage-mongodb/src/stream_live_tests.rs b/crates/storage-mongodb/src/stream_live_tests.rs index 67c9ff6da..10043ed90 100644 --- a/crates/storage-mongodb/src/stream_live_tests.rs +++ b/crates/storage-mongodb/src/stream_live_tests.rs @@ -11,8 +11,6 @@ //! `cargo test -p extenddb-storage-mongodb --lib -- --ignored live_stream`. //! Each test uses its own catalog and data databases and drops them. -use std::sync::Arc; - use mongodb::bson::{Document, doc}; use extenddb_core::types::{ @@ -37,15 +35,11 @@ fn uri() -> String { async fn engine() -> MongoEngine { let tag = uuid::Uuid::new_v4().simple().to_string(); let client = mongodb::Client::with_uri_str(uri()).await.expect("client"); - let engine = MongoEngine { - client: client.clone(), - catalog_db: client.database(&format!("s{}_cat", &tag[..12])), - data_db: client.database(&format!("s{}_data", &tag[..12])), - region: "us-east-1".to_owned(), - gsi_cache: Arc::default(), - tx_read_concern: mongodb::options::ReadConcern::snapshot(), - backup_liveness: crate::backup_engine::BackupLiveness::default(), - }; + let engine = MongoEngine::for_test( + &client, + client.database(&format!("s{}_cat", &tag[..12])), + client.database(&format!("s{}_data", &tag[..12])), + ); engine .catalog_db .collection::("settings") From 0f9256badb3dab1617c7e429c70e116c1eece79c Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:46:19 +0000 Subject: [PATCH 24/44] test(batch): drop the MongoDB test of a batch with no request applied Main answers 503 ServiceUnavailable to a BatchWriteItem that applied none of its requests. Its write_batch_deferral_tests.rs checks that answer for Transient and TransactionConflict errors. The MongoDB wire test of a batch whose only request reached the deadline checked the same answer, so it goes. The other two tests check the deadline on a held item and the shared deadline of this branch, and stay. --- tests/test_mongodb_batch_write_contention.py | 23 -------------------- 1 file changed, 23 deletions(-) diff --git a/tests/test_mongodb_batch_write_contention.py b/tests/test_mongodb_batch_write_contention.py index cb7ec9f07..73f50633f 100644 --- a/tests/test_mongodb_batch_write_contention.py +++ b/tests/test_mongodb_batch_write_contention.py @@ -30,7 +30,6 @@ import boto3 import pytest from botocore.config import Config -from botocore.exceptions import ClientError from conftest import scoped_table @@ -206,25 +205,3 @@ def test_the_requests_of_a_batch_share_one_contention_deadline( assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" assert _w(dynamodb_client, gsi_table, b1) == "v1" assert _w(dynamodb_client, gsi_table, b2) == "v0" - - -def test_a_batch_none_of_whose_requests_was_applied_answers_the_error( - dynamodb_client, endpoint_url, mongodb_container, gsi_table -): - """A batch whose only request reaches the deadline answers 503 and applies nothing.""" - b = f"b-{uuid.uuid4().hex[:8]}" - dynamodb_client.put_item(TableName=gsi_table, Item=_item(b, "v0")) - client = _client(endpoint_url, retries=False) - - hold = Hold(mongodb_container, gsi_table, b, HOLD) - started = time.monotonic() - with pytest.raises(ClientError) as err: - client.batch_write_item( - RequestItems={gsi_table: [{"PutRequest": {"Item": _item(b, "v1")}}]} - ) - elapsed = time.monotonic() - started - hold.wait() - - assert err.value.response["ResponseMetadata"]["HTTPStatusCode"] == 503 - assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" - assert _w(dynamodb_client, gsi_table, b) == "v0", "the put of B was applied" From ef7548c80f615504b5f8a6195e4e506880c59b5d Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:49:10 +0000 Subject: [PATCH 25/44] docs(mongodb): state the batch and give-up answers that main brings Main answers 503 to a BatchWriteItem that applied none of its requests, and its consumed capacity counts the applied requests only. A DeleteItem or UpdateItem that spends its attempt budget also answers 503 on main. The contention row in docs/differences-from-dynamodb.md now states those answers. The row from main for contended writes on a MongoDB table gave the limit as 50 attempts. It now gives both bounds of this branch, 1,000 attempts and 10 seconds. The RFC names contention_deadline_exhausted beside retry_budget_exhausted. --- docs/differences-from-dynamodb.md | 4 ++-- docs/rfcs/0000-mongodb-backend.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index 84aa169a4..ddadb4d4f 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -21,7 +21,7 @@ from a request sent to that service or backend. | Transaction read concern (MongoDB backend only) | No user-configurable equivalent | `snapshot` is the default, fidelity-preserving mode. With `majority` or `local`, `TransactGetItems` is not guaranteed a single point-in-time snapshot and transaction condition reads have weaker isolation. With `local`, a condition can be evaluated against data that is later rolled back after failover. | | Contended `TransactWriteItems` | A transaction that conflicts with another in-flight transaction on the same item is canceled with a `TransactionConflict` reason ("Transaction is ongoing for the item") | **PostgreSQL** queues the second transaction on the first one's row locks and **SQLite** runs one writer at a time, so both normally commit and no `TransactionConflict` is returned. PostgreSQL locks each transaction's items in table and key order, so two `TransactWriteItems` cannot deadlock on each other. A PutItem, UpdateItem, DeleteItem or BatchWriteItem of an item that a `TransactWriteItems` has locked also waits for it, where the Amazon DynamoDB developer guide lists that request as a conflict. On PostgreSQL this includes an item that the transaction checks or deletes while it is missing: the transaction holds the key until it commits or rolls back, so a request that creates the item waits. Many transactions that check one hot missing item queue less fairly than on an existing item, because every waiter wakes at each commit and only one proceeds. Each such check or delete of a missing item also inserts and deletes its key, so it writes WAL and leaves a dead row and index entry for autovacuum, even though it changes no item. When PostgreSQL still aborts a transaction (a deadlock with another lock holder, or a serialization failure under a stricter `default_transaction_isolation`), the answer is a `TransactionCanceledException` with a `TransactionConflict` reason, not a 500. **MongoDB** retries write conflicts internally and cancels with `TransactionConflict` on every item only after sustained contention. A client that relies on `TransactionConflict` to back off does not see it under ordinary contention. | | `BatchWriteItem` where no request could be applied now | A request that could not be applied is returned in `UnprocessedItems` with a 200 when another request of the batch applied, whichever came first (measured 2026-10-27 in us-east-1, client retries off, while a `TransactWriteItems` held the items: a batch of two requests, one on a held item, answered 200 with that request unprocessed and the other applied). When no request applied the batch answers an error, not a 200 with every request unprocessed (measured 2026-10-27 in us-east-1, client retries off, with every request on an item a `TransactWriteItems` held, for one request and for two: 500 `InternalServerError`) | The same 200 for a partial batch. When no request could be applied the answer is 503 `ServiceUnavailable`, which the SDKs retry with backoff, so a client's `UnprocessedItems` loop does not spin on a batch that makes no progress. Every request is tried before the batch answers, so a batch of 25 contended requests can spend 25 retry budgets in one call. A connection error ends the attempt: the request that met it and the requests after it are returned unprocessed, or the batch answers 503 if nothing applied. An internal storage error fails the batch with 500 as before. On a Cassandra table without TTL, a put or a delete of an item that a `TransactWriteItems` holds returns `TransactionCanceled`, which is not left for the client to replay, so the batch answers 400 `TransactionCanceledException` after the requests ahead of it were applied. A Cassandra write that spends its retries on a lost race, or on a held item of a TTL table, returns `TransactionConflict`, left in `UnprocessedItems`. A table of which no request applied gets no `ConsumedCapacity` entry. What the service reports for such a table in a partial batch is not measured. | -| Contended single-item writes on a MongoDB table that has a stream or an index | `PutItem`, `DeleteItem` and `UpdateItem` on one item are applied one after another and each answers 200 (measured 2026-10-23, client retries off, on a table with a GSI and a stream); `TransactionConflictException` is answered to them only while a `TransactWriteItems` holds the item | MongoDB aborts the later of two transactions that write one document, so each write runs as a transaction that is re-run with jittered backoff, up to 50 attempts. A write on a table with a stream also conflicts on the sequence counter of the shard it is assigned to, so writers of different keys can conflict. A write that could not commit in 50 attempts answers 503 `ServiceUnavailable`, which the SDKs retry, where the service answers 200. The server records that 503 as a system error, not under `TransactionConflict` (which counts `TransactionConflictException` answers), so contention shows in the system error metrics. The warning in the server log carries the last error that ended an attempt of the failed write. | +| Contended single-item writes on a MongoDB table that has a stream or an index | `PutItem`, `DeleteItem` and `UpdateItem` on one item are applied one after another and each answers 200 (measured 2026-10-23, client retries off, on a table with a GSI and a stream); `TransactionConflictException` is answered to them only while a `TransactWriteItems` holds the item | MongoDB aborts the later of two transactions that write one document, so each write runs as a transaction that is re-run with jittered backoff, up to 1,000 attempts or 10 seconds (see "Sustained contention on one item" under Capacity and Throttling). A write on a table with a stream also conflicts on the sequence counter of the shard it is assigned to, so writers of different keys can conflict. A write that reaches either bound answers 503 `ServiceUnavailable`, which the SDKs retry, where the service answers 200. The server records that 503 as a system error, not under `TransactionConflict` (which counts `TransactionConflictException` answers), so contention shows in the system error metrics. The warning in the server log carries the last error that ended an attempt of the failed write. | ## Authentication and Authorization (AWS IAM/STS auth surface used by DynamoDB) @@ -203,7 +203,7 @@ alone, with the same wording. For PartiQL this holds in `ExecuteStatement`, | On-demand capacity | Automatic scaling | Fixed initial burst capacity (4000 WCU / 12000 RCU), no auto-scaling | | Warm throughput | Pre-provisions partitions to the requested read and write units; raising it is a long-running operation (`Status: UPDATING` for minutes, the previous values reported meanwhile); a request above the account's table maximum is `LimitExceededException` | Stored and reported with the same shape, defaults and validation so clients that read it (the Terraform AWS provider since 6.13.0) work: the previous values are reported with `Status: UPDATING` for `control_plane_delay_seconds` after a change, then the new values `ACTIVE`; a decrease is measured against the reported values, as on the service. Nothing acts on the values: no pre-warming and no scaling at the database level. The account maximum is not known to ExtendDB and is not enforced. A provisioned table whose warm throughput was never set, or was set on one side only, reports its new units at once after a capacity change, where the service reports the old units until the table is `ACTIVE`. Switching a table to `PAY_PER_REQUEST` stores 12000 read and 4000 write units where its warm throughput is lower, and the stored values stay after a switch back to `PROVISIONED`, as on the service; the response to the switch reads the new values with `Status: UPDATING` for `control_plane_delay_seconds`, where the service answers with the previous values and `Status: ACTIVE` | | Throttling | Always on; throttles requests that exceed provisioned/burst capacity. No setting to disable | Configurable via `throttling_enabled` runtime setting (default: `true`) | -| Sustained contention on one item (MongoDB backend) | Applies concurrent single-item writes on one key one after another and answers 200 to each. Measured to every call at 32 and 64 writers putting one key of a table with a GSI. A `PutItem` is answered 400 `TransactionConflictException` only while an in-flight `TransactWriteItems` holds the item (33 of 80 calls measured), and a `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | A write on a table with a secondary index writes the item and its index entries in one transaction, so concurrent writes on one key abort each other and are retried. One request's retry loop stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key: `PutItem` answers 503 `ServiceUnavailable` on either bound, which the SDK retry modes retry and which succeeds once the contention has passed. `DeleteItem` and `UpdateItem` answer 503 on the deadline and 400 `TransactionConflictException` when their 1,000 attempts run out first. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. In a `BatchWriteItem` the write requests share one deadline, measured from the start of the request: a request that reaches it is returned in `UnprocessedItems` and the other requests are still applied, which is how the service answers a batch with a request on an item a transaction holds. When none of its requests was applied, the batch answers the error its request gave up with: 503 `ServiceUnavailable`, or 400 `TransactionConflictException` when a `DeleteItem` runs out of its 1,000 attempts first. The service answered 500 `InternalServerError` to 54 of 80 batches whose only request was on an item a transaction held. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | +| Sustained contention on one item (MongoDB backend) | Applies concurrent single-item writes on one key one after another and answers 200 to each. Measured to every call at 32 and 64 writers putting one key of a table with a GSI. A `PutItem` is answered 400 `TransactionConflictException` only while an in-flight `TransactWriteItems` holds the item (33 of 80 calls measured), and a `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | A write on a table with a secondary index writes the item and its index entries in one transaction, so concurrent writes on one key abort each other and are retried. One request's retry loop stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key: `PutItem`, `DeleteItem` and `UpdateItem` answer 503 `ServiceUnavailable` on either bound, which the SDK retry modes retry and which succeeds once the contention has passed. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. In a `BatchWriteItem` the write requests share one deadline, measured from the start of the request: a request that reaches it is returned in `UnprocessedItems` and the other requests are still applied, which is how the service answers a batch with a request on an item a transaction holds. The consumed capacity counts the applied requests only. When none of its requests was applied, the batch answers 503 `ServiceUnavailable`. The service answered 500 `InternalServerError` to 54 of 80 batches whose only request was on an item a transaction held. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | ## PartiQL diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index 46b5c797f..0f32ca485 100644 --- a/docs/rfcs/0000-mongodb-backend.md +++ b/docs/rfcs/0000-mongodb-backend.md @@ -136,7 +136,7 @@ Transaction implementation: `data_engine.rs` — `transact_write_items_impl`, `t ### Write conflict handling -**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place, which is idempotent, up to `TRANSIENT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff up to `CONTENTION_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). `backoff_sleep` waits a uniform 0 to 50 µs × 2^min(attempt, 8), which is at most 12.8 ms from the ninth attempt on and sums to at most about 550 ms over 50 attempts (275 ms expected); tokio's timer ticks every millisecond and rounds a sleep up to the next tick, so for the first five attempts (longest jitter under 1 ms) the wait is set by the tick, not by the jitter. The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The helpers that run inside the transaction (index entry writes, the stream shard lookup, the stream sequence counter draw and the stream record insert) map a transient conflict through `write_conflict::transaction_error` to `StorageError::Transient`, which the transaction bodies convert into a re-run of the transaction; any other driver error stays `StorageError::Internal`. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The TTL worker's conditional delete runs the DeleteItem loop, so it takes the deadline too. `BatchWriteItem` runs its writes inside one `request_clock` scope, so the loops of one batch measure one deadline from the start of the request, and the handler returns a request that reaches it in `UnprocessedItems`. The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race gives up after `TRANSIENT_RETRY_ATTEMPTS` (50) lost races. Exhausted retries on PutItem, DeleteItem and UpdateItem all return the `StorageError::Transient` built by `write_conflict::retry_budget_exhausted` (its message names the operation, the number of attempts and the last error that ended an attempt), and a write that reaches the deadline returns one of the same form that names the deadline instead of the attempts; both are answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry. On `TransactWriteItems` either bound surfaces as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. +**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place, which is idempotent, up to `TRANSIENT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff up to `CONTENTION_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). `backoff_sleep` waits a uniform 0 to 50 µs × 2^min(attempt, 8), which is at most 12.8 ms from the ninth attempt on and sums to at most about 550 ms over 50 attempts (275 ms expected); tokio's timer ticks every millisecond and rounds a sleep up to the next tick, so for the first five attempts (longest jitter under 1 ms) the wait is set by the tick, not by the jitter. The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The helpers that run inside the transaction (index entry writes, the stream shard lookup, the stream sequence counter draw and the stream record insert) map a transient conflict through `write_conflict::transaction_error` to `StorageError::Transient`, which the transaction bodies convert into a re-run of the transaction; any other driver error stays `StorageError::Internal`. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The TTL worker's conditional delete runs the DeleteItem loop, so it takes the deadline too. `BatchWriteItem` runs its writes inside one `request_clock` scope, so the loops of one batch measure one deadline from the start of the request, and the handler returns a request that reaches it in `UnprocessedItems`. The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race gives up after `TRANSIENT_RETRY_ATTEMPTS` (50) lost races. Exhausted retries on PutItem, DeleteItem and UpdateItem all return the `StorageError::Transient` built by `write_conflict::retry_budget_exhausted` (its message names the operation, the number of attempts and the last error that ended an attempt), answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on `TransactWriteItems` they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. A write that reaches the deadline gives up the same way: PutItem, DeleteItem and UpdateItem return the `StorageError::Transient` built by `write_conflict::contention_deadline_exhausted`, which names the deadline instead of the attempts, and a `TransactWriteItems` answers as it does when its attempts run out. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. **UpdateItem** additionally uses an optimistic-concurrency version guard on top of the transaction. A `_v` counter is stored on each document. The write path reads the current `_v` under the transaction snapshot, applies the update expression in memory, sets `_v = current_version + 1`, then executes `replace_one` filtered on both the primary key AND the expected `_v`. If `matched_count == 0`, a concurrent writer committed a higher version between the snapshot read and the replace; the attempt aborts and the outer retry loop re-reads. The version guard doubles up when a native-fast-path update (unconditional, no stream, no GSI) is possible — that path uses a single `find_one_and_update` with `$inc: {_v: 1}` outside a transaction, and always bumps the counter so a concurrent session-scoped update against a stale snapshot fails its versioned filter and retries. From 1a7eb7520d9fe75f3865eb320ab4087b8bf31fcd Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:56:21 +0000 Subject: [PATCH 26/44] docs(mongodb): say which error the 50-attempt loops delay The comment on TRANSIENT_RETRY_ATTEMPTS said that a longer budget only delays an HTTP 500. UpdateItem's native upsert gives up a lost create race with TransactionConflict. The comment now says that a longer budget delays the error each loop answers when it runs out. --- crates/storage-mongodb/src/data_engine.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 60c52d0ef..f4919d8b7 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -3874,8 +3874,8 @@ pub(crate) struct GsiBackfillProgress { /// again. #431 measured this budget, with its backoff, as the one that lets /// that race resolve: a put blocked by a document that is removed 100 ms after /// the put starts commits well inside it, and the storage tests fail when the -/// cap is put back to 5. A longer budget on either loop only delays the HTTP -/// 500 it answers. +/// cap is put back to 5. A longer budget on the commit retry or a create race +/// only delays the error that loop answers when it runs out. pub(crate) const TRANSIENT_RETRY_ATTEMPTS: u32 = 50; /// Maximum number of times a contention retry loop re-runs a transaction that From 6ddff4c7d5dab7bba2308a35e480a4e467c900a4 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:06:59 +0000 Subject: [PATCH 27/44] fix(mongodb): log the attempts a TransactWriteItems ran when it gives up The TransactWriteItems retry loop stops at the contention deadline as well as at CONTENTION_RETRY_ATTEMPTS. Its give-up warning reported 1,000 attempts in both cases. The loop now records the attempt at which the deadline stopped it, and the warning reports the attempts the loop ran. --- crates/storage-mongodb/src/data_engine.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index f4919d8b7..6d684ae6e 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -2472,6 +2472,8 @@ impl MongoEngine { let contention_started = contention_clock_start(); let mut last_cause = String::new(); + // The attempts the loop ran: every one, unless the deadline ended it. + let mut attempts_run = CONTENTION_RETRY_ATTEMPTS; for attempt in 0..CONTENTION_RETRY_ATTEMPTS { // A transaction that reaches the deadline stops retrying and // falls through to the give-up after the loop, which answers @@ -2480,6 +2482,7 @@ impl MongoEngine { // transaction that loses to contention. Only the single-item // writes answer the deadline with HTTP 503. if contention_deadline_passed(attempt, contention_started, self.contention_deadline) { + attempts_run = attempt; break; } let idempotency = idem_owned.as_ref().map(|(a, t, f)| IdempotencyKey { @@ -2679,7 +2682,7 @@ impl MongoEngine { // TransactionCanceled to TransactionCanceledException; the // reason codes are echoed back in the message. tracing::warn!( - attempts = CONTENTION_RETRY_ATTEMPTS, + attempts = attempts_run, error = %last_cause, "TransactWriteItems: could not commit; answering TransactionCanceled" ); From c1524f0c2e4c2bf44e8d21b75ecd8e5514438847 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:08:28 +0000 Subject: [PATCH 28/44] docs(mongodb): give the design doc the contention budget and deadline docs/design/13-storage-mongodb.md said that the PutItem, DeleteItem, UpdateItem and TransactWriteItems retry loop reads TRANSIENT_RETRY_ATTEMPTS and makes 50 attempts. On this branch the loop reads CONTENTION_RETRY_ATTEMPTS (1,000 attempts) and stops at CONTENTION_DEADLINE (10 s). The retry loop paragraph and the WriteConflict handling row now state both bounds, and the paragraph gives the sleep total of 1,000 attempts instead of 50. --- docs/design/13-storage-mongodb.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/design/13-storage-mongodb.md b/docs/design/13-storage-mongodb.md index 0f1b6990b..61755ebc0 100644 --- a/docs/design/13-storage-mongodb.md +++ b/docs/design/13-storage-mongodb.md @@ -743,13 +743,15 @@ re-run, and `classify_commit_error` retries `commitTransaction` in place, which is idempotent. **Retry loop.** Session-scoped writes (Put / Delete / Update / TWI) wrap -the transaction body in a `for attempt in 0..TRANSIENT_RETRY_ATTEMPTS` -loop (50 attempts). Each attempt starts a fresh transaction, runs the -body, and either commits, aborts and retries (transient), or aborts and -returns (fatal). Retries sleep with jittered exponential backoff: +the transaction body in a `for attempt in 0..CONTENTION_RETRY_ATTEMPTS` +loop (1,000 attempts). Before each attempt after the first, the loop stops +once `CONTENTION_DEADLINE` (10 s) has passed since the request started. +Each attempt starts a fresh transaction, runs the body, and either +commits, aborts and retries (transient), or aborts and returns (fatal). +Retries sleep with jittered exponential backoff: `backoff_sleep` waits a uniform 0 to 50 µs × 2^min(attempt, 8), which is at -most 12.8 ms from the ninth attempt on and sums to at most about 550 ms -over the 50 attempts (275 ms expected). Tokio's timer ticks every +most 12.8 ms from the ninth attempt on and sums to at most about 12.7 s +over the 1,000 attempts (6.4 s expected). Tokio's timer ticks every millisecond and rounds a sleep up to the next tick, so for the first five attempts (longest jitter under 1 ms) the wait is set by the tick, not by the jitter. The helpers that run inside the transaction (index entry @@ -1131,7 +1133,7 @@ The backend implements every trait in `extenddb-storage`: | Sequence assignment | Inside the write session | Prevents ordering holes where a fast writer commits a higher seq before a slower earlier one does. | | Stream shard ID | `shardId-{table_id}-{i:012}` | Cross-tenant isolation on same-named tables. | | Stream retention | 24h TTL index on `stream_records.created_at` + hourly cleanup worker | Primary enforcement at storage; worker is defense in depth. | -| WriteConflict handling | Retry with jittered exponential backoff (50 attempts); TWI exhaustion → `TransactionCanceled` with synthetic `TransactionConflict` reasons | Bounded tail latency; DDB-canonical error surface. | +| WriteConflict handling | Retry with jittered exponential backoff (up to 1,000 attempts or 10 s, whichever comes first); TWI exhaustion → `TransactionCanceled` with synthetic `TransactionConflict` reasons | Bounded tail latency; DDB-canonical error surface. | | UpdateItem concurrency | Configured transaction read concern + `_v` version filter + retry; native fast-path always `$inc: {_v: 1}` | Prevents lost updates; snapshot mode additionally supplies a point-in-time read view. | | Idempotency retention | Unique `(account_id, token)` index + 540s TTL + 600 ms data-plane age filter | Unique-index race safety under every accepted read concern; ≤10-min worst-case retention regardless of TTL-monitor cadence. | | Backups | Per-backup collection via server-side `$out` aggregation | No per-item driver traffic; metadata schema decoupled from collection name. | From 6823beee5afedcd94afe8f88f8bedeb19aeb4dc3 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:11:48 +0000 Subject: [PATCH 29/44] docs: state each contended MongoDB write fact in one row Two rows of docs/differences-from-dynamodb.md describe contended writes on MongoDB. The first is "Contended single-item writes on a MongoDB table that has a stream or an index". The second is "Sustained contention on one item (MongoDB backend)". Both stated four facts: - The service applies concurrent single-item writes on one key in order and answers 200 to each. - It answers TransactionConflictException to them only while a TransactWriteItems holds the item. - MongoDB aborts one of two transactions on a document, and ExtendDB retries the write. - At either bound a single-item write answers 503. The first row keeps these facts, with the measurements of both rows. The retry bounds of 1,000 attempts and 10 seconds stay in the second row only. Each row points to the other. --- docs/differences-from-dynamodb.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index ddadb4d4f..2fff7784f 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -21,7 +21,7 @@ from a request sent to that service or backend. | Transaction read concern (MongoDB backend only) | No user-configurable equivalent | `snapshot` is the default, fidelity-preserving mode. With `majority` or `local`, `TransactGetItems` is not guaranteed a single point-in-time snapshot and transaction condition reads have weaker isolation. With `local`, a condition can be evaluated against data that is later rolled back after failover. | | Contended `TransactWriteItems` | A transaction that conflicts with another in-flight transaction on the same item is canceled with a `TransactionConflict` reason ("Transaction is ongoing for the item") | **PostgreSQL** queues the second transaction on the first one's row locks and **SQLite** runs one writer at a time, so both normally commit and no `TransactionConflict` is returned. PostgreSQL locks each transaction's items in table and key order, so two `TransactWriteItems` cannot deadlock on each other. A PutItem, UpdateItem, DeleteItem or BatchWriteItem of an item that a `TransactWriteItems` has locked also waits for it, where the Amazon DynamoDB developer guide lists that request as a conflict. On PostgreSQL this includes an item that the transaction checks or deletes while it is missing: the transaction holds the key until it commits or rolls back, so a request that creates the item waits. Many transactions that check one hot missing item queue less fairly than on an existing item, because every waiter wakes at each commit and only one proceeds. Each such check or delete of a missing item also inserts and deletes its key, so it writes WAL and leaves a dead row and index entry for autovacuum, even though it changes no item. When PostgreSQL still aborts a transaction (a deadlock with another lock holder, or a serialization failure under a stricter `default_transaction_isolation`), the answer is a `TransactionCanceledException` with a `TransactionConflict` reason, not a 500. **MongoDB** retries write conflicts internally and cancels with `TransactionConflict` on every item only after sustained contention. A client that relies on `TransactionConflict` to back off does not see it under ordinary contention. | | `BatchWriteItem` where no request could be applied now | A request that could not be applied is returned in `UnprocessedItems` with a 200 when another request of the batch applied, whichever came first (measured 2026-10-27 in us-east-1, client retries off, while a `TransactWriteItems` held the items: a batch of two requests, one on a held item, answered 200 with that request unprocessed and the other applied). When no request applied the batch answers an error, not a 200 with every request unprocessed (measured 2026-10-27 in us-east-1, client retries off, with every request on an item a `TransactWriteItems` held, for one request and for two: 500 `InternalServerError`) | The same 200 for a partial batch. When no request could be applied the answer is 503 `ServiceUnavailable`, which the SDKs retry with backoff, so a client's `UnprocessedItems` loop does not spin on a batch that makes no progress. Every request is tried before the batch answers, so a batch of 25 contended requests can spend 25 retry budgets in one call. A connection error ends the attempt: the request that met it and the requests after it are returned unprocessed, or the batch answers 503 if nothing applied. An internal storage error fails the batch with 500 as before. On a Cassandra table without TTL, a put or a delete of an item that a `TransactWriteItems` holds returns `TransactionCanceled`, which is not left for the client to replay, so the batch answers 400 `TransactionCanceledException` after the requests ahead of it were applied. A Cassandra write that spends its retries on a lost race, or on a held item of a TTL table, returns `TransactionConflict`, left in `UnprocessedItems`. A table of which no request applied gets no `ConsumedCapacity` entry. What the service reports for such a table in a partial batch is not measured. | -| Contended single-item writes on a MongoDB table that has a stream or an index | `PutItem`, `DeleteItem` and `UpdateItem` on one item are applied one after another and each answers 200 (measured 2026-10-23, client retries off, on a table with a GSI and a stream); `TransactionConflictException` is answered to them only while a `TransactWriteItems` holds the item | MongoDB aborts the later of two transactions that write one document, so each write runs as a transaction that is re-run with jittered backoff, up to 1,000 attempts or 10 seconds (see "Sustained contention on one item" under Capacity and Throttling). A write on a table with a stream also conflicts on the sequence counter of the shard it is assigned to, so writers of different keys can conflict. A write that reaches either bound answers 503 `ServiceUnavailable`, which the SDKs retry, where the service answers 200. The server records that 503 as a system error, not under `TransactionConflict` (which counts `TransactionConflictException` answers), so contention shows in the system error metrics. The warning in the server log carries the last error that ended an attempt of the failed write. | +| Contended single-item writes on a MongoDB table that has a stream or an index | `PutItem`, `DeleteItem` and `UpdateItem` on one item are applied one after another and each answers 200 (measured 2026-10-23, client retries off, on a table with a GSI and a stream; every call answered 200 at 32 and 64 writers putting one key of a table with a GSI); `TransactionConflictException` is answered to them only while a `TransactWriteItems` holds the item (for `PutItem`, 33 of 80 calls measured) | MongoDB aborts the later of two transactions that write one document, so each write runs as a transaction that is re-run with jittered backoff until the attempt budget or the deadline ends it (see "Sustained contention on one item" under Capacity and Throttling). A write on a table with a stream also conflicts on the sequence counter of the shard it is assigned to, so writers of different keys can conflict. A write that reaches either bound answers 503 `ServiceUnavailable`, which the SDKs retry, where the service answers 200. The server records that 503 as a system error, not under `TransactionConflict` (which counts `TransactionConflictException` answers), so contention shows in the system error metrics. The warning in the server log carries the last error that ended an attempt of the failed write. | ## Authentication and Authorization (AWS IAM/STS auth surface used by DynamoDB) @@ -203,7 +203,7 @@ alone, with the same wording. For PartiQL this holds in `ExecuteStatement`, | On-demand capacity | Automatic scaling | Fixed initial burst capacity (4000 WCU / 12000 RCU), no auto-scaling | | Warm throughput | Pre-provisions partitions to the requested read and write units; raising it is a long-running operation (`Status: UPDATING` for minutes, the previous values reported meanwhile); a request above the account's table maximum is `LimitExceededException` | Stored and reported with the same shape, defaults and validation so clients that read it (the Terraform AWS provider since 6.13.0) work: the previous values are reported with `Status: UPDATING` for `control_plane_delay_seconds` after a change, then the new values `ACTIVE`; a decrease is measured against the reported values, as on the service. Nothing acts on the values: no pre-warming and no scaling at the database level. The account maximum is not known to ExtendDB and is not enforced. A provisioned table whose warm throughput was never set, or was set on one side only, reports its new units at once after a capacity change, where the service reports the old units until the table is `ACTIVE`. Switching a table to `PAY_PER_REQUEST` stores 12000 read and 4000 write units where its warm throughput is lower, and the stored values stay after a switch back to `PROVISIONED`, as on the service; the response to the switch reads the new values with `Status: UPDATING` for `control_plane_delay_seconds`, where the service answers with the previous values and `Status: ACTIVE` | | Throttling | Always on; throttles requests that exceed provisioned/burst capacity. No setting to disable | Configurable via `throttling_enabled` runtime setting (default: `true`) | -| Sustained contention on one item (MongoDB backend) | Applies concurrent single-item writes on one key one after another and answers 200 to each. Measured to every call at 32 and 64 writers putting one key of a table with a GSI. A `PutItem` is answered 400 `TransactionConflictException` only while an in-flight `TransactWriteItems` holds the item (33 of 80 calls measured), and a `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | A write on a table with a secondary index writes the item and its index entries in one transaction, so concurrent writes on one key abort each other and are retried. One request's retry loop stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key: `PutItem`, `DeleteItem` and `UpdateItem` answer 503 `ServiceUnavailable` on either bound, which the SDK retry modes retry and which succeeds once the contention has passed. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. In a `BatchWriteItem` the write requests share one deadline, measured from the start of the request: a request that reaches it is returned in `UnprocessedItems` and the other requests are still applied, which is how the service answers a batch with a request on an item a transaction holds. The consumed capacity counts the applied requests only. When none of its requests was applied, the batch answers 503 `ServiceUnavailable`. The service answered 500 `InternalServerError` to 54 of 80 batches whose only request was on an item a transaction held. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | +| Sustained contention on one item (MongoDB backend) | For `PutItem`, `DeleteItem` and `UpdateItem`, see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure. A `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | One request's retry loop (see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure) stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. In a `BatchWriteItem` the write requests share one deadline, measured from the start of the request: a request that reaches it is returned in `UnprocessedItems` and the other requests are still applied, which is how the service answers a batch with a request on an item a transaction holds. The consumed capacity counts the applied requests only. When none of its requests was applied, the batch answers 503 `ServiceUnavailable`. The service answered 500 `InternalServerError` to 54 of 80 batches whose only request was on an item a transaction held. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | ## PartiQL From ff3626e140de560a493ad32519ffa8631db0b7eb Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:33:59 +0000 Subject: [PATCH 30/44] docs: give the contention bounds where main's text says 50 attempts Main wrote three texts when the single-item writes and TransactWriteItems on MongoDB gave up after 50 attempts. On this branch those loops stop after 1,000 attempts or at the 10 s contention deadline, whichever comes first. - crates/storage/src/error.rs, on StorageError::Transient: a single-item write that could not commit in 1,000 attempts or within the deadline. The message names the bound that ended the retries. - tests/test_concurrency.py: the server's 1,000 attempts or 10 seconds. - tests/test_stream_write_contention.py: MongoDB cancels a transaction after 1,000 attempts or 10 seconds, whichever comes first. --- crates/storage/src/error.rs | 9 +++++---- tests/test_concurrency.py | 3 ++- tests/test_stream_write_contention.py | 11 ++++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/crates/storage/src/error.rs b/crates/storage/src/error.rs index 4da6b694d..157ffb5e7 100755 --- a/crates/storage/src/error.rs +++ b/crates/storage/src/error.rs @@ -77,10 +77,11 @@ pub enum StorageError { /// and the worker dropped claimed rows on transient errors. /// /// The MongoDB backend also returns it for a single-item write that could - /// not commit in its 50 attempts; the message names the operation, the - /// attempt count and the last error that ended an attempt. The engine logs - /// the message and answers HTTP 503 `ServiceUnavailable`, which the SDKs - /// retry. + /// not commit in 1,000 attempts or within its 10 s contention deadline. The + /// message names the operation, the bound that ended the retries (the + /// attempts or the seconds) and the last error that ended an attempt. The + /// engine logs the message and answers HTTP 503 `ServiceUnavailable`, which + /// the SDKs retry. #[error("{0}")] Transient(String), /// The backend discarded a consistent snapshot before the reader drained diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index fae903ed4..7621c4c7c 100755 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -49,7 +49,8 @@ def _retry_on_internal_error(fn, max_retries: int = _MAX_RETRIES): TransactionConflictException (OCC contention under high concurrency, more likely on Cassandra where Paxos latency is higher than on PostgreSQL/SQLite) and ServiceUnavailable (a MongoDB write on a table with - a stream or an index that could not commit in the server's 50 attempts). + a stream or an index that could not commit in the server's 1,000 attempts + or 10 seconds). The SDK retries a 503 only once under this client's max_attempts, so the harness retries it as well. """ diff --git a/tests/test_stream_write_contention.py b/tests/test_stream_write_contention.py index 5e37560f9..aab5ff9fd 100644 --- a/tests/test_stream_write_contention.py +++ b/tests/test_stream_write_contention.py @@ -389,11 +389,12 @@ def test_transactions_on_a_stream_table_all_commit( such a conflict has to run again; a server that reports the conflict as an error, whether 500 or 503, fails here. - After 50 attempts the MongoDB backend cancels a transaction with a - TransactionConflict reason on every operation (the answer for sustained - contention in docs/differences-from-dynamodb.md). Nothing was applied, so - the writer sends the transaction again, up to REPLAY_PASSES times, and the - checks at the end show each transaction applied once. + After 1,000 attempts or 10 seconds, whichever comes first, the MongoDB + backend cancels a transaction with a TransactionConflict reason on every + operation (the answer for sustained contention in + docs/differences-from-dynamodb.md). Nothing was applied, so the writer + sends the transaction again, up to REPLAY_PASSES times, and the checks at + the end show each transaction applied once. """ prefix = f"txn-{uuid.uuid4().hex[:8]}-" for i in range(TXN_WRITERS): From 95dc078ac53cf81075e5ff68f5ce6f802e24bffa Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:35:27 +0000 Subject: [PATCH 31/44] docs(batch): give each side's batch text the other side's rule Main's comment in crates/engine/src/batch_write_item.rs and its row in docs/differences-from-dynamodb.md say that a batch of 25 contended requests can spend 25 retry budgets. On MongoDB the requests of a batch now share one contention deadline, measured from the start of the batch. Both texts now say so. The CONTENTION_DEADLINE comment in data_engine.rs and the RFC say that the handler returns a request that reaches the deadline in UnprocessedItems. Main's handler does that when another request of the batch applied, and answers 503 when none did. Both texts now give both cases. --- crates/engine/src/batch_write_item.rs | 5 ++++- crates/storage-mongodb/src/data_engine.rs | 3 ++- docs/differences-from-dynamodb.md | 2 +- docs/rfcs/0000-mongodb-backend.md | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/batch_write_item.rs b/crates/engine/src/batch_write_item.rs index 9d184d94a..99883ea33 100755 --- a/crates/engine/src/batch_write_item.rs +++ b/crates/engine/src/batch_write_item.rs @@ -190,7 +190,10 @@ async fn write_batch( // retry with backoff, so a client's UnprocessedItems loop cannot spin on a // batch that makes no progress. Each failed request spends its own retry // budget first, so a batch of 25 contended requests can spend 25 budgets - // before it answers. + // before it answers. On MongoDB the requests also share one contention + // deadline, measured from the start of the batch (the request clock scope + // `handle_batch_write_item` opens), and no transaction retry starts once + // it has passed. // // The request faults the same page lists reject the whole batch before // anything is written. `check_batch` finds a missing table, a key that diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 6d684ae6e..7275bd909 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -3943,7 +3943,8 @@ const CONTENTION_RETRY_ATTEMPTS: u32 = 1000; /// spent attempt budget, with `TransactionCanceledException`. A loop measures /// the deadline from the start of its request (see `contention_clock_start`): /// the write requests of one `BatchWriteItem` share one deadline, and the -/// handler returns a request that reaches it in UnprocessedItems. The TTL +/// handler returns a request that reaches it in UnprocessedItems when another +/// request of the batch applied, and answers 503 when none did. The TTL /// worker's conditional delete runs the delete loop too, outside any request, /// so an expired item that stays contended holds the sweep until the deadline /// or the attempt budget ends that loop, where 50 attempts ended it before diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index 2fff7784f..620601420 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -20,7 +20,7 @@ from a request sent to that service or backend. | `TransactGetItems` overlapping an in-flight `TransactWriteItems` on the same item | May cancel the read with `TransactionCanceledException` and reason `TransactionConflict` | Never cancels for this reason on PostgreSQL: the read runs in one `REPEATABLE READ` snapshot taken at its first read, so it sees either all of the write or none of it and returns the pre-write state while the write is in flight. That is a legal serial order (the read ordered before the write) and the all-or-nothing guarantee holds, but a client that retries on `TransactionConflict` to observe the newest state will not get that signal here. SQLite behaves the same (one WAL snapshot); MongoDB with the default `snapshot` read concern likewise, and the row above covers the other read concerns. | | Transaction read concern (MongoDB backend only) | No user-configurable equivalent | `snapshot` is the default, fidelity-preserving mode. With `majority` or `local`, `TransactGetItems` is not guaranteed a single point-in-time snapshot and transaction condition reads have weaker isolation. With `local`, a condition can be evaluated against data that is later rolled back after failover. | | Contended `TransactWriteItems` | A transaction that conflicts with another in-flight transaction on the same item is canceled with a `TransactionConflict` reason ("Transaction is ongoing for the item") | **PostgreSQL** queues the second transaction on the first one's row locks and **SQLite** runs one writer at a time, so both normally commit and no `TransactionConflict` is returned. PostgreSQL locks each transaction's items in table and key order, so two `TransactWriteItems` cannot deadlock on each other. A PutItem, UpdateItem, DeleteItem or BatchWriteItem of an item that a `TransactWriteItems` has locked also waits for it, where the Amazon DynamoDB developer guide lists that request as a conflict. On PostgreSQL this includes an item that the transaction checks or deletes while it is missing: the transaction holds the key until it commits or rolls back, so a request that creates the item waits. Many transactions that check one hot missing item queue less fairly than on an existing item, because every waiter wakes at each commit and only one proceeds. Each such check or delete of a missing item also inserts and deletes its key, so it writes WAL and leaves a dead row and index entry for autovacuum, even though it changes no item. When PostgreSQL still aborts a transaction (a deadlock with another lock holder, or a serialization failure under a stricter `default_transaction_isolation`), the answer is a `TransactionCanceledException` with a `TransactionConflict` reason, not a 500. **MongoDB** retries write conflicts internally and cancels with `TransactionConflict` on every item only after sustained contention. A client that relies on `TransactionConflict` to back off does not see it under ordinary contention. | -| `BatchWriteItem` where no request could be applied now | A request that could not be applied is returned in `UnprocessedItems` with a 200 when another request of the batch applied, whichever came first (measured 2026-10-27 in us-east-1, client retries off, while a `TransactWriteItems` held the items: a batch of two requests, one on a held item, answered 200 with that request unprocessed and the other applied). When no request applied the batch answers an error, not a 200 with every request unprocessed (measured 2026-10-27 in us-east-1, client retries off, with every request on an item a `TransactWriteItems` held, for one request and for two: 500 `InternalServerError`) | The same 200 for a partial batch. When no request could be applied the answer is 503 `ServiceUnavailable`, which the SDKs retry with backoff, so a client's `UnprocessedItems` loop does not spin on a batch that makes no progress. Every request is tried before the batch answers, so a batch of 25 contended requests can spend 25 retry budgets in one call. A connection error ends the attempt: the request that met it and the requests after it are returned unprocessed, or the batch answers 503 if nothing applied. An internal storage error fails the batch with 500 as before. On a Cassandra table without TTL, a put or a delete of an item that a `TransactWriteItems` holds returns `TransactionCanceled`, which is not left for the client to replay, so the batch answers 400 `TransactionCanceledException` after the requests ahead of it were applied. A Cassandra write that spends its retries on a lost race, or on a held item of a TTL table, returns `TransactionConflict`, left in `UnprocessedItems`. A table of which no request applied gets no `ConsumedCapacity` entry. What the service reports for such a table in a partial batch is not measured. | +| `BatchWriteItem` where no request could be applied now | A request that could not be applied is returned in `UnprocessedItems` with a 200 when another request of the batch applied, whichever came first (measured 2026-10-27 in us-east-1, client retries off, while a `TransactWriteItems` held the items: a batch of two requests, one on a held item, answered 200 with that request unprocessed and the other applied). When no request applied the batch answers an error, not a 200 with every request unprocessed (measured 2026-10-27 in us-east-1, client retries off, with every request on an item a `TransactWriteItems` held, for one request and for two: 500 `InternalServerError`) | The same 200 for a partial batch. When no request could be applied the answer is 503 `ServiceUnavailable`, which the SDKs retry with backoff, so a client's `UnprocessedItems` loop does not spin on a batch that makes no progress. Every request is tried before the batch answers, so a batch of 25 contended requests can spend 25 retry budgets in one call. On MongoDB the requests of a batch share one contention deadline, measured from the start of the request (see "Sustained contention on one item" under Capacity and Throttling). A connection error ends the attempt: the request that met it and the requests after it are returned unprocessed, or the batch answers 503 if nothing applied. An internal storage error fails the batch with 500 as before. On a Cassandra table without TTL, a put or a delete of an item that a `TransactWriteItems` holds returns `TransactionCanceled`, which is not left for the client to replay, so the batch answers 400 `TransactionCanceledException` after the requests ahead of it were applied. A Cassandra write that spends its retries on a lost race, or on a held item of a TTL table, returns `TransactionConflict`, left in `UnprocessedItems`. A table of which no request applied gets no `ConsumedCapacity` entry. What the service reports for such a table in a partial batch is not measured. | | Contended single-item writes on a MongoDB table that has a stream or an index | `PutItem`, `DeleteItem` and `UpdateItem` on one item are applied one after another and each answers 200 (measured 2026-10-23, client retries off, on a table with a GSI and a stream; every call answered 200 at 32 and 64 writers putting one key of a table with a GSI); `TransactionConflictException` is answered to them only while a `TransactWriteItems` holds the item (for `PutItem`, 33 of 80 calls measured) | MongoDB aborts the later of two transactions that write one document, so each write runs as a transaction that is re-run with jittered backoff until the attempt budget or the deadline ends it (see "Sustained contention on one item" under Capacity and Throttling). A write on a table with a stream also conflicts on the sequence counter of the shard it is assigned to, so writers of different keys can conflict. A write that reaches either bound answers 503 `ServiceUnavailable`, which the SDKs retry, where the service answers 200. The server records that 503 as a system error, not under `TransactionConflict` (which counts `TransactionConflictException` answers), so contention shows in the system error metrics. The warning in the server log carries the last error that ended an attempt of the failed write. | ## Authentication and Authorization (AWS IAM/STS auth surface used by DynamoDB) diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index 0f32ca485..ae5e2a845 100644 --- a/docs/rfcs/0000-mongodb-backend.md +++ b/docs/rfcs/0000-mongodb-backend.md @@ -136,7 +136,7 @@ Transaction implementation: `data_engine.rs` — `transact_write_items_impl`, `t ### Write conflict handling -**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place, which is idempotent, up to `TRANSIENT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff up to `CONTENTION_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). `backoff_sleep` waits a uniform 0 to 50 µs × 2^min(attempt, 8), which is at most 12.8 ms from the ninth attempt on and sums to at most about 550 ms over 50 attempts (275 ms expected); tokio's timer ticks every millisecond and rounds a sleep up to the next tick, so for the first five attempts (longest jitter under 1 ms) the wait is set by the tick, not by the jitter. The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The helpers that run inside the transaction (index entry writes, the stream shard lookup, the stream sequence counter draw and the stream record insert) map a transient conflict through `write_conflict::transaction_error` to `StorageError::Transient`, which the transaction bodies convert into a re-run of the transaction; any other driver error stays `StorageError::Internal`. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The TTL worker's conditional delete runs the DeleteItem loop, so it takes the deadline too. `BatchWriteItem` runs its writes inside one `request_clock` scope, so the loops of one batch measure one deadline from the start of the request, and the handler returns a request that reaches it in `UnprocessedItems`. The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race gives up after `TRANSIENT_RETRY_ATTEMPTS` (50) lost races. Exhausted retries on PutItem, DeleteItem and UpdateItem all return the `StorageError::Transient` built by `write_conflict::retry_budget_exhausted` (its message names the operation, the number of attempts and the last error that ended an attempt), answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on `TransactWriteItems` they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. A write that reaches the deadline gives up the same way: PutItem, DeleteItem and UpdateItem return the `StorageError::Transient` built by `write_conflict::contention_deadline_exhausted`, which names the deadline instead of the attempts, and a `TransactWriteItems` answers as it does when its attempts run out. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. +**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place, which is idempotent, up to `TRANSIENT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff up to `CONTENTION_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). `backoff_sleep` waits a uniform 0 to 50 µs × 2^min(attempt, 8), which is at most 12.8 ms from the ninth attempt on and sums to at most about 550 ms over 50 attempts (275 ms expected); tokio's timer ticks every millisecond and rounds a sleep up to the next tick, so for the first five attempts (longest jitter under 1 ms) the wait is set by the tick, not by the jitter. The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The helpers that run inside the transaction (index entry writes, the stream shard lookup, the stream sequence counter draw and the stream record insert) map a transient conflict through `write_conflict::transaction_error` to `StorageError::Transient`, which the transaction bodies convert into a re-run of the transaction; any other driver error stays `StorageError::Internal`. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The TTL worker's conditional delete runs the DeleteItem loop, so it takes the deadline too. `BatchWriteItem` runs its writes inside one `request_clock` scope, so the loops of one batch measure one deadline from the start of the request, and the handler returns a request that reaches it in `UnprocessedItems` when another request of the batch applied, and answers 503 `ServiceUnavailable` when none did. The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race gives up after `TRANSIENT_RETRY_ATTEMPTS` (50) lost races. Exhausted retries on PutItem, DeleteItem and UpdateItem all return the `StorageError::Transient` built by `write_conflict::retry_budget_exhausted` (its message names the operation, the number of attempts and the last error that ended an attempt), answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on `TransactWriteItems` they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. A write that reaches the deadline gives up the same way: PutItem, DeleteItem and UpdateItem return the `StorageError::Transient` built by `write_conflict::contention_deadline_exhausted`, which names the deadline instead of the attempts, and a `TransactWriteItems` answers as it does when its attempts run out. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. **UpdateItem** additionally uses an optimistic-concurrency version guard on top of the transaction. A `_v` counter is stored on each document. The write path reads the current `_v` under the transaction snapshot, applies the update expression in memory, sets `_v = current_version + 1`, then executes `replace_one` filtered on both the primary key AND the expected `_v`. If `matched_count == 0`, a concurrent writer committed a higher version between the snapshot read and the replace; the attempt aborts and the outer retry loop re-reads. The version guard doubles up when a native-fast-path update (unconditional, no stream, no GSI) is possible — that path uses a single `find_one_and_update` with `$inc: {_v: 1}` outside a transaction, and always bumps the counter so a concurrent session-scoped update against a stale snapshot fails its versioned filter and retries. From 390e6be75b5676f1b6bf4058254a68c3a6ce87e6 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:36:30 +0000 Subject: [PATCH 32/44] refactor(mongodb): cut two comments that repeat text stated elsewhere The comment on TRANSIENT_RETRY_ATTEMPTS repeated the split between the contention loops and the other loops, which the comment on CONTENTION_RETRY_ATTEMPTS states. It now lists the loops that read 50 attempts and gives the create race measurement that sets the value. The comment on the request clock scope in batch_write_item.rs repeated the module comment of request_clock. It is now one line that points at the module. --- crates/engine/src/batch_write_item.rs | 5 +---- crates/storage-mongodb/src/data_engine.rs | 20 ++++++-------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/batch_write_item.rs b/crates/engine/src/batch_write_item.rs index 99883ea33..71435306e 100755 --- a/crates/engine/src/batch_write_item.rs +++ b/crates/engine/src/batch_write_item.rs @@ -108,10 +108,7 @@ pub async fn handle_batch_write_item( lookups.push(ctx.table_key_info_uncached(table_name).await); } - // The writes of the batch run in one request clock scope that starts with - // the batch, so a backend that bounds how long a contended write keeps - // retrying measures one bound for the whole batch instead of one per write - // request. + // One contention deadline for the batch's writes: see `request_clock`. extenddb_storage::request_clock::scope( started, write_batch( diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 7275bd909..b57beb242 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -3865,20 +3865,12 @@ pub(crate) struct GsiBackfillProgress { // ── Contention / retry helpers ────────────────────────────────────────── -/// Maximum number of attempts of the retry loops that the measurements behind -/// [`CONTENTION_RETRY_ATTEMPTS`] do not cover: the backup and restore -/// transactions, the in-place retry of `commitTransaction` after an -/// `UnknownTransactionCommitResult`, and the create-race give-up of an -/// unconditional PutItem and of UpdateItem's native upsert. -/// -/// The commit retry waits for the reply of one idempotent commit and never -/// re-runs the body. A lost create race is not a write conflict: the attempt -/// found no item and its insert hit an item another writer created and deleted -/// again. #431 measured this budget, with its backoff, as the one that lets -/// that race resolve: a put blocked by a document that is removed 100 ms after -/// the put starts commits well inside it, and the storage tests fail when the -/// cap is put back to 5. A longer budget on the commit retry or a create race -/// only delays the error that loop answers when it runs out. +/// Maximum number of attempts of the backup and restore transactions, the +/// in-place retry of `commitTransaction`, and the create-race give-up of an +/// unconditional PutItem and of UpdateItem's native upsert. #431 measured it, +/// with this backoff, for the create race. A put blocked by a document that +/// another writer removes 100 ms later commits well inside 50 attempts. With a +/// cap of 5 the storage tests fail. pub(crate) const TRANSIENT_RETRY_ATTEMPTS: u32 = 50; /// Maximum number of times a contention retry loop re-runs a transaction that From 67fc2e79192e88a087b32bf706c1d9ef63c54f7c Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:37:54 +0000 Subject: [PATCH 33/44] docs: state the BatchWriteItem answers in the batch row only In docs/differences-from-dynamodb.md the row "Sustained contention on one item (MongoDB backend)" repeated four facts of the row "`BatchWriteItem` where no request could be applied now": - The batch returns a request that it could not apply in UnprocessedItems while the other requests apply. - The consumed capacity counts the applied requests only. - A batch with no request applied answers 503. - The service answers 500 to such a batch. The sustained contention row now points at the batch row. The batch row states the shared contention deadline (from the previous commit) and now also states that the capacity counts the applied requests only. --- docs/differences-from-dynamodb.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index 620601420..f40cdee75 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -20,7 +20,7 @@ from a request sent to that service or backend. | `TransactGetItems` overlapping an in-flight `TransactWriteItems` on the same item | May cancel the read with `TransactionCanceledException` and reason `TransactionConflict` | Never cancels for this reason on PostgreSQL: the read runs in one `REPEATABLE READ` snapshot taken at its first read, so it sees either all of the write or none of it and returns the pre-write state while the write is in flight. That is a legal serial order (the read ordered before the write) and the all-or-nothing guarantee holds, but a client that retries on `TransactionConflict` to observe the newest state will not get that signal here. SQLite behaves the same (one WAL snapshot); MongoDB with the default `snapshot` read concern likewise, and the row above covers the other read concerns. | | Transaction read concern (MongoDB backend only) | No user-configurable equivalent | `snapshot` is the default, fidelity-preserving mode. With `majority` or `local`, `TransactGetItems` is not guaranteed a single point-in-time snapshot and transaction condition reads have weaker isolation. With `local`, a condition can be evaluated against data that is later rolled back after failover. | | Contended `TransactWriteItems` | A transaction that conflicts with another in-flight transaction on the same item is canceled with a `TransactionConflict` reason ("Transaction is ongoing for the item") | **PostgreSQL** queues the second transaction on the first one's row locks and **SQLite** runs one writer at a time, so both normally commit and no `TransactionConflict` is returned. PostgreSQL locks each transaction's items in table and key order, so two `TransactWriteItems` cannot deadlock on each other. A PutItem, UpdateItem, DeleteItem or BatchWriteItem of an item that a `TransactWriteItems` has locked also waits for it, where the Amazon DynamoDB developer guide lists that request as a conflict. On PostgreSQL this includes an item that the transaction checks or deletes while it is missing: the transaction holds the key until it commits or rolls back, so a request that creates the item waits. Many transactions that check one hot missing item queue less fairly than on an existing item, because every waiter wakes at each commit and only one proceeds. Each such check or delete of a missing item also inserts and deletes its key, so it writes WAL and leaves a dead row and index entry for autovacuum, even though it changes no item. When PostgreSQL still aborts a transaction (a deadlock with another lock holder, or a serialization failure under a stricter `default_transaction_isolation`), the answer is a `TransactionCanceledException` with a `TransactionConflict` reason, not a 500. **MongoDB** retries write conflicts internally and cancels with `TransactionConflict` on every item only after sustained contention. A client that relies on `TransactionConflict` to back off does not see it under ordinary contention. | -| `BatchWriteItem` where no request could be applied now | A request that could not be applied is returned in `UnprocessedItems` with a 200 when another request of the batch applied, whichever came first (measured 2026-10-27 in us-east-1, client retries off, while a `TransactWriteItems` held the items: a batch of two requests, one on a held item, answered 200 with that request unprocessed and the other applied). When no request applied the batch answers an error, not a 200 with every request unprocessed (measured 2026-10-27 in us-east-1, client retries off, with every request on an item a `TransactWriteItems` held, for one request and for two: 500 `InternalServerError`) | The same 200 for a partial batch. When no request could be applied the answer is 503 `ServiceUnavailable`, which the SDKs retry with backoff, so a client's `UnprocessedItems` loop does not spin on a batch that makes no progress. Every request is tried before the batch answers, so a batch of 25 contended requests can spend 25 retry budgets in one call. On MongoDB the requests of a batch share one contention deadline, measured from the start of the request (see "Sustained contention on one item" under Capacity and Throttling). A connection error ends the attempt: the request that met it and the requests after it are returned unprocessed, or the batch answers 503 if nothing applied. An internal storage error fails the batch with 500 as before. On a Cassandra table without TTL, a put or a delete of an item that a `TransactWriteItems` holds returns `TransactionCanceled`, which is not left for the client to replay, so the batch answers 400 `TransactionCanceledException` after the requests ahead of it were applied. A Cassandra write that spends its retries on a lost race, or on a held item of a TTL table, returns `TransactionConflict`, left in `UnprocessedItems`. A table of which no request applied gets no `ConsumedCapacity` entry. What the service reports for such a table in a partial batch is not measured. | +| `BatchWriteItem` where no request could be applied now | A request that could not be applied is returned in `UnprocessedItems` with a 200 when another request of the batch applied, whichever came first (measured 2026-10-27 in us-east-1, client retries off, while a `TransactWriteItems` held the items: a batch of two requests, one on a held item, answered 200 with that request unprocessed and the other applied). When no request applied the batch answers an error, not a 200 with every request unprocessed (measured 2026-10-27 in us-east-1, client retries off, with every request on an item a `TransactWriteItems` held, for one request and for two: 500 `InternalServerError`) | The same 200 for a partial batch. When no request could be applied the answer is 503 `ServiceUnavailable`, which the SDKs retry with backoff, so a client's `UnprocessedItems` loop does not spin on a batch that makes no progress. Every request is tried before the batch answers, so a batch of 25 contended requests can spend 25 retry budgets in one call. On MongoDB the requests of a batch share one contention deadline, measured from the start of the request (see "Sustained contention on one item" under Capacity and Throttling). A connection error ends the attempt: the request that met it and the requests after it are returned unprocessed, or the batch answers 503 if nothing applied. An internal storage error fails the batch with 500 as before. On a Cassandra table without TTL, a put or a delete of an item that a `TransactWriteItems` holds returns `TransactionCanceled`, which is not left for the client to replay, so the batch answers 400 `TransactionCanceledException` after the requests ahead of it were applied. A Cassandra write that spends its retries on a lost race, or on a held item of a TTL table, returns `TransactionConflict`, left in `UnprocessedItems`. The consumed capacity counts the applied requests only, and a table of which no request applied gets no `ConsumedCapacity` entry. What the service reports for such a table in a partial batch is not measured. | | Contended single-item writes on a MongoDB table that has a stream or an index | `PutItem`, `DeleteItem` and `UpdateItem` on one item are applied one after another and each answers 200 (measured 2026-10-23, client retries off, on a table with a GSI and a stream; every call answered 200 at 32 and 64 writers putting one key of a table with a GSI); `TransactionConflictException` is answered to them only while a `TransactWriteItems` holds the item (for `PutItem`, 33 of 80 calls measured) | MongoDB aborts the later of two transactions that write one document, so each write runs as a transaction that is re-run with jittered backoff until the attempt budget or the deadline ends it (see "Sustained contention on one item" under Capacity and Throttling). A write on a table with a stream also conflicts on the sequence counter of the shard it is assigned to, so writers of different keys can conflict. A write that reaches either bound answers 503 `ServiceUnavailable`, which the SDKs retry, where the service answers 200. The server records that 503 as a system error, not under `TransactionConflict` (which counts `TransactionConflictException` answers), so contention shows in the system error metrics. The warning in the server log carries the last error that ended an attempt of the failed write. | ## Authentication and Authorization (AWS IAM/STS auth surface used by DynamoDB) @@ -203,7 +203,7 @@ alone, with the same wording. For PartiQL this holds in `ExecuteStatement`, | On-demand capacity | Automatic scaling | Fixed initial burst capacity (4000 WCU / 12000 RCU), no auto-scaling | | Warm throughput | Pre-provisions partitions to the requested read and write units; raising it is a long-running operation (`Status: UPDATING` for minutes, the previous values reported meanwhile); a request above the account's table maximum is `LimitExceededException` | Stored and reported with the same shape, defaults and validation so clients that read it (the Terraform AWS provider since 6.13.0) work: the previous values are reported with `Status: UPDATING` for `control_plane_delay_seconds` after a change, then the new values `ACTIVE`; a decrease is measured against the reported values, as on the service. Nothing acts on the values: no pre-warming and no scaling at the database level. The account maximum is not known to ExtendDB and is not enforced. A provisioned table whose warm throughput was never set, or was set on one side only, reports its new units at once after a capacity change, where the service reports the old units until the table is `ACTIVE`. Switching a table to `PAY_PER_REQUEST` stores 12000 read and 4000 write units where its warm throughput is lower, and the stored values stay after a switch back to `PROVISIONED`, as on the service; the response to the switch reads the new values with `Status: UPDATING` for `control_plane_delay_seconds`, where the service answers with the previous values and `Status: ACTIVE` | | Throttling | Always on; throttles requests that exceed provisioned/burst capacity. No setting to disable | Configurable via `throttling_enabled` runtime setting (default: `true`) | -| Sustained contention on one item (MongoDB backend) | For `PutItem`, `DeleteItem` and `UpdateItem`, see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure. A `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | One request's retry loop (see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure) stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. In a `BatchWriteItem` the write requests share one deadline, measured from the start of the request: a request that reaches it is returned in `UnprocessedItems` and the other requests are still applied, which is how the service answers a batch with a request on an item a transaction holds. The consumed capacity counts the applied requests only. When none of its requests was applied, the batch answers 503 `ServiceUnavailable`. The service answered 500 `InternalServerError` to 54 of 80 batches whose only request was on an item a transaction held. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | +| Sustained contention on one item (MongoDB backend) | For `PutItem`, `DeleteItem` and `UpdateItem`, see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure. A `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | One request's retry loop (see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure) stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. For a `BatchWriteItem`, see "`BatchWriteItem` where no request could be applied now" under Storage and Infrastructure. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | ## PartiQL From 1eadcb9a62a2ed6451efcd5f6765f84c4ac71f4d Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:22:44 +0000 Subject: [PATCH 34/44] fix(partiql): give the statements of one BatchExecuteStatement one contention deadline BatchExecuteStatement ran its statements outside a request clock scope. On MongoDB each contended statement measured CONTENTION_DEADLINE (10 s) from its own start. A batch of N held statements answered after about N times 10 s. The handler now runs inside request_clock::scope, as BatchWriteItem does. The statements share one deadline, measured from the start of the request. Measured on MongoDB 7.0 with client retries off. A mongosh transaction held every item of a GSI table, and a batch of N UPDATE statements named those items. Before this change the batch answered in 50.1 s (N = 5) and 70.1 s (N = 7), in two runs each. After it the batch answered in 10.08 to 10.15 s for both. On main the update loop stopped after 50 attempts, and the same batch answered in 3.5 to 5.1 s. tests/test_mongodb_batch_write_contention.py holds three items past the deadline. The test requires the batch to answer within the deadline plus 3 s, with the three items unchanged. --- crates/engine/src/partiql/mod.rs | 7 +++- crates/storage-mongodb/src/data_engine.rs | 3 +- crates/storage/src/request_clock.rs | 3 +- tests/test_mongodb_batch_write_contention.py | 41 +++++++++++++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/partiql/mod.rs b/crates/engine/src/partiql/mod.rs index fb5e21044..9671da0ee 100644 --- a/crates/engine/src/partiql/mod.rs +++ b/crates/engine/src/partiql/mod.rs @@ -122,7 +122,12 @@ impl PartiqlExecutor for OperationContext { } async fn batch_execute_statement(&self, body: Value) -> Result { - batch::run(&translation_context(self), &body).await + // One contention deadline for the batch's statements: see `request_clock`. + extenddb_storage::request_clock::scope( + std::time::Instant::now(), + batch::run(&translation_context(self), &body), + ) + .await } async fn execute_transaction(&self, body: Value) -> Result { diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index b57beb242..05d0ddc71 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -3936,7 +3936,8 @@ const CONTENTION_RETRY_ATTEMPTS: u32 = 1000; /// the deadline from the start of its request (see `contention_clock_start`): /// the write requests of one `BatchWriteItem` share one deadline, and the /// handler returns a request that reaches it in UnprocessedItems when another -/// request of the batch applied, and answers 503 when none did. The TTL +/// request of the batch applied, and answers 503 when none did. The +/// statements of one `BatchExecuteStatement` share one deadline too. The TTL /// worker's conditional delete runs the delete loop too, outside any request, /// so an expired item that stays contended holds the sweep until the deadline /// or the attempt budget ends that loop, where 50 attempts ended it before diff --git a/crates/storage/src/request_clock.rs b/crates/storage/src/request_clock.rs index 675ef973c..52d2ba0e1 100644 --- a/crates/storage/src/request_clock.rs +++ b/crates/storage/src/request_clock.rs @@ -8,7 +8,8 @@ //! one request runs them inside [`scope`], and the backend measures the //! deadline from the instant the scope carries, so the calls share one //! deadline instead of each getting a deadline of its own. `BatchWriteItem` -//! does this for its write requests. Outside a scope [`started`] is `None` +//! does this for its write requests, and `BatchExecuteStatement` for its +//! statements. Outside a scope [`started`] is `None` //! and the backend measures from the start of each call, which is what a //! single-item request and a background worker get. diff --git a/tests/test_mongodb_batch_write_contention.py b/tests/test_mongodb_batch_write_contention.py index 73f50633f..3d4af104f 100644 --- a/tests/test_mongodb_batch_write_contention.py +++ b/tests/test_mongodb_batch_write_contention.py @@ -1,7 +1,7 @@ # Copyright 2026 ExtendDB contributors # SPDX-License-Identifier: Apache-2.0 -"""BatchWriteItem on the MongoDB backend when one of its items stays contended. +"""BatchWriteItem and BatchExecuteStatement on the MongoDB backend when their items stay contended. A mongosh session holds an item inside a MongoDB transaction it does not commit, so every attempt of a write to that item conflicts until the session @@ -205,3 +205,42 @@ def test_the_requests_of_a_batch_share_one_contention_deadline( assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" assert _w(dynamodb_client, gsi_table, b1) == "v1" assert _w(dynamodb_client, gsi_table, b2) == "v0" + + +def test_the_statements_of_a_batch_execute_statement_share_one_contention_deadline( + dynamodb_client, endpoint_url, mongodb_container, gsi_table +): + """A BatchExecuteStatement whose items are all held answers within one deadline. + + Three UPDATE statements name three items, each held past the deadline. The + first statement gives up when the deadline passes, and the other two give + up after one attempt each, because the deadline is measured from the start + of the batch. A deadline per statement would keep the batch open until the + holds end and let the later statements commit. + """ + pks = [f"s{i}-{uuid.uuid4().hex[:8]}" for i in range(3)] + for pk in pks: + dynamodb_client.put_item(TableName=gsi_table, Item=_item(pk, "v0")) + client = _client(endpoint_url, retries=False) + + # The three holds start one after another, so each lasts 5 s longer than + # HOLD to be sure the first is still held when the deadline passes. + holds = [Hold(mongodb_container, gsi_table, pk, HOLD + 5) for pk in pks] + started = time.monotonic() + resp = client.batch_execute_statement( + Statements=[ + { + "Statement": f'UPDATE "{gsi_table}" SET w = ? WHERE pk = ?', + "Parameters": [{"S": "v1"}, {"S": pk}], + } + for pk in pks + ] + ) + elapsed = time.monotonic() - started + for hold in holds: + hold.wait() + + print(f"the batch answered in {elapsed:.2f} s: {resp['Responses']}") + assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" + assert all("Error" in r for r in resp["Responses"]), resp["Responses"] + assert [_w(dynamodb_client, gsi_table, pk) for pk in pks] == ["v0"] * 3 From d714ee0c0d436317dc458984ad5f52bc2c530f98 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 24 Nov 2026 07:10:15 +0000 Subject: [PATCH 35/44] test(put-item): require nine in ten contended puts to commit - The hot-key wire test passed on main's code. A server that gives up after 50 attempts shed about a quarter of the 2,016 puts in two runs. That stays above the floor of half. The floor is now nine in ten, which that server fails. The runs of this branch recorded so far shed at most 2 of the 2,016 puts. - The failure record carries the error's message beside its status and code. The expected shed answer is the engine's full text, "503 ServiceUnavailable Service is temporarily unavailable". - Each put goes through conftest.call_with_handshake_retry, which main added for the race tests. The helper retries a TLS handshake that fails before the request goes out. The writer clients retry nothing by themselves, so without the helper that error fails the test. --- tests/test_put_item_gsi_contention.py | 35 +++++++++++++++++---------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/tests/test_put_item_gsi_contention.py b/tests/test_put_item_gsi_contention.py index 43847503a..6729d52ca 100644 --- a/tests/test_put_item_gsi_contention.py +++ b/tests/test_put_item_gsi_contention.py @@ -17,7 +17,7 @@ ServiceUnavailable, never 400 TransactionConflictException and never 500; the stored item is one writer's item, whole; and the index holds exactly one entry for it. These tests fail on a server that reports a lost race to the client as -a 400 or a 500, or that sheds more than half of the puts. +a 400 or a 500, or that sheds more than a tenth of the puts. """ from __future__ import annotations @@ -32,13 +32,14 @@ from botocore.config import Config from botocore.exceptions import ClientError -from conftest import configured_backend, scoped_table +from conftest import call_with_handshake_retry, configured_backend, scoped_table WRITERS = 32 PUTS_PER_WRITER = 63 -# The only failure a contended put may answer. The server sheds a put that -# has been losing to other writers for CONTENTION_DEADLINE (10 s). -SHED = "503 ServiceUnavailable" +# The only failure a contended put may answer, as status, code and message. +# The server sheds a put that has been losing to other writers for +# CONTENTION_DEADLINE (10 s). +SHED = "503 ServiceUnavailable Service is temporarily unavailable" @pytest.fixture(scope="module") @@ -83,7 +84,9 @@ def gsi_table(dynamodb_client): def _hammer(clients: list, table: str, pk: str) -> list[str]: """WRITERS threads each put PUTS_PER_WRITER items on `pk`, each with its own client. - Returns the failures. + Returns the failures, each as status, code and message. A TLS handshake + that fails before a put is sent is retried by `call_with_handshake_retry`, + since the clients retry nothing by themselves. """ start = threading.Barrier(WRITERS, timeout=30) failures: list[str] = [] @@ -97,9 +100,13 @@ def run(i: int): for n in range(PUTS_PER_WRITER): item = {"pk": {"S": pk}, "gk": {"S": f"g-{pk}-{i}-{n}"}, "w": {"S": f"w{i}-{n}"}} try: - client.put_item(TableName=table, Item=item) + call_with_handshake_retry(client.put_item, TableName=table, Item=item) except ClientError as e: - code = f"{e.response['ResponseMetadata']['HTTPStatusCode']} {e.response['Error']['Code']}" + r = e.response + code = ( + f"{r['ResponseMetadata']['HTTPStatusCode']} " + f"{r['Error']['Code']} {r['Error']['Message']}" + ) with lock: failures.append(code) except BaseException as e: # noqa: BLE001 - surfaced below @@ -138,16 +145,18 @@ def test_unconditional_puts_on_one_key_of_a_gsi_table_commit_or_are_shed( total = WRITERS * PUTS_PER_WRITER committed = total - len(failures) # How many of the contended puts were shed depends on the load the server - # is under, so the count is reported and only bounded loosely: a shed put - # is the only failure a caller can get, and at least half of the puts - # commit. A server that gives up at the first conflict sheds nearly every - # put of this shape and fails the second check. + # is under, so the count is reported and bounded: a shed put is the only + # failure a caller can get, and at least nine in ten of the puts commit. + # A server that gives up after 50 attempts, the budget the other retry + # loops keep, shed about a quarter of the puts of this shape in two runs + # at a load average of 14 to 20, and fails the second check; a server + # that gives up at the first conflict sheds nearly every put. print(f"{len(failures)} of {total} puts were shed, {committed} committed") assert set(failures) <= {SHED}, ( f"{len(failures)} of {total} puts failed with something other than a shed put: " f"{sorted(set(failures))}" ) - assert committed >= total // 2, f"only {committed} of {total} puts committed" + assert committed >= total * 9 // 10, f"only {committed} of {total} puts committed" final = dynamodb_client.get_item( TableName=gsi_table, Key={"pk": {"S": pk}}, ConsistentRead=True )["Item"] From 89c62f0bd813037dc73987b9e91f2567cf339912 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 24 Nov 2026 07:10:21 +0000 Subject: [PATCH 36/44] test(mongodb): make the attempt budget the one bound of the hot-key test - live_concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit ran on the production deadline of 10 s and required all 256 puts to commit. On a loaded host a put that queued behind the other writers could reach the deadline first and fail the test. The module doc rules that out: a test sets the deadline so that it decides which bound a write reaches. The test now sets a 600 s deadline, as the attempt budget test does. The budget is then the one bound a put here can reach. The doc comment says why. - The futures import moves to the top of the file. The doc comments write MongoDB, DynamoDB, PutItem and UpdateItem in backticks, as clippy's pedantic group asks. - The attempt budget test said the 1,000 attempts took 9.5 to 12.7 s over nine runs. The record of that measurement has six runs. --- .../src/data_engine_live_tests_contention.rs | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs index f67f81154..10b039d5a 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs @@ -1,7 +1,7 @@ // Copyright 2026 ExtendDB contributors // SPDX-License-Identifier: Apache-2.0 -//! Live MongoDB tests for writes that lose to concurrent writers on one item: +//! Live `MongoDB` tests for writes that lose to concurrent writers on one item: //! puts on one key of a table with a GSI, and the two bounds that end a //! contended write, the engine's contention deadline and the attempt budget. //! @@ -20,8 +20,9 @@ use extenddb_core::types::{ ReturnValuesOnConditionCheckFailure, TableKeyInfo, }; use extenddb_storage::error::StorageError; +use futures::TryStreamExt as _; -use super::super::{CONTENTION_DEADLINE, OwnedTransactWriteOp}; +use super::super::OwnedTransactWriteOp; use super::{finish, scratch_engine, table}; use crate::MongoEngine; use crate::data::{data_collection_name, document_to_item, item_to_document}; @@ -65,7 +66,7 @@ fn stored_document(item: &Item, key_info: &TableKeyInfo) -> Document { .expect("test item converts to a document") } -/// `SET w = :w` with its placeholder, for an UpdateItem under contention. +/// `SET w = :w` with its placeholder, for an `UpdateItem` under contention. fn set_w(value: &str) -> (Vec, ExpressionMaps) { let actions = vec![UpdateAction::Set { path: vec![PathElement::Attribute("w".to_owned())], @@ -124,6 +125,12 @@ async fn stored_item(engine: &MongoEngine, key_info: &TableKeyInfo, pk: &str) -> /// index holds the final item's entry only. On the service every one of these /// puts answers 200; the backend reaches that through its retry budget. /// +/// The deadline is set far above the time the budget takes, so the attempt +/// budget is the one bound a put here can reach. With the production deadline +/// of 10 s, a put that queued behind the other writers on a loaded host could +/// reach the deadline first and fail the test, which would measure the host's +/// load instead of the budget. +/// /// The table has a GSI row in the catalog, so `sync_indexes_in_session` /// writes an index entry per put and keeps the GSI cache at `true`. Without /// the row the first committed put flips the cache to `false`, the other @@ -136,7 +143,7 @@ async fn stored_item(engine: &MongoEngine, key_info: &TableKeyInfo, pk: &str) -> async fn live_concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit() { const WRITERS: usize = 32; const PUTS: usize = 8; - let engine = Arc::new(engine_with_deadline(CONTENTION_DEADLINE).await); + let engine = Arc::new(engine_with_deadline(Duration::from_secs(600)).await); let key_info = Arc::new(gsi_path_table(&engine, "hot-key")); let gsi_key_schema = vec![KeySchemaElement { attribute_name: "gk".to_owned(), @@ -201,7 +208,6 @@ async fn live_concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit let idx_coll = engine .data_db .collection::(&data_collection_name("hot-key-gk-index")); - use futures::TryStreamExt as _; let entries: Vec = idx_coll .find(doc! {}) .await @@ -376,7 +382,7 @@ async fn live_a_write_in_a_request_scope_measures_the_deadline_from_the_scope_st /// A `TransactWriteItems` that reaches the deadline answers a canceled /// transaction with a `TransactionConflict` reason per item, which is what -/// live DynamoDB answers a transaction that loses to contention (107 of 160 +/// live `DynamoDB` answers a transaction that loses to contention (107 of 160 /// calls at 16 writers on one key). It does not answer the HTTP 503 the /// single-item writes answer. #[tokio::test] @@ -450,12 +456,12 @@ async fn live_a_transaction_that_reaches_the_deadline_answers_a_canceled_transac finish(engine).await; } -/// An unconditional PutItem that spends its whole attempt budget on +/// An unconditional `PutItem` that spends its whole attempt budget on /// contention gives up with the transient error naming the attempts, which /// the engine answers as HTTP 503. /// /// The deadline is set far above the time the budget takes (9.5 to 12.7 s -/// against a document a transaction holds, over nine runs on a shared host), +/// against a document a transaction holds, over six runs on a shared host), /// so the attempt budget is the bound this test reaches. With the production /// deadline of 10 s either bound can fire first, which is why the deadline is /// the test's input rather than its race. From d138f8ef4f5ad615673fccb3216748c6e47cfc3e Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 24 Nov 2026 07:10:29 +0000 Subject: [PATCH 37/44] docs(mongodb): keep each contention measurement in one place - The CONTENTION_DEADLINE comment repeated the measurements of the RFC's write conflict section and the differences rows. Those are the time 1,000 attempts take, the 256-writer run, the longest call and the CPU cost. It now states the mechanism, the loops that take the deadline and what a caller gets, and points at the RFC section for the numbers. The CPU cost of a hot key, which only the comment stated, moves to the RFC section. - The RFC section and the attempt budget test said the 1,000 attempts took 9.5 to 12.7 s over nine runs. The record has six, which the CONTENTION_RETRY_ATTEMPTS comment already said. - The RFC section and the "Sustained contention on one item" row named only BatchWriteItem as the request whose storage calls share one deadline. Both now say that the statements of a BatchExecuteStatement share one too, and the contention_clock_start comment names both. - The same row said the other three backends keep writes on one key waiting. Cassandra answers some of them with TransactionConflictException (#802), which is why the wire test skips it. The row now says so, and that the deadline applies to MongoDB only. - The row said the 256-writer shape without the deadline left calls waiting 99.5 s. That figure came from a 5-round run on an earlier build. The row's 5,120-call numbers come from the 20-round run, in which 89 calls hit the client's 120 s read timeout. The row now gives that figure. - The TRANSIENT_RETRY_ATTEMPTS comment named the PR that measured the create race. It now states the measurement. - The request_clock module doc gets a reflow. The doc comments write MongoDB, PutItem, DeleteItem, UpdateItem and UnprocessedItems in backticks, as clippy's pedantic group asks. --- crates/storage-mongodb/src/data_engine.rs | 91 ++++++++++------------- crates/storage/src/request_clock.rs | 6 +- docs/differences-from-dynamodb.md | 2 +- docs/rfcs/0000-mongodb-backend.md | 2 +- 4 files changed, 44 insertions(+), 57 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 05d0ddc71..3bd3c1fd1 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -3867,29 +3867,29 @@ pub(crate) struct GsiBackfillProgress { /// Maximum number of attempts of the backup and restore transactions, the /// in-place retry of `commitTransaction`, and the create-race give-up of an -/// unconditional PutItem and of UpdateItem's native upsert. #431 measured it, -/// with this backoff, for the create race. A put blocked by a document that +/// unconditional `PutItem` and of `UpdateItem`'s native upsert. The create +/// race was measured with this backoff: a put blocked by a document that /// another writer removes 100 ms later commits well inside 50 attempts. With a /// cap of 5 the storage tests fail. pub(crate) const TRANSIENT_RETRY_ATTEMPTS: u32 = 50; /// Maximum number of times a contention retry loop re-runs a transaction that -/// MongoDB aborted as a transient conflict. +/// `MongoDB` aborted as a transient conflict. /// -/// MongoDB aborts the loser of a same-document conflict at once instead of +/// `MongoDB` aborts the loser of a same-document conflict at once instead of /// queueing it, so a hot key is settled by retries with backoff: of N /// writers on one key, one commits per round and the rest start over. The /// backoff is random, so a writer can lose many rounds in a row, and the /// chance of losing every attempt falls off exponentially with the budget. /// Measured with 32 clients putting one key of a table with a GSI (2,016 -/// puts per run, MongoDB 7.0 on one 16-core host): 50 attempts left 15 to +/// puts per run, `MongoDB` 7.0 on one 16-core host): 50 attempts left 15 to /// 25 percent of the puts without a commit; 400 left none in four runs at a /// load average of 15 and one put in 2,016 at a load average of 30, and 20 /// of 1,984 with 64 clients. `backoff_sleep` draws each sleep uniformly from /// 0 to 12.8 ms once the base is capped, so 1,000 attempts sleep about 6.4 s /// in total on average and 12.8 s at worst, plus the attempts' own round /// trips, before the write gives up. Against a transaction another session -/// holds open, where an attempt waits out MongoDB's lock timeout, a put that +/// holds open, where an attempt waits out `MongoDB`'s lock timeout, a put that /// loses all 1,000 attempts returned in 9.5 to 12.7 s over six runs on a /// shared host, under the SDKs' request timeouts. /// @@ -3903,57 +3903,44 @@ const CONTENTION_RETRY_ATTEMPTS: u32 = 1000; /// Wall-clock deadline for the contention retry loop of one request. /// /// The attempt budget bounds the number of attempts, not the time a caller -/// waits, because an attempt's own round trip has no bound. The same 1,000 -/// attempts against a transaction another session holds open returned in 9.5 -/// to 12.7 s over six runs on a shared host, and under many writers on one -/// key the attempts queue behind each other: at 256 writers putting one key -/// of a GSI table, 89 of 5,120 calls were still waiting when the client's -/// 120 s read timeout fired, and the 5,120 calls measured p50 29 s and p99 -/// 120 s. The budget alone therefore let a request outlive every SDK's -/// default read timeout. +/// waits: under many writers on one key the attempts queue behind each other, +/// and the budget alone let a request outlive every SDK's default read +/// timeout. The deadline is read before an attempt starts, so a request +/// answers within the deadline plus the attempt already running. That attempt +/// has no bound of its own: it waits for a connection from the pool +/// (`pool_size`, 50 by default) and for the commit's own retries, which can +/// take a couple of seconds. /// -/// 10 s sits above the 9.5 to 12.7 s a put spends losing all 1,000 attempts -/// and below botocore's 60 s default read timeout, so a caller gets an answer -/// to retry rather than a socket timeout. Which bound a write reaches first -/// depends on the host's load: against a held document, where the 1,000 -/// attempts and the deadline both land near 10 s, a DeleteItem reached its -/// attempt budget first in 2 of 4 rounds. The caller gets the same error class -/// on either bound: HTTP 503 for the single-item writes (see -/// `write_conflict::retry_budget_exhausted` and +/// 10 s sits above the time a put spends losing all 1,000 attempts against a +/// document another transaction holds and below botocore's 60 s default read +/// timeout, so a caller gets an answer to retry rather than a socket timeout. +/// Which bound a write reaches first depends on the host's load. The caller +/// gets the same error class on either bound: HTTP 503 for the single-item +/// writes (see `write_conflict::retry_budget_exhausted` and /// `write_conflict::contention_deadline_exhausted`) and /// `TransactionCanceledException` for `TransactWriteItems`. /// -/// The deadline is read before an attempt starts, so a request answers within -/// the deadline plus the attempt already running. That attempt has no bound -/// of its own: under many writers on one key it waits for a connection from -/// the pool (`pool_size`, 50 by default) and for the commit's own retries, -/// which can take a couple of seconds. At 256 writers on one key of a GSI table the -/// longest call measured 11.7 s. -/// -/// It governs the put, delete, update and transact loops. The single-item -/// writes answer it with HTTP 503; a transaction answers it as it answers a -/// spent attempt budget, with `TransactionCanceledException`. A loop measures -/// the deadline from the start of its request (see `contention_clock_start`): -/// the write requests of one `BatchWriteItem` share one deadline, and the -/// handler returns a request that reaches it in UnprocessedItems when another +/// It governs the put, delete, update and transact loops. A loop measures the +/// deadline from the start of its request (see `contention_clock_start`): the +/// write requests of one `BatchWriteItem` share one deadline, and the handler +/// returns a request that reaches it in `UnprocessedItems` when another /// request of the batch applied, and answers 503 when none did. The /// statements of one `BatchExecuteStatement` share one deadline too. The TTL -/// worker's conditional delete runs the delete loop too, outside any request, -/// so an expired item that stays contended holds the sweep until the deadline -/// or the attempt budget ends that loop, where 50 attempts ended it before -/// the budget was raised. The worker then logs the failure and moves to the -/// next item. +/// worker's conditional delete runs the delete loop outside any request, so an +/// expired item that stays contended holds the sweep until the deadline or the +/// attempt budget ends that loop, where 50 attempts ended it before the budget +/// was raised; the worker then logs the failure and moves to the next item. /// The GSI backfill loop shares the attempt budget and takes no deadline: it /// runs in the background worker in `ttl_worker.rs`, where no caller is /// waiting for a reply and giving up leaves the index CREATING. /// -/// The cost of the longer retry is CPU on the MongoDB server: the losers of a -/// hot key retry with a backoff capped at 12.8 ms for up to the deadline, so -/// the server stays busy committing and aborting transactions on that key for -/// as long as the contention lasts. With 64 and 256 writers putting one key of -/// a GSI table for 20 s, on a 16-core host at a load average of about 20, -/// mongod used about 1.3 cores and ran 1,670 and 1,810 transactions a second, -/// of which 98.7 and 99.6 percent aborted. +/// The longer retry costs CPU on the `MongoDB` server: the losers of a hot key +/// retry with a backoff capped at 12.8 ms for up to the deadline, so the +/// server stays busy committing and aborting transactions on that key for as +/// long as the contention lasts. The measurements behind the value (how long +/// the 1,000 attempts take, the 256-writer runs with and without the deadline, +/// and the CPU cost) are in the write conflict handling section of +/// `docs/rfcs/0000-mongodb-backend.md`. /// /// The engine carries the deadline in /// [`MongoEngine::contention_deadline`](crate::MongoEngine) so that a test can @@ -4137,11 +4124,11 @@ fn contention_deadline_passed( /// The instant a contention retry loop measures the engine's deadline from. /// /// A caller that makes several storage calls for one API request runs them -/// inside [`extenddb_storage::request_clock::scope`], as `BatchWriteItem` -/// does, and then every loop of that request measures from the start the -/// scope carries, so the request's writes share one deadline. Outside a -/// scope, which is where a single-item request and the TTL worker call from, -/// a loop measures from the start of its own call. +/// inside [`extenddb_storage::request_clock::scope`], as `BatchWriteItem` and +/// `BatchExecuteStatement` do, and then every loop of that request measures +/// from the start the scope carries, so the request's writes share one +/// deadline. Outside a scope, which is where a single-item request and the TTL +/// worker call from, a loop measures from the start of its own call. fn contention_clock_start() -> std::time::Instant { extenddb_storage::request_clock::started().unwrap_or_else(std::time::Instant::now) } diff --git a/crates/storage/src/request_clock.rs b/crates/storage/src/request_clock.rs index 52d2ba0e1..70c87e1e6 100644 --- a/crates/storage/src/request_clock.rs +++ b/crates/storage/src/request_clock.rs @@ -9,9 +9,9 @@ //! deadline from the instant the scope carries, so the calls share one //! deadline instead of each getting a deadline of its own. `BatchWriteItem` //! does this for its write requests, and `BatchExecuteStatement` for its -//! statements. Outside a scope [`started`] is `None` -//! and the backend measures from the start of each call, which is what a -//! single-item request and a background worker get. +//! statements. Outside a scope [`started`] is `None` and the backend measures +//! from the start of each call, which is what a single-item request and a +//! background worker get. use std::future::Future; use std::time::Instant; diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index d98942420..c69e74a2b 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -203,7 +203,7 @@ alone, with the same wording. For PartiQL this holds in `ExecuteStatement`, | On-demand capacity | Automatic scaling | Fixed initial burst capacity (4000 WCU / 12000 RCU), no auto-scaling | | Warm throughput | Pre-provisions partitions to the requested read and write units; raising it is a long-running operation (`Status: UPDATING` for minutes, the previous values reported meanwhile); a request above the account's table maximum is `LimitExceededException` | Stored and reported with the same shape, defaults and validation so clients that read it (the Terraform AWS provider since 6.13.0) work: the previous values are reported with `Status: UPDATING` for `control_plane_delay_seconds` after a change, then the new values `ACTIVE`; a decrease is measured against the reported values, as on the service. Nothing acts on the values: no pre-warming and no scaling at the database level. The account maximum is not known to ExtendDB and is not enforced. A provisioned table whose warm throughput was never set, or was set on one side only, reports its new units at once after a capacity change, where the service reports the old units until the table is `ACTIVE`. Switching a table to `PAY_PER_REQUEST` stores 12000 read and 4000 write units where its warm throughput is lower, and the stored values stay after a switch back to `PROVISIONED`, as on the service; the response to the switch reads the new values with `Status: UPDATING` for `control_plane_delay_seconds`, where the service answers with the previous values and `Status: ACTIVE` | | Throttling | Always on; throttles requests that exceed provisioned/burst capacity. No setting to disable | Configurable via `throttling_enabled` runtime setting (default: `true`) | -| Sustained contention on one item (MongoDB backend) | For `PutItem`, `DeleteItem` and `UpdateItem`, see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure. A `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | One request's retry loop (see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure) stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. Without the deadline the same 256-writer shape left calls waiting 99.5 s, so a client sees a shed request where it used to see a request outlive its own read timeout. For a `BatchWriteItem`, see "`BatchWriteItem` where no request could be applied now" under Storage and Infrastructure. The other three backends keep writes on one key waiting rather than shedding them, and the deadline does not apply to them | +| Sustained contention on one item (MongoDB backend) | For `PutItem`, `DeleteItem` and `UpdateItem`, see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure. A `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | One request's retry loop (see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure) stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. Without the deadline the same 256-writer shape left 89 of 5,120 calls waiting when the client's 120 s read timeout fired, so a client sees a shed request where it used to see a request outlive its own read timeout. For a `BatchWriteItem`, see "`BatchWriteItem` where no request could be applied now" under Storage and Infrastructure. The statements of a `BatchExecuteStatement` share one deadline too. SQLite and PostgreSQL keep writes on one key waiting rather than shedding them, and Cassandra answers some of them with `TransactionConflictException` (#802). The deadline applies to MongoDB only | ## PartiQL diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index ae5e2a845..254594c29 100644 --- a/docs/rfcs/0000-mongodb-backend.md +++ b/docs/rfcs/0000-mongodb-backend.md @@ -136,7 +136,7 @@ Transaction implementation: `data_engine.rs` — `transact_write_items_impl`, `t ### Write conflict handling -**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place, which is idempotent, up to `TRANSIENT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff up to `CONTENTION_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). `backoff_sleep` waits a uniform 0 to 50 µs × 2^min(attempt, 8), which is at most 12.8 ms from the ninth attempt on and sums to at most about 550 ms over 50 attempts (275 ms expected); tokio's timer ticks every millisecond and rounds a sleep up to the next tick, so for the first five attempts (longest jitter under 1 ms) the wait is set by the tick, not by the jitter. The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over nine runs on a shared host. The helpers that run inside the transaction (index entry writes, the stream shard lookup, the stream sequence counter draw and the stream record insert) map a transient conflict through `write_conflict::transaction_error` to `StorageError::Transient`, which the transaction bodies convert into a re-run of the transaction; any other driver error stays `StorageError::Internal`. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The TTL worker's conditional delete runs the DeleteItem loop, so it takes the deadline too. `BatchWriteItem` runs its writes inside one `request_clock` scope, so the loops of one batch measure one deadline from the start of the request, and the handler returns a request that reaches it in `UnprocessedItems` when another request of the batch applied, and answers 503 `ServiceUnavailable` when none did. The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. An unconditional PutItem that keeps losing the create race gives up after `TRANSIENT_RETRY_ATTEMPTS` (50) lost races. Exhausted retries on PutItem, DeleteItem and UpdateItem all return the `StorageError::Transient` built by `write_conflict::retry_budget_exhausted` (its message names the operation, the number of attempts and the last error that ended an attempt), answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on `TransactWriteItems` they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. A write that reaches the deadline gives up the same way: PutItem, DeleteItem and UpdateItem return the `StorageError::Transient` built by `write_conflict::contention_deadline_exhausted`, which names the deadline instead of the attempts, and a `TransactWriteItems` answers as it does when its attempts run out. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. +**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place, which is idempotent, up to `TRANSIENT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff up to `CONTENTION_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). `backoff_sleep` waits a uniform 0 to 50 µs × 2^min(attempt, 8), which is at most 12.8 ms from the ninth attempt on and sums to at most about 550 ms over 50 attempts (275 ms expected); tokio's timer ticks every millisecond and rounds a sleep up to the next tick, so for the first five attempts (longest jitter under 1 ms) the wait is set by the tick, not by the jitter. The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over six runs on a shared host. The helpers that run inside the transaction (index entry writes, the stream shard lookup, the stream sequence counter draw and the stream record insert) map a transient conflict through `write_conflict::transaction_error` to `StorageError::Transient`, which the transaction bodies convert into a re-run of the transaction; any other driver error stays `StorageError::Internal`. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The TTL worker's conditional delete runs the DeleteItem loop, so it takes the deadline too. `BatchWriteItem` runs its writes inside one `request_clock` scope, so the loops of one batch measure one deadline from the start of the request, and the handler returns a request that reaches it in `UnprocessedItems` when another request of the batch applied, and answers 503 `ServiceUnavailable` when none did. `BatchExecuteStatement` runs its statements inside one scope too. The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. The deadline costs CPU on the server, because the losers of a hot key retry with a backoff capped at 12.8 ms for up to 10 s: with 64 and then 256 writers putting one key of a GSI table for 20 s, on a 16-core host at a load average of 18 to 21, mongod used a median of 132 and 128 percent of one core and ran 1,666 and 1,811 transactions a second, of which 98.7 and 99.6 percent aborted. An unconditional PutItem that keeps losing the create race gives up after `TRANSIENT_RETRY_ATTEMPTS` (50) lost races. Exhausted retries on PutItem, DeleteItem and UpdateItem all return the `StorageError::Transient` built by `write_conflict::retry_budget_exhausted` (its message names the operation, the number of attempts and the last error that ended an attempt), answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on `TransactWriteItems` they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. A write that reaches the deadline gives up the same way: PutItem, DeleteItem and UpdateItem return the `StorageError::Transient` built by `write_conflict::contention_deadline_exhausted`, which names the deadline instead of the attempts, and a `TransactWriteItems` answers as it does when its attempts run out. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. **UpdateItem** additionally uses an optimistic-concurrency version guard on top of the transaction. A `_v` counter is stored on each document. The write path reads the current `_v` under the transaction snapshot, applies the update expression in memory, sets `_v = current_version + 1`, then executes `replace_one` filtered on both the primary key AND the expected `_v`. If `matched_count == 0`, a concurrent writer committed a higher version between the snapshot read and the replace; the attempt aborts and the outer retry loop re-reads. The version guard doubles up when a native-fast-path update (unconditional, no stream, no GSI) is possible — that path uses a single `find_one_and_update` with `$inc: {_v: 1}` outside a transaction, and always bumps the counter so a concurrent session-scoped update against a stale snapshot fails its versioned filter and retries. From 076975f3c442afc42a770e925b8951e2e62a2efe Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 24 Nov 2026 07:38:23 +0000 Subject: [PATCH 38/44] style(mongodb): clear clippy's pedantic warnings on the lines this branch adds - Main raised the minimum Rust version to 1.91.1, where Duration::from_mins is stable, so the pedantic group now asks for it. The two 600 s test deadlines read Duration::from_mins(10), and the 120 s upper bound of the attempt budget test reads Duration::from_mins(2). - The doc comments of create_race_exhausted and of the put race tests write PutItem, DeleteItem, MongoDB and PostgreSQL in backticks. - With this, cargo clippy -W clippy::pedantic reports no warning on a line this branch adds against main. --- crates/storage-mongodb/src/data_engine.rs | 4 ++-- .../src/data_engine_live_tests_contention.rs | 6 +++--- .../src/data_engine_live_tests_put_race.rs | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/storage-mongodb/src/data_engine.rs b/crates/storage-mongodb/src/data_engine.rs index 3bd3c1fd1..53ca590d4 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -4091,14 +4091,14 @@ pub(crate) fn is_duplicate_key(e: &mongodb::error::Error) -> bool { ) } -/// The error when every attempt of an unconditional PutItem lost the create +/// The error when every attempt of an unconditional `PutItem` lost the create /// race: on the sessionless path, each attempt found no item, and its insert /// hit an item another writer created, which was deleted again before the /// next attempt read it; on the transaction path, `TRANSIENT_RETRY_ATTEMPTS` /// attempts met a document that holds the item's `_id` without matching the /// key filter. /// `attempt` is the number of attempts made. The message is the internal -/// error (HTTP 500) the PostgreSQL backend returns when its own retries of +/// error (HTTP 500) the `PostgreSQL` backend returns when its own retries of /// this race run out. fn create_race_exhausted(attempt: u32) -> StorageError { StorageError::Internal(format!( diff --git a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs index 10b039d5a..1295172b8 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests_contention.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs @@ -143,7 +143,7 @@ async fn stored_item(engine: &MongoEngine, key_info: &TableKeyInfo, pk: &str) -> async fn live_concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit() { const WRITERS: usize = 32; const PUTS: usize = 8; - let engine = Arc::new(engine_with_deadline(Duration::from_secs(600)).await); + let engine = Arc::new(engine_with_deadline(Duration::from_mins(10)).await); let key_info = Arc::new(gsi_path_table(&engine, "hot-key")); let gsi_key_schema = vec![KeySchemaElement { attribute_name: "gk".to_owned(), @@ -468,7 +468,7 @@ async fn live_a_transaction_that_reaches_the_deadline_answers_a_canceled_transac #[tokio::test] #[ignore = "needs a MongoDB replica set in EXTENDDB_TEST_MONGODB_URI"] async fn live_unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error() { - let engine = engine_with_deadline(Duration::from_secs(600)).await; + let engine = engine_with_deadline(Duration::from_mins(10)).await; let maps = ExpressionMaps::default(); let key_info = gsi_path_table(&engine, "held-key"); let mut holder = hold_item_in_a_transaction(&engine, &key_info, "k1").await; @@ -513,7 +513,7 @@ async fn live_unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_t // The 1,000 attempts take 9.5 to 12.7 s on a shared host, and the sleeps // alone come to 12.8 s at worst, so the upper bound is loose: what it // rules out is a budget that no longer ends the loop at all. - assert!(elapsed < Duration::from_secs(120), "took {elapsed:?}"); + assert!(elapsed < Duration::from_mins(2), "took {elapsed:?}"); assert_eq!( stored_item(&engine, &key_info, "k1").await, Some(item_with("k1", "held")) diff --git a/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs b/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs index bc0c3eefb..463fbec24 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests_put_race.rs @@ -1,7 +1,7 @@ // Copyright 2026 ExtendDB contributors // SPDX-License-Identifier: Apache-2.0 -//! Live MongoDB tests for an unconditional PutItem whose insert hits the +//! Live `MongoDB` tests for an unconditional `PutItem` whose insert hits the //! `_id_` unique index: the put's filter on the key finds no item, and //! another document already holds the item's `_id`. On the sessionless path //! that is how a put that loses the create race to another put fails. On the @@ -52,7 +52,7 @@ fn document_id(item: &Item, key_info: &TableKeyInfo) -> Bson { .expect("document has an _id") } -/// An unconditional PutItem whose insert keeps colliding on `_id` retries for +/// An unconditional `PutItem` whose insert keeps colliding on `_id` retries for /// the whole retry budget, then gives up with the create race error, on the /// sessionless path and on the transaction path. /// @@ -108,11 +108,11 @@ enum Obstruction { /// It is replaced, under the same `_id`, with the item of another put /// that created the key. BecomesTheOtherPutsItem, - /// It is deleted, as a DeleteItem removes the item another put created. + /// It is deleted, as a `DeleteItem` removes the item another put created. IsDeleted, } -/// An unconditional PutItem whose first attempts collide on `_id` succeeds on +/// An unconditional `PutItem` whose first attempts collide on `_id` succeeds on /// a later attempt, on the sessionless path and on the transaction path. /// /// The put starts while a document holds the item's `_id` under another From c85e24e5b2d54335fdab2c47158253a73126fb02 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 24 Nov 2026 07:55:19 +0000 Subject: [PATCH 39/44] test(put-item): give the hot-key floor's comment the measured shed counts The comment above the floor said a 50-attempt server sheds about a quarter of the puts. It now gives the counts of two runs against a server built from main's production files. Those shed 431 and 444 of 2,016 puts at a load average of 17 to 19. The margin to the floor of 1,815 can then be read off. --- tests/test_put_item_gsi_contention.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_put_item_gsi_contention.py b/tests/test_put_item_gsi_contention.py index 6729d52ca..31f7ef0a5 100644 --- a/tests/test_put_item_gsi_contention.py +++ b/tests/test_put_item_gsi_contention.py @@ -148,9 +148,9 @@ def test_unconditional_puts_on_one_key_of_a_gsi_table_commit_or_are_shed( # is under, so the count is reported and bounded: a shed put is the only # failure a caller can get, and at least nine in ten of the puts commit. # A server that gives up after 50 attempts, the budget the other retry - # loops keep, shed about a quarter of the puts of this shape in two runs - # at a load average of 14 to 20, and fails the second check; a server - # that gives up at the first conflict sheds nearly every put. + # loops keep, shed 431 and 444 of the 2,016 puts in two runs at a load + # average of 17 to 19, and fails the second check; a server that gives up + # at the first conflict sheds nearly every put. print(f"{len(failures)} of {total} puts were shed, {committed} committed") assert set(failures) <= {SHED}, ( f"{len(failures)} of {total} puts failed with something other than a shed put: " From eaa83a5ee185ce16cb202db0f317dca34d05c620 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Wed, 25 Nov 2026 05:34:03 +0000 Subject: [PATCH 40/44] fix(partiql): answer a contended batch statement with TransactionConflict On MongoDB, a BatchExecuteStatement statement whose write reached the contention deadline or the attempt budget answered the code ValidationError. The message was "Service is temporarily unavailable", and the entry named the table. A client that does not retry a ValidationError drops that write. The service answers a statement whose item concurrent TransactWriteItems calls hold with the code TransactionConflict and no TableName. Its message is "Transaction is ongoing for the item", and the batch answers 200 (21 of 60 and 22 of 60 batches, measured 2026-11-25). StatementError::from_error maps ServiceUnavailable and TransactionConflictException to that entry and takes the message from CancellationReason::transaction_conflict. from_item_error leaves the TableName out of it. On Cassandra, a statement whose item a transaction holds arrives as TransactionConflictException and gets the same entry. The engine answers every transient storage fault with the same 503, a lost connection among them, so each of these faults gets this entry too. The BatchExecuteStatement test of test_mongodb_batch_write_contention.py asserts the three entries by equality. A unit test checks both errors through from_error and from_item_error. The differences row and the RFC paragraph say what such a statement answers. --- crates/engine/src/partiql/common.rs | 50 +++++++++++++------- crates/engine/src/partiql/common/tests.rs | 28 +++++++++++ docs/differences-from-dynamodb.md | 2 +- docs/rfcs/0000-mongodb-backend.md | 2 +- tests/test_mongodb_batch_write_contention.py | 12 ++++- 5 files changed, 74 insertions(+), 20 deletions(-) diff --git a/crates/engine/src/partiql/common.rs b/crates/engine/src/partiql/common.rs index 4e73cee80..053a3f11b 100644 --- a/crates/engine/src/partiql/common.rs +++ b/crates/engine/src/partiql/common.rs @@ -473,12 +473,12 @@ pub(super) fn flatten_and_lossy<'a>(expr: &'a WhereExpr, out: &mut Vec<&'a Where /// Per-statement failures inside a batch surface as short Codes without the /// `Exception` suffix, and the call itself stays HTTP 200. `TableName` is echoed /// only for an authorization refusal and the failures of the item operation a -/// statement runs as (see [`StatementError::from_item_error`]); the errors of -/// reading and translating a statement carry the `Error` object alone, -/// matching the service. A conditional failure on a -/// statement that set `ReturnValuesOnConditionCheckFailure` to `ALL_OLD` -/// carries the existing item on the `Error` itself, never on the entry -/// (measured 2026-11-02). +/// statement runs as, other than a write that lost to contention (see +/// [`StatementError::from_item_error`]); the errors of reading and translating +/// a statement carry the `Error` object alone, matching the service. A +/// conditional failure on a statement that set +/// `ReturnValuesOnConditionCheckFailure` to `ALL_OLD` carries the existing +/// item on the `Error` itself, never on the entry (measured 2026-11-02). pub(super) struct StatementError { pub code: &'static str, pub message: String, @@ -516,6 +516,24 @@ impl StatementError { include_table_name: true, item: None, }, + // A write the backend could not apply now. On MongoDB that is a + // write that reached its contention deadline or spent its attempt + // budget, which a single-item request answers 503. The engine + // answers a transient storage fault, a lost connection among them, + // with the same 503, so it takes this entry too. An item that a + // transaction holds arrives as `TransactionConflictException`. The + // service answers a statement whose item concurrent transactions + // hold with this code and message, and no `TableName` (measured + // 2026-11-25). + DynamoDbError::ServiceUnavailable(_) + | DynamoDbError::TransactionConflictException(_) => Self { + code: "TransactionConflict", + message: CancellationReason::transaction_conflict() + .message + .unwrap_or_default(), + include_table_name: false, + item: None, + }, // Everything else a well-formed statement can raise per-statement is // a validation error (partial key, bad value, key-type mismatch). other => Self { @@ -528,18 +546,18 @@ impl StatementError { } /// Map an error of the item operation a statement runs as: as - /// [`Self::from_error`], carrying the statement's `TableName`. The service - /// names the table on each of them: a key value past its size limit - /// (measured 2026-11-02 for SELECT, INSERT, UPDATE and DELETE), an - /// UPDATE of a key attribute, a secondary index key of the wrong type, - /// an update the item cannot take, and the refusal of - /// [`condition_key_refusal`] (measured 2026-11-05). The errors of reading - /// and translating a statement name none: a key value of the wrong type, - /// a missing parameter, a WHERE without the full key, a path into a key - /// attribute tested against values. + /// [`Self::from_error`], carrying the statement's `TableName` unless the + /// write lost to contention. The service names the table on each of the + /// others: a key value past its size limit (measured 2026-11-02 for + /// SELECT, INSERT, UPDATE and DELETE), an UPDATE of a key attribute, a + /// secondary index key of the wrong type, an update the item cannot take, + /// and the refusal of [`condition_key_refusal`] (measured 2026-11-05). + /// The errors of reading and translating a statement name none: a key + /// value of the wrong type, a missing parameter, a WHERE without the full + /// key, a path into a key attribute tested against values. pub(super) fn from_item_error(err: &DynamoDbError) -> Self { let mut error = Self::from_error(err); - error.include_table_name = true; + error.include_table_name = error.code != "TransactionConflict"; error } diff --git a/crates/engine/src/partiql/common/tests.rs b/crates/engine/src/partiql/common/tests.rs index 6b6617732..51f2d102f 100644 --- a/crates/engine/src/partiql/common/tests.rs +++ b/crates/engine/src/partiql/common/tests.rs @@ -183,6 +183,34 @@ fn an_error_of_the_item_operation_names_the_batch_table() { } } +/// A write that lost to contention, answered 503 on the single-item API or +/// held by a transaction, is the service's entry for an item that concurrent +/// transactions hold, with no `TableName` (measured 2026-11-25). +#[test] +fn a_write_that_lost_to_contention_answers_transaction_conflict() { + let conflict = json!({ + "Error": { + "Code": "TransactionConflict", + "Message": "Transaction is ongoing for the item" + } + }); + for error in [ + DynamoDbError::ServiceUnavailable("Service is temporarily unavailable".to_owned()), + DynamoDbError::TransactionConflictException("held".to_owned()), + ] { + assert_eq!( + StatementError::from_item_error(&error).into_entry(Some("t")), + conflict, + "{error:?}" + ); + assert_eq!( + StatementError::from_error(&error).into_entry(Some("t")), + conflict, + "{error:?}" + ); + } +} + #[test] fn a_conditional_failure_without_an_item_has_none() { let err = DynamoDbError::ConditionalCheckFailedException( diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index 21e26f8b3..0aed33313 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -210,7 +210,7 @@ alone, with the same wording. For PartiQL this holds in `ExecuteStatement`, | On-demand capacity | Automatic scaling | Fixed initial burst capacity (4000 WCU / 12000 RCU), no auto-scaling | | Warm throughput | Pre-provisions partitions to the requested read and write units; raising it is a long-running operation (`Status: UPDATING` for minutes, the previous values reported meanwhile); a request above the account's table maximum is `LimitExceededException` | Stored and reported with the same shape, defaults and validation so clients that read it (the Terraform AWS provider since 6.13.0) work: the previous values are reported with `Status: UPDATING` for `control_plane_delay_seconds` after a change, then the new values `ACTIVE`; a decrease is measured against the reported values, as on the service. Nothing acts on the values: no pre-warming and no scaling at the database level. The account maximum is not known to ExtendDB and is not enforced. A provisioned table whose warm throughput was never set, or was set on one side only, reports its new units at once after a capacity change, where the service reports the old units until the table is `ACTIVE`. Switching a table to `PAY_PER_REQUEST` stores 12000 read and 4000 write units where its warm throughput is lower, and the stored values stay after a switch back to `PROVISIONED`, as on the service; the response to the switch reads the new values with `Status: UPDATING` for `control_plane_delay_seconds`, where the service answers with the previous values and `Status: ACTIVE` | | Throttling | Always on; throttles requests that exceed provisioned/burst capacity. No setting to disable | Configurable via `throttling_enabled` runtime setting (default: `true`) | -| Sustained contention on one item (MongoDB backend) | For `PutItem`, `DeleteItem` and `UpdateItem`, see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure. A `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | One request's retry loop (see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure) stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. Without the deadline the same 256-writer shape left 89 of 5,120 calls waiting when the client's 120 s read timeout fired, so a client sees a shed request where it used to see a request outlive its own read timeout. For a `BatchWriteItem`, see "`BatchWriteItem` where no request could be applied now" under Storage and Infrastructure. The statements of a `BatchExecuteStatement` share one deadline too. SQLite and PostgreSQL keep writes on one key waiting rather than shedding them, and Cassandra answers some of them with `TransactionConflictException` (#802). The deadline applies to MongoDB only | +| Sustained contention on one item (MongoDB backend) | For `PutItem`, `DeleteItem` and `UpdateItem`, see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure. A `TransactWriteItems` that loses to other transactions on one key is answered 400 `TransactionCanceledException` with a `TransactionConflict` reason (107 of 160 calls measured) | One request's retry loop (see "Contended single-item writes on a MongoDB table that has a stream or an index" under Storage and Infrastructure) stops after 10 seconds (`CONTENTION_DEADLINE`) or 1,000 attempts, whichever comes first, and the request is then shed instead of waiting out the queue of writers on that key. `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason per item on either bound, which is the answer the service gives. How many requests are shed depends on the load on the server's host. Measured with one GSI table and all writers on one key: at 256 writers 1,262 of 5,120 puts committed and 3,858 were shed, with the longest call 11.7 s. Without the deadline the same 256-writer shape left 89 of 5,120 calls waiting when the client's 120 s read timeout fired, so a client sees a shed request where it used to see a request outlive its own read timeout. For a `BatchWriteItem`, see "`BatchWriteItem` where no request could be applied now" under Storage and Infrastructure. The statements of a `BatchExecuteStatement` share one deadline too, and a statement that reaches either bound answers the per-statement error `TransactionConflict` "Transaction is ongoing for the item", which is what the service answers for a statement whose item a transaction holds. SQLite and PostgreSQL keep writes on one key waiting rather than shedding them, and Cassandra answers some of them with `TransactionConflictException` (#802). The deadline applies to MongoDB only | ## Transactions diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index 254594c29..c9b3bc123 100644 --- a/docs/rfcs/0000-mongodb-backend.md +++ b/docs/rfcs/0000-mongodb-backend.md @@ -136,7 +136,7 @@ Transaction implementation: `data_engine.rs` — `transact_write_items_impl`, `t ### Write conflict handling -**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place, which is idempotent, up to `TRANSIENT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff up to `CONTENTION_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). `backoff_sleep` waits a uniform 0 to 50 µs × 2^min(attempt, 8), which is at most 12.8 ms from the ninth attempt on and sums to at most about 550 ms over 50 attempts (275 ms expected); tokio's timer ticks every millisecond and rounds a sleep up to the next tick, so for the first five attempts (longest jitter under 1 ms) the wait is set by the tick, not by the jitter. The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over six runs on a shared host. The helpers that run inside the transaction (index entry writes, the stream shard lookup, the stream sequence counter draw and the stream record insert) map a transient conflict through `write_conflict::transaction_error` to `StorageError::Transient`, which the transaction bodies convert into a re-run of the transaction; any other driver error stays `StorageError::Internal`. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The TTL worker's conditional delete runs the DeleteItem loop, so it takes the deadline too. `BatchWriteItem` runs its writes inside one `request_clock` scope, so the loops of one batch measure one deadline from the start of the request, and the handler returns a request that reaches it in `UnprocessedItems` when another request of the batch applied, and answers 503 `ServiceUnavailable` when none did. `BatchExecuteStatement` runs its statements inside one scope too. The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. The deadline costs CPU on the server, because the losers of a hot key retry with a backoff capped at 12.8 ms for up to 10 s: with 64 and then 256 writers putting one key of a GSI table for 20 s, on a 16-core host at a load average of 18 to 21, mongod used a median of 132 and 128 percent of one core and ran 1,666 and 1,811 transactions a second, of which 98.7 and 99.6 percent aborted. An unconditional PutItem that keeps losing the create race gives up after `TRANSIENT_RETRY_ATTEMPTS` (50) lost races. Exhausted retries on PutItem, DeleteItem and UpdateItem all return the `StorageError::Transient` built by `write_conflict::retry_budget_exhausted` (its message names the operation, the number of attempts and the last error that ended an attempt), answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on `TransactWriteItems` they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. A write that reaches the deadline gives up the same way: PutItem, DeleteItem and UpdateItem return the `StorageError::Transient` built by `write_conflict::contention_deadline_exhausted`, which names the deadline instead of the attempts, and a `TransactWriteItems` answers as it does when its attempts run out. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. +**Session-scoped writes** (PutItem, DeleteItem, UpdateItem, TransactWriteItems) detect transient MongoDB conflicts via `is_transient_write_conflict`, which returns true for the `TransientTransactionError` label or a raw `WriteConflict` (code 112). It does not match the `UnknownTransactionCommitResult` label: under that label the commit may already have applied, so the body must not be re-run, and `classify_commit_error` handles it on its own by retrying `commitTransaction` in place, which is idempotent, up to `TRANSIENT_RETRY_ATTEMPTS` (50). Conflicts trigger a retry loop with jittered exponential backoff up to `CONTENTION_RETRY_ATTEMPTS` (1,000; MongoDB aborts the loser of a same-document conflict at once and the backoff is random, so a writer on a key shared by N clients can lose many rounds in a row: 50 attempts left 15 to 25 percent of the puts of 32 clients on one key without a commit, 400 left one put in 2,016 on a loaded host). `backoff_sleep` waits a uniform 0 to 50 µs × 2^min(attempt, 8), which is at most 12.8 ms from the ninth attempt on and sums to at most about 550 ms over 50 attempts (275 ms expected); tokio's timer ticks every millisecond and rounds a sleep up to the next tick, so for the first five attempts (longest jitter under 1 ms) the wait is set by the tick, not by the jitter. The sleeps of 1,000 attempts come to about 6.4 s on average and 12.8 s at worst, before the attempts' own round trips; a put that loses all 1,000 attempts to a transaction another session holds open answered in 9.5 to 12.7 s over six runs on a shared host. The helpers that run inside the transaction (index entry writes, the stream shard lookup, the stream sequence counter draw and the stream record insert) map a transient conflict through `write_conflict::transaction_error` to `StorageError::Transient`, which the transaction bodies convert into a re-run of the transaction; any other driver error stays `StorageError::Internal`. The attempt budget bounds attempts and not wall time, so the loops that serve a request (PutItem, DeleteItem, UpdateItem, TransactWriteItems) also carry `CONTENTION_DEADLINE` (10 s), read before each attempt starts: a request answers within the deadline plus the attempt already running, instead of queueing behind other writers on one key until the client's read timeout fires (at 256 writers putting one key of a GSI table, 89 of 5,120 calls were still waiting at 120 s without it). The TTL worker's conditional delete runs the DeleteItem loop, so it takes the deadline too. `BatchWriteItem` runs its writes inside one `request_clock` scope, so the loops of one batch measure one deadline from the start of the request, and the handler returns a request that reaches it in `UnprocessedItems` when another request of the batch applied, and answers 503 `ServiceUnavailable` when none did. `BatchExecuteStatement` runs its statements inside one scope too, and a statement that reaches either bound answers the per-statement error `TransactionConflict` "Transaction is ongoing for the item", which is what the service answers for a statement whose item a transaction holds. The GSI backfill loop shares the attempt budget and takes no deadline, because it runs in the background worker where no caller is waiting. The deadline costs CPU on the server, because the losers of a hot key retry with a backoff capped at 12.8 ms for up to 10 s: with 64 and then 256 writers putting one key of a GSI table for 20 s, on a 16-core host at a load average of 18 to 21, mongod used a median of 132 and 128 percent of one core and ran 1,666 and 1,811 transactions a second, of which 98.7 and 99.6 percent aborted. An unconditional PutItem that keeps losing the create race gives up after `TRANSIENT_RETRY_ATTEMPTS` (50) lost races. Exhausted retries on PutItem, DeleteItem and UpdateItem all return the `StorageError::Transient` built by `write_conflict::retry_budget_exhausted` (its message names the operation, the number of attempts and the last error that ended an attempt), answered as HTTP 503 `ServiceUnavailable`, which the SDKs retry; on `TransactWriteItems` they surface as a `TransactionCanceled` with a synthetic per-op `TransactionConflict` cancellation reason so wire consumers see the DDB-canonical error string instead of a bare HTTP 500. A write that reaches the deadline gives up the same way: PutItem, DeleteItem and UpdateItem return the `StorageError::Transient` built by `write_conflict::contention_deadline_exhausted`, which names the deadline instead of the attempts, and a `TransactWriteItems` answers as it does when its attempts run out. The deadline is carried by the engine (`MongoEngine::contention_deadline`, `CONTENTION_DEADLINE` in every production construction) so a test can make one bound or the other the one its write reaches, instead of racing 1,000 attempts against a wall clock. **UpdateItem** additionally uses an optimistic-concurrency version guard on top of the transaction. A `_v` counter is stored on each document. The write path reads the current `_v` under the transaction snapshot, applies the update expression in memory, sets `_v = current_version + 1`, then executes `replace_one` filtered on both the primary key AND the expected `_v`. If `matched_count == 0`, a concurrent writer committed a higher version between the snapshot read and the replace; the attempt aborts and the outer retry loop re-reads. The version guard doubles up when a native-fast-path update (unconditional, no stream, no GSI) is possible — that path uses a single `find_one_and_update` with `$inc: {_v: 1}` outside a transaction, and always bumps the counter so a concurrent session-scoped update against a stale snapshot fails its versioned filter and retries. diff --git a/tests/test_mongodb_batch_write_contention.py b/tests/test_mongodb_batch_write_contention.py index 3d4af104f..1d8ab1a28 100644 --- a/tests/test_mongodb_batch_write_contention.py +++ b/tests/test_mongodb_batch_write_contention.py @@ -39,6 +39,12 @@ DEADLINE = 10 SLACK = 3 HOLD = DEADLINE + SLACK + 2 +# The entry Amazon DynamoDB answers for a BatchExecuteStatement statement whose +# item concurrent TransactWriteItems calls hold, in a batch that answers 200 +# (measured 2026-11-25). A statement the server gives up on answers it too. +CONFLICT = { + "Error": {"Code": "TransactionConflict", "Message": "Transaction is ongoing for the item"} +} @pytest.fixture() @@ -216,7 +222,8 @@ def test_the_statements_of_a_batch_execute_statement_share_one_contention_deadli first statement gives up when the deadline passes, and the other two give up after one attempt each, because the deadline is measured from the start of the batch. A deadline per statement would keep the batch open until the - holds end and let the later statements commit. + holds end and let the later statements commit. Each statement answers + CONFLICT. """ pks = [f"s{i}-{uuid.uuid4().hex[:8]}" for i in range(3)] for pk in pks: @@ -242,5 +249,6 @@ def test_the_statements_of_a_batch_execute_statement_share_one_contention_deadli print(f"the batch answered in {elapsed:.2f} s: {resp['Responses']}") assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" - assert all("Error" in r for r in resp["Responses"]), resp["Responses"] + assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200 + assert resp["Responses"] == [CONFLICT] * 3, resp["Responses"] assert [_w(dynamodb_client, gsi_table, pk) for pk in pks] == ["v0"] * 3 From 6350dca96c9dd604caacdf975afff0a4864adeb9 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Wed, 25 Nov 2026 05:41:56 +0000 Subject: [PATCH 41/44] test(put-item): say what the hot-key floor bounds The floor of tests/test_put_item_gsi_contention.py is total * 9 // 10, which is 1,814 of the 2,016 puts. It bounds how many puts the server sheds, at most 202. It does not hold the attempt budget: with the budget at 50 instead of 1,000, the test passed in 3 of 4 runs on 2026-11-25. The comment above the floor and the assertion message say what the floor bounds. The comment names the four live tests in data_engine_live_tests_contention.rs that fail with a budget of 50. The fifth one, the request scope test, passes with it. --- tests/test_put_item_gsi_contention.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/tests/test_put_item_gsi_contention.py b/tests/test_put_item_gsi_contention.py index 31f7ef0a5..8f479466c 100644 --- a/tests/test_put_item_gsi_contention.py +++ b/tests/test_put_item_gsi_contention.py @@ -144,19 +144,30 @@ def test_unconditional_puts_on_one_key_of_a_gsi_table_commit_or_are_shed( failures = _hammer(raw_clients, gsi_table, pk) total = WRITERS * PUTS_PER_WRITER committed = total - len(failures) - # How many of the contended puts were shed depends on the load the server - # is under, so the count is reported and bounded: a shed put is the only - # failure a caller can get, and at least nine in ten of the puts commit. - # A server that gives up after 50 attempts, the budget the other retry - # loops keep, shed 431 and 444 of the 2,016 puts in two runs at a load - # average of 17 to 19, and fails the second check; a server that gives up - # at the first conflict sheds nearly every put. + # How many of the contended puts are shed depends on the load on the + # server's host, so the count is reported and bounded: a shed put is the + # only failure a caller can get, and at least nine in ten of the puts + # (1,814 of 2,016) must commit, so at most 202 are shed. The floor does + # not hold the attempt budget: a server with a budget of 50 instead of + # 1,000 can pass it. Four of the five live tests in + # crates/storage-mongodb/src/data_engine_live_tests_contention.rs hold the + # budget, because each of them fails with a budget of 50: + # live_concurrent_unconditional_puts_on_one_key_of_a_gsi_table_all_commit + # live_a_single_item_write_that_reaches_the_deadline_answers_a_transient_error + # live_a_transaction_that_reaches_the_deadline_answers_a_canceled_transaction + # live_unconditional_put_that_spends_its_attempt_budget_gives_up_with_a_transient_error + floor = total * 9 // 10 print(f"{len(failures)} of {total} puts were shed, {committed} committed") assert set(failures) <= {SHED}, ( f"{len(failures)} of {total} puts failed with something other than a shed put: " f"{sorted(set(failures))}" ) - assert committed >= total * 9 // 10, f"only {committed} of {total} puts committed" + assert committed >= floor, ( + f"only {committed} of {total} puts committed, under the floor of {floor}: more " + "than a tenth were shed. The live tests in " + "crates/storage-mongodb/src/data_engine_live_tests_contention.rs hold the " + "attempt budget" + ) final = dynamodb_client.get_item( TableName=gsi_table, Key={"pk": {"S": pk}}, ConsistentRead=True )["Item"] From 3e85e2f3071866ccecd12977012aa4cbc932233d Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Wed, 25 Nov 2026 06:34:19 +0000 Subject: [PATCH 42/44] fix(engine): read the batch request clock through web_time BatchWriteItem and BatchExecuteStatement read std::time::Instant::now for the start of their request clock scope. Both handlers are in the WebAssembly build. On wasm32-unknown-unknown std has no clock, so the call panics. The clippy step that main's wasm workflow gained in #1009 refuses the call in batch_write_item.rs, and this branch failed it after the merge of main. The handlers now call web_time::Instant::now, and request_clock holds a web_time::Instant. Elsewhere web_time re-exports std::time. Native builds run the same code. --- crates/engine/src/batch_write_item.rs | 3 +-- crates/engine/src/partiql/mod.rs | 2 +- crates/storage/src/request_clock.rs | 3 ++- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/batch_write_item.rs b/crates/engine/src/batch_write_item.rs index 71435306e..135579b8f 100755 --- a/crates/engine/src/batch_write_item.rs +++ b/crates/engine/src/batch_write_item.rs @@ -5,7 +5,6 @@ use std::collections::HashMap; use std::sync::Arc; -use std::time::Instant; use serde_json::Value; @@ -46,7 +45,7 @@ pub async fn handle_batch_write_item( body: Value, ctx: &OperationContext, ) -> Result { - let started = Instant::now(); + let started = web_time::Instant::now(); let input: BatchWriteItemInput = serde_json::from_value(body).map_err(crate::deserialize_error)?; diff --git a/crates/engine/src/partiql/mod.rs b/crates/engine/src/partiql/mod.rs index a93b9b3cf..ec00a8655 100644 --- a/crates/engine/src/partiql/mod.rs +++ b/crates/engine/src/partiql/mod.rs @@ -124,7 +124,7 @@ impl PartiqlExecutor for OperationContext { async fn batch_execute_statement(&self, body: Value) -> Result { // One contention deadline for the batch's statements: see `request_clock`. extenddb_storage::request_clock::scope( - std::time::Instant::now(), + web_time::Instant::now(), batch::run(&translation_context(self), &body), ) .await diff --git a/crates/storage/src/request_clock.rs b/crates/storage/src/request_clock.rs index 70c87e1e6..4d807492e 100644 --- a/crates/storage/src/request_clock.rs +++ b/crates/storage/src/request_clock.rs @@ -14,7 +14,8 @@ //! background worker get. use std::future::Future; -use std::time::Instant; + +use web_time::Instant; tokio::task_local! { static REQUEST_STARTED: Instant; From f5c22b6034f0aab515eb02f71d84d51bf733b986 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Thu, 3 Dec 2026 01:50:51 +0000 Subject: [PATCH 43/44] fix(partiql): name the table on a contended batch statement's entry The service names the statement's table on the TransactionConflict entry of BatchExecuteStatement. In us-east-1, 184 of 184 conflict entries carried TableName, in batches that answered 200 (measured by 2026-11-26). eaa83a5ee left TableName out of that entry. from_item_error now names the table on every entry again, as on main. The TransactionConflict arm of from_error names it too. The unit test and the BatchExecuteStatement wire test expect TableName. --- crates/engine/src/partiql/common.rs | 38 ++++++++++---------- crates/engine/src/partiql/common/tests.rs | 6 ++-- tests/test_mongodb_batch_write_contention.py | 11 +++--- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/crates/engine/src/partiql/common.rs b/crates/engine/src/partiql/common.rs index 053a3f11b..4a311fe1b 100644 --- a/crates/engine/src/partiql/common.rs +++ b/crates/engine/src/partiql/common.rs @@ -473,12 +473,12 @@ pub(super) fn flatten_and_lossy<'a>(expr: &'a WhereExpr, out: &mut Vec<&'a Where /// Per-statement failures inside a batch surface as short Codes without the /// `Exception` suffix, and the call itself stays HTTP 200. `TableName` is echoed /// only for an authorization refusal and the failures of the item operation a -/// statement runs as, other than a write that lost to contention (see -/// [`StatementError::from_item_error`]); the errors of reading and translating -/// a statement carry the `Error` object alone, matching the service. A -/// conditional failure on a statement that set -/// `ReturnValuesOnConditionCheckFailure` to `ALL_OLD` carries the existing -/// item on the `Error` itself, never on the entry (measured 2026-11-02). +/// statement runs as (see [`StatementError::from_item_error`]); the errors of +/// reading and translating a statement carry the `Error` object alone, +/// matching the service. A conditional failure on a +/// statement that set `ReturnValuesOnConditionCheckFailure` to `ALL_OLD` +/// carries the existing item on the `Error` itself, never on the entry +/// (measured 2026-11-02). pub(super) struct StatementError { pub code: &'static str, pub message: String, @@ -523,15 +523,15 @@ impl StatementError { // with the same 503, so it takes this entry too. An item that a // transaction holds arrives as `TransactionConflictException`. The // service answers a statement whose item concurrent transactions - // hold with this code and message, and no `TableName` (measured - // 2026-11-25). + // hold with this code and message, and the statement's `TableName` + // (184 of 184 entries in us-east-1, measured by 2026-11-26). DynamoDbError::ServiceUnavailable(_) | DynamoDbError::TransactionConflictException(_) => Self { code: "TransactionConflict", message: CancellationReason::transaction_conflict() .message .unwrap_or_default(), - include_table_name: false, + include_table_name: true, item: None, }, // Everything else a well-formed statement can raise per-statement is @@ -546,18 +546,18 @@ impl StatementError { } /// Map an error of the item operation a statement runs as: as - /// [`Self::from_error`], carrying the statement's `TableName` unless the - /// write lost to contention. The service names the table on each of the - /// others: a key value past its size limit (measured 2026-11-02 for - /// SELECT, INSERT, UPDATE and DELETE), an UPDATE of a key attribute, a - /// secondary index key of the wrong type, an update the item cannot take, - /// and the refusal of [`condition_key_refusal`] (measured 2026-11-05). - /// The errors of reading and translating a statement name none: a key - /// value of the wrong type, a missing parameter, a WHERE without the full - /// key, a path into a key attribute tested against values. + /// [`Self::from_error`], carrying the statement's `TableName`. The service + /// names the table on each of them: a key value past its size limit + /// (measured 2026-11-02 for SELECT, INSERT, UPDATE and DELETE), an + /// UPDATE of a key attribute, a secondary index key of the wrong type, + /// an update the item cannot take, and the refusal of + /// [`condition_key_refusal`] (measured 2026-11-05). The errors of reading + /// and translating a statement name none: a key value of the wrong type, + /// a missing parameter, a WHERE without the full key, a path into a key + /// attribute tested against values. pub(super) fn from_item_error(err: &DynamoDbError) -> Self { let mut error = Self::from_error(err); - error.include_table_name = error.code != "TransactionConflict"; + error.include_table_name = true; error } diff --git a/crates/engine/src/partiql/common/tests.rs b/crates/engine/src/partiql/common/tests.rs index 51f2d102f..028f34890 100644 --- a/crates/engine/src/partiql/common/tests.rs +++ b/crates/engine/src/partiql/common/tests.rs @@ -185,14 +185,16 @@ fn an_error_of_the_item_operation_names_the_batch_table() { /// A write that lost to contention, answered 503 on the single-item API or /// held by a transaction, is the service's entry for an item that concurrent -/// transactions hold, with no `TableName` (measured 2026-11-25). +/// transactions hold, with the statement's `TableName` (184 of 184 entries in +/// us-east-1, measured by 2026-11-26). #[test] fn a_write_that_lost_to_contention_answers_transaction_conflict() { let conflict = json!({ "Error": { "Code": "TransactionConflict", "Message": "Transaction is ongoing for the item" - } + }, + "TableName": "t" }); for error in [ DynamoDbError::ServiceUnavailable("Service is temporarily unavailable".to_owned()), diff --git a/tests/test_mongodb_batch_write_contention.py b/tests/test_mongodb_batch_write_contention.py index 1d8ab1a28..fa681f64f 100644 --- a/tests/test_mongodb_batch_write_contention.py +++ b/tests/test_mongodb_batch_write_contention.py @@ -39,9 +39,10 @@ DEADLINE = 10 SLACK = 3 HOLD = DEADLINE + SLACK + 2 -# The entry Amazon DynamoDB answers for a BatchExecuteStatement statement whose -# item concurrent TransactWriteItems calls hold, in a batch that answers 200 -# (measured 2026-11-25). A statement the server gives up on answers it too. +# The error Amazon DynamoDB answers for a BatchExecuteStatement statement whose +# item concurrent TransactWriteItems calls hold, in a batch that answers 200. +# The entry also names the statement's table (184 of 184 entries in us-east-1, +# measured by 2026-11-26). A statement the server gives up on answers it too. CONFLICT = { "Error": {"Code": "TransactionConflict", "Message": "Transaction is ongoing for the item"} } @@ -223,7 +224,7 @@ def test_the_statements_of_a_batch_execute_statement_share_one_contention_deadli up after one attempt each, because the deadline is measured from the start of the batch. A deadline per statement would keep the batch open until the holds end and let the later statements commit. Each statement answers - CONFLICT. + CONFLICT and the table's name. """ pks = [f"s{i}-{uuid.uuid4().hex[:8]}" for i in range(3)] for pk in pks: @@ -250,5 +251,5 @@ def test_the_statements_of_a_batch_execute_statement_share_one_contention_deadli print(f"the batch answered in {elapsed:.2f} s: {resp['Responses']}") assert elapsed < DEADLINE + SLACK, f"the batch answered in {elapsed:.2f} s" assert resp["ResponseMetadata"]["HTTPStatusCode"] == 200 - assert resp["Responses"] == [CONFLICT] * 3, resp["Responses"] + assert resp["Responses"] == [{**CONFLICT, "TableName": gsi_table}] * 3, resp["Responses"] assert [_w(dynamodb_client, gsi_table, pk) for pk in pks] == ["v0"] * 3 From 86d02ba7cb6cb93f39ce9dd2e9e3f726b8479693 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Thu, 3 Dec 2026 01:50:51 +0000 Subject: [PATCH 44/44] docs(upgrade): add a v1.2.0 entry for the MongoDB contention bounds The upgrade manual gets an entry for the two behaviour changes of this branch. A contended MongoDB write retries for up to 10 seconds. A BatchExecuteStatement statement that loses to contention, or that meets a fault the server answers with 503, answers TransactionConflict on every backend. The entry goes after the v1.2.0 entry of main. --- docs/manuals/07-upgrade-manual.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index a5c914465..992367511 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -401,6 +401,16 @@ deployment, even when no catalog migration is required. **Who is affected.** PostgreSQL deployments that disable TTL on tables with long readers. No catalog migration or configuration change is required. The other backends are not affected. +### v1.2.0: on MongoDB, a contended write retries for up to 10 seconds, and a `BatchExecuteStatement` statement that loses answers `TransactionConflict` + +**What changed** (#487). On MongoDB, a `PutItem`, `DeleteItem`, `UpdateItem` or `TransactWriteItems` that loses to other writes on one item tries again. It stops when it commits, after 1,000 attempts, or 10 seconds after the start of the request. 1.1.0 stopped after 50 attempts. The requests of a `BatchWriteItem` and the statements of a `BatchExecuteStatement` share one 10 second deadline. Three answers change: + +- More contended writes commit, and a write that does not commit answers later. `PutItem`, `DeleteItem` and `UpdateItem` answer 503 `ServiceUnavailable`, and `TransactWriteItems` answers `TransactionCanceledException` with a `TransactionConflict` reason for each item, as in 1.1.0. The answer comes after the attempt that runs when the 10 seconds end. With 256 writers on one key, the longest call took 11.7 seconds, so a client read timeout near that can expire first. The writes that lose keep trying for that time, which uses more CPU on the MongoDB server. +- On MongoDB, a `BatchExecuteStatement` statement that reaches either bound answers the per-statement error `TransactionConflict` with the message `Transaction is ongoing for the item`, and the entry names the table. 1.1.0 answered `ValidationError` with the message `Service is temporarily unavailable`. +- On every backend, a `BatchExecuteStatement` statement that meets a fault that the server answers with 503, such as a lost storage connection, answers the same `TransactionConflict` entry. On Cassandra, so does a statement whose item a transaction holds. 1.1.0 answered these statements with `ValidationError` and the message of the error. + +**Who is affected.** MongoDB deployments with concurrent writers on one item, the read timeouts of their clients, and clients on any backend that read the `Code` of a `BatchExecuteStatement` entry. No catalog migration or configuration change is required. + ### v1.1.0: latency `segments` are summed into time buckets **What changed** (#398). The latency `segments` that `/metrics` and the console's `/console/metrics-data` return come from time buckets per account and operation. 1.0.0 kept one record per request for 24 hours. Each operation's count and means are still exact over the requests that the window covers. Four things change: