test_cache.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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._base import Config, RootConfig
  15. from synapse.config.cache import CacheConfig, add_resizable_cache
  16. from synapse.util.caches.lrucache import LruCache
  17. from tests.unittest import TestCase
  18. class FakeServer(Config):
  19. section = "server"
  20. class TestConfig(RootConfig):
  21. config_classes = [FakeServer, CacheConfig]
  22. class CacheConfigTests(TestCase):
  23. def setUp(self):
  24. # Reset caches before each test
  25. TestConfig().caches.reset()
  26. def test_individual_caches_from_environ(self):
  27. """
  28. Individual cache factors will be loaded from the environment.
  29. """
  30. config = {}
  31. t = TestConfig()
  32. t.caches._environ = {
  33. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  34. "SYNAPSE_NOT_CACHE": "BLAH",
  35. }
  36. t.read_config(config, config_dir_path="", data_dir_path="")
  37. self.assertEqual(dict(t.caches.cache_factors), {"something_or_other": 2.0})
  38. def test_config_overrides_environ(self):
  39. """
  40. Individual cache factors defined in the environment will take precedence
  41. over those in the config.
  42. """
  43. config = {"caches": {"per_cache_factors": {"foo": 2, "bar": 3}}}
  44. t = TestConfig()
  45. t.caches._environ = {
  46. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  47. "SYNAPSE_CACHE_FACTOR_FOO": 1,
  48. }
  49. t.read_config(config, config_dir_path="", data_dir_path="")
  50. self.assertEqual(
  51. dict(t.caches.cache_factors),
  52. {"foo": 1.0, "bar": 3.0, "something_or_other": 2.0},
  53. )
  54. def test_individual_instantiated_before_config_load(self):
  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(100)
  61. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  62. self.assertEqual(cache.max_size, 50)
  63. config = {"caches": {"per_cache_factors": {"foo": 3}}}
  64. t = TestConfig()
  65. t.read_config(config, config_dir_path="", data_dir_path="")
  66. self.assertEqual(cache.max_size, 300)
  67. def test_individual_instantiated_after_config_load(self):
  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 = {"caches": {"per_cache_factors": {"foo": 2}}}
  74. t = TestConfig()
  75. t.read_config(config, config_dir_path="", data_dir_path="")
  76. cache = 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):
  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(100)
  86. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  87. self.assertEqual(cache.max_size, 50)
  88. config = {"caches": {"global_factor": 4}}
  89. t = TestConfig()
  90. t.read_config(config, config_dir_path="", data_dir_path="")
  91. self.assertEqual(cache.max_size, 400)
  92. def test_global_instantiated_after_config_load(self):
  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 = {"caches": {"global_factor": 1.5}}
  99. t = TestConfig()
  100. t.read_config(config, config_dir_path="", data_dir_path="")
  101. cache = 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):
  105. """Some caches have asterisks in their name, test that they are set correctly."""
  106. config = {
  107. "caches": {
  108. "per_cache_factors": {"*cache_a*": 5, "cache_b": 6, "cache_c": 2}
  109. }
  110. }
  111. t = TestConfig()
  112. t.caches._environ = {
  113. "SYNAPSE_CACHE_FACTOR_CACHE_A": "2",
  114. "SYNAPSE_CACHE_FACTOR_CACHE_B": 3,
  115. }
  116. t.read_config(config, config_dir_path="", data_dir_path="")
  117. cache_a = 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(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(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):
  127. """Caches can disable applying cache factor updates, mainly used by
  128. event cache size.
  129. """
  130. config = {"caches": {"event_cache_size": "10k"}}
  131. t = TestConfig()
  132. t.read_config(config, config_dir_path="", data_dir_path="")
  133. cache = LruCache(
  134. max_size=t.caches.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)