From 83cf583adfee9a20dc81780c30f75530fe7ca237 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 1 May 2020 15:35:46 +0100 Subject: [PATCH] Allow creating an LruCache that's not affected by cache factor --- synapse/storage/data_stores/main/events_worker.py | 5 ++++- synapse/util/caches/descriptors.py | 11 ++++++++++- synapse/util/caches/expiringcache.py | 2 +- synapse/util/caches/lrucache.py | 12 +++++++++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/synapse/storage/data_stores/main/events_worker.py b/synapse/storage/data_stores/main/events_worker.py index 25511be5cb..e40a8999a9 100644 --- a/synapse/storage/data_stores/main/events_worker.py +++ b/synapse/storage/data_stores/main/events_worker.py @@ -75,7 +75,10 @@ class EventsWorkerStore(SQLBaseStore): super(EventsWorkerStore, self).__init__(database, db_conn, hs) self._get_event_cache = Cache( - "*getEvent*", keylen=3, max_entries=hs.config.caches.event_cache_size + "*getEvent*", + keylen=3, + max_entries=hs.config.caches.event_cache_size, + apply_cache_factor_from_config=False, ) self._event_fetch_lock = threading.Condition() diff --git a/synapse/util/caches/descriptors.py b/synapse/util/caches/descriptors.py index 2c8ea764b5..296706a396 100644 --- a/synapse/util/caches/descriptors.py +++ b/synapse/util/caches/descriptors.py @@ -88,7 +88,15 @@ class Cache(object): "_pending_deferred_cache", ) - def __init__(self, name, max_entries=1000, keylen=1, tree=False, iterable=False): + def __init__( + self, + name, + max_entries=1000, + keylen=1, + tree=False, + iterable=False, + apply_cache_factor_from_config=True, + ): cache_type = TreeCache if tree else dict self._pending_deferred_cache = cache_type() @@ -98,6 +106,7 @@ class Cache(object): cache_type=cache_type, size_callback=(lambda d: len(d)) if iterable else None, evicted_callback=self._on_evicted, + apply_cache_factor_from_config=apply_cache_factor_from_config, ) self.name = name diff --git a/synapse/util/caches/expiringcache.py b/synapse/util/caches/expiringcache.py index 4238a0fd33..2726b67b6d 100644 --- a/synapse/util/caches/expiringcache.py +++ b/synapse/util/caches/expiringcache.py @@ -52,11 +52,11 @@ class ExpiringCache(object): an item on access. Defaults to False. iterable (bool): If true, the size is calculated by summing the sizes of all entries, rather than the number of entries. - """ self._cache_name = cache_name self._original_max_size = max_len + self._max_size = int(max_len * cache_config.properties.default_factor_size) self._clock = clock diff --git a/synapse/util/caches/lrucache.py b/synapse/util/caches/lrucache.py index cfd2b33027..293a3f172a 100644 --- a/synapse/util/caches/lrucache.py +++ b/synapse/util/caches/lrucache.py @@ -57,6 +57,7 @@ class LruCache(object): cache_type=dict, size_callback=None, evicted_callback=None, + apply_cache_factor_from_config=True, ): """ Args: @@ -73,13 +74,22 @@ class LruCache(object): evicted_callback (func(int)|None): if not None, called on eviction with the size of the evicted entry + + apply_cache_factor_from_config (bool): If true, `max_size` will be + multiplied by a cache factor derived from the homeserver config. """ cache = cache_type() self.cache = cache # Used for introspection. # Save the original max size, and apply the default size factor. self._original_max_size = max_size - self.max_size = int(max_size * cache_config.properties.default_factor_size) + # We previously didn't apply the cache factor here, and as such some caches were + # not affected by the global cache factor. Add an option here to disable applying + # the cache factor when a cache is created + if apply_cache_factor_from_config: + self.max_size = int(max_size * cache_config.properties.default_factor_size) + else: + self.max_size = int(max_size) list_root = _Node(None, None, None, None) list_root.next_node = list_root