Skip to content

fix(presences): reset created flag on each transaction attempt - #14103

Merged
abnegate merged 1 commit into
mainfrom
fix-presence-retry-created-flag
Oct 5, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix-presence-retry-created-flag

Conversation

@abnegate

@abnegate abnegate commented Oct 5, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Problem. On databases without upsert-on-unique-index support, Presences\State::upsertForUser() runs find-then-create-or-update inside withTransaction(). $presenceCreated was only ever set to true inside 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.php runs State against 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:

  • create, then update: creation is reported once, only for the first upsert
  • a retried attempt that still creates: reported once
  • a retried attempt that updates the concurrently created presence: not reported, and the competing presence is returned and updated

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

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

🤖 Generated with Claude Code

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]>
@ghost

ghost commented Oct 5, 2026

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The flag reset preserves the caller contract and the regression tests cover the affected retry behavior; no concrete defects were found.

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.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Presences/State.php Derive the creation flag from each transaction attempt's lookup.
tests/unit/Presences/Mock/ConflictingMemory.php Add a retryable commit-conflict fake with an optional competing write.
tests/unit/Presences/StateTest.php Verify persisted presence state and creation notifications across creation, update, and retries.

Reviewed 1fd7758 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-presence-retry-created-flag (after).

Metric Before After Change
🚀 Requests/sec 192.14 193.13 ⚪ +0.5%
⏱️ Latency P50 88.99 ms 90.41 ms ⚪ +1.6%
⏱️ Latency P95 217.21 ms 206.78 ms ⚪ -4.8%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 90.41 206.78 12,369 193.13 -10.43
Account 173.98 327.68 651 10.83 -26.17
TablesDB 88.2 164.14 6,727 108.92 +5.64
Storage 81.58 174.6 3,255 53.89 -11.8
Functions 127.54 244.4 1,736 29.16 -24.5

Top API waits (after)

API request Max wait (ms)
account.name.update 543.04
account.prefs.update 530.07
functions.variables.update 446.76
storage.files.create 390.14
tablesdb.rows.create 341.86

@abnegate
abnegate merged commit 2aac602 into main Oct 5, 2026
50 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant