From 9a6d8faafe3c09d5f70767637a1f6fe1ff7d231a Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Thu, 28 Oct 2021 20:24:00 -0500 Subject: [PATCH] WIP: Don't include the event we branch from We want to backfill all of the history before adding the base event. But then there is a problem of how do we add the base event after exhausting all of the historical messages. Backfill will give us that extremity again but the current code will always choose the historical branch over and over and never move past it. I wish we could ask the federated homeserver if it already has the insertion event locally but we can't make any requests in the store code here :/ --- .../databases/main/event_federation.py | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/synapse/storage/databases/main/event_federation.py b/synapse/storage/databases/main/event_federation.py index 4a4d35f77c5e..779f3e81816f 100644 --- a/synapse/storage/databases/main/event_federation.py +++ b/synapse/storage/databases/main/event_federation.py @@ -1094,8 +1094,7 @@ def _get_backfill_events(self, txn, room_id, event_list, limit): if event_id in event_results: continue - event_results.add(event_id) - + found_connected_historical_messages = False if self.hs.config.experimental.msc2716_enabled: # Try and find any potential historical batches of message history. # @@ -1117,7 +1116,9 @@ def _get_backfill_events(self, txn, room_id, event_list, limit): connected_insertion_event_stream_ordering = row[1] connected_insertion_event_id = row[2] connected_insertion_event_type = row[3] + if connected_insertion_event_id not in event_results: + found_connected_historical_messages = True queue.put( ( -connected_insertion_event_depth, @@ -1146,18 +1147,26 @@ def _get_backfill_events(self, txn, room_id, event_list, limit): if row[2] not in event_results: queue.put((-row[0], -row[1], row[2], row[3])) - txn.execute( - connected_prev_event_query, - (event_id, False, limit - len(event_results)), - ) - prev_event_id_results = txn.fetchall() - logger.debug( - "_get_backfill_events: prev_event_ids %s", prev_event_id_results - ) + # Only add the event_result itself if we didn't branch off on the history first + # TODO: How can we not branch off to the historical batch if + # the federated homeserver already has it backfilled? We + # can't make any requests here (no async stuff and should + # really only be database calls) + if not found_connected_historical_messages: + event_results.add(event_id) + + txn.execute( + connected_prev_event_query, + (event_id, False, limit - len(event_results)), + ) + prev_event_id_results = txn.fetchall() + logger.debug( + "_get_backfill_events: prev_event_ids %s", prev_event_id_results + ) - for row in prev_event_id_results: - if row[2] not in event_results: - queue.put((-row[0], -row[1], row[2], row[3])) + for row in prev_event_id_results: + if row[2] not in event_results: + queue.put((-row[0], -row[1], row[2], row[3])) return event_results