Skip to content

Commit 458e641

Browse files
Reduce database usage in Sliding Sync by not querying for background update completion after the update is known to be complete. (#18718)
Signed-off-by: Olivier 'reivilibre <[email protected]> Co-authored-by: Eric Eastwood <[email protected]>
1 parent 1dd5f68 commit 458e641

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

changelog.d/18718.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reduce database usage in Sliding Sync by not querying for background update completion after the update is known to be complete.

synapse/storage/databases/main/events_worker.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ def get_chain_id_txn(txn: Cursor) -> int:
367367
replaces_index="event_txn_id_device_id_txn_id",
368368
)
369369

370+
self._has_finished_sliding_sync_background_jobs = False
371+
"""
372+
Flag to track when the sliding sync background jobs have
373+
finished (so we don't have to keep querying it every time)
374+
"""
375+
370376
def get_un_partial_stated_events_token(self, instance_name: str) -> int:
371377
return (
372378
self._un_partial_stated_events_stream_id_gen.get_current_token_for_writer(
@@ -2658,13 +2664,19 @@ def _get_events_by_user_in_room_txn(
26582664
async def have_finished_sliding_sync_background_jobs(self) -> bool:
26592665
"""Return if it's safe to use the sliding sync membership tables."""
26602666

2661-
return await self.db_pool.updates.have_completed_background_updates(
2667+
if self._has_finished_sliding_sync_background_jobs:
2668+
# as an optimisation, once the job finishes, don't issue another
2669+
# database transaction to check it, since it won't 'un-finish'
2670+
return True
2671+
2672+
self._has_finished_sliding_sync_background_jobs = await self.db_pool.updates.have_completed_background_updates(
26622673
(
26632674
_BackgroundUpdates.SLIDING_SYNC_PREFILL_JOINED_ROOMS_TO_RECALCULATE_TABLE_BG_UPDATE,
26642675
_BackgroundUpdates.SLIDING_SYNC_JOINED_ROOMS_BG_UPDATE,
26652676
_BackgroundUpdates.SLIDING_SYNC_MEMBERSHIP_SNAPSHOTS_BG_UPDATE,
26662677
)
26672678
)
2679+
return self._has_finished_sliding_sync_background_jobs
26682680

26692681
async def get_sent_invite_count_by_user(self, user_id: str, from_ts: int) -> int:
26702682
"""

0 commit comments

Comments
 (0)