This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Refactor have_seen_events to reduce OOMs #12886
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Refactor `have_seen_events` to reduce memory consumed when processing federation traffic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1356,14 +1356,23 @@ async def have_seen_events( | |
Returns: | ||
The set of events we have already seen. | ||
""" | ||
res = await self._have_seen_events_dict( | ||
(room_id, event_id) for event_id in event_ids | ||
) | ||
return {eid for ((_rid, eid), have_event) in res.items() if have_event} | ||
|
||
# @cachedList chomps lots of memory if you call it with a big list, so | ||
# we break it down. However, each batch requires its own index scan, so we make | ||
# the batches as big as possible. | ||
|
||
results: Set[str] = set() | ||
for chunk in batch_iter(event_ids, 500): | ||
r = await self._have_seen_events_dict( | ||
[(room_id, event_id) for event_id in chunk] | ||
) | ||
results.update(eid for ((_rid, eid), have_event) in r.items() if have_event) | ||
|
||
return results | ||
|
||
@cachedList(cached_method_name="have_seen_event", list_name="keys") | ||
async def _have_seen_events_dict( | ||
self, keys: Iterable[Tuple[str, str]] | ||
self, keys: Collection[Tuple[str, str]] | ||
) -> Dict[Tuple[str, str], bool]: | ||
"""Helper for have_seen_events | ||
|
||
|
@@ -1375,33 +1384,30 @@ async def _have_seen_events_dict( | |
cache_results = { | ||
(rid, eid) for (rid, eid) in keys if self._get_event_cache.contains((eid,)) | ||
} | ||
results = {x: True for x in cache_results} | ||
results = dict.fromkeys(cache_results, True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was a driveby optimisation while I was in the area. |
||
remaining = [k for k in keys if k not in cache_results] | ||
if not remaining: | ||
return results | ||
|
||
def have_seen_events_txn( | ||
txn: LoggingTransaction, chunk: Tuple[Tuple[str, str], ...] | ||
) -> None: | ||
def have_seen_events_txn(txn: LoggingTransaction) -> None: | ||
# we deliberately do *not* query the database for room_id, to make the | ||
# query an index-only lookup on `events_event_id_key`. | ||
# | ||
# We therefore pull the events from the database into a set... | ||
|
||
sql = "SELECT event_id FROM events AS e WHERE " | ||
clause, args = make_in_list_sql_clause( | ||
txn.database_engine, "e.event_id", [eid for (_rid, eid) in chunk] | ||
txn.database_engine, "e.event_id", [eid for (_rid, eid) in remaining] | ||
) | ||
txn.execute(sql + clause, args) | ||
found_events = {eid for eid, in txn} | ||
|
||
# ... and then we can update the results for each row in the batch | ||
results.update({(rid, eid): (eid in found_events) for (rid, eid) in chunk}) | ||
|
||
# each batch requires its own index scan, so we make the batches as big as | ||
# possible. | ||
for chunk in batch_iter((k for k in keys if k not in cache_results), 500): | ||
await self.db_pool.runInteraction( | ||
"have_seen_events", have_seen_events_txn, chunk | ||
# ... and then we can update the results for each key | ||
results.update( | ||
{(rid, eid): (eid in found_events) for (rid, eid) in remaining} | ||
) | ||
|
||
await self.db_pool.runInteraction("have_seen_events", have_seen_events_txn) | ||
return results | ||
|
||
@cached(max_entries=100000, tree=True) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OOI do we know why
@cachedList
is so heavyweight? That feels like it might be a useful thing to fix (separately)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's because it creates a new Deferred for each (missing) entry in the list. Maybe?
I agree it would be a useful thing to investigate. I did consider trying to do add support for batching to
@cachedList
, but it looked scary.