-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Speed up /keys/changes
#17548
Merged
Merged
Speed up /keys/changes
#17548
Changes from 2 commits
Commits
Show all changes
3 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 @@ | ||
Fix performance of device lists in `/key/changes` and sliding sync. |
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 |
---|---|---|
|
@@ -26,10 +26,11 @@ | |
|
||
from synapse.logging.opentracing import trace | ||
from synapse.storage._base import SQLBaseStore | ||
from synapse.storage.database import LoggingTransaction | ||
from synapse.storage.database import LoggingTransaction, make_in_list_sql_clause | ||
from synapse.storage.databases.main.stream import _filter_results_by_stream | ||
from synapse.types import RoomStreamToken | ||
from synapse.types import RoomStreamToken, StrCollection | ||
from synapse.util.caches.stream_change_cache import StreamChangeCache | ||
from synapse.util.iterutils import batch_iter | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -200,3 +201,62 @@ def get_current_state_deltas_for_room_txn( | |
return await self.db_pool.runInteraction( | ||
"get_current_state_deltas_for_room", get_current_state_deltas_for_room_txn | ||
) | ||
|
||
@trace | ||
async def get_current_state_deltas_for_rooms( | ||
self, | ||
room_ids: StrCollection, | ||
from_token: RoomStreamToken, | ||
to_token: RoomStreamToken, | ||
) -> List[StateDelta]: | ||
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 is just a batch version of the DB function directly above |
||
"""Get the state deltas between two tokens for the set of rooms.""" | ||
|
||
room_ids = self._curr_state_delta_stream_cache.get_entities_changed( | ||
room_ids, from_token.stream | ||
) | ||
if not room_ids: | ||
return [] | ||
|
||
def get_current_state_deltas_for_rooms_txn( | ||
txn: LoggingTransaction, | ||
room_ids: StrCollection, | ||
) -> List[StateDelta]: | ||
clause, args = make_in_list_sql_clause( | ||
self.database_engine, "room_id", room_ids | ||
) | ||
|
||
sql = f""" | ||
SELECT instance_name, stream_id, room_id, type, state_key, event_id, prev_event_id | ||
FROM current_state_delta_stream | ||
WHERE {clause} AND ? < stream_id AND stream_id <= ? | ||
ORDER BY stream_id ASC | ||
""" | ||
args.append(from_token.stream) | ||
args.append(to_token.get_max_stream_pos()) | ||
|
||
txn.execute(sql, args) | ||
|
||
return [ | ||
StateDelta( | ||
stream_id=row[1], | ||
room_id=row[2], | ||
event_type=row[3], | ||
state_key=row[4], | ||
event_id=row[5], | ||
prev_event_id=row[6], | ||
) | ||
for row in txn | ||
if _filter_results_by_stream(from_token, to_token, row[0], row[1]) | ||
] | ||
|
||
results = [] | ||
for batch in batch_iter(room_ids, 1000): | ||
deltas = await self.db_pool.runInteraction( | ||
"get_current_state_deltas_for_rooms", | ||
get_current_state_deltas_for_rooms_txn, | ||
batch, | ||
) | ||
|
||
results.extend(deltas) | ||
|
||
return results |
Oops, something went wrong.
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.
This comment is unnecessary now.
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.
at least the filter part