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

Only try to backfill event if we haven't tried before recently (exponential backoff) #13635

Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
78 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
a121bc3
Fix lints
MadLittleMods Sep 22, 2022
491aac6
Add context for why we have the is_state condition
MadLittleMods Sep 22, 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
1 change: 1 addition & 0 deletions changelog.d/13589.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Keep track when we attempt to backfill an event but fail so we can intelligently back-off in the future.
1 change: 1 addition & 0 deletions changelog.d/13635.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Exponentially backoff from backfilling the same event over and over.
70 changes: 32 additions & 38 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from unpaddedbase64 import decode_base64

from synapse import event_auth
from synapse.api.constants import EventContentFields, EventTypes, Membership
from synapse.api.constants import MAX_DEPTH, EventContentFields, EventTypes, Membership
from synapse.api.errors import (
AuthError,
CodeMessageException,
Expand Down Expand Up @@ -226,8 +226,9 @@ async def _maybe_backfill_inner(
"""
backwards_extremities = [
_BackfillPoint(event_id, depth, _BackfillPointType.BACKWARDS_EXTREMITY)
for event_id, depth in await self.store.get_oldest_event_ids_with_depth_in_room(
room_id
for event_id, depth in await self.store.get_backfill_points_in_room(
room_id=room_id,
current_depth=current_depth,
)
]

Expand All @@ -236,7 +237,8 @@ async def _maybe_backfill_inner(
insertion_events_to_be_backfilled = [
_BackfillPoint(event_id, depth, _BackfillPointType.INSERTION_PONT)
for event_id, depth in await self.store.get_insertion_event_backward_extremities_in_room(
room_id
room_id=room_id,
current_depth=current_depth,
)
]
logger.debug(
Expand All @@ -245,10 +247,6 @@ async def _maybe_backfill_inner(
insertion_events_to_be_backfilled,
)

if not backwards_extremities and not insertion_events_to_be_backfilled:
logger.debug("Not backfilling as no extremeties found.")
return False

# we now have a list of potential places to backpaginate from. We prefer to
# start with the most recent (ie, max depth), so let's sort the list.
sorted_backfill_points: List[_BackfillPoint] = sorted(
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -269,6 +267,29 @@ async def _maybe_backfill_inner(
sorted_backfill_points,
)

# If we have no backfill points lower than the `current_depth` then
# either we can a) bail or b) still attempt to backfill. We opt to try
# backfilling anyway just in case we do get relevant events.
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
if not sorted_backfill_points and current_depth != MAX_DEPTH:
logger.debug(
"_maybe_backfill_inner: all backfill points are *after* current depth. Backfilling anyway."
)
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,
processing_start_time=processing_start_time,
)
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.
logger.debug(
"_maybe_backfill_inner: Not backfilling as no backward extremeties found."
)
return False

# If we're approaching an extremity we trigger a backfill, otherwise we
# no-op.
#
Expand All @@ -282,43 +303,16 @@ async def _maybe_backfill_inner(
# 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 = sorted_backfill_points[0].depth
if current_depth - 2 * limit > max_depth:
max_depth_of_backfill_points = sorted_backfill_points[0].depth
if current_depth - 2 * limit > max_depth_of_backfill_points:
logger.debug(
"Not backfilling as we don't need to. %d < %d - 2 * %d",
max_depth,
max_depth_of_backfill_points,
current_depth,
limit,
)
return False

# We ignore extremities that have a greater depth than our current depth
# as:
# 1. we don't really care about getting events that have happened
# after our current position; and
# 2. we have likely previously tried and failed to backfill from that
# extremity, so to avoid getting "stuck" requesting the same
# backfill repeatedly we drop those extremities.
#
# However, we need to check that the filtered extremities are non-empty.
# If they are empty then either we can a) bail or b) still attempt to
# backfill. We opt to try backfilling anyway just in case we do get
# relevant events.
#
filtered_sorted_backfill_points = [
t for t in sorted_backfill_points if t.depth <= current_depth
]
if filtered_sorted_backfill_points:
logger.debug(
"_maybe_backfill_inner: backfill points before current depth: %s",
filtered_sorted_backfill_points,
)
sorted_backfill_points = filtered_sorted_backfill_points
else:
logger.debug(
"_maybe_backfill_inner: all backfill points are *after* current depth. Backfilling anyway."
)

# For performance's sake, we only want to paginate from a particular extremity
# if we can actually see the events we'll get. Otherwise, we'd just spend a lot
# of resources to get redacted events. We check each extremity in turn and
Expand Down
9 changes: 9 additions & 0 deletions synapse/handlers/federation_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,10 @@ async def _process_pulled_event(
self._sanity_check_event(event)
except SynapseError as err:
logger.warning("Event %s failed sanity check: %s", event_id, err)
if backfilled:
await self._store.record_event_failed_backfill_attempt(
event.room_id, event_id
)
return

try:
Expand Down Expand Up @@ -897,6 +901,11 @@ async def _process_pulled_event(
backfilled=backfilled,
)
except FederationError as e:
if backfilled:
await self._store.record_event_failed_backfill_attempt(
event.room_id, event_id
)

if e.code == 403:
logger.warning("Pulled event %s failed history check.", event_id)
else:
Expand Down
Loading