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

Commit 703a8f9

Browse files
Instrument state and state_group storage related things (tracing) (#15610)
Instrument `state` and `state_group` storage related things (tracing) so it's a little more clear where these database transactions are coming from as there is a lot of wires crossing in these functions. Part of `/messages` performance investigation: #13356
1 parent ca3c07e commit 703a8f9

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

changelog.d/15610.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Instrument `state` and `state_group` storage-related operations to better picture what's happening when tracing.

synapse/events/snapshot.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from synapse.appservice import ApplicationService
2121
from synapse.events import EventBase
22+
from synapse.logging.opentracing import tag_args, trace
2223
from synapse.types import JsonDict, StateMap
2324

2425
if TYPE_CHECKING:
@@ -242,6 +243,8 @@ def state_group(self) -> Optional[int]:
242243

243244
return self._state_group
244245

246+
@trace
247+
@tag_args
245248
async def get_current_state_ids(
246249
self, state_filter: Optional["StateFilter"] = None
247250
) -> Optional[StateMap[str]]:
@@ -275,6 +278,8 @@ async def get_current_state_ids(
275278

276279
return prev_state_ids
277280

281+
@trace
282+
@tag_args
278283
async def get_prev_state_ids(
279284
self, state_filter: Optional["StateFilter"] = None
280285
) -> StateMap[str]:

synapse/state/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
UnpersistedEventContextBase,
4646
)
4747
from synapse.logging.context import ContextResourceUsage
48+
from synapse.logging.opentracing import tag_args, trace
4849
from synapse.replication.http.state import ReplicationUpdateCurrentStateRestServlet
4950
from synapse.state import v1, v2
5051
from synapse.storage.databases.main.events_worker import EventRedactBehaviour
@@ -270,6 +271,8 @@ async def get_hosts_in_room_at_events(
270271
state = await entry.get_state(self._state_storage_controller, StateFilter.all())
271272
return await self.store.get_joined_hosts(room_id, state, entry)
272273

274+
@trace
275+
@tag_args
273276
async def calculate_context_info(
274277
self,
275278
event: EventBase,
@@ -465,6 +468,7 @@ async def compute_event_context(
465468

466469
return await unpersisted_context.persist(event)
467470

471+
@trace
468472
@measure_func()
469473
async def resolve_state_groups_for_events(
470474
self, room_id: str, event_ids: Collection[str], await_full_state: bool = True

synapse/storage/controllers/state.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def notify_room_un_partial_stated(self, room_id: str) -> None:
6767
"""
6868
self._partial_state_room_tracker.notify_un_partial_stated(room_id)
6969

70+
@trace
71+
@tag_args
7072
async def get_state_group_delta(
7173
self, state_group: int
7274
) -> Tuple[Optional[int], Optional[StateMap[str]]]:
@@ -84,6 +86,8 @@ async def get_state_group_delta(
8486
state_group_delta = await self.stores.state.get_state_group_delta(state_group)
8587
return state_group_delta.prev_group, state_group_delta.delta_ids
8688

89+
@trace
90+
@tag_args
8791
async def get_state_groups_ids(
8892
self, _room_id: str, event_ids: Collection[str], await_full_state: bool = True
8993
) -> Dict[int, MutableStateMap[str]]:
@@ -114,6 +118,8 @@ async def get_state_groups_ids(
114118

115119
return group_to_state
116120

121+
@trace
122+
@tag_args
117123
async def get_state_ids_for_group(
118124
self, state_group: int, state_filter: Optional[StateFilter] = None
119125
) -> StateMap[str]:
@@ -130,6 +136,8 @@ async def get_state_ids_for_group(
130136

131137
return group_to_state[state_group]
132138

139+
@trace
140+
@tag_args
133141
async def get_state_groups(
134142
self, room_id: str, event_ids: Collection[str]
135143
) -> Dict[int, List[EventBase]]:
@@ -165,6 +173,8 @@ async def get_state_groups(
165173
for group, event_id_map in group_to_ids.items()
166174
}
167175

176+
@trace
177+
@tag_args
168178
def _get_state_groups_from_groups(
169179
self, groups: List[int], state_filter: StateFilter
170180
) -> Awaitable[Dict[int, StateMap[str]]]:
@@ -183,6 +193,7 @@ def _get_state_groups_from_groups(
183193
return self.stores.state._get_state_groups_from_groups(groups, state_filter)
184194

185195
@trace
196+
@tag_args
186197
async def get_state_for_events(
187198
self, event_ids: Collection[str], state_filter: Optional[StateFilter] = None
188199
) -> Dict[str, StateMap[EventBase]]:
@@ -280,6 +291,8 @@ async def get_state_ids_for_events(
280291

281292
return {event: event_to_state[event] for event in event_ids}
282293

294+
@trace
295+
@tag_args
283296
async def get_state_for_event(
284297
self, event_id: str, state_filter: Optional[StateFilter] = None
285298
) -> StateMap[EventBase]:
@@ -303,6 +316,7 @@ async def get_state_for_event(
303316
return state_map[event_id]
304317

305318
@trace
319+
@tag_args
306320
async def get_state_ids_for_event(
307321
self,
308322
event_id: str,
@@ -333,6 +347,8 @@ async def get_state_ids_for_event(
333347
)
334348
return state_map[event_id]
335349

350+
@trace
351+
@tag_args
336352
def get_state_for_groups(
337353
self, groups: Iterable[int], state_filter: Optional[StateFilter] = None
338354
) -> Awaitable[Dict[int, MutableStateMap[str]]]:
@@ -402,6 +418,8 @@ async def store_state_group(
402418
event_id, room_id, prev_group, delta_ids, current_state_ids
403419
)
404420

421+
@trace
422+
@tag_args
405423
@cancellable
406424
async def get_current_state_ids(
407425
self,
@@ -442,6 +460,8 @@ async def get_current_state_ids(
442460
room_id, on_invalidate=on_invalidate
443461
)
444462

463+
@trace
464+
@tag_args
445465
async def get_canonical_alias_for_room(self, room_id: str) -> Optional[str]:
446466
"""Get canonical alias for room, if any
447467
@@ -466,6 +486,8 @@ async def get_canonical_alias_for_room(self, room_id: str) -> Optional[str]:
466486

467487
return event.content.get("canonical_alias")
468488

489+
@trace
490+
@tag_args
469491
async def get_current_state_deltas(
470492
self, prev_stream_id: int, max_stream_id: int
471493
) -> Tuple[int, List[Dict[str, Any]]]:
@@ -500,6 +522,7 @@ async def get_current_state_deltas(
500522
)
501523

502524
@trace
525+
@tag_args
503526
async def get_current_state(
504527
self, room_id: str, state_filter: Optional[StateFilter] = None
505528
) -> StateMap[EventBase]:
@@ -516,6 +539,8 @@ async def get_current_state(
516539

517540
return state_map
518541

542+
@trace
543+
@tag_args
519544
async def get_current_state_event(
520545
self, room_id: str, event_type: str, state_key: str
521546
) -> Optional[EventBase]:
@@ -527,6 +552,8 @@ async def get_current_state_event(
527552
)
528553
return state_map.get(key)
529554

555+
@trace
556+
@tag_args
530557
async def get_current_hosts_in_room(self, room_id: str) -> AbstractSet[str]:
531558
"""Get current hosts in room based on current state.
532559
@@ -538,6 +565,8 @@ async def get_current_hosts_in_room(self, room_id: str) -> AbstractSet[str]:
538565

539566
return await self.stores.main.get_current_hosts_in_room(room_id)
540567

568+
@trace
569+
@tag_args
541570
async def get_current_hosts_in_room_ordered(self, room_id: str) -> List[str]:
542571
"""Get current hosts in room based on current state.
543572
@@ -553,6 +582,8 @@ async def get_current_hosts_in_room_ordered(self, room_id: str) -> List[str]:
553582

554583
return await self.stores.main.get_current_hosts_in_room_ordered(room_id)
555584

585+
@trace
586+
@tag_args
556587
async def get_current_hosts_in_room_or_partial_state_approximation(
557588
self, room_id: str
558589
) -> Collection[str]:
@@ -582,6 +613,8 @@ async def get_current_hosts_in_room_or_partial_state_approximation(
582613

583614
return hosts
584615

616+
@trace
617+
@tag_args
585618
async def get_users_in_room_with_profiles(
586619
self, room_id: str
587620
) -> Mapping[str, ProfileInfo]:

synapse/storage/databases/state/bg_updates.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import logging
1616
from typing import TYPE_CHECKING, Dict, List, Mapping, Optional, Tuple, Union
1717

18+
from synapse.logging.opentracing import tag_args, trace
1819
from synapse.storage._base import SQLBaseStore
1920
from synapse.storage.database import (
2021
DatabasePool,
@@ -40,6 +41,8 @@ class StateGroupBackgroundUpdateStore(SQLBaseStore):
4041
updates.
4142
"""
4243

44+
@trace
45+
@tag_args
4346
def _count_state_group_hops_txn(
4447
self, txn: LoggingTransaction, state_group: int
4548
) -> int:
@@ -83,6 +86,8 @@ def _count_state_group_hops_txn(
8386

8487
return count
8588

89+
@trace
90+
@tag_args
8691
def _get_state_groups_from_groups_txn(
8792
self,
8893
txn: LoggingTransaction,

synapse/storage/databases/state/store.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from synapse.api.constants import EventTypes
2121
from synapse.events import EventBase
2222
from synapse.events.snapshot import UnpersistedEventContext, UnpersistedEventContextBase
23+
from synapse.logging.opentracing import tag_args, trace
2324
from synapse.storage._base import SQLBaseStore
2425
from synapse.storage.database import (
2526
DatabasePool,
@@ -159,6 +160,8 @@ def _get_state_group_delta_txn(txn: LoggingTransaction) -> _GetStateGroupDelta:
159160
"get_state_group_delta", _get_state_group_delta_txn
160161
)
161162

163+
@trace
164+
@tag_args
162165
@cancellable
163166
async def _get_state_groups_from_groups(
164167
self, groups: List[int], state_filter: StateFilter
@@ -187,6 +190,8 @@ async def _get_state_groups_from_groups(
187190

188191
return results
189192

193+
@trace
194+
@tag_args
190195
def _get_state_for_group_using_cache(
191196
self,
192197
cache: DictionaryCache[int, StateKey, str],
@@ -239,6 +244,8 @@ def _get_state_for_group_using_cache(
239244

240245
return state_filter.filter_state(state_dict_ids), not missing_types
241246

247+
@trace
248+
@tag_args
242249
@cancellable
243250
async def _get_state_for_groups(
244251
self, groups: Iterable[int], state_filter: Optional[StateFilter] = None
@@ -305,6 +312,8 @@ async def _get_state_for_groups(
305312

306313
return state
307314

315+
@trace
316+
@tag_args
308317
def _get_state_for_groups_using_cache(
309318
self,
310319
groups: Iterable[int],
@@ -403,6 +412,8 @@ def _insert_into_cache(
403412
fetched_keys=non_member_types,
404413
)
405414

415+
@trace
416+
@tag_args
406417
async def store_state_deltas_for_batched(
407418
self,
408419
events_and_context: List[Tuple[EventBase, UnpersistedEventContextBase]],
@@ -520,6 +531,8 @@ def insert_deltas_group_txn(
520531
prev_group,
521532
)
522533

534+
@trace
535+
@tag_args
523536
async def store_state_group(
524537
self,
525538
event_id: str,
@@ -772,6 +785,8 @@ def _purge_unreferenced_state_groups(
772785
((sg,) for sg in state_groups_to_delete),
773786
)
774787

788+
@trace
789+
@tag_args
775790
async def get_previous_state_groups(
776791
self, state_groups: Iterable[int]
777792
) -> Dict[int, int]:

0 commit comments

Comments
 (0)