Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit da77720

Browse files
authored
Check the stream position before checking if the cache is empty. (#14639)
An empty cache does not mean the entity has no changed, if it is earlier than the earliest known stream position return that the entity *has* changed since the cache cannot accurately answer that query.
1 parent f3ad68c commit da77720

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

changelog.d/14639.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where the user directory and room/user stats might be out of sync.

synapse/util/caches/stream_change_cache.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,17 @@ def has_any_entity_changed(self, stream_pos: int) -> bool:
213213
"""
214214
assert isinstance(stream_pos, int)
215215

216-
if not self._cache:
217-
# If the cache is empty, nothing can have changed.
218-
return False
219-
220216
# _cache is not valid at or before the earliest known stream position, so
221217
# return that an entity has changed.
222218
if stream_pos <= self._earliest_known_stream_pos:
223219
self.metrics.inc_misses()
224220
return True
225221

222+
# If the cache is empty, nothing can have changed.
223+
if not self._cache:
224+
self.metrics.inc_misses()
225+
return False
226+
226227
self.metrics.inc_hits()
227228
return stream_pos < self._cache.peekitem()[0]
228229

tests/util/test_stream_change_cache.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,10 @@ def test_has_any_entity_changed(self) -> None:
144144
"""
145145
cache = StreamChangeCache("#test", 1)
146146

147-
# With no entities, it returns False for the past, present, and future.
148-
self.assertFalse(cache.has_any_entity_changed(0))
149-
self.assertFalse(cache.has_any_entity_changed(1))
147+
# With no entities, it returns True for the past, present, and False for
148+
# the future.
149+
self.assertTrue(cache.has_any_entity_changed(0))
150+
self.assertTrue(cache.has_any_entity_changed(1))
150151
self.assertFalse(cache.has_any_entity_changed(2))
151152

152153
# We add an entity

0 commit comments

Comments
 (0)