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: Pre-populate room data for quick filtering/sorting #17512

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
147 commits
Select commit Hold shift + click to select a range
d26ac74
Start thinking about schemas
MadLittleMods Jul 31, 2024
e7e9cb2
Add changelog
MadLittleMods Jul 31, 2024
8392d6a
Use foreign keys
MadLittleMods Aug 1, 2024
ad1c887
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 5, 2024
2b5f07d
Start of updating `sliding_sync_joined_rooms`
MadLittleMods Aug 6, 2024
1a251d5
Fill in `sliding_sync_non_join_memberships` when current state changes
MadLittleMods Aug 6, 2024
f96d0c3
Special treatment for boolean columns
MadLittleMods Aug 6, 2024
2f3bd27
Test is running
MadLittleMods Aug 6, 2024
cb33580
Server left room test
MadLittleMods Aug 7, 2024
87d9561
Change to updating the latest membership in the room
MadLittleMods Aug 7, 2024
61cea4e
Closer to right
MadLittleMods Aug 7, 2024
68a3daf
Fix comparison and insert
MadLittleMods Aug 7, 2024
5b1053f
Better test assertions
MadLittleMods Aug 8, 2024
c590474
Test non-joins
MadLittleMods Aug 8, 2024
a1aaa47
Add more tests
MadLittleMods Aug 8, 2024
bf78692
Handle to_delete
MadLittleMods Aug 8, 2024
5cf3ad3
Handle server left room
MadLittleMods Aug 8, 2024
bc3796d
Fix some lints
MadLittleMods Aug 8, 2024
cc2d2b6
Fill in `stream_ordering`/`bump_stamp` when we add current state to t…
MadLittleMods Aug 8, 2024
ca90901
Fill in `stream_ordering`/`bump_stamp` for any event being persisted
MadLittleMods Aug 8, 2024
3367422
Need to fix upsert
MadLittleMods Aug 8, 2024
ed47a7e
Fix bumping when events are persisted out of order
MadLittleMods Aug 12, 2024
0af3b48
Refactor to `sliding_sync_membership_snapshots`
MadLittleMods Aug 12, 2024
552f8f4
Update descriptions
MadLittleMods Aug 12, 2024
f069659
Fix lints
MadLittleMods Aug 12, 2024
53232e6
Fill in for remote invites (out of band, outlier membership)
MadLittleMods Aug 12, 2024
ab074f5
Fix events from rooms we're not joined to affecting the joined room s…
MadLittleMods Aug 13, 2024
3e1f24e
User ID is not unique because user is joined to many rooms
MadLittleMods Aug 13, 2024
83a5858
Add tests for remote invites
MadLittleMods Aug 13, 2024
5589ae4
Add test for remote invite rejected/retracted
MadLittleMods Aug 13, 2024
3423eb7
Add test to make sure snapshot evolves with membership
MadLittleMods Aug 13, 2024
f600eac
Adjust test description
MadLittleMods Aug 13, 2024
517946d
Fix lints
MadLittleMods Aug 13, 2024
eb3a185
Fix federating backfill test
MadLittleMods Aug 13, 2024
a90f3d4
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 13, 2024
32ae162
Fix rejecting invite when no_longer_in_room (and other non-join trans…
MadLittleMods Aug 13, 2024
dc447a6
Clarify when/why we upsert
MadLittleMods Aug 13, 2024
96a4614
Update fixme comment
MadLittleMods Aug 13, 2024
3566abd
Fix boolean schema for Postgres
MadLittleMods Aug 13, 2024
5df94f4
Fix running into `StopIteration`
MadLittleMods Aug 13, 2024
8b0e169
More realistic remote room forgotten test
MadLittleMods Aug 13, 2024
f49003c
No invites needed
MadLittleMods Aug 13, 2024
c8508f1
Clean up tables when a room is purged/deleted
MadLittleMods Aug 14, 2024
9f551f0
Fix lints
MadLittleMods Aug 14, 2024
1c931cb
Add background update for `sliding_sync_joined_rooms`
MadLittleMods Aug 15, 2024
23e0d34
Add more tests
MadLittleMods Aug 15, 2024
d113e74
Fix lints
MadLittleMods Aug 15, 2024
4b42e44
Work on background update for `sliding_sync_membership_snapshots`
MadLittleMods Aug 15, 2024
cbeff57
Use helper
MadLittleMods Aug 16, 2024
6c2fc1d
Move background updates to `StateBackgroundUpdateStore`
MadLittleMods Aug 16, 2024
8461faf
Add historical case to background update
MadLittleMods Aug 16, 2024
fb5af8f
Add background update test for `sliding_sync_membership_snapshots`
MadLittleMods Aug 16, 2024
ef5f0fc
Add more tests
MadLittleMods Aug 16, 2024
419be7c
Finish off background update tests
MadLittleMods Aug 16, 2024
fa63c02
Fix lints
MadLittleMods Aug 16, 2024
2ec93e3
Move function next to other helpers
MadLittleMods Aug 16, 2024
c89d859
Fill in docstrings
MadLittleMods Aug 16, 2024
d2f5247
Update comment
MadLittleMods Aug 16, 2024
df0c57d
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 19, 2024
98fb56e
Prefer `_update_sliding_sync_tables_with_new_persisted_events_txn(...…
MadLittleMods Aug 20, 2024
8ee2e11
Add test to handle state reset in the meta data
MadLittleMods Aug 20, 2024
574a04a
Test state reset on membership
MadLittleMods Aug 20, 2024
6cc6bdb
Start of moving logic outside of the transaction (pre-process)
MadLittleMods Aug 20, 2024
95d39db
Closer types
MadLittleMods Aug 20, 2024
2964c56
Use dicts
MadLittleMods Aug 20, 2024
ac5b05c
Use `TypedDict`
MadLittleMods Aug 20, 2024
45c89ec
Move pre-processing completely outside transaction
MadLittleMods Aug 20, 2024
3eb77c3
Add sanity checks and fix wrong variable usage
MadLittleMods Aug 20, 2024
cc200ee
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 20, 2024
726a8e9
Attempt getting real events in backgroun update (needs work)
MadLittleMods Aug 21, 2024
357132d
Go back to simpler fetching senders
MadLittleMods Aug 21, 2024
0233e20
Use full event version after solving the circular import issues
MadLittleMods Aug 21, 2024
a5e06c6
Move back to the main store
MadLittleMods Aug 21, 2024
d3f90e4
Get full events for `_sliding_sync_joined_rooms_backfill`
MadLittleMods Aug 21, 2024
cda92af
No need to update `event_stream_ordering`/`bump_stamp` `ON CONFLICT`
MadLittleMods Aug 21, 2024
772c501
Use available `stream_id`
MadLittleMods Aug 21, 2024
f6d7ffd
Move `_calculate_sliding_sync_table_changes(...)` after we assign `st…
MadLittleMods Aug 21, 2024
e7a3328
Pre-populate `membership` and `membership_event_stream_ordering`
MadLittleMods Aug 21, 2024
5b1db39
Add `sender` column so we can tell leaves from kicks
MadLittleMods Aug 21, 2024
c612572
Move away from `stream_id`
MadLittleMods Aug 21, 2024
cda2311
Add `tombstone_successor_room_id` column
MadLittleMods Aug 21, 2024
513ec8e
Update tests
MadLittleMods Aug 21, 2024
8ddf5c7
Add tombstone to tests
MadLittleMods Aug 22, 2024
0271155
Better handle none case
MadLittleMods Aug 22, 2024
9724836
Log which room is strange
MadLittleMods Aug 22, 2024
0a938b1
Add missing boolean column to portdb script
MadLittleMods Aug 22, 2024
ee2ef0b
Add `forgotten` column
MadLittleMods Aug 22, 2024
b45b189
Fill out docstring
MadLittleMods Aug 22, 2024
31300f4
More docstring
MadLittleMods Aug 22, 2024
339500d
Fix sub-query selecting multiple rows
MadLittleMods Aug 22, 2024
9b8d201
Check `events_and_context` for state events
MadLittleMods Aug 22, 2024
fc73b6f
Rename `insert_key`/`insert_value`
MadLittleMods Aug 22, 2024
980ee9a
Prefer `simple_update_txn`
MadLittleMods Aug 22, 2024
6723824
Prefer `simple_delete_many_txn`
MadLittleMods Aug 22, 2024
d61aada
Simplify `_update_sliding_sync_tables_with_new_persisted_events_txn()`
MadLittleMods Aug 22, 2024
4d87fa6
"backfill" -> "bg_update"
MadLittleMods Aug 22, 2024
693c06b
Move away from backfill language
MadLittleMods Aug 22, 2024
bcba8cc
No need for transaction
MadLittleMods Aug 22, 2024
44432e2
Move tests to dedicated file
MadLittleMods Aug 22, 2024
6edc4c7
Allow for no `bump_stamp` (fix `portdb` CI job)
MadLittleMods Aug 22, 2024
0726a6d
Derive best effort `stream_ordering` outside of the transaction
MadLittleMods Aug 22, 2024
088a4c7
Use `simple_upsert_txn` to update `sliding_sync_joined_rooms`
MadLittleMods Aug 22, 2024
4b866c4
Simplify what we need to think about to grab the best effort value
MadLittleMods Aug 22, 2024
fdb8b59
Correct comment
MadLittleMods Aug 22, 2024
21cc97b
Use `simple_upsert_many_txn` for `sliding_sync_membership_snapshots`
MadLittleMods Aug 22, 2024
f8926d0
Fix partial-stated room re-syncing state but nothing has changed
MadLittleMods Aug 23, 2024
b6a7d2b
Use `simple_upsert_txn` for `sliding_sync_joined_rooms` in background…
MadLittleMods Aug 23, 2024
a57d47b
Use `simple_upsert_txn` for `sliding_sync_membership_snapshots` in ba…
MadLittleMods Aug 23, 2024
9795556
Update comment
MadLittleMods Aug 26, 2024
addb914
Split test cases
MadLittleMods Aug 26, 2024
8bddbe2
Clear out-of-date rows
MadLittleMods Aug 26, 2024
a94c1dd
Add more context for why
MadLittleMods Aug 26, 2024
6a44686
Why it matters
MadLittleMods Aug 26, 2024
eb3c84c
Kick-off background update for out-of-date snapshots
MadLittleMods Aug 26, 2024
53473a0
Adapt `sliding_sync_joined_rooms` background update to use `event_str…
MadLittleMods Aug 26, 2024
7fe5d31
Note down caveat about `forgotten`
MadLittleMods Aug 26, 2024
7a0c281
Add placeholder tests
MadLittleMods Aug 27, 2024
9764f62
Fix query in Postgres
MadLittleMods Aug 27, 2024
c51a309
Maybe: always start background update
MadLittleMods Aug 27, 2024
9a7d8c2
Start catch-up if nothing written yet
MadLittleMods Aug 27, 2024
4dc9e26
Add test for catch-up background update
MadLittleMods Aug 27, 2024
c8e17f7
Add test when no rooms
MadLittleMods Aug 27, 2024
e5e7269
Add more tests
MadLittleMods Aug 27, 2024
85a60c3
More tests
MadLittleMods Aug 28, 2024
56a4c0b
Round out tests
MadLittleMods Aug 28, 2024
9d08bc2
Remove debug logs
MadLittleMods Aug 28, 2024
a507f15
Use `stream_id` of some point before we fetch the current state
MadLittleMods Aug 28, 2024
94e1a54
`get_events(...)` will omit events from unknown room versions
MadLittleMods Aug 28, 2024
53b7309
Add `sliding_sync_joined_rooms_to_recalculate` table
MadLittleMods Aug 28, 2024
8468401
Adapt to using `sliding_sync_joined_rooms_to_recalculate` table
MadLittleMods Aug 28, 2024
da463fb
Add unique index right away for `sliding_sync_joined_rooms_to_recalcu…
MadLittleMods Aug 28, 2024
7c9c620
Remove all rooms pulled out from the queue
erikjohnston Aug 28, 2024
bb905cd
Only run the sliding sync background updates on the main database
erikjohnston Aug 28, 2024
6f9932d
Handle old rows with null event_stream_ordering column
erikjohnston Aug 28, 2024
ab414f2
Use event_auth table to get previous membership
erikjohnston Aug 28, 2024
90d0e03
Fix port script tests by handling empty DBs correctly
erikjohnston Aug 28, 2024
6a164eb
Clarify why we return early
MadLittleMods Aug 28, 2024
3f56efb
Note why it's there
MadLittleMods Aug 28, 2024
e12f95f
Update metric with how long it took with the index and upsert
MadLittleMods Aug 28, 2024
116b833
Ignore membership where we don't have a row in `rooms` table
MadLittleMods Aug 28, 2024
de30d19
Fix-up `rooms` table for old out-of-band memberships
MadLittleMods Aug 28, 2024
15ad7a7
Fill in placeholder issue
MadLittleMods Aug 28, 2024
2f6ee08
Fix join condition not working in Postgres
MadLittleMods Aug 28, 2024
6622a1c
Add `instance_name` to `sliding_sync_membership_snapshots`
MadLittleMods Aug 28, 2024
bcc3e50
Fully-insert `sliding_sync_joined_rooms` rows
MadLittleMods Aug 29, 2024
95d5471
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 29, 2024
b63188c
Explain more in schema
MadLittleMods Aug 29, 2024
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/17512.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.
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions synapse/_scripts/synapse_port_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@
"remote_media_cache": ["authenticated"],
"room_stats_state": ["is_federatable"],
"rooms": ["is_public", "has_auth_chain_index"],
"sliding_sync_joined_rooms": ["is_encrypted"],
"sliding_sync_membership_snapshots": [
"has_known_state",
"is_encrypted",
],
"users": ["shadow_banned", "approved", "locked", "suspended"],
"un_partial_stated_event_stream": ["rejection_status_changed"],
"users_who_share_rooms": ["share_private"],
Expand Down
2 changes: 2 additions & 0 deletions synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ class EventContentFields:
# `m.room.encryption`` algorithm field
ENCRYPTION_ALGORITHM: Final = "algorithm"

TOMBSTONE_SUCCESSOR_ROOM: Final = "replacement_room"


class EventUnsignedContentFields:
"""Fields found inside the 'unsigned' data on events"""
Expand Down
22 changes: 8 additions & 14 deletions synapse/handlers/sliding_sync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@
StreamKeyType,
StreamToken,
)
from synapse.types.handlers import SlidingSyncConfig, SlidingSyncResult
from synapse.types.handlers import (
SLIDING_SYNC_DEFAULT_BUMP_EVENT_TYPES,
SlidingSyncConfig,
SlidingSyncResult,
)
from synapse.types.state import StateFilter
from synapse.util.async_helpers import concurrently_execute
from synapse.visibility import filter_events_for_client
Expand All @@ -75,18 +79,6 @@
)


