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
Reduce federation replication traffic #2115
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
29574fd
Reduce federation presence replication traffic
erikjohnston b9b72bc
Comments
erikjohnston 6308ac4
Move get_interested_remotes back to presence handler
erikjohnston 2be8a28
Comments
erikjohnston 414522a
Move get_interested_parties
erikjohnston a8c8e4e
Comment
erikjohnston 9c712a3
Move get_presence_list_* to SlaveStore
erikjohnston c7ddb5e
Reuse get_interested_parties
erikjohnston 1745069
Comment
erikjohnston 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
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 |
---|---|---|
|
@@ -21,11 +21,11 @@ | |
|
||
from synapse.api.errors import HttpResponseException | ||
from synapse.util.async import run_on_reactor | ||
from synapse.util.logcontext import preserve_context_over_fn | ||
from synapse.util.logcontext import preserve_context_over_fn, preserve_fn | ||
from synapse.util.retryutils import NotRetryingDestination, get_retry_limiter | ||
from synapse.util.metrics import measure_func | ||
from synapse.types import get_domain_from_id | ||
from synapse.handlers.presence import format_user_presence_state | ||
from synapse.handlers.presence import format_user_presence_state, get_interested_remotes | ||
import synapse.metrics | ||
|
||
import logging | ||
|
@@ -77,8 +77,18 @@ def __init__(self, hs): | |
# destination -> list of tuple(edu, deferred) | ||
self.pending_edus_by_dest = edus = {} | ||
|
||
# Presence needs to be separate as we send single aggragate EDUs | ||
# Map of user_id -> UserPresenceState for all the pending presence | ||
# to be sent out by user_id. Entries here get processed and put in | ||
# pending_presence_by_dest | ||
self.pending_presence = {} | ||
|
||
# Map of destination -> user_id -> UserPresenceState of pending presence | ||
# to be sent to each destinations | ||
self.pending_presence_by_dest = presence = {} | ||
|
||
# Pending EDUs by their "key". Keyed EDUs are EDUs that get clobbered | ||
# based on their key (e.g. typing events by room_id) | ||
# Map of destination -> (edu_type, key) -> Edu | ||
self.pending_edus_keyed_by_dest = edus_keyed = {} | ||
|
||
metrics.register_callback( | ||
|
@@ -113,6 +123,8 @@ def __init__(self, hs): | |
self._is_processing = False | ||
self._last_poked_id = -1 | ||
|
||
self._processing_pending_presence = False | ||
|
||
def can_send_to(self, destination): | ||
"""Can we send messages to the given server? | ||
|
||
|
@@ -224,17 +236,71 @@ def _send_pdu(self, pdu, destinations): | |
self._attempt_new_transaction, destination | ||
) | ||
|
||
def send_presence(self, destination, states): | ||
if not self.can_send_to(destination): | ||
return | ||
@preserve_fn # the caller should not yield on this | ||
@defer.inlineCallbacks | ||
def send_presence(self, states): | ||
"""Send the new presence states to the appropriate destinations. | ||
|
||
self.pending_presence_by_dest.setdefault(destination, {}).update({ | ||
This actually queues up the presence states ready for sending and | ||
triggers a background task to process them and send out the transactions. | ||
|
||
Args: | ||
states (list(UserPresenceState)) | ||
""" | ||
|
||
# First we queue up the new presence by user ID, so multiple presence | ||
# updates in quick successtion are correctly handled | ||
# We only want to send presence for our own users, so lets always just | ||
# filter here just in case. | ||
self.pending_presence.update({ | ||
state.user_id: state for state in states | ||
if self.is_mine_id(state.user_id) | ||
}) | ||
|
||
preserve_context_over_fn( | ||
self._attempt_new_transaction, destination | ||
) | ||
# We then handle the new pending presence in batches, first figuring | ||
# out the destinations we need to send each state to and then poking it | ||
# to attempt a new transaction. We linearize this so that we don't | ||
# accidentally mess up the ordering and send multiple presence updates | ||
# in the wrong order | ||
if self._processing_pending_presence: | ||
return | ||
|
||
self._processing_pending_presence = True | ||
try: | ||
while True: | ||
states_map = self.pending_presence | ||
self.pending_presence = {} | ||
|
||
if not states_map: | ||
break | ||
|
||
yield self._process_presence_inner(states_map.values()) | ||
finally: | ||
self._processing_pending_presence = False | ||
|
||
@measure_func("txnqueue._process_presence") | ||
@defer.inlineCallbacks | ||
def _process_presence_inner(self, states): | ||
"""Given a list of states populate self.pending_presence_by_dest and | ||
poke to send a new transaction to each destination | ||
|
||
Args: | ||
states (list(UserPresenceState)) | ||
""" | ||
hosts_and_states = yield get_interested_remotes(self.store, states) | ||
|
||
for destinations, states in hosts_and_states: | ||
for destination in destinations: | ||
if not self.can_send_to(destination): | ||
continue | ||
|
||
self.pending_presence_by_dest.setdefault( | ||
destination, {} | ||
).update({ | ||
state.user_id: state for state in states | ||
}) | ||
|
||
preserve_fn(self._attempt_new_transaction)(destination) | ||
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. The above is copied verbatim from the old |
||
|
||
def send_edu(self, destination, edu_type, content, key=None): | ||
edu = Edu( | ||
|
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.
can you document what
presence_map
andpresence_changed
mean in the constructor so I can understand if this is a sane thing?