Compare commits
36 Commits
v1.99.0
...
anoa/temp_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ddaae83c3 | ||
|
|
f004ceed71 | ||
|
|
965e259426 | ||
|
|
0fc0a7deb8 | ||
|
|
2619891343 | ||
|
|
5f508e728a | ||
|
|
4aeb6fbe2c | ||
|
|
18c1dbfbac | ||
|
|
ac020dee7a | ||
|
|
2f4dbfa3e1 | ||
|
|
a96b5d9ca7 | ||
|
|
0b069b70a1 | ||
|
|
125c5a01dd | ||
|
|
a21702fe76 | ||
|
|
0a02b2a1c5 | ||
|
|
c76a412cc3 | ||
|
|
9735a08f04 | ||
|
|
946650c65a | ||
|
|
e174b2d19c | ||
|
|
f7ec52670b | ||
|
|
0e368eec58 | ||
|
|
c11c8ad39f | ||
|
|
d70be1871c | ||
|
|
92d4d1342a | ||
|
|
8439ce5e53 | ||
|
|
50fcb4a8c5 | ||
|
|
a14831d209 | ||
|
|
f888515a3c | ||
|
|
c580eb32ba | ||
|
|
e55591dc39 | ||
|
|
babf4eaece | ||
|
|
73cfdebfec | ||
|
|
f36f3ab990 | ||
|
|
135fdaae0d | ||
|
|
d684ec8a2b | ||
|
|
99ab65af2f |
1
changelog.d/6391.feature
Normal file
1
changelog.d/6391.feature
Normal file
@@ -0,0 +1 @@
|
||||
Synapse's cache factor can now be configured in `homeserver.yaml` by the `caches.global_factor` setting. Additionally, `caches.per_cache_factors` controls the cache factors for individual caches.
|
||||
1
changelog.d/6446.misc
Normal file
1
changelog.d/6446.misc
Normal file
@@ -0,0 +1 @@
|
||||
Add benchmarks for LruCache.
|
||||
@@ -38,7 +38,7 @@ from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
|
||||
from synapse.config.server import is_threepid_reserved
|
||||
from synapse.events import EventBase
|
||||
from synapse.types import StateMap, UserID
|
||||
from synapse.util.caches import CACHE_SIZE_FACTOR, register_cache
|
||||
from synapse.util.caches import register_cache
|
||||
from synapse.util.caches.lrucache import LruCache
|
||||
from synapse.util.metrics import Measure
|
||||
|
||||
@@ -74,7 +74,7 @@ class Auth(object):
|
||||
self.store = hs.get_datastore()
|
||||
self.state = hs.get_state_handler()
|
||||
|
||||
self.token_cache = LruCache(CACHE_SIZE_FACTOR * 10000)
|
||||
self.token_cache = LruCache(10000)
|
||||
register_cache("cache", "token_cache", self.token_cache)
|
||||
|
||||
self._account_validity = hs.config.account_validity
|
||||
|
||||
@@ -69,7 +69,6 @@ from synapse.server import HomeServer
|
||||
from synapse.storage import DataStore
|
||||
from synapse.storage.engines import IncorrectDatabaseSetup
|
||||
from synapse.storage.prepare_database import UpgradeDatabaseException
|
||||
from synapse.util.caches import CACHE_SIZE_FACTOR
|
||||
from synapse.util.httpresourcetree import create_resource_tree
|
||||
from synapse.util.manhole import manhole
|
||||
from synapse.util.module_loader import load_module
|
||||
@@ -488,8 +487,8 @@ def phone_stats_home(hs, stats, stats_process=_stats_process):
|
||||
|
||||
daily_sent_messages = yield hs.get_datastore().count_daily_sent_messages()
|
||||
stats["daily_sent_messages"] = daily_sent_messages
|
||||
stats["cache_factor"] = CACHE_SIZE_FACTOR
|
||||
stats["event_cache_size"] = hs.config.event_cache_size
|
||||
stats["cache_factor"] = hs.config.caches.global_factor
|
||||
stats["event_cache_size"] = hs.config.caches.event_cache_size
|
||||
|
||||
#
|
||||
# Performance statistics
|
||||
|
||||
112
synapse/config/cache.py
Normal file
112
synapse/config/cache.py
Normal file
@@ -0,0 +1,112 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2019 Matrix.org Foundation C.I.C.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import os
|
||||
from typing import Dict
|
||||
|
||||
from ._base import Config, ConfigError
|
||||
|
||||
_CACHES = {}
|
||||
_CACHE_PREFIX = "SYNAPSE_CACHE_FACTOR"
|
||||
DEFAULT_CACHE_SIZE_FACTOR = float(os.environ.get(_CACHE_PREFIX, 0.5))
|
||||
|
||||
_DEFAULT_CONFIG = """\
|
||||
# Cache configuration
|
||||
#
|
||||
# 'global_factor' controls the global cache factor. This overrides the
|
||||
# "SYNAPSE_CACHE_FACTOR" environment variable.
|
||||
#
|
||||
# 'per_cache_factors' is a dictionary of cache name to cache factor for that
|
||||
# individual cache.
|
||||
#
|
||||
#caches:
|
||||
# global_factor: 0.5
|
||||
# per_cache_factors:
|
||||
# get_users_who_share_room_with_user: 2
|
||||
#
|
||||
"""
|
||||
|
||||
# Callback to ensure that all caches are the correct size, registered when the
|
||||
# configuration has been loaded.
|
||||
_ENSURE_CORRECT_CACHE_SIZING = None
|
||||
|
||||
|
||||
def add_resizable_cache(cache_name, cache_resize_callback):
|
||||
_CACHES[cache_name.lower()] = cache_resize_callback
|
||||
if _ENSURE_CORRECT_CACHE_SIZING:
|
||||
_ENSURE_CORRECT_CACHE_SIZING()
|
||||
|
||||
|
||||
class CacheConfig(Config):
|
||||
section = "caches"
|
||||
_environ = os.environ
|
||||
|
||||
@staticmethod
|
||||
def _reset():
|
||||
global DEFAULT_CACHE_SIZE_FACTOR
|
||||
global _ENSURE_CORRECT_CACHE_SIZING
|
||||
|
||||
DEFAULT_CACHE_SIZE_FACTOR = float(os.environ.get(_CACHE_PREFIX, 0.5))
|
||||
_ENSURE_CORRECT_CACHE_SIZING = None
|
||||
_CACHES.clear()
|
||||
|
||||
def read_config(self, config, **kwargs):
|
||||
self.event_cache_size = self.parse_size(config.get("event_cache_size", "10K"))
|
||||
|
||||
global DEFAULT_CACHE_SIZE_FACTOR
|
||||
global _ENSURE_CORRECT_CACHE_SIZING
|
||||
|
||||
cache_config = config.get("caches", {})
|
||||
|
||||
self.global_factor = cache_config.get(
|
||||
"global_factor", DEFAULT_CACHE_SIZE_FACTOR
|
||||
)
|
||||
if not isinstance(self.global_factor, (int, float)):
|
||||
raise ConfigError("caches.global_factor must be a number.")
|
||||
|
||||
# Set the global one so that it's reflected in new caches
|
||||
DEFAULT_CACHE_SIZE_FACTOR = self.global_factor
|
||||
|
||||
# Load cache factors from the environment, but override them with the
|
||||
# ones in the config file if they exist
|
||||
individual_factors = {
|
||||
key[len(_CACHE_PREFIX) + 1 :].lower(): float(val)
|
||||
for key, val in self._environ.items()
|
||||
if key.startswith(_CACHE_PREFIX + "_")
|
||||
}
|
||||
|
||||
individual_factors_config = cache_config.get("per_cache_factors", {}) or {}
|
||||
if not isinstance(individual_factors_config, dict):
|
||||
raise ConfigError("caches.per_cache_factors must be a dictionary")
|
||||
|
||||
individual_factors.update(individual_factors_config)
|
||||
|
||||
self.cache_factors = dict() # type: Dict[str, float]
|
||||
|
||||
for cache, factor in individual_factors.items():
|
||||
if not isinstance(factor, (int, float)):
|
||||
raise ConfigError(
|
||||
"caches.per_cache_factors.%s must be a number" % (cache.lower(),)
|
||||
)
|
||||
self.cache_factors[cache.lower()] = factor
|
||||
|
||||
# Register the global callback so that the individual cache sizes get set.
|
||||
def ensure_cache_sizes():
|
||||
for cache_name, callback in _CACHES.items():
|
||||
new_factor = self.cache_factors.get(cache_name, self.global_factor)
|
||||
callback(new_factor)
|
||||
|
||||
_ENSURE_CORRECT_CACHE_SIZING = ensure_cache_sizes
|
||||
_ENSURE_CORRECT_CACHE_SIZING()
|
||||
@@ -57,8 +57,6 @@ class DatabaseConfig(Config):
|
||||
section = "database"
|
||||
|
||||
def read_config(self, config, **kwargs):
|
||||
self.event_cache_size = self.parse_size(config.get("event_cache_size", "10K"))
|
||||
|
||||
# We *experimentally* support specifying multiple databases via the
|
||||
# `databases` key. This is a map from a label to database config in the
|
||||
# same format as the `database` config option, plus an extra
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
from ._base import RootConfig
|
||||
from .api import ApiConfig
|
||||
from .appservice import AppServiceConfig
|
||||
from .cache import CacheConfig
|
||||
from .captcha import CaptchaConfig
|
||||
from .cas import CasConfig
|
||||
from .consent_config import ConsentConfig
|
||||
@@ -82,4 +83,5 @@ class HomeServerConfig(RootConfig):
|
||||
RoomDirectoryConfig,
|
||||
ThirdPartyRulesConfig,
|
||||
TracerConfig,
|
||||
CacheConfig,
|
||||
]
|
||||
|
||||
@@ -49,7 +49,6 @@ from synapse.http.proxyagent import ProxyAgent
|
||||
from synapse.logging.context import make_deferred_yieldable
|
||||
from synapse.logging.opentracing import set_tag, start_active_span, tags
|
||||
from synapse.util.async_helpers import timeout_deferred
|
||||
from synapse.util.caches import CACHE_SIZE_FACTOR
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -241,7 +240,8 @@ class SimpleHttpClient(object):
|
||||
# tends to do so in batches, so we need to allow the pool to keep
|
||||
# lots of idle connections around.
|
||||
pool = HTTPConnectionPool(self.reactor)
|
||||
pool.maxPersistentPerHost = max((100 * CACHE_SIZE_FACTOR, 5))
|
||||
# XXX: Why does this use the cache factor????
|
||||
pool.maxPersistentPerHost = max((100 * hs.config.caches.global_factor, 5))
|
||||
pool.cachedConnectionTimeout = 2 * 60
|
||||
|
||||
self.agent = ProxyAgent(
|
||||
|
||||
@@ -33,6 +33,8 @@ from prometheus_client import REGISTRY
|
||||
|
||||
from twisted.web.resource import Resource
|
||||
|
||||
from synapse.util import caches
|
||||
|
||||
try:
|
||||
from prometheus_client.samples import Sample
|
||||
except ImportError:
|
||||
@@ -103,13 +105,15 @@ def nameify_sample(sample):
|
||||
|
||||
|
||||
def generate_latest(registry, emit_help=False):
|
||||
|
||||
# Trigger the cache metrics to be rescraped, which updates the common
|
||||
# metrics but do not produce metrics themselves
|
||||
for collector in caches.collectors_by_name.values():
|
||||
collector.collect()
|
||||
|
||||
output = []
|
||||
|
||||
for metric in registry.collect():
|
||||
|
||||
if metric.name.startswith("__unused"):
|
||||
continue
|
||||
|
||||
if not metric.samples:
|
||||
# No samples, don't bother.
|
||||
continue
|
||||
|
||||
@@ -51,6 +51,7 @@ push_rules_delta_state_cache_metric = register_cache(
|
||||
"cache",
|
||||
"push_rules_delta_state_cache_metric",
|
||||
cache=[], # Meaningless size, as this isn't a cache that stores values
|
||||
resizable=False,
|
||||
)
|
||||
|
||||
|
||||
@@ -67,7 +68,8 @@ class BulkPushRuleEvaluator(object):
|
||||
self.room_push_rule_cache_metrics = register_cache(
|
||||
"cache",
|
||||
"room_push_rule_cache",
|
||||
cache=[], # Meaningless size, as this isn't a cache that stores values
|
||||
cache=[], # Meaningless size, as this isn't a cache that stores values,
|
||||
resizable=False,
|
||||
)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
|
||||
@@ -22,7 +22,7 @@ from six import string_types
|
||||
|
||||
from synapse.events import EventBase
|
||||
from synapse.types import UserID
|
||||
from synapse.util.caches import CACHE_SIZE_FACTOR, register_cache
|
||||
from synapse.util.caches import register_cache
|
||||
from synapse.util.caches.lrucache import LruCache
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -165,7 +165,7 @@ class PushRuleEvaluatorForEvent(object):
|
||||
|
||||
|
||||
# Caches (string, is_glob, word_boundary) -> regex for push. See _glob_matches
|
||||
regex_cache = LruCache(50000 * CACHE_SIZE_FACTOR)
|
||||
regex_cache = LruCache(50000)
|
||||
register_cache("cache", "regex_push_cache", regex_cache)
|
||||
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
from synapse.storage.data_stores.main.client_ips import LAST_SEEN_GRANULARITY
|
||||
from synapse.storage.database import Database
|
||||
from synapse.util.caches import CACHE_SIZE_FACTOR
|
||||
from synapse.util.caches.descriptors import Cache
|
||||
|
||||
from ._base import BaseSlavedStore
|
||||
@@ -26,7 +25,7 @@ class SlavedClientIpStore(BaseSlavedStore):
|
||||
super(SlavedClientIpStore, self).__init__(database, db_conn, hs)
|
||||
|
||||
self.client_ip_last_seen = Cache(
|
||||
name="client_ip_last_seen", keylen=4, max_entries=50000 * CACHE_SIZE_FACTOR
|
||||
name="client_ip_last_seen", keylen=4, max_entries=50000
|
||||
)
|
||||
|
||||
def insert_client_ip(self, user_id, access_token, ip, user_agent, device_id):
|
||||
|
||||
@@ -35,7 +35,6 @@ from synapse.state import v1, v2
|
||||
from synapse.storage.data_stores.main.events_worker import EventRedactBehaviour
|
||||
from synapse.types import StateMap
|
||||
from synapse.util.async_helpers import Linearizer
|
||||
from synapse.util.caches import get_cache_factor_for
|
||||
from synapse.util.caches.expiringcache import ExpiringCache
|
||||
from synapse.util.metrics import Measure, measure_func
|
||||
|
||||
@@ -53,7 +52,6 @@ state_groups_histogram = Histogram(
|
||||
KeyStateTuple = namedtuple("KeyStateTuple", ("context", "type", "state_key"))
|
||||
|
||||
|
||||
SIZE_OF_CACHE = 100000 * get_cache_factor_for("state_cache")
|
||||
EVICTION_TIMEOUT_SECONDS = 60 * 60
|
||||
|
||||
|
||||
@@ -447,7 +445,7 @@ class StateResolutionHandler(object):
|
||||
self._state_cache = ExpiringCache(
|
||||
cache_name="state_cache",
|
||||
clock=self.clock,
|
||||
max_len=SIZE_OF_CACHE,
|
||||
max_len=100000,
|
||||
expiry_ms=EVICTION_TIMEOUT_SECONDS * 1000,
|
||||
iterable=True,
|
||||
reset_expiry_on_get=True,
|
||||
|
||||
@@ -22,7 +22,6 @@ from twisted.internet import defer
|
||||
from synapse.metrics.background_process_metrics import wrap_as_background_process
|
||||
from synapse.storage._base import SQLBaseStore
|
||||
from synapse.storage.database import Database
|
||||
from synapse.util.caches import CACHE_SIZE_FACTOR
|
||||
from synapse.util.caches.descriptors import Cache
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -367,7 +366,7 @@ class ClientIpStore(ClientIpBackgroundUpdateStore):
|
||||
def __init__(self, database: Database, db_conn, hs):
|
||||
|
||||
self.client_ip_last_seen = Cache(
|
||||
name="client_ip_last_seen", keylen=4, max_entries=50000 * CACHE_SIZE_FACTOR
|
||||
name="client_ip_last_seen", keylen=4, max_entries=50000
|
||||
)
|
||||
|
||||
super(ClientIpStore, self).__init__(database, db_conn, hs)
|
||||
|
||||
@@ -75,7 +75,7 @@ class EventsWorkerStore(SQLBaseStore):
|
||||
super(EventsWorkerStore, self).__init__(database, db_conn, hs)
|
||||
|
||||
self._get_event_cache = Cache(
|
||||
"*getEvent*", keylen=3, max_entries=hs.config.event_cache_size
|
||||
"*getEvent*", keylen=3, max_entries=hs.config.caches.event_cache_size
|
||||
)
|
||||
|
||||
self._event_fetch_lock = threading.Condition()
|
||||
|
||||
@@ -28,7 +28,6 @@ from synapse.storage.data_stores.state.bg_updates import StateBackgroundUpdateSt
|
||||
from synapse.storage.database import Database
|
||||
from synapse.storage.state import StateFilter
|
||||
from synapse.types import StateMap
|
||||
from synapse.util.caches import get_cache_factor_for
|
||||
from synapse.util.caches.descriptors import cached
|
||||
from synapse.util.caches.dictionary_cache import DictionaryCache
|
||||
|
||||
@@ -90,11 +89,10 @@ class StateGroupDataStore(StateBackgroundUpdateStore, SQLBaseStore):
|
||||
self._state_group_cache = DictionaryCache(
|
||||
"*stateGroupCache*",
|
||||
# TODO: this hasn't been tuned yet
|
||||
50000 * get_cache_factor_for("stateGroupCache"),
|
||||
50000,
|
||||
)
|
||||
self._state_group_members_cache = DictionaryCache(
|
||||
"*stateGroupMembersCache*",
|
||||
500000 * get_cache_factor_for("stateGroupMembersCache"),
|
||||
"*stateGroupMembersCache*", 500000,
|
||||
)
|
||||
|
||||
@cached(max_entries=10000, iterable=True)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2015, 2016 OpenMarket Ltd
|
||||
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||
# Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
@@ -15,28 +15,18 @@
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Dict
|
||||
from typing import Callable, Dict, Optional
|
||||
|
||||
import six
|
||||
from six.moves import intern
|
||||
|
||||
from prometheus_client.core import REGISTRY, Gauge, GaugeMetricFamily
|
||||
import attr
|
||||
from prometheus_client.core import Gauge
|
||||
|
||||
from synapse.config.cache import add_resizable_cache
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CACHE_SIZE_FACTOR = float(os.environ.get("SYNAPSE_CACHE_FACTOR", 0.5))
|
||||
|
||||
|
||||
def get_cache_factor_for(cache_name):
|
||||
env_var = "SYNAPSE_CACHE_FACTOR_" + cache_name.upper()
|
||||
factor = os.environ.get(env_var)
|
||||
if factor:
|
||||
return float(factor)
|
||||
|
||||
return CACHE_SIZE_FACTOR
|
||||
|
||||
|
||||
caches_by_name = {}
|
||||
collectors_by_name = {} # type: Dict
|
||||
|
||||
@@ -44,6 +34,7 @@ cache_size = Gauge("synapse_util_caches_cache:size", "", ["name"])
|
||||
cache_hits = Gauge("synapse_util_caches_cache:hits", "", ["name"])
|
||||
cache_evicted = Gauge("synapse_util_caches_cache:evicted_size", "", ["name"])
|
||||
cache_total = Gauge("synapse_util_caches_cache:total", "", ["name"])
|
||||
cache_max_size = Gauge("synapse_util_caches_cache_max_size", "", ["name"])
|
||||
|
||||
response_cache_size = Gauge("synapse_util_caches_response_cache:size", "", ["name"])
|
||||
response_cache_hits = Gauge("synapse_util_caches_response_cache:hits", "", ["name"])
|
||||
@@ -53,67 +44,82 @@ response_cache_evicted = Gauge(
|
||||
response_cache_total = Gauge("synapse_util_caches_response_cache:total", "", ["name"])
|
||||
|
||||
|
||||
def register_cache(cache_type, cache_name, cache, collect_callback=None):
|
||||
"""Register a cache object for metric collection.
|
||||
@attr.s
|
||||
class CacheMetric(object):
|
||||
|
||||
_cache = attr.ib()
|
||||
_cache_type = attr.ib(type=str)
|
||||
_cache_name = attr.ib(type=str)
|
||||
_collect_callback = attr.ib(type=Optional[Callable])
|
||||
|
||||
hits = attr.ib(default=0)
|
||||
misses = attr.ib(default=0)
|
||||
evicted_size = attr.ib(default=0)
|
||||
|
||||
def inc_hits(self):
|
||||
self.hits += 1
|
||||
|
||||
def inc_misses(self):
|
||||
self.misses += 1
|
||||
|
||||
def inc_evictions(self, size=1):
|
||||
self.evicted_size += size
|
||||
|
||||
def describe(self):
|
||||
return []
|
||||
|
||||
def collect(self):
|
||||
try:
|
||||
if self._cache_type == "response_cache":
|
||||
response_cache_size.labels(self._cache_name).set(len(self._cache))
|
||||
response_cache_hits.labels(self._cache_name).set(self.hits)
|
||||
response_cache_evicted.labels(self._cache_name).set(self.evicted_size)
|
||||
response_cache_total.labels(self._cache_name).set(
|
||||
self.hits + self.misses
|
||||
)
|
||||
else:
|
||||
cache_size.labels(self._cache_name).set(len(self._cache))
|
||||
cache_hits.labels(self._cache_name).set(self.hits)
|
||||
cache_evicted.labels(self._cache_name).set(self.evicted_size)
|
||||
cache_total.labels(self._cache_name).set(self.hits + self.misses)
|
||||
if getattr(self._cache, "max_size", None):
|
||||
cache_max_size.labels(self._cache_name).set(self._cache.max_size)
|
||||
if self._collect_callback:
|
||||
self._collect_callback()
|
||||
except Exception as e:
|
||||
logger.warning("Error calculating metrics for %s: %s", self._cache_name, e)
|
||||
raise
|
||||
|
||||
|
||||
def register_cache(
|
||||
cache_type: str,
|
||||
cache_name: str,
|
||||
cache,
|
||||
collect_callback: Optional[Callable] = None,
|
||||
resizable: bool = True,
|
||||
resize_callback: Optional[Callable] = None,
|
||||
) -> CacheMetric:
|
||||
"""Register a cache object for metric collection and resizing.
|
||||
|
||||
Args:
|
||||
cache_type (str):
|
||||
cache_name (str): name of the cache
|
||||
cache (object): cache itself
|
||||
collect_callback (callable|None): if not None, a function which is called during
|
||||
metric collection to update additional metrics.
|
||||
cache_type
|
||||
cache_name: name of the cache
|
||||
cache: cache itself
|
||||
collect_callback: If given, a function which is called during metric
|
||||
collection to update additional metrics.
|
||||
resizable: Whether this cache supports being resized.
|
||||
resize_callback: A function which can be called to resize the cache.
|
||||
|
||||
Returns:
|
||||
CacheMetric: an object which provides inc_{hits,misses,evictions} methods
|
||||
"""
|
||||
if resizable:
|
||||
if not resize_callback:
|
||||
resize_callback = getattr(cache, "set_cache_factor")
|
||||
add_resizable_cache(cache_name, resize_callback)
|
||||
|
||||
# Check if the metric is already registered. Unregister it, if so.
|
||||
# This usually happens during tests, as at runtime these caches are
|
||||
# effectively singletons.
|
||||
metric = CacheMetric(cache, cache_type, cache_name, collect_callback)
|
||||
metric_name = "cache_%s_%s" % (cache_type, cache_name)
|
||||
if metric_name in collectors_by_name.keys():
|
||||
REGISTRY.unregister(collectors_by_name[metric_name])
|
||||
|
||||
class CacheMetric(object):
|
||||
|
||||
hits = 0
|
||||
misses = 0
|
||||
evicted_size = 0
|
||||
|
||||
def inc_hits(self):
|
||||
self.hits += 1
|
||||
|
||||
def inc_misses(self):
|
||||
self.misses += 1
|
||||
|
||||
def inc_evictions(self, size=1):
|
||||
self.evicted_size += size
|
||||
|
||||
def describe(self):
|
||||
return []
|
||||
|
||||
def collect(self):
|
||||
try:
|
||||
if cache_type == "response_cache":
|
||||
response_cache_size.labels(cache_name).set(len(cache))
|
||||
response_cache_hits.labels(cache_name).set(self.hits)
|
||||
response_cache_evicted.labels(cache_name).set(self.evicted_size)
|
||||
response_cache_total.labels(cache_name).set(self.hits + self.misses)
|
||||
else:
|
||||
cache_size.labels(cache_name).set(len(cache))
|
||||
cache_hits.labels(cache_name).set(self.hits)
|
||||
cache_evicted.labels(cache_name).set(self.evicted_size)
|
||||
cache_total.labels(cache_name).set(self.hits + self.misses)
|
||||
if collect_callback:
|
||||
collect_callback()
|
||||
except Exception as e:
|
||||
logger.warning("Error calculating metrics for %s: %s", cache_name, e)
|
||||
raise
|
||||
|
||||
yield GaugeMetricFamily("__unused", "")
|
||||
|
||||
metric = CacheMetric()
|
||||
REGISTRY.register(metric)
|
||||
caches_by_name[cache_name] = cache
|
||||
collectors_by_name[metric_name] = metric
|
||||
return metric
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import functools
|
||||
import inspect
|
||||
import logging
|
||||
@@ -30,7 +31,6 @@ from twisted.internet import defer
|
||||
from synapse.logging.context import make_deferred_yieldable, preserve_fn
|
||||
from synapse.util import unwrapFirstError
|
||||
from synapse.util.async_helpers import ObservableDeferred
|
||||
from synapse.util.caches import get_cache_factor_for
|
||||
from synapse.util.caches.lrucache import LruCache
|
||||
from synapse.util.caches.treecache import TreeCache, iterate_tree_cache_entry
|
||||
|
||||
@@ -81,7 +81,6 @@ class CacheEntry(object):
|
||||
class Cache(object):
|
||||
__slots__ = (
|
||||
"cache",
|
||||
"max_entries",
|
||||
"name",
|
||||
"keylen",
|
||||
"thread",
|
||||
@@ -111,6 +110,10 @@ class Cache(object):
|
||||
collect_callback=self._metrics_collection_callback,
|
||||
)
|
||||
|
||||
@property
|
||||
def max_entries(self):
|
||||
return self.cache.max_size
|
||||
|
||||
def _on_evicted(self, evicted_count):
|
||||
self.metrics.inc_evictions(evicted_count)
|
||||
|
||||
@@ -370,13 +373,11 @@ class CacheDescriptor(_CacheDescriptorBase):
|
||||
cache_context=cache_context,
|
||||
)
|
||||
|
||||
max_entries = int(max_entries * get_cache_factor_for(orig.__name__))
|
||||
|
||||
self.max_entries = max_entries
|
||||
self.tree = tree
|
||||
self.iterable = iterable
|
||||
|
||||
def __get__(self, obj, objtype=None):
|
||||
def __get__(self, obj, owner):
|
||||
cache = Cache(
|
||||
name=self.orig.__name__,
|
||||
max_entries=self.max_entries,
|
||||
|
||||
@@ -18,6 +18,7 @@ from collections import OrderedDict
|
||||
|
||||
from six import iteritems, itervalues
|
||||
|
||||
from synapse.config import cache as cache_config
|
||||
from synapse.metrics.background_process_metrics import run_as_background_process
|
||||
from synapse.util.caches import register_cache
|
||||
|
||||
@@ -55,11 +56,12 @@ class ExpiringCache(object):
|
||||
"""
|
||||
self._cache_name = cache_name
|
||||
|
||||
self.max_size = int(max_len * cache_config.DEFAULT_CACHE_SIZE_FACTOR)
|
||||
self._original_max_size = max_len
|
||||
|
||||
self._clock = clock
|
||||
|
||||
self._max_len = max_len
|
||||
self._expiry_ms = expiry_ms
|
||||
|
||||
self._reset_expiry_on_get = reset_expiry_on_get
|
||||
|
||||
self._cache = OrderedDict()
|
||||
@@ -82,9 +84,11 @@ class ExpiringCache(object):
|
||||
def __setitem__(self, key, value):
|
||||
now = self._clock.time_msec()
|
||||
self._cache[key] = _CacheEntry(now, value)
|
||||
self.evict()
|
||||
|
||||
def evict(self):
|
||||
# Evict if there are now too many items
|
||||
while self._max_len and len(self) > self._max_len:
|
||||
while self.max_size and len(self) > self.max_size:
|
||||
_key, value = self._cache.popitem(last=False)
|
||||
if self.iterable:
|
||||
self.metrics.inc_evictions(len(value.value))
|
||||
@@ -170,6 +174,23 @@ class ExpiringCache(object):
|
||||
else:
|
||||
return len(self._cache)
|
||||
|
||||
def set_cache_factor(self, factor: float) -> bool:
|
||||
"""
|
||||
Set the cache factor for this individual cache.
|
||||
|
||||
This will trigger a resize if it changes, which may require evicting
|
||||
items from the cache.
|
||||
|
||||
Returns:
|
||||
bool: Whether the cache changed size or not.
|
||||
"""
|
||||
new_size = int(self._original_max_size * factor)
|
||||
if new_size != self.max_size:
|
||||
self.max_size = new_size
|
||||
self.evict()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class _CacheEntry(object):
|
||||
__slots__ = ["time", "value"]
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
import threading
|
||||
from functools import wraps
|
||||
|
||||
@@ -76,6 +75,11 @@ class LruCache(object):
|
||||
"""
|
||||
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)
|
||||
|
||||
list_root = _Node(None, None, None, None)
|
||||
list_root.next_node = list_root
|
||||
list_root.prev_node = list_root
|
||||
@@ -83,7 +87,7 @@ class LruCache(object):
|
||||
lock = threading.Lock()
|
||||
|
||||
def evict():
|
||||
while cache_len() > max_size:
|
||||
while cache_len() > self.max_size:
|
||||
todelete = list_root.prev_node
|
||||
evicted_len = delete_node(todelete)
|
||||
cache.pop(todelete.key, None)
|
||||
@@ -236,6 +240,7 @@ class LruCache(object):
|
||||
return key in cache
|
||||
|
||||
self.sentinel = object()
|
||||
self._on_resize = evict
|
||||
self.get = cache_get
|
||||
self.set = cache_set
|
||||
self.setdefault = cache_set_default
|
||||
@@ -266,3 +271,20 @@ class LruCache(object):
|
||||
|
||||
def __contains__(self, key):
|
||||
return self.contains(key)
|
||||
|
||||
def set_cache_factor(self, factor: float) -> bool:
|
||||
"""
|
||||
Set the cache factor for this individual cache.
|
||||
|
||||
This will trigger a resize if it changes, which may require evicting
|
||||
items from the cache.
|
||||
|
||||
Returns:
|
||||
bool: Whether the cache changed size or not.
|
||||
"""
|
||||
new_size = int(self._original_max_size * factor)
|
||||
if new_size != self.max_size:
|
||||
self.max_size = new_size
|
||||
self._on_resize()
|
||||
return True
|
||||
return False
|
||||
|
||||
@@ -38,7 +38,7 @@ class ResponseCache(object):
|
||||
self.timeout_sec = timeout_ms / 1000.0
|
||||
|
||||
self._name = name
|
||||
self._metrics = register_cache("response_cache", name, self)
|
||||
self._metrics = register_cache("response_cache", name, self, resizable=False)
|
||||
|
||||
def size(self):
|
||||
return len(self.pending_result_cache)
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import math
|
||||
|
||||
from six import integer_types
|
||||
|
||||
@@ -35,17 +36,37 @@ class StreamChangeCache(object):
|
||||
"""
|
||||
|
||||
def __init__(self, name, current_stream_pos, max_size=10000, prefilled_cache=None):
|
||||
self._max_size = int(max_size * caches.CACHE_SIZE_FACTOR)
|
||||
self._original_max_size = max_size
|
||||
self.max_size = math.floor(max_size)
|
||||
self._entity_to_key = {}
|
||||
self._cache = SortedDict()
|
||||
self._earliest_known_stream_pos = current_stream_pos
|
||||
self.name = name
|
||||
self.metrics = caches.register_cache("cache", self.name, self._cache)
|
||||
self.metrics = caches.register_cache(
|
||||
"cache", self.name, self._cache, resize_callback=self.set_cache_factor
|
||||
)
|
||||
|
||||
if prefilled_cache:
|
||||
for entity, stream_pos in prefilled_cache.items():
|
||||
self.entity_has_changed(entity, stream_pos)
|
||||
|
||||
def set_cache_factor(self, factor: float) -> bool:
|
||||
"""
|
||||
Set the cache factor for this individual cache.
|
||||
|
||||
This will trigger a resize if it changes, which may require evicting
|
||||
items from the cache.
|
||||
|
||||
Returns:
|
||||
bool: Whether the cache changed size or not.
|
||||
"""
|
||||
new_size = math.floor(self._original_max_size * factor)
|
||||
if new_size != self.max_size:
|
||||
self.max_size = new_size
|
||||
self._evict()
|
||||
return True
|
||||
return False
|
||||
|
||||
def has_entity_changed(self, entity, stream_pos):
|
||||
"""Returns True if the entity may have been updated since stream_pos
|
||||
"""
|
||||
@@ -133,13 +154,13 @@ class StreamChangeCache(object):
|
||||
self._cache.pop(old_pos, None)
|
||||
self._cache[stream_pos] = entity
|
||||
self._entity_to_key[entity] = stream_pos
|
||||
self._evict()
|
||||
|
||||
while len(self._cache) > self._max_size:
|
||||
k, r = self._cache.popitem(0)
|
||||
self._earliest_known_stream_pos = max(
|
||||
k, self._earliest_known_stream_pos
|
||||
)
|
||||
self._entity_to_key.pop(r, None)
|
||||
def _evict(self):
|
||||
while len(self._cache) > self.max_size:
|
||||
k, r = self._cache.popitem(0)
|
||||
self._earliest_known_stream_pos = max(k, self._earliest_known_stream_pos)
|
||||
self._entity_to_key.pop(r, None)
|
||||
|
||||
def get_max_pos_of_last_change(self, entity):
|
||||
"""Returns an upper bound of the stream id of the last change to an
|
||||
|
||||
@@ -38,7 +38,7 @@ class TTLCache(object):
|
||||
|
||||
self._timer = timer
|
||||
|
||||
self._metrics = register_cache("ttl", cache_name, self)
|
||||
self._metrics = register_cache("ttl", cache_name, self, resizable=False)
|
||||
|
||||
def set(self, key, value, ttl):
|
||||
"""Add/update an entry in the cache
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
import sys
|
||||
from argparse import REMAINDER
|
||||
from contextlib import redirect_stderr
|
||||
from io import StringIO
|
||||
|
||||
@@ -21,7 +22,7 @@ import pyperf
|
||||
from synmark import make_reactor
|
||||
from synmark.suites import SUITES
|
||||
|
||||
from twisted.internet.defer import ensureDeferred
|
||||
from twisted.internet.defer import Deferred, ensureDeferred
|
||||
from twisted.logger import globalLogBeginner, textFileLogObserver
|
||||
from twisted.python.failure import Failure
|
||||
|
||||
@@ -40,7 +41,8 @@ def make_test(main):
|
||||
file_out = StringIO()
|
||||
with redirect_stderr(file_out):
|
||||
|
||||
d = ensureDeferred(main(reactor, loops))
|
||||
d = Deferred()
|
||||
d.addCallback(lambda _: ensureDeferred(main(reactor, loops)))
|
||||
|
||||
def on_done(_):
|
||||
if isinstance(_, Failure):
|
||||
@@ -50,6 +52,7 @@ def make_test(main):
|
||||
return _
|
||||
|
||||
d.addBoth(on_done)
|
||||
reactor.callWhenRunning(lambda: d.callback(True))
|
||||
reactor.run()
|
||||
|
||||
return d.result
|
||||
@@ -62,11 +65,13 @@ if __name__ == "__main__":
|
||||
def add_cmdline_args(cmd, args):
|
||||
if args.log:
|
||||
cmd.extend(["--log"])
|
||||
cmd.extend(args.tests)
|
||||
|
||||
runner = pyperf.Runner(
|
||||
processes=3, min_time=2, show_name=True, add_cmdline_args=add_cmdline_args
|
||||
processes=3, min_time=1.5, show_name=True, add_cmdline_args=add_cmdline_args
|
||||
)
|
||||
runner.argparser.add_argument("--log", action="store_true")
|
||||
runner.argparser.add_argument("tests", nargs=REMAINDER)
|
||||
runner.parse_args()
|
||||
|
||||
orig_loops = runner.args.loops
|
||||
@@ -79,6 +84,11 @@ if __name__ == "__main__":
|
||||
)
|
||||
setupdb()
|
||||
|
||||
if runner.args.tests:
|
||||
SUITES = list(
|
||||
filter(lambda x: x[0].__name__.split(".")[-1] in runner.args.tests, SUITES)
|
||||
)
|
||||
|
||||
for suite, loops in SUITES:
|
||||
if loops:
|
||||
runner.args.loops = loops
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
from . import logging
|
||||
from . import logging, lrucache, lrucache_evict
|
||||
|
||||
SUITES = [(logging, 1000), (logging, 10000), (logging, None)]
|
||||
SUITES = [
|
||||
(logging, 1000),
|
||||
(logging, 10000),
|
||||
(logging, None),
|
||||
(lrucache, None),
|
||||
(lrucache_evict, None),
|
||||
]
|
||||
|
||||
34
synmark/suites/lrucache.py
Normal file
34
synmark/suites/lrucache.py
Normal file
@@ -0,0 +1,34 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from pyperf import perf_counter
|
||||
|
||||
from synapse.util.caches.lrucache import LruCache
|
||||
|
||||
|
||||
async def main(reactor, loops):
|
||||
"""
|
||||
Benchmark `loops` number of insertions into LruCache without eviction.
|
||||
"""
|
||||
cache = LruCache(loops)
|
||||
|
||||
start = perf_counter()
|
||||
|
||||
for i in range(loops):
|
||||
cache[i] = True
|
||||
|
||||
end = perf_counter() - start
|
||||
|
||||
return end
|
||||
35
synmark/suites/lrucache_evict.py
Normal file
35
synmark/suites/lrucache_evict.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from pyperf import perf_counter
|
||||
|
||||
from synapse.util.caches.lrucache import LruCache
|
||||
|
||||
|
||||
async def main(reactor, loops):
|
||||
"""
|
||||
Benchmark `loops` number of insertions into LruCache where half of them are
|
||||
evicted.
|
||||
"""
|
||||
cache = LruCache(loops // 2)
|
||||
|
||||
start = perf_counter()
|
||||
|
||||
for i in range(loops):
|
||||
cache[i] = True
|
||||
|
||||
end = perf_counter() - start
|
||||
|
||||
return end
|
||||
125
tests/config/test_cache.py
Normal file
125
tests/config/test_cache.py
Normal file
@@ -0,0 +1,125 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2020 Matrix.org Foundation C.I.C.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from synapse.config._base import Config, RootConfig
|
||||
from synapse.config.cache import CacheConfig, add_resizable_cache
|
||||
from synapse.util.caches.lrucache import LruCache
|
||||
|
||||
from tests.unittest import TestCase
|
||||
|
||||
|
||||
class FakeServer(Config):
|
||||
section = "server"
|
||||
|
||||
|
||||
class TestConfig(RootConfig):
|
||||
config_classes = [FakeServer, CacheConfig]
|
||||
|
||||
|
||||
class CacheConfigTests(TestCase):
|
||||
def setUp(self):
|
||||
CacheConfig._reset()
|
||||
|
||||
def test_individual_caches_from_environ(self):
|
||||
"""
|
||||
Individual cache factors will be loaded from the environment.
|
||||
"""
|
||||
config = {}
|
||||
t = TestConfig()
|
||||
t.caches._environ = {
|
||||
"SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
|
||||
"SYNAPSE_NOT_CACHE": "BLAH",
|
||||
}
|
||||
t.read_config(config, config_dir_path="", data_dir_path="")
|
||||
|
||||
self.assertEqual(dict(t.caches.cache_factors), {"something_or_other": 2.0})
|
||||
|
||||
def test_config_overrides_environ(self):
|
||||
"""
|
||||
Individual cache factors defined in config will take precedence over
|
||||
ones in the environment.
|
||||
"""
|
||||
config = {"caches": {"per_cache_factors": {"foo": 2, "bar": 3}}}
|
||||
t = TestConfig()
|
||||
t.caches._environ = {
|
||||
"SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
|
||||
"SYNAPSE_CACHE_FACTOR_FOO": 1,
|
||||
}
|
||||
t.read_config(config, config_dir_path="", data_dir_path="")
|
||||
|
||||
self.assertEqual(
|
||||
dict(t.caches.cache_factors),
|
||||
{"foo": 2.0, "bar": 3.0, "something_or_other": 2.0},
|
||||
)
|
||||
|
||||
def test_individual_instantiated_before_config_load(self):
|
||||
"""
|
||||
If a cache is instantiated before the config is read, it will be given
|
||||
the default cache size in the interim, and then resized once the config
|
||||
is loaded.
|
||||
"""
|
||||
cache = LruCache(100)
|
||||
add_resizable_cache("foo", cache.set_cache_factor)
|
||||
self.assertEqual(cache.max_size, 50)
|
||||
|
||||
config = {"caches": {"per_cache_factors": {"foo": 3}}}
|
||||
t = TestConfig()
|
||||
t.read_config(config, config_dir_path="", data_dir_path="")
|
||||
|
||||
self.assertEqual(cache.max_size, 300)
|
||||
|
||||
def test_individual_instantiated_after_config_load(self):
|
||||
"""
|
||||
If a cache is instantiated after the config is read, it will be
|
||||
immediately resized to the correct size given the per_cache_factor if
|
||||
there is one.
|
||||
"""
|
||||
config = {"caches": {"per_cache_factors": {"foo": 2}}}
|
||||
t = TestConfig()
|
||||
t.read_config(config, config_dir_path="", data_dir_path="")
|
||||
|
||||
cache = LruCache(100)
|
||||
add_resizable_cache("foo", cache.set_cache_factor)
|
||||
self.assertEqual(cache.max_size, 200)
|
||||
|
||||
def test_global_instantiated_before_config_load(self):
|
||||
"""
|
||||
If a cache is instantiated before the config is read, it will be given
|
||||
the default cache size in the interim, and then resized to the new
|
||||
default cache size once the config is loaded.
|
||||
"""
|
||||
cache = LruCache(100)
|
||||
add_resizable_cache("foo", cache.set_cache_factor)
|
||||
self.assertEqual(cache.max_size, 50)
|
||||
|
||||
config = {"caches": {"global_factor": 4}}
|
||||
t = TestConfig()
|
||||
t.read_config(config, config_dir_path="", data_dir_path="")
|
||||
|
||||
self.assertEqual(cache.max_size, 400)
|
||||
|
||||
def test_global_instantiated_after_config_load(self):
|
||||
"""
|
||||
If a cache is instantiated after the config is read, it will be
|
||||
immediately resized to the correct size given the global factor if there
|
||||
is no per-cache factor.
|
||||
"""
|
||||
config = {"caches": {"global_factor": 1.5}}
|
||||
t = TestConfig()
|
||||
t.read_config(config, config_dir_path="", data_dir_path="")
|
||||
|
||||
cache = LruCache(100)
|
||||
add_resizable_cache("foo", cache.set_cache_factor)
|
||||
self.assertEqual(cache.max_size, 150)
|
||||
@@ -25,8 +25,8 @@ from synapse.util.caches.descriptors import Cache, cached
|
||||
from tests import unittest
|
||||
|
||||
|
||||
class CacheTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
class CacheTestCase(unittest.HomeserverTestCase):
|
||||
def prepare(self, reactor, clock, homeserver):
|
||||
self.cache = Cache("test")
|
||||
|
||||
def test_empty(self):
|
||||
@@ -96,7 +96,7 @@ class CacheTestCase(unittest.TestCase):
|
||||
cache.get(3)
|
||||
|
||||
|
||||
class CacheDecoratorTestCase(unittest.TestCase):
|
||||
class CacheDecoratorTestCase(unittest.HomeserverTestCase):
|
||||
@defer.inlineCallbacks
|
||||
def test_passthrough(self):
|
||||
class A(object):
|
||||
@@ -239,7 +239,7 @@ class CacheDecoratorTestCase(unittest.TestCase):
|
||||
callcount2 = [0]
|
||||
|
||||
class A(object):
|
||||
@cached(max_entries=4) # HACK: This makes it 2 due to cache factor
|
||||
@cached(max_entries=2)
|
||||
def func(self, key):
|
||||
callcount[0] += 1
|
||||
return key
|
||||
|
||||
@@ -43,7 +43,7 @@ class ApplicationServiceStoreTestCase(unittest.TestCase):
|
||||
)
|
||||
|
||||
hs.config.app_service_config_files = self.as_yaml_files
|
||||
hs.config.event_cache_size = 1
|
||||
hs.config.caches.event_cache_size = 1
|
||||
hs.config.password_providers = []
|
||||
|
||||
self.as_token = "token1"
|
||||
@@ -110,7 +110,7 @@ class ApplicationServiceTransactionStoreTestCase(unittest.TestCase):
|
||||
)
|
||||
|
||||
hs.config.app_service_config_files = self.as_yaml_files
|
||||
hs.config.event_cache_size = 1
|
||||
hs.config.caches.event_cache_size = 1
|
||||
hs.config.password_providers = []
|
||||
|
||||
self.as_list = [
|
||||
@@ -422,7 +422,7 @@ class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
|
||||
)
|
||||
|
||||
hs.config.app_service_config_files = [f1, f2]
|
||||
hs.config.event_cache_size = 1
|
||||
hs.config.caches.event_cache_size = 1
|
||||
hs.config.password_providers = []
|
||||
|
||||
database = hs.get_datastores().databases[0]
|
||||
@@ -440,7 +440,7 @@ class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
|
||||
)
|
||||
|
||||
hs.config.app_service_config_files = [f1, f2]
|
||||
hs.config.event_cache_size = 1
|
||||
hs.config.caches.event_cache_size = 1
|
||||
hs.config.password_providers = []
|
||||
|
||||
with self.assertRaises(ConfigError) as cm:
|
||||
@@ -464,7 +464,7 @@ class ApplicationServiceStoreConfigTestCase(unittest.TestCase):
|
||||
)
|
||||
|
||||
hs.config.app_service_config_files = [f1, f2]
|
||||
hs.config.event_cache_size = 1
|
||||
hs.config.caches.event_cache_size = 1
|
||||
hs.config.password_providers = []
|
||||
|
||||
with self.assertRaises(ConfigError) as cm:
|
||||
|
||||
@@ -51,7 +51,8 @@ class SQLBaseStoreTestCase(unittest.TestCase):
|
||||
|
||||
config = Mock()
|
||||
config._disable_native_upserts = True
|
||||
config.event_cache_size = 1
|
||||
config.caches = Mock()
|
||||
config.caches.event_cache_size = 1
|
||||
hs = TestHomeServer("test", config=config)
|
||||
|
||||
sqlite_config = {"name": "sqlite3"}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
from synapse.metrics import REGISTRY, InFlightGauge, generate_latest
|
||||
from synapse.util.caches.descriptors import Cache
|
||||
|
||||
from tests import unittest
|
||||
|
||||
@@ -129,3 +130,36 @@ class BuildInfoTests(unittest.TestCase):
|
||||
self.assertTrue(b"osversion=" in items[0])
|
||||
self.assertTrue(b"pythonversion=" in items[0])
|
||||
self.assertTrue(b"version=" in items[0])
|
||||
|
||||
|
||||
class CacheMetricsTests(unittest.HomeserverTestCase):
|
||||
def test_cache_metric(self):
|
||||
"""
|
||||
Caches produce metrics reflecting their state when scraped.
|
||||
"""
|
||||
CACHE_NAME = "cache_metrics_test_fgjkbdfg"
|
||||
cache = Cache(CACHE_NAME, max_entries=777)
|
||||
|
||||
items = {
|
||||
x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
|
||||
for x in filter(
|
||||
lambda x: b"cache_metrics_test_fgjkbdfg" in x,
|
||||
generate_latest(REGISTRY).split(b"\n"),
|
||||
)
|
||||
}
|
||||
|
||||
self.assertEqual(items["synapse_util_caches_cache_size"], "0.0")
|
||||
self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")
|
||||
|
||||
cache.prefill("1", "hi")
|
||||
|
||||
items = {
|
||||
x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
|
||||
for x in filter(
|
||||
lambda x: b"cache_metrics_test_fgjkbdfg" in x,
|
||||
generate_latest(REGISTRY).split(b"\n"),
|
||||
)
|
||||
}
|
||||
|
||||
self.assertEqual(items["synapse_util_caches_cache_size"], "1.0")
|
||||
self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")
|
||||
|
||||
@@ -21,7 +21,7 @@ from tests.utils import MockClock
|
||||
from .. import unittest
|
||||
|
||||
|
||||
class ExpiringCacheTestCase(unittest.TestCase):
|
||||
class ExpiringCacheTestCase(unittest.HomeserverTestCase):
|
||||
def test_get_set(self):
|
||||
clock = MockClock()
|
||||
cache = ExpiringCache("test", clock, max_len=1)
|
||||
|
||||
@@ -22,7 +22,7 @@ from synapse.util.caches.treecache import TreeCache
|
||||
from .. import unittest
|
||||
|
||||
|
||||
class LruCacheTestCase(unittest.TestCase):
|
||||
class LruCacheTestCase(unittest.HomeserverTestCase):
|
||||
def test_get_set(self):
|
||||
cache = LruCache(1)
|
||||
cache["key"] = "value"
|
||||
@@ -84,7 +84,7 @@ class LruCacheTestCase(unittest.TestCase):
|
||||
self.assertEquals(len(cache), 0)
|
||||
|
||||
|
||||
class LruCacheCallbacksTestCase(unittest.TestCase):
|
||||
class LruCacheCallbacksTestCase(unittest.HomeserverTestCase):
|
||||
def test_get(self):
|
||||
m = Mock()
|
||||
cache = LruCache(1)
|
||||
@@ -233,7 +233,7 @@ class LruCacheCallbacksTestCase(unittest.TestCase):
|
||||
self.assertEquals(m3.call_count, 1)
|
||||
|
||||
|
||||
class LruCacheSizedTestCase(unittest.TestCase):
|
||||
class LruCacheSizedTestCase(unittest.HomeserverTestCase):
|
||||
def test_evict(self):
|
||||
cache = LruCache(5, size_callback=len)
|
||||
cache["key1"] = [0]
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
from mock import patch
|
||||
|
||||
from synapse.util.caches.stream_change_cache import StreamChangeCache
|
||||
|
||||
from tests import unittest
|
||||
|
||||
|
||||
class StreamChangeCacheTests(unittest.TestCase):
|
||||
class StreamChangeCacheTests(unittest.HomeserverTestCase):
|
||||
"""
|
||||
Tests for StreamChangeCache.
|
||||
"""
|
||||
@@ -46,7 +44,6 @@ class StreamChangeCacheTests(unittest.TestCase):
|
||||
self.assertTrue(cache.has_entity_changed("user@foo.com", 0))
|
||||
self.assertTrue(cache.has_entity_changed("not@here.website", 0))
|
||||
|
||||
@patch("synapse.util.caches.CACHE_SIZE_FACTOR", 1.0)
|
||||
def test_has_entity_changed_pops_off_start(self):
|
||||
"""
|
||||
StreamChangeCache.entity_has_changed will respect the max size and
|
||||
|
||||
@@ -164,6 +164,7 @@ def default_config(name, parse=False):
|
||||
# disable user directory updates, because they get done in the
|
||||
# background, which upsets the test runner.
|
||||
"update_user_directory": False,
|
||||
"caches": {"global_factor": 1},
|
||||
}
|
||||
|
||||
if parse:
|
||||
|
||||
Reference in New Issue
Block a user