From 6138b1226a693bc2e7a443e09c79c9a1df2e8337 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 30 May 2022 12:28:38 +0100 Subject: [PATCH] Stop reading from `event_edges.room_id`. --- changelog.d/12914.misc | 1 + .../databases/main/event_federation.py | 2 +- .../storage/databases/main/events_worker.py | 33 +++++++++---------- synapse/storage/schema/__init__.py | 5 ++- 4 files changed, 21 insertions(+), 20 deletions(-) create mode 100644 changelog.d/12914.misc diff --git a/changelog.d/12914.misc b/changelog.d/12914.misc new file mode 100644 index 000000000000..07d819932a88 --- /dev/null +++ b/changelog.d/12914.misc @@ -0,0 +1 @@ +Preparation for database schema simplifications: stop reading from `event_edges.room_id`. diff --git a/synapse/storage/databases/main/event_federation.py b/synapse/storage/databases/main/event_federation.py index 562dcbe94d3a..dcaf3b89aca0 100644 --- a/synapse/storage/databases/main/event_federation.py +++ b/synapse/storage/databases/main/event_federation.py @@ -1318,7 +1318,7 @@ def _get_missing_events( query = ( "SELECT prev_event_id FROM event_edges " - "WHERE room_id = ? AND event_id = ? AND is_state = ? " + "WHERE event_id = ? AND is_state = ? " "LIMIT ?" ) diff --git a/synapse/storage/databases/main/events_worker.py b/synapse/storage/databases/main/events_worker.py index a97d7e166444..b99b10778490 100644 --- a/synapse/storage/databases/main/events_worker.py +++ b/synapse/storage/databases/main/events_worker.py @@ -1928,6 +1928,18 @@ def is_event_next_to_gap_txn(txn: LoggingTransaction) -> bool: LIMIT 1 """ + # We consider any forward extremity as the latest in the room and + # not a forward gap. + # + # To expand, even though there is technically a gap at the front of + # the room where the forward extremities are, we consider those the + # latest messages in the room so asking other homeservers for more + # is useless. The new latest messages will just be federated as + # usual. + txn.execute(forward_extremity_query, (event.room_id, event.event_id)) + if txn.fetchone(): + return False + # Check to see whether the event in question is already referenced # by another event. If we don't see any edges, we're next to a # forward gap. @@ -1936,8 +1948,7 @@ def is_event_next_to_gap_txn(txn: LoggingTransaction) -> bool: /* Check to make sure the event referencing our event in question is not rejected */ LEFT JOIN rejections ON event_edges.event_id = rejections.event_id WHERE - event_edges.room_id = ? - AND event_edges.prev_event_id = ? + event_edges.prev_event_id = ? /* It's not a valid edge if the event referencing our event in * question is rejected. */ @@ -1945,25 +1956,11 @@ def is_event_next_to_gap_txn(txn: LoggingTransaction) -> bool: LIMIT 1 """ - # We consider any forward extremity as the latest in the room and - # not a forward gap. - # - # To expand, even though there is technically a gap at the front of - # the room where the forward extremities are, we consider those the - # latest messages in the room so asking other homeservers for more - # is useless. The new latest messages will just be federated as - # usual. - txn.execute(forward_extremity_query, (event.room_id, event.event_id)) - forward_extremities = txn.fetchall() - if len(forward_extremities): - return False - # If there are no forward edges to the event in question (another # event hasn't referenced this event in their prev_events), then we # assume there is a forward gap in the history. - txn.execute(forward_edge_query, (event.room_id, event.event_id)) - forward_edges = txn.fetchall() - if not len(forward_edges): + txn.execute(forward_edge_query, (event.event_id,)) + if not txn.fetchone(): return True return False diff --git a/synapse/storage/schema/__init__.py b/synapse/storage/schema/__init__.py index da98f05e0348..19466150d4c6 100644 --- a/synapse/storage/schema/__init__.py +++ b/synapse/storage/schema/__init__.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -SCHEMA_VERSION = 70 # remember to update the list below when updating +SCHEMA_VERSION = 71 # remember to update the list below when updating """Represents the expectations made by the codebase about the database schema This should be incremented whenever the codebase changes its requirements on the @@ -67,6 +67,9 @@ Changes in SCHEMA_VERSION = 70: - event_reference_hashes is no longer written to. + +Changes in SCHEMA_VERSION = 71: + - event_edges.room_id is no longer read from. """