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

Commit 91c60f3

Browse files
authored
Improve logging of state resolution (#8371)
I'd like to get a better insight into what we are doing with respect to state res. The list of state groups we are resolving across should be short (if it isn't, that's a massive problem in itself), so it should be fine to log it in ite entiretly. I've done some grepping and found approximately zero cases in which the "shortcut" code delivered the result, so I've ripped that out too.
1 parent 302dc89 commit 91c60f3

File tree

2 files changed

+17
-48
lines changed

2 files changed

+17
-48
lines changed

changelog.d/8371.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve logging of state resolution.

synapse/state/__init__.py

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
Sequence,
2626
Set,
2727
Union,
28-
cast,
2928
overload,
3029
)
3130

@@ -42,7 +41,7 @@
4241
from synapse.state import v1, v2
4342
from synapse.storage.databases.main.events_worker import EventRedactBehaviour
4443
from synapse.storage.roommember import ProfileInfo
45-
from synapse.types import Collection, MutableStateMap, StateMap
44+
from synapse.types import Collection, StateMap
4645
from synapse.util import Clock
4746
from synapse.util.async_helpers import Linearizer
4847
from synapse.util.caches.expiringcache import ExpiringCache
@@ -472,10 +471,9 @@ class StateResolutionHandler:
472471
def __init__(self, hs):
473472
self.clock = hs.get_clock()
474473

475-
# dict of set of event_ids -> _StateCacheEntry.
476-
self._state_cache = None
477474
self.resolve_linearizer = Linearizer(name="state_resolve_lock")
478475

476+
# dict of set of event_ids -> _StateCacheEntry.
479477
self._state_cache = ExpiringCache(
480478
cache_name="state_cache",
481479
clock=self.clock,
@@ -519,57 +517,28 @@ async def resolve_state_groups(
519517
Returns:
520518
The resolved state
521519
"""
522-
logger.debug("resolve_state_groups state_groups %s", state_groups_ids.keys())
523-
524520
group_names = frozenset(state_groups_ids.keys())
525521

526522
with (await self.resolve_linearizer.queue(group_names)):
527-
if self._state_cache is not None:
528-
cache = self._state_cache.get(group_names, None)
529-
if cache:
530-
return cache
523+
cache = self._state_cache.get(group_names, None)
524+
if cache:
525+
return cache
531526

532527
logger.info(
533-
"Resolving state for %s with %d groups", room_id, len(state_groups_ids)
528+
"Resolving state for %s with groups %s", room_id, list(group_names),
534529
)
535530

536531
state_groups_histogram.observe(len(state_groups_ids))
537532

538-
# start by assuming we won't have any conflicted state, and build up the new
539-
# state map by iterating through the state groups. If we discover a conflict,
540-
# we give up and instead use `resolve_events_with_store`.
541-
#
542-
# XXX: is this actually worthwhile, or should we just let
543-
# resolve_events_with_store do it?
544-
new_state = {} # type: MutableStateMap[str]
545-
conflicted_state = False
546-
for st in state_groups_ids.values():
547-
for key, e_id in st.items():
548-
if key in new_state:
549-
conflicted_state = True
550-
break
551-
new_state[key] = e_id
552-
if conflicted_state:
553-
break
554-
555-
if conflicted_state:
556-
logger.info("Resolving conflicted state for %r", room_id)
557-
with Measure(self.clock, "state._resolve_events"):
558-
# resolve_events_with_store returns a StateMap, but we can
559-
# treat it as a MutableStateMap as it is above. It isn't
560-
# actually mutated anymore (and is frozen in
561-
# _make_state_cache_entry below).
562-
new_state = cast(
563-
MutableStateMap,
564-
await resolve_events_with_store(
565-
self.clock,
566-
room_id,
567-
room_version,
568-
list(state_groups_ids.values()),
569-
event_map=event_map,
570-
state_res_store=state_res_store,
571-
),
572-
)
533+
with Measure(self.clock, "state._resolve_events"):
534+
new_state = await resolve_events_with_store(
535+
self.clock,
536+
room_id,
537+
room_version,
538+
list(state_groups_ids.values()),
539+
event_map=event_map,
540+
state_res_store=state_res_store,
541+
)
573542

574543
# if the new state matches any of the input state groups, we can
575544
# use that state group again. Otherwise we will generate a state_id
@@ -579,8 +548,7 @@ async def resolve_state_groups(
579548
with Measure(self.clock, "state.create_group_ids"):
580549
cache = _make_state_cache_entry(new_state, state_groups_ids)
581550

582-
if self._state_cache is not None:
583-
self._state_cache[group_names] = cache
551+
self._state_cache[group_names] = cache
584552

585553
return cache
586554

0 commit comments

Comments
 (0)