From 7166854f4169999fee0cd40a5ed389cc684b6dc8 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 2 May 2017 10:36:35 +0100 Subject: Add cache for get_current_hosts_in_room --- synapse/storage/roommember.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'synapse/storage') diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py index 7ad2198d96..1c0fa8a680 100644 --- a/synapse/storage/roommember.py +++ b/synapse/storage/roommember.py @@ -482,6 +482,44 @@ class RoomMemberStore(SQLBaseStore): defer.returnValue(False) + def get_joined_hosts(self, room_id, state_group, state_ids): + if not state_group: + # If state_group is None it means it has yet to be assigned a + # state group, i.e. we need to make sure that calls with a state_group + # of None don't hit previous cached calls with a None state_group. + # To do this we set the state_group to a new object as object() != object() + state_group = object() + + return self._get_joined_hosts( + room_id, state_group, state_ids + ) + + @cachedInlineCallbacks(num_args=3) + def _get_joined_hosts(self, room_id, state_group, current_state_ids): + # We don't use `state_group`, its there so that we can cache based + # on it. However, its important that its never None, since two current_state's + # with a state_group of None are likely to be different. + # See bulk_get_push_rules_for_room for how we work around this. + assert state_group is not None + + joined_hosts = set() + for (etype, state_key), event_id in current_state_ids.items(): + if etype == EventTypes.Member: + try: + host = get_domain_from_id(state_key) + except: + logger.warn("state_key not user_id: %s", state_key) + continue + + if host in joined_hosts: + continue + + event = yield self.get_event(event_id, allow_none=True) + if event and event.content["membership"] == Membership.JOIN: + joined_hosts.add(host) + + defer.returnValue(joined_hosts) + @defer.inlineCallbacks def _background_add_membership_profile(self, progress, batch_size): target_min_stream_id = progress.get( -- cgit 1.4.1 From dfaa58f72d3455affcac58c8b604d21935183e88 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 4 May 2017 14:50:24 +0100 Subject: Fix comment and num args --- synapse/state.py | 2 +- synapse/storage/roommember.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'synapse/storage') diff --git a/synapse/state.py b/synapse/state.py index f8b18a4a2d..02fee47f39 100644 --- a/synapse/state.py +++ b/synapse/state.py @@ -179,7 +179,7 @@ class StateHandler(object): def get_current_hosts_in_room(self, room_id, latest_event_ids=None): if not latest_event_ids: latest_event_ids = yield self.store.get_latest_event_ids_in_room(room_id) - logger.debug("calling resolve_state_groups from get_current_user_in_room") + logger.debug("calling resolve_state_groups from get_current_hosts_in_room") entry = yield self.resolve_state_groups(room_id, latest_event_ids) joined_hosts = yield self.store.get_joined_hosts( room_id, entry.state_id, entry.state diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py index 1c0fa8a680..c571da2ce4 100644 --- a/synapse/storage/roommember.py +++ b/synapse/storage/roommember.py @@ -494,7 +494,7 @@ class RoomMemberStore(SQLBaseStore): room_id, state_group, state_ids ) - @cachedInlineCallbacks(num_args=3) + @cachedInlineCallbacks(num_args=2) def _get_joined_hosts(self, room_id, state_group, current_state_ids): # We don't use `state_group`, its there so that we can cache based # on it. However, its important that its never None, since two current_state's -- cgit 1.4.1 From 07a07588a01a644837ccea57f2307d0450cd28d9 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 4 May 2017 14:52:28 +0100 Subject: Make caches bigger --- synapse/storage/roommember.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'synapse/storage') diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py index c571da2ce4..1963b95724 100644 --- a/synapse/storage/roommember.py +++ b/synapse/storage/roommember.py @@ -494,7 +494,7 @@ class RoomMemberStore(SQLBaseStore): room_id, state_group, state_ids ) - @cachedInlineCallbacks(num_args=2) + @cachedInlineCallbacks(num_args=2, max_entries=10000, iterable=True) def _get_joined_hosts(self, room_id, state_group, current_state_ids): # We don't use `state_group`, its there so that we can cache based # on it. However, its important that its never None, since two current_state's -- cgit 1.4.1 From 537dbadea05dfcc7fa855d06ae0f81405ed4e81f Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 4 May 2017 14:55:28 +0100 Subject: Intern host strings --- synapse/storage/roommember.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'synapse/storage') diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py index 1963b95724..6ec8a6345d 100644 --- a/synapse/storage/roommember.py +++ b/synapse/storage/roommember.py @@ -18,6 +18,7 @@ from twisted.internet import defer from collections import namedtuple from ._base import SQLBaseStore +from synapse.util.caches import intern_string from synapse.util.caches.descriptors import cached, cachedInlineCallbacks from synapse.util.stringutils import to_ascii @@ -516,7 +517,7 @@ class RoomMemberStore(SQLBaseStore): event = yield self.get_event(event_id, allow_none=True) if event and event.content["membership"] == Membership.JOIN: - joined_hosts.add(host) + joined_hosts.add(intern_string(host)) defer.returnValue(joined_hosts) -- cgit 1.4.1