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
Federation Sender & Appservice Pusher Stream Optimisations #13251
Merged
richvdh
merged 6 commits into
matrix-org:develop
from
Fizzadar:fed-as-stream-optimisations
Jul 15, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b42c899
Replace `get_new_events_for_appservice` with `get_all_new_events_stream`
Fizzadar 9910139
Pull received TS alongside events when processing the stream
Fizzadar 49581d7
Linting
Fizzadar 45157fa
Add changelog file
Fizzadar b1f11cc
Linting
Fizzadar c59d717
Fixup docstrings & txn names
Fizzadar 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 @@ | ||
Optimise federation sender and appservice pusher event stream processing queries. Contributed by Nick @ Beeper (@fizzadar). |
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
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
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
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
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 |
---|---|---|
|
@@ -1022,8 +1022,8 @@ def _get_events_around_txn( | |
} | ||
|
||
async def get_all_new_events_stream( | ||
self, from_id: int, current_id: int, limit: int | ||
) -> Tuple[int, List[EventBase]]: | ||
self, from_id: int, current_id: int, limit: int, get_prev_content: bool = False | ||
) -> Tuple[int, List[EventBase], Dict[str, Optional[int]]]: | ||
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. please update the docstring! 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. |
||
"""Get all new events | ||
|
||
Returns all events with from_id < stream_ordering <= current_id. | ||
|
@@ -1032,19 +1032,21 @@ async def get_all_new_events_stream( | |
from_id: the stream_ordering of the last event we processed | ||
current_id: the stream_ordering of the most recently processed event | ||
limit: the maximum number of events to return | ||
get_prev_content: whether to fetch previous event content | ||
|
||
Returns: | ||
A tuple of (next_id, events), where `next_id` is the next value to | ||
pass as `from_id` (it will either be the stream_ordering of the | ||
last returned event, or, if fewer than `limit` events were found, | ||
the `current_id`). | ||
A tuple of (next_id, events, event_to_received_ts), where `next_id` | ||
is the next value to pass as `from_id` (it will either be the | ||
stream_ordering of the last returned event, or, if fewer than `limit` | ||
events were found, the `current_id`). The `event_to_received_ts` is | ||
a dictionary mapping event ID to the event `received_ts`. | ||
""" | ||
|
||
def get_all_new_events_stream_txn( | ||
txn: LoggingTransaction, | ||
) -> Tuple[int, List[str]]: | ||
) -> Tuple[int, Dict[str, Optional[int]]]: | ||
sql = ( | ||
"SELECT e.stream_ordering, e.event_id" | ||
"SELECT e.stream_ordering, e.event_id, e.received_ts" | ||
" FROM events AS e" | ||
" WHERE" | ||
" ? < e.stream_ordering AND e.stream_ordering <= ?" | ||
|
@@ -1059,15 +1061,21 @@ def get_all_new_events_stream_txn( | |
if len(rows) == limit: | ||
upper_bound = rows[-1][0] | ||
|
||
return upper_bound, [row[1] for row in rows] | ||
event_to_received_ts: Dict[str, Optional[int]] = { | ||
row[1]: row[2] for row in rows | ||
} | ||
return upper_bound, event_to_received_ts | ||
|
||
upper_bound, event_ids = await self.db_pool.runInteraction( | ||
upper_bound, event_to_received_ts = await self.db_pool.runInteraction( | ||
"get_all_new_events_stream", get_all_new_events_stream_txn | ||
) | ||
|
||
events = await self.get_events_as_list(event_ids) | ||
events = await self.get_events_as_list( | ||
event_to_received_ts.keys(), | ||
get_prev_content=get_prev_content, | ||
) | ||
|
||
return upper_bound, events | ||
return upper_bound, events, event_to_received_ts | ||
|
||
async def get_federation_out_pos(self, typ: str) -> int: | ||
if self._need_to_reset_federation_stream_positions: | ||
|
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
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.
please could you give this a docstring?
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.
Added in c59d717