1
0

test_cache.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.util.caches.lrucache import LruCache
  16. from tests.unittest import TestCase
  17. class CacheConfigTests(TestCase):
  18. def setUp(self):
  19. # Reset caches before each test since there's global state involved.
  20. self.config = CacheConfig()
  21. self.config.reset()
  22. def tearDown(self):
  23. # Also reset the caches after each test to leave state pristine.
  24. self.config.reset()
  25. def test_individual_caches_from_environ(self):
  26. """
  27. Individual cache factors will be loaded from the environment.
  28. """
  29. config = {}
  30. self.config._environ = {
  31. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  32. "SYNAPSE_NOT_CACHE": "BLAH",
  33. }
  34. self.config.read_config(config, config_dir_path="", data_dir_path="")
  35. self.config.resize_all_caches()
  36. self.assertEqual(dict(self.config.cache_factors), {"something_or_other": 2.0})
  37. def test_config_overrides_environ(self):
  38. """
  39. Individual cache factors defined in the environment will take precedence
  40. over those in the config.
  41. """
  42. config = {"caches": {"per_cache_factors": {"foo": 2, "bar": 3}}}
  43. self.config._environ = {
  44. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  45. "SYNAPSE_CACHE_FACTOR_FOO": 1,
  46. }
  47. self.config.read_config(config, config_dir_path="", data_dir_path="")
  48. self.config.resize_all_caches()
  49. self.assertEqual(
  50. dict(self.config.cache_factors),
  51. {"foo": 1.0, "bar": 3.0, "something_or_other": 2.0},
  52. )
  53. def test_individual_instantiated_before_config_load(self):
  54. """
  55. If a cache is instantiated before the config is read, it will be given
  56. the default cache size in the interim, and then resized once the config
  57. is loaded.
  58. """
  59. cache = LruCache(100)
  60. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  61. self.assertEqual(cache.max_size, 50)
  62. config = {"caches": {"per_cache_factors": {"foo": 3}}}
  63. self.config.read_config(config)
  64. self.config.resize_all_caches()
  65. self.assertEqual(cache.max_size, 300)
  66. def test_individual_instantiated_after_config_load(self):
  67. """
  68. If a cache is instantiated after the config is read, it will be
  69. immediately resized to the correct size given the per_cache_factor if
  70. there is one.
  71. """
  72. config = {"caches": {"per_cache_factors": {"foo": 2}}}
  73. self.config.read_config(config, config_dir_path="", data_dir_path="")
  74. self.config.resize_all_caches()
  75. cache = LruCache(100)
  76. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  77. self.assertEqual(cache.max_size, 200)
  78. def test_global_instantiated_before_config_load(self):
  79. """
  80. If a cache is instantiated before the config is read, it will be given
  81. the default cache size in the interim, and then resized to the new
  82. default cache size once the config is loaded.
  83. """
  84. cache = LruCache(100)
  85. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  86. self.assertEqual(cache.max_size, 50)
  87. config = {"caches": {"global_factor": 4}}
  88. self.config.read_config(config, config_dir_path="", data_dir_path="")
  89. self.config.resize_all_caches()
  90. self.assertEqual(cache.max_size, 400)
  91. def test_global_instantiated_after_config_load(self):
  92. """
  93. If a cache is instantiated after the config is read, it will be
  94. immediately resized to the correct size given the global factor if there
  95. is no per-cache factor.
  96. """
  97. config = {"caches": {"global_factor": 1.5}}
  98. self.config.read_config(config, config_dir_path="", data_dir_path="")
  99. self.config.resize_all_caches()
  100. cache = LruCache(100)
  101. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  102. self.assertEqual(cache.max_size, 150)
  103. def test_cache_with_asterisk_in_name(self):
  104. """Some caches have asterisks in their name, test that they are set correctly."""
  105. config = {
  106. "caches": {
  107. "per_cache_factors": {"*cache_a*": 5, "cache_b": 6, "cache_c": 2}
  108. }
  109. }
  110. self.config._environ = {
  111. "SYNAPSE_CACHE_FACTOR_CACHE_A": "2",
  112. "SYNAPSE_CACHE_FACTOR_CACHE_B": 3,
  113. }
  114. self.config.read_config(config, config_dir_path="", data_dir_path="")
  115. self.config.resize_all_caches()
  116. cache_a = LruCache(100)
  117. add_resizable_cache("*cache_a*", cache_resize_callback=cache_a.set_cache_factor)
  118. self.assertEqual(cache_a.max_size, 200)
  119. cache_b = LruCache(100)
  120. add_resizable_cache("*Cache_b*", cache_resize_callback=cache_b.set_cache_factor)
  121. self.assertEqual(cache_b.max_size, 300)
  122. cache_c = LruCache(100)
  123. add_resizable_cache("*cache_c*", cache_resize_callback=cache_c.set_cache_factor)
  124. self.assertEqual(cache_c.max_size, 200)
  125. def test_apply_cache_factor_from_config(self):
  126. """Caches can disable applying cache factor updates, mainly used by
  127. event cache size.
  128. """
  129. config = {"caches": {"event_cache_size": "10k"}}
  130. self.config.read_config(config, config_dir_path="", data_dir_path="")
  131. self.config.resize_all_caches()
  132. cache = LruCache(
  133. max_size=self.config.event_cache_size,
  134. apply_cache_factor_from_config=False,
  135. )
  136. add_resizable_cache("event_cache", cache_resize_callback=cache.set_cache_factor)
  137. self.assertEqual(cache.max_size, 10240)