test_cache.py 6.1 KB

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