test_background_update.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. # Copyright 2021 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. from unittest.mock import Mock
  15. import yaml
  16. from twisted.internet.defer import Deferred, ensureDeferred
  17. from twisted.test.proto_helpers import MemoryReactor
  18. from synapse.server import HomeServer
  19. from synapse.storage.background_updates import (
  20. BackgroundUpdater,
  21. ForeignKeyConstraint,
  22. NotNullConstraint,
  23. run_validate_constraint_and_delete_rows_schema_delta,
  24. )
  25. from synapse.storage.database import LoggingTransaction
  26. from synapse.storage.engines import PostgresEngine, Sqlite3Engine
  27. from synapse.types import JsonDict
  28. from synapse.util import Clock
  29. from tests import unittest
  30. from tests.test_utils import make_awaitable, simple_async_mock
  31. from tests.unittest import override_config
  32. class BackgroundUpdateTestCase(unittest.HomeserverTestCase):
  33. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  34. self.updates: BackgroundUpdater = self.hs.get_datastores().main.db_pool.updates
  35. # the base test class should have run the real bg updates for us
  36. self.assertTrue(
  37. self.get_success(self.updates.has_completed_background_updates())
  38. )
  39. self.update_handler = Mock()
  40. self.updates.register_background_update_handler(
  41. "test_update", self.update_handler
  42. )
  43. self.store = self.hs.get_datastores().main
  44. async def update(self, progress: JsonDict, count: int) -> int:
  45. duration_ms = 10
  46. await self.clock.sleep((count * duration_ms) / 1000)
  47. progress = {"my_key": progress["my_key"] + 1}
  48. await self.store.db_pool.runInteraction(
  49. "update_progress",
  50. self.updates._background_update_progress_txn,
  51. "test_update",
  52. progress,
  53. )
  54. return count
  55. def test_do_background_update(self) -> None:
  56. # the time we claim it takes to update one item when running the update
  57. duration_ms = 10
  58. # the target runtime for each bg update
  59. target_background_update_duration_ms = 100
  60. self.get_success(
  61. self.store.db_pool.simple_insert(
  62. "background_updates",
  63. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  64. )
  65. )
  66. self.update_handler.side_effect = self.update
  67. self.update_handler.reset_mock()
  68. res = self.get_success(
  69. self.updates.do_next_background_update(False),
  70. by=0.02,
  71. )
  72. self.assertFalse(res)
  73. # on the first call, we should get run with the default background update size
  74. self.update_handler.assert_called_once_with(
  75. {"my_key": 1}, self.updates.default_background_batch_size
  76. )
  77. # second step: complete the update
  78. # we should now get run with a much bigger number of items to update
  79. async def update(progress: JsonDict, count: int) -> int:
  80. self.assertEqual(progress, {"my_key": 2})
  81. self.assertAlmostEqual(
  82. count,
  83. target_background_update_duration_ms / duration_ms,
  84. places=0,
  85. )
  86. await self.updates._end_background_update("test_update")
  87. return count
  88. self.update_handler.side_effect = update
  89. self.update_handler.reset_mock()
  90. result = self.get_success(self.updates.do_next_background_update(False))
  91. self.assertFalse(result)
  92. self.update_handler.assert_called_once()
  93. # third step: we don't expect to be called any more
  94. self.update_handler.reset_mock()
  95. result = self.get_success(self.updates.do_next_background_update(False))
  96. self.assertTrue(result)
  97. self.assertFalse(self.update_handler.called)
  98. @override_config(
  99. yaml.safe_load(
  100. """
  101. background_updates:
  102. default_batch_size: 20
  103. """
  104. )
  105. )
  106. def test_background_update_default_batch_set_by_config(self) -> None:
  107. """
  108. Test that the background update is run with the default_batch_size set by the config
  109. """
  110. self.get_success(
  111. self.store.db_pool.simple_insert(
  112. "background_updates",
  113. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  114. )
  115. )
  116. self.update_handler.side_effect = self.update
  117. self.update_handler.reset_mock()
  118. res = self.get_success(
  119. self.updates.do_next_background_update(False),
  120. by=0.01,
  121. )
  122. self.assertFalse(res)
  123. # on the first call, we should get run with the default background update size specified in the config
  124. self.update_handler.assert_called_once_with({"my_key": 1}, 20)
  125. def test_background_update_default_sleep_behavior(self) -> None:
  126. """
  127. Test default background update behavior, which is to sleep
  128. """
  129. self.get_success(
  130. self.store.db_pool.simple_insert(
  131. "background_updates",
  132. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  133. )
  134. )
  135. self.update_handler.side_effect = self.update
  136. self.update_handler.reset_mock()
  137. self.updates.start_doing_background_updates()
  138. # 2: advance the reactor less than the default sleep duration (1000ms)
  139. self.reactor.pump([0.5])
  140. # check that an update has not been run
  141. self.update_handler.assert_not_called()
  142. # advance reactor past default sleep duration
  143. self.reactor.pump([1])
  144. # check that update has been run
  145. self.update_handler.assert_called()
  146. @override_config(
  147. yaml.safe_load(
  148. """
  149. background_updates:
  150. sleep_duration_ms: 500
  151. """
  152. )
  153. )
  154. def test_background_update_sleep_set_in_config(self) -> None:
  155. """
  156. Test that changing the sleep time in the config changes how long it sleeps
  157. """
  158. self.get_success(
  159. self.store.db_pool.simple_insert(
  160. "background_updates",
  161. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  162. )
  163. )
  164. self.update_handler.side_effect = self.update
  165. self.update_handler.reset_mock()
  166. self.updates.start_doing_background_updates()
  167. # 2: advance the reactor less than the configured sleep duration (500ms)
  168. self.reactor.pump([0.45])
  169. # check that an update has not been run
  170. self.update_handler.assert_not_called()
  171. # advance reactor past config sleep duration but less than default duration
  172. self.reactor.pump([0.75])
  173. # check that update has been run
  174. self.update_handler.assert_called()
  175. @override_config(
  176. yaml.safe_load(
  177. """
  178. background_updates:
  179. sleep_enabled: false
  180. """
  181. )
  182. )
  183. def test_disabling_background_update_sleep(self) -> None:
  184. """
  185. Test that disabling sleep in the config results in bg update not sleeping
  186. """
  187. self.get_success(
  188. self.store.db_pool.simple_insert(
  189. "background_updates",
  190. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  191. )
  192. )
  193. self.update_handler.side_effect = self.update
  194. self.update_handler.reset_mock()
  195. self.updates.start_doing_background_updates()
  196. # 2: advance the reactor very little
  197. self.reactor.pump([0.025])
  198. # check that an update has run
  199. self.update_handler.assert_called()
  200. @override_config(
  201. yaml.safe_load(
  202. """
  203. background_updates:
  204. background_update_duration_ms: 500
  205. """
  206. )
  207. )
  208. def test_background_update_duration_set_in_config(self) -> None:
  209. """
  210. Test that the desired duration set in the config is used in determining batch size
  211. """
  212. # Duration of one background update item
  213. duration_ms = 10
  214. self.get_success(
  215. self.store.db_pool.simple_insert(
  216. "background_updates",
  217. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  218. )
  219. )
  220. self.update_handler.side_effect = self.update
  221. self.update_handler.reset_mock()
  222. res = self.get_success(
  223. self.updates.do_next_background_update(False),
  224. by=0.02,
  225. )
  226. self.assertFalse(res)
  227. # the first update was run with the default batch size, this should be run with 500ms as the
  228. # desired duration
  229. async def update(progress: JsonDict, count: int) -> int:
  230. self.assertEqual(progress, {"my_key": 2})
  231. self.assertAlmostEqual(
  232. count,
  233. 500 / duration_ms,
  234. places=0,
  235. )
  236. await self.updates._end_background_update("test_update")
  237. return count
  238. self.update_handler.side_effect = update
  239. self.get_success(self.updates.do_next_background_update(False))
  240. @override_config(
  241. yaml.safe_load(
  242. """
  243. background_updates:
  244. min_batch_size: 5
  245. """
  246. )
  247. )
  248. def test_background_update_min_batch_set_in_config(self) -> None:
  249. """
  250. Test that the minimum batch size set in the config is used
  251. """
  252. # a very long-running individual update
  253. duration_ms = 50
  254. self.get_success(
  255. self.store.db_pool.simple_insert(
  256. "background_updates",
  257. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  258. )
  259. )
  260. # Run the update with the long-running update item
  261. async def update_long(progress: JsonDict, count: int) -> int:
  262. await self.clock.sleep((count * duration_ms) / 1000)
  263. progress = {"my_key": progress["my_key"] + 1}
  264. await self.store.db_pool.runInteraction(
  265. "update_progress",
  266. self.updates._background_update_progress_txn,
  267. "test_update",
  268. progress,
  269. )
  270. return count
  271. self.update_handler.side_effect = update_long
  272. self.update_handler.reset_mock()
  273. res = self.get_success(
  274. self.updates.do_next_background_update(False),
  275. by=1,
  276. )
  277. self.assertFalse(res)
  278. # the first update was run with the default batch size, this should be run with minimum batch size
  279. # as the first items took a very long time
  280. async def update_short(progress: JsonDict, count: int) -> int:
  281. self.assertEqual(progress, {"my_key": 2})
  282. self.assertEqual(count, 5)
  283. await self.updates._end_background_update("test_update")
  284. return count
  285. self.update_handler.side_effect = update_short
  286. self.get_success(self.updates.do_next_background_update(False))
  287. class BackgroundUpdateControllerTestCase(unittest.HomeserverTestCase):
  288. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  289. self.updates: BackgroundUpdater = self.hs.get_datastores().main.db_pool.updates
  290. # the base test class should have run the real bg updates for us
  291. self.assertTrue(
  292. self.get_success(self.updates.has_completed_background_updates())
  293. )
  294. self.update_deferred: Deferred[int] = Deferred()
  295. self.update_handler = Mock(return_value=self.update_deferred)
  296. self.updates.register_background_update_handler(
  297. "test_update", self.update_handler
  298. )
  299. # Mock out the AsyncContextManager
  300. class MockCM:
  301. __aenter__ = simple_async_mock(return_value=None)
  302. __aexit__ = simple_async_mock(return_value=None)
  303. self._update_ctx_manager = MockCM
  304. # Mock out the `update_handler` callback
  305. self._on_update = Mock(return_value=self._update_ctx_manager())
  306. # Define a default batch size value that's not the same as the internal default
  307. # value (100).
  308. self._default_batch_size = 500
  309. # Register the callbacks with more mocks
  310. self.hs.get_module_api().register_background_update_controller_callbacks(
  311. on_update=self._on_update,
  312. min_batch_size=Mock(return_value=make_awaitable(self._default_batch_size)),
  313. default_batch_size=Mock(
  314. return_value=make_awaitable(self._default_batch_size),
  315. ),
  316. )
  317. def test_controller(self) -> None:
  318. store = self.hs.get_datastores().main
  319. self.get_success(
  320. store.db_pool.simple_insert(
  321. "background_updates",
  322. values={"update_name": "test_update", "progress_json": "{}"},
  323. )
  324. )
  325. # Set the return value for the context manager.
  326. enter_defer: Deferred[int] = Deferred()
  327. self._update_ctx_manager.__aenter__ = Mock(return_value=enter_defer)
  328. # Start the background update.
  329. do_update_d = ensureDeferred(self.updates.do_next_background_update(True))
  330. self.pump()
  331. # `run_update` should have been called, but the update handler won't be
  332. # called until the `enter_defer` (returned by `__aenter__`) is resolved.
  333. self._on_update.assert_called_once_with(
  334. "test_update",
  335. "master",
  336. False,
  337. )
  338. self.assertFalse(do_update_d.called)
  339. self.assertFalse(self.update_deferred.called)
  340. # Resolving the `enter_defer` should call the update handler, which then
  341. # blocks.
  342. enter_defer.callback(100)
  343. self.pump()
  344. self.update_handler.assert_called_once_with({}, self._default_batch_size)
  345. self.assertFalse(self.update_deferred.called)
  346. self._update_ctx_manager.__aexit__.assert_not_called()
  347. # Resolving the update handler deferred should cause the
  348. # `do_next_background_update` to finish and return
  349. self.update_deferred.callback(100)
  350. self.pump()
  351. self._update_ctx_manager.__aexit__.assert_called()
  352. self.get_success(do_update_d)
  353. class BackgroundUpdateValidateConstraintTestCase(unittest.HomeserverTestCase):
  354. """Tests the validate contraint and delete background handlers."""
  355. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  356. self.updates: BackgroundUpdater = self.hs.get_datastores().main.db_pool.updates
  357. # the base test class should have run the real bg updates for us
  358. self.assertTrue(
  359. self.get_success(self.updates.has_completed_background_updates())
  360. )
  361. self.store = self.hs.get_datastores().main
  362. def test_not_null_constraint(self) -> None:
  363. # Create the initial tables, where we have some invalid data.
  364. """Tests adding a not null constraint."""
  365. table_sql = """
  366. CREATE TABLE test_constraint(
  367. a INT PRIMARY KEY,
  368. b INT
  369. );
  370. """
  371. self.get_success(
  372. self.store.db_pool.execute(
  373. "test_not_null_constraint", lambda _: None, table_sql
  374. )
  375. )
  376. # We add an index so that we can check that its correctly recreated when
  377. # using SQLite.
  378. index_sql = "CREATE INDEX test_index ON test_constraint(a)"
  379. self.get_success(
  380. self.store.db_pool.execute(
  381. "test_not_null_constraint", lambda _: None, index_sql
  382. )
  383. )
  384. self.get_success(
  385. self.store.db_pool.simple_insert("test_constraint", {"a": 1, "b": 1})
  386. )
  387. self.get_success(
  388. self.store.db_pool.simple_insert("test_constraint", {"a": 2, "b": None})
  389. )
  390. self.get_success(
  391. self.store.db_pool.simple_insert("test_constraint", {"a": 3, "b": 3})
  392. )
  393. # Now lets do the migration
  394. table2_sqlite = """
  395. CREATE TABLE test_constraint2(
  396. a INT PRIMARY KEY,
  397. b INT,
  398. CONSTRAINT test_constraint_name CHECK (b is NOT NULL)
  399. );
  400. """
  401. def delta(txn: LoggingTransaction) -> None:
  402. run_validate_constraint_and_delete_rows_schema_delta(
  403. txn,
  404. ordering=1000,
  405. update_name="test_bg_update",
  406. table="test_constraint",
  407. constraint_name="test_constraint_name",
  408. constraint=NotNullConstraint("b"),
  409. sqlite_table_name="test_constraint2",
  410. sqlite_table_schema=table2_sqlite,
  411. )
  412. self.get_success(
  413. self.store.db_pool.runInteraction(
  414. "test_not_null_constraint",
  415. delta,
  416. )
  417. )
  418. if isinstance(self.store.database_engine, PostgresEngine):
  419. # Postgres uses a background update
  420. self.updates.register_background_validate_constraint_and_delete_rows(
  421. "test_bg_update",
  422. table="test_constraint",
  423. constraint_name="test_constraint_name",
  424. constraint=NotNullConstraint("b"),
  425. unique_columns=["a"],
  426. )
  427. # Tell the DataStore that it hasn't finished all updates yet
  428. self.store.db_pool.updates._all_done = False
  429. # Now let's actually drive the updates to completion
  430. self.wait_for_background_updates()
  431. # Check the correct values are in the new table.
  432. rows = self.get_success(
  433. self.store.db_pool.simple_select_list(
  434. table="test_constraint",
  435. keyvalues={},
  436. retcols=("a", "b"),
  437. )
  438. )
  439. self.assertCountEqual(rows, [{"a": 1, "b": 1}, {"a": 3, "b": 3}])
  440. # And check that invalid rows get correctly rejected.
  441. self.get_failure(
  442. self.store.db_pool.simple_insert("test_constraint", {"a": 2, "b": None}),
  443. exc=self.store.database_engine.module.IntegrityError,
  444. )
  445. # Check the index is still there for SQLite.
  446. if isinstance(self.store.database_engine, Sqlite3Engine):
  447. # Ensure the index exists in the schema.
  448. self.get_success(
  449. self.store.db_pool.simple_select_one_onecol(
  450. table="sqlite_master",
  451. keyvalues={"tbl_name": "test_constraint"},
  452. retcol="name",
  453. )
  454. )
  455. def test_foreign_constraint(self) -> None:
  456. """Tests adding a not foreign key constraint."""
  457. # Create the initial tables, where we have some invalid data.
  458. base_sql = """
  459. CREATE TABLE base_table(
  460. b INT PRIMARY KEY
  461. );
  462. """
  463. table_sql = """
  464. CREATE TABLE test_constraint(
  465. a INT PRIMARY KEY,
  466. b INT NOT NULL
  467. );
  468. """
  469. self.get_success(
  470. self.store.db_pool.execute(
  471. "test_foreign_key_constraint", lambda _: None, base_sql
  472. )
  473. )
  474. self.get_success(
  475. self.store.db_pool.execute(
  476. "test_foreign_key_constraint", lambda _: None, table_sql
  477. )
  478. )
  479. self.get_success(self.store.db_pool.simple_insert("base_table", {"b": 1}))
  480. self.get_success(
  481. self.store.db_pool.simple_insert("test_constraint", {"a": 1, "b": 1})
  482. )
  483. self.get_success(
  484. self.store.db_pool.simple_insert("test_constraint", {"a": 2, "b": 2})
  485. )
  486. self.get_success(self.store.db_pool.simple_insert("base_table", {"b": 3}))
  487. self.get_success(
  488. self.store.db_pool.simple_insert("test_constraint", {"a": 3, "b": 3})
  489. )
  490. table2_sqlite = """
  491. CREATE TABLE test_constraint2(
  492. a INT PRIMARY KEY,
  493. b INT NOT NULL,
  494. CONSTRAINT test_constraint_name FOREIGN KEY (b) REFERENCES base_table (b)
  495. );
  496. """
  497. def delta(txn: LoggingTransaction) -> None:
  498. run_validate_constraint_and_delete_rows_schema_delta(
  499. txn,
  500. ordering=1000,
  501. update_name="test_bg_update",
  502. table="test_constraint",
  503. constraint_name="test_constraint_name",
  504. constraint=ForeignKeyConstraint(
  505. "base_table", [("b", "b")], deferred=False
  506. ),
  507. sqlite_table_name="test_constraint2",
  508. sqlite_table_schema=table2_sqlite,
  509. )
  510. self.get_success(
  511. self.store.db_pool.runInteraction(
  512. "test_foreign_key_constraint",
  513. delta,
  514. )
  515. )
  516. if isinstance(self.store.database_engine, PostgresEngine):
  517. # Postgres uses a background update
  518. self.updates.register_background_validate_constraint_and_delete_rows(
  519. "test_bg_update",
  520. table="test_constraint",
  521. constraint_name="test_constraint_name",
  522. constraint=ForeignKeyConstraint(
  523. "base_table", [("b", "b")], deferred=False
  524. ),
  525. unique_columns=["a"],
  526. )
  527. # Tell the DataStore that it hasn't finished all updates yet
  528. self.store.db_pool.updates._all_done = False
  529. # Now let's actually drive the updates to completion
  530. self.wait_for_background_updates()
  531. # Check the correct values are in the new table.
  532. rows = self.get_success(
  533. self.store.db_pool.simple_select_list(
  534. table="test_constraint",
  535. keyvalues={},
  536. retcols=("a", "b"),
  537. )
  538. )
  539. self.assertCountEqual(rows, [{"a": 1, "b": 1}, {"a": 3, "b": 3}])
  540. # And check that invalid rows get correctly rejected.
  541. self.get_failure(
  542. self.store.db_pool.simple_insert("test_constraint", {"a": 2, "b": 2}),
  543. exc=self.store.database_engine.module.IntegrityError,
  544. )