Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 197fbb1

Browse files
authored
Remove legacy code of single user device resync api (#15418)
* Removed single-user resync usage and updated it to use multi-user counterpart Signed-off-by: Alok Kumar Singh [email protected]
1 parent 5e024a0 commit 197fbb1

File tree

6 files changed

+26
-122
lines changed

6 files changed

+26
-122
lines changed

changelog.d/15418.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Always use multi-user device resync replication endpoints.

synapse/handlers/device.py

+5-53
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
import logging
17-
from http import HTTPStatus
1817
from typing import (
1918
TYPE_CHECKING,
2019
Any,
@@ -921,12 +920,8 @@ class DeviceListWorkerUpdater:
921920
def __init__(self, hs: "HomeServer"):
922921
from synapse.replication.http.devices import (
923922
ReplicationMultiUserDevicesResyncRestServlet,
924-
ReplicationUserDevicesResyncRestServlet,
925923
)
926924

927-
self._user_device_resync_client = (
928-
ReplicationUserDevicesResyncRestServlet.make_client(hs)
929-
)
930925
self._multi_user_device_resync_client = (
931926
ReplicationMultiUserDevicesResyncRestServlet.make_client(hs)
932927
)
@@ -948,37 +943,7 @@ async def multi_user_device_resync(
948943
# Shortcut empty requests
949944
return {}
950945

951-
try:
952-
return await self._multi_user_device_resync_client(user_ids=user_ids)
953-
except SynapseError as err:
954-
if not (
955-
err.code == HTTPStatus.NOT_FOUND and err.errcode == Codes.UNRECOGNIZED
956-
):
957-
raise
958-
959-
# Fall back to single requests
960-
result: Dict[str, Optional[JsonDict]] = {}
961-
for user_id in user_ids:
962-
result[user_id] = await self._user_device_resync_client(user_id=user_id)
963-
return result
964-
965-
async def user_device_resync(
966-
self, user_id: str, mark_failed_as_stale: bool = True
967-
) -> Optional[JsonDict]:
968-
"""Fetches all devices for a user and updates the device cache with them.
969-
970-
Args:
971-
user_id: The user's id whose device_list will be updated.
972-
mark_failed_as_stale: Whether to mark the user's device list as stale
973-
if the attempt to resync failed.
974-
Returns:
975-
A dict with device info as under the "devices" in the result of this
976-
request:
977-
https://matrix.org/docs/spec/server_server/r0.1.2#get-matrix-federation-v1-user-devices-userid
978-
None when we weren't able to fetch the device info for some reason,
979-
e.g. due to a connection problem.
980-
"""
981-
return (await self.multi_user_device_resync([user_id]))[user_id]
946+
return await self._multi_user_device_resync_client(user_ids=user_ids)
982947

983948

984949
class DeviceListUpdater(DeviceListWorkerUpdater):
@@ -1131,7 +1096,7 @@ async def _handle_device_updates(self, user_id: str) -> None:
11311096
)
11321097

11331098
if resync:
1134-
await self.user_device_resync(user_id)
1099+
await self.multi_user_device_resync([user_id])
11351100
else:
11361101
# Simply update the single device, since we know that is the only
11371102
# change (because of the single prev_id matching the current cache)
@@ -1198,10 +1163,9 @@ async def _maybe_retry_device_resync(self) -> None:
11981163
for user_id in need_resync:
11991164
try:
12001165
# Try to resync the current user's devices list.
1201-
result = await self.user_device_resync(
1202-
user_id=user_id,
1203-
mark_failed_as_stale=False,
1204-
)
1166+
result = (await self.multi_user_device_resync([user_id], False))[
1167+
user_id
1168+
]
12051169

12061170
# user_device_resync only returns a result if it managed to
12071171
# successfully resync and update the database. Updating the table
@@ -1260,18 +1224,6 @@ async def multi_user_device_resync(
12601224

12611225
return result
12621226

1263-
async def user_device_resync(
1264-
self, user_id: str, mark_failed_as_stale: bool = True
1265-
) -> Optional[JsonDict]:
1266-
result, failed = await self._user_device_resync_returning_failed(user_id)
1267-
1268-
if failed and mark_failed_as_stale:
1269-
# Mark the remote user's device list as stale so we know we need to retry
1270-
# it later.
1271-
await self.store.mark_remote_users_device_caches_as_stale((user_id,))
1272-
1273-
return result
1274-
12751227
async def _user_device_resync_returning_failed(
12761228
self, user_id: str
12771229
) -> Tuple[Optional[JsonDict], bool]:

synapse/handlers/devicemessage.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
log_kv,
2626
set_tag,
2727
)
28-
from synapse.replication.http.devices import ReplicationUserDevicesResyncRestServlet
28+
from synapse.replication.http.devices import (
29+
ReplicationMultiUserDevicesResyncRestServlet,
30+
)
2931
from synapse.types import JsonDict, Requester, StreamKeyType, UserID, get_domain_from_id
3032
from synapse.util import json_encoder
3133
from synapse.util.stringutils import random_string
@@ -71,12 +73,12 @@ def __init__(self, hs: "HomeServer"):
7173
# sync. We do all device list resyncing on the master instance, so if
7274
# we're on a worker we hit the device resync replication API.
7375
if hs.config.worker.worker_app is None:
74-
self._user_device_resync = (
75-
hs.get_device_handler().device_list_updater.user_device_resync
76+
self._multi_user_device_resync = (
77+
hs.get_device_handler().device_list_updater.multi_user_device_resync
7678
)
7779
else:
78-
self._user_device_resync = (
79-
ReplicationUserDevicesResyncRestServlet.make_client(hs)
80+
self._multi_user_device_resync = (
81+
ReplicationMultiUserDevicesResyncRestServlet.make_client(hs)
8082
)
8183

8284
# a rate limiter for room key requests. The keys are
@@ -198,7 +200,7 @@ async def _check_for_unknown_devices(
198200
await self.store.mark_remote_users_device_caches_as_stale((sender_user_id,))
199201

200202
# Immediately attempt a resync in the background
201-
run_in_background(self._user_device_resync, user_id=sender_user_id)
203+
run_in_background(self._multi_user_device_resync, user_ids=[sender_user_id])
202204

203205
async def send_device_message(
204206
self,

synapse/handlers/federation_event.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@
7070
trace,
7171
)
7272
from synapse.metrics.background_process_metrics import run_as_background_process
73-
from synapse.replication.http.devices import ReplicationUserDevicesResyncRestServlet
73+
from synapse.replication.http.devices import (
74+
ReplicationMultiUserDevicesResyncRestServlet,
75+
)
7476
from synapse.replication.http.federation import (
7577
ReplicationFederationSendEventsRestServlet,
7678
)
@@ -167,8 +169,8 @@ def __init__(self, hs: "HomeServer"):
167169

168170
self._send_events = ReplicationFederationSendEventsRestServlet.make_client(hs)
169171
if hs.config.worker.worker_app:
170-
self._user_device_resync = (
171-
ReplicationUserDevicesResyncRestServlet.make_client(hs)
172+
self._multi_user_device_resync = (
173+
ReplicationMultiUserDevicesResyncRestServlet.make_client(hs)
172174
)
173175
else:
174176
self._device_list_updater = hs.get_device_handler().device_list_updater
@@ -1487,9 +1489,11 @@ async def _resync_device(self, sender: str) -> None:
14871489

14881490
# Immediately attempt a resync in the background
14891491
if self._config.worker.worker_app:
1490-
await self._user_device_resync(user_id=sender)
1492+
await self._multi_user_device_resync(user_ids=[sender])
14911493
else:
1492-
await self._device_list_updater.user_device_resync(sender)
1494+
await self._device_list_updater.multi_user_device_resync(
1495+
user_ids=[sender]
1496+
)
14931497
except Exception:
14941498
logger.exception("Failed to resync device for %s", sender)
14951499

synapse/replication/http/devices.py

-57
Original file line numberDiff line numberDiff line change
@@ -28,62 +28,6 @@
2828
logger = logging.getLogger(__name__)
2929

3030

31-
class ReplicationUserDevicesResyncRestServlet(ReplicationEndpoint):
32-
"""Ask master to resync the device list for a user by contacting their
33-
server.
34-
35-
This must happen on master so that the results can be correctly cached in
36-
the database and streamed to workers.
37-
38-
Request format:
39-
40-
POST /_synapse/replication/user_device_resync/:user_id
41-
42-
{}
43-
44-
Response is equivalent to ` /_matrix/federation/v1/user/devices/:user_id`
45-
response, e.g.:
46-
47-
{
48-
"user_id": "@alice:example.org",
49-
"devices": [
50-
{
51-
"device_id": "JLAFKJWSCS",
52-
"keys": { ... },
53-
"device_display_name": "Alice's Mobile Phone"
54-
}
55-
]
56-
}
57-
"""
58-
59-
NAME = "user_device_resync"
60-
PATH_ARGS = ("user_id",)
61-
CACHE = False
62-
63-
def __init__(self, hs: "HomeServer"):
64-
super().__init__(hs)
65-
66-
from synapse.handlers.device import DeviceHandler
67-
68-
handler = hs.get_device_handler()
69-
assert isinstance(handler, DeviceHandler)
70-
self.device_list_updater = handler.device_list_updater
71-
72-
self.store = hs.get_datastores().main
73-
self.clock = hs.get_clock()
74-
75-
@staticmethod
76-
async def _serialize_payload(user_id: str) -> JsonDict: # type: ignore[override]
77-
return {}
78-
79-
async def _handle_request( # type: ignore[override]
80-
self, request: Request, content: JsonDict, user_id: str
81-
) -> Tuple[int, Optional[JsonDict]]:
82-
user_devices = await self.device_list_updater.user_device_resync(user_id)
83-
84-
return 200, user_devices
85-
86-
8731
class ReplicationMultiUserDevicesResyncRestServlet(ReplicationEndpoint):
8832
"""Ask master to resync the device list for multiple users from the same
8933
remote server by contacting their server.
@@ -216,6 +160,5 @@ async def _handle_request( # type: ignore[override]
216160

217161

218162
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
219-
ReplicationUserDevicesResyncRestServlet(hs).register(http_server)
220163
ReplicationMultiUserDevicesResyncRestServlet(hs).register(http_server)
221164
ReplicationUploadKeysForUserRestServlet(hs).register(http_server)

tests/test_federation.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ def test_cross_signing_keys_retry(self) -> None:
267267
# Resync the device list.
268268
device_handler = self.hs.get_device_handler()
269269
self.get_success(
270-
device_handler.device_list_updater.user_device_resync(remote_user_id),
270+
device_handler.device_list_updater.multi_user_device_resync(
271+
[remote_user_id]
272+
),
271273
)
272274

273275
# Retrieve the cross-signing keys for this user.

0 commit comments

Comments
 (0)