forked-synapse/synapse/storage
Richard van der Hoff af2248f8bf
Optimise missing prev_event handling (#9601)
Background: When we receive incoming federation traffic, and notice that we are missing prev_events from 
the incoming traffic, first we do a `/get_missing_events` request, and then if we still have missing prev_events,
we set up new backwards-extremities. To do that, we need to make a `/state_ids` request to ask the remote
server for the state at those prev_events, and then we may need to then ask the remote server for any events
in that state which we don't already have, as well as the auth events for those missing state events, so that we
can auth them.

This PR attempts to optimise the processing of that state request. The `state_ids` API returns a list of the state
events, as well as a list of all the auth events for *all* of those state events. The optimisation comes from the
observation that we are currently loading all of those auth events into memory at the start of the operation, but
we almost certainly aren't going to need *all* of the auth events. Rather, we can check that we have them, and
leave the actual load into memory for later. (Ideally the federation API would tell us which auth events we're
actually going to need, but it doesn't.)

The effect of this is to reduce the number of events that I need to load for an event in Matrix HQ from about
60000 to about 22000, which means it can stay in my in-memory cache, whereas previously the sheer number
of events meant that all 60K events had to be loaded from db for each request, due to the amount of cache
churn. (NB I've already tripled the size of the cache from its default of 10K).

Unfortunately I've ended up basically C&Ping `_get_state_for_room` and `_get_events_from_store_or_dest` into
a new method, because `_get_state_for_room` is also called during backfill, which expects the auth events to be
returned, so the same tricks don't work. That said, I don't really know why that codepath is completely different
(ultimately we're doing the same thing in setting up a new backwards extremity) so I've left a TODO suggesting
that we clean it up.
2021-03-15 13:51:02 +00:00
..
databases Optimise missing prev_event handling (#9601) 2021-03-15 13:51:02 +00:00
engines Update black, and run auto formatting over the codebase (#9381) 2021-02-16 22:32:34 +00:00
schema Only run one background update at a time 2020-03-31 17:43:58 +01:00
util Refactor to ensure we call check_consistency (#9470) 2021-02-24 10:13:53 +00:00
__init__.py Update black, and run auto formatting over the codebase (#9381) 2021-02-16 22:32:34 +00:00
_base.py Add additional type hints to the storage module. (#8980) 2020-12-30 08:09:53 -05:00
background_updates.py Update black, and run auto formatting over the codebase (#9381) 2021-02-16 22:32:34 +00:00
database.py Refactor to ensure we call check_consistency (#9470) 2021-02-24 10:13:53 +00:00
keys.py Add additional type hints to the storage module. (#8980) 2020-12-30 08:09:53 -05:00
persist_events.py Update black, and run auto formatting over the codebase (#9381) 2021-02-16 22:32:34 +00:00
prepare_database.py Update black, and run auto formatting over the codebase (#9381) 2021-02-16 22:32:34 +00:00
purge_events.py Purge chain cover tables when purging events. (#9498) 2021-03-03 11:04:08 -05:00
push_rule.py Move storage classes into a main "data store". 2019-10-21 16:05:06 +01:00
relations.py Add additional type hints to the storage module. (#8980) 2020-12-30 08:09:53 -05:00
roommember.py Bump the mypy and mypy-zope versions. (#9529) 2021-03-03 07:19:19 -05:00
state.py Update black, and run auto formatting over the codebase (#9381) 2021-02-16 22:32:34 +00:00
types.py Update type hints for Cursor to match PEP 249. (#9299) 2021-02-05 15:39:19 -05:00