Compare commits

...

3 Commits

Author SHA1 Message Date
Brendan Abolivier 1a692d0e29
Apply suggestions from code review 2020-05-21 17:02:05 +02:00
Brendan Abolivier d906ca6577
Improve docs 2020-05-21 16:58:10 +02:00
Brendan Abolivier 70d17c53dc
Apply suggestion from review 2020-05-21 16:57:54 +02:00
3 changed files with 27 additions and 16 deletions

View File

@ -710,7 +710,9 @@ class DeviceListUpdater(object):
# means that if an exception is raised by this function, it must be # means that if an exception is raised by this function, it must be
# because of a database issue, which means _maybe_retry_device_resync # because of a database issue, which means _maybe_retry_device_resync
# probably won't be able to go much further anyway. # probably won't be able to go much further anyway.
result = yield self.user_device_resync(user_id=user_id) result = yield self.user_device_resync(
user_id=user_id, mark_failed_as_stale=False,
)
# user_device_resync only returns a result if it managed to successfully # user_device_resync only returns a result if it managed to successfully
# resync and update the database. Updating the table of users requiring # resync and update the database. Updating the table of users requiring
# resync isn't necessary here as user_device_resync already does it # resync isn't necessary here as user_device_resync already does it
@ -724,11 +726,13 @@ class DeviceListUpdater(object):
self._resync_retry_in_progress = False self._resync_retry_in_progress = False
@defer.inlineCallbacks @defer.inlineCallbacks
def user_device_resync(self, user_id): def user_device_resync(self, user_id, mark_failed_as_stale=True):
"""Fetches all devices for a user and updates the device cache with them. """Fetches all devices for a user and updates the device cache with them.
Args: Args:
user_id (str): The user's id whose device_list will be updated. user_id (str): The user's id whose device_list will be updated.
mark_failed_as_stale (bool): Whether to mark the user's device list as stale
if the attempt to resync failed.
Returns: Returns:
Deferred[dict]: a dict with device info as under the "devices" in the result of this Deferred[dict]: a dict with device info as under the "devices" in the result of this
request: request:
@ -740,17 +744,22 @@ class DeviceListUpdater(object):
try: try:
result = yield self.federation.query_user_devices(origin, user_id) result = yield self.federation.query_user_devices(origin, user_id)
except NotRetryingDestination: except NotRetryingDestination:
# Mark the remote user's device list as stale so we know we need to retry it if mark_failed_as_stale:
# later. # Mark the remote user's device list as stale so we know we need to retry
yield self.store.mark_remote_user_device_cache_as_stale(user_id) # it later.
yield self.store.mark_remote_user_device_cache_as_stale(user_id)
return return
except (RequestSendFailed, HttpResponseException) as e: except (RequestSendFailed, HttpResponseException) as e:
logger.warning( logger.warning(
"Failed to handle device list update for %s: %s", user_id, e, "Failed to handle device list update for %s: %s", user_id, e,
) )
# Mark the remote user's device list as stale so we know we need to retry it
# later. if mark_failed_as_stale:
yield self.store.mark_remote_user_device_cache_as_stale(user_id) # Mark the remote user's device list as stale so we know we need to retry
# it later.
yield self.store.mark_remote_user_device_cache_as_stale(user_id)
# We abort on exceptions rather than accepting the update # We abort on exceptions rather than accepting the update
# as otherwise synapse will 'forget' that its device list # as otherwise synapse will 'forget' that its device list
# is out of date. If we bail then we will retry the resync # is out of date. If we bail then we will retry the resync
@ -769,9 +778,12 @@ class DeviceListUpdater(object):
{"message": "Exception raised by federation request", "exception": e} {"message": "Exception raised by federation request", "exception": e}
) )
logger.exception("Failed to handle device list update for %s", user_id) logger.exception("Failed to handle device list update for %s", user_id)
# Mark the remote user's device list as stale so we know we need to retry it
# later. if mark_failed_as_stale:
yield self.store.mark_remote_user_device_cache_as_stale(user_id) # Mark the remote user's device list as stale so we know we need to retry
# it later.
yield self.store.mark_remote_user_device_cache_as_stale(user_id)
return return
log_kv({"result": result}) log_kv({"result": result})
stream_id = result["stream_id"] stream_id = result["stream_id"]

View File

@ -653,7 +653,8 @@ class DeviceWorkerStore(SQLBaseStore):
self, user_ids: Optional[Collection[str]] = None, self, user_ids: Optional[Collection[str]] = None,
) -> Set[str]: ) -> Set[str]:
"""Given a list of remote users return the list of users that we """Given a list of remote users return the list of users that we
should resync the device lists for. should resync the device lists for. If None is given instead of a list,
return every user that we should resync the device lists for.
Returns: Returns:
The IDs of users whose device lists need resync. The IDs of users whose device lists need resync.

View File

@ -168,14 +168,12 @@ class MessageAcceptTests(unittest.HomeserverTestCase):
# Register the mock on the federation client. # Register the mock on the federation client.
federation_client = self.homeserver.get_federation_client() federation_client = self.homeserver.get_federation_client()
federation_client.query_user_devices = Mock() federation_client.query_user_devices = Mock(side_effect=query_user_devices)
federation_client.query_user_devices.side_effect = query_user_devices
# Register a mock on the store so that the incoming update doesn't fail because # Register a mock on the store so that the incoming update doesn't fail because
# we don't share a room with the user. # we don't share a room with the user.
store = self.homeserver.get_datastore() store = self.homeserver.get_datastore()
store.get_rooms_for_user = Mock() store.get_rooms_for_user = Mock(return_value=["!someroom:test"])
store.get_rooms_for_user.return_value = ["!someroom:test"]
# Manually inject a fake device list update. We need this update to include at # Manually inject a fake device list update. We need this update to include at
# least one prev_id so that the user's device list will need to be retried. # least one prev_id so that the user's device list will need to be retried.