diff --git a/crates/engine/src/batch_write_item.rs b/crates/engine/src/batch_write_item.rs index 3806e0e44..135579b8f 100755 --- a/crates/engine/src/batch_write_item.rs +++ b/crates/engine/src/batch_write_item.rs @@ -45,6 +45,7 @@ pub async fn handle_batch_write_item( body: Value, ctx: &OperationContext, ) -> Result { + let started = web_time::Instant::now(); let input: BatchWriteItemInput = serde_json::from_value(body).map_err(crate::deserialize_error)?; @@ -106,13 +107,17 @@ pub async fn handle_batch_write_item( lookups.push(ctx.table_key_info_uncached(table_name).await); } - write_batch( - &*ctx.storage, - &ctx.limits, - &ctx.region, - &input, - &tables, - lookups, + // One contention deadline for the batch's writes: see `request_clock`. + extenddb_storage::request_clock::scope( + started, + write_batch( + &*ctx.storage, + &ctx.limits, + &ctx.region, + &input, + &tables, + lookups, + ), ) .await } @@ -181,7 +186,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/engine/src/partiql/common.rs b/crates/engine/src/partiql/common.rs index 4e73cee80..4a311fe1b 100644 --- a/crates/engine/src/partiql/common.rs +++ b/crates/engine/src/partiql/common.rs @@ -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 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: true, + 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 { diff --git a/crates/engine/src/partiql/common/tests.rs b/crates/engine/src/partiql/common/tests.rs index 6b6617732..028f34890 100644 --- a/crates/engine/src/partiql/common/tests.rs +++ b/crates/engine/src/partiql/common/tests.rs @@ -183,6 +183,36 @@ 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 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()), + 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/crates/engine/src/partiql/mod.rs b/crates/engine/src/partiql/mod.rs index d3439eb8a..afbf79f34 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( + web_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/backup_live_tests.rs b/crates/storage-mongodb/src/backup_live_tests.rs index 7ca23a7b7..438a855ed 100644 --- a/crates/storage-mongodb/src/backup_live_tests.rs +++ b/crates/storage-mongodb/src/backup_live_tests.rs @@ -145,13 +145,8 @@ impl Fixture { async fn engine_on(catalog: &str, data: &str, liveness: BackupLiveness) -> MongoEngine { let client = mongodb::Client::with_uri_str(uri()).await.expect("client"); MongoEngine { - client: client.clone(), - catalog_db: client.database(catalog), - data_db: client.database(data), - region: REGION.to_owned(), - gsi_cache: Arc::default(), - tx_read_concern: mongodb::options::ReadConcern::snapshot(), backup_liveness: liveness, + ..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 6ecfc7f29..07d530ba3 100644 --- a/crates/storage-mongodb/src/data_engine.rs +++ b/crates/storage-mongodb/src/data_engine.rs @@ -487,7 +487,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) @@ -498,13 +515,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 @@ -514,9 +541,18 @@ impl MongoEngine { .map_err(|e| StorageError::Internal(e.to_string()))?; let tx_options = self.write_transaction_options(); + let mut lost_create_races = 0; + 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(crate::write_conflict::contention_deadline_exhausted( + "PutItem", + self.contention_deadline, + &last_cause, + )); + } let new_doc = item_to_document(&item, &key_info.key_schema, &key_info.attribute_definitions)?; // Capture before the transaction's first database operation. If @@ -530,6 +566,9 @@ impl MongoEngine { .await .map_err(|e| StorageError::Internal(e.to_string()))?; + // 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; @@ -596,12 +635,35 @@ impl MongoEngine { .upsert(true) .return_document(ReturnDocument::Before) .build(); - let old_doc = coll + // 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 backoff of a write + // conflict and counts the attempt against + // `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) .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(e.to_string())); + } + Err(e) => return Err(TxErr::from(e)), + }; old_item = old_doc.as_ref().map(document_to_item).transpose()?; } @@ -641,6 +703,18 @@ impl MongoEngine { Err(TxErr::Transient(cause)) => { last_cause = crate::write_conflict::retrying("PutItem", attempt, cause); let _ = session.abort_transaction().await; + if lost_create_race { + lost_create_races += 1; + // A duplicate key on `_id` counts against the create + // race budget: a put that has met a document holding + // the item's `_id` this many times is not waiting out + // contention on an item, so it gives up here with the + // create race error rather than spending the + // contention budget. + if lost_create_races >= TRANSIENT_RETRY_ATTEMPTS { + return Err(create_race_exhausted(lost_create_races)); + } + } backoff_sleep(attempt).await; continue; } @@ -654,7 +728,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, )) } @@ -727,9 +801,17 @@ impl MongoEngine { .map_err(|e| StorageError::Internal(e.to_string()))?; let tx_options = self.write_transaction_options(); + 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(crate::write_conflict::contention_deadline_exhausted( + "DeleteItem", + self.contention_deadline, + &last_cause, + )); + } // 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); @@ -831,7 +913,7 @@ impl MongoEngine { Err(crate::write_conflict::retry_budget_exhausted( "DeleteItem", - TRANSIENT_RETRY_ATTEMPTS, + CONTENTION_RETRY_ATTEMPTS, &last_cause, )) } @@ -984,9 +1066,17 @@ impl MongoEngine { .map_err(|e| StorageError::Internal(e.to_string()))?; let tx_options = self.write_transaction_options(); + 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(crate::write_conflict::contention_deadline_exhausted( + "UpdateItem", + self.contention_deadline, + &last_cause, + )); + } // 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. @@ -1200,7 +1290,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, )) } @@ -2369,8 +2459,22 @@ impl MongoEngine { ) }); + let contention_started = contention_clock_start(); + let mut last_cause = String::new(); - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + // 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 + // `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, self.contention_deadline) { + attempts_run = attempt; + break; + } let idempotency = idem_owned.as_ref().map(|(a, t, f)| IdempotencyKey { account_id: a.as_str(), token: t.as_str(), @@ -2568,7 +2672,7 @@ impl MongoEngine { // TransactionCanceled to TransactionCanceledException; the // reason codes are echoed back in the message. tracing::warn!( - attempts = TRANSIENT_RETRY_ATTEMPTS, + attempts = attempts_run, error = %last_cause, "TransactWriteItems: could not commit; answering TransactionCanceled" ); @@ -3474,7 +3578,11 @@ impl MongoEngine { ) .build(); - for attempt in 0..TRANSIENT_RETRY_ATTEMPTS { + // 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..CONTENTION_RETRY_ATTEMPTS { session .start_transaction() .with_options(tx_options.clone()) @@ -3747,13 +3855,91 @@ 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 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. 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` 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. `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 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 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. +/// +/// The attempt budget bounds the number of attempts, not the time a caller +/// 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 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`. +/// +/// 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 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 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 +/// 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); + /// 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 @@ -3876,17 +4062,65 @@ pub(crate) enum CommitOutcome { RerunBody(String), } -/// 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. pub(crate) 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: 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 +/// 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" + )) +} + +/// Whether the contention retry loop of a request that entered it at +/// `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, + deadline: std::time::Duration, +) -> bool { + 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` 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) } /// Build a `mongodb::error::Error` carrying the given error labels. @@ -4447,6 +4681,85 @@ 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, CONTENTION_DEADLINE)); + assert!(contention_deadline_passed(1, spent, CONTENTION_DEADLINE)); + assert!(contention_deadline_passed( + 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( + CONTENTION_RETRY_ATTEMPTS - 1, + 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 + )); + } + + /// 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)); + } + + /// 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))); + } + #[derive(Default)] struct RecordingGsiIndexWriter { direct_writes: AtomicUsize, @@ -4482,15 +4795,11 @@ mod tests { let client = mongodb::Client::with_uri_str("mongodb://localhost:27017") .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(), - }; + let engine = 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 { attribute_name: "pk".to_owned(), 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.rs b/crates/storage-mongodb/src/data_engine_live_tests.rs index ce91c4555..47934821b 100644 --- a/crates/storage-mongodb/src/data_engine_live_tests.rs +++ b/crates/storage-mongodb/src/data_engine_live_tests.rs @@ -44,6 +44,12 @@ mod delete_table; #[path = "data_engine_live_tests_update_window.rs"] mod update_window; +#[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..1295172b8 --- /dev/null +++ b/crates/storage-mongodb/src/data_engine_live_tests_contention.rs @@ -0,0 +1,522 @@ +// 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 futures::TryStreamExt as _; + +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}; + +/// 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 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 +/// 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(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(), + 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")); + 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!( + msg.starts_with(&format!("{operation}: could not commit in 2 seconds: ")), + "{msg}" + ); + 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 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!( + msg.starts_with("PutItem: could not commit in 2 seconds: "), + "{msg}" + ), + 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 +/// 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 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. +#[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_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; + + 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)) => { + // The budget is written out rather than read from + // `CONTENTION_RETRY_ATTEMPTS`, so a change to the constant fails + // here instead of changing what the test expects. + assert!( + msg.starts_with("PutItem: could not commit in 1000 attempts: "), + "{msg}" + ); + 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 upper bound is loose: what it + // rules out is a budget that no longer ends the loop at all. + assert!(elapsed < Duration::from_mins(2), "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/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..463fbec24 --- /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/crates/storage-mongodb/src/lib.rs b/crates/storage-mongodb/src/lib.rs index 59b515dcc..0e3025951 100644 --- a/crates/storage-mongodb/src/lib.rs +++ b/crates/storage-mongodb/src/lib.rs @@ -429,6 +429,12 @@ pub struct MongoEngine { tx_read_concern: mongodb::options::ReadConcern, /// Heartbeat timing of a restore's job record (see [`backup_engine`]). pub(crate) backup_liveness: backup_engine::BackupLiveness, + /// 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 @@ -618,7 +624,27 @@ impl MongoEngine { let catalog_db = catalog_database(&client); let data_db = client.database("extenddb_data"); - Ok(Self { + Ok(Self::from_parts( + client, + catalog_db, + data_db, + region, + tx_read_concern, + )) + } + + /// The engine on `catalog_db` and `data_db` of `client`, with every other + /// field at the value `new` gives it. `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, + ) -> Self { + Self { client, catalog_db, data_db, @@ -626,7 +652,27 @@ impl MongoEngine { gsi_cache: Arc::default(), tx_read_concern, backup_liveness: backup_engine::BackupLiveness::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. 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(), + ) } /// Read concern to use for a newly-opened multi-document transaction. 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), } } diff --git a/crates/storage-mongodb/src/snapshot_engine.rs b/crates/storage-mongodb/src/snapshot_engine.rs index cffda95f9..68c8cc562 100644 --- a/crates/storage-mongodb/src/snapshot_engine.rs +++ b/crates/storage-mongodb/src/snapshot_engine.rs @@ -530,13 +530,8 @@ mod tests { .await .expect("setting"); 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(), + ..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_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") diff --git a/crates/storage-mongodb/src/stream_records_live_tests.rs b/crates/storage-mongodb/src/stream_records_live_tests.rs index fadf3f6fb..1cfe76918 100644 --- a/crates/storage-mongodb/src/stream_records_live_tests.rs +++ b/crates/storage-mongodb/src/stream_records_live_tests.rs @@ -14,8 +14,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; @@ -53,13 +51,12 @@ impl Fixture { 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: REGION.to_owned(), - gsi_cache: Arc::default(), - tx_read_concern: mongodb::options::ReadConcern::snapshot(), - backup_liveness: crate::backup_engine::BackupLiveness::default(), + ..MongoEngine::for_test( + &client, + client.database(&format!("s{}_cat", &tag[..12])), + client.database(&format!("s{}_data", &tag[..12])), + ) }; Self { engine } } 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!( diff --git a/crates/storage/src/error.rs b/crates/storage/src/error.rs index 1334a3536..4198cd4d3 100755 --- a/crates/storage/src/error.rs +++ b/crates/storage/src/error.rs @@ -80,10 +80,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/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 1ddc642d7..5d176e859 100755 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -37,6 +37,7 @@ pub mod migrations; pub mod operations; pub mod password; pub mod policy_migration; +pub mod request_clock; pub mod restore_engine; pub mod secret_cipher; pub mod secret_keys; diff --git a/crates/storage/src/request_clock.rs b/crates/storage/src/request_clock.rs new file mode 100644 index 000000000..4d807492e --- /dev/null +++ b/crates/storage/src/request_clock.rs @@ -0,0 +1,70 @@ +// 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, 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. + +use std::future::Future; + +use web_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/docs/design/13-storage-mongodb.md b/docs/design/13-storage-mongodb.md index ce7a9d493..a1f94c2e5 100644 --- a/docs/design/13-storage-mongodb.md +++ b/docs/design/13-storage-mongodb.md @@ -753,13 +753,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 @@ -1143,7 +1145,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. | diff --git a/docs/differences-from-dynamodb.md b/docs/differences-from-dynamodb.md index 1e1257c8d..eec49e206 100755 --- a/docs/differences-from-dynamodb.md +++ b/docs/differences-from-dynamodb.md @@ -20,8 +20,8 @@ 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. | -| 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. | +| `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) @@ -220,6 +220,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, 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/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index f970cf6c9..0f6d4dc9d 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -442,6 +442,16 @@ In that time the table serves item operations, Query, Scan, TTL, tags and backup **Who is affected.** A client that pages a parallel Scan of a SQLite deployment across the upgrade. The start keys that 1.1.0 returned follow the old rule. ExtendDB can refuse such a key as a key from another segment. When ExtendDB accepts it, the later pages follow the new rule, so the client can miss items or get items twice. Start such a Scan again after the upgrade. A Scan that pages across a rollback to 1.1.0 can get the same answers. PostgreSQL, MongoDB and Cassandra are not affected. No catalog migration or configuration change is required. +### 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: diff --git a/docs/rfcs/0000-mongodb-backend.md b/docs/rfcs/0000-mongodb-backend.md index 54359b724..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. Conflicts trigger a retry loop with jittered exponential backoff up to `TRANSIENT_RETRY_ATTEMPTS` (50). `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 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 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`. 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. +**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. @@ -234,8 +234,8 @@ 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. | -| WriteConflict handling | Detect via the `TransientTransactionError` label or raw code 112; retry with backoff. A commit that ends in the `UnknownTransactionCommitResult` label is retried as a commit, never as a body | Converts a raw HTTP 500 into a retryable operation; TWI exhaustion surfaces as `TransactionCanceled` with per-op `TransactionConflict` reasons. | +| 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 the `TransientTransactionError` label or raw code 112; retry with backoff. A commit that ends in the `UnknownTransactionCommitResult` label is retried as a commit, never as a body | Converts a raw HTTP 500 into a retryable operation; a single-item write that reaches `CONTENTION_DEADLINE` or spends its attempt budget answers 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. | 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_mongodb_batch_write_contention.py b/tests/test_mongodb_batch_write_contention.py new file mode 100644 index 000000000..fa681f64f --- /dev/null +++ b/tests/test_mongodb_batch_write_contention.py @@ -0,0 +1,255 @@ +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 + +"""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 +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 +# 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"} +} + + +@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" + + +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. Each statement answers + CONFLICT and the table's name. + """ + 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 resp["ResponseMetadata"]["HTTPStatusCode"] == 200 + assert resp["Responses"] == [{**CONFLICT, "TableName": gsi_table}] * 3, resp["Responses"] + assert [_w(dynamodb_client, gsi_table, pk) for pk in pks] == ["v0"] * 3 diff --git a/tests/test_put_item_create_race.py b/tests/test_put_item_create_race.py index 752628d2a..9fd9416af 100644 --- a/tests/test_put_item_create_race.py +++ b/tests/test_put_item_create_race.py @@ -32,38 +32,19 @@ 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.""" +def raw_clients(endpoint_url): + """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"), - "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") @@ -153,9 +134,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 @@ -170,6 +151,7 @@ def _race( errors: list[BaseException] = [] def run(i: int): + client = clients[i] item = { **_key(table_kind, pk), "w": {"S": f"w{i}"}, @@ -225,25 +207,23 @@ 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 + 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)) -@XFAIL_UNTIL_MONGODB_FIX 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) @@ -251,21 +231,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 @@ -285,12 +265,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. @@ -302,11 +282,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"]) @@ -365,23 +345,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) diff --git a/tests/test_put_item_gsi_contention.py b/tests/test_put_item_gsi_contention.py new file mode 100644 index 000000000..8f479466c --- /dev/null +++ b/tests/test_put_item_gsi_contention.py @@ -0,0 +1,185 @@ +# 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. +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. 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, or that sheds more than a tenth of the puts. +""" + +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 call_with_handshake_retry, configured_backend, scoped_table + +WRITERS = 32 +PUTS_PER_WRITER = 63 +# 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") +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"}), + } + if endpoint_url: + kwargs["endpoint_url"] = endpoint_url + if endpoint_url.startswith("https://"): + kwargs["verify"] = False + return [boto3.client(**kwargs) for _ in range(WRITERS)] + + +@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(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, 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] = [] + errors: list[BaseException] = [] + lock = threading.Lock() + + def run(i: int): + client = clients[i] + 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: + call_with_handshake_retry(client.put_item, TableName=table, Item=item) + except ClientError as e: + 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 + 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"] + + +@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 +): + pk = f"hot-{uuid.uuid4().hex[:8]}" + failures = _hammer(raw_clients, gsi_table, pk) + total = WRITERS * PUTS_PER_WRITER + committed = total - len(failures) + # 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 >= 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"] + # 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 + 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}" diff --git a/tests/test_stream_write_contention.py b/tests/test_stream_write_contention.py index b8083e0fd..b4a5f490d 100644 --- a/tests/test_stream_write_contention.py +++ b/tests/test_stream_write_contention.py @@ -400,11 +400,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):