presence.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # Copyright 2014-2016 OpenMarket Ltd
  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. from typing import TYPE_CHECKING, Dict, List, Tuple
  15. from synapse.api.presence import PresenceState, UserPresenceState
  16. from synapse.replication.tcp.streams import PresenceStream
  17. from synapse.storage._base import SQLBaseStore, make_in_list_sql_clause
  18. from synapse.storage.database import DatabasePool
  19. from synapse.storage.engines import PostgresEngine
  20. from synapse.storage.types import Connection
  21. from synapse.storage.util.id_generators import MultiWriterIdGenerator, StreamIdGenerator
  22. from synapse.util.caches.descriptors import cached, cachedList
  23. from synapse.util.caches.stream_change_cache import StreamChangeCache
  24. from synapse.util.iterutils import batch_iter
  25. if TYPE_CHECKING:
  26. from synapse.server import HomeServer
  27. class PresenceStore(SQLBaseStore):
  28. def __init__(
  29. self,
  30. database: DatabasePool,
  31. db_conn: Connection,
  32. hs: "HomeServer",
  33. ):
  34. super().__init__(database, db_conn, hs)
  35. self._can_persist_presence = (
  36. hs.get_instance_name() in hs.config.worker.writers.presence
  37. )
  38. if isinstance(database.engine, PostgresEngine):
  39. self._presence_id_gen = MultiWriterIdGenerator(
  40. db_conn=db_conn,
  41. db=database,
  42. stream_name="presence_stream",
  43. instance_name=self._instance_name,
  44. tables=[("presence_stream", "instance_name", "stream_id")],
  45. sequence_name="presence_stream_sequence",
  46. writers=hs.config.worker.writers.to_device,
  47. )
  48. else:
  49. self._presence_id_gen = StreamIdGenerator(
  50. db_conn, "presence_stream", "stream_id"
  51. )
  52. self._presence_on_startup = self._get_active_presence(db_conn)
  53. presence_cache_prefill, min_presence_val = self.db_pool.get_cache_dict(
  54. db_conn,
  55. "presence_stream",
  56. entity_column="user_id",
  57. stream_column="stream_id",
  58. max_value=self._presence_id_gen.get_current_token(),
  59. )
  60. self.presence_stream_cache = StreamChangeCache(
  61. "PresenceStreamChangeCache",
  62. min_presence_val,
  63. prefilled_cache=presence_cache_prefill,
  64. )
  65. async def update_presence(self, presence_states):
  66. assert self._can_persist_presence
  67. stream_ordering_manager = self._presence_id_gen.get_next_mult(
  68. len(presence_states)
  69. )
  70. async with stream_ordering_manager as stream_orderings:
  71. await self.db_pool.runInteraction(
  72. "update_presence",
  73. self._update_presence_txn,
  74. stream_orderings,
  75. presence_states,
  76. )
  77. return stream_orderings[-1], self._presence_id_gen.get_current_token()
  78. def _update_presence_txn(self, txn, stream_orderings, presence_states):
  79. for stream_id, state in zip(stream_orderings, presence_states):
  80. txn.call_after(
  81. self.presence_stream_cache.entity_has_changed, state.user_id, stream_id
  82. )
  83. txn.call_after(self._get_presence_for_user.invalidate, (state.user_id,))
  84. # Actually insert new rows
  85. self.db_pool.simple_insert_many_txn(
  86. txn,
  87. table="presence_stream",
  88. values=[
  89. {
  90. "stream_id": stream_id,
  91. "user_id": state.user_id,
  92. "state": state.state,
  93. "last_active_ts": state.last_active_ts,
  94. "last_federation_update_ts": state.last_federation_update_ts,
  95. "last_user_sync_ts": state.last_user_sync_ts,
  96. "status_msg": state.status_msg,
  97. "currently_active": state.currently_active,
  98. "instance_name": self._instance_name,
  99. }
  100. for stream_id, state in zip(stream_orderings, presence_states)
  101. ],
  102. )
  103. # Delete old rows to stop database from getting really big
  104. sql = "DELETE FROM presence_stream WHERE stream_id < ? AND "
  105. for states in batch_iter(presence_states, 50):
  106. clause, args = make_in_list_sql_clause(
  107. self.database_engine, "user_id", [s.user_id for s in states]
  108. )
  109. txn.execute(sql + clause, [stream_id] + list(args))
  110. async def get_all_presence_updates(
  111. self, instance_name: str, last_id: int, current_id: int, limit: int
  112. ) -> Tuple[List[Tuple[int, list]], int, bool]:
  113. """Get updates for presence replication stream.
  114. Args:
  115. instance_name: The writer we want to fetch updates from. Unused
  116. here since there is only ever one writer.
  117. last_id: The token to fetch updates from. Exclusive.
  118. current_id: The token to fetch updates up to. Inclusive.
  119. limit: The requested limit for the number of rows to return. The
  120. function may return more or fewer rows.
  121. Returns:
  122. A tuple consisting of: the updates, a token to use to fetch
  123. subsequent updates, and whether we returned fewer rows than exists
  124. between the requested tokens due to the limit.
  125. The token returned can be used in a subsequent call to this
  126. function to get further updatees.
  127. The updates are a list of 2-tuples of stream ID and the row data
  128. """
  129. if last_id == current_id:
  130. return [], current_id, False
  131. def get_all_presence_updates_txn(txn):
  132. sql = """
  133. SELECT stream_id, user_id, state, last_active_ts,
  134. last_federation_update_ts, last_user_sync_ts,
  135. status_msg,
  136. currently_active
  137. FROM presence_stream
  138. WHERE ? < stream_id AND stream_id <= ?
  139. ORDER BY stream_id ASC
  140. LIMIT ?
  141. """
  142. txn.execute(sql, (last_id, current_id, limit))
  143. updates = [(row[0], row[1:]) for row in txn]
  144. upper_bound = current_id
  145. limited = False
  146. if len(updates) >= limit:
  147. upper_bound = updates[-1][0]
  148. limited = True
  149. return updates, upper_bound, limited
  150. return await self.db_pool.runInteraction(
  151. "get_all_presence_updates", get_all_presence_updates_txn
  152. )
  153. @cached()
  154. def _get_presence_for_user(self, user_id):
  155. raise NotImplementedError()
  156. @cachedList(
  157. cached_method_name="_get_presence_for_user",
  158. list_name="user_ids",
  159. num_args=1,
  160. )
  161. async def get_presence_for_users(self, user_ids):
  162. rows = await self.db_pool.simple_select_many_batch(
  163. table="presence_stream",
  164. column="user_id",
  165. iterable=user_ids,
  166. keyvalues={},
  167. retcols=(
  168. "user_id",
  169. "state",
  170. "last_active_ts",
  171. "last_federation_update_ts",
  172. "last_user_sync_ts",
  173. "status_msg",
  174. "currently_active",
  175. ),
  176. desc="get_presence_for_users",
  177. )
  178. for row in rows:
  179. row["currently_active"] = bool(row["currently_active"])
  180. return {row["user_id"]: UserPresenceState(**row) for row in rows}
  181. async def get_presence_for_all_users(
  182. self,
  183. include_offline: bool = True,
  184. ) -> Dict[str, UserPresenceState]:
  185. """Retrieve the current presence state for all users.
  186. Note that the presence_stream table is culled frequently, so it should only
  187. contain the latest presence state for each user.
  188. Args:
  189. include_offline: Whether to include offline presence states
  190. Returns:
  191. A dict of user IDs to their current UserPresenceState.
  192. """
  193. users_to_state = {}
  194. exclude_keyvalues = None
  195. if not include_offline:
  196. # Exclude offline presence state
  197. exclude_keyvalues = {"state": "offline"}
  198. # This may be a very heavy database query.
  199. # We paginate in order to not block a database connection.
  200. limit = 100
  201. offset = 0
  202. while True:
  203. rows = await self.db_pool.runInteraction(
  204. "get_presence_for_all_users",
  205. self.db_pool.simple_select_list_paginate_txn,
  206. "presence_stream",
  207. orderby="stream_id",
  208. start=offset,
  209. limit=limit,
  210. exclude_keyvalues=exclude_keyvalues,
  211. retcols=(
  212. "user_id",
  213. "state",
  214. "last_active_ts",
  215. "last_federation_update_ts",
  216. "last_user_sync_ts",
  217. "status_msg",
  218. "currently_active",
  219. ),
  220. order_direction="ASC",
  221. )
  222. for row in rows:
  223. users_to_state[row["user_id"]] = UserPresenceState(**row)
  224. # We've run out of updates to query
  225. if len(rows) < limit:
  226. break
  227. offset += limit
  228. return users_to_state
  229. def get_current_presence_token(self):
  230. return self._presence_id_gen.get_current_token()
  231. def _get_active_presence(self, db_conn: Connection):
  232. """Fetch non-offline presence from the database so that we can register
  233. the appropriate time outs.
  234. """
  235. sql = (
  236. "SELECT user_id, state, last_active_ts, last_federation_update_ts,"
  237. " last_user_sync_ts, status_msg, currently_active FROM presence_stream"
  238. " WHERE state != ?"
  239. )
  240. txn = db_conn.cursor()
  241. txn.execute(sql, (PresenceState.OFFLINE,))
  242. rows = self.db_pool.cursor_to_dict(txn)
  243. txn.close()
  244. for row in rows:
  245. row["currently_active"] = bool(row["currently_active"])
  246. return [UserPresenceState(**row) for row in rows]
  247. def take_presence_startup_info(self):
  248. active_on_startup = self._presence_on_startup
  249. self._presence_on_startup = None
  250. return active_on_startup
  251. def process_replication_rows(self, stream_name, instance_name, token, rows):
  252. if stream_name == PresenceStream.NAME:
  253. self._presence_id_gen.advance(instance_name, token)
  254. for row in rows:
  255. self.presence_stream_cache.entity_has_changed(row.user_id, token)
  256. self._get_presence_for_user.invalidate((row.user_id,))
  257. return super().process_replication_rows(stream_name, instance_name, token, rows)