1
0

test_cache.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright 2020 Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.config.cache import CacheConfig, add_resizable_cache
  15. from synapse.types import JsonDict
  16. from synapse.util.caches.lrucache import LruCache
  17. from tests.unittest import TestCase
  18. class CacheConfigTests(TestCase):
  19. def setUp(self) -> None:
  20. # Reset caches before each test since there's global state involved.
  21. self.config = CacheConfig()
  22. self.config.reset()
  23. def tearDown(self) -> None:
  24. # Also reset the caches after each test to leave state pristine.
  25. self.config.reset()
  26. def test_individual_caches_from_environ(self) -> None:
  27. """
  28. Individual cache factors will be loaded from the environment.
  29. """
  30. config: JsonDict = {}
  31. self.config._environ = {
  32. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  33. "SYNAPSE_NOT_CACHE": "BLAH",
  34. }
  35. self.config.read_config(config, config_dir_path="", data_dir_path="")
  36. self.config.resize_all_caches()
  37. self.assertEqual(dict(self.config.cache_factors), {"something_or_other": 2.0})
  38. def test_config_overrides_environ(self) -> None:
  39. """
  40. Individual cache factors defined in the environment will take precedence
  41. over those in the config.
  42. """
  43. config: JsonDict = {"caches": {"per_cache_factors": {"foo": 2, "bar": 3}}}
  44. self.config._environ = {
  45. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  46. "SYNAPSE_CACHE_FACTOR_FOO": "1",
  47. }
  48. self.config.read_config(config, config_dir_path="", data_dir_path="")
  49. self.config.resize_all_caches()
  50. self.assertEqual(
  51. dict(self.config.cache_factors),
  52. {"foo": 1.0, "bar": 3.0, "something_or_other": 2.0},
  53. )
  54. def test_individual_instantiated_before_config_load(self) -> None:
  55. """
  56. If a cache is instantiated before the config is read, it will be given
  57. the default cache size in the interim, and then resized once the config
  58. is loaded.
  59. """
  60. cache: LruCache = LruCache(100)
  61. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  62. self.assertEqual(cache.max_size, 50)
  63. config: JsonDict = {"caches": {"per_cache_factors": {"foo": 3}}}
  64. self.config.read_config(config)
  65. self.config.resize_all_caches()
  66. self.assertEqual(cache.max_size, 300)
  67. def test_individual_instantiated_after_config_load(self) -> None:
  68. """
  69. If a cache is instantiated after the config is read, it will be
  70. immediately resized to the correct size given the per_cache_factor if
  71. there is one.
  72. """
  73. config: JsonDict = {"caches": {"per_cache_factors": {"foo": 2}}}
  74. self.config.read_config(config, config_dir_path="", data_dir_path="")
  75. self.config.resize_all_caches()
  76. cache: LruCache = LruCache(100)
  77. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  78. self.assertEqual(cache.max_size, 200)
  79. def test_global_instantiated_before_config_load(self) -> None:
  80. """
  81. If a cache is instantiated before the config is read, it will be given
  82. the default cache size in the interim, and then resized to the new
  83. default cache size once the config is loaded.
  84. """
  85. cache: LruCache = LruCache(100)
  86. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  87. self.assertEqual(cache.max_size, 50)
  88. config: JsonDict = {"caches": {"global_factor": 4}}
  89. self.config.read_config(config, config_dir_path="", data_dir_path="")
  90. self.config.resize_all_caches()
  91. self.assertEqual(cache.max_size, 400)
  92. def test_global_instantiated_after_config_load(self) -> None:
  93. """
  94. If a cache is instantiated after the config is read, it will be
  95. immediately resized to the correct size given the global factor if there
  96. is no per-cache factor.
  97. """
  98. config: JsonDict = {"caches": {"global_factor": 1.5}}
  99. self.config.read_config(config, config_dir_path="", data_dir_path="")
  100. self.config.resize_all_caches()
  101. cache: LruCache = LruCache(100)
  102. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  103. self.assertEqual(cache.max_size, 150)
  104. def test_cache_with_asterisk_in_name(self) -> None:
  105. """Some caches have asterisks in their name, test that they are set correctly."""
  106. config: JsonDict = {
  107. "caches": {
  108. "per_cache_factors": {"*cache_a*": 5, "cache_b": 6, "cache_c": 2}
  109. }
  110. }
  111. self.config._environ = {
  112. "SYNAPSE_CACHE_FACTOR_CACHE_A": "2",
  113. "SYNAPSE_CACHE_FACTOR_CACHE_B": "3",
  114. }
  115. self.config.read_config(config, config_dir_path="", data_dir_path="")
  116. self.config.resize_all_caches()
  117. cache_a: LruCache = LruCache(100)
  118. add_resizable_cache("*cache_a*", cache_resize_callback=cache_a.set_cache_factor)
  119. self.assertEqual(cache_a.max_size, 200)
  120. cache_b: LruCache = LruCache(100)
  121. add_resizable_cache("*Cache_b*", cache_resize_callback=cache_b.set_cache_factor)
  122. self.assertEqual(cache_b.max_size, 300)
  123. cache_c: LruCache = LruCache(100)
  124. add_resizable_cache("*cache_c*", cache_resize_callback=cache_c.set_cache_factor)
  125. self.assertEqual(cache_c.max_size, 200)
  126. def test_apply_cache_factor_from_config(self) -> None:
  127. """Caches can disable applying cache factor updates, mainly used by
  128. event cache size.
  129. """
  130. config: JsonDict = {"caches": {"event_cache_size": "10k"}}
  131. self.config.read_config(config, config_dir_path="", data_dir_path="")
  132. self.config.resize_all_caches()
  133. cache: LruCache = LruCache(
  134. max_size=self.config.event_cache_size,
  135. apply_cache_factor_from_config=False,
  136. )
  137. add_resizable_cache("event_cache", cache_resize_callback=cache.set_cache_factor)
  138. self.assertEqual(cache.max_size, 10240)