54 Commits

Author SHA1 Message Date
David Robertson
f264c4ff90
Ditch the alphabet 2023-09-20 14:23:11 +01:00
David Robertson
ed7f052682
A bunch of comments from review 2023-09-20 14:01:15 +01:00
David Robertson
41ed56ecd7
Tweak test prose 2023-09-19 15:37:41 +01:00
David Robertson
fa227b79d3
Stop loading timelines if you hit missing_previous 2023-09-13 19:17:53 +01:00
Kegan Dougal
9209c61691 bugfix: set unsigned.redacted_because field on redaction
Element X relies on this field being set.
2023-09-07 10:16:00 +01:00
Kegan Dougal
b2c26b7e93 Redact events in the DB on m.room.redaction
Fixes #279
2023-08-31 17:06:44 +01:00
Kegan Dougal
ae29d14c6f Remove unused code 2023-07-19 15:56:43 +01:00
Kegan Dougal
baa5d05d31 Use the rooms table initially when querying latest nids 2023-07-13 18:19:00 +01:00
David Robertson
1717408dc3
Use fewer DB conns when events into the UserCache 2023-06-19 17:58:56 +01:00
Kegan Dougal
5d619fdfb5 Add LatestEvents struct and use it
This provides a back-channel to send event NIDs
from LL timeilnes to connections.
2023-06-13 10:02:52 +01:00
Kegan Dougal
efaf2648eb Merge branch 'main' into kegan/accurate-load-positions 2023-06-13 08:44:21 +01:00
David Robertson
3965001abe
Use int64 for event NIDs 2023-06-10 14:13:16 +01:00
Kegan Dougal
600c58acf3 Load loadPositions on conn startup 2023-06-09 17:28:01 +01:00
David Robertson
275538487b
Revert "Pass room ID to new query"
This reverts commit 6c9aa094ab10f4ddb066562841bf0f1f80773416.
2023-04-24 12:45:11 +01:00
David Robertson
6c9aa094ab
Pass room ID to new query 2023-04-24 12:04:23 +01:00
David Robertson
8324f6e654
SelectByIDs has an ordering guarantee 2023-04-22 00:36:39 +01:00
David Robertson
aa09d12d05
Check the new query reports multiple unknown IDs 2023-04-22 00:24:48 +01:00
David Robertson
bea931d8f2
Fix running unit test alongside other tests
Some txns are commited, so the tests aren't isolated.
2023-04-19 16:20:08 +01:00
David Robertson
b32e5dacaa
Fix test function args too
Need to work out some kind of local linter here
2023-04-19 15:27:57 +01:00
David Robertson
5a9fae1dea
Fix capitalisation 2023-04-19 15:26:34 +01:00
David Robertson
5dd43155b2
Test new events table query 2023-04-19 15:09:28 +01:00
Kegan Dougal
a7eed93722 Add comprehensive regression test for GlobalSnapshot(); ensure we clear db conns when tests end 2023-01-18 14:54:26 +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
1a55076478 Add NewJoinEvent shorthand for tests 2022-07-12 15:12:02 +01:00
Kegan Dougal
1380a71f80 bugfix: fix several issues which could cause corrupt state snapshots
A fundamental assumption in the proxy has been that the order of events
in `timeline` in v2 will be the same all the time. There's some evidence
to suggest this isn't true in the wild. This commit refactors the proxy
to not assume this. It does this by:
  - Not relying on the number of newly inserted rows and slicing the events
    to figure out _which_ events are new. Now the INSERT has `RETURNING event_id, event_nid`
    and we return a map from event ID to event NID to explicitly say which
    events are new.
  - Add more paranoia when calculating new state snapshots: if we see the
    same (type, state key) tuple more than once in a snapshot we error out.
  - Add regression tests which try to insert events out of order to trip the
    proxy up.
