Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 5649b7f

Browse files
authored
Fix missing _add_persisted_position (#8179)
This was forgotten in #8164.
1 parent 30426c7 commit 5649b7f

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

changelog.d/8179.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add functions to `MultiWriterIdGen` used by events stream.

synapse/storage/util/id_generators.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ def _mark_id_as_finished(self, next_id: int):
343343
curr = self._current_positions.get(self._instance_name, 0)
344344
self._current_positions[self._instance_name] = max(curr, next_id)
345345

346+
self._add_persisted_position(next_id)
347+
346348
def get_current_token(self) -> int:
347349
"""Returns the maximum stream id such that all stream ids less than or
348350
equal to it have been successfully persisted.

tests/storage/test_id_generators.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,31 @@ def _create(conn):
5858
return self.get_success(self.db_pool.runWithConnection(_create))
5959

6060
def _insert_rows(self, instance_name: str, number: int):
61+
"""Insert N rows as the given instance, inserting with stream IDs pulled
62+
from the postgres sequence.
63+
"""
64+
6165
def _insert(txn):
6266
for _ in range(number):
6367
txn.execute(
6468
"INSERT INTO foobar VALUES (nextval('foobar_seq'), ?)",
6569
(instance_name,),
6670
)
6771

68-
self.get_success(self.db_pool.runInteraction("test_single_instance", _insert))
72+
self.get_success(self.db_pool.runInteraction("_insert_rows", _insert))
73+
74+
def _insert_row_with_id(self, instance_name: str, stream_id: int):
75+
"""Insert one row as the given instance with given stream_id, updating
76+
the postgres sequence position to match.
77+
"""
78+
79+
def _insert(txn):
80+
txn.execute(
81+
"INSERT INTO foobar VALUES (?, ?)", (stream_id, instance_name,),
82+
)
83+
txn.execute("SELECT setval('foobar_seq', ?)", (stream_id,))
84+
85+
self.get_success(self.db_pool.runInteraction("_insert_row_with_id", _insert))
6986

7087
def test_empty(self):
7188
"""Test an ID generator against an empty database gives sensible
@@ -188,11 +205,17 @@ def test_get_persisted_upto_position(self):
188205
positions.
189206
"""
190207

191-
self._insert_rows("first", 3)
192-
self._insert_rows("second", 5)
208+
# The following tests are a bit cheeky in that we notify about new
209+
# positions via `advance` without *actually* advancing the postgres
210+
# sequence.
211+
212+
self._insert_row_with_id("first", 3)
213+
self._insert_row_with_id("second", 5)
193214

194215
id_gen = self._create_id_generator("first")
195216

217+
self.assertEqual(id_gen.get_positions(), {"first": 3, "second": 5})
218+
196219
# Min is 3 and there is a gap between 5, so we expect it to be 3.
197220
self.assertEqual(id_gen.get_persisted_upto_position(), 3)
198221

@@ -218,3 +241,26 @@ def test_get_persisted_upto_position(self):
218241
id_gen.advance("first", 11)
219242
id_gen.advance("second", 15)
220243
self.assertEqual(id_gen.get_persisted_upto_position(), 11)
244+
245+
def test_get_persisted_upto_position_get_next(self):
246+
"""Test that `get_persisted_upto_position` correctly tracks updates to
247+
positions when `get_next` is called.
248+
"""
249+
250+
self._insert_row_with_id("first", 3)
251+
self._insert_row_with_id("second", 5)
252+
253+
id_gen = self._create_id_generator("first")
254+
255+
self.assertEqual(id_gen.get_positions(), {"first": 3, "second": 5})
256+
257+
self.assertEqual(id_gen.get_persisted_upto_position(), 3)
258+
with self.get_success(id_gen.get_next()) as stream_id:
259+
self.assertEqual(stream_id, 6)
260+
self.assertEqual(id_gen.get_persisted_upto_position(), 3)
261+
262+
self.assertEqual(id_gen.get_persisted_upto_position(), 6)
263+
264+
# We assume that so long as `get_next` does correctly advance the
265+
# `persisted_upto_position` in this case, then it will be correct in the
266+
# other cases that are tested above (since they'll hit the same code).

0 commit comments

Comments
 (0)