test_lrucache.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. # Copyright 2015, 2016 OpenMarket Ltd
  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 typing import List, Tuple
  15. from unittest.mock import Mock, patch
  16. from synapse.metrics.jemalloc import JemallocStats
  17. from synapse.types import JsonDict
  18. from synapse.util.caches.lrucache import LruCache, setup_expire_lru_cache_entries
  19. from synapse.util.caches.treecache import TreeCache
  20. from tests import unittest
  21. from tests.unittest import override_config
  22. class LruCacheTestCase(unittest.HomeserverTestCase):
  23. def test_get_set(self) -> None:
  24. cache: LruCache[str, str] = LruCache(1)
  25. cache["key"] = "value"
  26. self.assertEqual(cache.get("key"), "value")
  27. self.assertEqual(cache["key"], "value")
  28. def test_eviction(self) -> None:
  29. cache: LruCache[int, int] = LruCache(2)
  30. cache[1] = 1
  31. cache[2] = 2
  32. self.assertEqual(cache.get(1), 1)
  33. self.assertEqual(cache.get(2), 2)
  34. cache[3] = 3
  35. self.assertEqual(cache.get(1), None)
  36. self.assertEqual(cache.get(2), 2)
  37. self.assertEqual(cache.get(3), 3)
  38. def test_setdefault(self) -> None:
  39. cache: LruCache[str, int] = LruCache(1)
  40. self.assertEqual(cache.setdefault("key", 1), 1)
  41. self.assertEqual(cache.get("key"), 1)
  42. self.assertEqual(cache.setdefault("key", 2), 1)
  43. self.assertEqual(cache.get("key"), 1)
  44. cache["key"] = 2 # Make sure overriding works.
  45. self.assertEqual(cache.get("key"), 2)
  46. def test_pop(self) -> None:
  47. cache: LruCache[str, int] = LruCache(1)
  48. cache["key"] = 1
  49. self.assertEqual(cache.pop("key"), 1)
  50. self.assertEqual(cache.pop("key"), None)
  51. def test_del_multi(self) -> None:
  52. # The type here isn't quite correct as they don't handle TreeCache well.
  53. cache: LruCache[Tuple[str, str], str] = LruCache(4, cache_type=TreeCache)
  54. cache[("animal", "cat")] = "mew"
  55. cache[("animal", "dog")] = "woof"
  56. cache[("vehicles", "car")] = "vroom"
  57. cache[("vehicles", "train")] = "chuff"
  58. self.assertEqual(len(cache), 4)
  59. self.assertEqual(cache.get(("animal", "cat")), "mew")
  60. self.assertEqual(cache.get(("vehicles", "car")), "vroom")
  61. cache.del_multi(("animal",)) # type: ignore[arg-type]
  62. self.assertEqual(len(cache), 2)
  63. self.assertEqual(cache.get(("animal", "cat")), None)
  64. self.assertEqual(cache.get(("animal", "dog")), None)
  65. self.assertEqual(cache.get(("vehicles", "car")), "vroom")
  66. self.assertEqual(cache.get(("vehicles", "train")), "chuff")
  67. # Man from del_multi say "Yes".
  68. def test_clear(self) -> None:
  69. cache: LruCache[str, int] = LruCache(1)
  70. cache["key"] = 1
  71. cache.clear()
  72. self.assertEqual(len(cache), 0)
  73. @override_config({"caches": {"per_cache_factors": {"mycache": 10}}})
  74. def test_special_size(self) -> None:
  75. cache: LruCache = LruCache(10, "mycache")
  76. self.assertEqual(cache.max_size, 100)
  77. class LruCacheCallbacksTestCase(unittest.HomeserverTestCase):
  78. def test_get(self) -> None:
  79. m = Mock()
  80. cache: LruCache[str, str] = LruCache(1)
  81. cache.set("key", "value")
  82. self.assertFalse(m.called)
  83. cache.get("key", callbacks=[m])
  84. self.assertFalse(m.called)
  85. cache.get("key", "value")
  86. self.assertFalse(m.called)
  87. cache.set("key", "value2")
  88. self.assertEqual(m.call_count, 1)
  89. cache.set("key", "value")
  90. self.assertEqual(m.call_count, 1)
  91. def test_multi_get(self) -> None:
  92. m = Mock()
  93. cache: LruCache[str, str] = LruCache(1)
  94. cache.set("key", "value")
  95. self.assertFalse(m.called)
  96. cache.get("key", callbacks=[m])
  97. self.assertFalse(m.called)
  98. cache.get("key", callbacks=[m])
  99. self.assertFalse(m.called)
  100. cache.set("key", "value2")
  101. self.assertEqual(m.call_count, 1)
  102. cache.set("key", "value")
  103. self.assertEqual(m.call_count, 1)
  104. def test_set(self) -> None:
  105. m = Mock()
  106. cache: LruCache[str, str] = LruCache(1)
  107. cache.set("key", "value", callbacks=[m])
  108. self.assertFalse(m.called)
  109. cache.set("key", "value")
  110. self.assertFalse(m.called)
  111. cache.set("key", "value2")
  112. self.assertEqual(m.call_count, 1)
  113. cache.set("key", "value")
  114. self.assertEqual(m.call_count, 1)
  115. def test_pop(self) -> None:
  116. m = Mock()
  117. cache: LruCache[str, str] = LruCache(1)
  118. cache.set("key", "value", callbacks=[m])
  119. self.assertFalse(m.called)
  120. cache.pop("key")
  121. self.assertEqual(m.call_count, 1)
  122. cache.set("key", "value")
  123. self.assertEqual(m.call_count, 1)
  124. cache.pop("key")
  125. self.assertEqual(m.call_count, 1)
  126. def test_del_multi(self) -> None:
  127. m1 = Mock()
  128. m2 = Mock()
  129. m3 = Mock()
  130. m4 = Mock()
  131. # The type here isn't quite correct as they don't handle TreeCache well.
  132. cache: LruCache[Tuple[str, str], str] = LruCache(4, cache_type=TreeCache)
  133. cache.set(("a", "1"), "value", callbacks=[m1])
  134. cache.set(("a", "2"), "value", callbacks=[m2])
  135. cache.set(("b", "1"), "value", callbacks=[m3])
  136. cache.set(("b", "2"), "value", callbacks=[m4])
  137. self.assertEqual(m1.call_count, 0)
  138. self.assertEqual(m2.call_count, 0)
  139. self.assertEqual(m3.call_count, 0)
  140. self.assertEqual(m4.call_count, 0)
  141. cache.del_multi(("a",)) # type: ignore[arg-type]
  142. self.assertEqual(m1.call_count, 1)
  143. self.assertEqual(m2.call_count, 1)
  144. self.assertEqual(m3.call_count, 0)
  145. self.assertEqual(m4.call_count, 0)
  146. def test_clear(self) -> None:
  147. m1 = Mock()
  148. m2 = Mock()
  149. cache: LruCache[str, str] = LruCache(5)
  150. cache.set("key1", "value", callbacks=[m1])
  151. cache.set("key2", "value", callbacks=[m2])
  152. self.assertEqual(m1.call_count, 0)
  153. self.assertEqual(m2.call_count, 0)
  154. cache.clear()
  155. self.assertEqual(m1.call_count, 1)
  156. self.assertEqual(m2.call_count, 1)
  157. def test_eviction(self) -> None:
  158. m1 = Mock(name="m1")
  159. m2 = Mock(name="m2")
  160. m3 = Mock(name="m3")
  161. cache: LruCache[str, str] = LruCache(2)
  162. cache.set("key1", "value", callbacks=[m1])
  163. cache.set("key2", "value", callbacks=[m2])
  164. self.assertEqual(m1.call_count, 0)
  165. self.assertEqual(m2.call_count, 0)
  166. self.assertEqual(m3.call_count, 0)
  167. cache.set("key3", "value", callbacks=[m3])
  168. self.assertEqual(m1.call_count, 1)
  169. self.assertEqual(m2.call_count, 0)
  170. self.assertEqual(m3.call_count, 0)
  171. cache.set("key3", "value")
  172. self.assertEqual(m1.call_count, 1)
  173. self.assertEqual(m2.call_count, 0)
  174. self.assertEqual(m3.call_count, 0)
  175. cache.get("key2")
  176. self.assertEqual(m1.call_count, 1)
  177. self.assertEqual(m2.call_count, 0)
  178. self.assertEqual(m3.call_count, 0)
  179. cache.set("key1", "value", callbacks=[m1])
  180. self.assertEqual(m1.call_count, 1)
  181. self.assertEqual(m2.call_count, 0)
  182. self.assertEqual(m3.call_count, 1)
  183. class LruCacheSizedTestCase(unittest.HomeserverTestCase):
  184. def test_evict(self) -> None:
  185. cache: LruCache[str, List[int]] = LruCache(5, size_callback=len)
  186. cache["key1"] = [0]
  187. cache["key2"] = [1, 2]
  188. cache["key3"] = [3]
  189. cache["key4"] = [4]
  190. self.assertEqual(cache["key1"], [0])
  191. self.assertEqual(cache["key2"], [1, 2])
  192. self.assertEqual(cache["key3"], [3])
  193. self.assertEqual(cache["key4"], [4])
  194. self.assertEqual(len(cache), 5)
  195. cache["key5"] = [5, 6]
  196. self.assertEqual(len(cache), 4)
  197. self.assertEqual(cache.get("key1"), None)
  198. self.assertEqual(cache.get("key2"), None)
  199. self.assertEqual(cache["key3"], [3])
  200. self.assertEqual(cache["key4"], [4])
  201. self.assertEqual(cache["key5"], [5, 6])
  202. def test_zero_size_drop_from_cache(self) -> None:
  203. """Test that `drop_from_cache` works correctly with 0-sized entries."""
  204. cache: LruCache[str, List[int]] = LruCache(5, size_callback=lambda x: 0)
  205. cache["key1"] = []
  206. self.assertEqual(len(cache), 0)
  207. assert isinstance(cache.cache, dict)
  208. cache.cache["key1"].drop_from_cache()
  209. self.assertIsNone(
  210. cache.pop("key1"), "Cache entry should have been evicted but wasn't"
  211. )
  212. class TimeEvictionTestCase(unittest.HomeserverTestCase):
  213. """Test that time based eviction works correctly."""
  214. def default_config(self) -> JsonDict:
  215. config = super().default_config()
  216. config.setdefault("caches", {})["expiry_time"] = "30m"
  217. return config
  218. def test_evict(self) -> None:
  219. setup_expire_lru_cache_entries(self.hs)
  220. cache: LruCache[str, int] = LruCache(5, clock=self.hs.get_clock())
  221. # Check that we evict entries we haven't accessed for 30 minutes.
  222. cache["key1"] = 1
  223. cache["key2"] = 2
  224. self.reactor.advance(20 * 60)
  225. self.assertEqual(cache.get("key1"), 1)
  226. self.reactor.advance(20 * 60)
  227. # We have only touched `key1` in the last 30m, so we expect that to
  228. # still be in the cache while `key2` should have been evicted.
  229. self.assertEqual(cache.get("key1"), 1)
  230. self.assertEqual(cache.get("key2"), None)
  231. # Check that re-adding an expired key works correctly.
  232. cache["key2"] = 3
  233. self.assertEqual(cache.get("key2"), 3)
  234. self.reactor.advance(20 * 60)
  235. self.assertEqual(cache.get("key2"), 3)
  236. self.reactor.advance(20 * 60)
  237. self.assertEqual(cache.get("key1"), None)
  238. self.assertEqual(cache.get("key2"), 3)
  239. class MemoryEvictionTestCase(unittest.HomeserverTestCase):
  240. @override_config(
  241. {
  242. "caches": {
  243. "cache_autotuning": {
  244. "max_cache_memory_usage": "700M",
  245. "target_cache_memory_usage": "500M",
  246. "min_cache_ttl": "5m",
  247. }
  248. }
  249. }
  250. )
  251. @patch("synapse.util.caches.lrucache.get_jemalloc_stats")
  252. def test_evict_memory(self, jemalloc_interface: Mock) -> None:
  253. mock_jemalloc_class = Mock(spec=JemallocStats)
  254. jemalloc_interface.return_value = mock_jemalloc_class
  255. # set the return value of get_stat() to be greater than max_cache_memory_usage
  256. mock_jemalloc_class.get_stat.return_value = 924288000
  257. setup_expire_lru_cache_entries(self.hs)
  258. cache: LruCache[str, int] = LruCache(4, clock=self.hs.get_clock())
  259. cache["key1"] = 1
  260. cache["key2"] = 2
  261. # advance the reactor less than the min_cache_ttl
  262. self.reactor.advance(60 * 2)
  263. # our items should still be in the cache
  264. self.assertEqual(cache.get("key1"), 1)
  265. self.assertEqual(cache.get("key2"), 2)
  266. # advance the reactor past the min_cache_ttl
  267. self.reactor.advance(60 * 6)
  268. # the items should be cleared from cache
  269. self.assertEqual(cache.get("key1"), None)
  270. self.assertEqual(cache.get("key2"), None)
  271. # add more stuff to caches
  272. cache["key1"] = 1
  273. cache["key2"] = 2
  274. # set the return value of get_stat() to be lower than target_cache_memory_usage
  275. mock_jemalloc_class.get_stat.return_value = 10000
  276. # advance the reactor past the min_cache_ttl
  277. self.reactor.advance(60 * 6)
  278. # the items should still be in the cache
  279. self.assertEqual(cache.get("key1"), 1)
  280. self.assertEqual(cache.get("key2"), 2)