summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2024-04-05 15:05:22 +0100
committerErik Johnston <erik@matrix.org>2024-04-05 15:43:32 +0100
commitc2439165466a6860eb29e4a7d5b691dcd85fb496 (patch)
treebbbade1ee44f883dc3fb681f9758a76e9cf249cb
parentPull out fewer receipts from DB when doing push (#17049) (diff)
downloadsynapse-c2439165466a6860eb29e4a7d5b691dcd85fb496.tar.xz
Reduce DB usage when backpaginating
In certain cases going from a stream token to a topological token can be
expensive (as it all rows in the room above the stream ordering), so we
refactor things so we do that calculation less.
-rw-r--r--synapse/handlers/pagination.py31
1 files changed, 21 insertions, 10 deletions
diff --git a/synapse/handlers/pagination.py b/synapse/handlers/pagination.py

index cd3a9088cd..7b15924331 100644 --- a/synapse/handlers/pagination.py +++ b/synapse/handlers/pagination.py
@@ -468,16 +468,6 @@ class PaginationHandler: room_id, requester, allow_departed_users=True ) - if pagin_config.direction == Direction.BACKWARDS: - # if we're going backwards, we might need to backfill. This - # requires that we have a topo token. - if room_token.topological: - curr_topo = room_token.topological - else: - curr_topo = await self.store.get_current_topological_token( - room_id, room_token.stream - ) - # If they have left the room then clamp the token to be before # they left the room, to save the effort of loading from the # database. @@ -496,6 +486,14 @@ class PaginationHandler: ) assert leave_token.topological is not None + # We need the topological part of the token to compare against. + if room_token.topological: + curr_topo = room_token.topological + else: + curr_topo = await self.store.get_current_topological_token( + room_id, room_token.stream + ) + if leave_token.topological < curr_topo: from_token = from_token.copy_and_replace( StreamKeyType.ROOM, leave_token @@ -561,6 +559,19 @@ class PaginationHandler: break previous_event_depth = event_depth + # if we're going backwards, we might need to backfill. This + # requires that we have a topo token. + if room_token.topological: + curr_topo = room_token.topological + elif events: + # If we've already fetched some events then we can just use + # those to get the right depth. + curr_topo = events[0].depth + else: + curr_topo = await self.store.get_current_topological_token( + room_id, room_token.stream + ) + # Backfill in the foreground if we found a big gap, have too many holes, # or we don't have enough events to fill the limit that the client asked # for.