summary refs log tree commit diff
path: root/synapse/storage/controllers/purge_events.py
diff options
context:
space:
mode:
authorErik Johnston <erikj@element.io>2025-02-03 20:04:19 +0100
committerGitHub <noreply@github.com>2025-02-03 19:04:19 +0000
commitc46d452c7cdf7289ae7e8677b44a88c747761dce (patch)
treef6e4e5926992e80d30afa59af0d757e0cf7da9c9 /synapse/storage/controllers/purge_events.py
parentAdd locking to more safely delete state groups: Part 2 (#18130) (diff)
downloadsynapse-c46d452c7cdf7289ae7e8677b44a88c747761dce.tar.xz
Fix bug where purging history could lead to increase in disk space usage (#18131)
When purging history, we try and delete any state groups that become
unreferenced (i.e. there are no longer any events that directly
reference them). When we delete a state group that is referenced by
another state group, we "de-delta" that state group so that it no longer
refers to the state group that is deleted.

There are two bugs with this approach that we fix here:
1. There is a common pattern where we end up storing two state groups
when persisting a state event: the state before and after the new state
event, where the latter is stored as a delta to the former. When
deleting state groups we only deleted the "new" state and left (and
potentially de-deltaed) the old state. This was due to a bug/typo when
trying to find referenced state groups.
2. There are times where we store unreferenced state groups in the DB,
during the purging of history these would not get rechecked and instead
always de-deltaed. Instead, we should check for this case and delete any
unreferenced state groups rather than de-deltaing them.

The effect of the above bugs is that when purging history we'd end up
with lots of unreferenced state groups that had been de-deltaed (i.e.
stored as the full state). This can lead to dramatic increases in
storage space used.
Diffstat (limited to 'synapse/storage/controllers/purge_events.py')
-rw-r--r--synapse/storage/controllers/purge_events.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/synapse/storage/controllers/purge_events.py b/synapse/storage/controllers/purge_events.py

index 2d6f80f770..47cec8c469 100644 --- a/synapse/storage/controllers/purge_events.py +++ b/synapse/storage/controllers/purge_events.py
@@ -128,6 +128,16 @@ class PurgeEventsStorageController: next_to_search |= prevs state_groups_seen |= prevs + # We also check to see if anything referencing the state groups are + # also unreferenced. This helps ensure that we delete unreferenced + # state groups, if we don't then we will de-delta them when we + # delete the other state groups leading to increased DB usage. + next_edges = await self.stores.state.get_next_state_groups(current_search) + nexts = set(next_edges.keys()) + nexts -= state_groups_seen + next_to_search |= nexts + state_groups_seen |= nexts + to_delete = state_groups_seen - referenced_groups return to_delete