Skip to content

Commit

Permalink
Don't use the SequenceGenerator to get the current seq value, inline …
Browse files Browse the repository at this point in the history
…the SQL
  • Loading branch information
sandhose committed Jun 14, 2024
1 parent c0f9ec9 commit 3e9474b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
11 changes: 7 additions & 4 deletions synapse/storage/util/id_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
from synapse.storage.engines import PostgresEngine
from synapse.storage.types import Cursor
from synapse.storage.util.sequence import (
PostgresSequenceGenerator,
build_sequence_generator,
)

Expand Down Expand Up @@ -280,7 +279,7 @@ def __init__(
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)
self._load_current_ids(db_conn, tables, sequence_name)

self._sequence_gen = build_sequence_generator(
db_conn=db_conn,
Expand Down Expand Up @@ -330,6 +329,7 @@ def _load_current_ids(
self,
db_conn: LoggingDatabaseConnection,
tables: List[Tuple[str, str, str]],
sequence_name: str,
) -> None:
cur = db_conn.cursor(txn_name="_load_current_ids")

Expand Down Expand Up @@ -369,9 +369,12 @@ def _load_current_ids(
# date. If we're using Postgres for the sequences, we can just use the current
# sequence value as our own position.
if self._instance_name in self._writers:
if isinstance(self._sequence_gen, PostgresSequenceGenerator):
if isinstance(self._db.engine, PostgresEngine):
cur.execute(f"SELECT last_value FROM {sequence_name}")
row = cur.fetchone()
assert row is not None
self._current_positions[self._instance_name] = (
self._sequence_gen.current_sequence_value(cur)
row[0] * self._return_factor
)

# We set the `_persisted_upto_position` to be the minimum of all current
Expand Down
7 changes: 0 additions & 7 deletions synapse/storage/util/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,6 @@ def get_next_mult_txn(self, txn: Cursor, n: int) -> List[int]:
)
return [i for (i,) in txn]

def current_sequence_value(self, txn: Cursor) -> int:
"""Load the current value of the sequence without bumping it"""
txn.execute(f"SELECT last_value FROM {self._sequence_name}")
row = txn.fetchone()
assert row is not None
return row[0]

def check_consistency(
self,
db_conn: "LoggingDatabaseConnection",
Expand Down

0 comments on commit 3e9474b

Please sign in to comment.