Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sliding sync: Fix bg update again (v3) #17634

Merged
merged 3 commits into from
Aug 30, 2024
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/17634.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pre-populate room data used in experimental [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575) Sliding Sync `/sync` endpoint for quick filtering/sorting.
31 changes: 26 additions & 5 deletions synapse/storage/databases/main/events_bg_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1961,9 +1961,30 @@ def _find_memberships_to_update_txn(
return 0

def _find_previous_membership_txn(
txn: LoggingTransaction, room_id: str, user_id: str, stream_ordering: int
txn: LoggingTransaction, room_id: str, user_id: str, event_id: str
) -> Tuple[str, str]:
# Find the previous invite/knock event before the leave event
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
#
# Here are some notes on how we landed on this query:
#
# We're using `topological_ordering` instead of `stream_ordering` because
# somehow it's possible to have your `leave` event backfilled with a
# negative `stream_ordering` and your previous `invite` event with a
# positive `stream_ordering` so we wouldn't have a chance of finding the
# previous membership with a naive `event_stream_ordering < ?` comparison.
#
# Also be careful because `room_memberships.event_stream_ordering` is
# nullable and not always filled in. You would need to join on `events` to
# rely on `events.stream_ordering` instead. Even though the
# `events.stream_ordering` also doesn't have a `NOT NULL` constraint, it
# doesn't have any rows where this is the case (checked on `matrix.org`).
# The fact the `events.stream_ordering` is a nullable column is a holdover
# from a rename of the column.
#
# You might also consider using the `event_auth` table to find the previous
# membership, but there are cases where somehow a membership event doesn't
# point back to the previous membership event in the auth events (unknown
# cause).
txn.execute(
"""
SELECT event_id, membership
Expand All @@ -1972,14 +1993,14 @@ def _find_previous_membership_txn(
WHERE
room_id = ?
AND m.user_id = ?
AND e.stream_ordering < ?
ORDER BY e.stream_ordering DESC
AND e.event_id != ?
ORDER BY e.topological_ordering DESC
LIMIT 1
""",
(
room_id,
user_id,
stream_ordering,
event_id,
),
)
row = txn.fetchone()
Expand Down Expand Up @@ -2106,7 +2127,7 @@ def _find_previous_membership_txn(
_find_previous_membership_txn,
room_id,
user_id,
membership_event_stream_ordering,
membership_event_id,
)
)

Expand Down
Loading