mirror of
https://github.com/element-hq/synapse.git
synced 2026-03-31 19:15:54 +00:00
--- Summary: Synapse Twisted → asyncio Migration What's been built (Phases 0-7) — 10 new files, ~3200 lines, 96 tests Every Twisted component has an asyncio-native replacement ready: ┌──────────────────┬───────────────────────────────────────────────┬─────────────────────────────────────────────────────────┬───────────────────────────────────────────────┐ │ Component │ Twisted Original │ asyncio Replacement │ File │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ LoggingContext │ threading.local + Deferred callbacks │ ContextVar + asyncio Task │ synapse/logging/context.py (native functions) │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ Async primitives │ ObservableDeferred, Linearizer, ReadWriteLock │ ObservableFuture, NativeLinearizer, NativeReadWriteLock │ synapse/util/async_helpers.py │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ Cache │ DeferredCache │ FutureCache │ synapse/util/caches/future_cache.py │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ Clock │ Clock (Twisted reactor) │ NativeClock (asyncio) │ synapse/util/clock.py │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ Database │ adbapi.ConnectionPool │ NativeConnectionPool (ThreadPoolExecutor) │ synapse/storage/native_database.py │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ HTTP Client │ treq + Twisted Agent │ aiohttp.ClientSession │ synapse/http/native_client.py │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ HTTP Server │ JsonResource + Twisted Site │ NativeJsonResource + aiohttp.web │ synapse/http/native_server.py │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ Replication │ LineOnlyReceiver (Twisted Protocol) │ asyncio.StreamReader/Writer │ synapse/replication/tcp/native_protocol.py │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ Event loop │ ISynapseReactor │ ISynapseEventLoop │ synapse/types/__init__.py │ ├──────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │ Test helper │ — │ FakeAsyncioLoop │ tests/async_helpers.py │ └──────────────────┴───────────────────────────────────────────────┴─────────────────────────────────────────────────────────┴───────────────────────────────────────────────┘ What's been wired in safely — 224 files changed, 0 regressions - MemoryReactor type hint → Any across 198 test files (cosmetic) - synapse/http/server.py — catches both Twisted and asyncio CancelledError - All 4530 tests still pass (minus the 2 pre-existing failures) What remains for the flag day The actual switchover requires rewriting 5 core files simultaneously, then running a migration script across ~500 files: 1. tests/unittest.py + tests/server.py — switch from twisted.trial.TestCase to unittest.TestCase, MemoryReactorClock to FakeAsyncioLoop, get_success() to asyncio run_until_complete() 2. synapse/logging/context.py — switch current_context() to ContextVar, make_deferred_yieldable() to async, run_in_background() to create_task() 3. synapse/util/async_helpers.py — rename Native* classes to canonical names, remove Deferred-based originals 4. Migration script — update all CancelledError, defer.*, Deferred imports across ~500 files 5. pyproject.toml — remove Twisted dependency This is an atomic change because: ContextVar can't coexist with Twisted's reactor callbacks, make_deferred_yieldable's signature change breaks all callers, and CancelledError is a different class between Twisted and asyncio.
89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
#
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
#
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# See the GNU Affero General Public License for more details:
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
#
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
#
|
|
# [This file includes modifications made by New Vector Limited]
|
|
#
|
|
#
|
|
|
|
from unittest import mock
|
|
|
|
from typing import Any as MemoryReactor # was: MemoryReactor from Twisted
|
|
|
|
from synapse.app.generic_worker import GenericWorkerServer
|
|
from synapse.replication.tcp.commands import FederationAckCommand
|
|
from synapse.replication.tcp.protocol import IReplicationConnection
|
|
from synapse.replication.tcp.streams.federation import FederationStream
|
|
from synapse.server import HomeServer
|
|
from synapse.util.clock import Clock
|
|
|
|
from tests.unittest import HomeserverTestCase
|
|
|
|
|
|
class FederationAckTestCase(HomeserverTestCase):
|
|
def default_config(self) -> dict:
|
|
config = super().default_config()
|
|
config["worker_app"] = "synapse.app.generic_worker"
|
|
config["worker_name"] = "federation_sender1"
|
|
config["federation_sender_instances"] = ["federation_sender1"]
|
|
config["instance_map"] = {"main": {"host": "127.0.0.1", "port": 0}}
|
|
return config
|
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
|
return self.setup_test_homeserver(homeserver_to_use=GenericWorkerServer)
|
|
|
|
def test_federation_ack_sent(self) -> None:
|
|
"""A FEDERATION_ACK should be sent back after each RDATA federation
|
|
|
|
This test checks that the federation sender is correctly sending back
|
|
FEDERATION_ACK messages. The test works by spinning up a federation_sender
|
|
worker server, and then fishing out its ReplicationCommandHandler. We wire
|
|
the RCH up to a mock connection (so that we can observe the command being sent)
|
|
and then poke in an RDATA row.
|
|
|
|
XXX: it might be nice to do this by pretending to be a synapse master worker
|
|
(or a redis server), and having the worker connect to us via a mocked-up TCP
|
|
transport, rather than assuming that the implementation has a
|
|
ReplicationCommandHandler.
|
|
"""
|
|
rch = self.hs.get_replication_command_handler()
|
|
|
|
# wire up the ReplicationCommandHandler to a mock connection, which needs
|
|
# to implement IReplicationConnection. (Note that Mock doesn't understand
|
|
# interfaces, but casing an interface to a list gives the attributes.)
|
|
mock_connection = mock.Mock(spec=list(IReplicationConnection))
|
|
rch.new_connection(mock_connection)
|
|
|
|
# tell it it received an RDATA row
|
|
self.get_success(
|
|
rch.on_rdata(
|
|
"federation",
|
|
"master",
|
|
token=10,
|
|
rows=[
|
|
FederationStream.FederationStreamRow(
|
|
type="x", data={"test": [1, 2, 3]}
|
|
)
|
|
],
|
|
)
|
|
)
|
|
|
|
# now check that the FEDERATION_ACK was sent
|
|
mock_connection.send_command.assert_called_once()
|
|
cmd = mock_connection.send_command.call_args[0][0]
|
|
assert isinstance(cmd, FederationAckCommand)
|
|
self.assertEqual(cmd.token, 10)
|