pusherpool.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2015, 2016 OpenMarket Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import logging
  17. from typing import TYPE_CHECKING, Dict, Iterable, Optional
  18. from prometheus_client import Gauge
  19. from synapse.metrics.background_process_metrics import (
  20. run_as_background_process,
  21. wrap_as_background_process,
  22. )
  23. from synapse.push import Pusher, PusherConfig, PusherConfigException
  24. from synapse.push.pusher import PusherFactory
  25. from synapse.types import JsonDict, RoomStreamToken
  26. from synapse.util.async_helpers import concurrently_execute
  27. if TYPE_CHECKING:
  28. from synapse.server import HomeServer
  29. logger = logging.getLogger(__name__)
  30. synapse_pushers = Gauge(
  31. "synapse_pushers", "Number of active synapse pushers", ["kind", "app_id"]
  32. )
  33. class PusherPool:
  34. """
  35. The pusher pool. This is responsible for dispatching notifications of new events to
  36. the http and email pushers.
  37. It provides three methods which are designed to be called by the rest of the
  38. application: `start`, `on_new_notifications`, and `on_new_receipts`: each of these
  39. delegates to each of the relevant pushers.
  40. Note that it is expected that each pusher will have its own 'processing' loop which
  41. will send out the notifications in the background, rather than blocking until the
  42. notifications are sent; accordingly Pusher.on_started, Pusher.on_new_notifications and
  43. Pusher.on_new_receipts are not expected to return awaitables.
  44. """
  45. def __init__(self, hs: "HomeServer"):
  46. self.hs = hs
  47. self.pusher_factory = PusherFactory(hs)
  48. self._should_start_pushers = hs.config.start_pushers
  49. self.store = self.hs.get_datastore()
  50. self.clock = self.hs.get_clock()
  51. self._account_validity = hs.config.account_validity
  52. # We shard the handling of push notifications by user ID.
  53. self._pusher_shard_config = hs.config.push.pusher_shard_config
  54. self._instance_name = hs.get_instance_name()
  55. # Record the last stream ID that we were poked about so we can get
  56. # changes since then. We set this to the current max stream ID on
  57. # startup as every individual pusher will have checked for changes on
  58. # startup.
  59. self._last_room_stream_id_seen = self.store.get_room_max_stream_ordering()
  60. # map from user id to app_id:pushkey to pusher
  61. self.pushers = {} # type: Dict[str, Dict[str, Pusher]]
  62. def start(self) -> None:
  63. """Starts the pushers off in a background process.
  64. """
  65. if not self._should_start_pushers:
  66. logger.info("Not starting pushers because they are disabled in the config")
  67. return
  68. run_as_background_process("start_pushers", self._start_pushers)
  69. async def add_pusher(
  70. self,
  71. user_id: str,
  72. access_token: Optional[int],
  73. kind: str,
  74. app_id: str,
  75. app_display_name: str,
  76. device_display_name: str,
  77. pushkey: str,
  78. lang: Optional[str],
  79. data: JsonDict,
  80. profile_tag: str = "",
  81. ) -> Optional[Pusher]:
  82. """Creates a new pusher and adds it to the pool
  83. Returns:
  84. The newly created pusher.
  85. """
  86. time_now_msec = self.clock.time_msec()
  87. # we try to create the pusher just to validate the config: it
  88. # will then get pulled out of the database,
  89. # recreated, added and started: this means we have only one
  90. # code path adding pushers.
  91. self.pusher_factory.create_pusher(
  92. PusherConfig(
  93. id=None,
  94. user_name=user_id,
  95. access_token=access_token,
  96. profile_tag=profile_tag,
  97. kind=kind,
  98. app_id=app_id,
  99. app_display_name=app_display_name,
  100. device_display_name=device_display_name,
  101. pushkey=pushkey,
  102. ts=time_now_msec,
  103. lang=lang,
  104. data=data,
  105. last_stream_ordering=None,
  106. last_success=None,
  107. failing_since=None,
  108. )
  109. )
  110. # create the pusher setting last_stream_ordering to the current maximum
  111. # stream ordering, so it will process pushes from this point onwards.
  112. last_stream_ordering = self.store.get_room_max_stream_ordering()
  113. await self.store.add_pusher(
  114. user_id=user_id,
  115. access_token=access_token,
  116. kind=kind,
  117. app_id=app_id,
  118. app_display_name=app_display_name,
  119. device_display_name=device_display_name,
  120. pushkey=pushkey,
  121. pushkey_ts=time_now_msec,
  122. lang=lang,
  123. data=data,
  124. last_stream_ordering=last_stream_ordering,
  125. profile_tag=profile_tag,
  126. )
  127. pusher = await self.start_pusher_by_id(app_id, pushkey, user_id)
  128. return pusher
  129. async def remove_pushers_by_app_id_and_pushkey_not_user(
  130. self, app_id: str, pushkey: str, not_user_id: str
  131. ) -> None:
  132. to_remove = await self.store.get_pushers_by_app_id_and_pushkey(app_id, pushkey)
  133. for p in to_remove:
  134. if p.user_name != not_user_id:
  135. logger.info(
  136. "Removing pusher for app id %s, pushkey %s, user %s",
  137. app_id,
  138. pushkey,
  139. p.user_name,
  140. )
  141. await self.remove_pusher(p.app_id, p.pushkey, p.user_name)
  142. async def remove_pushers_by_access_token(
  143. self, user_id: str, access_tokens: Iterable[int]
  144. ) -> None:
  145. """Remove the pushers for a given user corresponding to a set of
  146. access_tokens.
  147. Args:
  148. user_id: user to remove pushers for
  149. access_tokens: access token *ids* to remove pushers for
  150. """
  151. if not self._pusher_shard_config.should_handle(self._instance_name, user_id):
  152. return
  153. tokens = set(access_tokens)
  154. for p in await self.store.get_pushers_by_user_id(user_id):
  155. if p.access_token in tokens:
  156. logger.info(
  157. "Removing pusher for app id %s, pushkey %s, user %s",
  158. p.app_id,
  159. p.pushkey,
  160. p.user_name,
  161. )
  162. await self.remove_pusher(p.app_id, p.pushkey, p.user_name)
  163. def on_new_notifications(self, max_token: RoomStreamToken) -> None:
  164. if not self.pushers:
  165. # nothing to do here.
  166. return
  167. # We just use the minimum stream ordering and ignore the vector clock
  168. # component. This is safe to do as long as we *always* ignore the vector
  169. # clock components.
  170. max_stream_id = max_token.stream
  171. if max_stream_id < self._last_room_stream_id_seen:
  172. # Nothing to do
  173. return
  174. # We only start a new background process if necessary rather than
  175. # optimistically (to cut down on overhead).
  176. self._on_new_notifications(max_token)
  177. @wrap_as_background_process("on_new_notifications")
  178. async def _on_new_notifications(self, max_token: RoomStreamToken) -> None:
  179. # We just use the minimum stream ordering and ignore the vector clock
  180. # component. This is safe to do as long as we *always* ignore the vector
  181. # clock components.
  182. max_stream_id = max_token.stream
  183. prev_stream_id = self._last_room_stream_id_seen
  184. self._last_room_stream_id_seen = max_stream_id
  185. try:
  186. users_affected = await self.store.get_push_action_users_in_range(
  187. prev_stream_id, max_stream_id
  188. )
  189. for u in users_affected:
  190. # Don't push if the user account has expired
  191. if self._account_validity.enabled:
  192. expired = await self.store.is_account_expired(
  193. u, self.clock.time_msec()
  194. )
  195. if expired:
  196. continue
  197. if u in self.pushers:
  198. for p in self.pushers[u].values():
  199. p.on_new_notifications(max_token)
  200. except Exception:
  201. logger.exception("Exception in pusher on_new_notifications")
  202. async def on_new_receipts(
  203. self, min_stream_id: int, max_stream_id: int, affected_room_ids: Iterable[str]
  204. ) -> None:
  205. if not self.pushers:
  206. # nothing to do here.
  207. return
  208. try:
  209. # Need to subtract 1 from the minimum because the lower bound here
  210. # is not inclusive
  211. users_affected = await self.store.get_users_sent_receipts_between(
  212. min_stream_id - 1, max_stream_id
  213. )
  214. for u in users_affected:
  215. # Don't push if the user account has expired
  216. if self._account_validity.enabled:
  217. expired = await self.store.is_account_expired(
  218. u, self.clock.time_msec()
  219. )
  220. if expired:
  221. continue
  222. if u in self.pushers:
  223. for p in self.pushers[u].values():
  224. p.on_new_receipts(min_stream_id, max_stream_id)
  225. except Exception:
  226. logger.exception("Exception in pusher on_new_receipts")
  227. async def start_pusher_by_id(
  228. self, app_id: str, pushkey: str, user_id: str
  229. ) -> Optional[Pusher]:
  230. """Look up the details for the given pusher, and start it
  231. Returns:
  232. The pusher started, if any
  233. """
  234. if not self._should_start_pushers:
  235. return None
  236. if not self._pusher_shard_config.should_handle(self._instance_name, user_id):
  237. return None
  238. resultlist = await self.store.get_pushers_by_app_id_and_pushkey(app_id, pushkey)
  239. pusher_config = None
  240. for r in resultlist:
  241. if r.user_name == user_id:
  242. pusher_config = r
  243. pusher = None
  244. if pusher_config:
  245. pusher = await self._start_pusher(pusher_config)
  246. return pusher
  247. async def _start_pushers(self) -> None:
  248. """Start all the pushers
  249. """
  250. pushers = await self.store.get_all_pushers()
  251. # Stagger starting up the pushers so we don't completely drown the
  252. # process on start up.
  253. await concurrently_execute(self._start_pusher, pushers, 10)
  254. logger.info("Started pushers")
  255. async def _start_pusher(self, pusher_config: PusherConfig) -> Optional[Pusher]:
  256. """Start the given pusher
  257. Args:
  258. pusher_config: The pusher configuration with the values pulled from the db table
  259. Returns:
  260. The newly created pusher or None.
  261. """
  262. if not self._pusher_shard_config.should_handle(
  263. self._instance_name, pusher_config.user_name
  264. ):
  265. return None
  266. try:
  267. p = self.pusher_factory.create_pusher(pusher_config)
  268. except PusherConfigException as e:
  269. logger.warning(
  270. "Pusher incorrectly configured id=%i, user=%s, appid=%s, pushkey=%s: %s",
  271. pusher_config.id,
  272. pusher_config.user_name,
  273. pusher_config.app_id,
  274. pusher_config.pushkey,
  275. e,
  276. )
  277. return None
  278. except Exception:
  279. logger.exception(
  280. "Couldn't start pusher id %i: caught Exception", pusher_config.id,
  281. )
  282. return None
  283. if not p:
  284. return None
  285. appid_pushkey = "%s:%s" % (pusher_config.app_id, pusher_config.pushkey)
  286. byuser = self.pushers.setdefault(pusher_config.user_name, {})
  287. if appid_pushkey in byuser:
  288. byuser[appid_pushkey].on_stop()
  289. byuser[appid_pushkey] = p
  290. synapse_pushers.labels(type(p).__name__, p.app_id).inc()
  291. # Check if there *may* be push to process. We do this as this check is a
  292. # lot cheaper to do than actually fetching the exact rows we need to
  293. # push.
  294. user_id = pusher_config.user_name
  295. last_stream_ordering = pusher_config.last_stream_ordering
  296. if last_stream_ordering:
  297. have_notifs = await self.store.get_if_maybe_push_in_range_for_user(
  298. user_id, last_stream_ordering
  299. )
  300. else:
  301. # We always want to default to starting up the pusher rather than
  302. # risk missing push.
  303. have_notifs = True
  304. p.on_started(have_notifs)
  305. return p
  306. async def remove_pusher(self, app_id: str, pushkey: str, user_id: str) -> None:
  307. appid_pushkey = "%s:%s" % (app_id, pushkey)
  308. byuser = self.pushers.get(user_id, {})
  309. if appid_pushkey in byuser:
  310. logger.info("Stopping pusher %s / %s", user_id, appid_pushkey)
  311. pusher = byuser.pop(appid_pushkey)
  312. pusher.on_stop()
  313. synapse_pushers.labels(type(pusher).__name__, pusher.app_id).dec()
  314. await self.store.delete_pusher_by_app_id_pushkey_user_id(
  315. app_id, pushkey, user_id
  316. )