Repository navigation
fix(transactions): commit() no longer raises after the transaction was applied - #130
Conversation
…s applied Updated instances were refreshed by evaluating the update on the item as loaded; for an unversioned item another writer changed since, that could fail, and commit() raised a ValidationError after DynamoDB had applied the transaction. Such instances are now refreshed from the item as stored now, or forget their stored copy when it cannot be read. What a batch_writer() block that raises does is now documented and tested: it writes nothing more and keeps what is queued for flush(). Fixes #83 Co-authored-by: Carl Oscar Aaro <[email protected]>
|
Partly addressed as of fctl reviewed The fix does what it says: a failed local evaluation no longer makes On lines the diff doesn't show:
Reviewed by fctl · Review agent settings |
Also say in the architecture doc that a refresh after a commit reads the item when the update does not apply to the item as loaded. Co-authored-by: Carl Oscar Aaro <[email protected]>
|
Architecture doc finding: fixed in 6fee03a. The transaction section of |
|
Stack of 10 pull requests, merged in this order with "Squash and merge":
|
What and why
The two issues of #83:
commit()could raise after DynamoDB applied the transaction. Instances updated in a transaction are refreshed by evaluating the update on the item as it was loaded. For an unversioned item that another writer changed since, the evaluation could fail where DynamoDB's did not: alist_appendto a list the instance read as missing but another writer had stored.commit()then raised theValidationErrorthat normally means "not applied", and a caller who retried applied the change twice. Now, when the update does not apply to the loaded item, the instance is refreshed from the item as stored now (one consistent read). If that read fails or the item is gone, the instance forgets its stored copy, as if it had not been loaded. In both casescommit()returns normally andafter_updatehooks run. Versioned models are unaffected: their version condition guarantees the loaded item.BatchWriterblock that raises:__aexit__does not flush when the block raises, also when the exception comes from a failed automatic flush insideawait batch.put(). The writes are not dropped: they stay queued in the writer. I kept that behaviour, which matches transactions (a block that raises writes nothing), and documented it in the module and class docstrings, the writing guide and AGENT_GUIDE:len(batch)shows what is left, andawait batch.flush()writes it.Verified with integration tests on the in-memory backend and MiniStack: a transaction appending to a list another writer stored since the read refreshes the instance to
[1, 2], and one whose item is deleted right after the commit, or whose refresh read fails, returns with an unloaded instance; all fail without the fix. A fault-injection test shows that a block ending with a failed automatic flush, or with its own exception, keeps its writes for a laterflush().make checkpasses.Fixes #83
Checklist
make checkpasses locally (lint, types, clippy, consistency, tests)Worth confirming
flush().save()of it writes like one of a new instance.fctl session
Note
Low risk. It changes only what happens after a transaction was already applied, and only when refreshing an updated instance fails, plus documentation and tests for the batch writer.
What it does. A transaction refreshes the instances it updated by running the update on the item as it was loaded. For an unversioned item that another writer changed since, that could fail, so
commit()raised after DynamoDB had applied the transaction. Now such an instance is refreshed from the item as stored now, or forgets its stored copy if that read fails or the item is gone, andcommit()returns normally. The pull request also documents and tests that abatch_writer()block that raises writes nothing more and keeps its queue forflush(); the batch writer code itself is unchanged.Instance forgets its stored copy. When the fallback read fails or the item is gone, the updated instance becomes not loaded, so a later
save()of it writes like one of a new instance.fctl review's first look at
6fee03a· 9 files, +155 −8 · Review agent settings