test_cleanup_extrems.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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 os.path
  16. from unittest.mock import patch
  17. from mock import Mock
  18. import synapse.rest.admin
  19. from synapse.api.constants import EventTypes
  20. from synapse.rest.client.v1 import login, room
  21. from synapse.storage import prepare_database
  22. from synapse.types import Requester, UserID
  23. from tests.unittest import HomeserverTestCase
  24. class CleanupExtremBackgroundUpdateStoreTestCase(HomeserverTestCase):
  25. """
  26. Test the background update to clean forward extremities table.
  27. """
  28. def prepare(self, reactor, clock, homeserver):
  29. self.store = homeserver.get_datastore()
  30. self.room_creator = homeserver.get_room_creation_handler()
  31. # Create a test user and room
  32. self.user = UserID("alice", "test")
  33. self.requester = Requester(self.user, None, False, None, None)
  34. info = self.get_success(self.room_creator.create_room(self.requester, {}))
  35. self.room_id = info["room_id"]
  36. def run_background_update(self):
  37. """Re run the background update to clean up the extremities.
  38. """
  39. # Make sure we don't clash with in progress updates.
  40. self.assertTrue(self.store._all_done, "Background updates are still ongoing")
  41. schema_path = os.path.join(
  42. prepare_database.dir_path,
  43. "data_stores",
  44. "main",
  45. "schema",
  46. "delta",
  47. "54",
  48. "delete_forward_extremities.sql",
  49. )
  50. def run_delta_file(txn):
  51. prepare_database.executescript(txn, schema_path)
  52. self.get_success(
  53. self.store.runInteraction("test_delete_forward_extremities", run_delta_file)
  54. )
  55. # Ugh, have to reset this flag
  56. self.store._all_done = False
  57. while not self.get_success(self.store.has_completed_background_updates()):
  58. self.get_success(self.store.do_next_background_update(100), by=0.1)
  59. def test_soft_failed_extremities_handled_correctly(self):
  60. """Test that extremities are correctly calculated in the presence of
  61. soft failed events.
  62. Tests a graph like:
  63. A <- SF1 <- SF2 <- B
  64. Where SF* are soft failed.
  65. """
  66. # Create the room graph
  67. event_id_1 = self.create_and_send_event(self.room_id, self.user)
  68. event_id_2 = self.create_and_send_event(
  69. self.room_id, self.user, True, [event_id_1]
  70. )
  71. event_id_3 = self.create_and_send_event(
  72. self.room_id, self.user, True, [event_id_2]
  73. )
  74. event_id_4 = self.create_and_send_event(
  75. self.room_id, self.user, False, [event_id_3]
  76. )
  77. # Check the latest events are as expected
  78. latest_event_ids = self.get_success(
  79. self.store.get_latest_event_ids_in_room(self.room_id)
  80. )
  81. self.assertEqual(latest_event_ids, [event_id_4])
  82. def test_basic_cleanup(self):
  83. """Test that extremities are correctly calculated in the presence of
  84. soft failed events.
  85. Tests a graph like:
  86. A <- SF1 <- B
  87. Where SF* are soft failed, and with extremities of A and B
  88. """
  89. # Create the room graph
  90. event_id_a = self.create_and_send_event(self.room_id, self.user)
  91. event_id_sf1 = self.create_and_send_event(
  92. self.room_id, self.user, True, [event_id_a]
  93. )
  94. event_id_b = self.create_and_send_event(
  95. self.room_id, self.user, False, [event_id_sf1]
  96. )
  97. # Add the new extremity and check the latest events are as expected
  98. self.add_extremity(self.room_id, event_id_a)
  99. latest_event_ids = self.get_success(
  100. self.store.get_latest_event_ids_in_room(self.room_id)
  101. )
  102. self.assertEqual(set(latest_event_ids), set((event_id_a, event_id_b)))
  103. # Run the background update and check it did the right thing
  104. self.run_background_update()
  105. latest_event_ids = self.get_success(
  106. self.store.get_latest_event_ids_in_room(self.room_id)
  107. )
  108. self.assertEqual(latest_event_ids, [event_id_b])
  109. def test_chain_of_fail_cleanup(self):
  110. """Test that extremities are correctly calculated in the presence of
  111. soft failed events.
  112. Tests a graph like:
  113. A <- SF1 <- SF2 <- B
  114. Where SF* are soft failed, and with extremities of A and B
  115. """
  116. # Create the room graph
  117. event_id_a = self.create_and_send_event(self.room_id, self.user)
  118. event_id_sf1 = self.create_and_send_event(
  119. self.room_id, self.user, True, [event_id_a]
  120. )
  121. event_id_sf2 = self.create_and_send_event(
  122. self.room_id, self.user, True, [event_id_sf1]
  123. )
  124. event_id_b = self.create_and_send_event(
  125. self.room_id, self.user, False, [event_id_sf2]
  126. )
  127. # Add the new extremity and check the latest events are as expected
  128. self.add_extremity(self.room_id, event_id_a)
  129. latest_event_ids = self.get_success(
  130. self.store.get_latest_event_ids_in_room(self.room_id)
  131. )
  132. self.assertEqual(set(latest_event_ids), set((event_id_a, event_id_b)))
  133. # Run the background update and check it did the right thing
  134. self.run_background_update()
  135. latest_event_ids = self.get_success(
  136. self.store.get_latest_event_ids_in_room(self.room_id)
  137. )
  138. self.assertEqual(latest_event_ids, [event_id_b])
  139. def test_forked_graph_cleanup(self):
  140. r"""Test that extremities are correctly calculated in the presence of
  141. soft failed events.
  142. Tests a graph like, where time flows down the page:
  143. A B
  144. / \ /
  145. / \ /
  146. SF1 SF2
  147. | |
  148. SF3 |
  149. / \ |
  150. | \ |
  151. C SF4
  152. Where SF* are soft failed, and with them A, B and C marked as
  153. extremities. This should resolve to B and C being marked as extremity.
  154. """
  155. # Create the room graph
  156. event_id_a = self.create_and_send_event(self.room_id, self.user)
  157. event_id_b = self.create_and_send_event(self.room_id, self.user)
  158. event_id_sf1 = self.create_and_send_event(
  159. self.room_id, self.user, True, [event_id_a]
  160. )
  161. event_id_sf2 = self.create_and_send_event(
  162. self.room_id, self.user, True, [event_id_a, event_id_b]
  163. )
  164. event_id_sf3 = self.create_and_send_event(
  165. self.room_id, self.user, True, [event_id_sf1]
  166. )
  167. self.create_and_send_event(
  168. self.room_id, self.user, True, [event_id_sf2, event_id_sf3]
  169. ) # SF4
  170. event_id_c = self.create_and_send_event(
  171. self.room_id, self.user, False, [event_id_sf3]
  172. )
  173. # Add the new extremity and check the latest events are as expected
  174. self.add_extremity(self.room_id, event_id_a)
  175. latest_event_ids = self.get_success(
  176. self.store.get_latest_event_ids_in_room(self.room_id)
  177. )
  178. self.assertEqual(
  179. set(latest_event_ids), set((event_id_a, event_id_b, event_id_c))
  180. )
  181. # Run the background update and check it did the right thing
  182. self.run_background_update()
  183. latest_event_ids = self.get_success(
  184. self.store.get_latest_event_ids_in_room(self.room_id)
  185. )
  186. self.assertEqual(set(latest_event_ids), set([event_id_b, event_id_c]))
  187. class CleanupExtremDummyEventsTestCase(HomeserverTestCase):
  188. CONSENT_VERSION = "1"
  189. EXTREMITIES_COUNT = 50
  190. servlets = [
  191. synapse.rest.admin.register_servlets_for_client_rest_resource,
  192. login.register_servlets,
  193. room.register_servlets,
  194. ]
  195. def make_homeserver(self, reactor, clock):
  196. config = self.default_config()
  197. config["cleanup_extremities_with_dummy_events"] = True
  198. return self.setup_test_homeserver(config=config)
  199. def prepare(self, reactor, clock, homeserver):
  200. self.store = homeserver.get_datastore()
  201. self.room_creator = homeserver.get_room_creation_handler()
  202. self.event_creator_handler = homeserver.get_event_creation_handler()
  203. # Create a test user and room
  204. self.user = UserID.from_string(self.register_user("user1", "password"))
  205. self.token1 = self.login("user1", "password")
  206. self.requester = Requester(self.user, None, False, None, None)
  207. info = self.get_success(self.room_creator.create_room(self.requester, {}))
  208. self.room_id = info["room_id"]
  209. self.event_creator = homeserver.get_event_creation_handler()
  210. homeserver.config.user_consent_version = self.CONSENT_VERSION
  211. def test_send_dummy_event(self):
  212. self._create_extremity_rich_graph()
  213. # Pump the reactor repeatedly so that the background updates have a
  214. # chance to run.
  215. self.pump(10 * 60)
  216. latest_event_ids = self.get_success(
  217. self.store.get_latest_event_ids_in_room(self.room_id)
  218. )
  219. self.assertTrue(len(latest_event_ids) < 10, len(latest_event_ids))
  220. @patch("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY", new=0)
  221. def test_send_dummy_events_when_insufficient_power(self):
  222. self._create_extremity_rich_graph()
  223. # Criple power levels
  224. self.helper.send_state(
  225. self.room_id,
  226. EventTypes.PowerLevels,
  227. body={"users": {str(self.user): -1}},
  228. tok=self.token1,
  229. )
  230. # Pump the reactor repeatedly so that the background updates have a
  231. # chance to run.
  232. self.pump(10 * 60)
  233. latest_event_ids = self.get_success(
  234. self.store.get_latest_event_ids_in_room(self.room_id)
  235. )
  236. # Check that the room has not been pruned
  237. self.assertTrue(len(latest_event_ids) > 10)
  238. # New user with regular levels
  239. user2 = self.register_user("user2", "password")
  240. token2 = self.login("user2", "password")
  241. self.helper.join(self.room_id, user2, tok=token2)
  242. self.pump(10 * 60)
  243. latest_event_ids = self.get_success(
  244. self.store.get_latest_event_ids_in_room(self.room_id)
  245. )
  246. self.assertTrue(len(latest_event_ids) < 10, len(latest_event_ids))
  247. @patch("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY", new=0)
  248. def test_send_dummy_event_without_consent(self):
  249. self._create_extremity_rich_graph()
  250. self._enable_consent_checking()
  251. # Pump the reactor repeatedly so that the background updates have a
  252. # chance to run. Attempt to add dummy event with user that has not consented
  253. # Check that dummy event send fails.
  254. self.pump(10 * 60)
  255. latest_event_ids = self.get_success(
  256. self.store.get_latest_event_ids_in_room(self.room_id)
  257. )
  258. self.assertTrue(len(latest_event_ids) == self.EXTREMITIES_COUNT)
  259. # Create new user, and add consent
  260. user2 = self.register_user("user2", "password")
  261. token2 = self.login("user2", "password")
  262. self.get_success(
  263. self.store.user_set_consent_version(user2, self.CONSENT_VERSION)
  264. )
  265. self.helper.join(self.room_id, user2, tok=token2)
  266. # Background updates should now cause a dummy event to be added to the graph
  267. self.pump(10 * 60)
  268. latest_event_ids = self.get_success(
  269. self.store.get_latest_event_ids_in_room(self.room_id)
  270. )
  271. self.assertTrue(len(latest_event_ids) < 10, len(latest_event_ids))
  272. @patch("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY", new=250)
  273. def test_expiry_logic(self):
  274. """Simple test to ensure that _expire_rooms_to_exclude_from_dummy_event_insertion()
  275. expires old entries correctly.
  276. """
  277. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion[
  278. "1"
  279. ] = 100000
  280. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion[
  281. "2"
  282. ] = 200000
  283. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion[
  284. "3"
  285. ] = 300000
  286. self.event_creator_handler._expire_rooms_to_exclude_from_dummy_event_insertion()
  287. # All entries within time frame
  288. self.assertEqual(
  289. len(
  290. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion
  291. ),
  292. 3,
  293. )
  294. # Oldest room to expire
  295. self.pump(1)
  296. self.event_creator_handler._expire_rooms_to_exclude_from_dummy_event_insertion()
  297. self.assertEqual(
  298. len(
  299. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion
  300. ),
  301. 2,
  302. )
  303. # All rooms to expire
  304. self.pump(2)
  305. self.assertEqual(
  306. len(
  307. self.event_creator_handler._rooms_to_exclude_from_dummy_event_insertion
  308. ),
  309. 0,
  310. )
  311. def _create_extremity_rich_graph(self):
  312. """Helper method to create bushy graph on demand"""
  313. event_id_start = self.create_and_send_event(self.room_id, self.user)
  314. for _ in range(self.EXTREMITIES_COUNT):
  315. self.create_and_send_event(
  316. self.room_id, self.user, prev_event_ids=[event_id_start]
  317. )
  318. latest_event_ids = self.get_success(
  319. self.store.get_latest_event_ids_in_room(self.room_id)
  320. )
  321. self.assertEqual(len(latest_event_ids), 50)
  322. def _enable_consent_checking(self):
  323. """Helper method to enable consent checking"""
  324. self.event_creator._block_events_without_consent_error = "No consent from user"
  325. consent_uri_builder = Mock()
  326. consent_uri_builder.build_user_consent_uri.return_value = "http://example.com"
  327. self.event_creator._consent_uri_builder = consent_uri_builder