test_federation_catch_up.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. from typing import List, Tuple
  2. from mock import Mock
  3. from synapse.events import EventBase
  4. from synapse.federation.sender import PerDestinationQueue, TransactionManager
  5. from synapse.federation.units import Edu
  6. from synapse.rest import admin
  7. from synapse.rest.client.v1 import login, room
  8. from tests.test_utils import event_injection, make_awaitable
  9. from tests.unittest import FederatingHomeserverTestCase, override_config
  10. class FederationCatchUpTestCases(FederatingHomeserverTestCase):
  11. servlets = [
  12. admin.register_servlets,
  13. room.register_servlets,
  14. login.register_servlets,
  15. ]
  16. def make_homeserver(self, reactor, clock):
  17. return self.setup_test_homeserver(
  18. federation_transport_client=Mock(spec=["send_transaction"]),
  19. )
  20. def prepare(self, reactor, clock, hs):
  21. # stub out get_current_hosts_in_room
  22. state_handler = hs.get_state_handler()
  23. # This mock is crucial for destination_rooms to be populated.
  24. state_handler.get_current_hosts_in_room = Mock(
  25. return_value=make_awaitable(["test", "host2"])
  26. )
  27. # whenever send_transaction is called, record the pdu data
  28. self.pdus = []
  29. self.failed_pdus = []
  30. self.is_online = True
  31. self.hs.get_federation_transport_client().send_transaction.side_effect = (
  32. self.record_transaction
  33. )
  34. async def record_transaction(self, txn, json_cb):
  35. if self.is_online:
  36. data = json_cb()
  37. self.pdus.extend(data["pdus"])
  38. return {}
  39. else:
  40. data = json_cb()
  41. self.failed_pdus.extend(data["pdus"])
  42. raise IOError("Failed to connect because this is a test!")
  43. def get_destination_room(self, room: str, destination: str = "host2") -> dict:
  44. """
  45. Gets the destination_rooms entry for a (destination, room_id) pair.
  46. Args:
  47. room: room ID
  48. destination: what destination, default is "host2"
  49. Returns:
  50. Dictionary of { event_id: str, stream_ordering: int }
  51. """
  52. event_id, stream_ordering = self.get_success(
  53. self.hs.get_datastore().db_pool.execute(
  54. "test:get_destination_rooms",
  55. None,
  56. """
  57. SELECT event_id, stream_ordering
  58. FROM destination_rooms dr
  59. JOIN events USING (stream_ordering)
  60. WHERE dr.destination = ? AND dr.room_id = ?
  61. """,
  62. destination,
  63. room,
  64. )
  65. )[0]
  66. return {"event_id": event_id, "stream_ordering": stream_ordering}
  67. @override_config({"send_federation": True})
  68. def test_catch_up_destination_rooms_tracking(self):
  69. """
  70. Tests that we populate the `destination_rooms` table as needed.
  71. """
  72. self.register_user("u1", "you the one")
  73. u1_token = self.login("u1", "you the one")
  74. room = self.helper.create_room_as("u1", tok=u1_token)
  75. self.get_success(
  76. event_injection.inject_member_event(self.hs, room, "@user:host2", "join")
  77. )
  78. event_id_1 = self.helper.send(room, "wombats!", tok=u1_token)["event_id"]
  79. row_1 = self.get_destination_room(room)
  80. event_id_2 = self.helper.send(room, "rabbits!", tok=u1_token)["event_id"]
  81. row_2 = self.get_destination_room(room)
  82. # check: events correctly registered in order
  83. self.assertEqual(row_1["event_id"], event_id_1)
  84. self.assertEqual(row_2["event_id"], event_id_2)
  85. self.assertEqual(row_1["stream_ordering"], row_2["stream_ordering"] - 1)
  86. @override_config({"send_federation": True})
  87. def test_catch_up_last_successful_stream_ordering_tracking(self):
  88. """
  89. Tests that we populate the `destination_rooms` table as needed.
  90. """
  91. self.register_user("u1", "you the one")
  92. u1_token = self.login("u1", "you the one")
  93. room = self.helper.create_room_as("u1", tok=u1_token)
  94. # take the remote offline
  95. self.is_online = False
  96. self.get_success(
  97. event_injection.inject_member_event(self.hs, room, "@user:host2", "join")
  98. )
  99. self.helper.send(room, "wombats!", tok=u1_token)
  100. self.pump()
  101. lsso_1 = self.get_success(
  102. self.hs.get_datastore().get_destination_last_successful_stream_ordering(
  103. "host2"
  104. )
  105. )
  106. self.assertIsNone(
  107. lsso_1,
  108. "There should be no last successful stream ordering for an always-offline destination",
  109. )
  110. # bring the remote online
  111. self.is_online = True
  112. event_id_2 = self.helper.send(room, "rabbits!", tok=u1_token)["event_id"]
  113. lsso_2 = self.get_success(
  114. self.hs.get_datastore().get_destination_last_successful_stream_ordering(
  115. "host2"
  116. )
  117. )
  118. row_2 = self.get_destination_room(room)
  119. self.assertEqual(
  120. self.pdus[0]["content"]["body"],
  121. "rabbits!",
  122. "Test fault: didn't receive the right PDU",
  123. )
  124. self.assertEqual(
  125. row_2["event_id"],
  126. event_id_2,
  127. "Test fault: destination_rooms not updated correctly",
  128. )
  129. self.assertEqual(
  130. lsso_2,
  131. row_2["stream_ordering"],
  132. "Send succeeded but not marked as last_successful_stream_ordering",
  133. )
  134. @override_config({"send_federation": True}) # critical to federate
  135. def test_catch_up_from_blank_state(self):
  136. """
  137. Runs an overall test of federation catch-up from scratch.
  138. Further tests will focus on more narrow aspects and edge-cases, but I
  139. hope to provide an overall view with this test.
  140. """
  141. # bring the other server online
  142. self.is_online = True
  143. # let's make some events for the other server to receive
  144. self.register_user("u1", "you the one")
  145. u1_token = self.login("u1", "you the one")
  146. room_1 = self.helper.create_room_as("u1", tok=u1_token)
  147. room_2 = self.helper.create_room_as("u1", tok=u1_token)
  148. # also critical to federate
  149. self.get_success(
  150. event_injection.inject_member_event(self.hs, room_1, "@user:host2", "join")
  151. )
  152. self.get_success(
  153. event_injection.inject_member_event(self.hs, room_2, "@user:host2", "join")
  154. )
  155. self.helper.send_state(
  156. room_1, event_type="m.room.topic", body={"topic": "wombat"}, tok=u1_token
  157. )
  158. # check: PDU received for topic event
  159. self.assertEqual(len(self.pdus), 1)
  160. self.assertEqual(self.pdus[0]["type"], "m.room.topic")
  161. # take the remote offline
  162. self.is_online = False
  163. # send another event
  164. self.helper.send(room_1, "hi user!", tok=u1_token)
  165. # check: things didn't go well since the remote is down
  166. self.assertEqual(len(self.failed_pdus), 1)
  167. self.assertEqual(self.failed_pdus[0]["content"]["body"], "hi user!")
  168. # let's delete the federation transmission queue
  169. # (this pretends we are starting up fresh.)
  170. self.assertFalse(
  171. self.hs.get_federation_sender()
  172. ._per_destination_queues["host2"]
  173. .transmission_loop_running
  174. )
  175. del self.hs.get_federation_sender()._per_destination_queues["host2"]
  176. # let's also clear any backoffs
  177. self.get_success(
  178. self.hs.get_datastore().set_destination_retry_timings("host2", None, 0, 0)
  179. )
  180. # bring the remote online and clear the received pdu list
  181. self.is_online = True
  182. self.pdus = []
  183. # now we need to initiate a federation transaction somehow…
  184. # to do that, let's send another event (because it's simple to do)
  185. # (do it to another room otherwise the catch-up logic decides it doesn't
  186. # need to catch up room_1 — something I overlooked when first writing
  187. # this test)
  188. self.helper.send(room_2, "wombats!", tok=u1_token)
  189. # we should now have received both PDUs
  190. self.assertEqual(len(self.pdus), 2)
  191. self.assertEqual(self.pdus[0]["content"]["body"], "hi user!")
  192. self.assertEqual(self.pdus[1]["content"]["body"], "wombats!")
  193. def make_fake_destination_queue(
  194. self, destination: str = "host2"
  195. ) -> Tuple[PerDestinationQueue, List[EventBase]]:
  196. """
  197. Makes a fake per-destination queue.
  198. """
  199. transaction_manager = TransactionManager(self.hs)
  200. per_dest_queue = PerDestinationQueue(self.hs, transaction_manager, destination)
  201. results_list = []
  202. async def fake_send(
  203. destination_tm: str,
  204. pending_pdus: List[EventBase],
  205. _pending_edus: List[Edu],
  206. ) -> bool:
  207. assert destination == destination_tm
  208. results_list.extend(pending_pdus)
  209. return True # success!
  210. transaction_manager.send_new_transaction = fake_send
  211. return per_dest_queue, results_list
  212. @override_config({"send_federation": True})
  213. def test_catch_up_loop(self):
  214. """
  215. Tests the behaviour of _catch_up_transmission_loop.
  216. """
  217. # ARRANGE:
  218. # - a local user (u1)
  219. # - 3 rooms which u1 is joined to (and remote user @user:host2 is
  220. # joined to)
  221. # - some events (1 to 5) in those rooms
  222. # we have 'already sent' events 1 and 2 to host2
  223. per_dest_queue, sent_pdus = self.make_fake_destination_queue()
  224. self.register_user("u1", "you the one")
  225. u1_token = self.login("u1", "you the one")
  226. room_1 = self.helper.create_room_as("u1", tok=u1_token)
  227. room_2 = self.helper.create_room_as("u1", tok=u1_token)
  228. room_3 = self.helper.create_room_as("u1", tok=u1_token)
  229. self.get_success(
  230. event_injection.inject_member_event(self.hs, room_1, "@user:host2", "join")
  231. )
  232. self.get_success(
  233. event_injection.inject_member_event(self.hs, room_2, "@user:host2", "join")
  234. )
  235. self.get_success(
  236. event_injection.inject_member_event(self.hs, room_3, "@user:host2", "join")
  237. )
  238. # create some events
  239. self.helper.send(room_1, "you hear me!!", tok=u1_token)
  240. event_id_2 = self.helper.send(room_2, "wombats!", tok=u1_token)["event_id"]
  241. self.helper.send(room_3, "Matrix!", tok=u1_token)
  242. event_id_4 = self.helper.send(room_2, "rabbits!", tok=u1_token)["event_id"]
  243. event_id_5 = self.helper.send(room_3, "Synapse!", tok=u1_token)["event_id"]
  244. # destination_rooms should already be populated, but let us pretend that we already
  245. # sent (successfully) up to and including event id 2
  246. event_2 = self.get_success(self.hs.get_datastore().get_event(event_id_2))
  247. # also fetch event 5 so we know its last_successful_stream_ordering later
  248. event_5 = self.get_success(self.hs.get_datastore().get_event(event_id_5))
  249. self.get_success(
  250. self.hs.get_datastore().set_destination_last_successful_stream_ordering(
  251. "host2", event_2.internal_metadata.stream_ordering
  252. )
  253. )
  254. # ACT
  255. self.get_success(per_dest_queue._catch_up_transmission_loop())
  256. # ASSERT, noticing in particular:
  257. # - event 3 not sent out, because event 5 replaces it
  258. # - order is least recent first, so event 5 comes after event 4
  259. # - catch-up is completed
  260. self.assertEqual(len(sent_pdus), 2)
  261. self.assertEqual(sent_pdus[0].event_id, event_id_4)
  262. self.assertEqual(sent_pdus[1].event_id, event_id_5)
  263. self.assertFalse(per_dest_queue._catching_up)
  264. self.assertEqual(
  265. per_dest_queue._last_successful_stream_ordering,
  266. event_5.internal_metadata.stream_ordering,
  267. )
  268. @override_config({"send_federation": True})
  269. def test_catch_up_on_synapse_startup(self):
  270. """
  271. Tests the behaviour of get_catch_up_outstanding_destinations and
  272. _wake_destinations_needing_catchup.
  273. """
  274. # list of sorted server names (note that there are more servers than the batch
  275. # size used in get_catch_up_outstanding_destinations).
  276. server_names = ["server%02d" % number for number in range(42)] + ["zzzerver"]
  277. # ARRANGE:
  278. # - a local user (u1)
  279. # - a room which u1 is joined to (and remote users @user:serverXX are
  280. # joined to)
  281. # mark the remotes as online
  282. self.is_online = True
  283. self.register_user("u1", "you the one")
  284. u1_token = self.login("u1", "you the one")
  285. room_id = self.helper.create_room_as("u1", tok=u1_token)
  286. for server_name in server_names:
  287. self.get_success(
  288. event_injection.inject_member_event(
  289. self.hs, room_id, "@user:%s" % server_name, "join"
  290. )
  291. )
  292. # create an event
  293. self.helper.send(room_id, "deary me!", tok=u1_token)
  294. # ASSERT:
  295. # - All servers are up to date so none should have outstanding catch-up
  296. outstanding_when_successful = self.get_success(
  297. self.hs.get_datastore().get_catch_up_outstanding_destinations(None)
  298. )
  299. self.assertEqual(outstanding_when_successful, [])
  300. # ACT:
  301. # - Make the remote servers unreachable
  302. self.is_online = False
  303. # - Mark zzzerver as being backed-off from
  304. now = self.clock.time_msec()
  305. self.get_success(
  306. self.hs.get_datastore().set_destination_retry_timings(
  307. "zzzerver", now, now, 24 * 60 * 60 * 1000 # retry in 1 day
  308. )
  309. )
  310. # - Send an event
  311. self.helper.send(room_id, "can anyone hear me?", tok=u1_token)
  312. # ASSERT (get_catch_up_outstanding_destinations):
  313. # - all remotes are outstanding
  314. # - they are returned in batches of 25, in order
  315. outstanding_1 = self.get_success(
  316. self.hs.get_datastore().get_catch_up_outstanding_destinations(None)
  317. )
  318. self.assertEqual(len(outstanding_1), 25)
  319. self.assertEqual(outstanding_1, server_names[0:25])
  320. outstanding_2 = self.get_success(
  321. self.hs.get_datastore().get_catch_up_outstanding_destinations(
  322. outstanding_1[-1]
  323. )
  324. )
  325. self.assertNotIn("zzzerver", outstanding_2)
  326. self.assertEqual(len(outstanding_2), 17)
  327. self.assertEqual(outstanding_2, server_names[25:-1])
  328. # ACT: call _wake_destinations_needing_catchup
  329. # patch wake_destination to just count the destinations instead
  330. woken = []
  331. def wake_destination_track(destination):
  332. woken.append(destination)
  333. self.hs.get_federation_sender().wake_destination = wake_destination_track
  334. # cancel the pre-existing timer for _wake_destinations_needing_catchup
  335. # this is because we are calling it manually rather than waiting for it
  336. # to be called automatically
  337. self.hs.get_federation_sender()._catchup_after_startup_timer.cancel()
  338. self.get_success(
  339. self.hs.get_federation_sender()._wake_destinations_needing_catchup(), by=5.0
  340. )
  341. # ASSERT (_wake_destinations_needing_catchup):
  342. # - all remotes are woken up, save for zzzerver
  343. self.assertNotIn("zzzerver", woken)
  344. # - all destinations are woken exactly once; they appear once in woken.
  345. self.assertCountEqual(woken, server_names[:-1])