29 Commits

Author SHA1 Message Date
David Robertson
1ad581a3ec
recency comparator: avoid 2x map lookups 2023-05-25 11:30:42 +01:00
David Robertson
471c0a26eb
Fix the failing E2E test 2023-05-24 19:26:28 +01:00
David Robertson
3d753ddae7
Per-list bump event types 2023-05-24 19:24:16 +01:00
David Robertson
02c7fbdf4c
Teach SortableRooms its listKey 2023-05-24 18:27:36 +01:00
David Robertson
e6650709da
LastActivityTimestamp -> LastInterestedEventTimestamp 2023-05-24 12:48:04 +01:00
David Robertson
747a5bfb20
Separate LastActivity from LastMessage 2023-05-23 18:33:06 +01:00
David Robertson
fe8f570929
Fixes to context plumbing 2023-04-05 18:31:03 +01:00
David Robertson
d27dc37641
Additional context plumbing 2023-04-05 18:24:02 +01:00
David Robertson
83e746ec3c
Assert with context where possible 2023-04-05 18:24:02 +01:00
Kegan Dougal
8bccdb7342 perf: swap to pointers for s.allRooms
The RoomFinder accesses s.allRooms and is used when sorting the
room list, where we would expect many accesses. Previously, we
returned copies of room metadata, which caused significant amounts
of GC churn, enough to show up on traces.

Swap to using pointers and rename the function to `ReadOnlyRoom(roomID)`
to indicate that it isn't safe to write to this return value.
2023-02-02 11:26:08 +00:00
Kegan Dougal
aa28df161c Rename package -> github.com/matrix-org/sliding-sync 2022-12-15 11:08:50 +00:00
Kegan Dougal
be8543a21a add extensions for typing and receipts; bugfixes and additional perf improvements
Features:
 - Add `typing` extension.
 - Add `receipts` extension.
 - Add comprehensive prometheus `/metrics` activated via `SYNCV3_PROM`.
 - Add `SYNCV3_PPROF` support.
 - Add `by_notification_level` sort order.
 - Add `include_old_rooms` support.
 - Add support for `$ME` and `$LAZY`.
 - Add correct filtering when `*,*` is used as `required_state`.
 - Add `num_live` to each room response to indicate how many timeline entries are live.

