summary refs log tree commit diff
path: root/synapse/storage/databases/main/roommember.py (follow)
Commit message (Collapse)AuthorAgeFilesLines
* Remove superfluous `room_memberships` join from background update (#15733)Eric Eastwood2023-06-071-1/+0
| | | Spawning from https://github.com/matrix-org/synapse/pull/15731
* No need for the extra join since `membership` is built-in to ↵Eric Eastwood2023-06-061-4/+3
| | | | | | | `current_state_events` (#15731) This helps with the upstream `is_host_joined()` and `is_host_invited()` functions. `membership` was added to `current_state_events` in https://github.com/matrix-org/synapse/pull/5706 and forced in https://github.com/matrix-org/synapse/pull/13745
* Improve type hints for cached decorator. (#15658)Patrick Cloke2023-05-241-1/+1
| | | | | The cached decorators always return a Deferred, which was not properly propagated. It was close enough when wrapping coroutines, but failed if a bare function was wrapped.
* Add config option to forget rooms automatically when users leave them (#15224)Sean Quah2023-05-031-24/+45
| | | | | This is largely based off the stats and user directory updater code. Signed-off-by: Sean Quah <seanq@matrix.org>
* Speed up membership queries for users with forgotten rooms (#15385)Erik Johnston2023-04-041-1/+11
|
* Return read-only collections from `@cached` methods (#13755)Sean Quah2023-02-101-10/+9
| | | | | | | | | | | | | It's important that collections returned from `@cached` methods are not modified, otherwise future retrievals from the cache will return the modified collection. This applies to the return values from `@cached` methods and the values inside the dictionaries returned by `@cachedList` methods. It's not necessary for the dictionaries returned by `@cachedList` methods themselves to be read-only. Signed-off-by: Sean Quah <seanq@matrix.org> Co-authored-by: David Robertson <davidr@element.io>
* Fetch fewer events when getting hosts in room (#14962)David Robertson2023-02-021-2/+44
|
* Faster joins: omit partial rooms from eager syncs until the resync completes ↵David Robertson2023-01-231-5/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (#14870) * Allow `AbstractSet` in `StrCollection` Or else frozensets are excluded. This will be useful in an upcoming commit where I plan to change a function that accepts `List[str]` to accept `StrCollection` instead. * `rooms_to_exclude` -> `rooms_to_exclude_globally` I am about to make use of this exclusion mechanism to exclude rooms for a specific user and a specific sync. This rename helps to clarify the distinction between the global config and the rooms to exclude for a specific sync. * Better function names for internal sync methods * Track a list of excluded rooms on SyncResultBuilder I plan to feed a list of partially stated rooms for this sync to ignore * Exclude partial state rooms during eager sync using the mechanism established in the previous commit * Track un-partial-state stream in sync tokens So that we can work out which rooms have become fully-stated during a given sync period. * Fix mutation of `@cached` return value This was fouling up a complement test added alongside this PR. Excluding a room would mean the set of forgotten rooms in the cache would be extended. This means that room could be erroneously considered forgotten in the future. Introduced in #12310, Synapse 1.57.0. I don't think this had any user-visible side effects (until now). * SyncResultBuilder: track rooms to force as newly joined Similar plan as before. We've omitted rooms from certain sync responses; now we establish the mechanism to reintroduce them into future syncs. * Read new field, to present rooms as newly joined * Force un-partial-stated rooms to be newly-joined for eager incremental syncs only, provided they're still fully stated * Notify user stream listeners to wake up long polling syncs * Changelog * Typo fix Co-authored-by: Sean Quah <8349537+squahtx@users.noreply.github.com> * Unnecessary list cast Co-authored-by: Sean Quah <8349537+squahtx@users.noreply.github.com> * Rephrase comment Co-authored-by: Sean Quah <8349537+squahtx@users.noreply.github.com> * Another comment Co-authored-by: Sean Quah <8349537+squahtx@users.noreply.github.com> * Fixup merge(?) * Poke notifier when receiving un-partial-stated msg over replication * Fixup merge whoops Thanks MV :) Co-authored-by: Mathieu Velen <mathieuv@matrix.org> Co-authored-by: Mathieu Velten <mathieuv@matrix.org> Co-authored-by: Sean Quah <8349537+squahtx@users.noreply.github.com>
* Include heroes in partial join responses' state (#14442)David Robertson2022-11-151-0/+30
| | | | | | | | | | | * Pull out hero selection logic * Include heroes in partial join response's state * Changelog * Fixup trial test * Remove TODO
* Check appservice user interest against the local users instead of all users ↵Eric Eastwood2022-10-271-0/+3
| | | | (`get_users_in_room` mis-use) (#13958)
* Fix presence bug introduced in 1.64 by #13313 (#14243)Mathieu Velten2022-10-271-2/+1
| | | | | | | | | | | | | * Fix presence bug introduced in 1.64 by #13313 Signed-off-by: Mathieu Velten <mathieuv@matrix.org> * Add changelog * Add DISTINCT * Apply suggestions from code review Signed-off-by: Mathieu Velten <mathieuv@matrix.org>
* Bump flake8-bugbear from 21.3.2 to 22.9.23 (#14042)dependabot[bot]2022-10-191-2/+2
| | | | | Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Erik Johnston <erik@matrix.org> Co-authored-by: David Robertson <davidr@element.io>
* Batch up calls to `get_rooms_for_users` (#14109)Nick Mills-Barrett2022-10-121-1/+16
|
* Fix performance regression in `get_users_in_room` (#13972)Erik Johnston2022-09-301-56/+73
| | | | | Fixes #13942. Introduced in #13575. Basically, let's only get the ordered set of hosts out of the DB if we need an ordered set of hosts. Since we split the function up the caching won't be as good, but I think it will still be fine as e.g. multiple backfill requests for the same room will hit the cache.
* Optimise get_rooms_for_user (drop with_stream_ordering) (#13787)Nick Mills-Barrett2022-09-291-61/+56
|
* Handle the case of remote users leaving a partial join room for device lists ↵Erik Johnston2022-09-271-20/+26
| | | | (#13885)
* Deduplicate `is_server_notices_room`. (#13780)reivilibre2022-09-141-0/+17
|
* Remove check current state membership up to date (#13745)Nick Mills-Barrett2022-09-121-155/+47
| | | | | | | * Remove checks for membership column in current_state_events * Add schema script to force through the `current_state_events_membership` background job Contributed by Nick @ Beeper (@fizzadar).
* Update docstrings to explain the impact of partial state (#13750)Sean Quah2022-09-081-1/+16
| | | | | | | Update the docstrings for `get_users_in_room` and `get_current_hosts_in_room` to explain the impact of partial state. Signed-off-by: Sean Quah <seanq@matrix.org>
* Avoid raising errors due to malformed IDs in `get_current_hosts_in_room` ↵Sean Quah2022-09-081-1/+4
| | | | | | | | | | (#13748) Handle malformed user IDs with no colons in `get_current_hosts_in_room`. It's not currently possible for a malformed user ID to join a room, so this error would never be hit. Signed-off-by: Sean Quah <seanq@matrix.org>
* Cancel the processing of key query requests when they time out. (#13680)reivilibre2022-09-071-0/+2
|
* Remove cached wrap on `_get_joined_users_from_context` method (#13569)Nick Mills-Barrett2022-08-311-84/+38
| | | | | | | The method doesn't actually do any data fetching and the method that does, `_get_joined_profile_from_event_id`, has its own cache. Signed off by Nick @ Beeper (@Fizzadar).
* Optimize how we calculate `likely_domains` during backfill (#13575)Eric Eastwood2022-08-301-17/+71
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Optimize how we calculate `likely_domains` during backfill because I've seen this take 17s in production just to `get_current_state` which is used to `get_domains_from_state` (see case [*2. Loading tons of events* in the `/messages` investigation issue](https://github.com/matrix-org/synapse/issues/13356)). There are 3 ways we currently calculate hosts that are in the room: 1. `get_current_state` -> `get_domains_from_state` - Used in `backfill` to calculate `likely_domains` and `/timestamp_to_event` because it was cargo-culted from `backfill` - This one is being eliminated in favor of `get_current_hosts_in_room` in this PR 🕳 1. `get_current_hosts_in_room` - Used for other federation things like sending read receipts and typing indicators 1. `get_hosts_in_room_at_events` - Used when pushing out events over federation to other servers in the `_process_event_queue_loop` Fix https://github.com/matrix-org/synapse/issues/13626 Part of https://github.com/matrix-org/synapse/issues/13356 Mentioned in [internal doc](https://docs.google.com/document/d/1lvUoVfYUiy6UaHB6Rb4HicjaJAU40-APue9Q4vzuW3c/edit#bookmark=id.2tvwz3yhcafh) ### Query performance #### Before The query from `get_current_state` sucks just because we have to get all 80k events. And we see almost the exact same performance locally trying to get all of these events (16s vs 17s): ``` synapse=# SELECT type, state_key, event_id FROM current_state_events WHERE room_id = '!OGEhHVWSdvArJzumhm:matrix.org'; Time: 16035.612 ms (00:16.036) synapse=# SELECT type, state_key, event_id FROM current_state_events WHERE room_id = '!OGEhHVWSdvArJzumhm:matrix.org'; Time: 4243.237 ms (00:04.243) ``` But what about `get_current_hosts_in_room`: When there is 8M rows in the `current_state_events` table, the previous query in `get_current_hosts_in_room` took 13s from complete freshness (when the events were first added). But takes 930ms after a Postgres restart or 390ms if running back to back to back. ```sh $ psql synapse synapse=# \timing on synapse=# SELECT COUNT(DISTINCT substring(state_key FROM '@[^:]*:(.*)$')) FROM current_state_events WHERE type = 'm.room.member' AND membership = 'join' AND room_id = '!OGEhHVWSdvArJzumhm:matrix.org'; count ------- 4130 (1 row) Time: 13181.598 ms (00:13.182) synapse=# SELECT COUNT(*) from current_state_events where room_id = '!OGEhHVWSdvArJzumhm:matrix.org'; count ------- 80814 synapse=# SELECT COUNT(*) from current_state_events; count --------- 8162847 synapse=# SELECT pg_size_pretty( pg_total_relation_size('current_state_events') ); pg_size_pretty ---------------- 4702 MB ``` #### After I'm not sure how long it takes from complete freshness as I only really get that opportunity once (maybe restarting computer but that's cumbersome) and it's not really relevant to normal operating times. Maybe you get closer to the fresh times the more access variability there is so that Postgres caches aren't as exact. Update: The longest I've seen this run for is 6.4s and 4.5s after a computer restart. After a Postgres restart, it takes 330ms and running back to back takes 260ms. ```sh $ psql synapse synapse=# \timing on Timing is on. synapse=# SELECT substring(c.state_key FROM '@[^:]*:(.*)$') as host FROM current_state_events c /* Get the depth of the event from the events table */ INNER JOIN events AS e USING (event_id) WHERE c.type = 'm.room.member' AND c.membership = 'join' AND c.room_id = '!OGEhHVWSdvArJzumhm:matrix.org' GROUP BY host ORDER BY min(e.depth) ASC; Time: 333.800 ms ``` #### Going further To improve things further we could add a `limit` parameter to `get_current_hosts_in_room`. Realistically, we don't need 4k domains to choose from because there is no way we're going to query that many before we a) probably get an answer or b) we give up. Another thing we can do is optimize the query to use a index skip scan: - https://wiki.postgresql.org/wiki/Loose_indexscan - Index Skip Scan, https://commitfest.postgresql.org/37/1741/ - https://www.timescale.com/blog/how-we-made-distinct-queries-up-to-8000x-faster-on-postgresql/
* Directly lookup local membership instead of getting all members in a room ↵Eric Eastwood2022-08-241-0/+26
| | | | | first (`get_users_in_room` mis-use) (#13608) See https://github.com/matrix-org/synapse/pull/13575#discussion_r953023755
* Fix regression caused by #13573 (#13600)Erik Johnston2022-08-231-4/+6
| | | Broke in #13573.
* Cache user IDs instead of profile objects (#13573)Nick Mills-Barrett2022-08-231-38/+29
| | | The profile objects are never used and increase cache size significantly.
* Add forgotten status to Room Details API (#13503)Dirk Klimpel2022-08-171-0/+24
|
* Faster room joins: make `/joined_members` block whilst the room is partial ↵reivilibre2022-08-161-0/+3
| | | | stated. (#13514)
* Optimise async get event lookups (#13435)Nick Mills-Barrett2022-08-041-1/+1
| | | | | | Still maintains local in memory lookup optimisation, but does any external lookup as part of the deferred that prevents duplicate lookups for the same event at once. This makes the assumption that fetching from an external cache is a non-zero load operation.
* Refactor presence so we can prune user in room caches (#13313)Erik Johnston2022-07-251-15/+68
| | | | | | | | See #10826 and #10786 for context as to why we had to disable pruning on those caches. Now that `get_users_who_share_room_with_user` is called frequently only for presence, we just need to make calls to it less frequent and then we can remove the various levels of caching that is going on.
* Increase batch size of `bulk_get_push_rules` and ↵Shay2022-07-181-1/+1
| | | | `_get_joined_profiles_from_event_ids`. (#13300)
* Improve performance of query ` _get_subset_users_in_room_with_profiles` (#13299)Shay2022-07-181-1/+1
|
* Async get event cache prep (#13242)Nick Mills-Barrett2022-07-151-1/+3
| | | | | Some experimental prep work to enable external event caching based on #9379 & #12955. Doesn't actually move the cache at all, just lays the groundwork for async implemented caches. Signed off by Nick @ Beeper (@Fizzadar)
* Don't pull out state in `compute_event_context` for unconflicted state (#13267)Erik Johnston2022-07-141-27/+8
|
* Don't pull out the full state when calculating push actions (#13078)Erik Johnston2022-07-111-0/+86
|
* Reduce state pulled from DB due to sending typing and receipts over ↵Erik Johnston2022-06-061-0/+37
| | | | | federation (#12964) Reducing the amount of state we pull from the DB is useful as fetching state is expensive in terms of DB, CPU and memory.
* Mutual rooms: Remove dependency on user directory (#12836)Jonathan de Jong2022-05-301-0/+24
|
* allow `on_invalidate=None` in `@cached` methods (#12769)David Robertson2022-05-171-1/+2
|
* Add some type hints to datastore (#12717)Dirk Klimpel2022-05-171-43/+83
|
* Refactor and convert `Linearizer` to async (#12357)Sean Quah2022-04-051-1/+1
| | | | | | | | | | | Refactor and convert `Linearizer` to async. This makes a `Linearizer` cancellation bug easier to fix. Also refactor to use an async context manager, which eliminates an unlikely footgun where code that doesn't immediately use the context manager could forget to release the lock. Signed-off-by: Sean Quah <seanq@element.io>
* Add a configuration to exclude rooms from sync response (#12310)Brendan Abolivier2022-03-301-5/+16
|
* Add cache for `get_membership_from_event_ids` (#12272)Erik Johnston2022-03-251-4/+33
| | | | | This should speed up push rule calculations for rooms with large numbers of local users when the main push rule cache fails. Co-authored-by: reivilibre <oliverw@matrix.org>
* Remove some unused variables/parameters. (#12187)Patrick Cloke2022-03-091-9/+5
|
* Optimise calculating device_list changes in `/sync`. (#11974)Erik Johnston2022-02-151-0/+62
| | | | | | For users with large accounts it is inefficient to calculate the set of users they share a room with (and takes a lot of space in the cache). Instead we can look at users whose devices have changed since the last sync and check if they share a room with the syncing user.
* Use auto_attribs/native type hints for attrs classes. (#11692)Patrick Cloke2022-01-131-3/+3
|
* Type hint the constructors of the data store classes (#11555)Sean Quah2021-12-131-4/+19
|
* Disambiguate queries on `state_key` (#11497)Richard van der Hoff2021-12-021-2/+2
| | | | | We're going to add a `state_key` column to the `events` table, so we need to add some disambiguation to queries which use it.
* Add remaining type hints to `synapse.events`. (#11098)Patrick Cloke2021-11-021-3/+5
|
* Add type hints for most `HomeServer` parameters (#11095)Sean Quah2021-10-221-3/+4
|
* Use direct references for configuration variables (part 4). (#10893)Patrick Cloke2021-09-231-1/+1
|
* Opt out of cache expiry for `get_users_who_share_room_with_user` (#10826)David Robertson2021-09-221-3/+8
| | | | * Allow LruCaches to opt out of time-based expiry * Don't expire `get_users_who_share_room` & friends
* Use direct references for some configuration variables (#10798)Patrick Cloke2021-09-131-1/+1
| | | | Instead of proxying through the magic getter of the RootConfig object. This should be more performant (and is more explicit).
* Easy refactors of the user directory (#10789)David Robertson2021-09-101-0/+5
| | | No functional changes here. This came out as I was working to tackle #5677
* Do not include rooms with an unknown room version in a sync response. (#10644)Patrick Cloke2021-08-191-3/+5
| | | | A user will still see this room if it is in a local cache, but it will not reappear if clearing the cache and reloading.
* Convert room member storage tuples to attrs. (#10629)Patrick Cloke2021-08-181-2/+6
| | | | Instead of using namedtuples. This helps with asserting type hints and code completion.
* Improve event caching code (#10119)Erik Johnston2021-08-041-4/+2
| | | | Ensure we only load an event from the DB once when the same event is requested multiple times at once.
* [pyupgrade] `synapse/` (#10348)Jonathan de Jong2021-07-191-1/+1
| | | | | | | | | This PR is tantamount to running ``` pyupgrade --py36-plus --keep-percent-format `find synapse/ -type f -name "*.py"` ``` Part of #9744
* Show all joinable rooms in the spaces summary. (#10298)Patrick Cloke2021-07-131-2/+11
| | | | | | | | | | Previously only world-readable rooms were shown. This means that rooms which are public, knockable, or invite-only with a pending invitation, are included in a space summary. It also applies the same logic to the experimental room version from MSC3083 -- if a user has access to the proper allowed rooms then it is shown in the spaces summary. This change is made per MSC3173 allowing stripped state of a room to be shown to any potential room joiner.
* Fix incorrect time magnitude on delayed call (#10195)Andrew Morgan2021-06-171-1/+1
| | | | | | | | | Fixes https://github.com/matrix-org/synapse/issues/10030. We were expecting milliseconds where we should have provided a value in seconds. The impact of this bug isn't too bad. The code is intended to count the number of remote servers that the homeserver can see and report that as a metric. This metric is supposed to run initially 1 second after server startup, and every 60s as well. Instead, it ran 1,000 seconds after server startup, and every 60s after startup. This fix allows for the correct metrics to be collected immediately, as well as preventing a random collection 1,000s in the future after startup.
* Use get_current_users_in_room from store and not StateHandler (#9910)Erik Johnston2021-05-051-2/+6
|
* Only store data in caches, not "smart" objects (#9845)Erik Johnston2021-04-231-72/+89
|
* Remove `synapse.types.Collection` (#9856)Richard van der Hoff2021-04-221-2/+12
| | | This is no longer required, since we have dropped support for Python 3.5.
* User directory: use calculated room membership state instead (#9821)Andrew Morgan2021-04-161-0/+27
| | | | | Fixes: #9797. Should help reduce CPU usage on the user directory, especially when memberships change in rooms with lots of state history.
* Remove redundant "coding: utf-8" lines (#9786)Jonathan de Jong2021-04-141-1/+0
| | | | | | | Part of #9744 Removes all redundant `# -*- coding: utf-8 -*-` lines from files, as python 3 automatically reads source code as utf-8 now. `Signed-off-by: Jonathan de Jong <jonathan@automatia.nl>`
* Update black, and run auto formatting over the codebase (#9381)Eric Eastwood2021-02-161-8/+9
| | | | | | | - Update black version to the latest - Run black auto formatting over the codebase - Run autoformatting according to [`docs/code_style.md `](https://github.com/matrix-org/synapse/blob/80d6dc9783aa80886a133756028984dbf8920168/docs/code_style.md) - Update `code_style.md` docs around installing black to use the correct version
* Use execute_batch in more places (#9188)Erik Johnston2021-01-211-5/+1
| | | | | * Use execute_batch in more places * Newsfile
* Speed up remote invite rejection database call (#8815)Andrew Morgan2020-11-251-1/+33
| | | | | | | | | | | | | This is another PR that grew out of #6739. The existing code for checking whether a user is currently invited to a room when they want to leave the room looks like the following: https://github.com/matrix-org/synapse/blob/f737368a26bb9eea401fcc3a5bdd7e0b59e91f09/synapse/handlers/room_member.py#L518-L540 It calls `get_invite_for_local_user_in_room`, which will actually query *all* rooms the user has been invited to, before iterating over them and matching via the room ID. It will then return a tuple of a lot of information which we pull the event ID out of. I need to do a similar check for knocking, but this code wasn't very efficient. I then tried to write a different implementation using `StateHandler.get_current_state` but this actually didn't work as we haven't *joined* the room yet - we've only been invited to it. That means that only certain tables in Synapse have our desired `invite` membership state. One of those tables is `local_current_membership`. So I wrote a store method that just queries that table instead
* Consistently use wrap_as_background_task in more places (#8599)Patrick Cloke2020-10-201-9/+7
|
* Add `DeferredCache.get_immediate` method (#8568)Richard van der Hoff2020-10-191-1/+1
| | | | | | | | | | | * Add `DeferredCache.get_immediate` method A bunch of things that are currently calling `DeferredCache.get` are only really interested in the result if it's completed. We can optimise and simplify this case. * Remove unused 'default' parameter to DeferredCache.get() * another get_immediate instance
* Move additional tasks to the background worker (#8458)Patrick Cloke2020-10-071-1/+4
|
* Add logging on startup/shutdown (#8448)Erik Johnston2020-10-021-10/+3
| | | | | This is so we can tell what is going on when things are taking a while to start up. The main change here is to ensure that transactions that are created during startup get correctly logged like normal transactions.
* Add EventStreamPosition type (#8388)Erik Johnston2020-09-241-5/+9
| | | | | | | | | | | | | | The idea is to remove some of the places we pass around `int`, where it can represent one of two things: 1. the position of an event in the stream; or 2. a token that partitions the stream, used as part of the stream tokens. The valid operations are then: 1. did a position happen before or after a token; 2. get all events that happened before or after a token; and 3. get all events between two tokens. (Note that we don't want to allow other operations as we want to change the tokens to be vector clocks rather than simple ints)
* Simplify super() calls to Python 3 syntax. (#8344)Patrick Cloke2020-09-181-3/+3
| | | | | | | This converts calls like super(Foo, self) -> super(). Generated with: sed -i "" -Ee 's/super\([^\(]+\)/super()/g' **/*.py
* Stop sub-classing object (#8249)Patrick Cloke2020-09-041-1/+1
|
* Fix typing for SyncHandler (#8237)Erik Johnston2020-09-031-3/+3
|
* Convert `event_push_actions`, `registration`, and `roommember` datastores to ↵Patrick Cloke2020-08-281-25/+27
| | | | async (#8197)
* Convert some of the general database methods to async (#8100)Patrick Cloke2020-08-171-2/+2
|
* Convert misc database code to async (#8087)Patrick Cloke2020-08-141-11/+6
|
* Convert the roommember database to async/await. (#8070)Patrick Cloke2020-08-121-165/+98
|
* Rename database classes to make some sense (#8033)Erik Johnston2020-08-051-0/+1139