2019-10-30 15:12:49 +00:00
|
|
|
#
|
2023-11-21 20:29:58 +00:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2019-10-30 15:12:49 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import itertools
|
|
|
|
import logging
|
2020-12-30 13:09:53 +00:00
|
|
|
from typing import TYPE_CHECKING, Set
|
|
|
|
|
2023-03-08 20:08:56 +00:00
|
|
|
from synapse.logging.context import nested_logging_context
|
2020-12-30 13:09:53 +00:00
|
|
|
from synapse.storage.databases import Databases
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-03-23 11:12:48 +00:00
|
|
|
from synapse.server import HomeServer
|
2019-10-30 15:12:49 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-05-31 12:17:50 +00:00
|
|
|
class PurgeEventsStorageController:
|
2019-10-30 15:12:49 +00:00
|
|
|
"""High level interface for purging rooms and event history."""
|
|
|
|
|
2020-12-30 13:09:53 +00:00
|
|
|
def __init__(self, hs: "HomeServer", stores: Databases):
|
2019-10-30 15:12:49 +00:00
|
|
|
self.stores = stores
|
|
|
|
|
2020-12-30 13:09:53 +00:00
|
|
|
async def purge_room(self, room_id: str) -> None:
|
2019-10-30 15:12:49 +00:00
|
|
|
"""Deletes all record of a room"""
|
|
|
|
|
2023-03-08 20:08:56 +00:00
|
|
|
with nested_logging_context(room_id):
|
|
|
|
state_groups_to_delete = await self.stores.main.purge_room(room_id)
|
|
|
|
await self.stores.state.purge_room_state(room_id, state_groups_to_delete)
|
2019-10-30 15:12:49 +00:00
|
|
|
|
2020-07-28 20:09:53 +00:00
|
|
|
async def purge_history(
|
|
|
|
self, room_id: str, token: str, delete_local_events: bool
|
|
|
|
) -> None:
|
2019-10-30 15:12:49 +00:00
|
|
|
"""Deletes room history before a certain point
|
|
|
|
|
|
|
|
Args:
|
2020-07-28 20:09:53 +00:00
|
|
|
room_id: The room ID
|
2019-10-30 15:12:49 +00:00
|
|
|
|
2020-07-28 20:09:53 +00:00
|
|
|
token: A topological token to delete events before
|
2019-10-30 15:12:49 +00:00
|
|
|
|
2020-07-28 20:09:53 +00:00
|
|
|
delete_local_events:
|
2019-10-30 15:12:49 +00:00
|
|
|
if True, we will delete local events as well as remote ones
|
|
|
|
(instead of just marking them as outliers and deleting their
|
|
|
|
state groups).
|
|
|
|
"""
|
2023-03-08 20:08:56 +00:00
|
|
|
with nested_logging_context(room_id):
|
|
|
|
state_groups = await self.stores.main.purge_history(
|
|
|
|
room_id, token, delete_local_events
|
|
|
|
)
|
2019-10-30 15:12:49 +00:00
|
|
|
|
2023-03-08 20:08:56 +00:00
|
|
|
logger.info("[purge] finding state groups that can be deleted")
|
|
|
|
sg_to_delete = await self._find_unreferenced_groups(state_groups)
|
2019-10-30 15:12:49 +00:00
|
|
|
|
2023-03-08 20:08:56 +00:00
|
|
|
await self.stores.state.purge_unreferenced_state_groups(
|
|
|
|
room_id, sg_to_delete
|
|
|
|
)
|
2019-10-30 15:12:49 +00:00
|
|
|
|
2020-07-28 20:09:53 +00:00
|
|
|
async def _find_unreferenced_groups(self, state_groups: Set[int]) -> Set[int]:
|
2019-10-30 15:12:49 +00:00
|
|
|
"""Used when purging history to figure out which state groups can be
|
|
|
|
deleted.
|
|
|
|
|
|
|
|
Args:
|
2020-07-28 20:09:53 +00:00
|
|
|
state_groups: Set of state groups referenced by events
|
2019-10-30 15:12:49 +00:00
|
|
|
that are going to be deleted.
|
|
|
|
|
|
|
|
Returns:
|
2020-07-28 20:09:53 +00:00
|
|
|
The set of state groups that can be deleted.
|
2019-10-30 15:12:49 +00:00
|
|
|
"""
|
|
|
|
# Set of events that we have found to be referenced by events
|
|
|
|
referenced_groups = set()
|
|
|
|
|
|
|
|
# Set of state groups we've already seen
|
|
|
|
state_groups_seen = set(state_groups)
|
|
|
|
|
|
|
|
# Set of state groups to handle next.
|
|
|
|
next_to_search = set(state_groups)
|
|
|
|
while next_to_search:
|
|
|
|
# We bound size of groups we're looking up at once, to stop the
|
|
|
|
# SQL query getting too big
|
|
|
|
if len(next_to_search) < 100:
|
|
|
|
current_search = next_to_search
|
|
|
|
next_to_search = set()
|
|
|
|
else:
|
|
|
|
current_search = set(itertools.islice(next_to_search, 100))
|
|
|
|
next_to_search -= current_search
|
|
|
|
|
2020-07-28 20:09:53 +00:00
|
|
|
referenced = await self.stores.main.get_referenced_state_groups(
|
2019-10-30 15:12:49 +00:00
|
|
|
current_search
|
|
|
|
)
|
|
|
|
referenced_groups |= referenced
|
|
|
|
|
|
|
|
# We don't continue iterating up the state group graphs for state
|
|
|
|
# groups that are referenced.
|
|
|
|
current_search -= referenced
|
|
|
|
|
2020-07-28 20:09:53 +00:00
|
|
|
edges = await self.stores.state.get_previous_state_groups(current_search)
|
2019-10-30 15:12:49 +00:00
|
|
|
|
|
|
|
prevs = set(edges.values())
|
|
|
|
# We don't bother re-handling groups we've already seen
|
|
|
|
prevs -= state_groups_seen
|
|
|
|
next_to_search |= prevs
|
|
|
|
state_groups_seen |= prevs
|
|
|
|
|
|
|
|
to_delete = state_groups_seen - referenced_groups
|
|
|
|
|
|
|
|
return to_delete
|