Skip to content

Commit 4e90221

Browse files
Sliding sync minor performance speed up using new table (#17787)
Use the new tables to work out which rooms have changed. --------- Co-authored-by: Eric Eastwood <eric.eastwood@beta.gouv.fr>
1 parent e2610de commit 4e90221

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

changelog.d/17787.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sliding sync minor performance speed up using new table.

synapse/handlers/sliding_sync/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
Requester,
5050
SlidingSyncStreamToken,
5151
StateMap,
52+
StrCollection,
5253
StreamKeyType,
5354
StreamToken,
5455
)
@@ -293,7 +294,6 @@ async def handle_room(room_id: str) -> None:
293294
# to record rooms as having updates even if there might not actually
294295
# be anything new for the user (e.g. due to event filters, events
295296
# having happened after the user left, etc).
296-
unsent_room_ids = []
297297
if from_token:
298298
# The set of rooms that the client (may) care about, but aren't
299299
# in any list range (or subscribed to).
@@ -305,15 +305,24 @@ async def handle_room(room_id: str) -> None:
305305
# TODO: Replace this with something faster. When we land the
306306
# sliding sync tables that record the most recent event
307307
# positions we can use that.
308-
missing_event_map_by_room = (
309-
await self.store.get_room_events_stream_for_rooms(
310-
room_ids=missing_rooms,
311-
from_key=to_token.room_key,
312-
to_key=from_token.stream_token.room_key,
313-
limit=1,
308+
unsent_room_ids: StrCollection
309+
if await self.store.have_finished_sliding_sync_background_jobs():
310+
unsent_room_ids = await (
311+
self.store.get_rooms_that_have_updates_since_sliding_sync_table(
312+
room_ids=missing_rooms,
313+
from_key=from_token.stream_token.room_key,
314+
)
314315
)
315-
)
316-
unsent_room_ids = list(missing_event_map_by_room)
316+
else:
317+
missing_event_map_by_room = (
318+
await self.store.get_room_events_stream_for_rooms(
319+
room_ids=missing_rooms,
320+
from_key=to_token.room_key,
321+
to_key=from_token.stream_token.room_key,
322+
limit=1,
323+
)
324+
)
325+
unsent_room_ids = list(missing_event_map_by_room)
317326

318327
new_connection_state.rooms.record_unsent_rooms(
319328
unsent_room_ids, from_token.stream_token.room_key

synapse/storage/databases/main/stream.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,48 @@ def get_rooms_that_changed(
751751
if self._events_stream_cache.has_entity_changed(room_id, from_id)
752752
}
753753

754+
async def get_rooms_that_have_updates_since_sliding_sync_table(
755+
self,
756+
room_ids: StrCollection,
757+
from_key: RoomStreamToken,
758+
) -> StrCollection:
759+
"""Return the rooms that probably have had updates since the given
760+
token (changes that are > `from_key`)."""
761+
# If the stream change cache is valid for the stream token, we can just
762+
# use the result of that.
763+
if from_key.stream >= self._events_stream_cache.get_earliest_known_position():
764+
return self._events_stream_cache.get_entities_changed(
765+
room_ids, from_key.stream
766+
)
767+
768+
def get_rooms_that_have_updates_since_sliding_sync_table_txn(
769+
txn: LoggingTransaction,
770+
) -> StrCollection:
771+
sql = """
772+
SELECT room_id
773+
FROM sliding_sync_joined_rooms
774+
WHERE {clause}
775+
AND event_stream_ordering > ?
776+
"""
777+
778+
results: Set[str] = set()
779+
for batch in batch_iter(room_ids, 1000):
780+
clause, args = make_in_list_sql_clause(
781+
self.database_engine, "room_id", batch
782+
)
783+
784+
args.append(from_key.stream)
785+
txn.execute(sql.format(clause=clause), args)
786+
787+
results.update(row[0] for row in txn)
788+
789+
return results
790+
791+
return await self.db_pool.runInteraction(
792+
"get_rooms_that_have_updates_since_sliding_sync_table",
793+
get_rooms_that_have_updates_since_sliding_sync_table_txn,
794+
)
795+
754796
async def paginate_room_events_by_stream_ordering(
755797
self,
756798
*,

0 commit comments

Comments
 (0)