test_cleanup_extrems.py 13 KB

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