Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Reload cache factors from disk on SIGHUP #12673

Merged
merged 17 commits into from
May 11, 2022
Merged
3 changes: 1 addition & 2 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,14 @@ def reload_cache_config(config: HomeServerConfig) -> None:
# we call this closure.
assert config is not None
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring pwease?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# This will call CacheConfig.read_config, which will automatically call
# CacheConfig.resize_all_caches.
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: %s", config.caches.__dict__)
config.caches.resize_all_caches()


def setup_sentry(hs: "HomeServer") -> None:
Expand Down
1 change: 1 addition & 0 deletions synapse/app/generic_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ def start(config_options: List[str]) -> None:

synapse.events.USE_FROZEN_DICTS = config.server.use_frozen_dicts
synapse.util.caches.TRACK_MEMORY_USAGE = config.caches.track_memory_usage
config.caches.resize_all_caches()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to be in the synapse/app/_base.py so that the main process picks it up too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh ffs. It was meant to be. thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 55963ac. I also snuck in f199d82.


if config.server.gc_seconds:
synapse.metrics.MIN_TIME_BETWEEN_GCS = config.server.gc_seconds
Expand Down
24 changes: 13 additions & 11 deletions synapse/config/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ def generate_config_section(self, **kwargs: Any) -> str:
"""

def read_config(self, config: JsonDict, **kwargs: Any) -> None:
"""Populate this config object with values from `config`.

This method does NOT resize existing or future caches: use `resize_all_caches`.
We use two separate methods so that we can reject bad config before applying it.
"""
self.event_cache_size = self.parse_size(
config.get("event_cache_size", _DEFAULT_EVENT_CACHE_SIZE)
)
Expand All @@ -191,9 +196,6 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
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):
Expand Down Expand Up @@ -259,19 +261,19 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
cache_config.get("sync_response_cache_duration", 0)
)

# 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) -> None:
"""Ensure all cache sizes are up to date
"""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.
"""
# Set the global factor size, so that new caches are appropriately sized.
properties.default_factor_size = self.global_factor

# Store this function so that it can be called from other classes without
# needing an instance of CacheConfig
properties.resize_all_caches_func = self.resize_all_caches

# block other threads from modifying _CACHES while we iterate it.
with _CACHES_LOCK:
for cache_name, callback in _CACHES.items():
Expand Down