Skip to content

Commit 922788c

Browse files
authored
Purge chain cover tables when purging events. (matrix-org#9498)
1 parent d790d0d commit 922788c

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

changelog.d/9498.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Properly purge the event chain cover index when purging history.

synapse/storage/databases/main/purge_events.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class PurgeEventsStore(StateGroupWorkerStore, SQLBaseStore):
2828
async def purge_history(
2929
self, room_id: str, token: str, delete_local_events: bool
3030
) -> Set[int]:
31-
"""Deletes room history before a certain point
31+
"""Deletes room history before a certain point.
32+
33+
Note that only a single purge can occur at once, this is guaranteed via
34+
a higher level (in the PaginationHandler).
3235
3336
Args:
3437
room_id:
@@ -52,7 +55,9 @@ async def purge_history(
5255
delete_local_events,
5356
)
5457

55-
def _purge_history_txn(self, txn, room_id, token, delete_local_events):
58+
def _purge_history_txn(
59+
self, txn, room_id: str, token: RoomStreamToken, delete_local_events: bool
60+
) -> Set[int]:
5661
# Tables that should be pruned:
5762
# event_auth
5863
# event_backward_extremities
@@ -103,7 +108,7 @@ def _purge_history_txn(self, txn, room_id, token, delete_local_events):
103108
if max_depth < token.topological:
104109
# We need to ensure we don't delete all the events from the database
105110
# otherwise we wouldn't be able to send any events (due to not
106-
# having any backwards extremeties)
111+
# having any backwards extremities)
107112
raise SynapseError(
108113
400, "topological_ordering is greater than forward extremeties"
109114
)
@@ -154,7 +159,7 @@ def _purge_history_txn(self, txn, room_id, token, delete_local_events):
154159

155160
logger.info("[purge] Finding new backward extremities")
156161

157-
# We calculate the new entries for the backward extremeties by finding
162+
# We calculate the new entries for the backward extremities by finding
158163
# events to be purged that are pointed to by events we're not going to
159164
# purge.
160165
txn.execute(
@@ -296,7 +301,7 @@ async def purge_room(self, room_id: str) -> List[int]:
296301
"purge_room", self._purge_room_txn, room_id
297302
)
298303

299-
def _purge_room_txn(self, txn, room_id):
304+
def _purge_room_txn(self, txn, room_id: str) -> List[int]:
300305
# First we fetch all the state groups that should be deleted, before
301306
# we delete that information.
302307
txn.execute(
@@ -310,6 +315,31 @@ def _purge_room_txn(self, txn, room_id):
310315

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

318+
# Get all the auth chains that are referenced by events that are to be
319+
# deleted.
320+
txn.execute(
321+
"""
322+
SELECT chain_id, sequence_number FROM events
323+
LEFT JOIN event_auth_chains USING (event_id)
324+
WHERE room_id = ?
325+
""",
326+
(room_id,),
327+
)
328+
referenced_chain_id_tuples = list(txn)
329+
330+
logger.info("[purge] removing events from event_auth_chain_links")
331+
txn.executemany(
332+
"""
333+
DELETE FROM event_auth_chain_links WHERE
334+
(origin_chain_id = ? AND origin_sequence_number = ?) OR
335+
(target_chain_id = ? AND target_sequence_number = ?)
336+
""",
337+
(
338+
(chain_id, seq_num, chain_id, seq_num)
339+
for (chain_id, seq_num) in referenced_chain_id_tuples
340+
),
341+
)
342+
313343
# Now we delete tables which lack an index on room_id but have one on event_id
314344
for table in (
315345
"event_auth",
@@ -319,6 +349,8 @@ def _purge_room_txn(self, txn, room_id):
319349
"event_reference_hashes",
320350
"event_relations",
321351
"event_to_state_groups",
352+
"event_auth_chains",
353+
"event_auth_chain_to_calculate",
322354
"redactions",
323355
"rejections",
324356
"state_events",

synapse/storage/purge_events.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ async def _find_unreferenced_groups(self, state_groups: Set[int]) -> Set[int]:
7373
Returns:
7474
The set of state groups that can be deleted.
7575
"""
76-
# Graph of state group -> previous group
77-
graph = {}
78-
7976
# Set of events that we have found to be referenced by events
8077
referenced_groups = set()
8178

@@ -111,8 +108,6 @@ async def _find_unreferenced_groups(self, state_groups: Set[int]) -> Set[int]:
111108
next_to_search |= prevs
112109
state_groups_seen |= prevs
113110

114-
graph.update(edges)
115-
116111
to_delete = state_groups_seen - referenced_groups
117112

118113
return to_delete

0 commit comments

Comments
 (0)