Skip to content

Commit

Permalink
move purge_room_state to state store
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMaul committed Dec 18, 2024
1 parent af9334d commit 049fb31
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions synapse/storage/controllers/purge_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async def purge_room(self, room_id: str) -> None:

with nested_logging_context(room_id):
await self.stores.main.purge_room(room_id)
await self.stores.state.purge_room_state(room_id)

async def purge_history(
self, room_id: str, token: str, delete_local_events: bool
Expand Down
37 changes: 0 additions & 37 deletions synapse/storage/databases/main/purge_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,6 @@ def _purge_room_txn(self, txn: LoggingTransaction, room_id: str) -> None:
logger.info("[purge] removing from %s", table)
txn.execute("DELETE FROM %s WHERE room_id=?" % (table,), (room_id,))

self._purge_room_state_txn(txn, room_id)

# Other tables we do NOT need to clear out:
#
# - blocked_rooms
Expand All @@ -494,38 +492,3 @@ def _purge_room_txn(self, txn: LoggingTransaction, room_id: str) -> None:
# periodically anyway (https://github.com/matrix-org/synapse/issues/5888)

self._invalidate_caches_for_room_and_stream(txn, room_id)

def _purge_room_state_txn(
self,
txn: LoggingTransaction,
room_id: str,
) -> None:
# Delete all edges that reference a state group linked to room_id
logger.info("[purge] removing %s from state_group_edges", room_id)
txn.execute(
"""
DELETE FROM state_group_edges AS sge WHERE sge.state_group IN (
SELECT id FROM state_groups AS sg WHERE sg.room_id = ?
)""",
(room_id,),
)

# state_groups_state table has a room_id column but no index on it, unlike state_groups,
# so we delete them by matching the room_id through the state_groups table.
logger.info("[purge] removing %s from state_groups_state", room_id)
txn.execute(
"""
DELETE FROM state_groups_state AS sgs WHERE sgs.state_group IN (
SELECT id FROM state_groups AS sg WHERE sg.room_id = ?
)""",
(room_id,),
)

logger.info("[purge] removing %s from state_groups", room_id)
self.db_pool.simple_delete_many_txn(
txn,
table="state_groups",
column="room_id",
values=[room_id],
keyvalues={},
)
42 changes: 42 additions & 0 deletions synapse/storage/databases/state/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,3 +839,45 @@ async def get_previous_state_groups(
)

return dict(rows)

async def purge_room_state(self, room_id: str) -> None:
return await self.db_pool.runInteraction(
"purge_room_state",
self._purge_room_state_txn,
room_id,
)

def _purge_room_state_txn(
self,
txn: LoggingTransaction,
room_id: str,
) -> None:
# Delete all edges that reference a state group linked to room_id
logger.info("[purge] removing %s from state_group_edges", room_id)
txn.execute(
"""
DELETE FROM state_group_edges AS sge WHERE sge.state_group IN (
SELECT id FROM state_groups AS sg WHERE sg.room_id = ?
)""",
(room_id,),
)

# state_groups_state table has a room_id column but no index on it, unlike state_groups,
# so we delete them by matching the room_id through the state_groups table.
logger.info("[purge] removing %s from state_groups_state", room_id)
txn.execute(
"""
DELETE FROM state_groups_state AS sgs WHERE sgs.state_group IN (
SELECT id FROM state_groups AS sg WHERE sg.room_id = ?
)""",
(room_id,),
)

logger.info("[purge] removing %s from state_groups", room_id)
self.db_pool.simple_delete_many_txn(
txn,
table="state_groups",
column="room_id",
values=[room_id],
keyvalues={},
)

0 comments on commit 049fb31

Please sign in to comment.