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
Improve performance of getting typing updates for replication #3794
Merged
Merged
Changes from all 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 @@ | ||
Speed up calculation of typing updates for replication |
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
|
||
from synapse.api.errors import AuthError, SynapseError | ||
from synapse.types import UserID, get_domain_from_id | ||
from synapse.util.caches.stream_change_cache import StreamChangeCache | ||
from synapse.util.logcontext import run_in_background | ||
from synapse.util.metrics import Measure | ||
from synapse.util.wheel_timer import WheelTimer | ||
|
@@ -68,6 +69,11 @@ def __init__(self, hs): | |
# map room IDs to sets of users currently typing | ||
self._room_typing = {} | ||
|
||
# caches which room_ids changed at which serials | ||
self._typing_stream_change_cache = StreamChangeCache( | ||
"TypingStreamChangeCache", self._latest_room_serial, | ||
) | ||
|
||
self.clock.looping_call( | ||
self._handle_timeouts, | ||
5000, | ||
|
@@ -274,19 +280,29 @@ def _push_update_local(self, member, typing): | |
|
||
self._latest_room_serial += 1 | ||
self._room_serials[member.room_id] = self._latest_room_serial | ||
self._typing_stream_change_cache.entity_has_changed( | ||
member.room_id, self._latest_room_serial, | ||
) | ||
|
||
self.notifier.on_new_event( | ||
"typing_key", self._latest_room_serial, rooms=[member.room_id] | ||
) | ||
|
||
def get_all_typing_updates(self, last_id, current_id): | ||
# TODO: Work out a way to do this without scanning the entire state. | ||
if last_id == current_id: | ||
return [] | ||
|
||
changed_rooms = self._typing_stream_change_cache.get_all_entities_changed( | ||
last_id, | ||
) | ||
|
||
if changed_rooms is None: | ||
changed_rooms = self._room_serials | ||
|
||
rows = [] | ||
for room_id, serial in self._room_serials.items(): | ||
if last_id < serial and serial <= current_id: | ||
for room_id in changed_rooms: | ||
serial = self._room_serials[room_id] | ||
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. why do we bother to update and check room_serials as well as the stream change cache? 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. Mainly because the stream change caches are bound in size and will drop old rooms. Agreed its a bit wasteful to have both, but I'm not sure the best way of amalgamating them without causing us to send down all typing notifications for old clients. |
||
if last_id < serial <= current_id: | ||
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. TIL you can do this in python. |
||
typing = self._room_typing[room_id] | ||
rows.append((serial, room_id, list(typing))) | ||
rows.sort() | ||
|
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.
I can't see why this would happen.
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.
get_all_entities_changed
can returnNone
iflast_id
is sufficiently old?