mirror of
https://github.com/element-hq/synapse.git
synced 2026-04-15 03:46:39 +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.
158 lines
6.0 KiB
Python
158 lines
6.0 KiB
Python
#
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
#
|
|
# Copyright (C) 2025 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>.
|
|
#
|
|
|
|
from typing import Any as MemoryReactor # was: MemoryReactor from Twisted
|
|
|
|
from synapse.replication.tcp.streams._base import (
|
|
_STREAM_UPDATE_TARGET_ROW_COUNT,
|
|
ThreadSubscriptionsStream,
|
|
)
|
|
from synapse.server import HomeServer
|
|
from synapse.storage.database import LoggingTransaction
|
|
from synapse.util.clock import Clock
|
|
|
|
from tests.replication._base import BaseStreamTestCase
|
|
|
|
|
|
class ThreadSubscriptionsStreamTestCase(BaseStreamTestCase):
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
|
super().prepare(reactor, clock, hs)
|
|
|
|
# Postgres
|
|
def f(txn: LoggingTransaction) -> None:
|
|
txn.execute(
|
|
"""
|
|
ALTER TABLE thread_subscriptions
|
|
DROP CONSTRAINT thread_subscriptions_fk_users,
|
|
DROP CONSTRAINT thread_subscriptions_fk_rooms,
|
|
DROP CONSTRAINT thread_subscriptions_fk_events;
|
|
""",
|
|
)
|
|
|
|
self.get_success(
|
|
self.hs.get_datastores().main.db_pool.runInteraction(
|
|
"disable_foreign_keys", f
|
|
)
|
|
)
|
|
|
|
def test_thread_subscription_updates(self) -> None:
|
|
"""Test replication with thread subscription updates"""
|
|
store = self.hs.get_datastores().main
|
|
|
|
# Create thread subscription updates
|
|
updates = []
|
|
room_id = "!test_room:example.com"
|
|
|
|
# Generate several thread subscription updates
|
|
for i in range(_STREAM_UPDATE_TARGET_ROW_COUNT + 5):
|
|
thread_root_id = f"$thread_{i}:example.com"
|
|
self.get_success(
|
|
store.subscribe_user_to_thread(
|
|
"@test_user:example.org",
|
|
room_id,
|
|
thread_root_id,
|
|
automatic_event_orderings=None,
|
|
)
|
|
)
|
|
updates.append(thread_root_id)
|
|
|
|
# Also add one in a different room
|
|
other_room_id = "!other_room:example.com"
|
|
other_thread_root_id = "$other_thread:example.com"
|
|
self.get_success(
|
|
store.subscribe_user_to_thread(
|
|
"@test_user:example.org",
|
|
other_room_id,
|
|
other_thread_root_id,
|
|
automatic_event_orderings=None,
|
|
)
|
|
)
|
|
|
|
# Not yet connected: no rows should yet have been received
|
|
self.assertEqual([], self.test_handler.received_rdata_rows)
|
|
|
|
# Now reconnect to pull the updates
|
|
self.reconnect()
|
|
self.replicate()
|
|
|
|
# We should have received all the expected rows in the right order
|
|
# Filter the updates to only include thread subscription changes
|
|
received_thread_subscription_rows = [
|
|
row
|
|
for row in self.test_handler.received_rdata_rows
|
|
if row[0] == ThreadSubscriptionsStream.NAME
|
|
]
|
|
|
|
# Verify all the thread subscription updates
|
|
for thread_id in updates:
|
|
(stream_name, token, row) = received_thread_subscription_rows.pop(0)
|
|
self.assertEqual(stream_name, ThreadSubscriptionsStream.NAME)
|
|
self.assertIsInstance(row, ThreadSubscriptionsStream.ROW_TYPE)
|
|
self.assertEqual(row.user_id, "@test_user:example.org")
|
|
self.assertEqual(row.room_id, room_id)
|
|
self.assertEqual(row.event_id, thread_id)
|
|
|
|
# Verify the last update in the different room
|
|
(stream_name, token, row) = received_thread_subscription_rows.pop(0)
|
|
self.assertEqual(stream_name, ThreadSubscriptionsStream.NAME)
|
|
self.assertIsInstance(row, ThreadSubscriptionsStream.ROW_TYPE)
|
|
self.assertEqual(row.user_id, "@test_user:example.org")
|
|
self.assertEqual(row.room_id, other_room_id)
|
|
self.assertEqual(row.event_id, other_thread_root_id)
|
|
|
|
self.assertEqual([], received_thread_subscription_rows)
|
|
|
|
def test_multiple_users_thread_subscription_updates(self) -> None:
|
|
"""Test replication with thread subscription updates for multiple users"""
|
|
store = self.hs.get_datastores().main
|
|
room_id = "!test_room:example.com"
|
|
thread_root_id = "$thread_root:example.com"
|
|
|
|
# Create updates for multiple users
|
|
users = ["@user1:example.com", "@user2:example.com", "@user3:example.com"]
|
|
for user_id in users:
|
|
self.get_success(
|
|
store.subscribe_user_to_thread(
|
|
user_id, room_id, thread_root_id, automatic_event_orderings=None
|
|
)
|
|
)
|
|
|
|
# Check no rows have been received yet
|
|
self.replicate()
|
|
self.assertEqual([], self.test_handler.received_rdata_rows)
|
|
|
|
# Not yet connected: no rows should yet have been received
|
|
self.reconnect()
|
|
self.replicate()
|
|
|
|
# We should have received all the expected rows
|
|
# Filter the updates to only include thread subscription changes
|
|
received_thread_subscription_rows = [
|
|
row
|
|
for row in self.test_handler.received_rdata_rows
|
|
if row[0] == ThreadSubscriptionsStream.NAME
|
|
]
|
|
|
|
# Should have one update per user
|
|
self.assertEqual(len(received_thread_subscription_rows), len(users))
|
|
|
|
# Verify all updates
|
|
for i, user_id in enumerate(users):
|
|
(stream_name, token, row) = received_thread_subscription_rows[i]
|
|
self.assertEqual(stream_name, ThreadSubscriptionsStream.NAME)
|
|
self.assertIsInstance(row, ThreadSubscriptionsStream.ROW_TYPE)
|
|
self.assertEqual(row.user_id, user_id)
|
|
self.assertEqual(row.room_id, room_id)
|
|
self.assertEqual(row.event_id, thread_root_id)
|