Repository navigation
fix(presences): reset created flag on each transaction attempt - #14103
Merged
Merged
Conversation
On databases without upsert-on-unique-index support, upsertForUser runs a find-then-create-or-update inside withTransaction. The created flag was only ever set to true inside the callback, so when the first attempt took the create branch and its commit failed with a retryable error, a rerun that found a presence committed in the meantime (and updated it) still reported a creation. Both callers use that signal to count a new presence in the users-presence usage metric, so the metric over-counted. Derive the flag from the lookup on every attempt so it always reflects what the committed attempt did. Co-Authored-By: Claude Opus 5.5 <[email protected]>
🟢 Tier S · Ready to merge
The change recalculates the presence-created flag on every transaction attempt so usage callbacks reflect the successful attempt. It adds a Memory-adapter test double and regression coverage for ordinary creation/update, retrying creation, and updating a concurrently created presence after a retry.
📂 Walkthrough · 3
Reviewed |
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
abnegate
added a commit
that referenced
this pull request
Oct 5, 2026
Bring in main e182a59: #14103 resets the presence created flag on every transaction attempt, #14104 purges the cached collection before a schema change is announced (the race behind the intermittent TablesDB testPatchAttribute 400 column_not_available), and #14105 deletes attributes through the databases handle. Conflicts: - src/Appwrite/Presences/State.php: kept the branch's lookup (userId query with authorization skipped) and took main's per-attempt reset of $presenceCreated, so a retried attempt that updates a concurrent presence no longer reports a creation. - tests/unit/Presences/StateTest.php (added on both sides): union of the branch's permission tests and main's retry tests. main's setUp() is ported to database 8.0 (Collection + Attribute::string instead of the array createCollection/createAttribute with Database::VAR_STRING). The retry test fails when the per-attempt reset is removed. Adaptations: - tests/e2e/Services/Databases/DatabasesBase.php: main's new testCreateIndexOnAttributeAsSoonAsAvailable sent Database::INDEX_KEY, which 8.0 removed; it now sends IndexType::Key->value like the rest of the file. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Problem. On databases without upsert-on-unique-index support,
Presences\State::upsertForUser()runs find-then-create-or-update insidewithTransaction().$presenceCreatedwas only ever set totrueinside the callback and never reset. If the first attempt took the create branch and its commit failed with a retryable error, a rerun that found a presence committed in the meantime (and updated it) still reported a creation, so both callers counted a new presence in the users-presence usage metric.Fix. Derive the flag from the lookup on every attempt (
$presenceCreated = $existingPresence->isEmpty()), so it reflects what the committed attempt did.Test Plan
New
tests/unit/Presences/StateTest.phprunsStateagainst the Memory database adapter. A test double fails the first commit with a retryable transaction error, and can optionally commit a competing presence for the same user before the retry. Cases:The last case fails on
main(Failed asserting that 1 is identical to 0) and passes with the fix.Related PRs and Issues
Found during the utopia-php/database 8.0.0 upgrade review (#11649).
Checklist
🤖 Generated with Claude Code