From 458e6410e8388fbc6b60b5575850405939bc53eb Mon Sep 17 00:00:00 2001 From: reivilibre Date: Thu, 24 Jul 2025 15:58:39 +0100 Subject: [PATCH] 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 Co-authored-by: Eric Eastwood --- changelog.d/18718.misc | 1 + synapse/storage/databases/main/events_worker.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelog.d/18718.misc diff --git a/changelog.d/18718.misc b/changelog.d/18718.misc new file mode 100644 index 000000000..609e05b12 --- /dev/null +++ b/changelog.d/18718.misc @@ -0,0 +1 @@ +Reduce database usage in Sliding Sync by not querying for background update completion after the update is known to be complete. \ No newline at end of file diff --git a/synapse/storage/databases/main/events_worker.py b/synapse/storage/databases/main/events_worker.py index 562097165..e067b53d9 100644 --- a/synapse/storage/databases/main/events_worker.py +++ b/synapse/storage/databases/main/events_worker.py @@ -367,6 +367,12 @@ class EventsWorkerStore(SQLBaseStore): replaces_index="event_txn_id_device_id_txn_id", ) + self._has_finished_sliding_sync_background_jobs = False + """ + Flag to track when the sliding sync background jobs have + finished (so we don't have to keep querying it every time) + """ + def get_un_partial_stated_events_token(self, instance_name: str) -> int: return ( self._un_partial_stated_events_stream_id_gen.get_current_token_for_writer( @@ -2658,13 +2664,19 @@ class EventsWorkerStore(SQLBaseStore): async def have_finished_sliding_sync_background_jobs(self) -> bool: """Return if it's safe to use the sliding sync membership tables.""" - return await self.db_pool.updates.have_completed_background_updates( + if self._has_finished_sliding_sync_background_jobs: + # as an optimisation, once the job finishes, don't issue another + # database transaction to check it, since it won't 'un-finish' + return True + + self._has_finished_sliding_sync_background_jobs = await self.db_pool.updates.have_completed_background_updates( ( _BackgroundUpdates.SLIDING_SYNC_PREFILL_JOINED_ROOMS_TO_RECALCULATE_TABLE_BG_UPDATE, _BackgroundUpdates.SLIDING_SYNC_JOINED_ROOMS_BG_UPDATE, _BackgroundUpdates.SLIDING_SYNC_MEMBERSHIP_SNAPSHOTS_BG_UPDATE, ) ) + return self._has_finished_sliding_sync_background_jobs async def get_sent_invite_count_by_user(self, user_id: str, from_ts: int) -> int: """