-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix message duplication if something goes wrong after persisting the event #8476
Changes from 7 commits
29d4b88
9b68b63
76d1d94
0869a4f
3613071
1a49e23
d22241e
801dda2
136b97e
33909dd
cd89f44
7a1c417
8f5931d
22eb206
3daf421
9503e17
46d4919
807d44c
58a70d2
6419d09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1314,14 +1314,19 @@ def get_next_event_to_expire_txn(txn): | |
| ) | ||
|
|
||
| async def get_event_id_from_transaction_id( | ||
| self, user_id: str, token_id: str, txn_id: str | ||
| self, room_id: str, user_id: str, token_id: int, txn_id: str | ||
| ) -> Optional[str]: | ||
| """Look up if we have already persisted an event for the transaction ID, | ||
| returning the event ID if so. | ||
| """ | ||
| return await self.db_pool.simple_select_one_onecol( | ||
| table="event_txn_id", | ||
| keyvalues={"user_id": user_id, "token_id": token_id, "txn_id": txn_id}, | ||
| keyvalues={ | ||
| "room_id": room_id, | ||
| "user_id": user_id, | ||
| "token_id": token_id, | ||
| "txn_id": txn_id, | ||
| }, | ||
| retcol="event_id", | ||
| allow_none=True, | ||
| desc="get_event_id_from_transaction_id", | ||
|
|
@@ -1342,7 +1347,7 @@ async def get_already_persisted_events( | |
| txn_id = getattr(event.internal_metadata, "txn_id", None) | ||
| if token_id and txn_id: | ||
|
Comment on lines
+1341
to
+1353
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slightly feeling that it shouldn't be the storage layer's responsibility to do this digging, but ymmv
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeeeah, though this only gets called from
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| existing = await self.get_event_id_from_transaction_id( | ||
| event.sender, token_id, txn_id | ||
| event.room_id, event.sender, token_id, txn_id | ||
| ) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if we want to try and batch these up a bit? The standard
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like something similar could be made which takes an iterable of tuples (or dicts)?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, I'm just not sure how much I want to try and do that for something that is going in an RC
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. park it for now, optimise later? |
||
| if existing: | ||
| mapping[event.event_id] = existing | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,16 +17,24 @@ | |
| -- A map of recent events persisted with transaction IDs. Used to deduplicate | ||
| -- send event requests with the same transaction ID. | ||
|
Comment on lines
+17
to
+18
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nice to mention what these are expected to be deduplicated across (user, device, transaction). I think most of the other tables we have use the |
||
| -- | ||
| -- Note, transaction IDs are scoped to the user ID/access token that was used to | ||
| -- make the request. | ||
| CREATE TABLE event_txn_id ( | ||
| -- Note: transaction IDs are scoped to the room ID/user ID/access token that was | ||
| -- used to make the request. | ||
| -- | ||
| -- Note: The foreign key constraints are ON DELETE CASCADE, as if we delete the | ||
| -- events or access token we don't want to try and de-duplicate the event. | ||
| CREATE TABLE IF NOT EXISTS event_txn_id ( | ||
| event_id TEXT NOT NULL, | ||
erikjohnston marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| room_id TEXT NOT NULL, | ||
| user_id TEXT NOT NULL, | ||
| token_id BIGINT NOT NULL, | ||
richvdh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| txn_id TEXT NOT NULL, | ||
| inserted_ts BIGINT NOT NULL | ||
| inserted_ts BIGINT NOT NULL, | ||
| FOREIGN KEY (event_id) | ||
| REFERENCES events (event_id) ON DELETE CASCADE, | ||
| FOREIGN KEY (token_id) | ||
| REFERENCES access_tokens (id) ON DELETE CASCADE | ||
| ); | ||
|
|
||
| CREATE UNIQUE INDEX event_txn_id_event_id ON event_txn_id(event_id); | ||
| CREATE UNIQUE INDEX event_txn_id_txn_id ON event_txn_id(user_id, token_id, txn_id); | ||
| CREATE INDEX event_txn_id_ts ON event_txn_id(inserted_ts); | ||
| CREATE UNIQUE INDEX IF NOT EXISTS event_txn_id_event_id ON event_txn_id(event_id); | ||
| CREATE UNIQUE INDEX IF NOT EXISTS event_txn_id_txn_id ON event_txn_id(room_id, user_id, token_id, txn_id); | ||
| CREATE INDEX IF NOT EXISTS event_txn_id_ts ON event_txn_id(inserted_ts); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,7 +96,9 @@ def add_to_queue(self, room_id, events_and_contexts, backfilled): | |
|
|
||
| Returns: | ||
| defer.Deferred: a deferred which will resolve once the events are | ||
| persisted. Runs its callbacks *without* a logcontext. | ||
| persisted. Runs its callbacks *without* a logcontext. The result | ||
| is the same as that returned by the callback passed to | ||
| `handle_queue`. | ||
| """ | ||
| queue = self._event_persist_queues.setdefault(room_id, deque()) | ||
| if queue: | ||
|
|
@@ -229,7 +231,7 @@ async def persist_events( | |
| for room_id in partitioned: | ||
| self._maybe_start_persisting(room_id) | ||
|
|
||
| # The deferred returns a map from event ID to existing event ID if the | ||
| # Each deferred returns a map from event ID to existing event ID if the | ||
| # event was deduplicated. (The dict may also include other entries if | ||
| # the event was persisted in a batch with other events). | ||
| # | ||
|
|
@@ -324,7 +326,8 @@ async def _persist_events( | |
| # | ||
| # We should have checked this a long time before we get here, but it's | ||
| # possible that different send event requests race in such a way that | ||
| # they both pass the earlier checks. | ||
| # they both pass the earlier checks. Checking here isn't racey as we can | ||
| # have only one `_persist_events` per room being called at a time. | ||
| replaced_events = await self.main_store.get_already_persisted_events( | ||
| (event for event, _ in events_and_contexts) | ||
| ) | ||
erikjohnston marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+331
to
+333
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I might have paged this out, but can we be sure that
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, hmm, I don't think it should happen because we linearize based on txn ID elsewhere, but we should add a check anyway |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.