123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- # -*- coding: utf-8 -*-
- # Copyright 2020 The Matrix.org Foundation C.I.C.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- from synapse.storage.database import DatabasePool
- from synapse.storage.engines import IncorrectDatabaseSetup
- from synapse.storage.util.id_generators import MultiWriterIdGenerator
- from tests.unittest import HomeserverTestCase
- from tests.utils import USE_POSTGRES_FOR_TESTS
- class MultiWriterIdGeneratorTestCase(HomeserverTestCase):
- if not USE_POSTGRES_FOR_TESTS:
- skip = "Requires Postgres"
- def prepare(self, reactor, clock, hs):
- self.store = hs.get_datastore()
- self.db_pool = self.store.db_pool # type: DatabasePool
- self.get_success(self.db_pool.runInteraction("_setup_db", self._setup_db))
- def _setup_db(self, txn):
- txn.execute("CREATE SEQUENCE foobar_seq")
- txn.execute(
- """
- CREATE TABLE foobar (
- stream_id BIGINT NOT NULL,
- instance_name TEXT NOT NULL,
- data TEXT
- );
- """
- )
- def _create_id_generator(
- self, instance_name="master", writers=["master"]
- ) -> MultiWriterIdGenerator:
- def _create(conn):
- return MultiWriterIdGenerator(
- conn,
- self.db_pool,
- stream_name="test_stream",
- instance_name=instance_name,
- tables=[("foobar", "instance_name", "stream_id")],
- sequence_name="foobar_seq",
- writers=writers,
- )
- return self.get_success_or_raise(self.db_pool.runWithConnection(_create))
- def _insert_rows(self, instance_name: str, number: int):
- """Insert N rows as the given instance, inserting with stream IDs pulled
- from the postgres sequence.
- """
- def _insert(txn):
- for _ in range(number):
- txn.execute(
- "INSERT INTO foobar VALUES (nextval('foobar_seq'), ?)",
- (instance_name,),
- )
- txn.execute(
- """
- INSERT INTO stream_positions VALUES ('test_stream', ?, lastval())
- ON CONFLICT (stream_name, instance_name) DO UPDATE SET stream_id = lastval()
- """,
- (instance_name,),
- )
- self.get_success(self.db_pool.runInteraction("_insert_rows", _insert))
- def _insert_row_with_id(self, instance_name: str, stream_id: int):
- """Insert one row as the given instance with given stream_id, updating
- the postgres sequence position to match.
- """
- def _insert(txn):
- txn.execute(
- "INSERT INTO foobar VALUES (?, ?)", (stream_id, instance_name,),
- )
- txn.execute("SELECT setval('foobar_seq', ?)", (stream_id,))
- txn.execute(
- """
- INSERT INTO stream_positions VALUES ('test_stream', ?, ?)
- ON CONFLICT (stream_name, instance_name) DO UPDATE SET stream_id = ?
- """,
- (instance_name, stream_id, stream_id),
- )
- self.get_success(self.db_pool.runInteraction("_insert_row_with_id", _insert))
- def test_empty(self):
- """Test an ID generator against an empty database gives sensible
- current positions.
- """
- id_gen = self._create_id_generator()
- # The table is empty so we expect an empty map for positions
- self.assertEqual(id_gen.get_positions(), {})
- def test_single_instance(self):
- """Test that reads and writes from a single process are handled
- correctly.
- """
- # Prefill table with 7 rows written by 'master'
- self._insert_rows("master", 7)
- id_gen = self._create_id_generator()
- self.assertEqual(id_gen.get_positions(), {"master": 7})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
- # Try allocating a new ID gen and check that we only see position
- # advanced after we leave the context manager.
- async def _get_next_async():
- async with id_gen.get_next() as stream_id:
- self.assertEqual(stream_id, 8)
- self.assertEqual(id_gen.get_positions(), {"master": 7})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
- self.get_success(_get_next_async())
- self.assertEqual(id_gen.get_positions(), {"master": 8})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 8)
- def test_out_of_order_finish(self):
- """Test that IDs persisted out of order are correctly handled
- """
- # Prefill table with 7 rows written by 'master'
- self._insert_rows("master", 7)
- id_gen = self._create_id_generator()
- self.assertEqual(id_gen.get_positions(), {"master": 7})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
- ctx1 = self.get_success(id_gen.get_next())
- ctx2 = self.get_success(id_gen.get_next())
- ctx3 = self.get_success(id_gen.get_next())
- ctx4 = self.get_success(id_gen.get_next())
- s1 = self.get_success(ctx1.__aenter__())
- s2 = self.get_success(ctx2.__aenter__())
- s3 = self.get_success(ctx3.__aenter__())
- s4 = self.get_success(ctx4.__aenter__())
- self.assertEqual(s1, 8)
- self.assertEqual(s2, 9)
- self.assertEqual(s3, 10)
- self.assertEqual(s4, 11)
- self.assertEqual(id_gen.get_positions(), {"master": 7})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
- self.get_success(ctx2.__aexit__(None, None, None))
- self.assertEqual(id_gen.get_positions(), {"master": 7})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
- self.get_success(ctx1.__aexit__(None, None, None))
- self.assertEqual(id_gen.get_positions(), {"master": 9})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 9)
- self.get_success(ctx4.__aexit__(None, None, None))
- self.assertEqual(id_gen.get_positions(), {"master": 9})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 9)
- self.get_success(ctx3.__aexit__(None, None, None))
- self.assertEqual(id_gen.get_positions(), {"master": 11})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 11)
- def test_multi_instance(self):
- """Test that reads and writes from multiple processes are handled
- correctly.
- """
- self._insert_rows("first", 3)
- self._insert_rows("second", 4)
- first_id_gen = self._create_id_generator("first", writers=["first", "second"])
- second_id_gen = self._create_id_generator("second", writers=["first", "second"])
- # The first ID gen will notice that it can advance its token to 7 as it
- # has no in progress writes...
- self.assertEqual(first_id_gen.get_positions(), {"first": 7, "second": 7})
- self.assertEqual(first_id_gen.get_current_token_for_writer("first"), 7)
- self.assertEqual(first_id_gen.get_current_token_for_writer("second"), 7)
- # ... but the second ID gen doesn't know that.
- self.assertEqual(second_id_gen.get_positions(), {"first": 3, "second": 7})
- self.assertEqual(second_id_gen.get_current_token_for_writer("first"), 3)
- self.assertEqual(second_id_gen.get_current_token_for_writer("second"), 7)
- # Try allocating a new ID gen and check that we only see position
- # advanced after we leave the context manager.
- async def _get_next_async():
- async with first_id_gen.get_next() as stream_id:
- self.assertEqual(stream_id, 8)
- self.assertEqual(
- first_id_gen.get_positions(), {"first": 7, "second": 7}
- )
- self.get_success(_get_next_async())
- self.assertEqual(first_id_gen.get_positions(), {"first": 8, "second": 7})
- # However the ID gen on the second instance won't have seen the update
- self.assertEqual(second_id_gen.get_positions(), {"first": 3, "second": 7})
- # ... but calling `get_next` on the second instance should give a unique
- # stream ID
- async def _get_next_async():
- async with second_id_gen.get_next() as stream_id:
- self.assertEqual(stream_id, 9)
- self.assertEqual(
- second_id_gen.get_positions(), {"first": 3, "second": 7}
- )
- self.get_success(_get_next_async())
- self.assertEqual(second_id_gen.get_positions(), {"first": 3, "second": 9})
- # If the second ID gen gets told about the first, it correctly updates
- second_id_gen.advance("first", 8)
- self.assertEqual(second_id_gen.get_positions(), {"first": 8, "second": 9})
- def test_get_next_txn(self):
- """Test that the `get_next_txn` function works correctly.
- """
- # Prefill table with 7 rows written by 'master'
- self._insert_rows("master", 7)
- id_gen = self._create_id_generator()
- self.assertEqual(id_gen.get_positions(), {"master": 7})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
- # Try allocating a new ID gen and check that we only see position
- # advanced after we leave the context manager.
- def _get_next_txn(txn):
- stream_id = id_gen.get_next_txn(txn)
- self.assertEqual(stream_id, 8)
- self.assertEqual(id_gen.get_positions(), {"master": 7})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
- self.get_success(self.db_pool.runInteraction("test", _get_next_txn))
- self.assertEqual(id_gen.get_positions(), {"master": 8})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 8)
- def test_get_persisted_upto_position(self):
- """Test that `get_persisted_upto_position` correctly tracks updates to
- positions.
- """
- # The following tests are a bit cheeky in that we notify about new
- # positions via `advance` without *actually* advancing the postgres
- # sequence.
- self._insert_row_with_id("first", 3)
- self._insert_row_with_id("second", 5)
- id_gen = self._create_id_generator("worker", writers=["first", "second"])
- self.assertEqual(id_gen.get_positions(), {"first": 3, "second": 5})
- # Min is 3 and there is a gap between 5, so we expect it to be 3.
- self.assertEqual(id_gen.get_persisted_upto_position(), 3)
- # We advance "first" straight to 6. Min is now 5 but there is no gap so
- # we expect it to be 6
- id_gen.advance("first", 6)
- self.assertEqual(id_gen.get_persisted_upto_position(), 6)
- # No gap, so we expect 7.
- id_gen.advance("second", 7)
- self.assertEqual(id_gen.get_persisted_upto_position(), 7)
- # We haven't seen 8 yet, so we expect 7 still.
- id_gen.advance("second", 9)
- self.assertEqual(id_gen.get_persisted_upto_position(), 7)
- # Now that we've seen 7, 8 and 9 we can got straight to 9.
- id_gen.advance("first", 8)
- self.assertEqual(id_gen.get_persisted_upto_position(), 9)
- # Jump forward with gaps. The minimum is 11, even though we haven't seen
- # 10 we know that everything before 11 must be persisted.
- id_gen.advance("first", 11)
- id_gen.advance("second", 15)
- self.assertEqual(id_gen.get_persisted_upto_position(), 11)
- def test_get_persisted_upto_position_get_next(self):
- """Test that `get_persisted_upto_position` correctly tracks updates to
- positions when `get_next` is called.
- """
- self._insert_row_with_id("first", 3)
- self._insert_row_with_id("second", 5)
- id_gen = self._create_id_generator("first", writers=["first", "second"])
- self.assertEqual(id_gen.get_positions(), {"first": 5, "second": 5})
- self.assertEqual(id_gen.get_persisted_upto_position(), 5)
- async def _get_next_async():
- async with id_gen.get_next() as stream_id:
- self.assertEqual(stream_id, 6)
- self.assertEqual(id_gen.get_persisted_upto_position(), 5)
- self.get_success(_get_next_async())
- self.assertEqual(id_gen.get_persisted_upto_position(), 6)
- # We assume that so long as `get_next` does correctly advance the
- # `persisted_upto_position` in this case, then it will be correct in the
- # other cases that are tested above (since they'll hit the same code).
- def test_restart_during_out_of_order_persistence(self):
- """Test that restarting a process while another process is writing out
- of order updates are handled correctly.
- """
- # Prefill table with 7 rows written by 'master'
- self._insert_rows("master", 7)
- id_gen = self._create_id_generator()
- self.assertEqual(id_gen.get_positions(), {"master": 7})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
- # Persist two rows at once
- ctx1 = self.get_success(id_gen.get_next())
- ctx2 = self.get_success(id_gen.get_next())
- s1 = self.get_success(ctx1.__aenter__())
- s2 = self.get_success(ctx2.__aenter__())
- self.assertEqual(s1, 8)
- self.assertEqual(s2, 9)
- self.assertEqual(id_gen.get_positions(), {"master": 7})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)
- # We finish persisting the second row before restart
- self.get_success(ctx2.__aexit__(None, None, None))
- # We simulate a restart of another worker by just creating a new ID gen.
- id_gen_worker = self._create_id_generator("worker")
- # Restarted worker should not see the second persisted row
- self.assertEqual(id_gen_worker.get_positions(), {"master": 7})
- self.assertEqual(id_gen_worker.get_current_token_for_writer("master"), 7)
- # Now if we persist the first row then both instances should jump ahead
- # correctly.
- self.get_success(ctx1.__aexit__(None, None, None))
- self.assertEqual(id_gen.get_positions(), {"master": 9})
- id_gen_worker.advance("master", 9)
- self.assertEqual(id_gen_worker.get_positions(), {"master": 9})
- def test_writer_config_change(self):
- """Test that changing the writer config correctly works.
- """
- self._insert_row_with_id("first", 3)
- self._insert_row_with_id("second", 5)
- # Initial config has two writers
- id_gen = self._create_id_generator("worker", writers=["first", "second"])
- self.assertEqual(id_gen.get_persisted_upto_position(), 3)
- self.assertEqual(id_gen.get_current_token_for_writer("first"), 3)
- self.assertEqual(id_gen.get_current_token_for_writer("second"), 5)
- # New config removes one of the configs. Note that if the writer is
- # removed from config we assume that it has been shut down and has
- # finished persisting, hence why the persisted upto position is 5.
- id_gen_2 = self._create_id_generator("second", writers=["second"])
- self.assertEqual(id_gen_2.get_persisted_upto_position(), 5)
- self.assertEqual(id_gen_2.get_current_token_for_writer("second"), 5)
- # This config points to a single, previously unused writer.
- id_gen_3 = self._create_id_generator("third", writers=["third"])
- self.assertEqual(id_gen_3.get_persisted_upto_position(), 5)
- # For new writers we assume their initial position to be the current
- # persisted up to position. This stops Synapse from doing a full table
- # scan when a new writer comes along.
- self.assertEqual(id_gen_3.get_current_token_for_writer("third"), 5)
- id_gen_4 = self._create_id_generator("fourth", writers=["third"])
- self.assertEqual(id_gen_4.get_current_token_for_writer("third"), 5)
- # Check that we get a sane next stream ID with this new config.
- async def _get_next_async():
- async with id_gen_3.get_next() as stream_id:
- self.assertEqual(stream_id, 6)
- self.get_success(_get_next_async())
- self.assertEqual(id_gen_3.get_persisted_upto_position(), 6)
- # If we add back the old "first" then we shouldn't see the persisted up
- # to position revert back to 3.
- id_gen_5 = self._create_id_generator("five", writers=["first", "third"])
- self.assertEqual(id_gen_5.get_persisted_upto_position(), 6)
- self.assertEqual(id_gen_5.get_current_token_for_writer("first"), 6)
- self.assertEqual(id_gen_5.get_current_token_for_writer("third"), 6)
- def test_sequence_consistency(self):
- """Test that we error out if the table and sequence diverges.
- """
- # Prefill with some rows
- self._insert_row_with_id("master", 3)
- # Now we add a row *without* updating the stream ID
- def _insert(txn):
- txn.execute("INSERT INTO foobar VALUES (26, 'master')")
- self.get_success(self.db_pool.runInteraction("_insert", _insert))
- # Creating the ID gen should error
- with self.assertRaises(IncorrectDatabaseSetup):
- self._create_id_generator("first")
- class BackwardsMultiWriterIdGeneratorTestCase(HomeserverTestCase):
- """Tests MultiWriterIdGenerator that produce *negative* stream IDs.
- """
- if not USE_POSTGRES_FOR_TESTS:
- skip = "Requires Postgres"
- def prepare(self, reactor, clock, hs):
- self.store = hs.get_datastore()
- self.db_pool = self.store.db_pool # type: DatabasePool
- self.get_success(self.db_pool.runInteraction("_setup_db", self._setup_db))
- def _setup_db(self, txn):
- txn.execute("CREATE SEQUENCE foobar_seq")
- txn.execute(
- """
- CREATE TABLE foobar (
- stream_id BIGINT NOT NULL,
- instance_name TEXT NOT NULL,
- data TEXT
- );
- """
- )
- def _create_id_generator(
- self, instance_name="master", writers=["master"]
- ) -> MultiWriterIdGenerator:
- def _create(conn):
- return MultiWriterIdGenerator(
- conn,
- self.db_pool,
- stream_name="test_stream",
- instance_name=instance_name,
- tables=[("foobar", "instance_name", "stream_id")],
- sequence_name="foobar_seq",
- writers=writers,
- positive=False,
- )
- return self.get_success(self.db_pool.runWithConnection(_create))
- def _insert_row(self, instance_name: str, stream_id: int):
- """Insert one row as the given instance with given stream_id.
- """
- def _insert(txn):
- txn.execute(
- "INSERT INTO foobar VALUES (?, ?)", (stream_id, instance_name,),
- )
- txn.execute(
- """
- INSERT INTO stream_positions VALUES ('test_stream', ?, ?)
- ON CONFLICT (stream_name, instance_name) DO UPDATE SET stream_id = ?
- """,
- (instance_name, -stream_id, -stream_id),
- )
- self.get_success(self.db_pool.runInteraction("_insert_row", _insert))
- def test_single_instance(self):
- """Test that reads and writes from a single process are handled
- correctly.
- """
- id_gen = self._create_id_generator()
- async def _get_next_async():
- async with id_gen.get_next() as stream_id:
- self._insert_row("master", stream_id)
- self.get_success(_get_next_async())
- self.assertEqual(id_gen.get_positions(), {"master": -1})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), -1)
- self.assertEqual(id_gen.get_persisted_upto_position(), -1)
- async def _get_next_async2():
- async with id_gen.get_next_mult(3) as stream_ids:
- for stream_id in stream_ids:
- self._insert_row("master", stream_id)
- self.get_success(_get_next_async2())
- self.assertEqual(id_gen.get_positions(), {"master": -4})
- self.assertEqual(id_gen.get_current_token_for_writer("master"), -4)
- self.assertEqual(id_gen.get_persisted_upto_position(), -4)
- # Test loading from DB by creating a second ID gen
- second_id_gen = self._create_id_generator()
- self.assertEqual(second_id_gen.get_positions(), {"master": -4})
- self.assertEqual(second_id_gen.get_current_token_for_writer("master"), -4)
- self.assertEqual(second_id_gen.get_persisted_upto_position(), -4)
- def test_multiple_instance(self):
- """Tests that having multiple instances that get advanced over
- federation works corretly.
- """
- id_gen_1 = self._create_id_generator("first", writers=["first", "second"])
- id_gen_2 = self._create_id_generator("second", writers=["first", "second"])
- async def _get_next_async():
- async with id_gen_1.get_next() as stream_id:
- self._insert_row("first", stream_id)
- id_gen_2.advance("first", stream_id)
- self.get_success(_get_next_async())
- self.assertEqual(id_gen_1.get_positions(), {"first": -1})
- self.assertEqual(id_gen_2.get_positions(), {"first": -1})
- self.assertEqual(id_gen_1.get_persisted_upto_position(), -1)
- self.assertEqual(id_gen_2.get_persisted_upto_position(), -1)
- async def _get_next_async2():
- async with id_gen_2.get_next() as stream_id:
- self._insert_row("second", stream_id)
- id_gen_1.advance("second", stream_id)
- self.get_success(_get_next_async2())
- self.assertEqual(id_gen_1.get_positions(), {"first": -2, "second": -2})
- self.assertEqual(id_gen_2.get_positions(), {"first": -1, "second": -2})
- self.assertEqual(id_gen_1.get_persisted_upto_position(), -2)
- self.assertEqual(id_gen_2.get_persisted_upto_position(), -2)
- class MultiTableMultiWriterIdGeneratorTestCase(HomeserverTestCase):
- if not USE_POSTGRES_FOR_TESTS:
- skip = "Requires Postgres"
- def prepare(self, reactor, clock, hs):
- self.store = hs.get_datastore()
- self.db_pool = self.store.db_pool # type: DatabasePool
- self.get_success(self.db_pool.runInteraction("_setup_db", self._setup_db))
- def _setup_db(self, txn):
- txn.execute("CREATE SEQUENCE foobar_seq")
- txn.execute(
- """
- CREATE TABLE foobar1 (
- stream_id BIGINT NOT NULL,
- instance_name TEXT NOT NULL,
- data TEXT
- );
- """
- )
- txn.execute(
- """
- CREATE TABLE foobar2 (
- stream_id BIGINT NOT NULL,
- instance_name TEXT NOT NULL,
- data TEXT
- );
- """
- )
- def _create_id_generator(
- self, instance_name="master", writers=["master"]
- ) -> MultiWriterIdGenerator:
- def _create(conn):
- return MultiWriterIdGenerator(
- conn,
- self.db_pool,
- stream_name="test_stream",
- instance_name=instance_name,
- tables=[
- ("foobar1", "instance_name", "stream_id"),
- ("foobar2", "instance_name", "stream_id"),
- ],
- sequence_name="foobar_seq",
- writers=writers,
- )
- return self.get_success_or_raise(self.db_pool.runWithConnection(_create))
- def _insert_rows(
- self,
- table: str,
- instance_name: str,
- number: int,
- update_stream_table: bool = True,
- ):
- """Insert N rows as the given instance, inserting with stream IDs pulled
- from the postgres sequence.
- """
- def _insert(txn):
- for _ in range(number):
- txn.execute(
- "INSERT INTO %s VALUES (nextval('foobar_seq'), ?)" % (table,),
- (instance_name,),
- )
- if update_stream_table:
- txn.execute(
- """
- INSERT INTO stream_positions VALUES ('test_stream', ?, lastval())
- ON CONFLICT (stream_name, instance_name) DO UPDATE SET stream_id = lastval()
- """,
- (instance_name,),
- )
- self.get_success(self.db_pool.runInteraction("_insert_rows", _insert))
- def test_load_existing_stream(self):
- """Test creating ID gens with multiple tables that have rows from after
- the position in `stream_positions` table.
- """
- self._insert_rows("foobar1", "first", 3)
- self._insert_rows("foobar2", "second", 3)
- self._insert_rows("foobar2", "second", 1, update_stream_table=False)
- first_id_gen = self._create_id_generator("first", writers=["first", "second"])
- second_id_gen = self._create_id_generator("second", writers=["first", "second"])
- # The first ID gen will notice that it can advance its token to 7 as it
- # has no in progress writes...
- self.assertEqual(first_id_gen.get_positions(), {"first": 7, "second": 6})
- self.assertEqual(first_id_gen.get_current_token_for_writer("first"), 7)
- self.assertEqual(first_id_gen.get_current_token_for_writer("second"), 6)
- self.assertEqual(first_id_gen.get_persisted_upto_position(), 7)
- # ... but the second ID gen doesn't know that.
- self.assertEqual(second_id_gen.get_positions(), {"first": 3, "second": 7})
- self.assertEqual(second_id_gen.get_current_token_for_writer("first"), 3)
- self.assertEqual(second_id_gen.get_current_token_for_writer("second"), 7)
- self.assertEqual(first_id_gen.get_persisted_upto_position(), 7)
|