test_id_generators.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. # Copyright 2020 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 typing import List, Optional
  15. from twisted.test.proto_helpers import MemoryReactor
  16. from synapse.server import HomeServer
  17. from synapse.storage.database import (
  18. DatabasePool,
  19. LoggingDatabaseConnection,
  20. LoggingTransaction,
  21. )
  22. from synapse.storage.engines import IncorrectDatabaseSetup
  23. from synapse.storage.types import Cursor
  24. from synapse.storage.util.id_generators import MultiWriterIdGenerator, StreamIdGenerator
  25. from synapse.util import Clock
  26. from tests.unittest import HomeserverTestCase
  27. from tests.utils import USE_POSTGRES_FOR_TESTS
  28. class StreamIdGeneratorTestCase(HomeserverTestCase):
  29. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  30. self.store = hs.get_datastores().main
  31. self.db_pool: DatabasePool = self.store.db_pool
  32. self.get_success(self.db_pool.runInteraction("_setup_db", self._setup_db))
  33. def _setup_db(self, txn: LoggingTransaction) -> None:
  34. txn.execute(
  35. """
  36. CREATE TABLE foobar (
  37. stream_id BIGINT NOT NULL,
  38. data TEXT
  39. );
  40. """
  41. )
  42. txn.execute("INSERT INTO foobar VALUES (123, 'hello world');")
  43. def _create_id_generator(self) -> StreamIdGenerator:
  44. def _create(conn: LoggingDatabaseConnection) -> StreamIdGenerator:
  45. return StreamIdGenerator(
  46. db_conn=conn,
  47. table="foobar",
  48. column="stream_id",
  49. )
  50. return self.get_success_or_raise(self.db_pool.runWithConnection(_create))
  51. def test_initial_value(self) -> None:
  52. """Check that we read the current token from the DB."""
  53. id_gen = self._create_id_generator()
  54. self.assertEqual(id_gen.get_current_token(), 123)
  55. def test_single_gen_next(self) -> None:
  56. """Check that we correctly increment the current token from the DB."""
  57. id_gen = self._create_id_generator()
  58. async def test_gen_next() -> None:
  59. async with id_gen.get_next() as next_id:
  60. # We haven't persisted `next_id` yet; current token is still 123
  61. self.assertEqual(id_gen.get_current_token(), 123)
  62. # But we did learn what the next value is
  63. self.assertEqual(next_id, 124)
  64. # Once the context manager closes we assume that the `next_id` has been
  65. # written to the DB.
  66. self.assertEqual(id_gen.get_current_token(), 124)
  67. self.get_success(test_gen_next())
  68. def test_multiple_gen_nexts(self) -> None:
  69. """Check that we handle overlapping calls to gen_next sensibly."""
  70. id_gen = self._create_id_generator()
  71. async def test_gen_next() -> None:
  72. ctx1 = id_gen.get_next()
  73. ctx2 = id_gen.get_next()
  74. ctx3 = id_gen.get_next()
  75. # Request three new stream IDs.
  76. self.assertEqual(await ctx1.__aenter__(), 124)
  77. self.assertEqual(await ctx2.__aenter__(), 125)
  78. self.assertEqual(await ctx3.__aenter__(), 126)
  79. # None are persisted: current token unchanged.
  80. self.assertEqual(id_gen.get_current_token(), 123)
  81. # Persist each in turn.
  82. await ctx1.__aexit__(None, None, None)
  83. self.assertEqual(id_gen.get_current_token(), 124)
  84. await ctx2.__aexit__(None, None, None)
  85. self.assertEqual(id_gen.get_current_token(), 125)
  86. await ctx3.__aexit__(None, None, None)
  87. self.assertEqual(id_gen.get_current_token(), 126)
  88. self.get_success(test_gen_next())
  89. def test_multiple_gen_nexts_closed_in_different_order(self) -> None:
  90. """Check that we handle overlapping calls to gen_next, even when their IDs
  91. created and persisted in different orders."""
  92. id_gen = self._create_id_generator()
  93. async def test_gen_next() -> None:
  94. ctx1 = id_gen.get_next()
  95. ctx2 = id_gen.get_next()
  96. ctx3 = id_gen.get_next()
  97. # Request three new stream IDs.
  98. self.assertEqual(await ctx1.__aenter__(), 124)
  99. self.assertEqual(await ctx2.__aenter__(), 125)
  100. self.assertEqual(await ctx3.__aenter__(), 126)
  101. # None are persisted: current token unchanged.
  102. self.assertEqual(id_gen.get_current_token(), 123)
  103. # Persist them in a different order, starting with 126 from ctx3.
  104. await ctx3.__aexit__(None, None, None)
  105. # We haven't persisted 124 from ctx1 yet---current token is still 123.
  106. self.assertEqual(id_gen.get_current_token(), 123)
  107. # Now persist 124 from ctx1.
  108. await ctx1.__aexit__(None, None, None)
  109. # Current token is then 124, waiting for 125 to be persisted.
  110. self.assertEqual(id_gen.get_current_token(), 124)
  111. # Finally persist 125 from ctx2.
  112. await ctx2.__aexit__(None, None, None)
  113. # Current token is then 126 (skipping over 125).
  114. self.assertEqual(id_gen.get_current_token(), 126)
  115. self.get_success(test_gen_next())
  116. def test_gen_next_while_still_waiting_for_persistence(self) -> None:
  117. """Check that we handle overlapping calls to gen_next."""
  118. id_gen = self._create_id_generator()
  119. async def test_gen_next() -> None:
  120. ctx1 = id_gen.get_next()
  121. ctx2 = id_gen.get_next()
  122. ctx3 = id_gen.get_next()
  123. # Request two new stream IDs.
  124. self.assertEqual(await ctx1.__aenter__(), 124)
  125. self.assertEqual(await ctx2.__aenter__(), 125)
  126. # Persist ctx2 first.
  127. await ctx2.__aexit__(None, None, None)
  128. # Still waiting on ctx1's ID to be persisted.
  129. self.assertEqual(id_gen.get_current_token(), 123)
  130. # Now request a third stream ID. It should be 126 (the smallest ID that
  131. # we've not yet handed out.)
  132. self.assertEqual(await ctx3.__aenter__(), 126)
  133. self.get_success(test_gen_next())
  134. class MultiWriterIdGeneratorTestCase(HomeserverTestCase):
  135. if not USE_POSTGRES_FOR_TESTS:
  136. skip = "Requires Postgres"
  137. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  138. self.store = hs.get_datastores().main
  139. self.db_pool: DatabasePool = self.store.db_pool
  140. self.get_success(self.db_pool.runInteraction("_setup_db", self._setup_db))
  141. def _setup_db(self, txn: LoggingTransaction) -> None:
  142. txn.execute("CREATE SEQUENCE foobar_seq")
  143. txn.execute(
  144. """
  145. CREATE TABLE foobar (
  146. stream_id BIGINT NOT NULL,
  147. instance_name TEXT NOT NULL,
  148. data TEXT
  149. );
  150. """
  151. )
  152. def _create_id_generator(
  153. self, instance_name: str = "master", writers: Optional[List[str]] = None
  154. ) -> MultiWriterIdGenerator:
  155. def _create(conn: LoggingDatabaseConnection) -> MultiWriterIdGenerator:
  156. return MultiWriterIdGenerator(
  157. conn,
  158. self.db_pool,
  159. stream_name="test_stream",
  160. instance_name=instance_name,
  161. tables=[("foobar", "instance_name", "stream_id")],
  162. sequence_name="foobar_seq",
  163. writers=writers or ["master"],
  164. )
  165. return self.get_success_or_raise(self.db_pool.runWithConnection(_create))
  166. def _insert_rows(self, instance_name: str, number: int) -> None:
  167. """Insert N rows as the given instance, inserting with stream IDs pulled
  168. from the postgres sequence.
  169. """
  170. def _insert(txn: LoggingTransaction) -> None:
  171. for _ in range(number):
  172. txn.execute(
  173. "INSERT INTO foobar VALUES (nextval('foobar_seq'), ?)",
  174. (instance_name,),
  175. )
  176. txn.execute(
  177. """
  178. INSERT INTO stream_positions VALUES ('test_stream', ?, lastval())
  179. ON CONFLICT (stream_name, instance_name) DO UPDATE SET stream_id = lastval()
  180. """,
  181. (instance_name,),
  182. )
  183. self.get_success(self.db_pool.runInteraction("_insert_rows", _insert))
  184. def _insert_row_with_id(self, instance_name: str, stream_id: int) -> None:
  185. """Insert one row as the given instance with given stream_id, updating
  186. the postgres sequence position to match.
  187. """
  188. def _insert(txn: LoggingTransaction) -> None:
  189. txn.execute(
  190. "INSERT INTO foobar VALUES (?, ?)",
  191. (
  192. stream_id,
  193. instance_name,
  194. ),
  195. )
  196. txn.execute("SELECT setval('foobar_seq', ?)", (stream_id,))
  197. txn.execute(
  198. """
  199. INSERT INTO stream_positions VALUES ('test_stream', ?, ?)
  200. ON CONFLICT (stream_name, instance_name) DO UPDATE SET stream_id = ?
  201. """,
  202. (instance_name, stream_id, stream_id),
  203. )
  204. self.get_success(self.db_pool.runInteraction("_insert_row_with_id", _insert))
  205. def test_empty(self) -> None:
  206. """Test an ID generator against an empty database gives sensible
  207. current positions.
  208. """
  209. id_gen = self._create_id_generator()
  210. # The table is empty so we expect an empty map for positions
  211. self.assertEqual(id_gen.get_positions(), {})
  212. def test_single_instance(self) -> None:
  213. """Test that reads and writes from a single process are handled
  214. correctly.
  215. """
  216. # Prefill table with 7 rows written by 'master'
  217. self._insert_rows("master", 7)
  218. id_gen = self._create_id_generator()
  219. self.assertEqual(id_gen.get_positions(), {"master": 7})
  220. self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
  221. # Try allocating a new ID gen and check that we only see position
  222. # advanced after we leave the context manager.
  223. async def _get_next_async() -> None:
  224. async with id_gen.get_next() as stream_id:
  225. self.assertEqual(stream_id, 8)
  226. self.assertEqual(id_gen.get_positions(), {"master": 7})
  227. self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
  228. self.get_success(_get_next_async())
  229. self.assertEqual(id_gen.get_positions(), {"master": 8})
  230. self.assertEqual(id_gen.get_current_token_for_writer("master"), 8)
  231. def test_out_of_order_finish(self) -> None:
  232. """Test that IDs persisted out of order are correctly handled"""
  233. # Prefill table with 7 rows written by 'master'
  234. self._insert_rows("master", 7)
  235. id_gen = self._create_id_generator()
  236. self.assertEqual(id_gen.get_positions(), {"master": 7})
  237. self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
  238. ctx1 = id_gen.get_next()
  239. ctx2 = id_gen.get_next()
  240. ctx3 = id_gen.get_next()
  241. ctx4 = id_gen.get_next()
  242. s1 = self.get_success(ctx1.__aenter__())
  243. s2 = self.get_success(ctx2.__aenter__())
  244. s3 = self.get_success(ctx3.__aenter__())
  245. s4 = self.get_success(ctx4.__aenter__())
  246. self.assertEqual(s1, 8)
  247. self.assertEqual(s2, 9)
  248. self.assertEqual(s3, 10)
  249. self.assertEqual(s4, 11)
  250. self.assertEqual(id_gen.get_positions(), {"master": 7})
  251. self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
  252. self.get_success(ctx2.__aexit__(None, None, None))
  253. self.assertEqual(id_gen.get_positions(), {"master": 7})
  254. self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
  255. self.get_success(ctx1.__aexit__(None, None, None))
  256. self.assertEqual(id_gen.get_positions(), {"master": 9})
  257. self.assertEqual(id_gen.get_current_token_for_writer("master"), 9)
  258. self.get_success(ctx4.__aexit__(None, None, None))
  259. self.assertEqual(id_gen.get_positions(), {"master": 9})
  260. self.assertEqual(id_gen.get_current_token_for_writer("master"), 9)
  261. self.get_success(ctx3.__aexit__(None, None, None))
  262. self.assertEqual(id_gen.get_positions(), {"master": 11})
  263. self.assertEqual(id_gen.get_current_token_for_writer("master"), 11)
  264. def test_multi_instance(self) -> None:
  265. """Test that reads and writes from multiple processes are handled
  266. correctly.
  267. """
  268. self._insert_rows("first", 3)
  269. self._insert_rows("second", 4)
  270. first_id_gen = self._create_id_generator("first", writers=["first", "second"])
  271. second_id_gen = self._create_id_generator("second", writers=["first", "second"])
  272. # The first ID gen will notice that it can advance its token to 7 as it
  273. # has no in progress writes...
  274. self.assertEqual(first_id_gen.get_positions(), {"first": 7, "second": 7})
  275. self.assertEqual(first_id_gen.get_current_token_for_writer("first"), 7)
  276. self.assertEqual(first_id_gen.get_current_token_for_writer("second"), 7)
  277. # ... but the second ID gen doesn't know that.
  278. self.assertEqual(second_id_gen.get_positions(), {"first": 3, "second": 7})
  279. self.assertEqual(second_id_gen.get_current_token_for_writer("first"), 3)
  280. self.assertEqual(second_id_gen.get_current_token_for_writer("second"), 7)
  281. # Try allocating a new ID gen and check that we only see position
  282. # advanced after we leave the context manager.
  283. async def _get_next_async() -> None:
  284. async with first_id_gen.get_next() as stream_id:
  285. self.assertEqual(stream_id, 8)
  286. self.assertEqual(
  287. first_id_gen.get_positions(), {"first": 7, "second": 7}
  288. )
  289. self.get_success(_get_next_async())
  290. self.assertEqual(first_id_gen.get_positions(), {"first": 8, "second": 7})
  291. # However the ID gen on the second instance won't have seen the update
  292. self.assertEqual(second_id_gen.get_positions(), {"first": 3, "second": 7})
  293. # ... but calling `get_next` on the second instance should give a unique
  294. # stream ID
  295. async def _get_next_async2() -> None:
  296. async with second_id_gen.get_next() as stream_id:
  297. self.assertEqual(stream_id, 9)
  298. self.assertEqual(
  299. second_id_gen.get_positions(), {"first": 3, "second": 7}
  300. )
  301. self.get_success(_get_next_async2())
  302. self.assertEqual(second_id_gen.get_positions(), {"first": 3, "second": 9})
  303. # If the second ID gen gets told about the first, it correctly updates
  304. second_id_gen.advance("first", 8)
  305. self.assertEqual(second_id_gen.get_positions(), {"first": 8, "second": 9})
  306. def test_get_next_txn(self) -> None:
  307. """Test that the `get_next_txn` function works correctly."""
  308. # Prefill table with 7 rows written by 'master'
  309. self._insert_rows("master", 7)
  310. id_gen = self._create_id_generator()
  311. self.assertEqual(id_gen.get_positions(), {"master": 7})
  312. self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
  313. # Try allocating a new ID gen and check that we only see position
  314. # advanced after we leave the context manager.
  315. def _get_next_txn(txn: LoggingTransaction) -> None:
  316. stream_id = id_gen.get_next_txn(txn)
  317. self.assertEqual(stream_id, 8)
  318. self.assertEqual(id_gen.get_positions(), {"master": 7})
  319. self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
  320. self.get_success(self.db_pool.runInteraction("test", _get_next_txn))
  321. self.assertEqual(id_gen.get_positions(), {"master": 8})
  322. self.assertEqual(id_gen.get_current_token_for_writer("master"), 8)
  323. def test_get_persisted_upto_position(self) -> None:
  324. """Test that `get_persisted_upto_position` correctly tracks updates to
  325. positions.
  326. """
  327. # The following tests are a bit cheeky in that we notify about new
  328. # positions via `advance` without *actually* advancing the postgres
  329. # sequence.
  330. self._insert_row_with_id("first", 3)
  331. self._insert_row_with_id("second", 5)
  332. id_gen = self._create_id_generator("worker", writers=["first", "second"])
  333. self.assertEqual(id_gen.get_positions(), {"first": 3, "second": 5})
  334. # Min is 3 and there is a gap between 5, so we expect it to be 3.
  335. self.assertEqual(id_gen.get_persisted_upto_position(), 3)
  336. # We advance "first" straight to 6. Min is now 5 but there is no gap so
  337. # we expect it to be 6
  338. id_gen.advance("first", 6)
  339. self.assertEqual(id_gen.get_persisted_upto_position(), 6)
  340. # No gap, so we expect 7.
  341. id_gen.advance("second", 7)
  342. self.assertEqual(id_gen.get_persisted_upto_position(), 7)
  343. # We haven't seen 8 yet, so we expect 7 still.
  344. id_gen.advance("second", 9)
  345. self.assertEqual(id_gen.get_persisted_upto_position(), 7)
  346. # Now that we've seen 7, 8 and 9 we can got straight to 9.
  347. id_gen.advance("first", 8)
  348. self.assertEqual(id_gen.get_persisted_upto_position(), 9)
  349. # Jump forward with gaps. The minimum is 11, even though we haven't seen
  350. # 10 we know that everything before 11 must be persisted.
  351. id_gen.advance("first", 11)
  352. id_gen.advance("second", 15)
  353. self.assertEqual(id_gen.get_persisted_upto_position(), 11)
  354. def test_get_persisted_upto_position_get_next(self) -> None:
  355. """Test that `get_persisted_upto_position` correctly tracks updates to
  356. positions when `get_next` is called.
  357. """
  358. self._insert_row_with_id("first", 3)
  359. self._insert_row_with_id("second", 5)
  360. id_gen = self._create_id_generator("first", writers=["first", "second"])
  361. self.assertEqual(id_gen.get_positions(), {"first": 5, "second": 5})
  362. self.assertEqual(id_gen.get_persisted_upto_position(), 5)
  363. async def _get_next_async() -> None:
  364. async with id_gen.get_next() as stream_id:
  365. self.assertEqual(stream_id, 6)
  366. self.assertEqual(id_gen.get_persisted_upto_position(), 5)
  367. self.get_success(_get_next_async())
  368. self.assertEqual(id_gen.get_persisted_upto_position(), 6)
  369. # We assume that so long as `get_next` does correctly advance the
  370. # `persisted_upto_position` in this case, then it will be correct in the
  371. # other cases that are tested above (since they'll hit the same code).
  372. def test_restart_during_out_of_order_persistence(self) -> None:
  373. """Test that restarting a process while another process is writing out
  374. of order updates are handled correctly.
  375. """
  376. # Prefill table with 7 rows written by 'master'
  377. self._insert_rows("master", 7)
  378. id_gen = self._create_id_generator()
  379. self.assertEqual(id_gen.get_positions(), {"master": 7})
  380. self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
  381. # Persist two rows at once
  382. ctx1 = id_gen.get_next()
  383. ctx2 = id_gen.get_next()
  384. s1 = self.get_success(ctx1.__aenter__())
  385. s2 = self.get_success(ctx2.__aenter__())
  386. self.assertEqual(s1, 8)
  387. self.assertEqual(s2, 9)
  388. self.assertEqual(id_gen.get_positions(), {"master": 7})
  389. self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
  390. # We finish persisting the second row before restart
  391. self.get_success(ctx2.__aexit__(None, None, None))
  392. # We simulate a restart of another worker by just creating a new ID gen.
  393. id_gen_worker = self._create_id_generator("worker")
  394. # Restarted worker should not see the second persisted row
  395. self.assertEqual(id_gen_worker.get_positions(), {"master": 7})
  396. self.assertEqual(id_gen_worker.get_current_token_for_writer("master"), 7)
  397. # Now if we persist the first row then both instances should jump ahead
  398. # correctly.
  399. self.get_success(ctx1.__aexit__(None, None, None))
  400. self.assertEqual(id_gen.get_positions(), {"master": 9})
  401. id_gen_worker.advance("master", 9)
  402. self.assertEqual(id_gen_worker.get_positions(), {"master": 9})
  403. def test_writer_config_change(self) -> None:
  404. """Test that changing the writer config correctly works."""
  405. self._insert_row_with_id("first", 3)
  406. self._insert_row_with_id("second", 5)
  407. # Initial config has two writers
  408. id_gen = self._create_id_generator("worker", writers=["first", "second"])
  409. self.assertEqual(id_gen.get_persisted_upto_position(), 3)
  410. self.assertEqual(id_gen.get_current_token_for_writer("first"), 3)
  411. self.assertEqual(id_gen.get_current_token_for_writer("second"), 5)
  412. # New config removes one of the configs. Note that if the writer is
  413. # removed from config we assume that it has been shut down and has
  414. # finished persisting, hence why the persisted upto position is 5.
  415. id_gen_2 = self._create_id_generator("second", writers=["second"])
  416. self.assertEqual(id_gen_2.get_persisted_upto_position(), 5)
  417. self.assertEqual(id_gen_2.get_current_token_for_writer("second"), 5)
  418. # This config points to a single, previously unused writer.
  419. id_gen_3 = self._create_id_generator("third", writers=["third"])
  420. self.assertEqual(id_gen_3.get_persisted_upto_position(), 5)
  421. # For new writers we assume their initial position to be the current
  422. # persisted up to position. This stops Synapse from doing a full table
  423. # scan when a new writer comes along.
  424. self.assertEqual(id_gen_3.get_current_token_for_writer("third"), 5)
  425. id_gen_4 = self._create_id_generator("fourth", writers=["third"])
  426. self.assertEqual(id_gen_4.get_current_token_for_writer("third"), 5)
  427. # Check that we get a sane next stream ID with this new config.
  428. async def _get_next_async() -> None:
  429. async with id_gen_3.get_next() as stream_id:
  430. self.assertEqual(stream_id, 6)
  431. self.get_success(_get_next_async())
  432. self.assertEqual(id_gen_3.get_persisted_upto_position(), 6)
  433. # If we add back the old "first" then we shouldn't see the persisted up
  434. # to position revert back to 3.
  435. id_gen_5 = self._create_id_generator("five", writers=["first", "third"])
  436. self.assertEqual(id_gen_5.get_persisted_upto_position(), 6)
  437. self.assertEqual(id_gen_5.get_current_token_for_writer("first"), 6)
  438. self.assertEqual(id_gen_5.get_current_token_for_writer("third"), 6)
  439. def test_sequence_consistency(self) -> None:
  440. """Test that we error out if the table and sequence diverges."""
  441. # Prefill with some rows
  442. self._insert_row_with_id("master", 3)
  443. # Now we add a row *without* updating the stream ID
  444. def _insert(txn: Cursor) -> None:
  445. txn.execute("INSERT INTO foobar VALUES (26, 'master')")
  446. self.get_success(self.db_pool.runInteraction("_insert", _insert))
  447. # Creating the ID gen should error
  448. with self.assertRaises(IncorrectDatabaseSetup):
  449. self._create_id_generator("first")
  450. class BackwardsMultiWriterIdGeneratorTestCase(HomeserverTestCase):
  451. """Tests MultiWriterIdGenerator that produce *negative* stream IDs."""
  452. if not USE_POSTGRES_FOR_TESTS:
  453. skip = "Requires Postgres"
  454. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  455. self.store = hs.get_datastores().main
  456. self.db_pool: DatabasePool = self.store.db_pool
  457. self.get_success(self.db_pool.runInteraction("_setup_db", self._setup_db))
  458. def _setup_db(self, txn: LoggingTransaction) -> None:
  459. txn.execute("CREATE SEQUENCE foobar_seq")
  460. txn.execute(
  461. """
  462. CREATE TABLE foobar (
  463. stream_id BIGINT NOT NULL,
  464. instance_name TEXT NOT NULL,
  465. data TEXT
  466. );
  467. """
  468. )
  469. def _create_id_generator(
  470. self, instance_name: str = "master", writers: Optional[List[str]] = None
  471. ) -> MultiWriterIdGenerator:
  472. def _create(conn: LoggingDatabaseConnection) -> MultiWriterIdGenerator:
  473. return MultiWriterIdGenerator(
  474. conn,
  475. self.db_pool,
  476. stream_name="test_stream",
  477. instance_name=instance_name,
  478. tables=[("foobar", "instance_name", "stream_id")],
  479. sequence_name="foobar_seq",
  480. writers=writers or ["master"],
  481. positive=False,
  482. )
  483. return self.get_success(self.db_pool.runWithConnection(_create))
  484. def _insert_row(self, instance_name: str, stream_id: int) -> None:
  485. """Insert one row as the given instance with given stream_id."""
  486. def _insert(txn: LoggingTransaction) -> None:
  487. txn.execute(
  488. "INSERT INTO foobar VALUES (?, ?)",
  489. (
  490. stream_id,
  491. instance_name,
  492. ),
  493. )
  494. txn.execute(
  495. """
  496. INSERT INTO stream_positions VALUES ('test_stream', ?, ?)
  497. ON CONFLICT (stream_name, instance_name) DO UPDATE SET stream_id = ?
  498. """,
  499. (instance_name, -stream_id, -stream_id),
  500. )
  501. self.get_success(self.db_pool.runInteraction("_insert_row", _insert))
  502. def test_single_instance(self) -> None:
  503. """Test that reads and writes from a single process are handled
  504. correctly.
  505. """
  506. id_gen = self._create_id_generator()
  507. async def _get_next_async() -> None:
  508. async with id_gen.get_next() as stream_id:
  509. self._insert_row("master", stream_id)
  510. self.get_success(_get_next_async())
  511. self.assertEqual(id_gen.get_positions(), {"master": -1})
  512. self.assertEqual(id_gen.get_current_token_for_writer("master"), -1)
  513. self.assertEqual(id_gen.get_persisted_upto_position(), -1)
  514. async def _get_next_async2() -> None:
  515. async with id_gen.get_next_mult(3) as stream_ids:
  516. for stream_id in stream_ids:
  517. self._insert_row("master", stream_id)
  518. self.get_success(_get_next_async2())
  519. self.assertEqual(id_gen.get_positions(), {"master": -4})
  520. self.assertEqual(id_gen.get_current_token_for_writer("master"), -4)
  521. self.assertEqual(id_gen.get_persisted_upto_position(), -4)
  522. # Test loading from DB by creating a second ID gen
  523. second_id_gen = self._create_id_generator()
  524. self.assertEqual(second_id_gen.get_positions(), {"master": -4})
  525. self.assertEqual(second_id_gen.get_current_token_for_writer("master"), -4)
  526. self.assertEqual(second_id_gen.get_persisted_upto_position(), -4)
  527. def test_multiple_instance(self) -> None:
  528. """Tests that having multiple instances that get advanced over
  529. federation works corretly.
  530. """
  531. id_gen_1 = self._create_id_generator("first", writers=["first", "second"])
  532. id_gen_2 = self._create_id_generator("second", writers=["first", "second"])
  533. async def _get_next_async() -> None:
  534. async with id_gen_1.get_next() as stream_id:
  535. self._insert_row("first", stream_id)
  536. id_gen_2.advance("first", stream_id)
  537. self.get_success(_get_next_async())
  538. self.assertEqual(id_gen_1.get_positions(), {"first": -1})
  539. self.assertEqual(id_gen_2.get_positions(), {"first": -1})
  540. self.assertEqual(id_gen_1.get_persisted_upto_position(), -1)
  541. self.assertEqual(id_gen_2.get_persisted_upto_position(), -1)
  542. async def _get_next_async2() -> None:
  543. async with id_gen_2.get_next() as stream_id:
  544. self._insert_row("second", stream_id)
  545. id_gen_1.advance("second", stream_id)
  546. self.get_success(_get_next_async2())
  547. self.assertEqual(id_gen_1.get_positions(), {"first": -2, "second": -2})
  548. self.assertEqual(id_gen_2.get_positions(), {"first": -1, "second": -2})
  549. self.assertEqual(id_gen_1.get_persisted_upto_position(), -2)
  550. self.assertEqual(id_gen_2.get_persisted_upto_position(), -2)
  551. class MultiTableMultiWriterIdGeneratorTestCase(HomeserverTestCase):
  552. if not USE_POSTGRES_FOR_TESTS:
  553. skip = "Requires Postgres"
  554. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  555. self.store = hs.get_datastores().main
  556. self.db_pool: DatabasePool = self.store.db_pool
  557. self.get_success(self.db_pool.runInteraction("_setup_db", self._setup_db))
  558. def _setup_db(self, txn: LoggingTransaction) -> None:
  559. txn.execute("CREATE SEQUENCE foobar_seq")
  560. txn.execute(
  561. """
  562. CREATE TABLE foobar1 (
  563. stream_id BIGINT NOT NULL,
  564. instance_name TEXT NOT NULL,
  565. data TEXT
  566. );
  567. """
  568. )
  569. txn.execute(
  570. """
  571. CREATE TABLE foobar2 (
  572. stream_id BIGINT NOT NULL,
  573. instance_name TEXT NOT NULL,
  574. data TEXT
  575. );
  576. """
  577. )
  578. def _create_id_generator(
  579. self, instance_name: str = "master", writers: Optional[List[str]] = None
  580. ) -> MultiWriterIdGenerator:
  581. def _create(conn: LoggingDatabaseConnection) -> MultiWriterIdGenerator:
  582. return MultiWriterIdGenerator(
  583. conn,
  584. self.db_pool,
  585. stream_name="test_stream",
  586. instance_name=instance_name,
  587. tables=[
  588. ("foobar1", "instance_name", "stream_id"),
  589. ("foobar2", "instance_name", "stream_id"),
  590. ],
  591. sequence_name="foobar_seq",
  592. writers=writers or ["master"],
  593. )
  594. return self.get_success_or_raise(self.db_pool.runWithConnection(_create))
  595. def _insert_rows(
  596. self,
  597. table: str,
  598. instance_name: str,
  599. number: int,
  600. update_stream_table: bool = True,
  601. ) -> None:
  602. """Insert N rows as the given instance, inserting with stream IDs pulled
  603. from the postgres sequence.
  604. """
  605. def _insert(txn: LoggingTransaction) -> None:
  606. for _ in range(number):
  607. txn.execute(
  608. "INSERT INTO %s VALUES (nextval('foobar_seq'), ?)" % (table,),
  609. (instance_name,),
  610. )
  611. if update_stream_table:
  612. txn.execute(
  613. """
  614. INSERT INTO stream_positions VALUES ('test_stream', ?, lastval())
  615. ON CONFLICT (stream_name, instance_name) DO UPDATE SET stream_id = lastval()
  616. """,
  617. (instance_name,),
  618. )
  619. self.get_success(self.db_pool.runInteraction("_insert_rows", _insert))
  620. def test_load_existing_stream(self) -> None:
  621. """Test creating ID gens with multiple tables that have rows from after
  622. the position in `stream_positions` table.
  623. """
  624. self._insert_rows("foobar1", "first", 3)
  625. self._insert_rows("foobar2", "second", 3)
  626. self._insert_rows("foobar2", "second", 1, update_stream_table=False)
  627. first_id_gen = self._create_id_generator("first", writers=["first", "second"])
  628. second_id_gen = self._create_id_generator("second", writers=["first", "second"])
  629. # The first ID gen will notice that it can advance its token to 7 as it
  630. # has no in progress writes...
  631. self.assertEqual(first_id_gen.get_positions(), {"first": 7, "second": 6})
  632. self.assertEqual(first_id_gen.get_current_token_for_writer("first"), 7)
  633. self.assertEqual(first_id_gen.get_current_token_for_writer("second"), 6)
  634. self.assertEqual(first_id_gen.get_persisted_upto_position(), 7)
  635. # ... but the second ID gen doesn't know that.
  636. self.assertEqual(second_id_gen.get_positions(), {"first": 3, "second": 7})
  637. self.assertEqual(second_id_gen.get_current_token_for_writer("first"), 3)
  638. self.assertEqual(second_id_gen.get_current_token_for_writer("second"), 7)
  639. self.assertEqual(first_id_gen.get_persisted_upto_position(), 7)