Skip to content

Commit 3ee669b

Browse files
MadLittleModsFrenchGithubUser
authored andcommitted
Prefer close backfill points (absolute distance) (element-hq#19748)
This isn't fixing any particular issue. It's just a follow-up I thought about after merging element-hq#19611 since we're now also dealing with backfill points in the nearby range ahead of the `current_depth`. And it's possible that the previous sort could bias to all nearby backfill points ahead of the `current_depth` that don't extend into the visible window of events we're paginating through.
1 parent a4e96e9 commit 3ee669b

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

changelog.d/19748.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prefer close backfill points (absolute distance).

synapse/handlers/federation.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,22 @@ async def _maybe_backfill_inner(
275275
)
276276
]
277277

278-
# we now have a list of potential places to backpaginate from. We prefer to
279-
# start with the most recent (ie, max depth), so let's sort the list.
278+
# we now have a list of potential places to backpaginate from. Figure out which
279+
# ones we should prefer, so let's sort the list.
280280
sorted_backfill_points: list[_BackfillPoint] = sorted(
281281
backwards_extremities,
282-
key=lambda e: -int(e.depth),
282+
key=lambda e: (
283+
# Prefer backfill points that are closer to the `current_depth`
284+
# (absolute distance)
285+
abs(current_depth - e.depth),
286+
# For the tie-break, we care about events that are actually in the past
287+
# as they're more likely to reveal history that we can return (something
288+
# absolutely in the past is better than something can potentially extend
289+
# into the past).
290+
#
291+
# This sorts ascending so 0 sorts before 1
292+
0 if current_depth >= e.depth else 1,
293+
),
283294
)
284295

285296
logger.debug(
@@ -300,7 +311,7 @@ async def _maybe_backfill_inner(
300311
str(len(sorted_backfill_points)),
301312
)
302313

303-
# If we have no backfill points lower than the `current_depth` then either we
314+
# If we have no backfill points lower than the `nearby_depth` then either we
304315
# can a) bail or b) still attempt to backfill. We opt to try backfilling anyway
305316
# just in case we do get relevant events. This is good for eventual consistency
306317
# sake but we don't need to block the client for something that is just as

0 commit comments

Comments
 (0)