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

Keep track when we try and fail to process a pulled event #13589

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
34 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
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
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
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
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
7 changes: 7 additions & 0 deletions synapse/federation/federation_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ async def _check_sigs_and_hash(
"event_id": pdu.event_id,
}
)

# TODO: Is it okay to assume this is called from backfilling?
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
#
# In any case, we definately don't want to keep fetching spam over
# and over and failing it.
await self._store.record_event_backfill_attempt(pdu.event_id)

# we redact (to save disk space) as well as soft-failing (to stop
# using the event in prev_events).
redacted_event = prune_event(pdu)
Expand Down
5 changes: 5 additions & 0 deletions synapse/handlers/federation_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,8 @@ 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:
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
await self._store.record_event_backfill_attempt(event_id)
return

try:
Expand Down Expand Up @@ -897,6 +899,9 @@ async def _process_pulled_event(
backfilled=backfilled,
)
except FederationError as e:
if backfilled:
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
await self._store.record_event_backfill_attempt(event_id)
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved

if e.code == 403:
logger.warning("Pulled event %s failed history check.", event_id)
else:
Expand Down
16 changes: 16 additions & 0 deletions synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,22 @@ def _get_backfill_events(

return event_id_results

@trace
async def record_event_backfill_attempt(self, event_id: str) -> None:
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
await self.db_pool.simple_upsert(
table="event_backfill_attempts",
keyvalues={"event_id": event_id},
values={
"event_id": event_id,
# TODO: This needs to increment
"num_attempts": 1,
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
"last_attempt_ts": self._clock.time_msec(),
},
insertion_values={},
desc="record_event_backfill_attempt",
lock=False,
)

async def get_missing_events(
self,
room_id: str,
Expand Down
28 changes: 19 additions & 9 deletions synapse/storage/databases/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2435,17 +2435,27 @@ def _update_backward_extremeties(
"DELETE FROM event_backward_extremities"
" WHERE event_id = ? AND room_id = ?"
)
backward_extremities_to_remove = [
(ev.event_id, ev.room_id)
for ev in events
if not ev.internal_metadata.is_outlier()
# If we encountered an event with no prev_events, then we might
# as well remove it now because it won't ever have anything else
# to backfill from.
or len(ev.prev_event_ids()) == 0
]
txn.execute_batch(
query,
[
(ev.event_id, ev.room_id)
for ev in events
if not ev.internal_metadata.is_outlier()
# If we encountered an event with no prev_events, then we might
# as well remove it now because it won't ever have anything else
# to backfill from.
or len(ev.prev_event_ids()) == 0
],
backward_extremities_to_remove,
)

# Since we no longer need it as a backward extremity, it won't be
# backfilled again so we no longer need to store the backfill attempts
# around it.
query = "DELETE FROM event_backfill_attempts" " WHERE event_id = ?"
txn.execute_batch(
query,
backward_extremities_to_remove,
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright 2022 The Matrix.org Foundation C.I.C
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


-- Add a table that keeps track of when we last tried to backfill an event. This
-- allows us to be more intelligent when we decide to retry (we don't need to
-- fail over and over) and we can process that event in the background so we
-- don't block on it each time.
CREATE TABLE IF NOT EXISTS event_backfill_attempts(
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
event_id TEXT NOT NULL,
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
num_attempts INT NOT NULL,
last_attempt_ts BIGINT NOT NULL
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
);
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved

CREATE UNIQUE INDEX IF NOT EXISTS event_backfill_attempts_event_id ON event_backfill_attempts(event_id);
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved