Repository navigation
fix(databases): check transaction conflicts against the row version seen at staging - #14574
jackeddisciple wants to merge 10 commits into
Conversation
…een at staging The commit compared a row's $updatedAt with the time the operation was staged. A concurrent commit stamps its rows when it writes them but only makes them visible at COMMIT, so a claim staged inside that window passed the check and silently overwrote the first one. Staging now records the row's $updatedAt and the commit checks against that, so any write that lands after staging conflicts, no matter when it was stamped. Fixes appwrite#14489
🟢 Tier S · Ready to mergeTransaction logs now persist the row version observed when individual operations are staged, and commits use that version for conflict detection while preserving a staging-time fallback for legacy logs. The PR adds the transaction-log schema migration, captures versions across row-operation staging paths, and covers the behavior with unit and end-to-end tests. The newest changes also revise how bulk operations update transaction state and collect affected documents. Latest changes: The newest commits adjust bulk-operation commit handling to track updated state and event documents directly, and rename a loop variable in batch permission normalization.
Note @jackeddisciple does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve. Couldn't attach to a lineThese findings could not be placed on a changed line, so they were not posted as inline comments.
📂 Walkthrough · 11
Merged |
… against Upsert read the row a second time just to get $updatedAt. A write landing between the two reads got recorded as the version the upsert was based on, so the commit could overwrite it without a conflict. Staging now reads the row once and uses it for both the inherited permissions and the recorded version, same as update and delete.
The findings that requested changes are resolved. See the summary comment for what is still open.
|
@copilot resolve the merge conflicts in this pull request |
…taged row bulkCreate and bulkUpsert reused $document as the loop variable, so documentUpdatedAt got read off an array and staging crashed.
|
Hey @HarshMN2345 , any update on this PR? |
Still in review. It hasn't been a priority yet, but We will get to it. |
🟢 Tier S · Ready to merge
Transaction conflict checks now compare against the row version the caller read when staging, not the staging time. Update, upsert, delete and the batch operations endpoint record that Latest changes: Transactional upserts now read the row without the caller's permissions, in both the direct endpoint and the batch endpoint, so write-only upserts record the row version, and a new e2e test covers that case.
Note @jackeddisciple does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve. 📂 Walkthrough · 10
✅ Fixed since the last review · 1
Merged |
…permission Upsert needs update permission, not read, but a staged upsert read its row through the caller's permissions. A caller who could update the row but not read it got an empty document, so the staged log recorded no version and the commit fell back to the staging time. A write stamped between the row's version and the staging then got overwritten without a conflict. The upsert endpoint and upsert operations now read the row without the caller's permissions, as the library does when it writes the upsert, so the recorded version is the row's. The upsert endpoint also inherits the stored permissions in a transaction, as it already does outside one, instead of replacing them with the caller's defaults.
|
Hey @HarshMN2345 @ArnabChatterjee20k , any update on this PR review? |
What does this PR do?
The commit checked each row's
$updatedAtagainst the time the operation was staged. A committing transaction stamps a row when it writes it, but nobody else sees that write until COMMIT. If another client stages and reads the row in between, it still sees the old value. The other writer's stamp is also older than this client's staging time, so its commit passes the check and silently overwrites the first claim. That's the double claim from the issue.Staging now stores the row's
$updatedAton the transaction log (documentUpdatedAt), and the commit checks against that instead. The library always stamps a write after the row's previous$updatedAt, so any write that lands after staging conflicts. It no longer matters when the other write was stamped, or which node's clock stamped it. Logs staged before the upgrade have no value and fall back to the staging time, so transactions in flight during the upgrade keep working.transactionLogsgets adocumentUpdatedAtdatetime attribute, added to existing projects in V25.TransactionLogdocument picks the timestamp, and the commit uses it. I renamed$createdAtto$timestampin the commit handlers since it isn't always the creation time anymore. That rename is most of the diff inTransactions/Update.php.Known limits, unchanged here:
Test Plan
I reproduced it with the script from the issue on a local stack (TablesDB, Postgres). I added 3 ms of latency to Postgres with netem so a 91 operation commit takes a few seconds, like on a separate DB server. Without that the window is too short to hit locally.
probe 90 300 10burst 10 20 30gave one winner per round on both. I couldn't trigger that variant locally; the issue needed two API nodes for it.I also ran it on a project created on main. Staging keeps working before the migration, using the old check. After
migratethe column exists and the probe gives 0 of 10.Tests:
TransactionLogTest: against a real SQLite database, a write stamped before staging but landing after it now conflicts, an untouched row still commits, and older logs fall back to the staging time. Fails without the fix.MigrationVersionsTest::testV25LetsALegacyInstallStageTheRowVersion: atransactionLogstable without the column can store it after V25. Fails without the migration step.testCommitConflictsWithWriteStampedBeforeStaging: stages through the batch endpoint, update, upsert (with and without permissions) and delete. It then writes the row from outside with a$updatedAtbetween the old version and the staging time, which is what an in flight commit leaves behind. The commit must fail and the outside write must survive. Passes on TablesDB and Legacy, fails on main.The TablesDB and Legacy transaction suites and both ACID suites pass locally, except
testCommitEventPayloadCarriesSurfaceIds. That one fails the same way on main because I didn't run the webhook request catcher. I didn't run the DocumentsDB variants (no Mongo locally), so CI covers those. Utopia, Migration, Databases and General unit suites pass, and pint, phpstan and rector are clean.Related PRs and Issues
Fixes #14489
Checklist