Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Split purge API into events vs state and add PurgeEventsStorage #6295

Merged
merged 14 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 43 additions & 27 deletions synapse/storage/data_stores/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,10 @@ def purge_room(self, room_id):
"""Deletes all record of a room

Args:
room_id (str):
room_id (str)

Returns:
Deferred[List[int]]: The list of state groups to delete.
"""

return self.runInteraction("purge_room", self._purge_room_txn, room_id)
Expand Down Expand Up @@ -1714,10 +1717,24 @@ def _purge_room_txn(self, txn, room_id):
# index on them. In any case we should be clearing out 'stream' tables
# periodically anyway (#5888)

# Now we fetch all the state groups that should be deleted.
txn.execute(
"""
SELECT DISTINCT state_group FROM events
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

erm, haven't we just deleted all the relevant rows from events?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah. And the tests were broken.

INNER JOIN event_to_state_groups USING(event_id)
WHERE events.room_id = ?
""",
(room_id,),
)

state_groups = [row[0] for row in txn]

# TODO: we could probably usefully do a bunch of cache invalidation here

logger.info("[purge] done")

return state_groups

def purge_unreferenced_state_groups(
self, room_id: str, state_groups_to_delete: Set[int]
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
) -> defer.Deferred:
Expand Down Expand Up @@ -1825,54 +1842,53 @@ def get_previous_state_groups(self, state_groups):

return {row["state_group"]: row["prev_state_group"] for row in rows}

def purge_room_state(self, room_id):
def purge_room_state(self, room_id, state_groups_to_delete):
"""Deletes all record of a room from state tables

Args:
room_id (str):
state_groups_to_delete (list[int]): State groups to delete
"""

return self.runInteraction(
"purge_room_state", self._purge_room_state_txn, room_id
"purge_room_state",
self._purge_room_state_txn,
room_id,
state_groups_to_delete,
)

def _purge_room_state_txn(self, txn, room_id):
def _purge_room_state_txn(self, txn, room_id, state_groups_to_delete):
# first we have to delete the state groups states
logger.info("[purge] removing %s from state_groups_state", room_id)

txn.execute(
"""
DELETE FROM state_groups_state
WHERE state_group IN (
SELECT state_group FROM state_groups
WHERE room_id = ?
)
""",
(room_id,),
self._simple_delete_many_txn(
txn,
table="state_groups_state",
column="state_group",
iterable=state_groups_to_delete,
keyvalues={},
)

# ... and the state group edges
logger.info("[purge] removing %s from state_group_edges", room_id)

txn.execute(
"""
DELETE FROM state_group_edges
WHERE state_group IN (
SELECT state_group FROM state_groups
WHERE room_id = ?
)
""",
(room_id,),
self._simple_delete_many_txn(
txn,
table="state_group_edges",
column="state_group",
iterable=state_groups_to_delete,
keyvalues={},
)

# ... and the state groups
logger.info("[purge] removing %s from state_groups", room_id)

txn.execute(
"""
DELETE FROM state_groups WHERE room_id = ?
""",
(room_id,),
self._simple_delete_many_txn(
txn,
table="state_groups",
column="id",
iterable=state_groups_to_delete,
keyvalues={},
)

async def is_event_after(self, event_id1, event_id2):
Expand Down

This file was deleted.

7 changes: 0 additions & 7 deletions synapse/storage/data_stores/main/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,6 @@ class StateBackgroundUpdateStore(
STATE_GROUP_INDEX_UPDATE_NAME = "state_group_state_type_index"
CURRENT_STATE_INDEX_UPDATE_NAME = "current_state_members_idx"
EVENT_STATE_GROUP_INDEX_UPDATE_NAME = "event_to_state_groups_sg_index"
STATE_GROUPS_ROOM_INDEX_UPDATE_NAME = "state_groups_room_id_idx"

def __init__(self, db_conn, hs):
super(StateBackgroundUpdateStore, self).__init__(db_conn, hs)
Expand All @@ -1047,12 +1046,6 @@ def __init__(self, db_conn, hs):
table="event_to_state_groups",
columns=["state_group"],
)
self.register_background_index_update(
self.STATE_GROUPS_ROOM_INDEX_UPDATE_NAME,
index_name="state_groups_room_id_idx",
table="state_groups",
columns=["room_id"],
)

@defer.inlineCallbacks
def _background_deduplicate_state(self, progress, batch_size):
Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/purge_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def purge_room(self, room_id: str):
"""Deletes all record of a room
"""

yield self.stores.main.purge_room(room_id)
yield self.stores.main.purge_room_state(room_id)
state_groups_to_delete = yield self.stores.main.purge_room(room_id)
yield self.stores.main.purge_room_state(room_id, state_groups_to_delete)

@defer.inlineCallbacks
def purge_history(self, room_id, token, delete_local_events):
Expand Down