summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2016-03-22 15:31:13 +0000
committerErik Johnston <erik@matrix.org>2016-03-22 16:06:04 +0000
commit99f929f36b396b7152b3840c11e8debc5505f673 (patch)
treea9114efc5467d6cade40cdd55affc817042614f9
parentMeasure StateHandler._resolve_events (diff)
downloadsynapse-99f929f36b396b7152b3840c11e8debc5505f673.tar.xz
Make StateHandler._state_cache only store event_ids.
-rw-r--r--synapse/state.py24
-rw-r--r--synapse/storage/events.py25
2 files changed, 42 insertions, 7 deletions
diff --git a/synapse/state.py b/synapse/state.py
index e096329721..9d90a437d3 100644
--- a/synapse/state.py
+++ b/synapse/state.py
@@ -28,6 +28,7 @@ from collections import namedtuple
 
 import logging
 import hashlib
+import os
 
 logger = logging.getLogger(__name__)
 
@@ -35,8 +36,11 @@ logger = logging.getLogger(__name__)
 KeyStateTuple = namedtuple("KeyStateTuple", ("context", "type", "state_key"))
 
 
-SIZE_OF_CACHE = 1000
-EVICTION_TIMEOUT_SECONDS = 20
+CACHE_SIZE_FACTOR = float(os.environ.get("SYNAPSE_CACHE_FACTOR", 0.1))
+
+
+SIZE_OF_CACHE = int(5000 * CACHE_SIZE_FACTOR)
+EVICTION_TIMEOUT_SECONDS = 60 * 60
 
 
 class _StateCacheEntry(object):
@@ -92,7 +96,9 @@ class StateHandler(object):
 
         if cache:
             cache.ts = self.clock.time_msec()
-            state = cache.state
+
+            event_dict = yield self.store.get_events(cache.state.values())
+            state = {(e.type, e.state_key): e for e in event_dict.values()}
         else:
             res = yield self.resolve_state_groups(room_id, event_ids)
             state = res[1]
@@ -191,14 +197,18 @@ class StateHandler(object):
             cache = self._state_cache.get(frozenset(event_ids), None)
             if cache and cache.state_group:
                 cache.ts = self.clock.time_msec()
-                prev_state = cache.state.get((event_type, state_key), None)
+
+                event_dict = yield self.store.get_events(cache.state.values())
+                state = {(e.type, e.state_key): e for e in event_dict.values()}
+
+                prev_state = state.get((event_type, state_key), None)
                 if prev_state:
                     prev_state = prev_state.event_id
                     prev_states = [prev_state]
                 else:
                     prev_states = []
                 defer.returnValue(
-                    (cache.state_group, cache.state, prev_states)
+                    (cache.state_group, state, prev_states)
                 )
 
         state_groups = yield self.store.get_state_groups(
@@ -226,7 +236,7 @@ class StateHandler(object):
 
             if self._state_cache is not None:
                 cache = _StateCacheEntry(
-                    state=state,
+                    state={key: event.event_id for key, event in state.items()},
                     state_group=name,
                     ts=self.clock.time_msec()
                 )
@@ -241,7 +251,7 @@ class StateHandler(object):
 
         if self._state_cache is not None:
             cache = _StateCacheEntry(
-                state=new_state,
+                state={key: event.event_id for key, event in new_state.items()},
                 state_group=None,
                 ts=self.clock.time_msec()
             )
diff --git a/synapse/storage/events.py b/synapse/storage/events.py
index e444b64cee..584e659d4a 100644
--- a/synapse/storage/events.py
+++ b/synapse/storage/events.py
@@ -151,6 +151,31 @@ class EventsStore(SQLBaseStore):
 
         defer.returnValue(events[0] if events else None)
 
+    @defer.inlineCallbacks
+    def get_events(self, event_ids, check_redacted=True,
+                   get_prev_content=False, allow_rejected=False):
+        """Get events from the database
+
+        Args:
+            event_ids (list): The event_ids of the events to fetch
+            check_redacted (bool): If True, check if event has been redacted
+                and redact it.
+            get_prev_content (bool): If True and event is a state event,
+                include the previous states content in the unsigned field.
+            allow_rejected (bool): If True return rejected events.
+
+        Returns:
+            Deferred : Dict from event_id to event.
+        """
+        events = yield self._get_events(
+            event_ids,
+            check_redacted=check_redacted,
+            get_prev_content=get_prev_content,
+            allow_rejected=allow_rejected,
+        )
+
+        defer.returnValue({e.event_id: e for e in events})
+
     @log_function
     def _persist_event_txn(self, txn, event, context,
                            is_new_state=True, current_state=None):