events_worker.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import itertools
  16. import logging
  17. from collections import namedtuple
  18. from canonicaljson import json
  19. from twisted.internet import defer
  20. from synapse.api.errors import NotFoundError
  21. # these are only included to make the type annotations work
  22. from synapse.events import EventBase # noqa: F401
  23. from synapse.events import FrozenEvent
  24. from synapse.events.snapshot import EventContext # noqa: F401
  25. from synapse.events.utils import prune_event
  26. from synapse.metrics.background_process_metrics import run_as_background_process
  27. from synapse.util.logcontext import (
  28. LoggingContext,
  29. PreserveLoggingContext,
  30. make_deferred_yieldable,
  31. run_in_background,
  32. )
  33. from synapse.util.metrics import Measure
  34. from ._base import SQLBaseStore
  35. logger = logging.getLogger(__name__)
  36. # These values are used in the `enqueus_event` and `_do_fetch` methods to
  37. # control how we batch/bulk fetch events from the database.
  38. # The values are plucked out of thing air to make initial sync run faster
  39. # on jki.re
  40. # TODO: Make these configurable.
  41. EVENT_QUEUE_THREADS = 3 # Max number of threads that will fetch events
  42. EVENT_QUEUE_ITERATIONS = 3 # No. times we block waiting for requests for events
  43. EVENT_QUEUE_TIMEOUT_S = 0.1 # Timeout when waiting for requests for events
  44. _EventCacheEntry = namedtuple("_EventCacheEntry", ("event", "redacted_event"))
  45. class EventsWorkerStore(SQLBaseStore):
  46. def get_received_ts(self, event_id):
  47. """Get received_ts (when it was persisted) for the event.
  48. Raises an exception for unknown events.
  49. Args:
  50. event_id (str)
  51. Returns:
  52. Deferred[int|None]: Timestamp in milliseconds, or None for events
  53. that were persisted before received_ts was implemented.
  54. """
  55. return self._simple_select_one_onecol(
  56. table="events",
  57. keyvalues={
  58. "event_id": event_id,
  59. },
  60. retcol="received_ts",
  61. desc="get_received_ts",
  62. )
  63. @defer.inlineCallbacks
  64. def get_event(self, event_id, check_redacted=True,
  65. get_prev_content=False, allow_rejected=False,
  66. allow_none=False, check_room_id=None):
  67. """Get an event from the database by event_id.
  68. Args:
  69. event_id (str): The event_id of the event to fetch
  70. check_redacted (bool): If True, check if event has been redacted
  71. and redact it.
  72. get_prev_content (bool): If True and event is a state event,
  73. include the previous states content in the unsigned field.
  74. allow_rejected (bool): If True return rejected events.
  75. allow_none (bool): If True, return None if no event found, if
  76. False throw a NotFoundError
  77. check_room_id (str|None): if not None, check the room of the found event.
  78. If there is a mismatch, behave as per allow_none.
  79. Returns:
  80. Deferred : A FrozenEvent.
  81. """
  82. events = yield self._get_events(
  83. [event_id],
  84. check_redacted=check_redacted,
  85. get_prev_content=get_prev_content,
  86. allow_rejected=allow_rejected,
  87. )
  88. event = events[0] if events else None
  89. if event is not None and check_room_id is not None:
  90. if event.room_id != check_room_id:
  91. event = None
  92. if event is None and not allow_none:
  93. raise NotFoundError("Could not find event %s" % (event_id,))
  94. defer.returnValue(event)
  95. @defer.inlineCallbacks
  96. def get_events(self, event_ids, check_redacted=True,
  97. get_prev_content=False, allow_rejected=False):
  98. """Get events from the database
  99. Args:
  100. event_ids (list): The event_ids of the events to fetch
  101. check_redacted (bool): If True, check if event has been redacted
  102. and redact it.
  103. get_prev_content (bool): If True and event is a state event,
  104. include the previous states content in the unsigned field.
  105. allow_rejected (bool): If True return rejected events.
  106. Returns:
  107. Deferred : Dict from event_id to event.
  108. """
  109. events = yield self._get_events(
  110. event_ids,
  111. check_redacted=check_redacted,
  112. get_prev_content=get_prev_content,
  113. allow_rejected=allow_rejected,
  114. )
  115. defer.returnValue({e.event_id: e for e in events})
  116. @defer.inlineCallbacks
  117. def _get_events(self, event_ids, check_redacted=True,
  118. get_prev_content=False, allow_rejected=False):
  119. if not event_ids:
  120. defer.returnValue([])
  121. event_id_list = event_ids
  122. event_ids = set(event_ids)
  123. event_entry_map = self._get_events_from_cache(
  124. event_ids,
  125. allow_rejected=allow_rejected,
  126. )
  127. missing_events_ids = [e for e in event_ids if e not in event_entry_map]
  128. if missing_events_ids:
  129. log_ctx = LoggingContext.current_context()
  130. log_ctx.record_event_fetch(len(missing_events_ids))
  131. missing_events = yield self._enqueue_events(
  132. missing_events_ids,
  133. check_redacted=check_redacted,
  134. allow_rejected=allow_rejected,
  135. )
  136. event_entry_map.update(missing_events)
  137. events = []
  138. for event_id in event_id_list:
  139. entry = event_entry_map.get(event_id, None)
  140. if not entry:
  141. continue
  142. if allow_rejected or not entry.event.rejected_reason:
  143. if check_redacted and entry.redacted_event:
  144. event = entry.redacted_event
  145. else:
  146. event = entry.event
  147. events.append(event)
  148. if get_prev_content:
  149. if "replaces_state" in event.unsigned:
  150. prev = yield self.get_event(
  151. event.unsigned["replaces_state"],
  152. get_prev_content=False,
  153. allow_none=True,
  154. )
  155. if prev:
  156. event.unsigned = dict(event.unsigned)
  157. event.unsigned["prev_content"] = prev.content
  158. event.unsigned["prev_sender"] = prev.sender
  159. defer.returnValue(events)
  160. def _invalidate_get_event_cache(self, event_id):
  161. self._get_event_cache.invalidate((event_id,))
  162. def _get_events_from_cache(self, events, allow_rejected, update_metrics=True):
  163. """Fetch events from the caches
  164. Args:
  165. events (list(str)): list of event_ids to fetch
  166. allow_rejected (bool): Whether to teturn events that were rejected
  167. update_metrics (bool): Whether to update the cache hit ratio metrics
  168. Returns:
  169. dict of event_id -> _EventCacheEntry for each event_id in cache. If
  170. allow_rejected is `False` then there will still be an entry but it
  171. will be `None`
  172. """
  173. event_map = {}
  174. for event_id in events:
  175. ret = self._get_event_cache.get(
  176. (event_id,), None,
  177. update_metrics=update_metrics,
  178. )
  179. if not ret:
  180. continue
  181. if allow_rejected or not ret.event.rejected_reason:
  182. event_map[event_id] = ret
  183. else:
  184. event_map[event_id] = None
  185. return event_map
  186. def _do_fetch(self, conn):
  187. """Takes a database connection and waits for requests for events from
  188. the _event_fetch_list queue.
  189. """
  190. i = 0
  191. while True:
  192. with self._event_fetch_lock:
  193. event_list = self._event_fetch_list
  194. self._event_fetch_list = []
  195. if not event_list:
  196. single_threaded = self.database_engine.single_threaded
  197. if single_threaded or i > EVENT_QUEUE_ITERATIONS:
  198. self._event_fetch_ongoing -= 1
  199. return
  200. else:
  201. self._event_fetch_lock.wait(EVENT_QUEUE_TIMEOUT_S)
  202. i += 1
  203. continue
  204. i = 0
  205. self._fetch_event_list(conn, event_list)
  206. def _fetch_event_list(self, conn, event_list):
  207. """Handle a load of requests from the _event_fetch_list queue
  208. Args:
  209. conn (twisted.enterprise.adbapi.Connection): database connection
  210. event_list (list[Tuple[list[str], Deferred]]):
  211. The fetch requests. Each entry consists of a list of event
  212. ids to be fetched, and a deferred to be completed once the
  213. events have been fetched.
  214. """
  215. with Measure(self._clock, "_fetch_event_list"):
  216. try:
  217. event_id_lists = zip(*event_list)[0]
  218. event_ids = [
  219. item for sublist in event_id_lists for item in sublist
  220. ]
  221. rows = self._new_transaction(
  222. conn, "do_fetch", [], [],
  223. self._fetch_event_rows, event_ids,
  224. )
  225. row_dict = {
  226. r["event_id"]: r
  227. for r in rows
  228. }
  229. # We only want to resolve deferreds from the main thread
  230. def fire(lst, res):
  231. for ids, d in lst:
  232. if not d.called:
  233. try:
  234. with PreserveLoggingContext():
  235. d.callback([
  236. res[i]
  237. for i in ids
  238. if i in res
  239. ])
  240. except Exception:
  241. logger.exception("Failed to callback")
  242. with PreserveLoggingContext():
  243. self.hs.get_reactor().callFromThread(fire, event_list, row_dict)
  244. except Exception as e:
  245. logger.exception("do_fetch")
  246. # We only want to resolve deferreds from the main thread
  247. def fire(evs):
  248. for _, d in evs:
  249. if not d.called:
  250. with PreserveLoggingContext():
  251. d.errback(e)
  252. with PreserveLoggingContext():
  253. self.hs.get_reactor().callFromThread(fire, event_list)
  254. @defer.inlineCallbacks
  255. def _enqueue_events(self, events, check_redacted=True, allow_rejected=False):
  256. """Fetches events from the database using the _event_fetch_list. This
  257. allows batch and bulk fetching of events - it allows us to fetch events
  258. without having to create a new transaction for each request for events.
  259. """
  260. if not events:
  261. defer.returnValue({})
  262. events_d = defer.Deferred()
  263. with self._event_fetch_lock:
  264. self._event_fetch_list.append(
  265. (events, events_d)
  266. )
  267. self._event_fetch_lock.notify()
  268. if self._event_fetch_ongoing < EVENT_QUEUE_THREADS:
  269. self._event_fetch_ongoing += 1
  270. should_start = True
  271. else:
  272. should_start = False
  273. if should_start:
  274. run_as_background_process(
  275. "fetch_events",
  276. self.runWithConnection,
  277. self._do_fetch,
  278. )
  279. logger.debug("Loading %d events", len(events))
  280. with PreserveLoggingContext():
  281. rows = yield events_d
  282. logger.debug("Loaded %d events (%d rows)", len(events), len(rows))
  283. if not allow_rejected:
  284. rows[:] = [r for r in rows if not r["rejects"]]
  285. res = yield make_deferred_yieldable(defer.gatherResults(
  286. [
  287. run_in_background(
  288. self._get_event_from_row,
  289. row["internal_metadata"], row["json"], row["redacts"],
  290. rejected_reason=row["rejects"],
  291. )
  292. for row in rows
  293. ],
  294. consumeErrors=True
  295. ))
  296. defer.returnValue({
  297. e.event.event_id: e
  298. for e in res if e
  299. })
  300. def _fetch_event_rows(self, txn, events):
  301. rows = []
  302. N = 200
  303. for i in range(1 + len(events) // N):
  304. evs = events[i * N:(i + 1) * N]
  305. if not evs:
  306. break
  307. sql = (
  308. "SELECT "
  309. " e.event_id as event_id, "
  310. " e.internal_metadata,"
  311. " e.json,"
  312. " r.redacts as redacts,"
  313. " rej.event_id as rejects "
  314. " FROM event_json as e"
  315. " LEFT JOIN rejections as rej USING (event_id)"
  316. " LEFT JOIN redactions as r ON e.event_id = r.redacts"
  317. " WHERE e.event_id IN (%s)"
  318. ) % (",".join(["?"] * len(evs)),)
  319. txn.execute(sql, evs)
  320. rows.extend(self.cursor_to_dict(txn))
  321. return rows
  322. @defer.inlineCallbacks
  323. def _get_event_from_row(self, internal_metadata, js, redacted,
  324. rejected_reason=None):
  325. with Measure(self._clock, "_get_event_from_row"):
  326. d = json.loads(js)
  327. internal_metadata = json.loads(internal_metadata)
  328. if rejected_reason:
  329. rejected_reason = yield self._simple_select_one_onecol(
  330. table="rejections",
  331. keyvalues={"event_id": rejected_reason},
  332. retcol="reason",
  333. desc="_get_event_from_row_rejected_reason",
  334. )
  335. original_ev = FrozenEvent(
  336. d,
  337. internal_metadata_dict=internal_metadata,
  338. rejected_reason=rejected_reason,
  339. )
  340. redacted_event = None
  341. if redacted:
  342. redacted_event = prune_event(original_ev)
  343. redaction_id = yield self._simple_select_one_onecol(
  344. table="redactions",
  345. keyvalues={"redacts": redacted_event.event_id},
  346. retcol="event_id",
  347. desc="_get_event_from_row_redactions",
  348. )
  349. redacted_event.unsigned["redacted_by"] = redaction_id
  350. # Get the redaction event.
  351. because = yield self.get_event(
  352. redaction_id,
  353. check_redacted=False,
  354. allow_none=True,
  355. )
  356. if because:
  357. # It's fine to do add the event directly, since get_pdu_json
  358. # will serialise this field correctly
  359. redacted_event.unsigned["redacted_because"] = because
  360. cache_entry = _EventCacheEntry(
  361. event=original_ev,
  362. redacted_event=redacted_event,
  363. )
  364. self._get_event_cache.prefill((original_ev.event_id,), cache_entry)
  365. defer.returnValue(cache_entry)
  366. @defer.inlineCallbacks
  367. def have_events_in_timeline(self, event_ids):
  368. """Given a list of event ids, check if we have already processed and
  369. stored them as non outliers.
  370. """
  371. rows = yield self._simple_select_many_batch(
  372. table="events",
  373. retcols=("event_id",),
  374. column="event_id",
  375. iterable=list(event_ids),
  376. keyvalues={"outlier": False},
  377. desc="have_events_in_timeline",
  378. )
  379. defer.returnValue(set(r["event_id"] for r in rows))
  380. @defer.inlineCallbacks
  381. def have_seen_events(self, event_ids):
  382. """Given a list of event ids, check if we have already processed them.
  383. Args:
  384. event_ids (iterable[str]):
  385. Returns:
  386. Deferred[set[str]]: The events we have already seen.
  387. """
  388. results = set()
  389. def have_seen_events_txn(txn, chunk):
  390. sql = (
  391. "SELECT event_id FROM events as e WHERE e.event_id IN (%s)"
  392. % (",".join("?" * len(chunk)), )
  393. )
  394. txn.execute(sql, chunk)
  395. for (event_id, ) in txn:
  396. results.add(event_id)
  397. # break the input up into chunks of 100
  398. input_iterator = iter(event_ids)
  399. for chunk in iter(lambda: list(itertools.islice(input_iterator, 100)),
  400. []):
  401. yield self.runInteraction(
  402. "have_seen_events",
  403. have_seen_events_txn,
  404. chunk,
  405. )
  406. defer.returnValue(results)
  407. def get_seen_events_with_rejections(self, event_ids):
  408. """Given a list of event ids, check if we rejected them.
  409. Args:
  410. event_ids (list[str])
  411. Returns:
  412. Deferred[dict[str, str|None):
  413. Has an entry for each event id we already have seen. Maps to
  414. the rejected reason string if we rejected the event, else maps
  415. to None.
  416. """
  417. if not event_ids:
  418. return defer.succeed({})
  419. def f(txn):
  420. sql = (
  421. "SELECT e.event_id, reason FROM events as e "
  422. "LEFT JOIN rejections as r ON e.event_id = r.event_id "
  423. "WHERE e.event_id = ?"
  424. )
  425. res = {}
  426. for event_id in event_ids:
  427. txn.execute(sql, (event_id,))
  428. row = txn.fetchone()
  429. if row:
  430. _, rejected = row
  431. res[event_id] = rejected
  432. return res
  433. return self.runInteraction("get_rejection_reasons", f)