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

Commit

Permalink
Refactor recursive code so we can wrap just the redaction part
Browse files Browse the repository at this point in the history
  • Loading branch information
MadLittleMods committed Aug 11, 2022
1 parent 54b3676 commit 477fad6
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions synapse/storage/databases/main/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
current_context,
make_deferred_yieldable,
)
from synapse.logging.opentracing import start_active_span, tag_args, trace
from synapse.metrics.background_process_metrics import (
run_as_background_process,
wrap_as_background_process,
Expand Down Expand Up @@ -430,6 +431,8 @@ async def get_events(

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

@trace
@tag_args
async def get_events_as_list(
self,
event_ids: Collection[str],
Expand Down Expand Up @@ -1090,15 +1093,11 @@ async def _get_events_from_db(
"""
fetched_event_ids: Set[str] = set()
fetched_events: Dict[str, _EventRow] = {}
events_to_fetch = event_ids

while events_to_fetch:
row_map = await self._enqueue_events(events_to_fetch)

async def _recursively_fetch_redactions(row_map: Dict[str, _EventRow]) -> None:
# we need to recursively fetch any redactions of those events
redaction_ids: Set[str] = set()
for event_id in events_to_fetch:
row = row_map.get(event_id)
for event_id, row in row_map.items():
fetched_event_ids.add(event_id)
if row:
fetched_events[event_id] = row
Expand All @@ -1107,6 +1106,14 @@ async def _get_events_from_db(
events_to_fetch = redaction_ids.difference(fetched_event_ids)
if events_to_fetch:
logger.debug("Also fetching redaction events %s", events_to_fetch)
row_map = await self._enqueue_events(events_to_fetch)
await _recursively_fetch_redactions(row_map)

events_to_fetch = event_ids
row_map = await self._enqueue_events(events_to_fetch)

with start_active_span("recursively fetching redactions"):
await _recursively_fetch_redactions(row_map)

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

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

@trace
@tag_args
async def have_seen_events(
self, room_id: str, event_ids: Iterable[str]
) -> Set[str]:
Expand Down

0 comments on commit 477fad6

Please sign in to comment.