summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Robertson <davidr@element.io>2022-12-15 16:14:42 +0000
committerDavid Robertson <davidr@element.io>2022-12-15 16:19:02 +0000
commit0929515049435dab0e7fd3f6f5c957d1495ed225 (patch)
tree146e53e8967d042d984ec7c851be1b6ecd4542b3
parentPull out a method to start a resync (diff)
downloadsynapse-dmr/faster-joins-leave-during-resync.tar.xz
-rw-r--r--synapse/handlers/federation.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py
index 966889414c..e92fbe74b8 100644
--- a/synapse/handlers/federation.py
+++ b/synapse/handlers/federation.py
@@ -37,6 +37,8 @@ from signedjson.key import decode_verify_key_bytes
 from signedjson.sign import verify_signed_json
 from unpaddedbase64 import decode_base64
 
+from twisted.internet.defer import Deferred
+
 from synapse import event_auth
 from synapse.api.constants import MAX_DEPTH, EventContentFields, EventTypes, Membership
 from synapse.api.errors import (
@@ -179,6 +181,12 @@ class FederationHandler:
                 "resume_sync_partial_state_room", self._resume_sync_partial_state_room
             )
 
+        # The resync processes currently in progress, keyed by room ID.
+        # Keep track of these so we can cancel them and cleanup if all local users leave
+        # that room.
+        # TODO(faster_joins): actually cancel them! https://github.com/matrix-org/synapse/issues/12802
+        self._partial_state_resyncs: Dict[str, "Deferred[None]"] = {}
+
     @trace
     async def maybe_backfill(
         self, room_id: str, current_depth: int, limit: int
@@ -1630,6 +1638,9 @@ class FederationHandler:
     async def _start_partial_state_resync(
         self, origin: Optional[str], servers_in_room: List[str], room_id: str
     ) -> None:
+        # We shouldn't start a second resync if one is already in progress.
+        assert room_id not in self._partial_state_resyncs
+
         d = run_as_background_process(
             desc="sync_partial_state_room",
             func=self._sync_partial_state_room,
@@ -1637,6 +1648,8 @@ class FederationHandler:
             other_destinations=servers_in_room,
             room_id=room_id,
         )
+        self._partial_state_resyncs[room_id] = d
+
     async def _sync_partial_state_room(
         self,
         initial_destination: Optional[str],
@@ -1767,6 +1780,10 @@ class FederationHandler:
         #   https://github.com/matrix-org/synapse/issues/12814
         #   https://github.com/matrix-org/synapse/issues/12815
 
+        # This resync has completed---stop tracking its progress.
+        self._partial_state_resyncs.pop(room_id, None)
+
+
 def _prioritise_destinations_for_partial_state_resync(
     initial_destination: Optional[str],
     other_destinations: Collection[str],