Skip to content

Commit 0c7d991

Browse files
authored
Fix registering of background updates for split main/state db (#18509)
The background updates are being registered on an object that is for the _state_ database, but the actual tables are on the _main_ database. This just moves them to a different store that can access the right stuff. I noticed this when trying to do a full schema dump cause I was curious what has changed since the last one. Fixes #16054 ### Pull Request Checklist <!-- Please read https://element-hq.github.io/synapse/latest/development/contributing_guide.html before submitting your pull request --> * [x] Pull request is based on the develop branch * [x] Pull request includes a [changelog file](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#changelog). The entry should: - Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from `EventStore` to `EventWorkerStore`.". - Use markdown where necessary, mostly for `code blocks`. - End with either a period (.) or an exclamation mark (!). - Start with a capital letter. - Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry. * [x] [Code style](https://element-hq.github.io/synapse/latest/code_style.html) is correct (run the [linters](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#run-the-linters))
1 parent 6fabf82 commit 0c7d991

5 files changed

Lines changed: 33 additions & 32 deletions

File tree

changelog.d/18509.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `KeyError` on background updates when using split main/state databases.

rust/src/identifier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum IdentifierError {
2727

2828
impl fmt::Display for IdentifierError {
2929
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30-
write!(f, "{:?}", self)
30+
write!(f, "{self:?}")
3131
}
3232
}
3333

synapse/storage/databases/main/events_bg_updates.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,27 @@ def __init__(
294294
where_clause="NOT outlier",
295295
)
296296

297+
# These indices are needed to validate the foreign key constraint
298+
# when events are deleted.
299+
self.db_pool.updates.register_background_index_update(
300+
_BackgroundUpdates.CURRENT_STATE_EVENTS_STREAM_ORDERING_INDEX_UPDATE_NAME,
301+
index_name="current_state_events_stream_ordering_idx",
302+
table="current_state_events",
303+
columns=["event_stream_ordering"],
304+
)
305+
self.db_pool.updates.register_background_index_update(
306+
_BackgroundUpdates.ROOM_MEMBERSHIPS_STREAM_ORDERING_INDEX_UPDATE_NAME,
307+
index_name="room_memberships_stream_ordering_idx",
308+
table="room_memberships",
309+
columns=["event_stream_ordering"],
310+
)
311+
self.db_pool.updates.register_background_index_update(
312+
_BackgroundUpdates.LOCAL_CURRENT_MEMBERSHIP_STREAM_ORDERING_INDEX_UPDATE_NAME,
313+
index_name="local_current_membership_stream_ordering_idx",
314+
table="local_current_membership",
315+
columns=["event_stream_ordering"],
316+
)
317+
297318
# Handle background updates for Sliding Sync tables
298319
#
299320
self.db_pool.updates.register_background_update_handler(

synapse/storage/databases/state/bg_updates.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,6 @@ class StateBackgroundUpdateStore(StateGroupBackgroundUpdateStore):
290290
STATE_GROUPS_ROOM_INDEX_UPDATE_NAME = "state_groups_room_id_idx"
291291
STATE_GROUP_EDGES_UNIQUE_INDEX_UPDATE_NAME = "state_group_edges_unique_idx"
292292

293-
CURRENT_STATE_EVENTS_STREAM_ORDERING_INDEX_UPDATE_NAME = (
294-
"current_state_events_stream_ordering_idx"
295-
)
296-
ROOM_MEMBERSHIPS_STREAM_ORDERING_INDEX_UPDATE_NAME = (
297-
"room_memberships_stream_ordering_idx"
298-
)
299-
LOCAL_CURRENT_MEMBERSHIP_STREAM_ORDERING_INDEX_UPDATE_NAME = (
300-
"local_current_membership_stream_ordering_idx"
301-
)
302-
303293
def __init__(
304294
self,
305295
database: DatabasePool,
@@ -336,27 +326,6 @@ def __init__(
336326
replaces_index="state_group_edges_idx",
337327
)
338328

339-
# These indices are needed to validate the foreign key constraint
340-
# when events are deleted.
341-
self.db_pool.updates.register_background_index_update(
342-
self.CURRENT_STATE_EVENTS_STREAM_ORDERING_INDEX_UPDATE_NAME,
343-
index_name="current_state_events_stream_ordering_idx",
344-
table="current_state_events",
345-
columns=["event_stream_ordering"],
346-
)
347-
self.db_pool.updates.register_background_index_update(
348-
self.ROOM_MEMBERSHIPS_STREAM_ORDERING_INDEX_UPDATE_NAME,
349-
index_name="room_memberships_stream_ordering_idx",
350-
table="room_memberships",
351-
columns=["event_stream_ordering"],
352-
)
353-
self.db_pool.updates.register_background_index_update(
354-
self.LOCAL_CURRENT_MEMBERSHIP_STREAM_ORDERING_INDEX_UPDATE_NAME,
355-
index_name="local_current_membership_stream_ordering_idx",
356-
table="local_current_membership",
357-
columns=["event_stream_ordering"],
358-
)
359-
360329
async def _background_deduplicate_state(
361330
self, progress: dict, batch_size: int
362331
) -> int:

synapse/types/storage/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ class _BackgroundUpdates:
3838

3939
EVENTS_JUMP_TO_DATE_INDEX = "events_jump_to_date_index"
4040

41+
CURRENT_STATE_EVENTS_STREAM_ORDERING_INDEX_UPDATE_NAME = (
42+
"current_state_events_stream_ordering_idx"
43+
)
44+
ROOM_MEMBERSHIPS_STREAM_ORDERING_INDEX_UPDATE_NAME = (
45+
"room_memberships_stream_ordering_idx"
46+
)
47+
LOCAL_CURRENT_MEMBERSHIP_STREAM_ORDERING_INDEX_UPDATE_NAME = (
48+
"local_current_membership_stream_ordering_idx"
49+
)
50+
4151
SLIDING_SYNC_PREFILL_JOINED_ROOMS_TO_RECALCULATE_TABLE_BG_UPDATE = (
4252
"sliding_sync_prefill_joined_rooms_to_recalculate_table_bg_update"
4353
)

0 commit comments

Comments
 (0)