This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow configuration of Synapse's cache without using synctl or environment variables #6391
Merged
Merged
Changes from 59 commits
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
99ab65a
fix up logging to use rapidjson
hawkowl d684ec8
benchmarks
hawkowl 135fdaa
update linting
hawkowl f36f3ab
Merge remote-tracking branch 'origin/develop' into hawkowl/structured…
hawkowl 73cfdeb
fix
hawkowl babf4ea
Merge branch 'develop' into hawkowl/structured-logging-perf
hawkowl e55591d
Merge remote-tracking branch 'origin/develop' into hawkowl/structured…
hawkowl c580eb3
revert this
hawkowl f888515
Merge remote-tracking branch 'origin/develop' into hawkowl/structured…
hawkowl a14831d
Move cache configuration into a homeserver config option, instead of …
hawkowl 50fcb4a
Re-sync every so often, in case caches appear
hawkowl 8439ce5
Merge remote-tracking branch 'origin/develop' into hawkowl/structured…
hawkowl 92d4d13
fixes
hawkowl d70be18
Merge remote-tracking branch 'origin/develop' into hawkowl/structured…
hawkowl c11c8ad
more fixes
hawkowl 0e368ee
more fixes
hawkowl f7ec526
add some LruCache benchmarks
hawkowl e174b2d
fix style
hawkowl 946650c
Merge remote-tracking branch 'origin/develop' into hawkowl/benchmark-…
hawkowl 9735a08
newsfile
hawkowl c76a412
Merge remote-tracking branch 'origin/develop' into hawkowl/cache-conf…
hawkowl 0a02b2a
cleanup
hawkowl a21702f
cleanup so it can refer to late-config
hawkowl 125c5a0
Merge branch 'hawkowl/benchmark-lrucache' into hawkowl/cache-config-w…
hawkowl 0b069b7
make expiring caches resizeable
hawkowl a96b5d9
Load cache factors from the environment, and add a test.
hawkowl 2f4dbfa
document as well as refactor so that CacheMetric is not nested
hawkowl ac020de
fix style
hawkowl 18c1dbf
don't need this comment
hawkowl 4aeb6fb
Merge remote-tracking branch 'origin/develop' into hawkowl/cache-conf…
hawkowl 5f508e7
add tests for individual cache sizing, and fix up the individual cach…
hawkowl 2619891
fix
hawkowl 0fc0a7d
fix
hawkowl 965e259
fixes
hawkowl f004cee
Merge remote-tracking branch 'origin/develop' into hawkowl/cache-conf…
hawkowl 546c23e
Merge branch 'develop' of github.com:matrix-org/synapse into hawkowl/…
anoadragon453 7117b89
Update synapse/config/cache.py
anoadragon453 4ed7aa1
Move separate CacheConfig global vars into a single global dict
anoadragon453 be0737a
Move _DEFAULT_CONFIG to class method
anoadragon453 14fbf27
Merge branch 'hawkowl/cache-config-without-synctl' of github.com:matr…
anoadragon453 cc1dd98
lint
anoadragon453 de25659
Fix typing
anoadragon453 6e979a1
sample config
anoadragon453 fec13be
Fix lint
anoadragon453 905c833
Add some mypy ignores
anoadragon453 f300c08
Merge branch 'develop' of github.com:matrix-org/synapse into hawkowl/…
anoadragon453 28f7f59
Re-add default_size_factor multipler. Revert _max_size modification
anoadragon453 5f3eddc
Refactor globals in CacheConfig
anoadragon453 fcc9c3a
dict() -> {}
anoadragon453 5acca12
Some variable cleanups in cache.py
anoadragon453 fdef3ad
Make add_resizable_cache args clearer in cache tests
anoadragon453 ca98241
lint
anoadragon453 48709a9
Pull global cache prefix from environment by default
anoadragon453 9e3a9ba
Make config follow the guidelines
anoadragon453 fa56e16
sample config
anoadragon453 83cf583
Allow creating an LruCache that's not affected by cache factor
anoadragon453 424215a
Fix cache: config block parsing
anoadragon453 c7f3bf6
Environment takes precedence over config values
anoadragon453 485f177
fix tests
anoadragon453 fe89050
Fix and clarify config comments
anoadragon453 ae6070b
Move event_cache_size to CacheConfig
anoadragon453 4448633
Follow config file conventions. Move cache config location
anoadragon453 f8ed432
Attempt to explain the default global cache factor
anoadragon453 c1b339b
Merge branch 'develop' of github.com:matrix-org/synapse into hawkowl/…
anoadragon453 06fbfc2
Merge branch 'develop' of github.com:matrix-org/synapse into hawkowl/…
anoadragon453 edb04df
Merge branch 'develop' of github.com:matrix-org/synapse into hawkowl/…
anoadragon453 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1973,3 +1973,20 @@ opentracing: | |
# | ||
# logging: | ||
# false | ||
|
||
|
||
## Cache Configuration ## | ||
|
||
# Caching can be configured through the following options. | ||
# | ||
caches: | ||
# Controls the global cache factor. This overrides the "SYNAPSE_CACHE_FACTOR" | ||
# environment variable. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please can you explain what a global cache factor is, and document the default setting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've attempted to do so.
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# global_factor: 0.5 | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# A dictionary of cache name to cache factor for that | ||
# individual cache. | ||
# | ||
# per_cache_factors: | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# get_users_who_share_room_with_user: 2 | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
# -*- 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 Callable, Dict | ||
|
||
from ._base import Config, ConfigError | ||
|
||
# The prefix for all cache factor-related environment variables | ||
_CACHES = {} | ||
_CACHE_PREFIX = "SYNAPSE_CACHE_FACTOR" | ||
_DEFAULT_FACTOR_SIZE = 0.5 | ||
_DEFAULT_EVENT_CACHE_SIZE = "10K" | ||
|
||
|
||
class CacheProperties(object): | ||
def __init__(self): | ||
# The default factor size for all caches | ||
self.default_factor_size = float( | ||
os.environ.get(_CACHE_PREFIX, _DEFAULT_FACTOR_SIZE) | ||
) | ||
self.resize_all_caches_func = None | ||
|
||
|
||
properties = CacheProperties() | ||
|
||
|
||
def add_resizable_cache(cache_name: str, cache_resize_callback: Callable): | ||
"""Register a cache that's size can dynamically change | ||
|
||
Args: | ||
cache_name: A reference to the cache | ||
cache_resize_callback: A callback function that will be ran whenever | ||
the cache needs to be resized | ||
""" | ||
_CACHES[cache_name.lower()] = cache_resize_callback | ||
|
||
# Ensure all loaded caches are sized appropriately | ||
# | ||
# This method should only run once the config has been read, | ||
# as it uses values read from it | ||
if properties.resize_all_caches_func: | ||
properties.resize_all_caches_func() | ||
|
||
|
||
class CacheConfig(Config): | ||
section = "caches" | ||
_environ = os.environ | ||
|
||
@staticmethod | ||
def reset(): | ||
"""Resets the caches to their defaults. Used for tests.""" | ||
properties.default_factor_size = float( | ||
os.environ.get(_CACHE_PREFIX, _DEFAULT_FACTOR_SIZE) | ||
) | ||
properties.resize_all_caches_func = None | ||
_CACHES.clear() | ||
|
||
def generate_config_section(self, **kwargs): | ||
return """\ | ||
## Cache Configuration ## | ||
|
||
# Caching can be configured through the following options. | ||
# | ||
caches: | ||
# Controls the global cache factor. This overrides the "SYNAPSE_CACHE_FACTOR" | ||
# environment variable. | ||
# | ||
# global_factor: 0.5 | ||
|
||
# A dictionary of cache name to cache factor for that | ||
# individual cache. | ||
# | ||
# per_cache_factors: | ||
# get_users_who_share_room_with_user: 2 | ||
""" | ||
|
||
def read_config(self, config, **kwargs): | ||
self.event_cache_size = self.parse_size( | ||
config.get("event_cache_size", _DEFAULT_EVENT_CACHE_SIZE) | ||
) | ||
self.cache_factors = {} # type: Dict[str, float] | ||
|
||
cache_config = config.get("caches") or {} | ||
self.global_factor = cache_config.get( | ||
"global_factor", properties.default_factor_size | ||
) | ||
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 | ||
properties.default_factor_size = self.global_factor | ||
|
||
# Load cache factors from the config | ||
individual_factors = cache_config.get("per_cache_factors", {}) or {} | ||
if not isinstance(individual_factors, dict): | ||
raise ConfigError("caches.per_cache_factors must be a dictionary") | ||
|
||
# Override factors from environment if necessary | ||
individual_factors.update( | ||
{ | ||
key[len(_CACHE_PREFIX) + 1 :].lower(): float(val) | ||
for key, val in self._environ.items() | ||
if key.startswith(_CACHE_PREFIX + "_") | ||
} | ||
) | ||
|
||
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 | ||
|
||
# Resize all caches (if necessary) with the new factors we've loaded | ||
self.resize_all_caches() | ||
|
||
# Store this function so that it can be called from other classes without | ||
# needing an instance of Config | ||
properties.resize_all_caches_func = self.resize_all_caches | ||
|
||
def resize_all_caches(self): | ||
"""Ensure all cache sizes are up to date | ||
|
||
For each cache, run the mapped callback function with either | ||
a specific cache factor or the default, global one. | ||
""" | ||
for cache_name, callback in _CACHES.items(): | ||
new_factor = self.cache_factors.get(cache_name, self.global_factor) | ||
callback(new_factor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised the very end of the file is the most appropriate place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved it much further up, near the database config. What do you think?