Increase DB/CPU perf of _is_server_still_joined check. (#6936)

* Increase DB/CPU perf of `_is_server_still_joined` check.

For rooms with large amount of state a single user leaving could cause
us to go and load a lot of membership events and then pull out
membership state in a large number of batches.

* Newsfile

* Update synapse/storage/persist_events.py

Co-Authored-By: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>

* Fix adding if too soon

* Update docstring

* Review comments

* Woops typo

Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
This commit is contained in:
Erik Johnston 2020-02-19 10:15:49 +00:00 committed by GitHub
parent 5e4a438556
commit 0d0bc35792
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 15 deletions

View file

@ -868,6 +868,37 @@ class RoomMemberWorkerStore(EventsWorkerStore):
desc="get_membership_from_event_ids",
)
async def is_local_host_in_room_ignoring_users(
self, room_id: str, ignore_users: Collection[str]
) -> bool:
"""Check if there are any local users, excluding those in the given
list, in the room.
"""
clause, args = make_in_list_sql_clause(
self.database_engine, "user_id", ignore_users
)
sql = """
SELECT 1 FROM local_current_membership
WHERE
room_id = ? AND membership = ?
AND NOT (%s)
LIMIT 1
""" % (
clause,
)
def _is_local_host_in_room_ignoring_users_txn(txn):
txn.execute(sql, (room_id, Membership.JOIN, *args))
return bool(txn.fetchone())
return await self.db.runInteraction(
"is_local_host_in_room_ignoring_users",
_is_local_host_in_room_ignoring_users_txn,
)
class RoomMemberBackgroundUpdateStore(SQLBaseStore):
def __init__(self, database: Database, db_conn, hs):