From 17789170089c85fa32ed3223d2c51b2e88c1ca40 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Mon, 17 Jun 2024 11:54:46 +0200 Subject: [PATCH] Make sure we load after the sequence has been fixed up --- synapse/storage/util/id_generators.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/synapse/storage/util/id_generators.py b/synapse/storage/util/id_generators.py index 24c3676d52c..f1e27298883 100644 --- a/synapse/storage/util/id_generators.py +++ b/synapse/storage/util/id_generators.py @@ -278,9 +278,6 @@ def __init__( # no active writes in progress. self._max_position_of_local_instance = self._max_seen_allocated_stream_id - # This goes and fills out the above state from the database. - self._load_current_ids(db_conn, tables, sequence_name) - self._sequence_gen = build_sequence_generator( db_conn=db_conn, database_engine=db.engine, @@ -305,6 +302,13 @@ def __init__( positive=positive, ) + # This goes and fills out the above state from the database. + # This may read on the PostgreSQL sequence, and + # SequenceGenerator.check_consistency might have fixed up the sequence, which + # means the SequenceGenerator needs to be setup before we read the value from + # the sequence. + self._load_current_ids(db_conn, tables, sequence_name) + self._max_seen_allocated_stream_id = max( self._current_positions.values(), default=1 ) @@ -373,9 +377,7 @@ def _load_current_ids( cur.execute(f"SELECT last_value FROM {sequence_name}") row = cur.fetchone() assert row is not None - self._current_positions[self._instance_name] = ( - row[0] * self._return_factor - ) + self._current_positions[self._instance_name] = row[0] # We set the `_persisted_upto_position` to be the minimum of all current # positions. If empty we use the max stream ID from the DB table.