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

Stop reading from event_edges.room_id. #12914

Merged
merged 4 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/12914.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preparation for database schema simplifications: stop reading from `event_edges.room_id`.
7 changes: 2 additions & 5 deletions synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,17 +1318,14 @@ 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 NOT is_state "
"LIMIT ?"
)

while front and len(event_results) < limit:
new_front = set()
for event_id in front:
txn.execute(
query, (room_id, event_id, False, limit - len(event_results))
)

txn.execute(query, (event_id, limit - len(event_results)))
new_results = {t[0] for t in txn} - seen_events

new_front |= new_results
Expand Down
33 changes: 15 additions & 18 deletions synapse/storage/databases/main/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +1931 to +1941
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm moving this up (from lines 1948-1960) while I'm here so that it's next to the query definition. I found it it confusing that we defined two queries, then didn't use them for a while.

Also simplifying the condition to avoid building a result list.


# 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.
Expand All @@ -1936,34 +1948,19 @@ 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.
*/
AND rejections.event_id IS NULL
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
Expand Down
5 changes: 4 additions & 1 deletion synapse/storage/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""


Expand Down