From c83db929391d2dc35e7517f151741a1faac8070d Mon Sep 17 00:00:00 2001 From: Nick Barrett Date: Sat, 16 Jul 2022 08:54:23 +0200 Subject: [PATCH] Simplify purge room by just re-running the same txn --- .../storage/databases/main/purge_events.py | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/synapse/storage/databases/main/purge_events.py b/synapse/storage/databases/main/purge_events.py index c0ffefcf74cd..704d41014d20 100644 --- a/synapse/storage/databases/main/purge_events.py +++ b/synapse/storage/databases/main/purge_events.py @@ -319,26 +319,22 @@ async def purge_room(self, room_id: str) -> List[int]: Returns: The list of state groups to delete. """ + + # This first runs the purge transaction with READ_COMMITTED isolation level, + # meaning any new rows in the tables will not trigger a serialization error. + # We then run the same purge a second time without this isolation level to + # purge any of those rows which were added during the first. + state_groups_to_delete = await self.db_pool.runInteraction( "purge_room", self._purge_room_txn, room_id=room_id, - # This is safe because we don't care if room data is updated during the transaction, note - # we run a second transaction to cleanup tables we may write to during this transaction. isolation_level=IsolationLevel.READ_COMMITTED, ) - def _purge_room_second_pass_txn(txn: LoggingTransaction, room_id: str) -> None: - for table in ( - "event_push_actions", - "stream_ordering_to_exterm", - ): - logger.info("[purge] removing %s from %s", room_id, table) - txn.execute("DELETE FROM %s WHERE room_id=?" % (table,), (room_id,)) - await self.db_pool.runInteraction( - "purge_room_second_pass", - _purge_room_second_pass_txn, + "purge_room", + self._purge_room_txn, room_id=room_id, )