Bug fixes:
 - Use a stricter comparison function on ranges: fixes an issue whereby UTs fail on go1.19 due to change in sorting algorithm.
 - Send back an `errcode` on HTTP errors (e.g expired sessions).
 - Remove `unsigned.txn_id` on insertion into the DB. Otherwise other users would see other users txn IDs :(
 - Improve range delta algorithm: previously it didn't handle cases like `[0,20] -> [20,30]` and would panic.
 - Send HTTP 400 for invalid range requests.
 - Don't publish no-op unread counts which just adds extra noise.
 - Fix leaking DB connections which could eventually consume all available connections.
 - Ensure we always unblock WaitUntilInitialSync even on invalid access tokens. Other code relies on WaitUntilInitialSync() actually returning at _some_ point e.g on startup we have N workers which bound the number of concurrent pollers made at any one time, we need to not just hog a worker forever.

Improvements:
 - Greatly improve startup times of sync3 handlers by improving `JoinedRoomsTracker`: a modest amount of data would take ~28s to create the handler, now it takes 4s.
 - Massively improve initial initial v3 sync times, by refactoring `JoinedRoomsTracker`, from ~47s to <1s.
 - Add `SlidingSyncUntil...` in tests to reduce races.
 - Tweak the API shape of JoinedUsersForRoom to reduce state block processing time for large rooms from 63s to 39s.
 - Add trace task for initial syncs.
 - Include the proxy version in UA strings.
 - HTTP errors now wait 1s before returning to stop clients tight-looping on error.
 - Pending event buffer is now 2000.
 - Index the room ID first to cull the most events when returning timeline entries. Speeds up `SelectLatestEventsBetween` by a factor of 8.
 - Remove cancelled `m.room_key_requests` from the to-device inbox. Cuts down the amount of events in the inbox by ~94% for very large (20k+) inboxes, ~50% for moderate sized (200 events) inboxes. Adds book-keeping to remember the unacked to-device position for each client.
2022-12-14 18:53:55 +00:00
Kegan Dougal
dcad80f51f bugfix: send correct deltas for deletions at the front of windows
Previously we wouldn't send deletions for this, even though they shift
all elements to the left. Add a battery of unit tests for the list delta
algorithm, and standardise on the practice of issuing a DELETE prior to
an INSERT for newly inserted rooms, regardless of where in the window
they appear. Previously, we may skip the DELETE at the end of the list,
which was just inconsistent of us.
2022-08-31 17:54:07 +01:00
Kegan Dougal
10bd0da932 refactor: add ops.go to calculate list ops
re-jig ConnState to use this new function, with unit tests.
2022-08-31 13:43:09 +01:00
Kegan Dougal
19f8b4dbf7 refactor: add RoomFinder and use it in InternalRequestLists
This is part of a series of refactors aimed to improve the performance
and complexity of calculating list deltas, which up until now exists in
its current form due to organic growth of the codebase.

This specific refactor introduces a new interface `RoomFinder` which
can map room IDs to `*RoomConnMetadata` which is used by `ConnState`.
All the sliding sync lists now use the `RoomFinder` instead of keeping
their own copies of `RoomConnMetadata`, meaning per-connection, rooms
just have 1 copy in-memory. This cuts down on memory usage as well as
cuts down on GC churn as we would constantly be replacing N rooms for
each update, where N is the total number of lists on that connection.
For Element-Web, N=7 currently to handle Favourites, Low Priority, DMs,
Rooms, Spaces, Invites, Search. This also has the benefit of creating
a single source of truth in `InternalRequestLists.allRooms` which can
be updated once and then a list of list deltas can be calculated off
the back of that. Previously, `allRooms` was _only_ used to seed new
lists, which created a weird imbalance as we would need to update both
`allRooms` _and_ each `FilteredSortableRooms` to keep things in-sync.

This refactor is incomplete in its present form, as we need to make
use of the new `RoomDelta` struct to efficiently package list updates.
2022-08-26 10:09:41 +01:00
Kegan Dougal
657f8ccc5d Use pointers to RoomConnMetadata 2022-08-25 15:30:07 +01:00
Kegan Dougal
bcb1c42ccb bugfix: update the roomIDToIndex map when rooms are removed
We weren't doing this previously, but things didn't blow up because
we would almost always call resort() shortly afterwards which _would_
update the map with the new sort positions. In some cases this could
cause the lists to be sorting with incorrect index positions, notably:
 - when an invite is retired.
 - when a room no longer meets filter criteria and is removed.

This could be a major source of duplicate rooms.
2022-08-16 15:56:16 +01:00
Kegan Dougal
59cddd08c7 bugfix: update the 'name' field on rooms when relevant actions occur
Relevant actions include:
 - People joining/leaving a room
 - An m.room.name or m.room.canonical_alias event is sent
 - etc..

Prior to this, we just set the room name field for initial=true
rooms only. This meant that if a room name was updated whilst it was
in the visible range (or currently subscribed to), we wouldn't set
this field resulting in stale names for clients. This was particularly
prominent when you created a room, as the initial member event would
cause the room to appear in the list as "Empty room" which then would
never be updated even if there was a subsequent `m.room.name` event
sent.

Fixed with regression tests.
2022-08-11 15:07:36 +01:00
Kegan Dougal
54e1cfbb0e bugfix: ensure newly joined live-stream rooms don't cause 500s
This was caused by the GlobalCache not having a metadata entry for
the new room, which in some cases prevented a stub from being made.

With regression test.
2022-08-10 19:48:03 +01:00
Kegan Dougal
94af21830e refactor: further consolidate live update code to be more consistent
Also fix typos in tests which would cause pointers to print on failure
not the integers they are pointing at.
2022-03-24 14:29:15 +00:00
Kegan Dougal
bd2fa1d74d Add stub account_data extension; migrate UserCache/GlobalCache to caches pkg
caches pkg is required to avoid import loops as sync3 depends on extensions
as the extension type is embedded in the `sync3.Request`, but extensions will
need to know about the caches for account data to function correctly.
2022-03-23 16:34:43 +00:00
Kegan Dougal
70f54fe2d2 Better comments 2022-03-23 14:13:59 +00:00
Kegan Dougal
10ae8c0797 Add UserRoomData to filter checks and thread it through
This will allow us to apply filters on user-specific room data
such as if the user is invited or if the user has this room as
a DM room or not.
2021-11-09 10:36:29 +00:00
Kegan Dougal
c91e2c47b5 Return deleted index when rooms get filtered out when streaming
With tests
2021-11-08 16:49:21 +00:00
Kegan Dougal
8951c3737d Add FilteredSortableRooms which wraps SortableRooms with filters 2021-11-08 16:21:02 +00:00
Kegan Dougal
33cf1542aa Add Assert() function which works like C assert()
Particularly as the server expands into multiple lists and
filters, having a way to quickly detect off-by-one index
errors is important, so add an assert() function which
will panic() if SYNCV3_DEBUG=1 else log an angry message.
2021-11-08 13:04:03 +00:00
Kegan Dougal
11b1260d07 Split sync3 into sync3 and sync3/handler
`sync3` contains data structures and logic which is very isolated and
testable (think ConnMap, Room, Request, SortableRooms, etc) whereas
`sync3/handler` contains control flow which calls into `sync3` data
structures.

This has numerous benefits:
 - Gnarly complicated structs like `ConnState` are now more isolated
   from the codebase, forcing better API design on `sync3` structs.
 - The inability to do import cycles forces structs in `sync3` to remain
   simple: they cannot pull in control flow logic from `sync3/handler`
   without causing a compile error.
 - It's significantly easier to figure out where to start looking for
   code that executes when a new request is received, for new developers.
 - It simplifies the number of things that `ConnState` can touch. Previously
   we were gut wrenching out of convenience but now we're forced to move
   more logic from `ConnState` into `sync3` (depending on the API design).
   For example, adding `SortableRooms.RoomIDs()`.
2021-11-05 15:45:04 +00:00
Kegan Dougal
12bdf20f61 Factor out code which touches sortedJoinedRooms to SortableRooms
In preparation for multiple SortableRooms (for non-E2EE and E2EE)
2021-11-04 18:02:58 +00:00
Kegan Dougal
cc2c3df1eb Implement all sort orders and allow them to mix 2021-11-02 11:09:26 +00:00