You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

502 lines
18 KiB

  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from typing import Any, List, Set, Tuple, cast
  16. from synapse.api.errors import SynapseError
  17. from synapse.storage.database import LoggingTransaction
  18. from synapse.storage.databases.main import CacheInvalidationWorkerStore
  19. from synapse.storage.databases.main.state import StateGroupWorkerStore
  20. from synapse.storage.engines import PostgresEngine
  21. from synapse.storage.engines._base import IsolationLevel
  22. from synapse.types import RoomStreamToken
  23. logger = logging.getLogger(__name__)
  24. class PurgeEventsStore(StateGroupWorkerStore, CacheInvalidationWorkerStore):
  25. async def purge_history(
  26. self, room_id: str, token: str, delete_local_events: bool
  27. ) -> Set[int]:
  28. """Deletes room history before a certain point.
  29. Note that only a single purge can occur at once, this is guaranteed via
  30. a higher level (in the PaginationHandler).
  31. Args:
  32. room_id:
  33. token: A topological token to delete events before
  34. delete_local_events:
  35. if True, we will delete local events as well as remote ones
  36. (instead of just marking them as outliers and deleting their
  37. state groups).
  38. Returns:
  39. The set of state groups that are referenced by deleted events.
  40. """
  41. parsed_token = await RoomStreamToken.parse(self, token)
  42. return await self.db_pool.runInteraction(
  43. "purge_history",
  44. self._purge_history_txn,
  45. room_id,
  46. parsed_token,
  47. delete_local_events,
  48. )
  49. def _purge_history_txn(
  50. self,
  51. txn: LoggingTransaction,
  52. room_id: str,
  53. token: RoomStreamToken,
  54. delete_local_events: bool,
  55. ) -> Set[int]:
  56. # Tables that should be pruned:
  57. # event_auth
  58. # event_backward_extremities
  59. # event_edges
  60. # event_forward_extremities
  61. # event_json
  62. # event_push_actions
  63. # event_relations
  64. # event_search
  65. # event_to_state_groups
  66. # events
  67. # rejections
  68. # room_depth
  69. # state_groups
  70. # state_groups_state
  71. # destination_rooms
  72. # we will build a temporary table listing the events so that we don't
  73. # have to keep shovelling the list back and forth across the
  74. # connection. Annoyingly the python sqlite driver commits the
  75. # transaction on CREATE, so let's do this first.
  76. #
  77. # furthermore, we might already have the table from a previous (failed)
  78. # purge attempt, so let's drop the table first.
  79. if isinstance(self.database_engine, PostgresEngine):
  80. # Disable statement timeouts for this transaction; purging rooms can
  81. # take a while!
  82. txn.execute("SET LOCAL statement_timeout = 0")
  83. txn.execute("DROP TABLE IF EXISTS events_to_purge")
  84. txn.execute(
  85. "CREATE TEMPORARY TABLE events_to_purge ("
  86. " event_id TEXT NOT NULL,"
  87. " should_delete BOOLEAN NOT NULL"
  88. ")"
  89. )
  90. # First ensure that we're not about to delete all the forward extremeties
  91. txn.execute(
  92. "SELECT e.event_id, e.depth FROM events as e "
  93. "INNER JOIN event_forward_extremities as f "
  94. "ON e.event_id = f.event_id "
  95. "AND e.room_id = f.room_id "
  96. "WHERE f.room_id = ?",
  97. (room_id,),
  98. )
  99. rows = txn.fetchall()
  100. # if we already have no forwards extremities (for example because they were
  101. # cleared out by the `delete_old_current_state_events` background database
  102. # update), then we may as well carry on.
  103. if rows:
  104. max_depth = max(row[1] for row in rows)
  105. if max_depth < token.topological:
  106. # We need to ensure we don't delete all the events from the database
  107. # otherwise we wouldn't be able to send any events (due to not
  108. # having any backwards extremities)
  109. raise SynapseError(
  110. 400, "topological_ordering is greater than forward extremities"
  111. )
  112. logger.info("[purge] looking for events to delete")
  113. should_delete_expr = "state_events.state_key IS NULL"
  114. should_delete_params: Tuple[Any, ...] = ()
  115. if not delete_local_events:
  116. should_delete_expr += " AND event_id NOT LIKE ?"
  117. # We include the parameter twice since we use the expression twice
  118. should_delete_params += ("%:" + self.hs.hostname, "%:" + self.hs.hostname)
  119. should_delete_params += (room_id, token.topological)
  120. # Note that we insert events that are outliers and aren't going to be
  121. # deleted, as nothing will happen to them.
  122. txn.execute(
  123. "INSERT INTO events_to_purge"
  124. " SELECT event_id, %s"
  125. " FROM events AS e LEFT JOIN state_events USING (event_id)"
  126. " WHERE (NOT outlier OR (%s)) AND e.room_id = ? AND topological_ordering < ?"
  127. % (should_delete_expr, should_delete_expr),
  128. should_delete_params,
  129. )
  130. # We create the indices *after* insertion as that's a lot faster.
  131. # create an index on should_delete because later we'll be looking for
  132. # the should_delete / shouldn't_delete subsets
  133. txn.execute(
  134. "CREATE INDEX events_to_purge_should_delete"
  135. " ON events_to_purge(should_delete)"
  136. )
  137. # We do joins against events_to_purge for e.g. calculating state
  138. # groups to purge, etc., so lets make an index.
  139. txn.execute("CREATE INDEX events_to_purge_id ON events_to_purge(event_id)")
  140. txn.execute("SELECT event_id, should_delete FROM events_to_purge")
  141. event_rows = txn.fetchall()
  142. logger.info(
  143. "[purge] found %i events before cutoff, of which %i can be deleted",
  144. len(event_rows),
  145. sum(1 for e in event_rows if e[1]),
  146. )
  147. logger.info("[purge] Finding new backward extremities")
  148. # We calculate the new entries for the backward extremities by finding
  149. # events to be purged that are pointed to by events we're not going to
  150. # purge.
  151. txn.execute(
  152. "SELECT DISTINCT e.event_id FROM events_to_purge AS e"
  153. " INNER JOIN event_edges AS ed ON e.event_id = ed.prev_event_id"
  154. " LEFT JOIN events_to_purge AS ep2 ON ed.event_id = ep2.event_id"
  155. " WHERE ep2.event_id IS NULL"
  156. )
  157. new_backwards_extrems = txn.fetchall()
  158. logger.info("[purge] replacing backward extremities: %r", new_backwards_extrems)
  159. txn.execute(
  160. "DELETE FROM event_backward_extremities WHERE room_id = ?", (room_id,)
  161. )
  162. # Update backward extremeties
  163. txn.execute_batch(
  164. "INSERT INTO event_backward_extremities (room_id, event_id)"
  165. " VALUES (?, ?)",
  166. [(room_id, event_id) for event_id, in new_backwards_extrems],
  167. )
  168. logger.info("[purge] finding state groups referenced by deleted events")
  169. # Get all state groups that are referenced by events that are to be
  170. # deleted.
  171. txn.execute(
  172. """
  173. SELECT DISTINCT state_group FROM events_to_purge
  174. INNER JOIN event_to_state_groups USING (event_id)
  175. """
  176. )
  177. referenced_state_groups = {sg for sg, in txn}
  178. logger.info(
  179. "[purge] found %i referenced state groups", len(referenced_state_groups)
  180. )
  181. logger.info("[purge] removing events from event_to_state_groups")
  182. txn.execute(
  183. "DELETE FROM event_to_state_groups "
  184. "WHERE event_id IN (SELECT event_id from events_to_purge)"
  185. )
  186. # Delete all remote non-state events
  187. for table in (
  188. "event_edges",
  189. "events",
  190. "event_json",
  191. "event_auth",
  192. "event_forward_extremities",
  193. "event_relations",
  194. "event_search",
  195. "rejections",
  196. "redactions",
  197. ):
  198. logger.info("[purge] removing events from %s", table)
  199. txn.execute(
  200. "DELETE FROM %s WHERE event_id IN ("
  201. " SELECT event_id FROM events_to_purge WHERE should_delete"
  202. ")" % (table,)
  203. )
  204. # event_push_actions lacks an index on event_id, and has one on
  205. # (room_id, event_id) instead.
  206. for table in ("event_push_actions",):
  207. logger.info("[purge] removing events from %s", table)
  208. txn.execute(
  209. "DELETE FROM %s WHERE room_id = ? AND event_id IN ("
  210. " SELECT event_id FROM events_to_purge WHERE should_delete"
  211. ")" % (table,),
  212. (room_id,),
  213. )
  214. # Mark all state and own events as outliers
  215. logger.info("[purge] marking remaining events as outliers")
  216. txn.execute(
  217. "UPDATE events SET outlier = TRUE"
  218. " WHERE event_id IN ("
  219. " SELECT event_id FROM events_to_purge "
  220. " WHERE NOT should_delete"
  221. ")"
  222. )
  223. # synapse tries to take out an exclusive lock on room_depth whenever it
  224. # persists events (because upsert), and once we run this update, we
  225. # will block that for the rest of our transaction.
  226. #
  227. # So, let's stick it at the end so that we don't block event
  228. # persistence.
  229. #
  230. # We do this by calculating the minimum depth of the backwards
  231. # extremities. However, the events in event_backward_extremities
  232. # are ones we don't have yet so we need to look at the events that
  233. # point to it via event_edges table.
  234. txn.execute(
  235. """
  236. SELECT COALESCE(MIN(depth), 0)
  237. FROM event_backward_extremities AS eb
  238. INNER JOIN event_edges AS eg ON eg.prev_event_id = eb.event_id
  239. INNER JOIN events AS e ON e.event_id = eg.event_id
  240. WHERE eb.room_id = ?
  241. """,
  242. (room_id,),
  243. )
  244. (min_depth,) = cast(Tuple[int], txn.fetchone())
  245. logger.info("[purge] updating room_depth to %d", min_depth)
  246. txn.execute(
  247. "UPDATE room_depth SET min_depth = ? WHERE room_id = ?",
  248. (min_depth, room_id),
  249. )
  250. # finally, drop the temp table. this will commit the txn in sqlite,
  251. # so make sure to keep this actually last.
  252. txn.execute("DROP TABLE events_to_purge")
  253. self._invalidate_cache_and_stream_bulk(
  254. txn,
  255. self._get_state_group_for_event,
  256. [(event_id,) for event_id, _ in event_rows],
  257. )
  258. # XXX: This is racy, since have_seen_events could be called between the
  259. # transaction completing and the invalidation running. On the other hand,
  260. # that's no different to calling `have_seen_events` just before the
  261. # event is deleted from the database.
  262. self._invalidate_cache_and_stream_bulk(
  263. txn,
  264. self.have_seen_event,
  265. [
  266. (room_id, event_id)
  267. for event_id, should_delete in event_rows
  268. if should_delete
  269. ],
  270. )
  271. for event_id, should_delete in event_rows:
  272. if should_delete:
  273. self.invalidate_get_event_cache_after_txn(txn, event_id)
  274. logger.info("[purge] done")
  275. self._invalidate_caches_for_room_events_and_stream(txn, room_id)
  276. return referenced_state_groups
  277. async def purge_room(self, room_id: str) -> List[int]:
  278. """Deletes all record of a room
  279. Args:
  280. room_id
  281. Returns:
  282. The list of state groups to delete.
  283. """
  284. # This first runs the purge transaction with READ_COMMITTED isolation level,
  285. # meaning any new rows in the tables will not trigger a serialization error.
  286. # We then run the same purge a second time without this isolation level to
  287. # purge any of those rows which were added during the first.
  288. logger.info("[purge] Starting initial main purge of [1/2]")
  289. state_groups_to_delete = await self.db_pool.runInteraction(
  290. "purge_room",
  291. self._purge_room_txn,
  292. room_id=room_id,
  293. isolation_level=IsolationLevel.READ_COMMITTED,
  294. )
  295. logger.info("[purge] Starting secondary main purge of [2/2]")
  296. state_groups_to_delete.extend(
  297. await self.db_pool.runInteraction(
  298. "purge_room",
  299. self._purge_room_txn,
  300. room_id=room_id,
  301. ),
  302. )
  303. logger.info("[purge] Done with main purge")
  304. return state_groups_to_delete
  305. def _purge_room_txn(self, txn: LoggingTransaction, room_id: str) -> List[int]:
  306. # This collides with event persistence so we cannot write new events and metadata into
  307. # a room while deleting it or this transaction will fail.
  308. if isinstance(self.database_engine, PostgresEngine):
  309. txn.execute(
  310. "SELECT room_version FROM rooms WHERE room_id = ? FOR UPDATE",
  311. (room_id,),
  312. )
  313. # First, fetch all the state groups that should be deleted, before
  314. # we delete that information.
  315. txn.execute(
  316. """
  317. SELECT DISTINCT state_group FROM events
  318. INNER JOIN event_to_state_groups USING(event_id)
  319. WHERE events.room_id = ?
  320. """,
  321. (room_id,),
  322. )
  323. state_groups = [row[0] for row in txn]
  324. # Get all the auth chains that are referenced by events that are to be
  325. # deleted.
  326. txn.execute(
  327. """
  328. SELECT chain_id, sequence_number FROM events
  329. LEFT JOIN event_auth_chains USING (event_id)
  330. WHERE room_id = ?
  331. """,
  332. (room_id,),
  333. )
  334. referenced_chain_id_tuples = list(txn)
  335. logger.info("[purge] removing from event_auth_chain_links")
  336. txn.executemany(
  337. """
  338. DELETE FROM event_auth_chain_links WHERE
  339. origin_chain_id = ? AND origin_sequence_number = ?
  340. """,
  341. referenced_chain_id_tuples,
  342. )
  343. # Now we delete tables which lack an index on room_id but have one on event_id
  344. for table in (
  345. "event_auth",
  346. "event_edges",
  347. "event_json",
  348. "event_push_actions_staging",
  349. "event_relations",
  350. "event_to_state_groups",
  351. "event_auth_chains",
  352. "event_auth_chain_to_calculate",
  353. "redactions",
  354. "rejections",
  355. "state_events",
  356. ):
  357. logger.info("[purge] removing from %s", table)
  358. txn.execute(
  359. """
  360. DELETE FROM %s WHERE event_id IN (
  361. SELECT event_id FROM events WHERE room_id=?
  362. )
  363. """
  364. % (table,),
  365. (room_id,),
  366. )
  367. # next, the tables with an index on room_id (or no useful index)
  368. for table in (
  369. "current_state_events",
  370. "destination_rooms",
  371. "event_backward_extremities",
  372. "event_forward_extremities",
  373. "event_push_actions",
  374. "event_search",
  375. "event_failed_pull_attempts",
  376. # Note: the partial state tables have foreign keys between each other, and to
  377. # `events` and `rooms`. We need to delete from them in the right order.
  378. "partial_state_events",
  379. "partial_state_rooms_servers",
  380. "partial_state_rooms",
  381. # Note: the _membership(s) tables have foreign keys to the `events` table
  382. # so must be deleted first.
  383. "local_current_membership",
  384. "room_memberships",
  385. "events",
  386. "federation_inbound_events_staging",
  387. "receipts_graph",
  388. "receipts_linearized",
  389. "room_aliases",
  390. "room_depth",
  391. "room_stats_state",
  392. "room_stats_current",
  393. "room_stats_earliest_token",
  394. "stream_ordering_to_exterm",
  395. "users_in_public_rooms",
  396. "users_who_share_private_rooms",
  397. # no useful index, but let's clear them anyway
  398. "appservice_room_list",
  399. "e2e_room_keys",
  400. "event_push_summary",
  401. "pusher_throttle",
  402. "room_account_data",
  403. "room_tags",
  404. # "rooms" happens last, to keep the foreign keys in the other tables
  405. # happy
  406. "rooms",
  407. ):
  408. logger.info("[purge] removing from %s", table)
  409. txn.execute("DELETE FROM %s WHERE room_id=?" % (table,), (room_id,))
  410. # Other tables we do NOT need to clear out:
  411. #
  412. # - blocked_rooms
  413. # This is important, to make sure that we don't accidentally rejoin a blocked
  414. # room after it was purged
  415. #
  416. # - user_directory
  417. # This has a room_id column, but it is unused
  418. #
  419. # Other tables that we might want to consider clearing out include:
  420. #
  421. # - event_reports
  422. # Given that these are intended for abuse management my initial
  423. # inclination is to leave them in place.
  424. #
  425. # - current_state_delta_stream
  426. # - ex_outlier_stream
  427. # - room_tags_revisions
  428. # The problem with these is that they are largeish and there is no room_id
  429. # index on them. In any case we should be clearing out 'stream' tables
  430. # periodically anyway (https://github.com/matrix-org/synapse/issues/5888)
  431. self._invalidate_caches_for_room_and_stream(txn, room_id)
  432. return state_groups