2022-06-08 18:20:10 +01:00
Kegan Dougal
17cc4e6ec1 perf: reduce the number of SQL queries further when pulling required_state 2022-04-25 20:35:27 +01:00
Kegan Dougal
dd6e6da50c Inject prev_batch values into timeline UserRoomData 2022-03-31 15:10:42 +01:00
Kegan Dougal
5dc1c38764 Add prev_batch column to events table
This will be used to return prev batch tokens to the client
on a best-effort basis.
2022-03-31 14:29:26 +01:00
Kegan Dougal
15a4b5a903 bugfix: gracefully handle seeing the same event in the timeline of a /sync response
Whilst this goes against the spec and is likely a Synapse bug, we still need to
work in the real world so handle it, log it, and add a regression test for it.
2022-03-18 10:59:38 +00:00
Kegan Dougal
10f94336ba state: rewrite SELECT ... IN to be SELECT ... ANY
Using ANY allows us to give a single parameter containing many many
entries, which bypasses the postgres parameter limit of 65535. Without
this, large rooms like Matrix HQ which have current state >65535 events
will not be stored correctly.

Add torture test to the events table to assert that we can query >65535
events.
2022-02-22 17:38:18 +00:00
Kegan Dougal
a9a57ddfda Remove unused function 2021-10-28 12:22:22 +01:00
Kegan Dougal
51e6ac5469 HeroInfoForAllRooms: add queries for join/invite counts 2021-10-27 11:01:28 +01:00
Kegan Dougal
300e82e988 Finish tests for membership column 2021-10-27 10:13:57 +01:00
Kegan Dougal
0b58b0f62b Store membership changes in events table 2021-10-26 18:39:22 +01:00
Kegan Dougal
a895f64af5 Namespace user ID per test to avoid test failure 2021-10-21 18:14:31 +01:00
Kegan Dougal
7036fdd54f Add SelectEventsWithTypeStateKeyInRooms in prep for loading visible event nid ranges for a subset of rooms 2021-10-21 18:13:06 +01:00
Kegan Dougal
e04b38726a Fix a bug which can happen when v2 sync returns dupe events
Add regression tests as well.
2021-10-01 16:55:50 +01:00
Kegan Dougal
92e8085910 Fix blocking test 2021-10-01 12:51:38 +01:00
Kegan Dougal
c8b59e75af Store event as BYTEA not JSONB
Because JSONB doesn't allow the valid JSON character `\u0000`
(the null byte). See https://www.postgresql.org/message-id/CAE37PpOn%3DMcGeokmny4tm4FTHmXSG4KydgUJemKqT9XxkrrTmg%40mail.gmail.com

We don't pull out fields from the JSON anymore in queries, so
this is fine to be stored as an opaque binary blob.
2021-10-01 12:09:23 +01:00
Kegan Dougal
ac72b9b10b Add SortableRoom for globally sortable room fields (name, last timestamp)
- Fix a few issues around pulling current state for a room.
- Add a function to extract the most recent message in a room, for
  pre-populating the last timestamp.
2021-09-22 17:44:40 +01:00
Kegan Dougal
65cbdb07c8 Add type|state_key cols to events table; refactor select in
- Add `verifyAll` flag to assert if all events should be in the SELECT result
- Factor out `testutils.NewStateEvent`
2021-08-20 15:56:17 +01:00
Kegan Dougal
45e9e432bc Track the state before on each event rather than after
It's easier to roll forward than roll backwards. Add 'replaces_nid' field
on the events table which tells which nid in the snapshot gets replaced, if any.
2021-08-18 18:21:40 +01:00
Kegan Dougal
8490a68066 Completely re-work how snapshots get calculated
Make them after every state event as we cannot batch them per v2 /sync response
2021-08-18 16:34:44 +01:00
Kegan Dougal
04f52e5dc6 Add limited test for room_member 2021-08-06 18:12:41 +01:00
Kegan Dougal
60c3368279 Add AfterEpochSnapshotID to events table 2021-08-06 12:38:21 +01:00
Kegan Dougal
a8ced5411d room_member stream: implement streaming data in range, start work on pagination
This stream uses the event NID, not its own position. Add a snapshot ID to events
in the events table in preparation for loading state snapshots from an event NID.
2021-08-06 12:14:03 +01:00
Kegan Dougal
bcfe9b051f Add to_device table; factor out Chunkify so we can re-use it 2021-08-02 16:45:09 +01:00
Kegan Dougal
747d85e87c Map Auth header to user via /whoami; add query for highest nid
Add stub room list stream
2021-06-16 18:56:31 +01:00
Kegan Dougal
159530bed1 Add v2 polling; chunk event insertion to support Matrix HQ 2021-06-04 13:02:28 +01:00