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

Optimise async get event lookups #13435

Merged
merged 11 commits into from
Aug 4, 2022
19 changes: 15 additions & 4 deletions synapse/storage/databases/main/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,10 @@ async def _get_events_from_cache_or_db(
Returns:
map from event id to result
"""
event_entry_map = await self._get_events_from_cache(
# Shortcut: check if we have any events in the *in memory* cache - this function
# may be called repeatedly for the same event so at this point we cannot reach
# out to any external cache for performance reasons.
richvdh marked this conversation as resolved.
Show resolved Hide resolved
event_entry_map = self._get_events_from_local_cache(
event_ids,
)

Expand Down Expand Up @@ -632,7 +635,7 @@ async def _get_events_from_cache_or_db(

if missing_events_ids:

async def get_missing_events_from_db() -> Dict[str, EventCacheEntry]:
async def get_missing_events_from_cache_or_db() -> Dict[str, EventCacheEntry]:
"""Fetches the events in `missing_event_ids` from the database.

Also creates entries in `self._current_event_fetches` to allow
Expand All @@ -657,10 +660,18 @@ async def get_missing_events_from_db() -> Dict[str, EventCacheEntry]:
# the events have been redacted, and if so pulling the redaction event
# out of the database to check it.
#
missing_events = {}
try:
missing_events = await self._get_events_from_db(
# First fetch from the cache - including any external caches
richvdh marked this conversation as resolved.
Show resolved Hide resolved
cache_missing_events = await self._get_events_from_cache(
missing_events_ids,
)
richvdh marked this conversation as resolved.
Show resolved Hide resolved
missing_events.update(cache_missing_events)
# Now actually fetch any remaining events from the DB
db_missing_events = await self._get_events_from_db(
missing_events_ids - set(cache_missing_events.keys()),
)
Fizzadar marked this conversation as resolved.
Show resolved Hide resolved
missing_events.update(db_missing_events)
except Exception as e:
with PreserveLoggingContext():
fetching_deferred.errback(e)
Expand All @@ -679,7 +690,7 @@ async def get_missing_events_from_db() -> Dict[str, EventCacheEntry]:
# cancellations, since multiple `_get_events_from_cache_or_db` calls can
# reuse the same fetch.
missing_events: Dict[str, EventCacheEntry] = await delay_cancellation(
get_missing_events_from_db()
get_missing_events_from_cache_or_db()
)
event_entry_map.update(missing_events)

Expand Down