Skip to content

fix(mqtt): scope stored push messages to the topic instance - #14867

Merged
abnegate merged 3 commits into
mainfrom
fix/mqtt-topic-instance-ledger
Oct 23, 2026
Merged

abnegate merged 3 commits into
mainfrom
fix/mqtt-topic-instance-ledger

Conversation

@abnegate

Copy link
Copy Markdown
Member

What does this PR do?

Stored push messages now record the topic's internal id. Replay and publish-side deduplication use it, the replay index moves to (topicInternalId, sequence), and deleting a topic deletes its stored messages, so a topic recreated under the same id starts clean. Messages stored before this change have no internal id and are not replayed; the existing age-based prune removes them within the retention window. V25 migration adds the attribute and swaps the index.

Test Plan

New unit tests in tests/unit/Mqtt/ReplayCursorTest.php, push adapter and V25Test additions, and e2e testRecreatedTopicDoesNotReplayDeletedTopicMessages. MQTT e2e 39/39, Messaging e2e, full unit suite and migrate on all projects pass. Lint, PHPStan and Rector are clean.

Related PRs and Issues

  • None

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

Stored push messages now record the topic's internal id. Replay and publish-side deduplication use it, the replay index moves to (topicInternalId, sequence), and deleting a topic deletes its stored messages, so a topic recreated under the same id starts clean. Messages stored before this change have no internal id and are not replayed; the existing age-based prune removes them within the retention window. V25 migration adds the attribute and swaps the index.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review complete. 🟠 1 high

💬 Inline comments (1)

  • 🟠 Unique index (messageId, topic) contradicts new topicInternalId dedup key — common.php:389

The change binds offline-message dedup and replay to a per-topic-instance identifier so a recreated topic under the same id no longer inherits its predecessor's ledger state. A V25 migration drops the old (topic, sequence) index and backfills cached collection state, while the deletes worker gains pushLedger cleanup on topic deletion.

Files Change
app/config/collections/common.php, src/Appwrite/Migration/Version/V25.php Adds topicInternalId to the push-ledger schema and migrates existing deployments to it.
src/Appwrite/Messaging/Adapter/Push/Appwrite.php, src/Appwrite/Mqtt/Handler.php Ledger lookups, replay filtering, and expiry now filter on topicInternalId.
src/Appwrite/Platform/Workers/Deletes.php Deletes the topic's push-ledger rows (and subscriber rows) during topic cleanup.
tests/unit/Mqtt/*, tests/unit/Appwrite/Messaging/Adapter/Push/*, tests/unit/Migration/Version/V25Test.php, tests/e2e/Services/Mqtt/* New unit and e2e coverage for cursor replay, dedup, and the migration.

Reviewed commit: 4c07219

@ghost

ghost commented Oct 23, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

This PR binds MQTT push-ledger rows and replay cursors to a topic’s internal instance ID, preventing messages from a deleted topic from crossing into a recreated topic with the same public ID. It also removes a topic’s ledger rows on deletion and adds a V25 migration plus adapter, replay, migration, and MQTT end-to-end coverage.

Latest changes: The newest commits also scope the ledger’s unique index to the topic instance, swap the index during V25 migration, and cover reused message IDs and pre-upgrade ledger rows.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 10
File Change
app/config/collections/common.php Adds topic-instance identity to the push ledger and scopes replay and deduplication indexes to it.
src/Appwrite/Messaging/Adapter/Push/Appwrite.php Stores and looks up ledger rows using the topic’s internal ID.
src/Appwrite/Migration/Version/V25.php Adds the topic-instance attribute and migrates the push-ledger indexes for existing projects.
src/Appwrite/Mqtt/Handler.php Loads replay backlogs by topic internal ID.
src/Appwrite/Platform/Workers/Deletes.php Deletes a topic’s push-ledger rows with its subscribers.
tests/e2e/Services/Mqtt/MqttServerTest.php Covers that a recreated topic does not replay messages from its predecessor.
tests/unit/Appwrite/Messaging/Adapter/Push/AppwriteTest.php Covers topic-instance persistence, message-ID reuse after recreation, and legacy rows.
tests/unit/Migration/Version/V25Test.php Covers the V25 attribute and index migration with pre-upgrade ledger data.
tests/unit/Mqtt/ReplayCursorTest.php Covers replay isolation for recreated topics and stale cursors.
tests/unit/Mqtt/ReplayTestCase.php Updates replay fixtures to include topic-instance identity.
🔇 Filtered out · 1

Findings Hansi considered but did not post.

Finding Why
MongoDB legacy rows can block the new unique index Verifier: The pinned utopia-php/database 8.3.14 Mongo adapter already handles this. I inspected Adapter/Mongo.php at composer.lock's source commit 86e31fb84e535c69e58bcbc19037bf1fb472fccb: createIndex() adds a partialFilterExpression for unique indexes, and getPartialFilterExpression() requires every indexed field to exist and have a non-null BSON type. Legacy rows missing topicInternalId are therefore excluded from this compound unique index and cannot collide on (messageId, null). The claimed index-creation failure is not reachable from those legacy rows.

Merged main (d63021b); no changes to this PR's code since ed930d0 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@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 A · See the inline comments. Summary

Comment thread app/config/collections/common.php Resolved

@luxor-code-review luxor-code-review Bot 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.

This PR re-keys MQTT push-ledger deduplication, replay, and cleanup from the stable topic id to a per-topic-instance topicInternalId, with a V25 migration and new unit/e2e tests.

Key findings

  • 🟠 Unique index (messageId, topic) contradicts new topicInternalId dedup key — common.php:389

Comment thread app/config/collections/common.php Resolved
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/mqtt-topic-instance-ledger (after).

Metric Before After Change
🚀 Requests/sec 209.86 216.78 ⚪ +3.3%
⏱️ Latency P50 80.26 ms 80.25 ms ⚪ 0%
⏱️ Latency P95 191.83 ms 188.08 ms ⚪ -2%
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 80.25 188.08 13,680 216.78 -3.75
Account 151.61 291.89 720 12.2 +14.58
TablesDB 77.7 144.56 7,440 120.35 -8.51
Storage 74.04 177.3 3,600 59.8 -10.65
Functions 115.93 222.9 1,920 32.65 +12.24

Top API waits (after)

API request Max wait (ms)
account.name.update 496.68
account.prefs.update 472.91
functions.variables.update 386.19
functions.create 344.72
tablesdb.rows.update 333.57

Deduplication looks a message up by (messageId, topicInternalId), but the unique index still covered (messageId, topic). A topic recreated under the same id, or a row written before the instance binding, made the lookup miss while the insert collided, so the publish failed. The index now covers (messageId, topicInternalId); NULLs are distinct under it, so pre-upgrade rows never block a publish. V25 drops the old index and creates the new one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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

@abnegate
abnegate merged commit 9723727 into main Oct 23, 2026
67 checks passed
@abnegate
abnegate deleted the fix/mqtt-topic-instance-ledger branch October 23, 2026 04:04
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