test_id_generators.py 26 KB

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