Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Limit and filter the number of backfill points to get from the database #13879

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
e0d7fab
Keep track when we tried to backfill an event
MadLittleMods Aug 23, 2022
b8d55d3
Record in some fail spots
MadLittleMods Aug 25, 2022
f63d823
Merge branch 'develop' into madlittlemods/keep-track-when-we-tried-to…
MadLittleMods Aug 25, 2022
bec26e2
Record and clear attempts
MadLittleMods Aug 25, 2022
fee37c3
Add changelog
MadLittleMods Aug 25, 2022
d1290be
Remove from when spam checker fails
MadLittleMods Aug 25, 2022
f9119d0
Custom upsert to increment
MadLittleMods Aug 25, 2022
f5c6fe7
Fix lints
MadLittleMods Aug 25, 2022
16ebec0
Remove extra whitespace
MadLittleMods Aug 25, 2022
ce07aa1
Move to correct folder
MadLittleMods Aug 25, 2022
5811ba1
Set the version back
MadLittleMods Aug 25, 2022
cf2b093
Fix `TypeError: not all arguments converted during string formatting`
MadLittleMods Aug 25, 2022
cbb4194
Add test to make sure failed backfill attempts are recorded
MadLittleMods Aug 26, 2022
621c5d3
Clean up test
MadLittleMods Aug 26, 2022
75c07bb
Fix comments
MadLittleMods Aug 26, 2022
783cce5
Add test for clearing backfill attempts
MadLittleMods Aug 26, 2022
54ef84b
Maybe a better comment
MadLittleMods Aug 26, 2022
7bf3e7f
WIP: Just working on the query
MadLittleMods Aug 26, 2022
37ff009
Move comment to where it matters
MadLittleMods Aug 26, 2022
a58d191
Silly graph pt 1
MadLittleMods Aug 26, 2022
f127ad1
Silly graph pt 2
MadLittleMods Aug 26, 2022
18abbf4
Tests running (not working)
MadLittleMods Aug 27, 2022
23310f5
Passing test
MadLittleMods Aug 27, 2022
64e01d8
Add test for A and B
MadLittleMods Aug 27, 2022
47bac25
Add tests for backfill attempts
MadLittleMods Aug 27, 2022
2ebed9d
Remove `GROUP BY backward_extrem.event_id` (seems unnecessary)
MadLittleMods Aug 27, 2022
60b3b92
Clarify why that much time
MadLittleMods Aug 27, 2022
e9f603d
Label ? slot
MadLittleMods Aug 27, 2022
a8f1464
Better explanation
MadLittleMods Aug 27, 2022
bbd1c94
Add changelog
MadLittleMods Aug 27, 2022
dd1db25
Fix lints
MadLittleMods Aug 27, 2022
c583eef
Update docstring
MadLittleMods Aug 27, 2022
ea4a3ad
Apply same changes to `get_insertion_event_backward_extremities_in_room`
MadLittleMods Aug 27, 2022
f495752
Use power and capitalize AS
MadLittleMods Aug 27, 2022
f2061b9
Use SQLite compatible power of 2 (left shift)
MadLittleMods Aug 31, 2022
e4192d7
Update table name with "failed" and include room_id in the primary key
MadLittleMods Aug 31, 2022
7a44932
Rename to record_event_failed_backfill_attempt
MadLittleMods Aug 31, 2022
86d98ca
Merge branch 'develop' into madlittlemods/keep-track-when-we-tried-to…
MadLittleMods Aug 31, 2022
29f584e
Merge branch 'madlittlemods/keep-track-when-we-tried-to-backfill-an-e…
MadLittleMods Aug 31, 2022
506a8dd
Changes after merging madlittlemods/keep-track-when-we-tried-to-backf…
MadLittleMods Aug 31, 2022
361ce5c
Use compatible least/min on each db platform
MadLittleMods Aug 31, 2022
b09d8a2
Fix SQLite no such column error when comparing table to null
MadLittleMods Aug 31, 2022
965d142
Add comment about how these are sorted by depth now
MadLittleMods Aug 31, 2022
267777f
Apply same least compatiblity to insertion event extremities
MadLittleMods Aug 31, 2022
d0cd42a
Fix lints
MadLittleMods Aug 31, 2022
3d9f625
Try fix ambiguous column (remove unsued table)
MadLittleMods Sep 1, 2022
33a3c64
Fix ambiguous column
MadLittleMods Sep 1, 2022
6736d10
Add tests for get_insertion_event_backward_extremities_in_room
MadLittleMods Sep 1, 2022
6eba1d4
Fix up test descriptions
MadLittleMods Sep 1, 2022
1464101
Add _unsafe_to_upsert_tables check
MadLittleMods Sep 1, 2022
71c7738
Add foreign key references
MadLittleMods Sep 1, 2022
df8c76d
Merge branch 'develop' into madlittlemods/keep-track-when-we-tried-to…
MadLittleMods Sep 1, 2022
d45b078
Remove reference to event that won't be in the events table
MadLittleMods Sep 1, 2022
c939422
Merge branch 'madlittlemods/keep-track-when-we-tried-to-backfill-an-e…
MadLittleMods Sep 1, 2022
599e212
Fix approximate typo
MadLittleMods Sep 1, 2022
bc8046b
Clarify what depth sort
MadLittleMods Sep 1, 2022
ea08006
Fix typos
MadLittleMods Sep 1, 2022
9a85bb4
Normal is not obvious
MadLittleMods Sep 1, 2022
7204cce
Fix left-shift math
MadLittleMods Sep 1, 2022
8f214b1
Fix foreign key constraint
MadLittleMods Sep 2, 2022
33ad64e
Merge branch 'develop' into madlittlemods/keep-track-when-we-tried-to…
MadLittleMods Sep 9, 2022
63bec99
Remove emulated upsert code (all of our dbs no support it)
MadLittleMods Sep 9, 2022
31d7502
Table rename `event_failed_pull_attempts`
MadLittleMods Sep 9, 2022
0b5f1db
Add `last_cause` column
MadLittleMods Sep 9, 2022
4ce7709
Merge branch 'develop' into madlittlemods/keep-track-when-we-tried-to…
MadLittleMods Sep 12, 2022
d3a1f84
Merge branch 'develop' into madlittlemods/keep-track-when-we-tried-to…
MadLittleMods Sep 13, 2022
1347686
Update schema version summary
MadLittleMods Sep 13, 2022
57182dc
Remove backfilled check since we plan to go general anyway
MadLittleMods Sep 14, 2022
e58bc65
Merge branch 'develop' into madlittlemods/keep-track-when-we-tried-to…
MadLittleMods Sep 14, 2022
70019d2
Move change to latest delta 73
MadLittleMods Sep 14, 2022
46a1a20
Merge branch 'madlittlemods/keep-track-when-we-tried-to-backfill-an-e…
MadLittleMods Sep 14, 2022
91c5be0
Merge branch 'develop' into madlittlemods/13622-do-not-retry-backfill…
MadLittleMods Sep 14, 2022
7ea40b1
Updates after schema changes in the other PR
MadLittleMods Sep 14, 2022
40ec8d8
Remove debug logging
MadLittleMods Sep 14, 2022
47aa375
Merge branch 'develop' into madlittlemods/13622-do-not-retry-backfill…
MadLittleMods Sep 22, 2022
1208540
Remove orthogonal `current_depth` changes
MadLittleMods Sep 22, 2022
1738619
Revert "Remove orthogonal `current_depth` changes"
MadLittleMods Sep 22, 2022
3f1b695
Typos
MadLittleMods Sep 22, 2022
aae06fd
Merge branch 'develop' into madlittlemods/backfill-points-filtering-a…
MadLittleMods Sep 23, 2022
bae72e4
I didn't try to change the lock (shrug)
MadLittleMods Sep 23, 2022
4f2b3ae
Make sure our timing doesn't get thrown off
MadLittleMods Sep 23, 2022
44e2a5d
Add limit, fix lints and add changelog
MadLittleMods Sep 23, 2022
685bbee
Fixup language
MadLittleMods Sep 27, 2022
9921f24
Remove redundant comment
MadLittleMods Sep 27, 2022
7c96481
Simpler, behavior equivalent condition when nothing to backfill
MadLittleMods Sep 27, 2022
b7368ba
Explain what passing None does
MadLittleMods Sep 27, 2022
4823bb2
Only explain what's necessary to understand current_depth
MadLittleMods Sep 27, 2022
829cef8
Explain what the depth correlates to
MadLittleMods Sep 27, 2022
db90283
More accurate descroption of get_backfill_points_in_room
MadLittleMods Sep 27, 2022
aba8b0f
Remove extra context that describes our usage
MadLittleMods Sep 27, 2022
e727a31
Merge branch 'develop' into madlittlemods/backfill-points-filtering-a…
MadLittleMods Sep 27, 2022
379ccb4
Point at specific event
MadLittleMods Sep 28, 2022
21f5933
Start with a one line description
MadLittleMods Sep 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ async def _maybe_backfill_inner(
room_id: The room to backfill in.
current_depth: The depth to check at for any upcoming backfill points.
limit: The max number of events to request from the remote federated server.
processing_start_time: The time when `maybe_backfill` started
processing. Only used for timing.
processing_start_time: The time when `maybe_backfill` started processing.
Only used for timing. If `None`, no timing observation will be made.
"""
backwards_extremities = [
_BackfillPoint(event_id, depth, _BackfillPointType.BACKWARDS_EXTREMITY)
Expand Down Expand Up @@ -284,22 +284,23 @@ async def _maybe_backfill_inner(
# backfilling anyway just in case we do get relevant events.
if not sorted_backfill_points and current_depth != MAX_DEPTH:
logger.debug(
"_maybe_backfill_inner: all backfill points are *after* current depth. Backfilling anyway."
"_maybe_backfill_inner: all backfill points are *after* current depth. Trying again with later backfill points."
)
return await self._maybe_backfill_inner(
room_id=room_id,
# We use `MAX_DEPTH` so that we find all backfill points next
# time (all events are below the `MAX_DEPTH`)
current_depth=MAX_DEPTH,
limit=limit,
# We don't want to another timing observation from this nested
# recursive call. The top-most call can record the time overall
# otherwise the smaller one will throw off the results.
# We don't want to start another timing observation from this
# nested recursive call. The top-most call can record the time
# overall otherwise the smaller one will throw off the results.
processing_start_time=None,
)
elif not sorted_backfill_points and current_depth == MAX_DEPTH:
# Even after trying again with `MAX_DEPTH`, we didn't find any
# backward extremities to backfill from.

# Even after recursing with `MAX_DEPTH`, we didn't find any
# backward extremities to backfill from.
if not sorted_backfill_points:
logger.debug(
"_maybe_backfill_inner: Not backfilling as no backward extremeties found."
)
Expand All @@ -314,10 +315,6 @@ async def _maybe_backfill_inner(
# chose more than one times the limit in case of failure, but choosing a
# much larger factor will result in triggering a backfill request much
# earlier than necessary.
#
# XXX: shouldn't we do this *after* the filter by depth below? Again, we don't
# care about events that have happened after our current position.
#
max_depth_of_backfill_points = sorted_backfill_points[0].depth
if current_depth - 2 * limit > max_depth_of_backfill_points:
logger.debug(
Expand Down
54 changes: 22 additions & 32 deletions synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,17 +730,13 @@ async def get_backfill_points_in_room(
limit: int,
) -> List[Tuple[str, int]]:
"""
Gets the oldest events(backwards extremities) in the room along with the
approximate depth. Sorted by depth, highest to lowest (descending) so the closest
events to the `current_depth` are first in the list.
Get the backward extremities to backfill from in the room along with the
approximate depth. Only returns events that are at a depth lower than or
equal to the `current_depth`. Sorted by depth, highest to lowest (descending)
so the closest events to the `current_depth` are first in the list.
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved

We use this function so that we can compare and see if a client's
`current_depth` at their current scrollback is within pagination range
of the event extremities. If the `current_depth` is close to the depth
of given oldest event, we can trigger a backfill.

We ignore extremities that have a greater depth than our `current_depth`
as:
We ignore extremities that are newer than the user's current scroll position
(ie, those with depth greater than `current_depth`) as:
1. we don't really care about getting events that have happened
after our current position; and
2. by the nature of paginating and scrolling back, we have likely
Expand All @@ -750,9 +746,7 @@ async def get_backfill_points_in_room(

Args:
room_id: Room where we want to find the oldest events
current_depth: The depth at the users current scrollback position
because we only care about finding events older than the given
`current_depth` when scrolling and paginating backwards.
current_depth: The depth at the user's current scrollback position
limit: The max number of backfill points to return

Returns:
Expand Down Expand Up @@ -812,9 +806,10 @@ def get_backfill_points_in_room_txn(
* means less than the given depth) because we're looking backwards
* from the `current_depth` when backfilling.
*
* current_depth (ignore events that come after this, ignore 2-4)
* |
* <oldest-in-time> [0]<--[1]▼<--[2]<--[3]<--[4] <newest-in-time>
* current_depth (ignore events that come after this, ignore 2-4)
* |
* ▼
* <oldest-in-time> [0]<--[1]<--[2]<--[3]<--[4] <newest-in-time>
*/
AND event.depth <= ? /* current_depth */
/**
Expand Down Expand Up @@ -877,17 +872,13 @@ async def get_insertion_event_backward_extremities_in_room(
) -> List[Tuple[str, int]]:
"""
Get the insertion events we know about that we haven't backfilled yet
along with the approximate depth. Sorted by depth, highest to lowest
(descending) so the closest events to the `current_depth` are first
in the list.

We use this function so that we can compare and see if someones
`current_depth` at their current scrollback is within pagination range
of the insertion event. If the `current_depth` is close to the depth
of the given insertion event, we can trigger a backfill.
along with the approximate depth. Only returns insertion events that are
at a depth lower than or equal to the `current_depth`. Sorted by depth,
highest to lowest (descending) so the closest events to the
`current_depth` are first in the list.

We ignore insertion events that have a greater depth than our `current_depth`
as:
We ignore insertion events that are newer than the user's current scroll
position (ie, those with depth greater than `current_depth`) as:
1. we don't really care about getting events that have happened
after our current position; and
2. by the nature of paginating and scrolling back, we have likely
Expand All @@ -897,9 +888,7 @@ async def get_insertion_event_backward_extremities_in_room(

Args:
room_id: Room where we want to find the oldest events
current_depth: The depth at the users current scrollback position because
we only care about finding events older than the given
`current_depth` when scrolling and paginating backwards.
current_depth: The depth at the user's current scrollback position
limit: The max number of insertion event extremities to return

Returns:
Expand Down Expand Up @@ -934,9 +923,10 @@ def get_insertion_event_backward_extremities_in_room_txn(
* means less than the given depth) because we're looking backwards
* from the `current_depth` when backfilling.
*
* current_depth (ignore events that come after this, ignore 2-4)
* |
* <oldest-in-time> [0]<--[1]▼<--[2]<--[3]<--[4] <newest-in-time>
* current_depth (ignore events that come after this, ignore 2-4)
* |
* ▼
* <oldest-in-time> [0]<--[1]<--[2]<--[3]<--[4] <newest-in-time>
*/
AND event.depth <= ? /* current_depth */
/**
Expand Down