deviceinbox.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket 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 logging
  16. from canonicaljson import json
  17. from twisted.internet import defer
  18. from .background_updates import BackgroundUpdateStore
  19. from synapse.util.caches.expiringcache import ExpiringCache
  20. logger = logging.getLogger(__name__)
  21. class DeviceInboxStore(BackgroundUpdateStore):
  22. DEVICE_INBOX_STREAM_ID = "device_inbox_stream_drop"
  23. def __init__(self, db_conn, hs):
  24. super(DeviceInboxStore, self).__init__(db_conn, hs)
  25. self.register_background_index_update(
  26. "device_inbox_stream_index",
  27. index_name="device_inbox_stream_id_user_id",
  28. table="device_inbox",
  29. columns=["stream_id", "user_id"],
  30. )
  31. self.register_background_update_handler(
  32. self.DEVICE_INBOX_STREAM_ID,
  33. self._background_drop_index_device_inbox,
  34. )
  35. # Map of (user_id, device_id) to the last stream_id that has been
  36. # deleted up to. This is so that we can no op deletions.
  37. self._last_device_delete_cache = ExpiringCache(
  38. cache_name="last_device_delete_cache",
  39. clock=self._clock,
  40. max_len=10000,
  41. expiry_ms=30 * 60 * 1000,
  42. )
  43. @defer.inlineCallbacks
  44. def add_messages_to_device_inbox(self, local_messages_by_user_then_device,
  45. remote_messages_by_destination):
  46. """Used to send messages from this server.
  47. Args:
  48. sender_user_id(str): The ID of the user sending these messages.
  49. local_messages_by_user_and_device(dict):
  50. Dictionary of user_id to device_id to message.
  51. remote_messages_by_destination(dict):
  52. Dictionary of destination server_name to the EDU JSON to send.
  53. Returns:
  54. A deferred stream_id that resolves when the messages have been
  55. inserted.
  56. """
  57. def add_messages_txn(txn, now_ms, stream_id):
  58. # Add the local messages directly to the local inbox.
  59. self._add_messages_to_local_device_inbox_txn(
  60. txn, stream_id, local_messages_by_user_then_device
  61. )
  62. # Add the remote messages to the federation outbox.
  63. # We'll send them to a remote server when we next send a
  64. # federation transaction to that destination.
  65. sql = (
  66. "INSERT INTO device_federation_outbox"
  67. " (destination, stream_id, queued_ts, messages_json)"
  68. " VALUES (?,?,?,?)"
  69. )
  70. rows = []
  71. for destination, edu in remote_messages_by_destination.items():
  72. edu_json = json.dumps(edu)
  73. rows.append((destination, stream_id, now_ms, edu_json))
  74. txn.executemany(sql, rows)
  75. with self._device_inbox_id_gen.get_next() as stream_id:
  76. now_ms = self.clock.time_msec()
  77. yield self.runInteraction(
  78. "add_messages_to_device_inbox",
  79. add_messages_txn,
  80. now_ms,
  81. stream_id,
  82. )
  83. for user_id in local_messages_by_user_then_device.keys():
  84. self._device_inbox_stream_cache.entity_has_changed(
  85. user_id, stream_id
  86. )
  87. for destination in remote_messages_by_destination.keys():
  88. self._device_federation_outbox_stream_cache.entity_has_changed(
  89. destination, stream_id
  90. )
  91. defer.returnValue(self._device_inbox_id_gen.get_current_token())
  92. @defer.inlineCallbacks
  93. def add_messages_from_remote_to_device_inbox(
  94. self, origin, message_id, local_messages_by_user_then_device
  95. ):
  96. def add_messages_txn(txn, now_ms, stream_id):
  97. # Check if we've already inserted a matching message_id for that
  98. # origin. This can happen if the origin doesn't receive our
  99. # acknowledgement from the first time we received the message.
  100. already_inserted = self._simple_select_one_txn(
  101. txn, table="device_federation_inbox",
  102. keyvalues={"origin": origin, "message_id": message_id},
  103. retcols=("message_id",),
  104. allow_none=True,
  105. )
  106. if already_inserted is not None:
  107. return
  108. # Add an entry for this message_id so that we know we've processed
  109. # it.
  110. self._simple_insert_txn(
  111. txn, table="device_federation_inbox",
  112. values={
  113. "origin": origin,
  114. "message_id": message_id,
  115. "received_ts": now_ms,
  116. },
  117. )
  118. # Add the messages to the approriate local device inboxes so that
  119. # they'll be sent to the devices when they next sync.
  120. self._add_messages_to_local_device_inbox_txn(
  121. txn, stream_id, local_messages_by_user_then_device
  122. )
  123. with self._device_inbox_id_gen.get_next() as stream_id:
  124. now_ms = self.clock.time_msec()
  125. yield self.runInteraction(
  126. "add_messages_from_remote_to_device_inbox",
  127. add_messages_txn,
  128. now_ms,
  129. stream_id,
  130. )
  131. for user_id in local_messages_by_user_then_device.keys():
  132. self._device_inbox_stream_cache.entity_has_changed(
  133. user_id, stream_id
  134. )
  135. defer.returnValue(stream_id)
  136. def _add_messages_to_local_device_inbox_txn(self, txn, stream_id,
  137. messages_by_user_then_device):
  138. sql = (
  139. "UPDATE device_max_stream_id"
  140. " SET stream_id = ?"
  141. " WHERE stream_id < ?"
  142. )
  143. txn.execute(sql, (stream_id, stream_id))
  144. local_by_user_then_device = {}
  145. for user_id, messages_by_device in messages_by_user_then_device.items():
  146. messages_json_for_user = {}
  147. devices = messages_by_device.keys()
  148. if len(devices) == 1 and devices[0] == "*":
  149. # Handle wildcard device_ids.
  150. sql = (
  151. "SELECT device_id FROM devices"
  152. " WHERE user_id = ?"
  153. )
  154. txn.execute(sql, (user_id,))
  155. message_json = json.dumps(messages_by_device["*"])
  156. for row in txn:
  157. # Add the message for all devices for this user on this
  158. # server.
  159. device = row[0]
  160. messages_json_for_user[device] = message_json
  161. else:
  162. if not devices:
  163. continue
  164. sql = (
  165. "SELECT device_id FROM devices"
  166. " WHERE user_id = ? AND device_id IN ("
  167. + ",".join("?" * len(devices))
  168. + ")"
  169. )
  170. # TODO: Maybe this needs to be done in batches if there are
  171. # too many local devices for a given user.
  172. txn.execute(sql, [user_id] + devices)
  173. for row in txn:
  174. # Only insert into the local inbox if the device exists on
  175. # this server
  176. device = row[0]
  177. message_json = json.dumps(messages_by_device[device])
  178. messages_json_for_user[device] = message_json
  179. if messages_json_for_user:
  180. local_by_user_then_device[user_id] = messages_json_for_user
  181. if not local_by_user_then_device:
  182. return
  183. sql = (
  184. "INSERT INTO device_inbox"
  185. " (user_id, device_id, stream_id, message_json)"
  186. " VALUES (?,?,?,?)"
  187. )
  188. rows = []
  189. for user_id, messages_by_device in local_by_user_then_device.items():
  190. for device_id, message_json in messages_by_device.items():
  191. rows.append((user_id, device_id, stream_id, message_json))
  192. txn.executemany(sql, rows)
  193. def get_new_messages_for_device(
  194. self, user_id, device_id, last_stream_id, current_stream_id, limit=100
  195. ):
  196. """
  197. Args:
  198. user_id(str): The recipient user_id.
  199. device_id(str): The recipient device_id.
  200. current_stream_id(int): The current position of the to device
  201. message stream.
  202. Returns:
  203. Deferred ([dict], int): List of messages for the device and where
  204. in the stream the messages got to.
  205. """
  206. has_changed = self._device_inbox_stream_cache.has_entity_changed(
  207. user_id, last_stream_id
  208. )
  209. if not has_changed:
  210. return defer.succeed(([], current_stream_id))
  211. def get_new_messages_for_device_txn(txn):
  212. sql = (
  213. "SELECT stream_id, message_json FROM device_inbox"
  214. " WHERE user_id = ? AND device_id = ?"
  215. " AND ? < stream_id AND stream_id <= ?"
  216. " ORDER BY stream_id ASC"
  217. " LIMIT ?"
  218. )
  219. txn.execute(sql, (
  220. user_id, device_id, last_stream_id, current_stream_id, limit
  221. ))
  222. messages = []
  223. for row in txn:
  224. stream_pos = row[0]
  225. messages.append(json.loads(row[1]))
  226. if len(messages) < limit:
  227. stream_pos = current_stream_id
  228. return (messages, stream_pos)
  229. return self.runInteraction(
  230. "get_new_messages_for_device", get_new_messages_for_device_txn,
  231. )
  232. @defer.inlineCallbacks
  233. def delete_messages_for_device(self, user_id, device_id, up_to_stream_id):
  234. """
  235. Args:
  236. user_id(str): The recipient user_id.
  237. device_id(str): The recipient device_id.
  238. up_to_stream_id(int): Where to delete messages up to.
  239. Returns:
  240. A deferred that resolves to the number of messages deleted.
  241. """
  242. # If we have cached the last stream id we've deleted up to, we can
  243. # check if there is likely to be anything that needs deleting
  244. last_deleted_stream_id = self._last_device_delete_cache.get(
  245. (user_id, device_id), None
  246. )
  247. if last_deleted_stream_id:
  248. has_changed = self._device_inbox_stream_cache.has_entity_changed(
  249. user_id, last_deleted_stream_id
  250. )
  251. if not has_changed:
  252. defer.returnValue(0)
  253. def delete_messages_for_device_txn(txn):
  254. sql = (
  255. "DELETE FROM device_inbox"
  256. " WHERE user_id = ? AND device_id = ?"
  257. " AND stream_id <= ?"
  258. )
  259. txn.execute(sql, (user_id, device_id, up_to_stream_id))
  260. return txn.rowcount
  261. count = yield self.runInteraction(
  262. "delete_messages_for_device", delete_messages_for_device_txn
  263. )
  264. # Update the cache, ensuring that we only ever increase the value
  265. last_deleted_stream_id = self._last_device_delete_cache.get(
  266. (user_id, device_id), 0
  267. )
  268. self._last_device_delete_cache[(user_id, device_id)] = max(
  269. last_deleted_stream_id, up_to_stream_id
  270. )
  271. defer.returnValue(count)
  272. def get_all_new_device_messages(self, last_pos, current_pos, limit):
  273. """
  274. Args:
  275. last_pos(int):
  276. current_pos(int):
  277. limit(int):
  278. Returns:
  279. A deferred list of rows from the device inbox
  280. """
  281. if last_pos == current_pos:
  282. return defer.succeed([])
  283. def get_all_new_device_messages_txn(txn):
  284. # We limit like this as we might have multiple rows per stream_id, and
  285. # we want to make sure we always get all entries for any stream_id
  286. # we return.
  287. upper_pos = min(current_pos, last_pos + limit)
  288. sql = (
  289. "SELECT max(stream_id), user_id"
  290. " FROM device_inbox"
  291. " WHERE ? < stream_id AND stream_id <= ?"
  292. " GROUP BY user_id"
  293. )
  294. txn.execute(sql, (last_pos, upper_pos))
  295. rows = txn.fetchall()
  296. sql = (
  297. "SELECT max(stream_id), destination"
  298. " FROM device_federation_outbox"
  299. " WHERE ? < stream_id AND stream_id <= ?"
  300. " GROUP BY destination"
  301. )
  302. txn.execute(sql, (last_pos, upper_pos))
  303. rows.extend(txn)
  304. # Order by ascending stream ordering
  305. rows.sort()
  306. return rows
  307. return self.runInteraction(
  308. "get_all_new_device_messages", get_all_new_device_messages_txn
  309. )
  310. def get_to_device_stream_token(self):
  311. return self._device_inbox_id_gen.get_current_token()
  312. def get_new_device_msgs_for_remote(
  313. self, destination, last_stream_id, current_stream_id, limit=100
  314. ):
  315. """
  316. Args:
  317. destination(str): The name of the remote server.
  318. last_stream_id(int|long): The last position of the device message stream
  319. that the server sent up to.
  320. current_stream_id(int|long): The current position of the device
  321. message stream.
  322. Returns:
  323. Deferred ([dict], int|long): List of messages for the device and where
  324. in the stream the messages got to.
  325. """
  326. has_changed = self._device_federation_outbox_stream_cache.has_entity_changed(
  327. destination, last_stream_id
  328. )
  329. if not has_changed or last_stream_id == current_stream_id:
  330. return defer.succeed(([], current_stream_id))
  331. def get_new_messages_for_remote_destination_txn(txn):
  332. sql = (
  333. "SELECT stream_id, messages_json FROM device_federation_outbox"
  334. " WHERE destination = ?"
  335. " AND ? < stream_id AND stream_id <= ?"
  336. " ORDER BY stream_id ASC"
  337. " LIMIT ?"
  338. )
  339. txn.execute(sql, (
  340. destination, last_stream_id, current_stream_id, limit
  341. ))
  342. messages = []
  343. for row in txn:
  344. stream_pos = row[0]
  345. messages.append(json.loads(row[1]))
  346. if len(messages) < limit:
  347. stream_pos = current_stream_id
  348. return (messages, stream_pos)
  349. return self.runInteraction(
  350. "get_new_device_msgs_for_remote",
  351. get_new_messages_for_remote_destination_txn,
  352. )
  353. def delete_device_msgs_for_remote(self, destination, up_to_stream_id):
  354. """Used to delete messages when the remote destination acknowledges
  355. their receipt.
  356. Args:
  357. destination(str): The destination server_name
  358. up_to_stream_id(int): Where to delete messages up to.
  359. Returns:
  360. A deferred that resolves when the messages have been deleted.
  361. """
  362. def delete_messages_for_remote_destination_txn(txn):
  363. sql = (
  364. "DELETE FROM device_federation_outbox"
  365. " WHERE destination = ?"
  366. " AND stream_id <= ?"
  367. )
  368. txn.execute(sql, (destination, up_to_stream_id))
  369. return self.runInteraction(
  370. "delete_device_msgs_for_remote",
  371. delete_messages_for_remote_destination_txn
  372. )
  373. @defer.inlineCallbacks
  374. def _background_drop_index_device_inbox(self, progress, batch_size):
  375. def reindex_txn(conn):
  376. txn = conn.cursor()
  377. txn.execute(
  378. "DROP INDEX IF EXISTS device_inbox_stream_id"
  379. )
  380. txn.close()
  381. yield self.runWithConnection(reindex_txn)
  382. yield self._end_background_update(self.DEVICE_INBOX_STREAM_ID)
  383. defer.returnValue(1)