# The event types that clients should consider as new activity.
DEFAULT_BUMP_EVENT_TYPES = {
EventTypes.Create,
EventTypes.Message,
EventTypes.Encrypted,
EventTypes.Sticker,
EventTypes.CallInvite,
EventTypes.PollStart,
EventTypes.LiveLocationShareStart,
}


class SlidingSyncHandler:
def __init__(self, hs: "HomeServer"):
self.clock = hs.get_clock()
Expand Down Expand Up @@ -986,7 +978,9 @@ async def get_room_sync_data(
# Figure out the last bump event in the room
last_bump_event_result = (
await self.store.get_last_event_pos_in_room_before_stream_ordering(
room_id, to_token.room_key, event_types=DEFAULT_BUMP_EVENT_TYPES
room_id,
to_token.room_key,
event_types=SLIDING_SYNC_DEFAULT_BUMP_EVENT_TYPES,
)
)

Expand Down
9 changes: 8 additions & 1 deletion synapse/storage/controllers/persist_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,15 @@ async def _update_current_state(
"""
state = await self._calculate_current_state(room_id)
delta = await self._calculate_state_delta(room_id, state)
sliding_sync_table_changes = (
await self.persist_events_store._calculate_sliding_sync_table_changes(
room_id, [], delta
)
)

await self.persist_events_store.update_current_state(room_id, delta)
await self.persist_events_store.update_current_state(
room_id, delta, sliding_sync_table_changes
)

async def _calculate_current_state(self, room_id: str) -> StateMap[str]:
"""Calculate the current state of a room, based on the forward extremities
Expand Down
29 changes: 15 additions & 14 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
Iterable,
Iterator,
List,
Mapping,
Optional,
Sequence,
Tuple,
Expand Down Expand Up @@ -1254,9 +1255,9 @@ def simple_upsert_txn(
self,
txn: LoggingTransaction,
table: str,
keyvalues: Dict[str, Any],
values: Dict[str, Any],
insertion_values: Optional[Dict[str, Any]] = None,
keyvalues: Mapping[str, Any],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To allow for TypedDict to be passed here

values: Mapping[str, Any],
insertion_values: Optional[Mapping[str, Any]] = None,
where_clause: Optional[str] = None,
) -> bool:
"""
Expand Down Expand Up @@ -1299,9 +1300,9 @@ def simple_upsert_txn_emulated(
self,
txn: LoggingTransaction,
table: str,
keyvalues: Dict[str, Any],
values: Dict[str, Any],
insertion_values: Optional[Dict[str, Any]] = None,
keyvalues: Mapping[str, Any],
values: Mapping[str, Any],
insertion_values: Optional[Mapping[str, Any]] = None,
where_clause: Optional[str] = None,
lock: bool = True,
) -> bool:
Expand All @@ -1322,7 +1323,7 @@ def simple_upsert_txn_emulated(

if lock:
# We need to lock the table :(
self.engine.lock_table(txn, table)
txn.database_engine.lock_table(txn, table)

def _getwhere(key: str) -> str:
# If the value we're passing in is None (aka NULL), we need to use
Expand Down Expand Up @@ -1376,13 +1377,13 @@ def _getwhere(key: str) -> str:
# successfully inserted
return True

@staticmethod
def simple_upsert_txn_native_upsert(
self,
txn: LoggingTransaction,
table: str,
keyvalues: Dict[str, Any],
values: Dict[str, Any],
insertion_values: Optional[Dict[str, Any]] = None,
keyvalues: Mapping[str, Any],
values: Mapping[str, Any],
insertion_values: Optional[Mapping[str, Any]] = None,
where_clause: Optional[str] = None,
) -> bool:
"""
Expand Down Expand Up @@ -1535,8 +1536,8 @@ def simple_upsert_many_txn_emulated(

self.simple_upsert_txn_emulated(txn, table, _keys, _vals, lock=False)

@staticmethod
def simple_upsert_many_txn_native_upsert(
self,
txn: LoggingTransaction,
table: str,
key_names: Collection[str],
Expand Down Expand Up @@ -1966,8 +1967,8 @@ async def simple_update(
def simple_update_txn(
txn: LoggingTransaction,
table: str,
keyvalues: Dict[str, Any],
updatevalues: Dict[str, Any],
keyvalues: Mapping[str, Any],
updatevalues: Mapping[str, Any],
) -> int:
"""
Update rows in the given database table.
Expand Down
Loading
Loading