Skip to content

Commit

Permalink
Cleanup deleted state group references (#18165)
Browse files Browse the repository at this point in the history
### Pull Request Checklist

<!-- Please read
https://element-hq.github.io/synapse/latest/development/contributing_guide.html
before submitting your pull request -->

* [x] Pull request is based on the develop branch
* [x] Pull request includes a [changelog
file](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#changelog).
The entry should:
- Be a short description of your change which makes sense to users.
"Fixed a bug that prevented receiving messages from other servers."
instead of "Moved X method from `EventStore` to `EventWorkerStore`.".
  - Use markdown where necessary, mostly for `code blocks`.
  - End with either a period (.) or an exclamation mark (!).
  - Start with a capital letter.
- Feel free to credit yourself, by adding a sentence "Contributed by
@github_username." or "Contributed by [Your Name]." to the end of the
entry.
* [x] [Code
style](https://element-hq.github.io/synapse/latest/code_style.html) is
correct
(run the
[linters](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#run-the-linters))
  • Loading branch information
devonh authored Feb 18, 2025
1 parent 2d4f289 commit ecad88f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/18165.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cleanup deleted state group references.
8 changes: 8 additions & 0 deletions synapse/storage/databases/state/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,10 +828,18 @@ def _purge_unreferenced_state_groups(
"DELETE FROM state_groups_state WHERE state_group = ?",
[(sg,) for sg in state_groups_to_delete],
)
txn.execute_batch(
"DELETE FROM state_group_edges WHERE state_group = ?",
[(sg,) for sg in state_groups_to_delete],
)
txn.execute_batch(
"DELETE FROM state_groups WHERE id = ?",
[(sg,) for sg in state_groups_to_delete],
)
txn.execute_batch(
"DELETE FROM state_groups_pending_deletion WHERE state_group = ?",
[(sg,) for sg in state_groups_to_delete],
)

return True

Expand Down
35 changes: 34 additions & 1 deletion tests/storage/test_purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def test_purge_unreferenced_state_group(self) -> None:
1 + self.state_deletion_store.DELAY_BEFORE_DELETION_MS / 1000
)

# We expect that the unreferenced state group has been deleted.
# We expect that the unreferenced state group has been deleted from all tables.
row = self.get_success(
self.state_store.db_pool.simple_select_one_onecol(
table="state_groups",
Expand All @@ -259,6 +259,39 @@ def test_purge_unreferenced_state_group(self) -> None:
)
self.assertIsNone(row)

row = self.get_success(
self.state_store.db_pool.simple_select_one_onecol(
table="state_groups_state",
keyvalues={"state_group": unreferenced_state_group},
retcol="state_group",
allow_none=True,
desc="test_purge_unreferenced_state_group",
)
)
self.assertIsNone(row)

row = self.get_success(
self.state_store.db_pool.simple_select_one_onecol(
table="state_group_edges",
keyvalues={"state_group": unreferenced_state_group},
retcol="state_group",
allow_none=True,
desc="test_purge_unreferenced_state_group",
)
)
self.assertIsNone(row)

row = self.get_success(
self.state_store.db_pool.simple_select_one_onecol(
table="state_groups_pending_deletion",
keyvalues={"state_group": unreferenced_state_group},
retcol="state_group",
allow_none=True,
desc="test_purge_unreferenced_state_group",
)
)
self.assertIsNone(row)

# We expect there to now only be one state group for the room, which is
# the state group of the last event (as the only outlier).
state_groups = self.get_success(
Expand Down

0 comments on commit ecad88f

Please sign in to comment.