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

Commit 477fad6

Browse files
committed
Refactor recursive code so we can wrap just the redaction part
See #13489 (comment)
1 parent 54b3676 commit 477fad6

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

synapse/storage/databases/main/events_worker.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
current_context,
5555
make_deferred_yieldable,
5656
)
57+
from synapse.logging.opentracing import start_active_span, tag_args, trace
5758
from synapse.metrics.background_process_metrics import (
5859
run_as_background_process,
5960
wrap_as_background_process,
@@ -430,6 +431,8 @@ async def get_events(
430431

431432
return {e.event_id: e for e in events}
432433

434+
@trace
435+
@tag_args
433436
async def get_events_as_list(
434437
self,
435438
event_ids: Collection[str],
@@ -1090,15 +1093,11 @@ async def _get_events_from_db(
10901093
"""
10911094
fetched_event_ids: Set[str] = set()
10921095
fetched_events: Dict[str, _EventRow] = {}
1093-
events_to_fetch = event_ids
1094-
1095-
while events_to_fetch:
1096-
row_map = await self._enqueue_events(events_to_fetch)
10971096

1097+
async def _recursively_fetch_redactions(row_map: Dict[str, _EventRow]) -> None:
10981098
# we need to recursively fetch any redactions of those events
10991099
redaction_ids: Set[str] = set()
1100-
for event_id in events_to_fetch:
1101-
row = row_map.get(event_id)
1100+
for event_id, row in row_map.items():
11021101
fetched_event_ids.add(event_id)
11031102
if row:
11041103
fetched_events[event_id] = row
@@ -1107,6 +1106,14 @@ async def _get_events_from_db(
11071106
events_to_fetch = redaction_ids.difference(fetched_event_ids)
11081107
if events_to_fetch:
11091108
logger.debug("Also fetching redaction events %s", events_to_fetch)
1109+
row_map = await self._enqueue_events(events_to_fetch)
1110+
await _recursively_fetch_redactions(row_map)
1111+
1112+
events_to_fetch = event_ids
1113+
row_map = await self._enqueue_events(events_to_fetch)
1114+
1115+
with start_active_span("recursively fetching redactions"):
1116+
await _recursively_fetch_redactions(row_map)
11101117

11111118
# build a map from event_id to EventBase
11121119
event_map: Dict[str, EventBase] = {}
@@ -1424,6 +1431,8 @@ async def have_events_in_timeline(self, event_ids: Iterable[str]) -> Set[str]:
14241431

14251432
return {r["event_id"] for r in rows}
14261433

1434+
@trace
1435+
@tag_args
14271436
async def have_seen_events(
14281437
self, room_id: str, event_ids: Iterable[str]
14291438
) -> Set[str]:

0 commit comments

Comments
 (0)