Skip to content

fix(databases): purge cached collection before announcing schema changes - #14104

Merged
abnegate merged 1 commit into
mainfrom
fix-attribute-available-cache-race
Oct 5, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix-attribute-available-cache-race

Conversation

@abnegate

@abnegate abnegate commented Oct 5, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Problem. The databases worker marked an attribute available, fired the realtime event, and only then purged the cached collection document in finally. The collection document embeds its attributes and indexes. So while the event was being dispatched, GET .../attributes/:key already returned available (it reads the attribute document, which updateDocument purges), but index create still validated against the cached collection, where the column was still processing. A client that created an index as soon as the attribute was available could get 400 column_not_available. This showed up as a flake in E2E / MariaDB (shared) / TablesDB testPatchAttribute.

Fix. In every worker path (create/delete attribute, including relationship attributes and their related collection, and create/delete index), purge the collection caches first and fire the event afterwards. No other work then runs between the metadata write and the purge. The purge stays in finally, so the failure paths (failed / stuck) still clear the cache. Attribute deletion now fires its event after the dependent index metadata is adjusted and purged, instead of before.

Test Plan

The new e2e testCreateIndexOnAttributeAsSoonAsAvailable is in DatabasesBase, so it runs on Legacy, TablesDB and VectorsDB for every adapter. It creates five attributes and indexes each one the moment its attribute reports available. It polls every 5 ms instead of the default 500 ms, so every run exercises the gap.

A deterministic fail-first e2e is not possible here. The race is between two writes inside the worker, and an e2e can only make it likely, not force it (worker tests are out of scope per AGENTS.md). With the fix, the gap shrinks to the time between the metadata commit and the next cache call. Before the fix, it included the realtime dispatch. A truly zero gap would need index create to re-check the attribute document when the cached collection disagrees. That is left out to keep this change to the worker ordering.

Related PRs and Issues

Seen as a flake on #11649 (run 37267066799, attempt 1).

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

The databases worker marked an attribute or index available (or
failed, or deleted) and then fired the realtime event before purging
the cached collection document in its finally block. The collection
document embeds its attributes and indexes, so for the duration of
the event dispatch the API still served the old schema from cache
while the attribute document itself already read as available. A
client that polled the attribute and created an index on it as soon
as it was available could hit that gap and get a 400 "not yet
available" (seen as a flake in TablesDB testPatchAttribute).

Purge the collection caches first and announce afterwards in every
worker path (create/delete attribute, create/delete index), so no
work sits between the metadata write and the purge. The purge stays
in finally so the failure paths still clear the cache. Attribute
deletion now announces after the dependent index metadata is
adjusted, for the same reason.

The new e2e creates several attributes and indexes each one the
moment its attribute reports available, polling tightly so the
window is exercised on every run.

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 changed ordering and polling test are consistent with the existing worker and E2E contracts, with no concrete defects found.

The worker now purges collection caches before dispatching attribute and index realtime events, including related collections. Attribute deletion dispatch is also moved after dependent index metadata updates. A shared E2E test creates indexes immediately after attributes become available and waits for those indexes to complete.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 2
File Change
src/Appwrite/Platform/Modules/Databases/Workers/Databases.php Moves realtime dispatch after cache purges and dependent index updates.
tests/e2e/Services/Databases/DatabasesBase.php Adds rapid attribute polling followed by immediate index creation coverage.

Reviewed c61c2df · 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-attribute-available-cache-race (after).

Metric Before After Change
🚀 Requests/sec 200.65 199.28 ⚪ -0.7%
⏱️ Latency P50 85.65 ms 87.04 ms ⚪ +1.6%
⏱️ Latency P95 205.28 ms 206.14 ms ⚪ +0.4%
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 87.04 206.14 12,540 199.28 +0.87
Account 168.95 324.83 660 11.01 -17.16
TablesDB 83.75 153.66 6,820 109.98 -2.03
Storage 79.99 172.33 3,300 55.16 +2.69
Functions 124.94 255.95 1,760 30.04 -6.31

Top API waits (after)

API request Max wait (ms)
storage.buckets.create 487.28
account.name.update 481.42
functions.variables.update 413.66
functions.variables.create 377.74
tablesdb.rows.get 376.05

@abnegate
abnegate merged commit 2c62316 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