test_events.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 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. from typing import List, Optional
  16. from synapse.api.constants import EventTypes, Membership
  17. from synapse.events import EventBase
  18. from synapse.replication.tcp.commands import RdataCommand
  19. from synapse.replication.tcp.streams._base import _STREAM_UPDATE_TARGET_ROW_COUNT
  20. from synapse.replication.tcp.streams.events import (
  21. EventsStreamCurrentStateRow,
  22. EventsStreamEventRow,
  23. EventsStreamRow,
  24. )
  25. from synapse.rest import admin
  26. from synapse.rest.client.v1 import login, room
  27. from tests.replication._base import BaseStreamTestCase
  28. from tests.test_utils.event_injection import inject_event, inject_member_event
  29. class EventsStreamTestCase(BaseStreamTestCase):
  30. servlets = [
  31. admin.register_servlets,
  32. login.register_servlets,
  33. room.register_servlets,
  34. ]
  35. def prepare(self, reactor, clock, hs):
  36. super().prepare(reactor, clock, hs)
  37. self.user_id = self.register_user("u1", "pass")
  38. self.user_tok = self.login("u1", "pass")
  39. self.reconnect()
  40. self.room_id = self.helper.create_room_as(tok=self.user_tok)
  41. self.test_handler.received_rdata_rows.clear()
  42. def test_update_function_event_row_limit(self):
  43. """Test replication with many non-state events
  44. Checks that all events are correctly replicated when there are lots of
  45. event rows to be replicated.
  46. """
  47. # disconnect, so that we can stack up some changes
  48. self.disconnect()
  49. # generate lots of non-state events. We inject them using inject_event
  50. # so that they are not send out over replication until we call self.replicate().
  51. events = [
  52. self._inject_test_event()
  53. for _ in range(_STREAM_UPDATE_TARGET_ROW_COUNT + 1)
  54. ]
  55. # also one state event
  56. state_event = self._inject_state_event()
  57. # check we're testing what we think we are: no rows should yet have been
  58. # received
  59. self.assertEqual([], self.test_handler.received_rdata_rows)
  60. # now reconnect to pull the updates
  61. self.reconnect()
  62. self.replicate()
  63. # we should have received all the expected rows in the right order (as
  64. # well as various cache invalidation updates which we ignore)
  65. received_rows = [
  66. row for row in self.test_handler.received_rdata_rows if row[0] == "events"
  67. ]
  68. for event in events:
  69. stream_name, token, row = received_rows.pop(0)
  70. self.assertEqual("events", stream_name)
  71. self.assertIsInstance(row, EventsStreamRow)
  72. self.assertEqual(row.type, "ev")
  73. self.assertIsInstance(row.data, EventsStreamEventRow)
  74. self.assertEqual(row.data.event_id, event.event_id)
  75. stream_name, token, row = received_rows.pop(0)
  76. self.assertIsInstance(row, EventsStreamRow)
  77. self.assertIsInstance(row.data, EventsStreamEventRow)
  78. self.assertEqual(row.data.event_id, state_event.event_id)
  79. stream_name, token, row = received_rows.pop(0)
  80. self.assertEqual("events", stream_name)
  81. self.assertIsInstance(row, EventsStreamRow)
  82. self.assertEqual(row.type, "state")
  83. self.assertIsInstance(row.data, EventsStreamCurrentStateRow)
  84. self.assertEqual(row.data.event_id, state_event.event_id)
  85. self.assertEqual([], received_rows)
  86. def test_update_function_huge_state_change(self):
  87. """Test replication with many state events
  88. Ensures that all events are correctly replicated when there are lots of
  89. state change rows to be replicated.
  90. """
  91. # we want to generate lots of state changes at a single stream ID.
  92. #
  93. # We do this by having two branches in the DAG. On one, we have a moderator
  94. # which that generates lots of state; on the other, we de-op the moderator,
  95. # thus invalidating all the state.
  96. OTHER_USER = "@other_user:localhost"
  97. # have the user join
  98. self.get_success(
  99. inject_member_event(self.hs, self.room_id, OTHER_USER, Membership.JOIN)
  100. )
  101. # Update existing power levels with mod at PL50
  102. pls = self.helper.get_state(
  103. self.room_id, EventTypes.PowerLevels, tok=self.user_tok
  104. )
  105. pls["users"][OTHER_USER] = 50
  106. self.helper.send_state(
  107. self.room_id, EventTypes.PowerLevels, pls, tok=self.user_tok,
  108. )
  109. # this is the point in the DAG where we make a fork
  110. fork_point = self.get_success(
  111. self.hs.get_datastore().get_latest_event_ids_in_room(self.room_id)
  112. ) # type: List[str]
  113. events = [
  114. self._inject_state_event(sender=OTHER_USER)
  115. for _ in range(_STREAM_UPDATE_TARGET_ROW_COUNT)
  116. ]
  117. self.replicate()
  118. # all those events and state changes should have landed
  119. self.assertGreaterEqual(
  120. len(self.test_handler.received_rdata_rows), 2 * len(events)
  121. )
  122. # disconnect, so that we can stack up the changes
  123. self.disconnect()
  124. self.test_handler.received_rdata_rows.clear()
  125. # a state event which doesn't get rolled back, to check that the state
  126. # before the huge update comes through ok
  127. state1 = self._inject_state_event()
  128. # roll back all the state by de-modding the user
  129. prev_events = fork_point
  130. pls["users"][OTHER_USER] = 0
  131. pl_event = self.get_success(
  132. inject_event(
  133. self.hs,
  134. prev_event_ids=prev_events,
  135. type=EventTypes.PowerLevels,
  136. state_key="",
  137. sender=self.user_id,
  138. room_id=self.room_id,
  139. content=pls,
  140. )
  141. )
  142. # one more bit of state that doesn't get rolled back
  143. state2 = self._inject_state_event()
  144. # check we're testing what we think we are: no rows should yet have been
  145. # received
  146. self.assertEqual([], self.test_handler.received_rdata_rows)
  147. # now reconnect to pull the updates
  148. self.reconnect()
  149. self.replicate()
  150. # we should have received all the expected rows in the right order (as
  151. # well as various cache invalidation updates which we ignore)
  152. #
  153. # we expect:
  154. #
  155. # - two rows for state1
  156. # - the PL event row, plus state rows for the PL event and each
  157. # of the states that got reverted.
  158. # - two rows for state2
  159. received_rows = [
  160. row for row in self.test_handler.received_rdata_rows if row[0] == "events"
  161. ]
  162. # first check the first two rows, which should be state1
  163. stream_name, token, row = received_rows.pop(0)
  164. self.assertEqual("events", stream_name)
  165. self.assertIsInstance(row, EventsStreamRow)
  166. self.assertEqual(row.type, "ev")
  167. self.assertIsInstance(row.data, EventsStreamEventRow)
  168. self.assertEqual(row.data.event_id, state1.event_id)
  169. stream_name, token, row = received_rows.pop(0)
  170. self.assertIsInstance(row, EventsStreamRow)
  171. self.assertEqual(row.type, "state")
  172. self.assertIsInstance(row.data, EventsStreamCurrentStateRow)
  173. self.assertEqual(row.data.event_id, state1.event_id)
  174. # now the last two rows, which should be state2
  175. stream_name, token, row = received_rows.pop(-2)
  176. self.assertEqual("events", stream_name)
  177. self.assertIsInstance(row, EventsStreamRow)
  178. self.assertEqual(row.type, "ev")
  179. self.assertIsInstance(row.data, EventsStreamEventRow)
  180. self.assertEqual(row.data.event_id, state2.event_id)
  181. stream_name, token, row = received_rows.pop(-1)
  182. self.assertIsInstance(row, EventsStreamRow)
  183. self.assertEqual(row.type, "state")
  184. self.assertIsInstance(row.data, EventsStreamCurrentStateRow)
  185. self.assertEqual(row.data.event_id, state2.event_id)
  186. # that should leave us with the rows for the PL event
  187. self.assertEqual(len(received_rows), len(events) + 2)
  188. stream_name, token, row = received_rows.pop(0)
  189. self.assertEqual("events", stream_name)
  190. self.assertIsInstance(row, EventsStreamRow)
  191. self.assertEqual(row.type, "ev")
  192. self.assertIsInstance(row.data, EventsStreamEventRow)
  193. self.assertEqual(row.data.event_id, pl_event.event_id)
  194. # the state rows are unsorted
  195. state_rows = [] # type: List[EventsStreamCurrentStateRow]
  196. for stream_name, token, row in received_rows:
  197. self.assertEqual("events", stream_name)
  198. self.assertIsInstance(row, EventsStreamRow)
  199. self.assertEqual(row.type, "state")
  200. self.assertIsInstance(row.data, EventsStreamCurrentStateRow)
  201. state_rows.append(row.data)
  202. state_rows.sort(key=lambda r: r.state_key)
  203. sr = state_rows.pop(0)
  204. self.assertEqual(sr.type, EventTypes.PowerLevels)
  205. self.assertEqual(sr.event_id, pl_event.event_id)
  206. for sr in state_rows:
  207. self.assertEqual(sr.type, "test_state_event")
  208. # "None" indicates the state has been deleted
  209. self.assertIsNone(sr.event_id)
  210. def test_update_function_state_row_limit(self):
  211. """Test replication with many state events over several stream ids.
  212. """
  213. # we want to generate lots of state changes, but for this test, we want to
  214. # spread out the state changes over a few stream IDs.
  215. #
  216. # We do this by having two branches in the DAG. On one, we have four moderators,
  217. # each of which that generates lots of state; on the other, we de-op the users,
  218. # thus invalidating all the state.
  219. NUM_USERS = 4
  220. STATES_PER_USER = _STREAM_UPDATE_TARGET_ROW_COUNT // 4 + 1
  221. user_ids = ["@user%i:localhost" % (i,) for i in range(NUM_USERS)]
  222. # have the users join
  223. for u in user_ids:
  224. self.get_success(
  225. inject_member_event(self.hs, self.room_id, u, Membership.JOIN)
  226. )
  227. # Update existing power levels with mod at PL50
  228. pls = self.helper.get_state(
  229. self.room_id, EventTypes.PowerLevels, tok=self.user_tok
  230. )
  231. pls["users"].update({u: 50 for u in user_ids})
  232. self.helper.send_state(
  233. self.room_id, EventTypes.PowerLevels, pls, tok=self.user_tok,
  234. )
  235. # this is the point in the DAG where we make a fork
  236. fork_point = self.get_success(
  237. self.hs.get_datastore().get_latest_event_ids_in_room(self.room_id)
  238. ) # type: List[str]
  239. events = [] # type: List[EventBase]
  240. for user in user_ids:
  241. events.extend(
  242. self._inject_state_event(sender=user) for _ in range(STATES_PER_USER)
  243. )
  244. self.replicate()
  245. # all those events and state changes should have landed
  246. self.assertGreaterEqual(
  247. len(self.test_handler.received_rdata_rows), 2 * len(events)
  248. )
  249. # disconnect, so that we can stack up the changes
  250. self.disconnect()
  251. self.test_handler.received_rdata_rows.clear()
  252. # now roll back all that state by de-modding the users
  253. prev_events = fork_point
  254. pl_events = []
  255. for u in user_ids:
  256. pls["users"][u] = 0
  257. e = self.get_success(
  258. inject_event(
  259. self.hs,
  260. prev_event_ids=prev_events,
  261. type=EventTypes.PowerLevels,
  262. state_key="",
  263. sender=self.user_id,
  264. room_id=self.room_id,
  265. content=pls,
  266. )
  267. )
  268. prev_events = [e.event_id]
  269. pl_events.append(e)
  270. # check we're testing what we think we are: no rows should yet have been
  271. # received
  272. self.assertEqual([], self.test_handler.received_rdata_rows)
  273. # now reconnect to pull the updates
  274. self.reconnect()
  275. self.replicate()
  276. # we should have received all the expected rows in the right order (as
  277. # well as various cache invalidation updates which we ignore)
  278. received_rows = [
  279. row for row in self.test_handler.received_rdata_rows if row[0] == "events"
  280. ]
  281. self.assertGreaterEqual(len(received_rows), len(events))
  282. for i in range(NUM_USERS):
  283. # for each user, we expect the PL event row, followed by state rows for
  284. # the PL event and each of the states that got reverted.
  285. stream_name, token, row = received_rows.pop(0)
  286. self.assertEqual("events", stream_name)
  287. self.assertIsInstance(row, EventsStreamRow)
  288. self.assertEqual(row.type, "ev")
  289. self.assertIsInstance(row.data, EventsStreamEventRow)
  290. self.assertEqual(row.data.event_id, pl_events[i].event_id)
  291. # the state rows are unsorted
  292. state_rows = [] # type: List[EventsStreamCurrentStateRow]
  293. for j in range(STATES_PER_USER + 1):
  294. stream_name, token, row = received_rows.pop(0)
  295. self.assertEqual("events", stream_name)
  296. self.assertIsInstance(row, EventsStreamRow)
  297. self.assertEqual(row.type, "state")
  298. self.assertIsInstance(row.data, EventsStreamCurrentStateRow)
  299. state_rows.append(row.data)
  300. state_rows.sort(key=lambda r: r.state_key)
  301. sr = state_rows.pop(0)
  302. self.assertEqual(sr.type, EventTypes.PowerLevels)
  303. self.assertEqual(sr.event_id, pl_events[i].event_id)
  304. for sr in state_rows:
  305. self.assertEqual(sr.type, "test_state_event")
  306. # "None" indicates the state has been deleted
  307. self.assertIsNone(sr.event_id)
  308. self.assertEqual([], received_rows)
  309. def test_backwards_stream_id(self):
  310. """
  311. Test that RDATA that comes after the current position should be discarded.
  312. """
  313. # disconnect, so that we can stack up some changes
  314. self.disconnect()
  315. # Generate an events. We inject them using inject_event so that they are
  316. # not send out over replication until we call self.replicate().
  317. event = self._inject_test_event()
  318. # check we're testing what we think we are: no rows should yet have been
  319. # received
  320. self.assertEqual([], self.test_handler.received_rdata_rows)
  321. # now reconnect to pull the updates
  322. self.reconnect()
  323. self.replicate()
  324. # We should have received the expected single row (as well as various
  325. # cache invalidation updates which we ignore).
  326. received_rows = [
  327. row for row in self.test_handler.received_rdata_rows if row[0] == "events"
  328. ]
  329. # There should be a single received row.
  330. self.assertEqual(len(received_rows), 1)
  331. stream_name, token, row = received_rows[0]
  332. self.assertEqual("events", stream_name)
  333. self.assertIsInstance(row, EventsStreamRow)
  334. self.assertEqual(row.type, "ev")
  335. self.assertIsInstance(row.data, EventsStreamEventRow)
  336. self.assertEqual(row.data.event_id, event.event_id)
  337. # Reset the data.
  338. self.test_handler.received_rdata_rows = []
  339. # Save the current token for later.
  340. worker_events_stream = self.worker_hs.get_replication_streams()["events"]
  341. prev_token = worker_events_stream.current_token("master")
  342. # Manually send an old RDATA command, which should get dropped. This
  343. # re-uses the row from above, but with an earlier stream token.
  344. self.hs.get_tcp_replication().send_command(
  345. RdataCommand("events", "master", 1, row)
  346. )
  347. # No updates have been received (because it was discard as old).
  348. received_rows = [
  349. row for row in self.test_handler.received_rdata_rows if row[0] == "events"
  350. ]
  351. self.assertEqual(len(received_rows), 0)
  352. # Ensure the stream has not gone backwards.
  353. current_token = worker_events_stream.current_token("master")
  354. self.assertGreaterEqual(current_token, prev_token)
  355. event_count = 0
  356. def _inject_test_event(
  357. self, body: Optional[str] = None, sender: Optional[str] = None, **kwargs
  358. ) -> EventBase:
  359. if sender is None:
  360. sender = self.user_id
  361. if body is None:
  362. body = "event %i" % (self.event_count,)
  363. self.event_count += 1
  364. return self.get_success(
  365. inject_event(
  366. self.hs,
  367. room_id=self.room_id,
  368. sender=sender,
  369. type="test_event",
  370. content={"body": body},
  371. **kwargs
  372. )
  373. )
  374. def _inject_state_event(
  375. self,
  376. body: Optional[str] = None,
  377. state_key: Optional[str] = None,
  378. sender: Optional[str] = None,
  379. ) -> EventBase:
  380. if sender is None:
  381. sender = self.user_id
  382. if state_key is None:
  383. state_key = "state_%i" % (self.event_count,)
  384. self.event_count += 1
  385. if body is None:
  386. body = "state event %s" % (state_key,)
  387. return self.get_success(
  388. inject_event(
  389. self.hs,
  390. room_id=self.room_id,
  391. sender=sender,
  392. type="test_state_event",
  393. state_key=state_key,
  394. content={"body": body},
  395. )
  396. )