-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Reload cache factors from disk on SIGHUP #12673
Changes from all commits
d942823
077ce90
1f668be
2a5ce2f
69da4ca
85f4385
1febfd6
4039b2d
b7a6ec5
519f98b
b9595ad
1118b9f
f6e1394
55963ac
f199d82
412414d
a7418c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Synapse will now reload [cache config](https://matrix-org.github.io/synapse/latest/usage/configuration/config_documentation.html#caching) when it receives a [SIGHUP](https://en.wikipedia.org/wiki/SIGHUP) signal. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,9 +48,12 @@ | |
from twisted.protocols.tls import TLSMemoryBIOFactory | ||
from twisted.python.threadpool import ThreadPool | ||
|
||
import synapse.util.caches | ||
from synapse.api.constants import MAX_PDU_SIZE | ||
from synapse.app import check_bind_error | ||
from synapse.app.phone_stats_home import start_phone_stats_home | ||
from synapse.config import ConfigError | ||
from synapse.config._base import format_config_error | ||
from synapse.config.homeserver import HomeServerConfig | ||
from synapse.config.server import ManholeConfig | ||
from synapse.crypto import context_factory | ||
|
@@ -426,6 +429,10 @@ def run_sighup(*args: Any, **kwargs: Any) -> None: | |
signal.signal(signal.SIGHUP, run_sighup) | ||
|
||
register_sighup(refresh_certificate, hs) | ||
register_sighup(reload_cache_config, hs.config) | ||
|
||
# Apply the cache config. | ||
hs.config.caches.resize_all_caches() | ||
|
||
# Load the certificate from disk. | ||
refresh_certificate(hs) | ||
|
@@ -480,6 +487,43 @@ def run_sighup(*args: Any, **kwargs: Any) -> None: | |
atexit.register(gc.freeze) | ||
|
||
|
||
def reload_cache_config(config: HomeServerConfig) -> None: | ||
"""Reload cache config from disk and immediately apply it.resize caches accordingly. | ||
|
||
If the config is invalid, a `ConfigError` is logged and no changes are made. | ||
|
||
Otherwise, this: | ||
- replaces the `caches` section on the given `config` object, | ||
- resizes all caches according to the new cache factors, and | ||
|
||
Note that the following cache config keys are read, but not applied: | ||
- event_cache_size: used to set a max_size and _original_max_size on | ||
EventsWorkerStore._get_event_cache when it is created. We'd have to update | ||
the _original_max_size (and maybe | ||
- sync_response_cache_duration: would have to update the timeout_sec attribute on | ||
HomeServer -> SyncHandler -> ResponseCache. | ||
- track_memory_usage. This affects synapse.util.caches.TRACK_MEMORY_USAGE which | ||
influences Synapse's self-reported metrics. | ||
|
||
Also, the HTTPConnectionPool in SimpleHTTPClient sets its maxPersistentPerHost | ||
parameter based on the global_factor. This won't be applied on a config reload. | ||
""" | ||
try: | ||
previous_cache_config = config.reload_config_section("caches") | ||
except ConfigError as e: | ||
logger.warning("Failed to reload cache config") | ||
for f in format_config_error(e): | ||
logger.warning(f) | ||
else: | ||
logger.debug( | ||
"New cache config. Was:\n %s\nNow:\n", | ||
previous_cache_config.__dict__, | ||
config.caches.__dict__, | ||
) | ||
synapse.util.caches.TRACK_MEMORY_USAGE = config.caches.track_memory_usage | ||
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. Hmm, so this is safe to update to Happy for us to do one of:
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'll go for option 1 and update the docstring. I'll also update the docs to only say that SIGHUP reloads cache factors. 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. Fair! 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. One more look over please? |
||
config.caches.resize_all_caches() | ||
|
||
|
||
def setup_sentry(hs: "HomeServer") -> None: | ||
"""Enable sentry integration, if enabled in configuration""" | ||
|
||
|
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.
Docstring pwease?
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.
412414d